Skip to content

Fix tracing JIT taking the wrong branch after a VM interrupt - #25636

Merged
arnaud-lb merged 2 commits into
php:PHP-8.5from
bm1549:brian.marks/trace-jit-vm-interrupt-jmp
Nov 24, 2026
Merged

arnaud-lb merged 2 commits into
php:PHP-8.5from
bm1549:brian.marks/trace-jit-vm-interrupt-jmp

Conversation

@bm1549

@bm1549 bm1549 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

The description below was written with an LLM.

On the CALL and TAILCALL VMs, the tracing JIT can take the wrong branch after a VM interrupt.

zend_jit_trace_handler() runs opcodes that the trace doesn't compile natively, such as COALESCE, by calling their VM handler and storing the returned opline in IP. When the handler takes a jump while an interrupt is pending, zend_interrupt_helper runs the interrupt function and returns the jump target tagged with ZEND_VM_ENTER_BIT. The opline guard after the handler then fails, and the trace exits to the fall-through branch. For example, $arr[$k] ?? 'MISSING' returns 'MISSING' even though the key exists.

This is a follow-up to #24793, which fixed the same issue for the function JIT in zend_jit_handler(). This change clears ZEND_VM_ENTER_BIT from the handler result in the tracing JIT too, when an interrupt function is installed.

The new test, ext/opcache/tests/jit/trace_vm_interrupt_jmp_001.phpt, raises the interrupt deterministically with zend_test's VmInterruptComparable and zend_test.observer.set_vm_interrupt_on_begin. Without the fix, it prints int(0) and then "MISSING" four times.

Tested on the PHP-8.5 (91452ab) and PHP-8.6 (feaa8a1) heads, ZTS built with clang (TAILCALL VM) on arm64:

  • The new test fails before the fix and passes after it on both branches.
  • With an extension that sets EG(vm_interrupt) from another thread every 1–200 µs, a minimal ?? loop failed in 88/200 (8.6) and 109/200 (8.5) runs before the fix, and in none after it.
  • All of ext/opcache/tests/jit passes with the fix on both branches.

zend_jit_trace_handler() runs some opcodes, such as COALESCE, through their
VM handler and stores the returned opline in IP. When the handler takes a
jump while an interrupt is pending, zend_interrupt_helper runs the interrupt
function and returns the jump target with ZEND_VM_ENTER_BIT set. The opline
guard after the handler then fails, and the trace exits to the fall-through
branch, so e.g. `$arr[$k] ?? 'default'` returns the default although the key
exists.

Clear ZEND_VM_ENTER_BIT when an interrupt function is installed, as
zend_jit_handler() already does for the function JIT. Only the CALL and
TAILCALL VMs are affected.
@bm1549
bm1549 force-pushed the brian.marks/trace-jit-vm-interrupt-jmp branch from 4f2d5f9 to 30c6bd9 Compare November 20, 2026 22:45
@bm1549
bm1549 changed the base branch from master to PHP-8.5 November 20, 2026 22:45
@bm1549
bm1549 marked this pull request as ready for review November 20, 2026 23:38
@bm1549
bm1549 requested a review from dstogov as a code owner November 20, 2026 23:38
@realFlowControl

Copy link
Copy Markdown
Contributor

@arnaud-lb could you have a look please?

* PHP-8.5: (25 commits)
  [skip ci] Add APP_DEBUG=0 for Symfony Preloading test
  streams: fix stale readbuflen after stream_filter_remove() on a read filter
  Update NEWS for phpGH-20920 LibreSSL build fix
  Fix phpGH-20920: do not use OPENSSL_strndup() for peer CN matching
  Simplify php_openssl_load_all_certs_from_file()
  Fix phpGH-20920: only register PKCS7_NO_DUAL_CONTENT when it is defined
  Fix phpGH-20920: guard ENGINE_cleanup() with OPENSSL_NO_ENGINE again
  ext/soap: Fix NULL dereference on empty Body with matching SOAPAction (phpGH-25661)
  ext/dom: Fix use-after-free in getElementById() after removing an ID attribute
  ext/reflection: Don't call destructor when constructor is not public in newInstance(Args)()
  ext/phar: unset() of an entry with an open handle use-after-free.
  ext/intl: Fix IntlCalendar::setTime() build on PHP 8.5 and later
  ext/intl: Reject non-finite timestamps in IntlCalendar::setTime() (php#25668)
  [skip ci] Speed up community build
  [skip ci] Backport test-suite.yml changes from master
  Fix propagation of public(set) of readonly trait property
  Make test accept different outputs
  Fix phpGH-25528: Retain the Phar OpenSSL signing key (php#25529)
  ext/pdo_sqlite: Fix use-after-free of bound string and LOB parameters
  ext/opcache: Fix JIT ASSIGN_DIM undefined OP_DATA and dim reporting on non-array containers (phpGH-24665)
  ...
@arnaud-lb
arnaud-lb merged commit 171b97e into php:PHP-8.5 Nov 24, 2026
1 of 2 checks passed
arnaud-lb added a commit that referenced this pull request Nov 24, 2026
* PHP-8.6:
  Fix tracing JIT taking the wrong branch after a VM interrupt (#25636)
@arnaud-lb

Copy link
Copy Markdown
Member

Thank you!

Schwarzemann pushed a commit to Schwarzemann/php-src that referenced this pull request Nov 24, 2026
* PHP-8.5:
  Fix tracing JIT taking the wrong branch after a VM interrupt (php#25636)
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.

3 participants