php-src/ext/pgsql
Niels Dossche 5941cdaaad
Fix ZTS crashes with persistent resources in modules (#13381)
On shutdown in ZTS the following happens:
- https://github.com/php/php-src/blob/master/Zend/zend.c#L1124-L1125
  gets executed. This destroys global persistent resources and destroys
  the modules. Furthermore, the modules are unloaded too.
- Further down, `ts_free_id(executor_globals_id)` gets executed, which
  calls `executor_globals_dtor`. This function destroys persistent
  resources for each thread.

Notice that in the last step, the modules that the persistent resource
belong to may already have been destroyed. This means that accessing
globals will cause a crash (I previously fixed this with ifdef magic),
or when the module is dynamically loaded we'll try jumping to a
destructor that is no longer loaded in memory. These scenarios cause
crashes.

It's not possible to move the `ts_free_id` call upwards, because that
may break assumptions of callers, and furthermore this would deallocate
the executor globals structure, which means that any access to those
will cause a segfault.

This patch adds a new API to the TSRM that allows running a callback on
a certain resource type. We use this API to destroy the persistent
resources in all threads prior to the module destruction, and keep the
rest of the resource dtor intact.

I verified this fix on Apache with postgres, both dynamically and
statically.

Fixes GH-12974.
2024-02-13 21:43:03 +01:00
..
tests GH-13355 fix new pgsql test 2024-02-08 22:44:03 +00:00
config.m4 check PQsetErrorContextVisibility availability (libpq >= 9.6) 2023-06-21 08:47:59 +02:00
config.w32 PGSQL and POD_SQL: don't include pg_config.h 2020-05-25 10:48:35 +02:00
CREDITS
pgsql.c Fix ZTS crashes with persistent resources in modules (#13381) 2024-02-13 21:43:03 +01:00
pgsql.stub.php Merge branch 'PHP-8.3' 2023-11-20 16:24:57 +00:00
pgsql_arginfo.h Merge branch 'PHP-8.3' 2023-11-20 16:24:57 +00:00
php_pgsql.h Merge branch 'PHP-8.2' into PHP-8.3 2023-12-23 17:15:06 +00:00