Skip to content

Commit d58d610

Browse files
committed
JIT: Check exception on exit
1 parent f146974 commit d58d610

File tree

7 files changed

+164
-16
lines changed

7 files changed

+164
-16
lines changed

Diff for: ext/opcache/jit/zend_jit_internal.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,18 @@ typedef enum _zend_jit_trace_stop {
313313

314314
#define ZEND_JIT_TRACE_SUPPORTED 0
315315

316-
#define ZEND_JIT_EXIT_JITED (1<<0)
317-
#define ZEND_JIT_EXIT_BLACKLISTED (1<<1)
318-
#define ZEND_JIT_EXIT_TO_VM (1<<2) /* exit to VM without attempt to create a side trace */
319-
#define ZEND_JIT_EXIT_RESTORE_CALL (1<<3) /* deoptimizer should restore EX(call) chain */
320-
#define ZEND_JIT_EXIT_POLYMORPHISM (1<<4) /* exit because of polymorphic call */
321-
#define ZEND_JIT_EXIT_FREE_OP1 (1<<5)
322-
#define ZEND_JIT_EXIT_FREE_OP2 (1<<6)
323-
#define ZEND_JIT_EXIT_PACKED_GUARD (1<<7)
324-
#define ZEND_JIT_EXIT_CLOSURE_CALL (1<<8) /* exit because of polymorphic INIT_DYNAMIC_CALL call */
325-
#define ZEND_JIT_EXIT_METHOD_CALL (1<<9) /* exit because of polymorphic INIT_METHOD_CALL call */
326-
#define ZEND_JIT_EXIT_INVALIDATE (1<<10) /* invalidate current trace */
316+
#define ZEND_JIT_EXIT_JITED (1<<0)
317+
#define ZEND_JIT_EXIT_BLACKLISTED (1<<1)
318+
#define ZEND_JIT_EXIT_TO_VM (1<<2) /* exit to VM without attempt to create a side trace */
319+
#define ZEND_JIT_EXIT_RESTORE_CALL (1<<3) /* deoptimizer should restore EX(call) chain */
320+
#define ZEND_JIT_EXIT_POLYMORPHISM (1<<4) /* exit because of polymorphic call */
321+
#define ZEND_JIT_EXIT_FREE_OP1 (1<<5)
322+
#define ZEND_JIT_EXIT_FREE_OP2 (1<<6)
323+
#define ZEND_JIT_EXIT_PACKED_GUARD (1<<7)
324+
#define ZEND_JIT_EXIT_CLOSURE_CALL (1<<8) /* exit because of polymorphic INIT_DYNAMIC_CALL call */
325+
#define ZEND_JIT_EXIT_METHOD_CALL (1<<9) /* exit because of polymorphic INIT_METHOD_CALL call */
326+
#define ZEND_JIT_EXIT_INVALIDATE (1<<10) /* invalidate current trace */
327+
#define ZEND_JIT_EXIT_CHECK_EXCEPTION (1<<11)
327328

328329
#define ZEND_JIT_EXIT_FIXED (1U<<31) /* the exit_info can't be changed by zend_jit_snapshot_handler() */
329330

Diff for: ext/opcache/jit/zend_jit_ir.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14696,7 +14696,7 @@ static int zend_jit_fetch_obj(zend_jit_ctx *jit,
1469614696
ZEND_ASSERT(end_inputs == IR_UNUSED);
1469714697
if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
1469814698
uint8_t type = concrete_type(res_info);
14699-
uint32_t flags = 0;
14699+
uint32_t flags = ZEND_JIT_EXIT_CHECK_EXCEPTION;
1470014700

1470114701
if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
1470214702
&& !delayed_fetch_this

Diff for: ext/opcache/jit/zend_jit_trace.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -3498,7 +3498,7 @@ static int zend_jit_trace_exit_needs_deoptimization(uint32_t trace_num, uint32_t
34983498
uint32_t stack_size;
34993499
zend_jit_trace_stack *stack;
35003500

3501-
if (opline || (flags & (ZEND_JIT_EXIT_RESTORE_CALL|ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2))) {
3501+
if (opline || (flags & (ZEND_JIT_EXIT_RESTORE_CALL|ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2|ZEND_JIT_EXIT_CHECK_EXCEPTION))) {
35023502
return 1;
35033503
}
35043504

@@ -3658,7 +3658,7 @@ static int zend_jit_trace_deoptimization(
36583658
}
36593659
}
36603660

3661-
if (flags & (ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2)) {
3661+
if (flags & (ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2|ZEND_JIT_EXIT_CHECK_EXCEPTION)) {
36623662
zend_jit_check_exception(jit);
36633663
}
36643664

@@ -8719,9 +8719,14 @@ int ZEND_FASTCALL zend_jit_trace_exit(uint32_t exit_num, zend_jit_registers_buf
87198719
EX(opline) = opline-1;
87208720
zval_ptr_dtor_nogc(EX_VAR((opline-1)->op1.var));
87218721
}
8722-
if (t->exit_info[exit_num].flags & (ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2)) {
8722+
if (t->exit_info[exit_num].flags & (ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2|ZEND_JIT_EXIT_CHECK_EXCEPTION)) {
87238723
if (EG(exception)) {
8724-
return 1;
8724+
/* EX(opline) was overridden in zend_jit_trace_exit_stub(),
8725+
* and may be wrong when IP is reused. */
8726+
if (GCC_GLOBAL_REGS) {
8727+
EX(opline) = EG(exception_op);
8728+
}
8729+
return 0;
87258730
}
87268731
}
87278732
if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL) {

Diff for: ext/opcache/tests/jit/gh18262-001.phpt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-18262 001 (Assertion failure Zend/zend_vm_execute.h JIT)
3+
--CREDITS--
4+
YuanchengJiang
5+
--FILE--
6+
<?php
7+
#[AllowDynamicProperties]
8+
class B {
9+
public int $fusion;
10+
}
11+
class C extends B {
12+
}
13+
class D extends C {
14+
public function __destruct() {
15+
}
16+
}
17+
$tests = [
18+
[C::class, new C()],
19+
[C::class, new B()],
20+
[D::class, new B()],
21+
];
22+
foreach ($tests as [$class, $instance]) {
23+
$obj = (new ReflectionClass($class))->newLazyProxy(function ($obj) use ($instance) {
24+
$instance->b = 1;
25+
return $instance;
26+
});
27+
var_dump($obj->b);
28+
}
29+
?>
30+
--EXPECTF--
31+
int(1)
32+
int(1)
33+
34+
Fatal error: Uncaught TypeError: %s in %s:%d
35+
Stack trace:
36+
#0 {main}
37+
thrown in %s on line %d

Diff for: ext/opcache/tests/jit/gh18262-002.phpt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
GH-18262 002 (Assertion failure Zend/zend_vm_execute.h JIT)
3+
--FILE--
4+
<?php
5+
#[AllowDynamicProperties]
6+
class B {
7+
public function __construct($init) {
8+
if ($init) {
9+
$this->b = $init;
10+
}
11+
}
12+
}
13+
14+
$tests = [
15+
new B(1),
16+
new B(0),
17+
];
18+
19+
set_error_handler(function ($_, $errstr) {
20+
throw new \Exception($errstr);
21+
});
22+
23+
foreach ($tests as $obj) {
24+
var_dump($obj->b);
25+
}
26+
?>
27+
--EXPECTF--
28+
int(1)
29+
30+
Fatal error: Uncaught Exception: Undefined property: B::$b in %s:%d
31+
Stack trace:
32+
#0 %s(%d): {closure:%s:%d}(2, 'Undefined prope...', '/home/arnaud/de...', 21)
33+
#1 {main}
34+
thrown in %s on line %d

Diff for: ext/opcache/tests/jit/gh18262-003.phpt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-18262 003 (Assertion failure Zend/zend_vm_execute.h JIT)
3+
--FILE--
4+
<?php
5+
#[AllowDynamicProperties]
6+
class B {
7+
public function __construct($init) {
8+
if ($init) {
9+
$this->b = $init;
10+
}
11+
}
12+
}
13+
14+
$tests = [
15+
new B(1),
16+
new B('str'), // slow deoptimization, create linked side trace
17+
new B(0), // jump to side trace with fast deoptimization
18+
];
19+
20+
set_error_handler(function ($_, $errstr) {
21+
throw new \Exception($errstr);
22+
});
23+
24+
foreach ($tests as $obj) {
25+
try {
26+
var_dump($obj->b);
27+
} catch (Exception $e) {
28+
printf("%s: %s\n", $e::class, $e->getMessage());
29+
}
30+
}
31+
?>
32+
--EXPECT--
33+
int(1)
34+
string(3) "str"
35+
Exception: Undefined property: B::$b

Diff for: ext/opcache/tests/jit/gh18262-004.phpt

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-18262 004 (Assertion failure Zend/zend_vm_execute.h JIT)
3+
--FILE--
4+
<?php
5+
class B {
6+
public function __construct(
7+
public $throw,
8+
) { }
9+
public function __get($name) {
10+
return $this->throw === '1' ? 'str' : 1;
11+
}
12+
public function __destruct() {
13+
if ($this->throw === '1') {
14+
throw new Exception(__METHOD__);
15+
}
16+
}
17+
}
18+
19+
$tests = [
20+
'0',
21+
'1',
22+
];
23+
24+
foreach ($tests as $test) {
25+
// Second iteration exits, and free op1 throws
26+
var_dump((new B($test))->b);
27+
}
28+
?>
29+
--EXPECTF--
30+
int(1)
31+
32+
Fatal error: Uncaught Exception: B::__destruct in %s:%d
33+
Stack trace:
34+
#0 %s(%d): B->__destruct()
35+
#1 {main}
36+
thrown in %s on line %d

0 commit comments

Comments
 (0)