pgsql: Avoid crashing when a JIT-inlined backend function throws an err - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Avoid crashing when a JIT-inlined backend function throws an err
Date
Msg-id E1sMu6Y-003GZY-Tf@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Avoid crashing when a JIT-inlined backend function throws an error.

errfinish() assumes that the __FUNC__ and __FILE__ arguments it's
passed are compile-time constant strings that can just be pointed
to rather than physically copied.  However, it's possible for LLVM
to generate code in which those pointers point into a dynamically
loaded code segment.  If that segment gets unloaded before we're
done with the ErrorData struct, we have dangling pointers that
will lead to SIGSEGV.  In simple cases that won't happen, because we
won't unload LLVM code before end of transaction.  But it's possible
to happen if the error is thrown within end-of-transaction code run by
_SPI_commit or _SPI_rollback, because since commit 2e517818f those
functions clean up by ending the transaction and starting a new one.

Rather than fixing this by adding pstrdup() overhead to every
elog/ereport sequence, let's fix it by copying the risky pointers
in CopyErrorData().  That solves it for _SPI_commit/_SPI_rollback
because they use that function to preserve the error data across
the transaction end/restart sequence; and it seems likely that
any other code doing something similar would need to do that too.

I'm suspicious that this behavior amounts to an LLVM bug (or a
bug in our use of it?), because it implies that string constant
references that should be pointer-equal according to a naive
understanding of C semantics will sometimes not be equal.
However, even if it is a bug and someday gets fixed, we'll have
to cope with the current behavior for a long time to come.

Report and patch by me.  Back-patch to all supported branches.

Discussion: https://postgr.es/m/1565654.1719425368@sss.pgh.pa.us

Branch
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/86fac88eed4b9a0874b1183573aa62d042262eba

Modified Files
--------------
src/backend/utils/error/elog.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)


pgsql-committers by date:

Previous
From: Heikki Linnakangas
Date:
Subject: pgsql: Fix MVCC bug with prepared xact with subxacts on standby
Next
From: Amit Langote
Date:
Subject: pgsql: SQL/JSON: Document behavior when input document is not jsonb