pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Christoph Berg <myon@debian.org>
Cc: Andres Freund <andres@anarazel.de>
Cc: PostgreSQL Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Bus error in pg_logical_slot_get_changes (9.4.7, sparc)
Date: Thu, 14 Apr 2016 13:11:43 -0400
Message-ID: <423.1460653903@sss.pgh.pa.us> (raw)
In-Reply-To: <20160414112518.GE11804@msg.df7cb.de>
References: <20160413211152.GA21711@msg.credativ.de>
	<20160413175827.dmlbtdf7c3mgmnex@alap3.anarazel.de>
	<20160414092919.GA11804@msg.df7cb.de>
	<20160414112518.GE11804@msg.df7cb.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Christoph Berg <myon@debian.org> writes:
>> ... the patch worked indeed, thanks!

> Duh, the patch does work on sparc, but it breaks amd64:

Well, yeah, because if Size is wider than int32 then that memcpy isn't
enough to initialize it.

But looking around a bit, I think that ReorderBufferRestoreChange is
almost completely broken on alignment-picky machines, if the expectation
is that the input data could be aligned arbitrarily.  Aside from this
particular problem, it contains multiple places where we cast "data"
to something other than char*, and that is sufficient to allow a
spec-compliant compiler to decide that "data" is aligned on more than
a char boundary and hence generate code that depends on such alignment.

I also noticed that the INTERNAL_SNAPSHOT case doesn't bother to advance
"data" past the data read, where all the other switch cases do.  In the
attached, I made it do likewise, but it looks like you could equally
easily just drop the final update of "data" in each switch case,
because nothing is looking at it after the switch.

Proposed patch attached, but I'm still wondering why the alignment-picky
buildfarm members aren't all failing on this.  It seems to strongly
suggest that the regression tests' coverage is lacking here.

			regards, tom lane

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

Attachments:

alignment-fixup.patch (text/x-diff, 3.6K) download diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 52c6986..b6f83f8 100644 *** a/src/backend/replication/logical/reorderbuffer.c --- b/src/backend/replication/logical/reorderbuffer.c *************** static void *** 2449,2463 **** ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn, char *data) { - ReorderBufferDiskChange *ondisk; ReorderBufferChange *change; - ondisk = (ReorderBufferDiskChange *) data; - change = ReorderBufferGetChange(rb); /* copy static part */ ! memcpy(change, &ondisk->change, sizeof(ReorderBufferChange)); data += sizeof(ReorderBufferDiskChange); --- 2449,2461 ---- ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn, char *data) { ReorderBufferChange *change; change = ReorderBufferGetChange(rb); /* copy static part */ ! memcpy(change, data + offsetof(ReorderBufferDiskChange, change), ! sizeof(ReorderBufferChange)); data += sizeof(ReorderBufferDiskChange); *************** ReorderBufferRestoreChange(ReorderBuffer *** 2471,2477 **** case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT: if (change->data.tp.oldtuple) { ! Size tuplelen = ((HeapTuple) data)->t_len; change->data.tp.oldtuple = ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); --- 2469,2479 ---- case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT: if (change->data.tp.oldtuple) { ! uint32 tuplelen; ! ! /* Must get tuplelen the hard way in case it's misaligned */ ! memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len), ! sizeof(uint32)); change->data.tp.oldtuple = ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); *************** ReorderBufferRestoreChange(ReorderBuffer *** 2492,2498 **** if (change->data.tp.newtuple) { ! Size tuplelen = ((HeapTuple) data)->t_len; change->data.tp.newtuple = ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); --- 2494,2504 ---- if (change->data.tp.newtuple) { ! uint32 tuplelen; ! ! /* Must get tuplelen the hard way in case it's misaligned */ ! memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len), ! sizeof(uint32)); change->data.tp.newtuple = ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); *************** ReorderBufferRestoreChange(ReorderBuffer *** 2538,2552 **** } case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT: { ! Snapshot oldsnap; Snapshot newsnap; Size size; ! oldsnap = (Snapshot) data; size = sizeof(SnapshotData) + ! sizeof(TransactionId) * oldsnap->xcnt + ! sizeof(TransactionId) * (oldsnap->subxcnt + 0); change->data.snapshot = MemoryContextAllocZero(rb->context, size); --- 2544,2558 ---- } case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT: { ! SnapshotData oldsnap; Snapshot newsnap; Size size; ! memcpy(&oldsnap, data, sizeof(SnapshotData)); size = sizeof(SnapshotData) + ! sizeof(TransactionId) * oldsnap.xcnt + ! sizeof(TransactionId) * oldsnap.subxcnt; change->data.snapshot = MemoryContextAllocZero(rb->context, size); *************** ReorderBufferRestoreChange(ReorderBuffer *** 2557,2562 **** --- 2563,2570 ---- (((char *) newsnap) + sizeof(SnapshotData)); newsnap->subxip = newsnap->xip + newsnap->xcnt; newsnap->copied = true; + + data += size; break; } /* the base struct contains all the data, easy peasy */

view thread (15+ messages)  latest in thread

Message-ID: <423.1460653903@sss.pgh.pa.us>
Permalink:  ../423.1460653903@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/423.1460653903@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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, myon@debian.org, andres@anarazel.de
  Subject: Re: Bus error in pg_logical_slot_get_changes (9.4.7, sparc)
  In-Reply-To: <423.1460653903@sss.pgh.pa.us>
  References: <20160413211152.GA21711@msg.credativ.de> <20160413175827.dmlbtdf7c3mgmnex@alap3.anarazel.de> <20160414092919.GA11804@msg.df7cb.de> <20160414112518.GE11804@msg.df7cb.de> <423.1460653903@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