Skip to content

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

Open
bm1549 wants to merge 1 commit into
php:PHP-8.5from
bm1549:brian.marks/trace-jit-vm-interrupt-jmp
Open

bm1549 wants to merge 1 commit 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

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
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