From b520aaf7bddc8d5d7322e50c9efe3f2c9e79055a Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 24 Aug 2026 18:28:58 +0100 Subject: [PATCH 1/2] Zend: grab closure info for FCC directly in Closure::__invoke() This only affects direct calls to Closure->__invoke(). --- Zend/zend_closures.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c index 76fecf928c70..87779dabd09e 100644 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@ -44,7 +44,6 @@ typedef struct _zend_closure { ZEND_API zend_class_entry *zend_ce_closure; static zend_object_handlers closure_handlers; -static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only); static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zend_object *this_ptr, bool is_fake, uint32_t flags); static inline uint32_t zend_closure_flags(const zend_closure *closure) @@ -68,11 +67,14 @@ ZEND_METHOD(Closure, __invoke) /* {{{ */ Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args) ZEND_PARSE_PARAMETERS_END(); - zend_fcall_info_cache fcc = { + zend_closure *closure = (zend_closure*)Z_OBJ_P(ZEND_THIS); + const zend_fcall_info_cache fcc = { + .function_handler = &closure->func, + .calling_scope = closure->called_scope, + .called_scope = closure->called_scope, + .object = closure->this_ptr, .closure = Z_OBJ_P(ZEND_THIS), }; - zend_closure_get_closure(Z_OBJ_P(ZEND_THIS), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false); - fcc.called_scope = fcc.calling_scope; zend_call_known_fcc(&fcc, return_value, num_args, args, named_args); zend_return_unwrap_ref(execute_data, return_value); From 368aeaf553e353611201423570d9d7859496b540 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 10 Sep 2026 16:38:48 +0100 Subject: [PATCH 2/2] Zend: add zend_get_closure() API Which is a small wrapper around zend_object->handlers->get_closure --- Zend/zend_API.c | 4 +-- Zend/zend_closures.h | 4 +++ Zend/zend_execute.c | 44 ++++++++++++++++----------------- ext/reflection/php_reflection.c | 17 ++++++------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Zend/zend_API.c b/Zend/zend_API.c index c19e9d64e266..81991a1e8385 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -4212,9 +4212,9 @@ ZEND_API bool zend_is_callable_at_frame( } case IS_OBJECT: - if (!Z_OBJ_HANDLER_P(callable, get_closure) || Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == FAILURE) { + if (UNEXPECTED(!zend_get_closure(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, true))) { if (error) *error = estrdup("no array or string given"); - return 0; + return false; } fcc->called_scope = fcc->calling_scope; fcc->closure = Z_OBJ_P(callable); diff --git a/Zend/zend_closures.h b/Zend/zend_closures.h index 8d4572842662..69c6ba3b823e 100644 --- a/Zend/zend_closures.h +++ b/Zend/zend_closures.h @@ -46,6 +46,10 @@ ZEND_API bool zend_fn_closure_equals_ex( const zend_function *fn_b, const zend_object *closure_zobj_b ); +zend_always_inline bool zend_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **this_ptr, bool check_only) { + return obj->handlers->get_closure && obj->handlers->get_closure(obj, ce_ptr, fptr_ptr, this_ptr, check_only); +} + END_EXTERN_C() #endif diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index b166bf7dc75c..3bdf815d8fd0 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -5213,33 +5213,31 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zend_o zend_object *object; uint32_t call_info; - if (EXPECTED(function->handlers->get_closure) && - EXPECTED(function->handlers->get_closure(function, &called_scope, &fbc, &object, 0) == SUCCESS)) { - - object_or_called_scope = called_scope; - if (EXPECTED(fbc->common.fn_flags & ZEND_ACC_CLOSURE)) { - /* Delay closure destruction until its invocation */ - GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc)); - ZEND_ASSERT(ZEND_ACC_FAKE_CLOSURE == ZEND_CALL_FAKE_CLOSURE); - call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE | - (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE); - if (object) { - call_info |= ZEND_CALL_HAS_THIS; - object_or_called_scope = object; - } - } else { - call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC; - if (object) { - call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS; - /* For $this pointer */ - object_or_called_scope = zend_object_copy(object); - } - } - } else { + if (UNEXPECTED(!zend_get_closure(function, &called_scope, &fbc, &object, false))) { zend_throw_error(NULL, "Object of type %pS is not callable", function->ce->name); return NULL; } + object_or_called_scope = called_scope; + if (EXPECTED(fbc->common.fn_flags & ZEND_ACC_CLOSURE)) { + /* Delay closure destruction until its invocation */ + GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc)); + ZEND_ASSERT(ZEND_ACC_FAKE_CLOSURE == ZEND_CALL_FAKE_CLOSURE); + call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE | + (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE); + if (object) { + call_info |= ZEND_CALL_HAS_THIS; + object_or_called_scope = object; + } + } else { + call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC; + if (object) { + call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS; + GC_ADDREF(object); /* For $this pointer */ + object_or_called_scope = object; + } + } + if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) { init_func_run_time_cache(&fbc->op_array); } diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 311208e419f8..7f91507ecf3b 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1813,11 +1813,12 @@ ZEND_METHOD(ReflectionFunctionAbstract, getClosureCalledClass) if (!Z_ISUNDEF(intern->obj)) { zend_class_entry *called_scope; zend_function *closure_func; - zend_object *object; - if (Z_OBJ_HANDLER(intern->obj, get_closure) - && Z_OBJ_HANDLER(intern->obj, get_closure)(Z_OBJ(intern->obj), &called_scope, &closure_func, &object, true) == SUCCESS - && closure_func && (called_scope || closure_func->common.scope) - ) { + zend_object *this_ptr; + + if (zend_get_closure(Z_OBJ(intern->obj), &called_scope, &closure_func, &this_ptr, true) + /* if zend_get_closure succeeds we have a non-NULL closure_func */ + && (called_scope || closure_func->common.scope)) { + ZEND_ASSERT(closure_func); zend_reflection_class_factory(called_scope ? called_scope : closure_func->common.scope, return_value); } } @@ -2070,8 +2071,7 @@ ZEND_METHOD(ReflectionFunction, invoke) fcc.object = NULL; if (!Z_ISUNDEF(intern->obj)) { - Z_OBJ_HT(intern->obj)->get_closure( - Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, false); + zend_get_closure(Z_OBJ(intern->obj), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false); } zval retval; @@ -2103,8 +2103,7 @@ ZEND_METHOD(ReflectionFunction, invokeArgs) fcc.object = NULL; if (!Z_ISUNDEF(intern->obj)) { - Z_OBJ_HT(intern->obj)->get_closure( - Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, false); + zend_get_closure(Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, false); } zval retval;