Skip to content

Reset user func trampoline values FFI may overwrite #10916

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 2 commits 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
3 changes: 3 additions & 0 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,9 @@ ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend
ZEND_MAP_PTR_INIT(func->run_time_cache, (void***)&dummy);
func->scope = fbc->common.scope;
/* reserve space for arguments, local and temporary variables */
/* last_var is overwritten by zend_ffi_cdata_get_closure() (overlapping
* internal function data) so we need to reset it */
func->last_var = 0;
func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, 2) : 2;
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;
Expand Down
43 changes: 43 additions & 0 deletions ext/ffi/tests/trampoline_reset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test
--EXTENSIONS--
ffi
--SKIPIF--
<?php
try {
$libc = FFI::cdef("int printf(const char *format, ...);", "libc.so.6");
} catch (Throwable $_) {
die('skip libc.so.6 not available');
}
?>
--INI--
ffi.enable=1
--FILE--
<?php
class Test
{
public static function __callStatic($name, $args)
{
echo "$name called\n";
}
}

$header = '
typedef struct _IO_FILE FILE;
extern FILE *stdout;
int fprintf(FILE *, const char *, ...);
int fflush(FILE *);
';
$ffi = FFI::cdef($header, 'libc.so.6');

Test::foo();
Test::bar();
$ffi->fprintf($ffi->stdout, "FFI\n");
$ffi->fflush($ffi->stdout);
Test::baz();
?>
--EXPECT--
foo called
bar called
FFI
baz called