]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Harden TAP tests that intentionally corrupt page checksums.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 Mar 2022 18:23:26 +0000 (14:23 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 Mar 2022 18:23:26 +0000 (14:23 -0400)
The previous method for doing that was to write zeroes into a
predetermined set of page locations.  However, there's a roughly
1-in-64K chance that the existing checksum will match by chance,
and yesterday several buildfarm animals started to reproducibly
see that, resulting in test failures because no checksum mismatch
was reported.

Since the checksum includes the page LSN, test success depends on
the length of the installation's WAL history, which is affected by
(at least) the initial catalog contents, the set of locales installed
on the system, and the length of the pathname of the test directory.
Sooner or later we were going to hit a chance match, and today is
that day.

Harden these tests by specifically inverting the checksum field and
leaving all else alone, thereby guaranteeing that the checksum is
incorrect.

In passing, fix places that were using seek() to set up for syswrite(),
a combination that the Perl docs very explicitly warn against.  We've
probably escaped problems because no regular buffered I/O is done on
these filehandles; but if it ever breaks, we wouldn't deserve or get
much sympathy.

Although we've only seen problems in HEAD, now that we recognize the
environmental dependencies it seems like it might be just a matter
of time until someone manages to hit this in back-branch testing.
Hence, back-patch to v11 where we started doing this kind of test.

Discussion: https://postgr.es/m/3192026.1648185780@sss.pgh.pa.us

src/bin/pg_basebackup/t/010_pg_basebackup.pl
src/test/perl/PostgresNode.pm

index 851a7db52f733869e98ca90d7edd1243ead6a51d..29d0ae0a42c243c52a7350ec72b7043263b888ca 100644 (file)
@@ -36,7 +36,7 @@ if (open my $badchars, '>>', "$tempdir/pgdata/FOO\xe0\xe0\xe0BAR")
 }
 
 $node->set_replication_conf();
-system_or_bail 'pg_ctl', '-D', $pgdata, 'reload';
+$node->reload;
 
 $node->command_fails(
        [ 'pg_basebackup', '-D', "$tempdir/backup" ],
@@ -494,17 +494,13 @@ my $file_corrupt2 = $node->safe_psql('postgres',
        q{SELECT b INTO corrupt2 FROM generate_series(1,2) AS b; ALTER TABLE corrupt2 SET (autovacuum_enabled=false); SELECT pg_relation_filepath('corrupt2')}
 );
 
-# set page header and block sizes
-my $pageheader_size = 24;
+# get block size for corruption steps
 my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
 
 # induce corruption
-system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
-open $file, '+<', "$pgdata/$file_corrupt1";
-seek($file, $pageheader_size, 0);
-syswrite($file, "\0\0\0\0\0\0\0\0\0");
-close $file;
-system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
+$node->stop;
+$node->corrupt_page_checksum($file_corrupt1, 0);
+$node->start;
 
 $node->command_checks_all(
        [ 'pg_basebackup', '-D', "$tempdir/backup_corrupt" ],
@@ -515,16 +511,12 @@ $node->command_checks_all(
 rmtree("$tempdir/backup_corrupt");
 
 # induce further corruption in 5 more blocks
-system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
-open $file, '+<', "$pgdata/$file_corrupt1";
+$node->stop;
 for my $i (1 .. 5)
 {
-       my $offset = $pageheader_size + $i * $block_size;
-       seek($file, $offset, 0);
-       syswrite($file, "\0\0\0\0\0\0\0\0\0");
+       $node->corrupt_page_checksum($file_corrupt1, $i * $block_size);
 }
-close $file;
-system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
+$node->start;
 
 $node->command_checks_all(
        [ 'pg_basebackup', '-D', "$tempdir/backup_corrupt2" ],
@@ -535,12 +527,9 @@ $node->command_checks_all(
 rmtree("$tempdir/backup_corrupt2");
 
 # induce corruption in a second file
-system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
-open $file, '+<', "$pgdata/$file_corrupt2";
-seek($file, $pageheader_size, 0);
-syswrite($file, "\0\0\0\0\0\0\0\0\0");
-close $file;
-system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
+$node->stop;
+$node->corrupt_page_checksum($file_corrupt2, 0);
+$node->start;
 
 $node->command_checks_all(
        [ 'pg_basebackup', '-D', "$tempdir/backup_corrupt3" ],
index 2816284aa021c6a5c1c96a022bd0d61f83e0d002..0c63c6062c7e819cabcb1df2b385815a09cf94ce 100644 (file)
@@ -2227,6 +2227,37 @@ sub pg_recvlogical_upto
 
 =pod
 
+=item $node->corrupt_page_checksum(self, file, page_offset)
+
+Intentionally corrupt the checksum field of one page in a file.
+The server must be stopped for this to work reliably.
+
+The file name should be specified relative to the cluster datadir.
+page_offset had better be a multiple of the cluster's block size.
+
+=cut
+
+sub corrupt_page_checksum
+{
+       my ($self, $file, $page_offset) = @_;
+       my $pgdata = $self->data_dir;
+       my $pageheader;
+
+       open my $fh, '+<', "$pgdata/$file" or die "open($file) failed: $!";
+       binmode $fh;
+       sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
+       sysread($fh, $pageheader, 24) or die "sysread failed: $!";
+       # This inverts the pd_checksum field (only); see struct PageHeaderData
+       $pageheader ^= "\0\0\0\0\0\0\0\0\xff\xff";
+       sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
+       syswrite($fh, $pageheader) or die "syswrite failed: $!";
+       close $fh;
+
+       return;
+}
+
+=pod
+
 =back
 
 =cut