pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dmitry Ivanov <d.ivanov@postgrespro.ru>
Cc: pgsql-hackers@postgresql.org
Cc: Yury Zhuravlev <u.zhuravlev@postgrespro.ru>
Subject: Re: Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
Date: Thu, 04 Aug 2016 16:19:25 -0400
Message-ID: <8848.1470341965@sss.pgh.pa.us> (raw)
In-Reply-To: <8629a08e-f3e0-4caa-9478-d85a454f656c@postgrespro.ru>
References: <1637882.WfYN5gPf1A@abook>
	<1996641.25QjV5kv6m@abook>
	<8629a08e-f3e0-4caa-9478-d85a454f656c@postgrespro.ru>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

Yury Zhuravlev <u.zhuravlev@postgrespro.ru> writes:
> Dmitry Ivanov wrote:
>>> Recently I've encountered a strange crash while calling elog(ERROR, "...")
>>> after the WaitForBackgroundWorkerShutdown() function. It turns out that
>>> _returns_ inside the PG_TRY()..PG_CATCH() block are *highly* undesirable,
>>> since they leave PG_exception_stack pointing to a local struct in a dead
>>> frame, which is an obvious UB. I've attached a patch which fixes this
>>> behavior in the aforementioned function, but there might be more errors
>>> like that elsewhere.

> Good catch. But in 9.6 it has been fixed by 
> db0f6cad4884bd4c835156d3a720d9a79dbd63a9 commit. 

Only accidentally, I'd say.  I chose to push this into HEAD as well,
so that WaitForBackgroundWorkerShutdown would look more like
WaitForBackgroundWorkerStartup, and would not have a risk of future
breakage if someone adds code after the for-loop.

I was pretty worried about the "more errors like that" point, because
we recently fixed another similar mistake in plpython, cf 1d2fe56e4.
However, a search using the attached quick-hack script to look for
"return" etc between PG_TRY and PG_CATCH did not find any other
instances.

It'd be nice to have some automatic way of catching future mistakes
like this, but I'm not sure about a reliable way to do that.
One idea is to add an Assert to PG_CATCH:

 #define PG_TRY()  \
     do { \
         sigjmp_buf *save_exception_stack = PG_exception_stack; \
         ErrorContextCallback *save_context_stack = error_context_stack; \
         sigjmp_buf local_sigjmp_buf; \
         if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
         { \
             PG_exception_stack = &local_sigjmp_buf
 
 #define PG_CATCH()    \
+            Assert(PG_exception_stack == &local_sigjmp_buf); \
         } \
         else \
         { \
             PG_exception_stack = save_exception_stack; \
             error_context_stack = save_context_stack

but there are a couple of disadvantages to this.  One is that you don't
find out about a problem unless the bad code path is actually exercised.
I'm afraid that the most tempting places to do something like this would
be low-probability error cases, and thus that the Assert would do little
to catch them.  (Case in point: the postmaster-death path fixed in this
patch would surely never have been caught by testing.)  The other and
really worse problem is that when the Assert does fire, it's nowhere near
the scene of the crime, so while it may tell you there's a problem it
does not give much help in locating the bug.

Anyone have a better idea?

			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

Attachments:

find_bad_pg_try (text/x-shellscript, 330B) download #!/bin/sh # search DIR for PG_TRY blocks containing WORD # return, break, continue, goto are good things to look for dir="$1" word="$2" for f in `find "$dir" -type f -name '*.c'` do sed -n -e '/PG_TRY()/,/PG_CATCH()/ { /'"$word"'/ =; /'"$word"'/ p }' $f | \ sed -e '/^[0-9]\+$/ { s|^[0-9]\+$|'"$f"': &: |; N }' done exit 0

view thread (4+ messages)

Message-ID: <8848.1470341965@sss.pgh.pa.us>
Permalink:  ../8848.1470341965@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/8848.1470341965@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, d.ivanov@postgrespro.ru, u.zhuravlev@postgrespro.ru
  Subject: Re: Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
  In-Reply-To: <8848.1470341965@sss.pgh.pa.us>
  References: <1637882.WfYN5gPf1A@abook> <1996641.25QjV5kv6m@abook> <8629a08e-f3e0-4caa-9478-d85a454f656c@postgrespro.ru> <8848.1470341965@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