Skip to content

opcache: Fix state left by killed processes - #25785

Open
dunglas wants to merge 3 commits into
php:PHP-8.4from
dunglas:fix/opcache-kill-safety
Open

dunglas wants to merge 3 commits into
php:PHP-8.4from
dunglas:fix/opcache-kill-safety

Conversation

@dunglas

@dunglas dunglas commented Nov 26, 2026

Copy link
Copy Markdown
Member

While I was working on the removal of Zend Signals (#25627), @bukka found an atomicity problem with interned strings (#25627). This PR applies his suggested fix and fixes similar issues I found while looking for other cases.

These bugs already happen today with SIGKILL and the OOM killer, and with SIGTERM on builds without Zend Signals (default in ZTS Docker images, ZTS HomebrewPHP, FrankenPHP static binaries etc).

That's why I opened this as a bug fix against PHP-8.4.

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me except for one comment, but this is worth a second review.

I found additional cases, for a separate PR:

  • zend_accel_hash_update() changes entry->data and entry->indirect separately, so a kill may leave only one of them updated. Maybe we can turn that into a single write by tagging the entry->data pointer and removing the indirect field.
  • Function JIT, zend_jit_collect_calls() temporarily stores process-local data into zend_func_info. A kill before cleanup would leave that visible to other requests.
  • zend_jit_stop_counter_handlers() sets ZCSG(jit_counters_stopped) before completing
  • accel_restart_is_active() clears restart_in_progress when the lock is free, so an NTS request that passed RINIT and didn't compile anything yet may access the half-reset cache (will be fixed by GH-25509).

Comment thread ext/opcache/ZendAccelerator.c Resolved
@arnaud-lb
arnaud-lb requested a review from bukka November 27, 2026 10:44
accel_new_interned_string() linked the new string into its hash slot
before writing it and advancing the buffer top. A process killed in
between, by SIGKILL or the OOM killer, left the slot pointing at the
unchanged top. When the next string written there hashed to the same
bucket, its collision link pointed at itself, and every later lookup in
that bucket spun forever.

Link the string last, once it is complete and the top has moved past
it.

Reported in php#25627 (comment).
The restarting process cleared restart_pending before resetting the
cache and only restored accelerator_enabled at the end. If it was
killed in between, by SIGKILL or the OOM killer, nothing scheduled
another restart and the cache stayed disabled until the server was
restarted.

Clear restart_pending last, so the next request redoes the whole
restart, whose steps can run again on a partially reset cache. Count
the restart just before, so a redone restart is counted once.

zend_accel_schedule_restart() set restart_pending before saving
accelerator_enabled into cache_status_before_restart. A kill in between
made the next restart restore a stale value, false on a cache that never
restarted before. Set restart_pending once that state is saved.

A kill can still leave restart_pending set with the cache enabled, and
requests kept using the cache while a restart was pending, so under
overlapping traffic the readers never drained and the restart waited for
opcache.force_restart_timeout, forever when it is 0. Don't let requests
use the cache while a restart is pending.

Found while working on the removal of Zend Signals in php#25627.
Compiled traces refer to their slot in zend_jit_traces by number, for
exit info and the stack map. Root traces were installed and side traces
linked into their parent before ZEND_JIT_TRACE_NUM moved past their
slot. If the process was killed in between, by SIGKILL or the OOM
killer, the next trace reused the slot while the installed code still
pointed at it, so a side exit from that code restored the VM state from
the wrong trace's metadata.

Do the bookkeeping first and make the code reachable last.

Found while working on the removal of Zend Signals in php#25627.
@dunglas
dunglas force-pushed the fix/opcache-kill-safety branch from b0ea33c to 652f848 Compare November 27, 2026 13:37
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
While compiling a trace, the tracing JIT builds the SSA, call graph and
return info of every function the trace goes through in the func_info
of the function in shared memory, from process-local arena memory, and
clears them at the end. If the process was killed in between, by
SIGKILL or the OOM killer, these func_infos kept pointers into the arena
of the dead process. The next trace entering one of these functions
took its SSA as already built and used it, and other compilations read
its stale return info, until the server was restarted.

Record each func_info in a list in shared memory before writing to it,
and reset the listed ones at the start of the next compilation.

Same problem as the function JIT one reported by Arnaud Le Blanc in
php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
zend_accel_hash_update() turns the direct entry of a key into an
indirect one when the key now resolves to another file, for example
after a file was replaced with a symlink. It wrote entry->data and
entry->indirect separately. If the process was killed in between, by
SIGKILL or the OOM killer, the entry kept indirect set to 0 with data
pointing to another hash entry, and every later lookup of the key read
that entry as a persistent script and crashed.

Lock-free readers could also see the two fields out of sync while the
update was in progress.

Tag the low bit of data instead of storing a separate flag, so the
update is a single store, and load the tagged pointer once in readers.

Reported by Arnaud Le Blanc in php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
When the JIT runs out of root traces, zend_jit_stop_counter_handlers()
restores the original VM handlers of every cached script. It set
jit_counters_stopped before doing so. If the process was killed in the
loop, by SIGKILL or the OOM killer, later calls returned early and the
remaining counting handlers stayed until the next restart: each hot
loop or function still entered the tracer once before being
blacklisted, and the cold ones kept paying for the counters.

Set the flag after the loop, which can safely run again.

Reported by Arnaud Le Blanc in php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
With opcache.jit triggers that compile functions at run time (first
execution, profiling or hot counters), zend_jit_collect_calls() builds
the call graph of the function in its func_info in shared memory, from
process-local arena memory, and zend_jit_cleanup_func_info() clears it
once the function is compiled. If the process was killed in between, by
SIGKILL or the OOM killer, the func_info kept pointers into the arena of
the dead process. Compiling a caller of that function then followed them
from zend_jit_cleanup_func_info() and crashed, and type inference read
the stale return info of the callee.

Reset the func_info of the function before building its call graph, and
those of its direct callees right after, as the compilation reads no
other.

Reported by Arnaud Le Blanc in php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
While compiling a trace, the tracing JIT builds the SSA, call graph and
return info of every function the trace goes through in the func_info
of the function in shared memory, from process-local arena memory, and
clears them at the end. If the process was killed in between, by
SIGKILL or the OOM killer, these func_infos kept pointers into the arena
of the dead process. The next trace entering one of these functions
took its SSA as already built and used it, and other compilations read
its stale return info, until the server was restarted.

Record each func_info in a list in shared memory before writing to it,
and reset the listed ones at the start of the next compilation.

Same problem as the function JIT one reported by Arnaud Le Blanc in
php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
When the JIT runs out of root traces, zend_jit_stop_counter_handlers()
restores the original VM handlers of every cached script. It set
jit_counters_stopped before doing so. If the process was killed in the
loop, by SIGKILL or the OOM killer, later calls returned early and the
remaining counting handlers stayed until the next restart: each hot
loop or function still entered the tracer once before being
blacklisted, and the cold ones kept paying for the counters.

Set the flag after the loop, which can safely run again.

Reported by Arnaud Le Blanc in php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
With opcache.jit triggers that compile functions at run time (first
execution, profiling or hot counters), zend_jit_collect_calls() builds
the call graph of the function in its func_info in shared memory, from
process-local arena memory, and zend_jit_cleanup_func_info() clears it
once the function is compiled. If the process was killed in between, by
SIGKILL or the OOM killer, the func_info kept pointers into the arena of
the dead process. Compiling a caller of that function then followed them
from zend_jit_cleanup_func_info() and crashed, and type inference read
the stale return info of the callee.

Reset the func_info of the function before building its call graph, and
those of its direct callees right after, as the compilation reads no
other.

Reported by Arnaud Le Blanc in php#25785.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
While compiling a trace, the tracing JIT builds the SSA, call graph and
return info of every function the trace goes through in the func_info
of the function in shared memory, from process-local arena memory, and
clears them at the end. If the process was killed in between, by
SIGKILL or the OOM killer, these func_infos kept pointers into the arena
of the dead process. The next trace entering one of these functions
took its SSA as already built and used it, and other compilations read
its stale return info, until the server was restarted.

Record each func_info in a list in shared memory before writing to it,
and reset the listed ones at the start of the next compilation.

Same problem as the function JIT one reported by Arnaud Le Blanc in
php#25785.
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