Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aYqtS-00072O-S6 for pgsql-bugs@arkaria.postgresql.org; Thu, 25 Feb 2016 08:03:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aYqtS-0000do-5X for pgsql-bugs@arkaria.postgresql.org; Thu, 25 Feb 2016 08:03:26 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aYqso-0008Nd-3m for pgsql-bugs@postgresql.org; Thu, 25 Feb 2016 08:02:46 +0000 Received: from mail-ob0-x234.google.com ([2607:f8b0:4003:c01::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aYqsf-0000GW-Ol for pgsql-bugs@postgresql.org; Thu, 25 Feb 2016 08:02:44 +0000 Received: by mail-ob0-x234.google.com with SMTP id dm2so41539924obb.2 for ; Thu, 25 Feb 2016 00:02:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zalando-de.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=nzx41WxIeG/MbYK8e5hbgV+7lJo+jHDxlc8yuHjtjwU=; b=g2r1Cc553J2E1m0oQaDrM/44a5xAKLyR2PHOfWCiNwvwiwcvHJ1pQm1dtnRi8+VKGY bNrOzshqQUFONsHWLqGjyawpJY9BQxfUMLQSXQxnZSVRFcBiyZrmw5JUswAeFNnFxQFe HrDksd+6JG2yQPD42A/6OKimPibxpIb6rybNGOfMjMFaldbG/dtwWnqPJeC83jO3nFb4 vqeGLf1Nuq7TS1y98HfK57n6rNu39Vg2bh7lwYHtRLGfXSZQ9PBY8lbrHaRvAiDk0+4E ZrK1+j2BVMY0xwnggksh9Qt8Egjg1haFHe3n0pT07v8Dqd7dKZhKAd7tL0LK0sP4xRsA bDYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=nzx41WxIeG/MbYK8e5hbgV+7lJo+jHDxlc8yuHjtjwU=; b=IIalcV1O56cFoZ1IVAyFRPEPFFRBLdRJfGsP8jabh40iw7Q46+RgSe/JiunO0DxFo/ JvTkpZeXhTviFnauF93764DN27vIjq5SmO8PdLIi2uPhjHDmbQfif++nbPeoKL9Ft+r1 s5elxOvgm137iTFlp0y8MdwAYHquy8bMIY0iOGlHwTVDb5FtHMEYnd4lCJtiULbaR7KQ WG5/6bci+z5/6VxAsMRgTT6G7cDOHER+7h7JDvc+0jfpMmmy13MgZSimD8UAmMf5Wgsb WtCqSG137rx45J7CgIZd/ibicq1poxxabdngMePoaazA6CtixXOopOPmGwMD4ecezud2 RMhw== X-Gm-Message-State: AG10YOTI1nGeuPIeJaySfq+K6GUyLaT6UUkACGEiIfPDKhqJ3l0+KEwPNTcQwXrL4ef0tG7+5Agj1NJiaDJJ1otN X-Received: by 10.60.97.233 with SMTP id ed9mr35194290oeb.17.1456387356767; Thu, 25 Feb 2016 00:02:36 -0800 (PST) MIME-Version: 1.0 Received: by 10.202.191.70 with HTTP; Thu, 25 Feb 2016 00:02:07 -0800 (PST) In-Reply-To: <20160224215221.bbbduc5nelq7tf6s@alap3.anarazel.de> References: <20160224125824.2573.27524@wrigleys.postgresql.org> <20160224205237.GA438345@alvherre.pgsql> <20160224215221.bbbduc5nelq7tf6s@alap3.anarazel.de> From: "Shulgin, Oleksandr" Date: Thu, 25 Feb 2016 09:02:07 +0100 Message-ID: Subject: Re: BUG #13985: Segmentation fault on PREPARE TRANSACTION To: Andres Freund Cc: Alvaro Herrera , chris.tessels@inergy.nl, Pg Bugs Content-Type: multipart/alternative; boundary=089e0115f22a0e6b74052c939b4a X-Pg-Spam-Score: -1.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 --089e0115f22a0e6b74052c939b4a Content-Type: text/plain; charset=UTF-8 On Wed, Feb 24, 2016 at 10:52 PM, Andres Freund wrote: > On 2016-02-24 17:52:37 -0300, Alvaro Herrera wrote: > > chris.tessels@inergy.nl wrote: > > > > > Core was generated by `postgres: mailinfo_ow mailinfo_ods > 10.50.6.6(4188'. > > > Program terminated with signal 11, Segmentation fault. > > > > > > #0 MinimumActiveBackends (min=50) at procarray.c:2472 > > > 2472 if (pgxact->xid == InvalidTransactionId) > > > > It's not surprising that you're not able to make this crash > > consistently, because it looks like the problem might be in concurrent > > modifications to the PGXACT array. This routine, MinimumActiveBackends, > > walks the PGPROC array explicitely without locks. There are comments > > indicating that this is safe, but evidently something has slipped in > > there. > > > > Apparently this code is trying to dereference an invalid pgxact, but > > it's not clear to me how this happens. Those structs are allocated in > > advance, and they are referenced in the code via array indexes, so even > > if the pgxact doesn't actually hold data about a valid transaction, > > dereferencing the XID shouldn't cause a crash. > > Well, that code is pretty, uh, questionable. E.g. for > int pgprocno = > arrayP->pgprocnos[index]; > volatile PGPROC *proc = &allProcs[pgprocno]; > volatile PGXACT *pgxact = &allPgXact[pgprocno]; > there's no guarantee that pgprocno is actually the same index for both > lookups and the following > if (pgprocno == -1) > continue; /* do not count > deleted entries */ > check. It's perfectly reasonable for a compiler to reload pgprocno from > memory, or just always reference it via memory. > Hm... this is against my understanding of what a compiler could (or should) do. Do you have a documentation reference or other evidence? A naive disassemble dump on-my-box(tm) suggests that pgprocno is stored on stack (with offset -0x1c) and is referenced from there: ... 0x00000000007f8011 <+53>: mov -0x18(%rbp),%rax 0x00000000007f8015 <+57>: mov -0x20(%rbp),%edx 0x00000000007f8018 <+60>: movslq %edx,%rdx 0x00000000007f801b <+63>: add $0x8,%rdx 0x00000000007f801f <+67>: mov 0x8(%rax,%rdx,4),%eax 0x00000000007f8023 <+71>: mov %eax,-0x1c(%rbp) 0x00000000007f8026 <+74>: mov 0x67b15b(%rip),%rdx # 0xe73188 0x00000000007f802d <+81>: mov -0x1c(%rbp),%eax # <== here 0x00000000007f8030 <+84>: cltq 0x00000000007f8032 <+86>: imul $0x338,%rax,%rax 0x00000000007f8039 <+93>: add %rdx,%rax 0x00000000007f803c <+96>: mov %rax,-0x10(%rbp) 0x00000000007f8040 <+100>: mov 0x67b149(%rip),%rcx # 0xe73190 0x00000000007f8047 <+107>: mov -0x1c(%rbp),%eax # <== here 0x00000000007f804a <+110>: movslq %eax,%rdx 0x00000000007f804d <+113>: mov %rdx,%rax 0x00000000007f8050 <+116>: add %rax,%rax 0x00000000007f8053 <+119>: add %rdx,%rax 0x00000000007f8056 <+122>: shl $0x2,%rax 0x00000000007f805a <+126>: add %rcx,%rax 0x00000000007f805d <+129>: mov %rax,-0x8(%rbp) 0x00000000007f8061 <+133>: cmpl $0xffffffff,-0x1c(%rbp) # <== and here 0x00000000007f8065 <+137>: je 0x7f80a4 ... I presume what happened here is that initially arrayP->pgprocnos[index] > was -1, but by the time if (pgprocno == -1) is reached, it changed to a > different value. > > It's also really crummy that we're doing the PGPROC/PGXACT lookups > before checking whether pgprocno is -1. > No doubt here. At the very least ISTM that we have to make pgprocno volatile (or use a > memory barrier - but we don't have sufficient support for those in the > older branches), and move the PGPROC/PGXACT lookups after the == -1 > check. > Use of volatile doesn't change the resulting code dramatically for me. The above is when compiling without optimizations. With -Og I'm getting similar code for the variant with volatile and if no volatile, pgprocno is just loaded into a register as one would expect. -- Alex --089e0115f22a0e6b74052c939b4a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On W= ed, Feb 24, 2016 at 10:52 PM, Andres Freund <andres@anarazel.de>= wrote:
On 2016-02-24 17:52:37 = -0300, Alvaro Herrera wrote:
> chris.tessels@inergy.nl= wrote:
>
> >=C2=A0 =C2=A0 =C2=A0Core was generated by `postgres: mailinfo_ow m= ailinfo_ods 10.50.6.6(4188'.
> >=C2=A0 =C2=A0 =C2=A0Program terminated with signal 11, Segmentatio= n fault.
> >
> >=C2=A0 =C2=A0 =C2=A0#0=C2=A0 MinimumActiveBackends (min=3D50) at p= rocarray.c:2472
> >=C2=A0 =C2=A0 =C2=A02472=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (pgxact->xid =3D=3D InvalidTransactionId)=
>
> It's not surprising that you're not able to make this crash > consistently, because it looks like the problem might be in concurrent=
> modifications to the PGXACT array.=C2=A0 This routine, MinimumActiveBa= ckends,
> walks the PGPROC array explicitely without locks.=C2=A0 There are comm= ents
> indicating that this is safe, but evidently something has slipped in > there.
>
> Apparently this code is trying to dereference an invalid pgxact, but > it's not clear to me how this happens.=C2=A0 Those structs are all= ocated in
> advance, and they are referenced in the code via array indexes, so eve= n
> if the pgxact doesn't actually hold data about a valid transaction= ,
> dereferencing the XID shouldn't cause a crash.

Well, that code is pretty, uh, questionable. E.g. for
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 int=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0pgprocno =3D = arrayP->pgprocnos[index];
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 volatile PGPROC *pr= oc =3D &allProcs[pgprocno];
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 volatile PGXACT *pg= xact =3D &allPgXact[pgprocno];
there's no guarantee that pgprocno is actually the same index for both<= br> lookups and the following
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (pgprocno =3D=3D= -1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 continue;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0/* do not count deleted entries */
check.=C2=A0 It's perfectly reasonable for a compiler to reload pgprocn= o from
memory, or just always reference it via memory.

Hm... =C2=A0this is against my understanding of what a compiler co= uld (or should) do. =C2=A0 Do you have a documentation reference or other e= vidence?

A naive disassemble dump on-my-box(tm) su= ggests that pgprocno is stored on stack (with offset -0x1c) and is referenc= ed from there:
=C2=A0 =C2=A0...
=C2=A0 = =C2=A00x00000000007f8011 <+53>: mov =C2=A0 =C2=A0-0x18(%rbp),%rax
=C2=A0 =C2=A00x000= 00000007f8015 <+57>: mov =C2=A0 =C2=A0-0x20(%rbp),%edx
=C2=A0 =C2=A00x00000000007f80= 18 <+60>: movslq %e= dx,%rdx
=C2=A0 =C2=A00x00000000007f801b <+63>: add =C2=A0 =C2=A0$0x8,%rdx
=C2=A0 =C2=A00x00000000007f801f <+67>: mov =C2=A0 =C2=A00x8(%rax,%rdx,4),%eax
=C2= =A0 =C2=A00x00000000007f8023 <+71>: mov =C2=A0 =C2=A0%eax,-0x1c(%rbp)
=C2=A0 =C2=A00= x00000000007f8026 <+74>: <= /span>mov =C2=A0 =C2=A00x67b15b(%rip),%rdx =C2=A0 =C2=A0 =C2=A0 =C2=A0# 0xe= 73188 <allProcs>
=C2=A0 =C2=A00x00000000007f802d <+81>= ;: mov =C2=A0 =C2=A0-0x1c= (%rbp),%eax =C2=A0 =C2=A0# <=3D=3D here
=C2=A0 =C2=A00x0000000= 0007f8030 <+84>: cl= tq =C2=A0=C2=A0
=C2=A0 =C2=A00x00000000007f8032 <+86>: imul =C2=A0 $0x338,%rax,%rax<= /div>
=C2=A0 =C2=A00x00000000007f8039 <+93>: add =C2=A0 =C2=A0%rdx,%rax
=C2=A0 = =C2=A00x00000000007f803c <+96>: mov =C2=A0 =C2=A0%rax,-0x10(%rbp)
=C2=A0 =C2=A00x000= 00000007f8040 <+100>: mov =C2=A0 =C2=A00x67b149(%rip),%rcx =C2=A0 =C2=A0 =C2=A0 =C2=A0# 0xe731= 90 <allPgXact>
=C2=A0 =C2=A00x00000000007f8047 <+107>= : mov =C2=A0 =C2=A0-0x1c(= %rbp),%eax=C2=A0=C2=A0 =C2=A0# <=3D=3D here
=C2=A0 =C2=A00x000= 00000007f804a <+110>: movslq %eax,%rdx
=C2=A0 =C2=A00x00000000007f804d <+113&= gt;: mov =C2=A0 =C2=A0%rd= x,%rax
=C2=A0 =C2=A00x00000000007f8050 <+116>: add =C2=A0 =C2=A0%rax,%rax
=C2=A0 =C2=A00x00000000007f8053 <+119>: add =C2=A0 =C2=A0%rdx,%rax
=C2=A0 =C2=A00x= 00000000007f8056 <+122>: <= /span>shl =C2=A0 =C2=A0$0x2,%rax
=C2=A0 =C2=A00x00000000007f805a = <+126>: add =C2=A0 = =C2=A0%rcx,%rax
=C2=A0 =C2=A00x00000000007f805d <+129>: mov =C2=A0 =C2=A0%rax,-0x8(%= rbp)
=C2=A0 =C2=A00x00000000007f8061 <+133>: cmpl =C2=A0 $0xffffffff,-0x1c(%rbp)=C2= =A0=C2=A0 =C2=A0# <=3D=3D and here
=C2=A0 =C2=A00x00000000007f= 8065 <+137>: je =C2= =A0 =C2=A0 0x7f80a4 <MinimumActiveBackends+200>
=C2=A0 =C2=A0...

I presume what happened here is that initially arrayP->pgprocnos[index]<= br> was -1, but by the time if (pgprocno =3D=3D -1) is reached, it changed to a=
different value.

It's also really crummy that we're doing the PGPROC/PGXACT lookups<= br> before checking whether pgprocno is -1.

No doubt here.

At the very least ISTM that we have to make pgprocno volatile (or use a
memory barrier - but we don't have sufficient support for those in the<= br> older branches), and move the PGPROC/PGXACT lookups after the =3D=3D -1
check.

Use of volatile doesn't chan= ge the resulting code dramatically for me.

The abo= ve is when compiling without optimizations.=C2=A0 With -Og I'm getting = similar code for the variant with volatile and if no volatile, pgprocno is = just loaded into a register as one would expect.

-= -
Alex

--089e0115f22a0e6b74052c939b4a--