[php-src] master: Merge branch 'PHP-8.5' into PHP-8.6

From: Date: Sun, 27 Sep 2026 17:15:56 +0000
Subject: [php-src] master: Merge branch 'PHP-8.5' into PHP-8.6
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141601@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-09-27T12:50:39-04:00 Commit: https://github.com/php/php-src/commit/b015e2ae4994eccc99802f806b1c2da0183f3b2c Raw diff: https://github.com/php/php-src/commit/b015e2ae4994eccc99802f806b1c2da0183f3b2c.diff Merge branch 'PHP-8.5' into PHP-8.6 * PHP-8.5: ext/ffi: Hold a reference to the callable of an FFI callback Changed paths: A ext/ffi/tests/callback_create_failure.phpt A ext/ffi/tests/callback_dtor_call.phpt A ext/ffi/tests/callback_object_lifetime.phpt M NEWS M ext/ffi/ffi.c Diff: diff --git a/NEWS b/NEWS index 24cbe0a83a5a..631267d56476 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,10 @@ PHP NEWS . Fixed OSS-Fuzz #565486253 (coerced arg with '...' on non-variadic function). (ndossche) +- FFI: + . Fixed crashes with FFI callbacks created from __call() trampolines + and array callables whose object is released. (Ilia Alshanetsky) + - Opcache: . Fix zend_analyze_calls() call_stack buffer overrun. (Mrmaxmeier) . Fixed bug GH-23679 (Tracing JIT writes a parent private property into a diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 518a9d9c47f0..691af066d5b8 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -916,9 +916,7 @@ static void zend_ffi_callback_hash_dtor(zval *zv) /* {{{ */ zend_ffi_callback_data *callback_data = Z_PTR_P(zv); ffi_closure_free(callback_data->callback); - if (callback_data->fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) { - OBJ_RELEASE(ZEND_CLOSURE_OBJECT(callback_data->fcc.function_handler)); - } + zend_fcc_dtor(&callback_data->fcc); for (uint32_t i = 0; i < callback_data->arg_count; ++i) { if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) { efree(callback_data->arg_types[i]); @@ -934,18 +932,12 @@ static void zend_ffi_callback_hash_dtor(zval *zv) /* {{{ */ static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, void* data) /* {{{ */ { zend_ffi_callback_data *callback_data = (zend_ffi_callback_data*)data; - zend_fcall_info fci; + zval *params; zend_ffi_type *ret_type; zval retval; ALLOCA_FLAG(use_heap) - fci.size = sizeof(zend_fcall_info); - ZVAL_UNDEF(&fci.function_name); - fci.retval = &retval; - fci.params = do_alloca(sizeof(zval) *callback_data->arg_count, use_heap); - fci.param_count = callback_data->arg_count; - fci.named_params = NULL; - fci.consumed_args = 0; + params = do_alloca(sizeof(zval) *callback_data->arg_count, use_heap); if (callback_data->type->func.args) { int n = 0; @@ -953,23 +945,20 @@ static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, v ZEND_HASH_PACKED_FOREACH_PTR(callback_data->type->func.args, arg_type) { arg_type = ZEND_FFI_TYPE(arg_type); - zend_ffi_cdata_to_zval(NULL, args[n], arg_type, BP_VAR_R, &fci.params[n], (zend_ffi_flags)(arg_type->attr & ZEND_FFI_ATTR_CONST), + zend_ffi_cdata_to_zval(NULL, args[n], arg_type, BP_VAR_R, &params[n], (zend_ffi_flags)(arg_type->attr & ZEND_FFI_ATTR_CONST), false, false); n++; } ZEND_HASH_FOREACH_END(); } - ZVAL_UNDEF(&retval); - if (zend_call_function(&fci, &callback_data->fcc) != SUCCESS) { - zend_throw_error(zend_ffi_exception_ce, "Cannot call callback"); - } + zend_call_known_fcc(&callback_data->fcc, &retval, callback_data->arg_count, params, NULL); if (callback_data->arg_count) { for (uint32_t n = 0; n < callback_data->arg_count; n++) { - zval_ptr_dtor(&fci.params[n]); + zval_ptr_dtor(&params[n]); } } - free_alloca(fci.params, use_heap); + free_alloca(params, use_heap); if (EG(exception)) { zend_error_noreturn(E_ERROR, "Throwing from FFI callbacks is not allowed"); @@ -1027,12 +1016,14 @@ static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ * arg_count = type->func.args ? zend_hash_num_elements(type->func.args) : 0; if (arg_count < fcc.function_handler->common.required_num_args) { zend_throw_error(zend_ffi_exception_ce, "Attempt to assign an invalid callback, insufficient number of arguments"); + zend_release_fcall_info_cache(&fcc); return NULL; } callback = ffi_closure_alloc(sizeof(ffi_closure), &code); if (!callback) { zend_throw_error(zend_ffi_exception_ce, "Cannot allocate callback"); + zend_release_fcall_info_cache(&fcc); return NULL; } @@ -1059,6 +1050,7 @@ static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ * } efree(callback_data); ffi_closure_free(callback); + zend_release_fcall_info_cache(&fcc); return NULL; } n++; @@ -1074,6 +1066,7 @@ static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ * } efree(callback_data); ffi_closure_free(callback); + zend_release_fcall_info_cache(&fcc); return NULL; } @@ -1095,6 +1088,7 @@ free_on_failure: ; } efree(callback_data); ffi_closure_free(callback); + zend_release_fcall_info_cache(&fcc); return NULL; } @@ -1104,9 +1098,7 @@ free_on_failure: ; } zend_hash_next_index_insert_ptr(FFI_G(callbacks), callback_data); - if (fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) { - GC_ADDREF(ZEND_CLOSURE_OBJECT(fcc.function_handler)); - } + zend_fcc_addref(&callback_data->fcc); return code; } diff --git a/ext/ffi/tests/callback_create_failure.phpt b/ext/ffi/tests/callback_create_failure.phpt new file mode 100644 index 000000000000..2809d9404c2a --- /dev/null +++ b/ext/ffi/tests/callback_create_failure.phpt @@ -0,0 +1,27 @@ +--TEST-- +FFI callback creation failure releases a __call trampoline +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +<?php +$ffi = FFI::cdef("struct E {}; typedef int (*cb_t)(struct E); struct S { cb_t f; };"); + +class Callback { + public function __call(string $name, array $arguments): int { + return 0; + } +} + +$s = $ffi->new("struct S"); +try { + $s->f = [new Callback(), 'compare']; +} catch (FFI\Exception $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} +echo "Done\n"; +?> +--EXPECT-- +FFI\Exception: Cannot prepare callback CIF +Done diff --git a/ext/ffi/tests/callback_dtor_call.phpt b/ext/ffi/tests/callback_dtor_call.phpt new file mode 100644 index 000000000000..40d72227d657 --- /dev/null +++ b/ext/ffi/tests/callback_dtor_call.phpt @@ -0,0 +1,33 @@ +--TEST-- +FFI callback bound to a __call() trampoline +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +<?php +$ffi = FFI::cdef("typedef int (*cb_t)(int); struct S { cb_t f; cb_t g; };"); + +class Callback { + public function __call(string $name, array $arguments): int { + echo $name, "(", $arguments[0], ")\n"; + + return $arguments[0] * 2; + } +} + +$callback = new Callback(); +$s = $ffi->new("struct S"); +$s->g = [$callback, 'unused']; +$s->f = [$callback, 'double']; +var_dump(($s->f)(1)); +var_dump(($s->f)(2)); +var_dump(($s->f)(3)); +?> +--EXPECT-- +double(1) +int(2) +double(2) +int(4) +double(3) +int(6) diff --git a/ext/ffi/tests/callback_object_lifetime.phpt b/ext/ffi/tests/callback_object_lifetime.phpt new file mode 100644 index 000000000000..741dd5a1d74c --- /dev/null +++ b/ext/ffi/tests/callback_object_lifetime.phpt @@ -0,0 +1,31 @@ +--TEST-- +FFI callback keeps the object of an array callable alive +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +<?php +$ffi = FFI::cdef("typedef int (*cb_t)(int); struct S { cb_t f; };"); + +class Callback { + public int $factor = 21; + + public function multiply(int $x): int { + return $this->factor * $x; + } + + public function __destruct() { + echo "Callback::__destruct\n"; + } +} + +$s = $ffi->new("struct S"); +$s->f = [new Callback(), 'multiply']; +var_dump(($s->f)(2)); +echo "Done\n"; +?> +--EXPECT-- +int(42) +Done +Callback::__destruct

« previous php.cvs (#141601) next »