From 30c6bd9c3cc61b32db7a42ebe9dc4c4463e23537 Mon Sep 17 00:00:00 2001 From: Brian Marks Date: Fri, 20 Nov 2026 17:16:22 -0500 Subject: [PATCH] Fix tracing JIT taking the wrong branch after a VM interrupt 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. --- NEWS | 2 + ext/opcache/jit/zend_jit_ir.c | 7 +++ .../tests/jit/trace_vm_interrupt_jmp_001.phpt | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 ext/opcache/tests/jit/trace_vm_interrupt_jmp_001.phpt diff --git a/NEWS b/NEWS index f33a114c4ccf..6e900da5fbf0 100644 --- a/NEWS +++ b/NEWS @@ -108,6 +108,8 @@ PHP NEWS (pavimus) . Fixed incorrect constant propagation when unpacking into partially known arrays. (Weilin Du) + . Fixed tracing JIT taking the wrong branch after a VM interrupt. + (Brian Marks) - PDO_Firebird: . Fixed stack buffer overflow with long connection user names, passwords, diff --git a/ext/opcache/jit/zend_jit_ir.c b/ext/opcache/jit/zend_jit_ir.c index 4fab838d9caa..e6fd3b022266 100644 --- a/ext/opcache/jit/zend_jit_ir.c +++ b/ext/opcache/jit/zend_jit_ir.c @@ -17228,6 +17228,13 @@ static int zend_jit_trace_handler(zend_jit_ctx *jit, const zend_op_array *op_arr jit_STORE_IP(jit, ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT))); } else { + /* A handler that jumps checks for interrupts. Once the interrupt + * function has run, it returns the next opline with + * ZEND_VM_ENTER_BIT set, which would fail the opline guard below + * and exit to the wrong branch. */ + if (zend_interrupt_function) { + ref = ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT)); + } jit_STORE_IP(jit, ref); } } diff --git a/ext/opcache/tests/jit/trace_vm_interrupt_jmp_001.phpt b/ext/opcache/tests/jit/trace_vm_interrupt_jmp_001.phpt new file mode 100644 index 000000000000..0cd513b95ba1 --- /dev/null +++ b/ext/opcache/tests/jit/trace_vm_interrupt_jmp_001.phpt @@ -0,0 +1,47 @@ +--TEST-- +Tracing JIT: a jumping handler must not take the other branch after a VM interrupt +--DESCRIPTION-- +The object comparison raises an interrupt, and the observer INI setting +installs an interrupt callback. The tracing JIT runs COALESCE through its VM +handler. Taking the jump, the handler checks for interrupts and runs the +callback, then returns the jump target with ZEND_VM_ENTER_BIT set. Without +clearing that bit, the opline guard after the handler fails and the trace +exits to the fall-through branch, so `??` returns its default even though +the key exists. + +This fails on the CALL and TAILCALL VMs only; the HYBRID VM doesn't tag the +returned opline. +--EXTENSIONS-- +opcache +zend_test +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.file_update_protection=0 +opcache.jit=tracing +opcache.jit_buffer_size=64M +opcache.jit_hot_func=1 +opcache.jit_hot_loop=1 +opcache.jit_hot_return=1 +opcache.jit_hot_side_exit=1 +zend_test.observer.set_vm_interrupt_on_begin=1 +--FILE-- + $i])); +} +?> +--EXPECT-- +int(0) +int(1) +int(2) +int(3) +int(4)