Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qcFSa-00ESgr-78 for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Sep 2023 01:30:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qcFSY-001lAp-Qv for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Sep 2023 01:29:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qcFSX-001lA6-2g for pgsql-bugs@lists.postgresql.org; Sat, 02 Sep 2023 01:29:58 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qcFSQ-002FbE-85 for pgsql-bugs@lists.postgresql.org; Sat, 02 Sep 2023 01:29:55 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 0D8725C0145; Fri, 1 Sep 2023 21:29:49 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 01 Sep 2023 21:29:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm1; t=1693618189; x=1693704589; bh=g3 yc5gE711T6Rdya18ctQLPTWNHvbbVm3GkBKW/ZKpk=; b=ggMgf5qIXb1nLVI2Lc 1En2vUS+gI66WOjFHGBo8eMSGsTaTd+/lHxwit2I5ro+NYMVfDIStp3f9VwxC05G q6ZoUwAoBiZjPtrezih0k6QYzLNu4du/GUMIG5ZBaOJnESOgNB8S28+FnAR43n1I 2KhrMjdJoZfRhV2L2e5PdGy3VokW9+vo0EpvpXOhZwmFNKncZnC1NfBV2B3bnUr6 a4ISlgnYb1g+LEtJ0pN1Rx/4J7rbfRSj0wtnZhBvlyMf+wRv+CLgUbv2UDMxVM7S W/K1lP6GwaWk436J0TiJUDqpqjv2wZz39UtNx+0hNVHgkP9EN7xo1NzpNqMpx28s eWmA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1693618189; x=1693704589; bh=g3yc5gE711T6R dya18ctQLPTWNHvbbVm3GkBKW/ZKpk=; b=wChYGovb+dJ3gOHSfaV6Yy+Z0NAGq HDFN4VcaFjLtvJn+VQw9OUjYc76nFOwmN5uiDJCgZ6vR4WR0ofCWU5v/vWe3aW9k 8Lven9UFUx6FEifYW+Wi1wkpMp0Ns3WVZ2UIIpEx3CcX0pb0nBhv/OKFe6mv/Nig fLOwWAzQQKdbWYIqiWqxlAq6Jwsc5Af0C7+bBqqfDTt2v4jP/NWO8rpXfMZqyXLk TJxM3xm82i1afiWCGVgpjheiJ0vCM+0uULukX9n63P+nDRJ5apIJXEidVdXFrmEE esn+gjLAEIL+ES9+OhlniNDGovbRqHl+r6dzBeu0hDhpl0bkN5SOEYLyw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudegfedggeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredt tddvnecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesph grqhhuihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepteelieefudffhffhtdetleeg geegfffhkeeuveetiefgudduvedutefggeeivdejnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiih ii X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Sep 2023 21:29:45 -0400 (EDT) Date: Sat, 2 Sep 2023 10:29:39 +0900 From: Michael Paquier To: Thomas Munro Cc: Noah Misch , Tom Lane , Kyotaro Horiguchi , exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17928: Standby fails to decode WAL on termination of primary Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="DzGwaSfRGfsjpLnV" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --DzGwaSfRGfsjpLnV Content-Type: multipart/mixed; boundary="KrQNqFl8Q8LiCsP7" Content-Disposition: inline --KrQNqFl8Q8LiCsP7 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Aug 21, 2023 at 08:32:39AM +0900, Michael Paquier wrote: > I am not sure that I'll be able to do more on this topic this week, at > least that's some progress. Some time later.. I have spent more time and thoughts on the whole test suite, finishing with the attached 0003 that applies on top of your own patches. I am really looking forward to making this whole logic more robust, so as WAL replay can be made itself more robust for the OOM/end-of-wal detection on HEAD for standbys and crash recovery. While looking at the whole, I have considered a few things that may make the test cleaner, like: - Calculating the segment name and its offset from the end_lsn of a record directly from the backend, but it felt inelegant to pass through more subroutine layers the couple ($segment, offset) rather than just a LSN, so guessing the segment number and the offset while the cluster is offline if OK by me. - The TLI can be queried from the server rather than hardcoded. - I've been thinking about bundling the tests of each sub-section in their own subroutine, but that felt a bit awkward, particularly for the part where we need a correct $prev_lsn in the record header written to enforce other code paths. - The test needs better documentation. One of the things I kept staring at is cross-checking pack() and the dependency to the C structures, so I have added more details there, explaining more the whys and the hows. I have also looked again at the C code for a few hours, and still got the impression that this is rather solid. There are two things that may be better: - Document at the top of allocate_recordbuf() that this should never be called with a length coming from a header until it is validated. - Removing AllocSizeIsValid() for the non-FRONTEND path should be OK. What do you think? -- Michael --KrQNqFl8Q8LiCsP7 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Add-TAP-tests-for-end-of-WAL-conditions.patch" Content-Transfer-Encoding: quoted-printable =46rom 8a8dd8252dd3b096d1c652ab78671d6457ecc95a Mon Sep 17 00:00:00 2001 =46rom: Thomas Munro Date: Tue, 15 Aug 2023 17:37:53 +1200 Subject: [PATCH v3 1/3] Add TAP tests for end-of-WAL conditions. XXX --- src/test/recovery/meson.build | 1 + src/test/recovery/t/038_end_of_wal.pl | 415 ++++++++++++++++++++++++++ 2 files changed, 416 insertions(+) create mode 100644 src/test/recovery/t/038_end_of_wal.pl diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index e7328e4894..10407668d4 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -43,6 +43,7 @@ tests +=3D { 't/035_standby_logical_decoding.pl', 't/036_truncated_dropped.pl', 't/037_invalid_database.pl', + 't/038_end_of_wal.pl', ], }, } diff --git a/src/test/recovery/t/038_end_of_wal.pl b/src/test/recovery/t/03= 8_end_of_wal.pl new file mode 100644 index 0000000000..fefb5b6034 --- /dev/null +++ b/src/test/recovery/t/038_end_of_wal.pl @@ -0,0 +1,415 @@ +# Copyright (c) 2023, PostgreSQL Global Development Group +# +# Test ways of detecting end of WAL. + +use strict; +use warnings; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; +use Fcntl qw(SEEK_SET); + +use integer; # causes / operator to use integer math + +# Values that could be extracted from the source tree +my $RECORD_HEADER_SIZE =3D 24; +my $XLP_PAGE_MAGIC =3D 0xd113; +my $XLP_FIRST_IS_CONTRECORD =3D 0x1; + +# Values queried from the server +my $WAL_SEGMENT_SIZE; +my $WAL_BLOCK_SIZE; + +sub wal_segment_path +{ + my $node =3D shift; + my $tli =3D shift; + my $segment =3D shift; + my $wal_path =3D sprintf("%s/pg_wal/%08X%08X%08X", + $node->data_dir, + $tli, + 0, + $segment); + return $wal_path; +} + +sub lsn_to_segment_and_offset +{ + my $lsn =3D shift; + return ($lsn / $WAL_SEGMENT_SIZE, $lsn % $WAL_SEGMENT_SIZE); +} + +sub get_log_size +{ + my $node =3D shift; + return (stat $node->logfile)[7]; +} + +sub write_wal +{ + my $node =3D shift; + my $tli =3D shift; + my $lsn =3D shift; + my $data =3D shift; + + my ($segment, $offset) =3D lsn_to_segment_and_offset($lsn); + my $path =3D wal_segment_path($node, $tli, $segment); + + open my $fh, "+<:raw", $path or die; + seek($fh, $offset, SEEK_SET) or die; + print $fh $data; + close $fh; +} + +sub emit_message +{ + my $node =3D shift; + my $size =3D shift; + return int($node->safe_psql('postgres', "select pg_logical_emit_message(t= rue, '', repeat('a', $size)) - '0/0'")); +} + +sub get_insert_lsn +{ + my $node =3D shift; + return int($node->safe_psql('postgres', "select pg_current_wal_insert_lsn= () - '0/0'")); +} + +sub get_int_setting +{ + my $node =3D shift; + my $name =3D shift; + return int($node->safe_psql('postgres', "select setting from pg_settings = where name =3D '$name'")); +} + +sub format_lsn +{ + my $lsn =3D shift; + return sprintf("%X/%X", $lsn >> 32, $lsn & 0xffffffff); +} + +sub start_of_page +{ + my $lsn =3D shift; + return $lsn & ~($WAL_BLOCK_SIZE - 1); +} + +sub start_of_next_page +{ + my $lsn =3D shift; + return start_of_page($lsn) + $WAL_BLOCK_SIZE; +} + +sub record_header +{ + my $xl_tot_len =3D shift; + my $xl_xid =3D shift || 0; + my $xl_prev =3D shift || 0; + my $xl_info =3D shift || 0; + my $xl_rmid =3D shift || 0; + my $xl_crc =3D shift || 0; + + # struct XLogRecord + return pack("IIQCCBBI", + $xl_tot_len, + $xl_xid, + $xl_prev, + $xl_info, + $xl_rmid, + 0, + 0, + $xl_crc); +} + +sub page_header +{ + my $xlp_magic =3D shift; + my $xlp_info =3D shift || 0; + my $xlp_tli =3D shift || 0; + my $xlp_pageaddr =3D shift || 0; + my $xlp_rem_len =3D shift || 0; + + # struct XLogPageHeaderData + return pack("SSIQI", + $xlp_magic, + $xlp_info, + $xlp_tli, + $xlp_pageaddr, + $xlp_rem_len); +} + +# Make sure we are far away enough from the end of a page that we could in= sert +# a couple of small records. +sub advance_out_of_record_splitting_zone +{ + my $node =3D shift; + + my $end_lsn =3D get_insert_lsn($node); + my $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; + while ($page_offset >=3D $WAL_BLOCK_SIZE - 2000) + { + $end_lsn =3D emit_message($node, 2000); + $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; + } + return $end_lsn; +} + +# Advance so close to the end of a page that an XLogRecordHeader wouldn't = fit. +sub advance_to_record_splitting_zone +{ + my $node =3D shift; + + my $end_lsn =3D get_insert_lsn($node); + my $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; + + # get fairly close to the end of a page in big steps + while ($page_offset <=3D $WAL_BLOCK_SIZE - 512) + { + $end_lsn =3D emit_message($node, $WAL_BLOCK_SIZE - $page_offset - 256); + $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; + } + + # calibrate our message size so that we can get closer 8 bytes at + # a time (XXX something better is surely possible) + my $message_size =3D $WAL_BLOCK_SIZE - 80; + while ($page_offset <=3D $WAL_BLOCK_SIZE - $RECORD_HEADER_SIZE) + { + $end_lsn =3D emit_message($node, $message_size); + + my $old_offset =3D $page_offset; + $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; + + # adjust the message size until it causes 8 bytes changes in + # offset + my $delta =3D $page_offset - $old_offset; + if ($delta > 8) + { + $message_size -=3D 8; + } + elsif ($delta <=3D 0) + { + $message_size +=3D 8; + } + } + return $end_lsn; +} + + +my $node =3D PostgreSQL::Test::Cluster->new("node"); +$node->init; +$node->append_conf('postgresql.conf', + q[wal_level =3D minimal + autovacuum =3D off + checkpoint_timeout =3D '30min' +]); +$node->start; +$node->safe_psql('postgres', "create table t as select 42"); + +$WAL_SEGMENT_SIZE =3D get_int_setting($node, 'wal_segment_size'); +$WAL_BLOCK_SIZE =3D get_int_setting($node, 'wal_block_size'); + + +my $end_lsn; +my $prev_lsn; +my $log_size; + +########################################################################### +note "Single-page end-of-WAL detection"; +########################################################################### + +# xl_tot_len is 0 (a common case, we hit trailing zeroes) +$end_lsn =3D advance_out_of_record_splitting_zone($node); +$node->stop('immediate'); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid record length at .*: expected at least 24,= got 0", + $log_size), + "xl_tot_len zero"); + +# xl_tot_len is < 24 (presumably recycled garbage) +$end_lsn =3D advance_out_of_record_splitting_zone($node); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(23)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid record length at .*: expected at least 24,= got 23", + $log_size), + "xl_tot_len short"); + +# need more pages, but xl_prev check fails first +$end_lsn =3D advance_out_of_record_splitting_zone($node); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + 0xdeadbeef)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("record with incorrect prev-link 0/DEADBEEF at .*", + $log_size), + "xl_prev bad"); + +# xl_crc check fails +advance_out_of_record_splitting_zone($node); +$end_lsn =3D emit_message($node, 10); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn - 8, '!'); # corrupt a byte in that record +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("incorrect resource manager data checksum in record= at .*", + $log_size), + "xl_crc bad"); + + +########################################################################### +note "Multi-page end-of-WAL detection, header is not split"; +########################################################################### + +# good xl_prev, we hit zero page next (zero magic) +$prev_lsn =3D advance_out_of_record_splitting_zone($node); +#my $prev_lsn =3D emit_message($node, 0); +$end_lsn =3D emit_message($node, 0); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + $prev_lsn)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid magic number 0000 .* LSN .*", $log_size), + "xlp_magic zero"); + +# good xl_prev, we hit garbage page next (bad magic) +$prev_lsn =3D advance_out_of_record_splitting_zone($node); +$end_lsn =3D emit_message($node, 0); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + $prev_lsn)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header(0xcafe, + 0, + 1, + 0)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid magic number CAFE .* LSN .*", $log_size), + "xlp_magic bad"); + +# good xl_prev, we hit typical recycled page (good xlp_magic, bad xlp_page= addr) +$prev_lsn =3D advance_out_of_record_splitting_zone($node); +$end_lsn =3D emit_message($node, 0); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + $prev_lsn)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header($XLP_PAGE_MAGIC, + 0, + 1, + 0xbaaaaaad)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("unexpected pageaddr 0/BAAAAAAD in .*, LSN .*,", $l= og_size), + "xlp_pageaddr bad"); + +# good xl_prev, xlp_magic, xlp_pageaddr, but bogus xlp_info +$prev_lsn =3D advance_out_of_record_splitting_zone($node); +$end_lsn =3D emit_message($node, 0); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 42, + $prev_lsn)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header($XLP_PAGE_MAGIC, + 0x1234, + 1, + start_of_next_page($end_lsn))); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid info bits 1234 in .*, LSN .*,", $log_size), + "xlp_info bad"); + +# good xl_prev, xlp_magic, xlp_pageaddr, but xlp_info doesn't mention cont= record +$prev_lsn =3D advance_out_of_record_splitting_zone($node); +$end_lsn =3D emit_message($node, 0); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 42, + $prev_lsn)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header($XLP_PAGE_MAGIC, + 0, + 1, + start_of_next_page($end_lsn))); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("there is no contrecord flag at .*", $log_size), + "xlp_info lacks XLP_FIRST_IS_CONTRECORD"); + +# good xl_prev, xlp_magic, xlp_pageaddr, xlp_info but xlp_rem_len doesn't = add up +$prev_lsn =3D advance_out_of_record_splitting_zone($node); +$end_lsn =3D emit_message($node, 0); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 42, + $prev_lsn)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header($XLP_PAGE_MAGIC, + $XLP_FIRST_IS_CONTRECORD, + 1, + start_of_next_page($end_lsn), + 123456)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid contrecord length 123456 .* at .*", $log_s= ize), + "xlp_rem_len bad"); + + +########################################################################### +note "Multi-page, but header is split, so page checks are done first"; +########################################################################### + +# xlp_prev is bad and xl_tot_len is too big, but we'll check xlp_magic fir= st +$end_lsn =3D advance_to_record_splitting_zone($node); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + 0xdeadbeef)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid magic number 0000 .* LSN .*", $log_size), + "xlp_magic zero (split record header)"); + +# and we'll also check xlp_pageaddr before any header checks +$end_lsn =3D advance_to_record_splitting_zone($node); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + 0xdeadbeef)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header($XLP_PAGE_MAGIC, + $XLP_FIRST_IS_CONTRECORD, + 1, + 0xbaaaaaad)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("unexpected pageaddr 0/BAAAAAAD in .*, LSN .*,", $l= og_size), + "xlp_pageaddr bad (split record header)"); + +# we'll also discover that xlp_rem_len doesn't add up before any +# header checks +$end_lsn =3D advance_to_record_splitting_zone($node); +$node->stop('immediate'); +write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, + 0, + 0xdeadbeef)); +write_wal($node, 1, start_of_next_page($end_lsn), + page_header($XLP_PAGE_MAGIC, + $XLP_FIRST_IS_CONTRECORD, + 1, + start_of_next_page($end_lsn), + 123456)); +$log_size =3D get_log_size($node); +$node->start; +ok($node->log_contains("invalid contrecord length 123456 .* at .*", $log_s= ize), + "xlp_rem_len bad (split record header)"); + +done_testing(); --=20 2.40.1 --KrQNqFl8Q8LiCsP7 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0002-Don-t-trust-unvalidated-xl_tot_len.patch" Content-Transfer-Encoding: quoted-printable =46rom 4db94cbdf5075b84a2c8ccc257d90d3671624413 Mon Sep 17 00:00:00 2001 =46rom: Thomas Munro Date: Mon, 14 Aug 2023 17:06:42 +1200 Subject: [PATCH v3 2/3] Don't trust unvalidated xl_tot_len. xl_tot_len comes first in a WAL record. Usually we don't trust it to be the true length until we've validated the record header. If the record header was split across two pages, previously we wouldn't do the validation until after we'd already tried to allocate enough memory to hold the record, which was bad because it might actually be garbage bytes from a recycled WAL file, so we could try to allocate a lot of memory. Release 15 made it worse. Since 70b4f82a4b5, we'd at least generate an end-of-WAL condition if the garbage 4 byte value happened to be > 1GB, but we'd still try to allocate up to 1GB of memory bogusly otherwise. That was an improvement, but unfortunately release 15 tries to allocate another object before that, so you could get a FATAL error and recovery could fail. We can fix both variants of the problem more fundamentally in old and new branches using pre-existing page-level validation. Whenever a record header spans a page boundary, we just need to defer all memory allocation based on xl_tot_len until we've read the following page. At that point we know that its first sector is not recycled data, by checking its xlp_pageaddr, and that its xlp_rem_len agrees with xl_tot_len on the preceding page. That is strong evidence that xl_tot_len was truly the start of a record that was logged. This problem was most likely to occur on a standby, because walreceiver.c recycles WAL files without zeroing out trailing regions of each page. We should fix that too, but that wouldn't protect us from rare crash scenarios where the trailing zeroes don't make it to disk. Back-patch to 12. This failure mode is rare, and 11 is about to go out of support, so we decided not to change its final release. Reported-by: Alexander Lakhin Discussion: https://postgr.es/m/17928-aa92416a70ff44a2%40postgresql.org --- src/backend/access/transam/xlogreader.c | 110 +++++++++++++----------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/t= ransam/xlogreader.c index c9f9f6e98f..d5ed139131 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -203,18 +203,6 @@ allocate_recordbuf(XLogReaderState *state, uint32 recl= ength) =20 #ifndef FRONTEND =20 - /* - * Note that in much unlucky circumstances, the random data read from a - * recycled segment can cause this routine to be called with a size - * causing a hard failure at allocation. For a standby, this would cause - * the instance to stop suddenly with a hard failure, preventing it to - * retry fetching WAL from one of its sources which could allow it to move - * on with replay without a manual restart. If the data comes from a past - * recycled segment and is still valid, then the allocation may succeed - * but record checks are going to fail so this would be short-lived. If - * the allocation fails because of a memory shortage, then this is not a - * hard failure either per the guarantee given by MCXT_ALLOC_NO_OOM. - */ if (!AllocSizeIsValid(newSize)) return false; =20 @@ -669,41 +657,26 @@ restart: } else { - /* XXX: more validation should be done here */ - if (total_len < SizeOfXLogRecord) - { - report_invalid_record(state, - "invalid record length at %X/%X: expected at least %u, got %u", - LSN_FORMAT_ARGS(RecPtr), - (uint32) SizeOfXLogRecord, total_len); - goto err; - } + /* We'll validate the header once we have the next page. */ gotheader =3D false; } =20 /* - * Find space to decode this record. Don't allow oversized allocation if - * the caller requested nonblocking. Otherwise, we *have* to try to - * decode the record now because the caller has nothing else to do, so - * allow an oversized record to be palloc'd if that turns out to be - * necessary. + * Try to find space to decode this record, if we can do so without + * calling palloc. If we can't, we'll try again below after we've + * validated that total_len isn't garbage bytes from a recycled WAL page. */ decoded =3D XLogReadRecordAlloc(state, total_len, - !nonblocking /* allow_oversized */ ); - if (decoded =3D=3D NULL) + false /* allow_oversized */ ); + if (decoded =3D=3D NULL && nonblocking) { /* - * There is no space in the decode buffer. The caller should help - * with that problem by consuming some records. + * There is no space in the circular decode buffer, and the caller is + * only reading ahead. The caller should consume existing records to + * make space. */ - if (nonblocking) - return XLREAD_WOULDBLOCK; - - /* We failed to allocate memory for an oversized record. */ - report_invalid_record(state, - "out of memory while trying to decode a record of length %u", tot= al_len); - goto err; + return XLREAD_WOULDBLOCK; } =20 len =3D XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ; @@ -718,16 +691,11 @@ restart: assembled =3D true; =20 /* - * Enlarge readRecordBuf as needed. + * We always have space for a couple of pages, enough to validate a + * boundary-spanning record header. */ - if (total_len > state->readRecordBufSize && - !allocate_recordbuf(state, total_len)) - { - /* We treat this as a "bogus data" condition */ - report_invalid_record(state, "record length %u at %X/%X too long", - total_len, LSN_FORMAT_ARGS(RecPtr)); - goto err; - } + Assert(state->readRecordBufSize >=3D XLOG_BLCKSZ * 2); + Assert(state->readRecordBufSize >=3D len); =20 /* Copy the first fragment of the record from the first page. */ memcpy(state->readRecordBuf, @@ -824,8 +792,35 @@ restart: goto err; gotheader =3D true; } - } while (gotlen < total_len); =20 + /* + * We might need a bigger buffer. We have validated the record + * header, in the case that it split over a page boundary. We've + * also cross-checked total_len against xlp_rem_len on the second + * page, and verified xlp_pageaddr on both. + */ + if (total_len > state->readRecordBufSize) + { + char save_copy[XLOG_BLCKSZ * 2]; + + /* + * Save and restore the data we already had. It can't be more + * than two pages. + */ + Assert(gotlen <=3D lengthof(save_copy)); + Assert(gotlen <=3D state->readRecordBufSize); + memcpy(save_copy, state->readRecordBuf, gotlen); + if (!allocate_recordbuf(state, total_len)) + { + /* We treat this as a "bogus data" condition */ + report_invalid_record(state, "record length %u at %X/%X too long", + total_len, LSN_FORMAT_ARGS(RecPtr)); + goto err; + } + memcpy(state->readRecordBuf, save_copy, gotlen); + buffer =3D state->readRecordBuf + gotlen; + } + } while (gotlen < total_len); Assert(gotheader); =20 record =3D (XLogRecord *) state->readRecordBuf; @@ -867,6 +862,25 @@ restart: state->NextRecPtr -=3D XLogSegmentOffset(state->NextRecPtr, state->segcx= t.ws_segsize); } =20 + /* + * If we got here without a DecodedXLogRecord, it means we needed to + * validate total_len before trusting it, but by now now we've done that. + */ + if (decoded =3D=3D NULL) + { + Assert(!nonblocking); + decoded =3D XLogReadRecordAlloc(state, + total_len, + true /* allow_oversized */ ); + if (decoded =3D=3D NULL) + { + /* We failed to allocate memory for an oversized record. */ + report_invalid_record(state, + "out of memory while trying to decode a record of length %u", to= tal_len); + goto err; + } + } + if (DecodeXLogRecord(state, decoded, record, RecPtr, &errormsg)) { /* Record the location of the next record. */ @@ -895,8 +909,6 @@ restart: state->decode_queue_head =3D decoded; return XLREAD_SUCCESS; } - else - return XLREAD_FAIL; =20 err: if (assembled) --=20 2.40.1 --KrQNqFl8Q8LiCsP7 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0003-Adjust-few-things-on-top-of-Thomas-patch-series.patch" Content-Transfer-Encoding: quoted-printable =46rom 6e0afd1f212714641b764ecf7ecc9ce1ee83c414 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Sat, 2 Sep 2023 10:18:39 +0900 Subject: [PATCH v3 3/3] Adjust few things on top of Thomas' patch series --- src/backend/access/transam/xlogreader.c | 10 +- src/test/perl/PostgreSQL/Test/Utils.pm | 43 +++ src/test/recovery/t/038_end_of_wal.pl | 386 +++++++++++++----------- 3 files changed, 253 insertions(+), 186 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/t= ransam/xlogreader.c index d5ed139131..5bc7639a00 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -192,6 +192,9 @@ XLogReaderFree(XLogReaderState *state) * XLOG_BLCKSZ, and make sure it's at least 5*Max(BLCKSZ, XLOG_BLCKSZ) to = start * with. (That is enough for all "normal" records, but very large commit = or * abort records might need more space.) + * + * Note: This routine should *never* be called for xl_tot_len until the he= ader + * of the record has been fully validated. */ static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength) @@ -201,13 +204,6 @@ allocate_recordbuf(XLogReaderState *state, uint32 recl= ength) newSize +=3D XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ); newSize =3D Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ)); =20 -#ifndef FRONTEND - - if (!AllocSizeIsValid(newSize)) - return false; - -#endif - if (state->readRecordBuf) pfree(state->readRecordBuf); state->readRecordBuf =3D diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/Postgre= SQL/Test/Utils.pm index 617caa022f..d7d59eb745 100644 --- a/src/test/perl/PostgreSQL/Test/Utils.pm +++ b/src/test/perl/PostgreSQL/Test/Utils.pm @@ -71,6 +71,7 @@ our @EXPORT =3D qw( chmod_recursive check_pg_config dir_symlink + scan_header system_or_bail system_log run_log @@ -702,6 +703,48 @@ sub chmod_recursive =20 =3Dpod =20 +=3Ditem scan_header(header_path, regexp) + +Returns an array that stores all the matches of the given regular expressi= on +within the PostgreSQL installation's C. This can be used to +retrieve specific value patterns from the installation's header files. + +=3Dcut + +sub scan_header +{ + my ($header_path, $regexp) =3D @_; + + my ($stdout, $stderr); + my $result =3D IPC::Run::run [ 'pg_config', '--includedir' ], '>', + \$stdout, '2>', \$stderr + or die "could not execute pg_config"; + chomp($stdout); + $stdout =3D~ s/\r$//; + + open my $header_h, '<', "$stdout/$header_path" or die "$!"; + + my @match =3D undef; + while (<$header_h>) + { + my $line =3D $_; + + if ($line =3D~ /^$regexp/) + { + # Found match, so store all the results. + @match =3D @{^CAPTURE}; + last; + } + } + + close $header_h; + die "could not find match in header $header_path\n" + unless @match; + return @match; +} + +=3Dpod + =3Ditem check_pg_config(regexp) =20 Return the number of matches of the given regular expression diff --git a/src/test/recovery/t/038_end_of_wal.pl b/src/test/recovery/t/03= 8_end_of_wal.pl index fefb5b6034..321f5965d1 100644 --- a/src/test/recovery/t/038_end_of_wal.pl +++ b/src/test/recovery/t/038_end_of_wal.pl @@ -1,6 +1,7 @@ # Copyright (c) 2023, PostgreSQL Global Development Group # -# Test ways of detecting end of WAL. +# Test detecting end of WAL replay. This test suite generates fake +# WAL records able to trigger various failure scenarios at replay. =20 use strict; use warnings; @@ -9,42 +10,44 @@ use PostgreSQL::Test::Utils; use Test::More; use Fcntl qw(SEEK_SET); =20 -use integer; # causes / operator to use integer math +use integer; # causes / operator to use integer math =20 -# Values that could be extracted from the source tree +# Header size of record header. my $RECORD_HEADER_SIZE =3D 24; -my $XLP_PAGE_MAGIC =3D 0xd113; -my $XLP_FIRST_IS_CONTRECORD =3D 0x1; + +# Fields retrieved from code headers. +my @scan_result =3D scan_header('server/access/xlog_internal.h', + '#define\s+XLOG_PAGE_MAGIC\s+(\w+)'); +my $XLP_PAGE_MAGIC =3D hex($scan_result[0]); +@scan_result =3D scan_header('server/access/xlog_internal.h', + '#define\s+XLP_FIRST_IS_CONTRECORD\s+(\w+)'); +my $XLP_FIRST_IS_CONTRECORD =3D hex($scan_result[0]); =20 # Values queried from the server my $WAL_SEGMENT_SIZE; my $WAL_BLOCK_SIZE; +my $TLI; =20 +# Build path of a WAL segment. sub wal_segment_path { my $node =3D shift; my $tli =3D shift; my $segment =3D shift; - my $wal_path =3D sprintf("%s/pg_wal/%08X%08X%08X", - $node->data_dir, - $tli, - 0, - $segment); + my $wal_path =3D + sprintf("%s/pg_wal/%08X%08X%08X", $node->data_dir, $tli, 0, $segment); return $wal_path; } =20 +# Calculate from a LSN (in bytes) its segment number and its offset. sub lsn_to_segment_and_offset { my $lsn =3D shift; return ($lsn / $WAL_SEGMENT_SIZE, $lsn % $WAL_SEGMENT_SIZE); } =20 -sub get_log_size -{ - my $node =3D shift; - return (stat $node->logfile)[7]; -} - +# Write some arbitrary data in WAL for the given segment at offset. +# This should be called while the cluster is offline. sub write_wal { my $node =3D shift; @@ -61,30 +64,37 @@ sub write_wal close $fh; } =20 +# Emit a WAL record of arbitrary size. Returns the end LSN of the +# record inserted, in bytes. sub emit_message { my $node =3D shift; my $size =3D shift; - return int($node->safe_psql('postgres', "select pg_logical_emit_message(t= rue, '', repeat('a', $size)) - '0/0'")); + return int( + $node->safe_psql( + 'postgres', + "SELECT pg_logical_emit_message(true, '', repeat('a', $size)) - '0/0'" + )); } =20 +# Get the current insert LSN of a node, in bytes. sub get_insert_lsn { my $node =3D shift; - return int($node->safe_psql('postgres', "select pg_current_wal_insert_lsn= () - '0/0'")); + return int( + $node->safe_psql( + 'postgres', "SELECT pg_current_wal_insert_lsn() - '0/0'")); } =20 +# Get GUC value, converted to an int. sub get_int_setting { my $node =3D shift; my $name =3D shift; - return int($node->safe_psql('postgres', "select setting from pg_settings = where name =3D '$name'")); -} - -sub format_lsn -{ - my $lsn =3D shift; - return sprintf("%X/%X", $lsn >> 32, $lsn & 0xffffffff); + return int( + $node->safe_psql( + 'postgres', + "SELECT setting FROM pg_settings WHERE name =3D '$name'")); } =20 sub start_of_page @@ -99,7 +109,10 @@ sub start_of_next_page return start_of_page($lsn) + $WAL_BLOCK_SIZE; } =20 -sub record_header +# Build a fake WAL record header based on the data given by the caller. +# This needs to follow the format of the C structure XLogRecord. To +# be inserted with write_wal(). +sub build_record_header { my $xl_tot_len =3D shift; my $xl_xid =3D shift || 0; @@ -108,19 +121,22 @@ sub record_header my $xl_rmid =3D shift || 0; my $xl_crc =3D shift || 0; =20 - # struct XLogRecord + # This needs to follow the structure XLogRecord: + # I for xl_tot_len + # I for xl_xid + # Q for xl_prev + # C for xl_info + # C for xl_rmid + # BB for two bytes of padding + # I for xl_crc return pack("IIQCCBBI", - $xl_tot_len, - $xl_xid, - $xl_prev, - $xl_info, - $xl_rmid, - 0, - 0, - $xl_crc); + $xl_tot_len, $xl_xid, $xl_prev, $xl_info, $xl_rmid, 0, 0, $xl_crc); } =20 -sub page_header +# Build a fake WAL page header, based on the data given by the caller +# This needs to follow the format of the C structure XLogPageHeaderData. +# To be inserted with write_wal(). +sub build_page_header { my $xlp_magic =3D shift; my $xlp_info =3D shift || 0; @@ -128,32 +144,37 @@ sub page_header my $xlp_pageaddr =3D shift || 0; my $xlp_rem_len =3D shift || 0; =20 - # struct XLogPageHeaderData + # This needs to follow the structure XLogPageHeaderData: + # S for xlp_magic + # S for xlp_info + # I for xlp_tli + # Q for xlp_pageaddr + # I for xlp_rem_len return pack("SSIQI", - $xlp_magic, - $xlp_info, - $xlp_tli, - $xlp_pageaddr, - $xlp_rem_len); + $xlp_magic, $xlp_info, $xlp_tli, $xlp_pageaddr, $xlp_rem_len); } =20 # Make sure we are far away enough from the end of a page that we could in= sert -# a couple of small records. +# a couple of small records. This inserts a few records of a fixed size, = until +# the threshold gets close enough to the end of the WAL page inserting rec= ords +# to. sub advance_out_of_record_splitting_zone { my $node =3D shift; =20 + my $page_threshold =3D $WAL_BLOCK_SIZE / 4; my $end_lsn =3D get_insert_lsn($node); my $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; - while ($page_offset >=3D $WAL_BLOCK_SIZE - 2000) + while ($page_offset >=3D $WAL_BLOCK_SIZE - $page_threshold) { - $end_lsn =3D emit_message($node, 2000); + $end_lsn =3D emit_message($node, $page_threshold); $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; } return $end_lsn; } =20 -# Advance so close to the end of a page that an XLogRecordHeader wouldn't = fit. +# Advance so close to the end of a page that an XLogRecordHeader would not +# fit on it. sub advance_to_record_splitting_zone { my $node =3D shift; @@ -161,15 +182,15 @@ sub advance_to_record_splitting_zone my $end_lsn =3D get_insert_lsn($node); my $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; =20 - # get fairly close to the end of a page in big steps + # Get fairly close to the end of a page in big steps while ($page_offset <=3D $WAL_BLOCK_SIZE - 512) { $end_lsn =3D emit_message($node, $WAL_BLOCK_SIZE - $page_offset - 256); $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; } =20 - # calibrate our message size so that we can get closer 8 bytes at - # a time (XXX something better is surely possible) + # Calibrate our message size so that we can get closer 8 bytes at + # a time. my $message_size =3D $WAL_BLOCK_SIZE - 80; while ($page_offset <=3D $WAL_BLOCK_SIZE - $RECORD_HEADER_SIZE) { @@ -178,8 +199,8 @@ sub advance_to_record_splitting_zone my $old_offset =3D $page_offset; $page_offset =3D $end_lsn % $WAL_BLOCK_SIZE; =20 - # adjust the message size until it causes 8 bytes changes in - # offset + # Adjust the message size until it causes 8 bytes changes in + # offset, enough to be able to split a record header. my $delta =3D $page_offset - $old_offset; if ($delta > 8) { @@ -193,223 +214,230 @@ sub advance_to_record_splitting_zone return $end_lsn; } =20 - +# Setup a new node. The configuration chosen here minimizes the number +# of arbitrary records that could get generated in a cluster. Enlarging +# checkpoint_timeout avoids noise with checkpoint activity. wal_level +# set to "minimal" avoids random standby snapshot records. Autovacuum +# could also trigger randomly, generating random WAL activity of its own. my $node =3D PostgreSQL::Test::Cluster->new("node"); $node->init; -$node->append_conf('postgresql.conf', - q[wal_level =3D minimal +$node->append_conf( + 'postgresql.conf', + q[wal_level =3D minimal autovacuum =3D off checkpoint_timeout =3D '30min' ]); $node->start; -$node->safe_psql('postgres', "create table t as select 42"); +$node->safe_psql('postgres', "CREATE TABLE t AS SELECT 42"); =20 $WAL_SEGMENT_SIZE =3D get_int_setting($node, 'wal_segment_size'); $WAL_BLOCK_SIZE =3D get_int_setting($node, 'wal_block_size'); - +$TLI =3D $node->safe_psql('postgres', + "SELECT timeline_id FROM pg_control_checkpoint();"); =20 my $end_lsn; my $prev_lsn; -my $log_size; =20 ########################################################################### note "Single-page end-of-WAL detection"; ########################################################################### =20 -# xl_tot_len is 0 (a common case, we hit trailing zeroes) +# xl_tot_len is 0 (a common case, we hit trailing zeroes). $end_lsn =3D advance_out_of_record_splitting_zone($node); $node->stop('immediate'); -$log_size =3D get_log_size($node); +my $log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("invalid record length at .*: expected at least 24,= got 0", - $log_size), - "xl_tot_len zero"); +ok( $node->log_contains( + "invalid record length at .*: expected at least 24, got 0", $log_size + ), + "xl_tot_len zero"); =20 -# xl_tot_len is < 24 (presumably recycled garbage) +# xl_tot_len is < 24 (presumably recycled garbage). $end_lsn =3D advance_out_of_record_splitting_zone($node); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(23)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, build_record_header(23)); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("invalid record length at .*: expected at least 24,= got 23", - $log_size), - "xl_tot_len short"); +ok( $node->log_contains( + "invalid record length at .*: expected at least 24, got 23", + $log_size), + "xl_tot_len short"); =20 -# need more pages, but xl_prev check fails first +# Need more pages, but xl_prev check fails first. $end_lsn =3D advance_out_of_record_splitting_zone($node); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - 0xdeadbeef)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, 0xdeadbeef)); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("record with incorrect prev-link 0/DEADBEEF at .*", - $log_size), - "xl_prev bad"); +ok( $node->log_contains( + "record with incorrect prev-link 0/DEADBEEF at .*", $log_size), + "xl_prev bad"); =20 -# xl_crc check fails +# xl_crc check fails. advance_out_of_record_splitting_zone($node); $end_lsn =3D emit_message($node, 10); $node->stop('immediate'); -write_wal($node, 1, $end_lsn - 8, '!'); # corrupt a byte in that record -$log_size =3D get_log_size($node); +# Corrupt a byte in that record, breaking its CRC. +write_wal($node, $TLI, $end_lsn - 8, '!'); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("incorrect resource manager data checksum in record= at .*", - $log_size), - "xl_crc bad"); +ok( $node->log_contains( + "incorrect resource manager data checksum in record at .*", $log_size + ), + "xl_crc bad"); =20 =20 ########################################################################### note "Multi-page end-of-WAL detection, header is not split"; ########################################################################### =20 -# good xl_prev, we hit zero page next (zero magic) +# This series of tests requires a valid xl_prev set in the record header +# written to WAL. + +# Good xl_prev, we hit zero page next (zero magic). $prev_lsn =3D advance_out_of_record_splitting_zone($node); -#my $prev_lsn =3D emit_message($node, 0); $end_lsn =3D emit_message($node, 0); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - $prev_lsn)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, $prev_lsn)); +$log_size =3D -s $node->logfile; $node->start; ok($node->log_contains("invalid magic number 0000 .* LSN .*", $log_size), - "xlp_magic zero"); + "xlp_magic zero"); =20 -# good xl_prev, we hit garbage page next (bad magic) +# Good xl_prev, we hit garbage page next (bad magic). $prev_lsn =3D advance_out_of_record_splitting_zone($node); $end_lsn =3D emit_message($node, 0); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - $prev_lsn)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header(0xcafe, - 0, - 1, - 0)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, $prev_lsn)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header(0xcafe, 0, 1, 0)); +$log_size =3D -s $node->logfile; $node->start; ok($node->log_contains("invalid magic number CAFE .* LSN .*", $log_size), - "xlp_magic bad"); + "xlp_magic bad"); =20 -# good xl_prev, we hit typical recycled page (good xlp_magic, bad xlp_page= addr) +# Good xl_prev, we hit typical recycled page (good xlp_magic, bad +# xlp_pageaddr). $prev_lsn =3D advance_out_of_record_splitting_zone($node); $end_lsn =3D emit_message($node, 0); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - $prev_lsn)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header($XLP_PAGE_MAGIC, - 0, - 1, - 0xbaaaaaad)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, $prev_lsn)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header($XLP_PAGE_MAGIC, 0, 1, 0xbaaaaaad)); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("unexpected pageaddr 0/BAAAAAAD in .*, LSN .*,", $l= og_size), - "xlp_pageaddr bad"); +ok( $node->log_contains( + "unexpected pageaddr 0/BAAAAAAD in .*, LSN .*,", $log_size), + "xlp_pageaddr bad"); =20 -# good xl_prev, xlp_magic, xlp_pageaddr, but bogus xlp_info +# Good xl_prev, xlp_magic, xlp_pageaddr, but bogus xlp_info. $prev_lsn =3D advance_out_of_record_splitting_zone($node); $end_lsn =3D emit_message($node, 0); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 42, - $prev_lsn)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header($XLP_PAGE_MAGIC, - 0x1234, - 1, - start_of_next_page($end_lsn))); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 42, $prev_lsn)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header( + $XLP_PAGE_MAGIC, 0x1234, 1, start_of_next_page($end_lsn))); +$log_size =3D -s $node->logfile; $node->start; ok($node->log_contains("invalid info bits 1234 in .*, LSN .*,", $log_size), - "xlp_info bad"); + "xlp_info bad"); =20 -# good xl_prev, xlp_magic, xlp_pageaddr, but xlp_info doesn't mention cont= record +# Good xl_prev, xlp_magic, xlp_pageaddr, but xlp_info doesn't mention +# continuation record. $prev_lsn =3D advance_out_of_record_splitting_zone($node); $end_lsn =3D emit_message($node, 0); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 42, - $prev_lsn)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header($XLP_PAGE_MAGIC, - 0, - 1, - start_of_next_page($end_lsn))); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 42, $prev_lsn)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header($XLP_PAGE_MAGIC, 0, 1, start_of_next_page($end_lsn))); +$log_size =3D -s $node->logfile; $node->start; ok($node->log_contains("there is no contrecord flag at .*", $log_size), - "xlp_info lacks XLP_FIRST_IS_CONTRECORD"); + "xlp_info lacks XLP_FIRST_IS_CONTRECORD"); =20 -# good xl_prev, xlp_magic, xlp_pageaddr, xlp_info but xlp_rem_len doesn't = add up +# Good xl_prev, xlp_magic, xlp_pageaddr, xlp_info but xlp_rem_len doesn't = add +# up. $prev_lsn =3D advance_out_of_record_splitting_zone($node); $end_lsn =3D emit_message($node, 0); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 42, - $prev_lsn)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header($XLP_PAGE_MAGIC, - $XLP_FIRST_IS_CONTRECORD, - 1, - start_of_next_page($end_lsn), - 123456)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 42, $prev_lsn)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header( + $XLP_PAGE_MAGIC, $XLP_FIRST_IS_CONTRECORD, + 1, start_of_next_page($end_lsn), + 123456)); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("invalid contrecord length 123456 .* at .*", $log_s= ize), - "xlp_rem_len bad"); +ok( $node->log_contains( + "invalid contrecord length 123456 .* at .*", $log_size), + "xlp_rem_len bad"); =20 =20 ########################################################################### note "Multi-page, but header is split, so page checks are done first"; ########################################################################### =20 -# xlp_prev is bad and xl_tot_len is too big, but we'll check xlp_magic fir= st +# xlp_prev is bad and xl_tot_len is too big, but we'll check xlp_magic fir= st. $end_lsn =3D advance_to_record_splitting_zone($node); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - 0xdeadbeef)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, 0xdeadbeef)); +$log_size =3D -s $node->logfile; $node->start; ok($node->log_contains("invalid magic number 0000 .* LSN .*", $log_size), - "xlp_magic zero (split record header)"); + "xlp_magic zero (split record header)"); =20 -# and we'll also check xlp_pageaddr before any header checks +# And we'll also check xlp_pageaddr before any header checks. $end_lsn =3D advance_to_record_splitting_zone($node); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - 0xdeadbeef)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header($XLP_PAGE_MAGIC, - $XLP_FIRST_IS_CONTRECORD, - 1, - 0xbaaaaaad)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, 0xdeadbeef)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header( + $XLP_PAGE_MAGIC, $XLP_FIRST_IS_CONTRECORD, 1, 0xbaaaaaad)); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("unexpected pageaddr 0/BAAAAAAD in .*, LSN .*,", $l= og_size), - "xlp_pageaddr bad (split record header)"); +ok( $node->log_contains( + "unexpected pageaddr 0/BAAAAAAD in .*, LSN .*,", $log_size), + "xlp_pageaddr bad (split record header)"); =20 -# we'll also discover that xlp_rem_len doesn't add up before any -# header checks +# We'll also discover that xlp_rem_len doesn't add up before any +# header checks, $end_lsn =3D advance_to_record_splitting_zone($node); $node->stop('immediate'); -write_wal($node, 1, $end_lsn, record_header(2 * 1024 * 1024 * 1024, - 0, - 0xdeadbeef)); -write_wal($node, 1, start_of_next_page($end_lsn), - page_header($XLP_PAGE_MAGIC, - $XLP_FIRST_IS_CONTRECORD, - 1, - start_of_next_page($end_lsn), - 123456)); -$log_size =3D get_log_size($node); +write_wal($node, $TLI, $end_lsn, + build_record_header(2 * 1024 * 1024 * 1024, 0, 0xdeadbeef)); +write_wal( + $node, $TLI, + start_of_next_page($end_lsn), + build_page_header( + $XLP_PAGE_MAGIC, $XLP_FIRST_IS_CONTRECORD, + 1, start_of_next_page($end_lsn), + 123456)); +$log_size =3D -s $node->logfile; $node->start; -ok($node->log_contains("invalid contrecord length 123456 .* at .*", $log_s= ize), - "xlp_rem_len bad (split record header)"); +ok( $node->log_contains( + "invalid contrecord length 123456 .* at .*", $log_size), + "xlp_rem_len bad (split record header)"); =20 done_testing(); --=20 2.40.1 --KrQNqFl8Q8LiCsP7-- --DzGwaSfRGfsjpLnV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmTykAMACgkQnvQgOdby QH3R1g//eJEGQjRipmss3TOAxRMUp8KGXMl33Q8bOJvyCO694VRuBrVw7SRL189H iLlu42LI77ksgepXOzAm6ixio8Gm797fTlf406PRO3MC8kPpEw2FddXgrGs5hiZA joPT690y98zsjxjCHr9Z0XG1fNjj55Zb2c9qxNkQkFm1t0O+W1MRKl67jDIiMkAb BjXT91CZmBLHIpz917xW9L3yuNNgH49fm6cH4OmDqR/WxVJLx+fCZT+/Eg2zXnBj EMA7H+xbjUbtSY0bulBNr4PY86LTZ2EsiHD/45JNm6n/WNBQk6K8my2I6OXTL1aU 9S1YRCJ0f1AFmUtIODbvSSViFDKKeK2spDLtvwGE8UdXAu6lG9NgnY0mA2zjqsML D7qw3IRWdt2LIsEGvloxOOhS5LJchgmCFD4TkPtF2HQgerWXpywePnxi/YxFxpfp GC8QMrIiCxNtBgHOtfvSdfwZ7FWDGEHpXKbuWhhGA1jnszJgNtXDCRp2+UoGGdhz 2cDwGYutPdF2qgCPI4irPMzQ7acMq6rMNhis0Z9FXuw5+Tg+KTsOewwtVwpPGql4 0ubLhM/drI/rGWwwazXH+3ZttAK5eIn6LoaWt55A7T7/rSP2uDW1d0mzCxgJejX8 Pp8Qk9CnwjAg/6GONsI0wcPzO51ULffqpYhCXArQWgb8b7G2nE0= =Pu8T -----END PGP SIGNATURE----- --DzGwaSfRGfsjpLnV--