Skip to content

Fix Segfault when using ReflectionFiber #10439 #10478

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
16 changes: 14 additions & 2 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -7017,7 +7017,13 @@ ZEND_METHOD(ReflectionFiber, getExecutingLine)
prev_execute_data = fiber->execute_data->prev_execute_data;
}

RETURN_LONG(prev_execute_data->opline->lineno);
while (prev_execute_data && (!prev_execute_data->func || !ZEND_USER_CODE(prev_execute_data->func->common.type))) {
prev_execute_data = prev_execute_data->prev_execute_data;
}
if (prev_execute_data && prev_execute_data->func && ZEND_USER_CODE(prev_execute_data->func->common.type)) {
RETURN_LONG(prev_execute_data->opline->lineno);
}
RETURN_NULL();
}

ZEND_METHOD(ReflectionFiber, getExecutingFile)
Expand All @@ -7035,7 +7041,13 @@ ZEND_METHOD(ReflectionFiber, getExecutingFile)
prev_execute_data = fiber->execute_data->prev_execute_data;
}

RETURN_STR_COPY(prev_execute_data->func->op_array.filename);
while (prev_execute_data && (!prev_execute_data->func || !ZEND_USER_CODE(prev_execute_data->func->common.type))) {
prev_execute_data = prev_execute_data->prev_execute_data;
}
if (prev_execute_data && prev_execute_data->func && ZEND_USER_CODE(prev_execute_data->func->common.type)) {
RETURN_STR_COPY(prev_execute_data->func->op_array.filename);
}
RETURN_NULL();
}

ZEND_METHOD(ReflectionFiber, getCallable)
Expand Down
4 changes: 2 additions & 2 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,9 @@ public function __construct(Fiber $fiber) {}

public function getFiber(): Fiber {}

public function getExecutingFile(): string {}
public function getExecutingFile(): ?string {}

public function getExecutingLine(): int {}
public function getExecutingLine(): ?int {}

public function getCallable(): callable {}

Expand Down
8 changes: 5 additions & 3 deletions ext/reflection/php_reflection_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: ab0dd21b2fc7ff18c39275e1ec82211c7058c32a */
* Stub hash: 164878e6c43f0e4c8bb7a7ae1e5c9650c240d775 */

ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
Expand Down Expand Up @@ -586,9 +586,11 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_ReflectionFiber_getFiber, 0, 0, Fiber, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_ReflectionFiber_getExecutingFile arginfo_class_ReflectionFunction___toString
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionFiber_getExecutingFile, 0, 0, IS_STRING, 1)
ZEND_END_ARG_INFO()

#define arginfo_class_ReflectionFiber_getExecutingLine arginfo_class_ReflectionAttribute_getTarget
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionFiber_getExecutingLine, 0, 0, IS_LONG, 1)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionFiber_getCallable, 0, 0, IS_CALLABLE, 0)
ZEND_END_ARG_INFO()
Expand Down
31 changes: 31 additions & 0 deletions ext/reflection/tests/ReflectionFiber_notrace_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
ReflectionFiber should not segfault when inspecting fibers with no stack frames before suspend
--FILE--
<?php
$f = new Fiber(Fiber::suspend(...));
$f->start();

$reflection = new ReflectionFiber($f);

var_dump($reflection->getExecutingFile());
var_dump($reflection->getExecutingLine());
var_dump($reflection->getTrace());

?>
--EXPECTF--
NULL
NULL
array(1) {
[0]=>
array(4) {
["function"]=>
string(7) "suspend"
["class"]=>
string(5) "Fiber"
["type"]=>
string(2) "::"
["args"]=>
array(0) {
}
}
}
61 changes: 61 additions & 0 deletions ext/reflection/tests/ReflectionFiber_notrace_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
ReflectionFiber should not segfault when inspecting fibers where the previous stack frame is a native function
--FILE--
<?php

namespace test;

$f = new \Fiber(fn() => call_user_func(["Fiber", "suspend"]));
$f->start();

$reflection = new \ReflectionFiber($f);

var_dump($reflection->getExecutingFile());
var_dump($reflection->getExecutingLine());
var_dump($reflection->getTrace());

?>
--EXPECTF--
string(%d) "%sReflectionFiber_notrace_2.php"
int(5)
array(3) {
[0]=>
array(4) {
["function"]=>
string(7) "suspend"
["class"]=>
string(5) "Fiber"
["type"]=>
string(2) "::"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(%d) "%sReflectionFiber_notrace_2.php"
["line"]=>
int(5)
["function"]=>
string(14) "call_user_func"
["args"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(5) "Fiber"
[1]=>
string(7) "suspend"
}
}
}
[2]=>
array(2) {
["function"]=>
string(14) "test\{closure}"
["args"]=>
array(0) {
}
}
}