php-src/TSRM
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
..
config.w32 Remove unused tsrm_strtok_r() function 2019-07-12 13:14:24 +02:00
LICENSE - Update to 2006 2006-01-04 23:55:42 +00:00
threads.m4 Check host_alias on one place (#13042) 2023-12-29 22:44:39 +01:00
tsrm_win32.c Merge branch 'PHP-8.1' into PHP-8.2 2023-02-07 13:38:39 +03:00
tsrm_win32.h Join shm_pair.info and .segment of Windows shm implementation 2022-06-10 17:18:27 +02:00
TSRM.c Fix ZTS crashes with persistent resources in modules (#13381) 2024-02-13 21:43:03 +01:00
TSRM.h Fix ZTS crashes with persistent resources in modules (#13381) 2024-02-13 21:43:03 +01:00
tsrm.m4 Merge branch 'PHP-7.4' 2019-07-01 13:26:17 +02:00