[php-src] PR #25472: Fix GH-25471: OPcache Windows startup race on freshly created shared memory
| From: | manueldimmler | Date: | Sun, 15 Nov 2026 21:36:49 +0000 |
| Subject: | [php-src] PR #25472: Fix GH-25471: OPcache Windows startup race on freshly created shared memory | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40663@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25472
Author: manueldimmler
### Problem
On Windows,
create_segments() (ext/opcache/shared_alloc_win32.c) releases
the
mapping mutex *before* the shared memory is initialized. When several php-cgi.exe
/ php.exe processes of the same installation start at nearly the same time and no
other process holds the OPcache mapping, one process creates the mapping and the
others attach to it. An attacher can run accel_post_startup() and read
ZSMMG(app_shared_globals) before the creator has set it, getting either
NULL
or a stale stack value, and crash with 0xC0000005 before any script runs. With
JIT enabled it can additionally run zend_jit_startup(..., reattached=true) on an
uninitialized JIT buffer and crash in dasm_encode. NTS and ZTS are both affected.
Linux is not affected (no backend there attaches to another process's segment).
Full analysis, crash-dump evidence and a reproducer: GH-25471.
### Fix
The creating process keeps the Win32 mapping mutex until the segment is fully
initialized, including the JIT buffer, and releases it through a new helper
zend_shared_alloc_startup_done() after zend_shared_alloc_save_state(). The
Win32 mutex is recursive, so the locks taken during initialization keep working.
tmp_shared_globals is additionally zero-initialized so the value copied into
shared memory never contains stack garbage.
Releasing the mutex right after zend_accel_init_shm() is not sufficient: an
attacher can then enter the JIT startup before the creator has initialized the
buffer. This was confirmed by a test build that forces the window with Sleep(50).
### Testing (Windows Server 2019, 4 vCPUs, official 8.4.27 binaries, no ionCube)
Groups of 8 processes started simultaneously, 12,000 starts per variant, variants
interleaved, 60 s timeout per process:
| php_opcache.dll | NTS, JIT | ZTS, JIT | NTS, no JIT | ZTS, no JIT |
|---|---|---|---|---|
| official 8.4.27 | 43 | 38 | 27 | 24 |
| own build, unpatched | 33 | 27 | 15 | 13 |
| **this patch** | **0** | **0** | **0** | **0** |
0 crashes in 48,000 starts, no hangs. With a Sleep(50)-forced window and JIT on
(1,600 starts each): unpatched ~75 %, unlock-after-init ~84 % (all in dasm_encode),
this patch 0. ext/opcache/tests with run-tests.php -j4: 814 passed / 0
failed
without JIT, 817 passed / 0 failed with opcache.jit=tracing, same for patched and
unpatched.
### Notes
- Windows-only change; on other platforms only the memset applies.
- The DLLs were cross-compiled with clang-cl / lld-link (not the official MSVC
toolchain), so the Windows CI build is the real check.
- Two pre-existing, unrelated issues found during review are reported separately
and are not addressed here: the ALLOC_FALLBACK path reads
app_shared_globals
without the ACCEL_BASE_POINTER_SIZE offset, and every attacher unconditionally
overwrites the cache-restart save-state point.
- The tmp_shared_globals zero-initialization can be split into its own commit if
preferred.