Skip to content

Fix opcache.enable_file_override lookups racing with OPcache restarts - #25483

Merged
arnaud-lb merged 3 commits into
php:PHP-8.4from
pavimus:fix-file-override-lookup-v2
Nov 16, 2026
Merged

arnaud-lb merged 3 commits into
php:PHP-8.4from
pavimus:fix-file-override-lookup-v2

Conversation

@pavimus

@pavimus pavimus commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Prepared with an AI assistant (Claude), which also ran the reproduction and the tests on my machine; I reviewed the result. The text below is AI-generated (quoted, as asked in CONTRIBUTING.md).

Found while working on OPcache restarts in threaded SAPIs (#24276), where @arnaud-lb asked to be
pinged on this PR. It affects NTS as well (e.g. FPM) and is independent of #24276.

With opcache.enable_file_override=1, file_exists(), is_file() and is_readable() first look
the file up in the shared hash (filename_is_in_cache()). Unlike other readers of the shared
hash outside of compilation (accelerator_get_scripts(), zend_accel_invalidate()), the lookup
neither checks ZCG(accelerator_enabled) nor takes the SHM read lock (opcache_is_script_cached()
uses the same function after checking only ZCG(accelerator_enabled)). A restart waits only for
processes that hold the read lock, so a request that is not counted as a reader (for example one
that started while the restart was pending, when the cache is disabled for it) can look the hash
up while another process is cleaning it in zend_accel_hash_clean() /
zend_shared_alloc_restore_state(), and then read the script entry it found.

The window can be shown deterministically with gdb (NTS debug build, php -S with
PHP_CLI_SERVER_WORKERS=2, enable_file_override=1): opcache_reset(), then a request that runs
the restart in RINIT is held by gdb at zend_accel_hash_clean(), and meanwhile another worker
serves is_file(__DIR__ . '/a.php'). Without the fix that worker calls zend_accel_hash_find()
on the shared hash with ZCSG(restart_in_progress) set and ZCG(accelerator_enabled) false
(3/3 runs); with the fix it falls back to the original function during the restart and looks the
hash up normally afterwards (3/3). I did not get a crash from this small case; there is no
.phpt, since it needs one process stopped inside the restart.

The fix does what accelerator_get_scripts() does: look the hash up only when the cache is
enabled for the request and accelerator_shm_read_lock() succeeds, release it afterwards, and
otherwise fall back to the original function. With #24276, under ZTS this means that only
requests admitted in RINIT look the hash up.

Tests: ext/opcache and Zend/tests on NTS and ZTS debug builds, 0 failures (same as without the
fix), including the file override tests issue0140.phpt and bug74663.phpt. The change merges
into PHP-8.5 cleanly; on master filename_is_in_cache() returns bool, the same change adapted
is ready if needed.

🤖 Generated with Claude Code

With opcache.enable_file_override=1, file_exists(), is_file() and
is_readable() look the file up in the shared hash table first. The
lookup neither checked whether the cache is enabled for the request nor
took the SHM read lock, so it could run while a restart (scheduled by
OOM, hash overflow or opcache_reset(), then executed by another process
or thread) cleaned the hash and the scripts, and dereference freed
entries.

Look the hash up only when the cache is enabled for the request and the
read lock is held, like accelerator_get_scripts() does; otherwise fall
back to the original function.
* PHP-8.4:
  Fix phpGH-8739: OPcache restart corrupts SHM in threaded SAPIs (php#24276)
@arnaud-lb
arnaud-lb merged commit 482c6a2 into php:PHP-8.4 Nov 16, 2026
1 check passed
arnaud-lb added a commit that referenced this pull request Nov 16, 2026
* PHP-8.6:
  Fix opcache.enable_file_override lookups racing with OPcache restarts (#25483)
@arnaud-lb

Copy link
Copy Markdown
Member

Thank you!

TimWolla pushed a commit to TimWolla/php-src that referenced this pull request Nov 16, 2026
* PHP-8.4:
  Fix opcache.enable_file_override lookups racing with OPcache restarts (php#25483)
TimWolla pushed a commit to TimWolla/php-src that referenced this pull request Nov 16, 2026
* PHP-8.5:
  Fix opcache.enable_file_override lookups racing with OPcache restarts (php#25483)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants