From: Sergey Koposov <skoposov@cmu.edu>
To: pg@bowt.ie <pg@bowt.ie>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
Date: Wed, 5 Jul 2017 19:06:19 +0000
Message-ID: <1499281579.8258.32.camel@cmu.edu> (raw)
In-Reply-To: <CAH2-Wz=wjX9ADNR6V6eoTqC0J0FZo+VxmU2PPryMJKjZW=8wxg@mail.gmail.com>
References: <20170629161637.1478.93109@wrigleys.postgresql.org>
<CAH2-Wz=wjX9ADNR6V6eoTqC0J0FZo+VxmU2PPryMJKjZW=8wxg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On Thu, 2017-06-29 at 10:00 -0700, Peter Geoghegan wrote:
> On Thu, Jun 29, 2017 at 9:16 AM, <skoposov@cmu.edu> wrote:
> > From a quick look of the code it looks to me that the reason for the bug is
> > the 32 bit int overflow in the j=2*i+1 calculation inside the
> > tuplesort_heap_siftup leading to negative values of j.
>
> It seems likely that the explanation is as simple as that. This
> happens during run generation with replacement selection. All versions
> are affected, but version 9.6+ is dramatically less likely to be
> affected, because replacement selection was all but killed in Postgres
> 9.6.
>
> This is an oversight in commit 263865a. The fix is to use a variable
> that won't overflow in tuplesort_heap_siftup() -- this is probably a
> one-liner, because when the variable overflows today, the correct
> behavior would be for control to break out of the loop that declares
> the overflowing variable "j", and, I don't see any similar problem in
> other heap maintenance routines. It's a very isolated problem.
>
> I could write a patch.
Just to avoid being forgotten, I attach a trivial patch against 9.5
branch as well as have created a commitfest submission
https://commitfest.postgresql.org/14/1189/
The script below allows to reproduce the bug (segfault) and test that
the patch fixes it: (>~70 GB of RAM are needed and 100+GB of disk
space)
---
create table xx3 as
select generate_series as a
from generate_series(0,(1.5*((1::bigint)<<31))::bigint);
set maintenance_work_mem to '70GB';
create index on xx3(a);
----
Hopefully somebody can take care of patching other PG branches.
Sergey
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:x.patch (text/x-patch, 901B) downloaddiff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.cnew file mode 100644index 6470832..5db16a0
*** a/src/backend/utils/sort/tuplesort.c
--- b/src/backend/utils/sort/tuplesort.c
*************** tuplesort_heap_siftup(Tuplesortstate *st
*** 2993,2999 ****
{
SortTuple *memtuples = state->memtuples;
SortTuple *tuple;
! int i,
n;
if (--state->memtupcount <= 0)
--- 2993,2999 ----
{
SortTuple *memtuples = state->memtuples;
SortTuple *tuple;
! int64 i,
n;
if (--state->memtupcount <= 0)
*************** tuplesort_heap_siftup(Tuplesortstate *st
*** 3006,3012 ****
i = 0; /* i is where the "hole" is */
for (;;)
{
! int j = 2 * i + 1;
if (j >= n)
break;
--- 3006,3012 ----
i = 0; /* i is where the "hole" is */
for (;;)
{
! int64 j = 2 * i + 1;
if (j >= n)
break;
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: skoposov@cmu.edu, pg@bowt.ie
Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
In-Reply-To: <1499281579.8258.32.camel@cmu.edu>
References: <20170629161637.1478.93109@wrigleys.postgresql.org> <CAH2-Wz=wjX9ADNR6V6eoTqC0J0FZo+VxmU2PPryMJKjZW=8wxg@mail.gmail.com> <1499281579.8258.32.camel@cmu.edu>
* 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