[php-src] PR #13381: Fix ZTS crashes with persistent resources in modules
| From: | nielsdos | Date: | Mon, 12 Feb 2024 19:24:35 +0000 |
| Subject: | [php-src] PR #13381: Fix ZTS crashes with persistent resources in modules | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-25067@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/13381
Author: nielsdos
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 that for
statically loaded modules 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 executor dtor intact.
I verified this fix on Apache with postgres, tested both with dynamically and statically loaded
modules.
I tried to think of other ideas, but I couldn't find other simple ones that won't break
BC.
**Important:** This patch is only for master because it changes public API and might be slightly
dangerous.
For stable branches I propose https://gist.github.com/nielsdos/338774e915a1b528eaa8bd68ba7cc54c
which is safer and does not change public API.
Fixes GH-12974. Although the issue only mentions postgres, other modules are affected too.