Skip to content

Commit 8c7e856

Browse files
committed
Fix phpGH-18107: Opcache CFG jmp optimization with try-finally breaks the exception table
If there's a try-finally where the try_op starts on a basic block with a single JMP, and the JMP optimization causes that basic block to become unreachable, then we update try_op. In this case, there is no catch_op, so try_op is erroneously set to 0, we should instead set it to `b->start`.
1 parent c531f3d commit 8c7e856

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Zend/Optimizer/zend_cfg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void zend_mark_reachable_blocks(const zend_op_array *op_array, zend_cfg *
144144
end = blocks + block_map[op_array->try_catch_array[j].finally_op];
145145
while (b != end) {
146146
if (b->flags & ZEND_BB_REACHABLE) {
147-
op_array->try_catch_array[j].try_op = op_array->try_catch_array[j].catch_op;
147+
op_array->try_catch_array[j].try_op = b->start;
148148
changed = 1;
149149
zend_mark_reachable(op_array->opcodes, cfg, blocks + block_map[op_array->try_catch_array[j].try_op]);
150150
break;

ext/opcache/tests/opt/gh18107.phpt

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
GH-18107 (Opcache CFG jmp optimization with try-finally breaks the exception table)
3+
--CREDITS--
4+
SpencerMalone
5+
--EXTENSIONS--
6+
opcache
7+
--INI--
8+
opcache.optimization_level=0x10
9+
opcache.opt_debug_level=0x20000
10+
--FILE--
11+
<?php
12+
13+
if (!isset($badvar)) {
14+
throw new Exception("Should happen");
15+
}
16+
try {
17+
while (true) { }
18+
} finally {
19+
throw new Exception("Should not happen");
20+
}
21+
22+
?>
23+
--EXPECTF--
24+
$_main:
25+
; (lines=%d, args=0, vars=%d, tmps=%d)
26+
; (after optimizer)
27+
; %s
28+
0000 T1 = ISSET_ISEMPTY_CV (isset) CV0($badvar)
29+
0001 JMPNZ T1 0006
30+
0002 V3 = NEW 1 string("Exception")
31+
0003 SEND_VAL_EX string("Should happen") 1
32+
0004 DO_FCALL
33+
0005 THROW V3
34+
0006 JMP 0006
35+
0007 V6 = NEW 1 string("Exception")
36+
0008 SEND_VAL_EX string("Should not happen") 1
37+
0009 DO_FCALL
38+
0010 THROW V6
39+
0011 FAST_RET T5
40+
EXCEPTION TABLE:
41+
0006, -, 0007, 0011
42+
Fatal error: Uncaught Exception: Should happen in %s:%d
43+
Stack trace:
44+
#0 {main}
45+
thrown in %s on line %d

0 commit comments

Comments
 (0)