Skip to content

Commit 71f1451

Browse files
danogtrowski
authored andcommittedOct 11, 2023
Fix GH-11121: ReflectionFiber segfault
Closes GH-12391. Co-authored-by: Aaron Piotrowski <aaron@trowski.com>
1 parent 1f4159e commit 71f1451

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed
 

‎NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.26
44

5+
- Fiber:
6+
. Fixed bug GH-11121 (ReflectionFiber segfault). (danog, trowski, bwoebi)
7+
58
- Opcache:
69
. Added warning when JIT cannot be enabled. (danog)
710
. Fixed bug GH-8143 (Crashes in zend_accel_inheritance_cache_find since

‎Zend/zend_fibers.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ static zend_always_inline zend_fiber_transfer zend_fiber_resume(zend_fiber *fibe
554554
{
555555
zend_fiber *previous = EG(active_fiber);
556556

557+
if (previous) {
558+
previous->execute_data = EG(current_execute_data);
559+
}
560+
557561
fiber->caller = EG(current_fiber_context);
558562
EG(active_fiber) = fiber;
559563

@@ -571,6 +575,7 @@ static zend_always_inline zend_fiber_transfer zend_fiber_suspend(zend_fiber *fib
571575
zend_fiber_context *caller = fiber->caller;
572576
fiber->previous = EG(current_fiber_context);
573577
fiber->caller = NULL;
578+
fiber->execute_data = EG(current_execute_data);
574579

575580
return zend_fiber_switch_to(caller, value, false);
576581
}
@@ -741,7 +746,6 @@ ZEND_METHOD(Fiber, suspend)
741746

742747
ZEND_ASSERT(fiber->context.status == ZEND_FIBER_STATUS_RUNNING || fiber->context.status == ZEND_FIBER_STATUS_SUSPENDED);
743748

744-
fiber->execute_data = EG(current_execute_data);
745749
fiber->stack_bottom->prev_execute_data = NULL;
746750

747751
zend_fiber_transfer transfer = zend_fiber_suspend(fiber, value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--TEST--
2+
GH-11121: Segfault when using ReflectionFiber
3+
--FILE--
4+
<?php
5+
function f() {
6+
Fiber::suspend();
7+
}
8+
9+
function g() {
10+
(new Fiber(function() {
11+
global $f;
12+
var_dump((new ReflectionFiber($f))->getTrace());
13+
}))->start();
14+
}
15+
16+
$f = new Fiber(function() { f(); max(...[1,2,3,4,5,6,7,8,9,10,11,12]); g(); });
17+
$f->start();
18+
$f->resume();
19+
20+
?>
21+
--EXPECTF--
22+
array(3) {
23+
[0]=>
24+
array(7) {
25+
["file"]=>
26+
string(%d) "%sReflectionFiber_bug_gh11121_1.php"
27+
["line"]=>
28+
int(10)
29+
["function"]=>
30+
string(5) "start"
31+
["class"]=>
32+
string(5) "Fiber"
33+
["object"]=>
34+
object(Fiber)#3 (0) {
35+
}
36+
["type"]=>
37+
string(2) "->"
38+
["args"]=>
39+
array(0) {
40+
}
41+
}
42+
[1]=>
43+
array(4) {
44+
["file"]=>
45+
string(%d) "%sReflectionFiber_bug_gh11121_1.php"
46+
["line"]=>
47+
int(13)
48+
["function"]=>
49+
string(1) "g"
50+
["args"]=>
51+
array(0) {
52+
}
53+
}
54+
[2]=>
55+
array(2) {
56+
["function"]=>
57+
string(9) "{closure}"
58+
["args"]=>
59+
array(0) {
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
GH-11121: Segfault when using ReflectionFiber
3+
--FILE--
4+
<?php
5+
6+
function f() {
7+
Fiber::suspend();
8+
}
9+
10+
function g() {
11+
(new Fiber(function() {
12+
global $f;
13+
var_dump((new ReflectionFiber($f))->getTrace());
14+
}))->start();
15+
}
16+
17+
$f = new Fiber(function() { f(); g(); });
18+
$f->start();
19+
$f->resume();
20+
21+
?>
22+
--EXPECTF--
23+
array(3) {
24+
[0]=>
25+
array(7) {
26+
["file"]=>
27+
string(%d) "%sReflectionFiber_bug_gh11121_2.php"
28+
["line"]=>
29+
int(11)
30+
["function"]=>
31+
string(5) "start"
32+
["class"]=>
33+
string(5) "Fiber"
34+
["object"]=>
35+
object(Fiber)#3 (0) {
36+
}
37+
["type"]=>
38+
string(2) "->"
39+
["args"]=>
40+
array(0) {
41+
}
42+
}
43+
[1]=>
44+
array(4) {
45+
["file"]=>
46+
string(%d) "%sReflectionFiber_bug_gh11121_2.php"
47+
["line"]=>
48+
int(14)
49+
["function"]=>
50+
string(1) "g"
51+
["args"]=>
52+
array(0) {
53+
}
54+
}
55+
[2]=>
56+
array(2) {
57+
["function"]=>
58+
string(9) "{closure}"
59+
["args"]=>
60+
array(0) {
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)