[php-src] PR #17249: Fix GH-17246: Nested shm protections cause segfault
| From: | nielsdos | Date: | Mon, 23 Dec 2024 17:58:48 +0000 |
| Subject: | [php-src] PR #17249: Fix GH-17246: Nested shm protections cause segfault | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-30098@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/17249
Author: nielsdos
This bug happens because of a nested
SHM_UNPROTECT() sequence. In particular:
```
unprotect memory at ext/opcache/ZendAccelerator.c:2127
protect memory at ext/opcache/ZendAccelerator.c:2160
unprotect memory at ext/opcache/ZendAccelerator.c:2164
unprotect memory at ext/opcache/jit/zend_jit_trace.c:7464
^^^ Nested
protect memory at ext/opcache/jit/zend_jit_trace.c:7591
^^^ Problem is here: it should not protect again due to the nested unprotect
protect memory at ext/opcache/ZendAccelerator.c:2191
^^^ This one should actually protect, not the previous one
```
The reason this nesting happen is because:
1. We try to include the script, this eventually calls cache_script_in_shared_memory
2. zend_optimize_script will eventually run SCCP as part of the DFA pass.
3. SCCP will try to replace constants, but can also run destructors when a partial array is
destructed here:
https://github.com/php/php-src/blob/4e9cde758eadf30cc4d596d6398c2c34c64197b4/Zend/Optimizer/sccp.c#L2387-L2389
In this case, this destruction invokes the tracing JIT, leading to the nested unprotects.
This patch adds a counter to track the nesting level of shm protections. This is a simple solution
to the problem. An alternative is not JITting during SCCP, but that would be a leaky abstraction. I
also think this solution is more general.
One downside is an extra check in case protect_memory is set, but production
configuration usually don't have this enabled. It may be possible to combine the
protect_memory and shm_nesting_level field in some way, but that will make
more complicated code.