Skip to content

ext/opcache: Defer signals while the JIT compiles - #25974

Draft
jorgsowa wants to merge 1 commit into
php:PHP-8.4from
jorgsowa:fix-jit-pcntl-001-assertion
Draft

jorgsowa wants to merge 1 commit into
php:PHP-8.4from
jorgsowa:fix-jit-pcntl-001-assertion

Conversation

@jorgsowa

Copy link
Copy Markdown
Contributor

JIT compiles store SSA and call graph data in the shared func_info of the op_arrays they touch, and clear it when the compile ends. If a signal kills the process mid-compile, func_info keeps pointing into the dead process's arena. The next compile in another process reuses it and crashes, or hits Assertion failed: info & (1 << type) in zend_jit_trace_type_to_info_ex().

This is the cause of the intermittent ext/pcntl/tests/001.phpt failure on ALPINE_X64_ASAN_DEBUG_ZTS: a forked child gets SIGTERM while compiling a trace, and the parent reuses its leftover SSA. In production, FPM's request_terminate_timeout sends the same SIGTERM.

Fix:

  • Wrap the five compile entry points (root trace, side trace, first-exec, hot counters, profiling at request shutdown) in HANDLE_BLOCK_INTERRUPTIONS(). Opcache already does this around its other SHM updates (ad55348). The JIT never did.
  • Clean up func_info when a function compile bails out too. Every blocked section unblocks before re-throwing the bailout (see 38f1288).

Concerns:

  • SIGKILL and the OOM killer can't be deferred, so they can still leave stale data. That's also true of every other opcache SHM update. Covering them requires wiping func_info on the next compile. I have that version, but it's more invasive than a fix for 8.4 should be.
  • A SIGTERM or SIGINT that arrives during a compile is delivered when the compile finishes. Compiles are short, and opcache already defers them the same way while persisting a script.
  • No effect on Windows or on --disable-zend-signals builds, where the macros compile to nothing.
  • There's no test for the bailout cleanup. I couldn't get a reliable mid-compile memory-limit bailout, and Zend MM hides the stale reads.

The tests fork a child and set opcache.jit_debug=2 in it, so it blocks writing the SSA dump to a socket in the middle of a compile. The parent sends SIGTERM, then runs code that would reuse the leftover data. All five tests crash under ASan without the fix.

Merging up: applies cleanly to PHP-8.5. On master there's a trivial conflict, 0/1 vs false/true in the zend_jit_check_funcs() calls.

The flaky test is the first attempt of ALPINE_X64_ASAN_DEBUG_ZTS on PR #25946, which failed: https://github.com/php/php-src/actions/runs/46443912300/job/141936152537

The re-run passed: https://github.com/php/php-src/actions/runs/46443912300/job/141988272688

A JIT compile stores SSA and call graph data in the shared func_info of
the op_arrays it touches, and clears it when the compile ends. If a
signal killed the process before that, func_info kept pointing into the
dead process's arena, and the next compile in another process reused
it. Defer signals during the compile, as opcache already does around
other SHM updates.

A function compile that bailed out skipped the cleanup too. Clean up on
bailout as well.
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.

1 participant