Skip to content

Fix GH-10496: Segfault in fiber with nested calls #10508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Zend/tests/fibers/gh10496.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Bug GH-10496 (Segfault when garbage collector is invoked inside of fiber)
--FILE--
<?php

function x(&$ref) {
$ref = new class() {
function __destruct() {
print "Dtor x()\n";
}
};
}
function suspend($x) {
Fiber::suspend();
}
$f = new Fiber(function() use (&$f) {
try {
x($var);
\ord(suspend(1));
} finally {
print "Cleaned\n";
}
});
$f->start();
unset($f);
gc_collect_cycles();
print "Collected\n";

?>
--EXPECT--
Cleaned
Dtor x()
Collected
10 changes: 9 additions & 1 deletion Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -4116,6 +4116,8 @@ static zend_always_inline zend_generator *zend_get_running_generator(EXECUTE_DAT

ZEND_API void zend_unfinished_calls_gc(zend_execute_data *execute_data, zend_execute_data *call, uint32_t op_num, zend_get_gc_buffer *buf) /* {{{ */
{
bool skip_current_call = (EX(func)->op_array.fn_flags & ZEND_ACC_GENERATOR) == 0 || (((zend_generator *) EX(return_value))->flags & ZEND_GENERATOR_IN_FIBER) != 0;

zend_op *opline = EX(func)->op_array.opcodes + op_num;
int level;
int do_exit;
Expand All @@ -4131,6 +4133,7 @@ ZEND_API void zend_unfinished_calls_gc(zend_execute_data *execute_data, zend_exe
opline->opcode == ZEND_NEW)) {
ZEND_ASSERT(op_num);
opline--;
skip_current_call = false;
}

do {
Expand Down Expand Up @@ -4189,7 +4192,7 @@ ZEND_API void zend_unfinished_calls_gc(zend_execute_data *execute_data, zend_exe
opline--;
}
} while (!do_exit);
if (call->prev_execute_data) {
if (skip_current_call || call->prev_execute_data) {
/* skip current call region */
level = 0;
do_exit = 0;
Expand Down Expand Up @@ -4219,6 +4222,11 @@ ZEND_API void zend_unfinished_calls_gc(zend_execute_data *execute_data, zend_exe
} while (!do_exit);
}

if (skip_current_call) {
skip_current_call = false;
continue;
}

if (EXPECTED(num_args > 0)) {
zval *p = ZEND_CALL_ARG(call, 1);
do {
Expand Down