Skip to content

Commit a0ddc2a

Browse files
committed
JIT: Fix run_time_cache offset stored without map_ptr dereference
zend_jit_do_fcall() loaded the callee run_time_cache through the closure direct-pointer shortcut whenever the call frame was flagged as a closure call. That flag is set for every ZEND_INIT_DYNAMIC_CALL, but a dynamic call may resolve to a non-closure function whose run_time_cache is a zend_map_ptr offset. The raw offset was then stored into EX(run_time_cache) without resolving it through CG(map_ptr_base), and a later cache lookup dereferenced a bogus address. Restrict the shortcut to statically-known closures. Unknown dynamic calls fall through to the general path, which resolves both offsets and direct pointers. Fixes GH-22443 Closes GH-22459
1 parent f605d20 commit a0ddc2a

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PHP NEWS
1212
. Fixed bug GH-22158 (Tracing JIT dispatches the observer begin handler
1313
through the wrong run_time_cache slot on megamorphic calls). (ptondereau,
1414
iliaal)
15+
. Fixed bug GH-22443 (Tracing JIT SIGSEGV on megamorphic dynamic calls from
16+
an undereferenced run_time_cache map_ptr offset). (iliaal)
1517

1618
- Intl:
1719
. Fixed Locale::lookup() and locale_lookup() to return NULL instead of the

‎ext/opcache/jit/zend_jit_ir.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10221,10 +10221,7 @@ static int zend_jit_do_fcall(zend_jit_ctx *jit, const zend_op *opline, const zen
1022110221
&& ZEND_MAP_PTR_IS_OFFSET(func->op_array.run_time_cache)) {
1022210222
run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_CG(map_ptr_base)),
1022310223
(uintptr_t)ZEND_MAP_PTR(func->op_array.run_time_cache)));
10224-
} else if ((func && (func->op_array.fn_flags & ZEND_ACC_CLOSURE)) ||
10225-
(JIT_G(current_frame) &&
10226-
JIT_G(current_frame)->call &&
10227-
TRACE_FRAME_IS_CLOSURE_CALL(JIT_G(current_frame)->call))) {
10224+
} else if (func && (func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
1022810225
/* Closures always use direct pointers */
1022910226
ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
1023010227

‎ext/opcache/tests/jit/gh22443.phpt‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
GH-22443 (SIGSEGV in tracing JIT: run_time_cache offset stored without map_ptr dereference)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.jit=tracing
9+
opcache.jit_buffer_size=64M
10+
opcache.jit_max_polymorphic_calls=0
11+
--FILE--
12+
<?php
13+
final class Svc {
14+
public function m0(...$a): int { return (int) array_sum($a) + 0; }
15+
public function m1(...$a): int { return (int) array_sum($a) + 1; }
16+
public function m2(...$a): int { return (int) array_sum($a) + 2; }
17+
public function m3(...$a): int { return (int) array_sum($a) + 3; }
18+
public function m4(...$a): int { return (int) array_sum($a) + 4; }
19+
public function m5(...$a): int { return (int) array_sum($a) + 5; }
20+
public function m6(...$a): int { return (int) array_sum($a) + 6; }
21+
public function m7(...$a): int { return (int) array_sum($a) + 7; }
22+
public function coldMethod(...$a): int { return $this->helper((int) array_sum($a)); }
23+
private function helper(int $x): int { return $x + 1; }
24+
}
25+
26+
function makeListener($listener): Closure {
27+
return function ($event, $payload) use ($listener) {
28+
return $listener(...array_values($payload));
29+
};
30+
}
31+
32+
function invokeListeners(array $listeners, string $event, array $payload): int {
33+
$r = 0;
34+
foreach ($listeners as $l) {
35+
$r += $l($event, $payload);
36+
}
37+
return $r;
38+
}
39+
40+
$svc = new Svc();
41+
$warm = [];
42+
for ($k = 0; $k < 8; $k++) {
43+
$warm[] = makeListener([$svc, 'm' . $k]);
44+
}
45+
$cold = [makeListener([$svc, 'coldMethod'])];
46+
47+
$s = 0;
48+
for ($i = 0; $i < 4000000; $i++) {
49+
$s += invokeListeners($warm, 'e', [$i & 7, ($i >> 2) & 7]);
50+
}
51+
for ($j = 0; $j < 5; $j++) {
52+
$s += invokeListeners($cold, 'e', [$j, $j + 1]);
53+
}
54+
echo "done\n";
55+
?>
56+
--EXPECT--
57+
done

0 commit comments

Comments
 (0)