Skip to content

Fixed GH-16233: Observer segfault when calling user function in internal function via trampoline #16252

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
4 changes: 3 additions & 1 deletion Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "zend_closures.h"
#include "zend_compile.h"
#include "zend_hash.h"
#include "zend_observer.h"

#define DEBUG_OBJECT_HANDLERS 0

Expand Down Expand Up @@ -1294,7 +1295,8 @@ ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend
* value so that it doesn't contain garbage when the engine allocates space for the next stack
* frame. This didn't cause any issues until now due to "lucky" structure layout. */
func->last_var = 0;
func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, 2) : 2;
uint32_t min_T = 2 + ZEND_OBSERVER_ENABLED;
func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, min_T) : min_T;
func->filename = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.filename : ZSTR_EMPTY_ALLOC();
func->line_start = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_start : 0;
func->line_end = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_end : 0;
Expand Down
19 changes: 19 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,23 @@ static ZEND_METHOD(_ZendTestMagicCall, __call)
RETURN_ARR(zend_new_pair(&name_zv, arguments));
}

static ZEND_METHOD(_ZendTestMagicCallForward, __call)
{
zend_string *name;
zval *arguments;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(name)
Z_PARAM_ARRAY(arguments)
ZEND_PARSE_PARAMETERS_END();

ZEND_IGNORE_VALUE(arguments);

zval func;
ZVAL_STR(&func, name);
call_user_function(NULL, NULL, &func, return_value, 0, NULL);
}

PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
Expand Down Expand Up @@ -993,6 +1010,8 @@ PHP_MINIT_FUNCTION(zend_test)

zend_test_magic_call = register_class__ZendTestMagicCall();

register_class__ZendTestMagicCallForward();

zend_register_functions(NULL, ext_function_legacy, NULL, EG(current_module)->type);

// Loading via dl() not supported with the observer API
Expand Down
5 changes: 5 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class _ZendTestMagicCall
public function __call(string $name, array $args): mixed {}
}

class _ZendTestMagicCallForward
{
public function __call(string $name, array $args): mixed {}
}

class _ZendTestChildClass extends _ZendTestClass
{
public function returnsThrowable(): Exception {}
Expand Down
21 changes: 20 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions ext/zend_test/tests/gh16233.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-16233 (Observer segfault when calling user function in internal function via trampoline)
--EXTENSIONS--
zend_test
--INI--
zend_test.observer.enabled=1
zend_test.observer.show_output=1
zend_test.observer.observe_all=1
--FILE--
<?php

function callee() {
echo "in callee\n";
}

$test = new _ZendTestMagicCallForward;
$test->callee();
echo "done\n";

?>
--EXPECTF--
<!-- init '%sgh16233.php' -->
<file '%sgh16233.php'>
<!-- init _ZendTestMagicCallForward::__call() -->
<_ZendTestMagicCallForward::__call>
<!-- init callee() -->
<callee>
in callee
</callee>
</_ZendTestMagicCallForward::__call>
done
</file '%sgh16233.php'>
Loading