Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 174877f

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 002c9dd commit 174877f

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

contrib/amcheck/t/001_verify_heapam.pl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PostgreSQL::Test::Cluster;
88
use PostgreSQL::Test::Utils;
99

10-
use Fcntl qw(:seek);
1110
use Test::More;
1211

1312
my ($node, $result);
@@ -193,8 +192,8 @@ sub corrupt_first_page
193192
# Corrupt some line pointers. The values are chosen to hit the
194193
# various line-pointer-corruption checks in verify_heapam.c
195194
# on both little-endian and big-endian architectures.
196-
seek($fh, 32, SEEK_SET)
197-
or BAIL_OUT("seek failed: $!");
195+
sysseek($fh, 32, 0)
196+
or BAIL_OUT("sysseek failed: $!");
198197
syswrite(
199198
$fh,
200199
pack("L*",

src/bin/pg_amcheck/t/003_check.pl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PostgreSQL::Test::Cluster;
88
use PostgreSQL::Test::Utils;
99

10-
use Fcntl qw(:seek);
1110
use Test::More;
1211

1312
my ($node, $port, %corrupt_page, %remove_relation);
@@ -90,8 +89,8 @@ sub corrupt_first_page
9089
# Corrupt some line pointers. The values are chosen to hit the
9190
# various line-pointer-corruption checks in verify_heapam.c
9291
# on both little-endian and big-endian architectures.
93-
seek($fh, 32, SEEK_SET)
94-
or BAIL_OUT("seek failed: $!");
92+
sysseek($fh, 32, 0)
93+
or BAIL_OUT("sysseek failed: $!");
9594
syswrite(
9695
$fh,
9796
pack("L*",

src/bin/pg_amcheck/t/004_verify_heapam.pl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PostgreSQL::Test::Cluster;
88
use PostgreSQL::Test::Utils;
99

10-
use Fcntl qw(:seek);
1110
use Test::More;
1211

1312
# This regression test demonstrates that the pg_amcheck binary correctly
@@ -99,8 +98,8 @@ sub read_tuple
9998
{
10099
my ($fh, $offset) = @_;
101100
my ($buffer, %tup);
102-
seek($fh, $offset, SEEK_SET)
103-
or BAIL_OUT("seek failed: $!");
101+
sysseek($fh, $offset, 0)
102+
or BAIL_OUT("sysseek failed: $!");
104103
defined(sysread($fh, $buffer, HEAPTUPLE_PACK_LENGTH))
105104
or BAIL_OUT("sysread failed: $!");
106105

@@ -165,8 +164,8 @@ sub write_tuple
165164
$tup->{c_va_header}, $tup->{c_va_vartag},
166165
$tup->{c_va_rawsize}, $tup->{c_va_extinfo},
167166
$tup->{c_va_valueid}, $tup->{c_va_toastrelid});
168-
seek($fh, $offset, SEEK_SET)
169-
or BAIL_OUT("seek failed: $!");
167+
sysseek($fh, $offset, 0)
168+
or BAIL_OUT("sysseek failed: $!");
170169
defined(syswrite($fh, $buffer, HEAPTUPLE_PACK_LENGTH))
171170
or BAIL_OUT("syswrite failed: $!");
172171
return;

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use warnings;
66
use File::Basename qw(basename dirname);
77
use File::Path qw(rmtree);
8-
use Fcntl qw(:seek);
98
use PostgreSQL::Test::Cluster;
109
use PostgreSQL::Test::Utils;
1110
use Test::More;
@@ -56,7 +55,7 @@
5655
}
5756

5857
$node->set_replication_conf();
59-
system_or_bail 'pg_ctl', '-D', $pgdata, 'reload';
58+
$node->reload;
6059

6160
$node->command_fails(
6261
[ @pg_basebackup_defs, '-D', "$tempdir/backup" ],
@@ -706,17 +705,13 @@
706705
q{CREATE TABLE corrupt2 AS SELECT b FROM generate_series(1,2) AS b; ALTER TABLE corrupt2 SET (autovacuum_enabled=false); SELECT pg_relation_filepath('corrupt2')}
707706
);
708707

709-
# set page header and block sizes
710-
my $pageheader_size = 24;
708+
# get block size for corruption steps
711709
my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
712710

713711
# induce corruption
714-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
715-
open $file, '+<', "$pgdata/$file_corrupt1";
716-
seek($file, $pageheader_size, SEEK_SET);
717-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
718-
close $file;
719-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
712+
$node->stop;
713+
$node->corrupt_page_checksum($file_corrupt1, 0);
714+
$node->start;
720715

721716
$node->command_checks_all(
722717
[ @pg_basebackup_defs, '-D', "$tempdir/backup_corrupt" ],
@@ -727,16 +722,12 @@
727722
rmtree("$tempdir/backup_corrupt");
728723

729724
# induce further corruption in 5 more blocks
730-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
731-
open $file, '+<', "$pgdata/$file_corrupt1";
725+
$node->stop;
732726
for my $i (1 .. 5)
733727
{
734-
my $offset = $pageheader_size + $i * $block_size;
735-
seek($file, $offset, SEEK_SET);
736-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
728+
$node->corrupt_page_checksum($file_corrupt1, $i * $block_size);
737729
}
738-
close $file;
739-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
730+
$node->start;
740731

741732
$node->command_checks_all(
742733
[ @pg_basebackup_defs, '-D', "$tempdir/backup_corrupt2" ],
@@ -747,12 +738,9 @@
747738
rmtree("$tempdir/backup_corrupt2");
748739

749740
# induce corruption in a second file
750-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
751-
open $file, '+<', "$pgdata/$file_corrupt2";
752-
seek($file, $pageheader_size, SEEK_SET);
753-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
754-
close $file;
755-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
741+
$node->stop;
742+
$node->corrupt_page_checksum($file_corrupt2, 0);
743+
$node->start;
756744

757745
$node->command_checks_all(
758746
[ @pg_basebackup_defs, '-D', "$tempdir/backup_corrupt3" ],

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PostgreSQL::Test::Cluster;
1010
use PostgreSQL::Test::Utils;
1111

12-
use Fcntl qw(:seek);
1312
use Test::More;
1413

1514

@@ -24,6 +23,7 @@ sub check_relation_corruption
2423
my $tablespace = shift;
2524
my $pgdata = $node->data_dir;
2625

26+
# Create table and discover its filesystem location.
2727
$node->safe_psql(
2828
'postgres',
2929
"CREATE TABLE $table AS SELECT a FROM generate_series(1,10000) AS a;
@@ -37,9 +37,6 @@ sub check_relation_corruption
3737
my $relfilenode_corrupted = $node->safe_psql('postgres',
3838
"SELECT relfilenode FROM pg_class WHERE relname = '$table';");
3939

40-
# Set page header and block size
41-
my $pageheader_size = 24;
42-
my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
4340
$node->stop;
4441

4542
# Checksums are correct for single relfilenode as the table is not
@@ -54,10 +51,7 @@ sub check_relation_corruption
5451
);
5552

5653
# Time to create some corruption
57-
open my $file, '+<', "$pgdata/$file_corrupted";
58-
seek($file, $pageheader_size, SEEK_SET);
59-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
60-
close $file;
54+
$node->corrupt_page_checksum($file_corrupted, 0);
6155

6256
# Checksum checks on single relfilenode fail
6357
$node->command_checks_all(

src/test/perl/PostgreSQL/Test/Cluster.pm

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

28572857
=pod
28582858
2859+
=item $node->corrupt_page_checksum(self, file, page_offset)
2860+
2861+
Intentionally corrupt the checksum field of one page in a file.
2862+
The server must be stopped for this to work reliably.
2863+
2864+
The file name should be specified relative to the cluster datadir.
2865+
page_offset had better be a multiple of the cluster's block size.
2866+
2867+
=cut
2868+
2869+
sub corrupt_page_checksum
2870+
{
2871+
my ($self, $file, $page_offset) = @_;
2872+
my $pgdata = $self->data_dir;
2873+
my $pageheader;
2874+
2875+
open my $fh, '+<', "$pgdata/$file" or die "open($file) failed: $!";
2876+
binmode $fh;
2877+
sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
2878+
sysread($fh, $pageheader, 24) or die "sysread failed: $!";
2879+
# This inverts the pd_checksum field (only); see struct PageHeaderData
2880+
$pageheader ^= "\0\0\0\0\0\0\0\0\xff\xff";
2881+
sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
2882+
syswrite($fh, $pageheader) or die "syswrite failed: $!";
2883+
close $fh;
2884+
2885+
return;
2886+
}
2887+
2888+
=pod
2889+
28592890
=back
28602891
28612892
=cut

0 commit comments

Comments
 (0)