Skip to content

Fix creating a first-class callable from FFI #12916

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

Open
wants to merge 1 commit into
base: PHP-8.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,6 @@ static zend_result zend_ffi_cdata_get_closure(zend_object *obj, zend_class_entry
func->common.arg_flags[0] = 0;
func->common.arg_flags[1] = 0;
func->common.arg_flags[2] = 0;
func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
func->common.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
/* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
func->common.num_args = 0;
Expand All @@ -2161,6 +2160,7 @@ static zend_result zend_ffi_cdata_get_closure(zend_object *obj, zend_class_entry

func->internal_function.reserved[0] = type;
func->internal_function.reserved[1] = *(void**)cdata->ptr;
func->internal_function.reserved[2] = func;

*ce_ptr = NULL;
*fptr_ptr= func;
Expand Down Expand Up @@ -2843,8 +2843,9 @@ static ZEND_FUNCTION(ffi_trampoline) /* {{{ */

exit:
zend_string_release(EX(func)->common.function_name);
if (EX(func)->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
zend_free_trampoline(EX(func));
zend_function *func = EX(func)->internal_function.reserved[2];
if (func) {
zend_free_trampoline(func);
EX(func) = NULL;
}
}
Expand Down Expand Up @@ -2899,7 +2900,6 @@ static zend_function *zend_ffi_get_func(zend_object **obj, zend_string *name, co
func->common.arg_flags[0] = 0;
func->common.arg_flags[1] = 0;
func->common.arg_flags[2] = 0;
func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
func->common.function_name = zend_string_copy(name);
/* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
func->common.num_args = 0;
Expand All @@ -2912,6 +2912,7 @@ static zend_function *zend_ffi_get_func(zend_object **obj, zend_string *name, co

func->internal_function.reserved[0] = type;
func->internal_function.reserved[1] = sym->addr;
func->internal_function.reserved[2] = func;

return func;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi/tests/bug79177.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ echo "done\n";
--EXPECTF--
Warning: Uncaught RuntimeException: Not allowed in %s:%d
Stack trace:
#0 %s(%d): {closure}()
#0 [internal function]: {closure}()
#1 %s(%d): FFI->bug79177()
#2 {main}
thrown in %s on line %d
Expand Down
27 changes: 27 additions & 0 deletions ext/ffi/tests/first_class_callable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Bug: Cannot create first-class callable from CData function
--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
$libc = FFI::cdef("int printf(const char *format, ...);", "libc.so.6");
$libc->printf("Hello world\n");
$closure = $libc->printf;
$closure("%s %s\n", "Hello", "world");
$closure = $libc->printf(...);
$closure("%s %s\n", "Hello", "world");
?>
--EXPECT--
Hello world
Hello world
Hello world