Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YII70-00079N-0I for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Feb 2015 14:36:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YII6z-0003S3-80 for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Feb 2015 14:36:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YII6y-0003Rx-I4 for pgsql-bugs@postgresql.org; Mon, 02 Feb 2015 14:36:24 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YII6s-0000Rd-IP for pgsql-bugs@postgresql.org; Mon, 02 Feb 2015 14:36:23 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id DCB1C28797 for ; Mon, 2 Feb 2015 06:36:12 -0800 (PST) Received: from EX13-CAS-006.vmware.com (EX13-CAS-006.vmware.com [10.113.191.56]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id D6B0FB0B3A for ; Mon, 2 Feb 2015 06:36:12 -0800 (PST) Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Mon, 2 Feb 2015 06:35:54 -0800 Message-ID: <54CF8B41.7020006@vmware.com> Date: Mon, 2 Feb 2015 16:35:45 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.3.0 MIME-Version: 1.0 To: Pg Bugs Subject: Re: Segfault on exclusion constraint violation References: <54CF808C.5080207@vmware.com> In-Reply-To: <54CF808C.5080207@vmware.com> Content-Type: multipart/mixed; boundary="------------090700000503090306050304" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --------------090700000503090306050304 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit On 02/02/2015 03:50 PM, Heikki Linnakangas wrote: > This only happens with assertions enabled. The culprit is commit > f88d4cfc9d417dac2ee41a8f5e593898e56fd2bd, which added the 'ctid' > argument to XactLockTableWait. check_exclusion_constraint calls > index_endscan() just before XactLockTableWait, but that free's the > memory the ctid points to. > > The fix for this particular instance is trivial: copy the ctid to a > local variable before calling index_endscan. However, looking at the > other XactLockTableWait() and MultiXactIdWait() calls, there are more > questionable pointers being passed. Most point to heap tuples on disk > pages, after releasing the lock on the page, although not the pin. The > one in EvalPlanQualFetch releases the pin too. > > I'll write up a patch to change those call sites to use local variables. > Hopefully it's trivial enough to still include in 9.4.1, although time > is really running out.. I'll commit the attached fix shortly, so please shout quickly if you see a problem with this. Aside from the potential for segfaults with assertions, I think the calls passed incorrect ctid anyway. For example: > --- a/src/backend/executor/execUtils.c > +++ b/src/backend/executor/execUtils.c > @@ -1307,7 +1307,7 @@ retry: > if (TransactionIdIsValid(xwait)) > { > index_endscan(index_scan); > - XactLockTableWait(xwait, heap, &tup->t_data->t_ctid, > + XactLockTableWait(xwait, heap, &tup->t_self, > XLTW_RecheckExclusionConstr); > goto retry; > } We don't really want to pass the heap tuple's ctid field. If the tuple was updated (again) in the same transaction, the one that's still in-progress, that points to the *next* tuple in the chain, but the error message says "while checking exclusion constraint on tuple (%u,%u) in relation \"%s\"". We should be passing the TID of the tuple itself, not the ctid value in the tuple's header. The attached patch fixes that too. - Heikki --------------090700000503090306050304 Content-Type: text/x-diff; name="0001-Fix-reference-after-free-when-waiting-for-another-xa.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Fix-reference-after-free-when-waiting-for-another-xa.pa"; filename*1="tch" From 2b53793ca2c32ca8b3d26dc8f0ee72c3a623ed69 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Mon, 2 Feb 2015 16:24:58 +0200 Subject: [PATCH 1/1] Fix reference-after-free when waiting for another xact due to constraint. If an insertion or update had to wait for another transaction to finish, because there was another insertion with conflicting key in progress, we would pass a just-free'd item pointer to XactLockTableWait(). All calls to XactLockTableWait() and MultiXactIdWait() had similar issues. Some passed a pointer to an buffer in the buffer cache, after already releasing the lock. The call in EvalPlanQualFetch had already released the pin too. All but the call in execUtils.c would merely lead to reporting a bogus ctid, however (or an assertion failure, if enabled). All the callers that passed HeapTuple->t_data->t_ctid were slightly bogus anyway: if the tuple was updated (again) in the same transaction, its ctid field would point to the next tuple in the chain, not the tuple itself. Backpatch to 9.4, where the 'ctid' argument to XactLockTableWait was added (in commit f88d4cfc) --- src/backend/access/heap/heapam.c | 16 ++++++++-------- src/backend/catalog/index.c | 4 ++-- src/backend/executor/execMain.c | 2 +- src/backend/executor/execUtils.c | 4 +++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 21e9d06..46060bc 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -2717,7 +2717,7 @@ l1: { /* wait for multixact */ MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate, infomask, - relation, &tp.t_data->t_ctid, XLTW_Delete, + relation, &(tp.t_self), XLTW_Delete, NULL); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -2744,7 +2744,7 @@ l1: else { /* wait for regular transaction to end */ - XactLockTableWait(xwait, relation, &tp.t_data->t_ctid, XLTW_Delete); + XactLockTableWait(xwait, relation, &(tp.t_self), XLTW_Delete); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); /* @@ -3261,7 +3261,7 @@ l2: /* wait for multixact */ MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, - relation, &oldtup.t_data->t_ctid, XLTW_Update, + relation, &oldtup.t_self, XLTW_Update, &remain); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -3341,7 +3341,7 @@ l2: */ heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, LockWaitBlock, &have_tuple_lock); - XactLockTableWait(xwait, relation, &oldtup.t_data->t_ctid, + XactLockTableWait(xwait, relation, &oldtup.t_self, XLTW_Update); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -4365,7 +4365,7 @@ l3: { case LockWaitBlock: MultiXactIdWait((MultiXactId) xwait, status, infomask, - relation, &tuple->t_data->t_ctid, XLTW_Lock, NULL); + relation, &tuple->t_self, XLTW_Lock, NULL); break; case LockWaitSkip: if (!ConditionalMultiXactIdWait((MultiXactId) xwait, @@ -4437,7 +4437,7 @@ l3: switch (wait_policy) { case LockWaitBlock: - XactLockTableWait(xwait, relation, &tuple->t_data->t_ctid, + XactLockTableWait(xwait, relation, &tuple->t_self, XLTW_Lock); break; case LockWaitSkip: @@ -5181,7 +5181,7 @@ l4: { LockBuffer(buf, BUFFER_LOCK_UNLOCK); XactLockTableWait(members[i].xid, rel, - &mytup.t_data->t_ctid, + &mytup.t_self, XLTW_LockUpdated); pfree(members); goto l4; @@ -5242,7 +5242,7 @@ l4: if (needwait) { LockBuffer(buf, BUFFER_LOCK_UNLOCK); - XactLockTableWait(rawxmax, rel, &mytup.t_data->t_ctid, + XactLockTableWait(rawxmax, rel, &mytup.t_self, XLTW_LockUpdated); goto l4; } diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 9bb9deb..f85ed93 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2328,7 +2328,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, */ LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); XactLockTableWait(xwait, heapRelation, - &heapTuple->t_data->t_ctid, + &heapTuple->t_self, XLTW_InsertIndexUnique); CHECK_FOR_INTERRUPTS(); goto recheck; @@ -2377,7 +2377,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, */ LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); XactLockTableWait(xwait, heapRelation, - &heapTuple->t_data->t_ctid, + &heapTuple->t_self, XLTW_InsertIndexUnique); CHECK_FOR_INTERRUPTS(); goto recheck; diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 20b3188..33b172b 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -2118,7 +2118,7 @@ EvalPlanQualFetch(EState *estate, Relation relation, int lockmode, { case LockWaitBlock: XactLockTableWait(SnapshotDirty.xmax, - relation, &tuple.t_data->t_ctid, + relation, &tuple.t_self, XLTW_FetchUpdated); break; case LockWaitSkip: diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c index 4b921fa..022041b 100644 --- a/src/backend/executor/execUtils.c +++ b/src/backend/executor/execUtils.c @@ -1245,6 +1245,7 @@ retry: ForwardScanDirection)) != NULL) { TransactionId xwait; + ItemPointerData ctid_wait; Datum existing_values[INDEX_MAX_KEYS]; bool existing_isnull[INDEX_MAX_KEYS]; char *error_new; @@ -1306,8 +1307,9 @@ retry: if (TransactionIdIsValid(xwait)) { + ctid_wait = tup->t_data->t_ctid; index_endscan(index_scan); - XactLockTableWait(xwait, heap, &tup->t_data->t_ctid, + XactLockTableWait(xwait, heap, &ctid_wait, XLTW_RecheckExclusionConstr); goto retry; } -- 2.1.4 --------------090700000503090306050304 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------090700000503090306050304--