Skip to content

Commit d09f765

Browse files
committed
Harden TAP tests that intentionally corrupt page checksums.
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
1 parent 3821d66 commit d09f765

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

src/bin/pg_basebackup/t/010_pg_basebackup.pl

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}
3737

3838
$node->set_replication_conf();
39-
system_or_bail 'pg_ctl', '-D', $pgdata, 'reload';
39+
$node->reload;
4040

4141
$node->command_fails(
4242
[ 'pg_basebackup', '-D', "$tempdir/backup" ],
@@ -491,17 +491,13 @@
491491
q{SELECT b INTO corrupt2 FROM generate_series(1,2) AS b; ALTER TABLE corrupt2 SET (autovacuum_enabled=false); SELECT pg_relation_filepath('corrupt2')}
492492
);
493493

494-
# set page header and block sizes
495-
my $pageheader_size = 24;
494+
# get block size for corruption steps
496495
my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
497496

498497
# induce corruption
499-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
500-
open $file, '+<', "$pgdata/$file_corrupt1";
501-
seek($file, $pageheader_size, 0);
502-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
503-
close $file;
504-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
498+
$node->stop;
499+
$node->corrupt_page_checksum($file_corrupt1, 0);
500+
$node->start;
505501

506502
$node->command_checks_all(
507503
[ 'pg_basebackup', '-D', "$tempdir/backup_corrupt" ],
@@ -512,16 +508,12 @@
512508
rmtree("$tempdir/backup_corrupt");
513509

514510
# induce further corruption in 5 more blocks
515-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
516-
open $file, '+<', "$pgdata/$file_corrupt1";
511+
$node->stop;
517512
for my $i (1 .. 5)
518513
{
519-
my $offset = $pageheader_size + $i * $block_size;
520-
seek($file, $offset, 0);
521-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
514+
$node->corrupt_page_checksum($file_corrupt1, $i * $block_size);
522515
}
523-
close $file;
524-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
516+
$node->start;
525517

526518
$node->command_checks_all(
527519
[ 'pg_basebackup', '-D', "$tempdir/backup_corrupt2" ],
@@ -532,12 +524,9 @@
532524
rmtree("$tempdir/backup_corrupt2");
533525

534526
# induce corruption in a second file
535-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
536-
open $file, '+<', "$pgdata/$file_corrupt2";
537-
seek($file, $pageheader_size, 0);
538-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
539-
close $file;
540-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
527+
$node->stop;
528+
$node->corrupt_page_checksum($file_corrupt2, 0);
529+
$node->start;
541530

542531
$node->command_checks_all(
543532
[ 'pg_basebackup', '-D', "$tempdir/backup_corrupt3" ],

src/bin/pg_checksums/t/002_actions.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sub check_relation_corruption
1919
my $tablespace = shift;
2020
my $pgdata = $node->data_dir;
2121

22+
# Create table and discover its filesystem location.
2223
$node->safe_psql(
2324
'postgres',
2425
"SELECT a INTO $table FROM generate_series(1,10000) AS a;
@@ -32,9 +33,6 @@ sub check_relation_corruption
3233
my $relfilenode_corrupted = $node->safe_psql('postgres',
3334
"SELECT relfilenode FROM pg_class WHERE relname = '$table';");
3435

35-
# Set page header and block size
36-
my $pageheader_size = 24;
37-
my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
3836
$node->stop;
3937

4038
# Checksums are correct for single relfilenode as the table is not
@@ -49,10 +47,7 @@ sub check_relation_corruption
4947
);
5048

5149
# Time to create some corruption
52-
open my $file, '+<', "$pgdata/$file_corrupted";
53-
seek($file, $pageheader_size, 0);
54-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
55-
close $file;
50+
$node->corrupt_page_checksum($file_corrupted, 0);
5651

5752
# Checksum checks on single relfilenode fail
5853
$node->command_checks_all(

src/test/perl/PostgresNode.pm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,37 @@ sub pg_recvlogical_upto
22372237

22382238
=pod
22392239
2240+
=item $node->corrupt_page_checksum(self, file, page_offset)
2241+
2242+
Intentionally corrupt the checksum field of one page in a file.
2243+
The server must be stopped for this to work reliably.
2244+
2245+
The file name should be specified relative to the cluster datadir.
2246+
page_offset had better be a multiple of the cluster's block size.
2247+
2248+
=cut
2249+
2250+
sub corrupt_page_checksum
2251+
{
2252+
my ($self, $file, $page_offset) = @_;
2253+
my $pgdata = $self->data_dir;
2254+
my $pageheader;
2255+
2256+
open my $fh, '+<', "$pgdata/$file" or die "open($file) failed: $!";
2257+
binmode $fh;
2258+
sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
2259+
sysread($fh, $pageheader, 24) or die "sysread failed: $!";
2260+
# This inverts the pd_checksum field (only); see struct PageHeaderData
2261+
$pageheader ^= "\0\0\0\0\0\0\0\0\xff\xff";
2262+
sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
2263+
syswrite($fh, $pageheader) or die "syswrite failed: $!";
2264+
close $fh;
2265+
2266+
return;
2267+
}
2268+
2269+
=pod
2270+
22402271
=back
22412272
22422273
=cut

0 commit comments

Comments
 (0)