Improve pg_re_throw: check if sigjmp_buf is valid and report error

From: Xiaoran Wang <fanfuxiaoran(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Improve pg_re_throw: check if sigjmp_buf is valid and report error
Date: 2024-08-19 06:17:17
Message-ID: CAGjhLkOx4Eg4y=9Y71HmEK5XAbC14tqV1_4iz7506CE+zprE3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

https://www.postgresql.org/message-id/CANncwrJTse6WKkUS_Y8Wj2PHVRvaqPxMk_qtEPsf_+NVPYxzyg@mail.gmail.com

As the problem discussed in the above thread, I also run into that. Besides
updating the doc, I would like to report a error for it.

If the code in PG_TRY contains any non local control flow other than
ereport(ERROR) like goto, break etc., the PG_CATCH or PG_END_TRY cannot
be called, then the PG_exception_stack will point to the memory whose
stack frame has been released. So after that, when the pg_re_throw
called, __longjmp() will crash and report Segmentation fault error.

In that case, to help developers to figure out the root cause easily, it is
better to report that 'the sigjmp_buf is invalid' rather than letting
the __longjmp report any error.

Addition to sigjmp_buf, add another field 'int magic' which is next to
the sigjum_buf in the local stack frame memory. The magic's value is always
'PG_exception_magic 0x12345678'. And in 'pg_re_throw' routine, check if
the magic's value is still '0x12345678', if not, that means the memory
where the 'PG_exception_stack' points to has been released, and the
'sigbuf'
must be invalid.

The related code is in patch 0001

------------------------------
I'm not sure if it is necessary to add a regress test for it. In patch
0002, to test the
patch can work correctly, I have added a function 'pg_re_throw_crash' in
regress.c

create function pg_re_throw_crash()
RETURNS void
AS :'regresslib', 'pg_re_throw_crash'
LANGUAGE C STRICT STABLE PARALLEL SAFE;
create above function and run 'select pg_re_throw_crash()', then will get
the error
'FATAL: Invalid sigjum_buf, code in PG_TRY cannot contain any non local
control flow other than ereport'

--
Best regards !
Xiaoran Wang

Attachment Content-Type Size
0001-Imporve-pg_re_throw-check-if-sigjmp_buf-is-valid-and.patch application/octet-stream 12.7 KB
0002-Test-pg_re_throw-checking-invalid-sigjmp_buf.patch application/octet-stream 1.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2024-08-19 06:18:20 Re: Apply PGDLLIMPORT markings to some GUC variables
Previous Message Amit Kapila 2024-08-19 06:07:44 Re: Conflict detection and logging in logical replication