Skip to content

Commit 29926c3

Browse files
committed
Merge remote-tracking branch 'derickr/gh10072-execute-ex' into PHP-8.1
2 parents 5f1311a + 44add3c commit 29926c3

File tree

5 files changed

+142
-6
lines changed

5 files changed

+142
-6
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ PHP NEWS
55
- Apache:
66
. Fixed bug GH-9949 (Partial content on incomplete POST request). (cmb)
77

8+
- Core:
9+
. Fixed bug GH-10072 (PHP crashes when execute_ex is overridden and a __call
10+
trampoline is used from internal code). (Derick)
11+
812
- Date:
913
. Fixed bug GH-9891 (DateTime modify with unixtimestamp (@) must work like
1014
setTimestamp). (Derick)

Zend/tests/gh10072-2.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-10072 (PHP crashes when execute_ex is overridden and a trampoline is used from internal code during shutdown)
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
zend_test.replace_zend_execute_ex=1
7+
opcache.jit=disable
8+
--FILE--
9+
<?php
10+
11+
class TrampolineTest {
12+
public function __call(string $name, array $arguments) {
13+
echo 'Trampoline for ', $name, PHP_EOL;
14+
}
15+
}
16+
17+
register_shutdown_function([new TrampolineTest(), 'shutdown']);
18+
?>
19+
--EXPECT--
20+
Trampoline for shutdown

Zend/tests/gh10072.phpt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
--TEST--
2+
GH-10072 (PHP crashes when execute_ex is overridden and a trampoline is used from internal code)
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
zend_test.replace_zend_execute_ex=1
7+
opcache.jit=disable
8+
--FILE--
9+
<?php
10+
class DummyStreamWrapper
11+
{
12+
/** @var resource|null */
13+
public $context;
14+
15+
/** @var resource|null */
16+
public $handle;
17+
18+
19+
public function stream_cast(int $castAs)
20+
{
21+
return $this->handle;
22+
}
23+
24+
25+
public function stream_close(): void
26+
{
27+
}
28+
29+
public function stream_open(string $path, string $mode, int $options = 0, ?string &$openedPath = null): bool
30+
{
31+
return true;
32+
}
33+
34+
35+
public function stream_read(int $count)
36+
{
37+
return 0;
38+
}
39+
40+
41+
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
42+
{
43+
return true;
44+
}
45+
46+
47+
public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
48+
{
49+
return false;
50+
}
51+
52+
53+
public function stream_stat()
54+
{
55+
return [];
56+
}
57+
58+
59+
public function stream_tell()
60+
{
61+
return [];
62+
}
63+
64+
65+
public function stream_truncate(int $newSize): bool
66+
{
67+
return true;
68+
}
69+
70+
71+
public function stream_write(string $data)
72+
{
73+
}
74+
75+
76+
public function unlink(string $path): bool
77+
{
78+
return false;
79+
}
80+
}
81+
82+
class TrampolineTest {
83+
/** @var resource|null */
84+
public $context;
85+
86+
/** @var object|null */
87+
private $wrapper;
88+
89+
public function __call(string $name, array $arguments) {
90+
if (!$this->wrapper) {
91+
$this->wrapper = new DummyStreamWrapper();
92+
}
93+
echo 'Trampoline for ', $name, PHP_EOL;
94+
return $this->wrapper->$name(...$arguments);
95+
}
96+
97+
}
98+
99+
stream_wrapper_register('custom', TrampolineTest::class);
100+
101+
102+
$fp = fopen("custom://myvar", "r+");
103+
?>
104+
--EXPECT--
105+
Trampoline for stream_open
106+
Trampoline for stream_close

Zend/zend_vm_def.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8663,7 +8663,9 @@ ZEND_VM_HANDLER(158, ZEND_CALL_TRAMPOLINE, ANY, ANY, SPEC(OBSERVER))
86638663
SAVE_OPLINE_EX();
86648664
ZEND_OBSERVER_FCALL_BEGIN(execute_data);
86658665
execute_data = EX(prev_execute_data);
8666-
LOAD_OPLINE();
8666+
if (execute_data) {
8667+
LOAD_OPLINE();
8668+
}
86678669
ZEND_ADD_CALL_FLAG(call, ZEND_CALL_TOP);
86688670
zend_execute_ex(call);
86698671
}
@@ -8713,7 +8715,7 @@ ZEND_VM_HANDLER(158, ZEND_CALL_TRAMPOLINE, ANY, ANY, SPEC(OBSERVER))
87138715

87148716
execute_data = EG(current_execute_data);
87158717

8716-
if (!EX(func) || !ZEND_USER_CODE(EX(func)->type) || (call_info & ZEND_CALL_TOP)) {
8718+
if (!execute_data || !EX(func) || !ZEND_USER_CODE(EX(func)->type) || (call_info & ZEND_CALL_TOP)) {
87178719
ZEND_VM_RETURN();
87188720
}
87198721

Zend/zend_vm_execute.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,7 +3319,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CALL_TRAMPOLINE_SPEC_HANDLER(Z
33193319
SAVE_OPLINE_EX();
33203320

33213321
execute_data = EX(prev_execute_data);
3322-
LOAD_OPLINE();
3322+
if (execute_data) {
3323+
LOAD_OPLINE();
3324+
}
33233325
ZEND_ADD_CALL_FLAG(call, ZEND_CALL_TOP);
33243326
zend_execute_ex(call);
33253327
}
@@ -3369,7 +3371,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CALL_TRAMPOLINE_SPEC_HANDLER(Z
33693371

33703372
execute_data = EG(current_execute_data);
33713373

3372-
if (!EX(func) || !ZEND_USER_CODE(EX(func)->type) || (call_info & ZEND_CALL_TOP)) {
3374+
if (!execute_data || !EX(func) || !ZEND_USER_CODE(EX(func)->type) || (call_info & ZEND_CALL_TOP)) {
33733375
ZEND_VM_RETURN();
33743376
}
33753377

@@ -3456,7 +3458,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CALL_TRAMPOLINE_SPEC_OBSERVER_
34563458
SAVE_OPLINE_EX();
34573459
zend_observer_fcall_begin(execute_data);
34583460
execute_data = EX(prev_execute_data);
3459-
LOAD_OPLINE();
3461+
if (execute_data) {
3462+
LOAD_OPLINE();
3463+
}
34603464
ZEND_ADD_CALL_FLAG(call, ZEND_CALL_TOP);
34613465
zend_execute_ex(call);
34623466
}
@@ -3506,7 +3510,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CALL_TRAMPOLINE_SPEC_OBSERVER_
35063510

35073511
execute_data = EG(current_execute_data);
35083512

3509-
if (!EX(func) || !ZEND_USER_CODE(EX(func)->type) || (call_info & ZEND_CALL_TOP)) {
3513+
if (!execute_data || !EX(func) || !ZEND_USER_CODE(EX(func)->type) || (call_info & ZEND_CALL_TOP)) {
35103514
ZEND_VM_RETURN();
35113515
}
35123516

0 commit comments

Comments
 (0)