pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@anarazel.de>
Cc: pgsql-hackers@postgresql.org
Subject: Re: skink's test_decoding failures in 9.4 branch
Date: Wed, 20 Jul 2016 19:44:07 -0400
Message-ID: <11035.1469058247@sss.pgh.pa.us> (raw)
In-Reply-To: <20160720170159.i7ctvb3i6ut3aref@alap3.anarazel.de>
References: <17922.1469030757@sss.pgh.pa.us>
	<21842.1469033104@sss.pgh.pa.us>
	<20160720170159.i7ctvb3i6ut3aref@alap3.anarazel.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

Andres Freund <andres@anarazel.de> writes:
> I guess either using valgrind's gdb server on error, or putting some
> asserts checking the size would be best. I can look into it, but it'll
> not be today likely.

I believe the problem is that DecodeUpdate is not on the same page as the
WAL-writing routines about how much data there is for an old_key_tuple.
Specifically, I see this in 9.4's log_heap_update():

        if (old_key_tuple)
        {
            ...
            xlhdr_idx.t_len = old_key_tuple->t_len;

            rdata[nr].data = (char *) old_key_tuple->t_data
                + offsetof(HeapTupleHeaderData, t_bits);
            rdata[nr].len = old_key_tuple->t_len
                - offsetof(HeapTupleHeaderData, t_bits);
            ...
        }

so that the amount of tuple data that's *actually* in WAL is
offsetof(HeapTupleHeaderData, t_bits) less than what t_len says.
However, over in DecodeUpdate, this is processed with

        xl_heap_header_len xlhdr;

        memcpy(&xlhdr, data, sizeof(xlhdr));
        ...
        datalen = xlhdr.t_len + SizeOfHeapHeader;
        ...
        DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);

and what DecodeXLogTuple does is

    int            datalen = len - SizeOfHeapHeader;
    (so we're back to datalen == xlhdr.t_len)
    ...
    memcpy(((char *) tuple->tuple.t_data) + offsetof(HeapTupleHeaderData, t_bits),
           data + SizeOfHeapHeader,
           datalen);

so that we are copying offsetof(HeapTupleHeaderData, t_bits) too much
data from the WAL buffer.  Most of the time this doesn't hurt but it's
making valgrind complain, and on a unlucky day we might crash entirely.

I have not looked to see if the bug also exists in > 9.4.  Also, it's
not very clear to me whether other call sites for DecodeXLogTuple might
have related bugs.

			regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



view thread (4+ messages)

Message-ID: <11035.1469058247@sss.pgh.pa.us>
Permalink:  ../11035.1469058247@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/11035.1469058247@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, andres@anarazel.de
  Subject: Re: skink's test_decoding failures in 9.4 branch
  In-Reply-To: <11035.1469058247@sss.pgh.pa.us>
  References: <17922.1469030757@sss.pgh.pa.us> <21842.1469033104@sss.pgh.pa.us> <20160720170159.i7ctvb3i6ut3aref@alap3.anarazel.de> <11035.1469058247@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox