Skip to content

Commit 30c6bd9

Browse files
committed
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.
1 parent c30231c commit 30c6bd9

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ PHP NEWS
108108
(pavimus)
109109
. Fixed incorrect constant propagation when unpacking into partially known
110110
arrays. (Weilin Du)
111+
. Fixed tracing JIT taking the wrong branch after a VM interrupt.
112+
(Brian Marks)
111113

112114
- PDO_Firebird:
113115
. Fixed stack buffer overflow with long connection user names, passwords,

‎ext/opcache/jit/zend_jit_ir.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17228,6 +17228,13 @@ static int zend_jit_trace_handler(zend_jit_ctx *jit, const zend_op_array *op_arr
1722817228

1722917229
jit_STORE_IP(jit, ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT)));
1723017230
} else {
17231+
/* A handler that jumps checks for interrupts. Once the interrupt
17232+
* function has run, it returns the next opline with
17233+
* ZEND_VM_ENTER_BIT set, which would fail the opline guard below
17234+
* and exit to the wrong branch. */
17235+
if (zend_interrupt_function) {
17236+
ref = ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT));
17237+
}
1723117238
jit_STORE_IP(jit, ref);
1723217239
}
1723317240
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Tracing JIT: a jumping handler must not take the other branch after a VM interrupt
3+
--DESCRIPTION--
4+
The object comparison raises an interrupt, and the observer INI setting
5+
installs an interrupt callback. The tracing JIT runs COALESCE through its VM
6+
handler. Taking the jump, the handler checks for interrupts and runs the
7+
callback, then returns the jump target with ZEND_VM_ENTER_BIT set. Without
8+
clearing that bit, the opline guard after the handler fails and the trace
9+
exits to the fall-through branch, so `??` returns its default even though
10+
the key exists.
11+
12+
This fails on the CALL and TAILCALL VMs only; the HYBRID VM doesn't tag the
13+
returned opline.
14+
--EXTENSIONS--
15+
opcache
16+
zend_test
17+
--INI--
18+
opcache.enable=1
19+
opcache.enable_cli=1
20+
opcache.file_update_protection=0
21+
opcache.jit=tracing
22+
opcache.jit_buffer_size=64M
23+
opcache.jit_hot_func=1
24+
opcache.jit_hot_loop=1
25+
opcache.jit_hot_return=1
26+
opcache.jit_hot_side_exit=1
27+
zend_test.observer.set_vm_interrupt_on_begin=1
28+
--FILE--
29+
<?php
30+
function get(VmInterruptComparable $left, VmInterruptComparable $right, array $arr) {
31+
// Raise an interrupt without an internal call that would handle it immediately.
32+
$result = $left == $right;
33+
return $arr['x'] ?? 'MISSING';
34+
}
35+
36+
$left = new VmInterruptComparable(1);
37+
$right = new VmInterruptComparable(2);
38+
for ($i = 0; $i < 5; $i++) {
39+
var_dump(get($left, $right, ['x' => $i]));
40+
}
41+
?>
42+
--EXPECT--
43+
int(0)
44+
int(1)
45+
int(2)
46+
int(3)
47+
int(4)

0 commit comments

Comments
 (0)