From 55193be135b5d9ecf5ad99e09825902e96a5d1c1 Mon Sep 17 00:00:00 2001 From: Pratik Bhujel Date: Wed, 18 Nov 2026 09:32:50 +0545 Subject: [PATCH] Fix GH-25380: Do not cache stateless closures with heap runtime cache ZEND_DECLARE_LAMBDA_FUNCTION caches closures in the declaring op_array's runtime cache and pushes a reference onto EG(lambda_cache), which is only released at request shutdown. When the declaring function has ZEND_ACC_HEAP_RT_CACHE (Closure::bind/bindTo with scope changes, Closure::call, or closures inside trait methods reused across classes), its runtime cache is freed with the closure or call, leaving unreachable closures accumulating in EG(lambda_cache) for the rest of the request. Skip stateless closure caching when EX(func) uses ZEND_ACC_HEAP_RT_CACHE. --- Zend/tests/closures/gh25380.phpt | 96 ++++++++++++++++++++++++++++++++ Zend/zend_vm_def.h | 1 + Zend/zend_vm_execute.h | 2 + 3 files changed, 99 insertions(+) create mode 100644 Zend/tests/closures/gh25380.phpt diff --git a/Zend/tests/closures/gh25380.phpt b/Zend/tests/closures/gh25380.phpt new file mode 100644 index 000000000000..2c7d97d9be45 --- /dev/null +++ b/Zend/tests/closures/gh25380.phpt @@ -0,0 +1,96 @@ +--TEST-- +GH-25380: Stateless closure cache must not retain closures when declaring closure has a heap runtime cache +--FILE-- + static::class)(); +}; + +$gen = function () { + yield (static fn () => static::class)(); +}; + +trait T { + public function m($k) { + $f = function () use ($k) { + return (static fn ($x) => $x + 1)($k); + }; + return $f(); + } +} + +class U1 { use T; } +class U2 { use T; } + +class Repo { + public function get(array $rows) { + return array_map(function ($r) { + return (static fn ($x) => $x * 2)($r); + }, $rows); + } +} + +$obj = new A(); +$u1 = new U1(); +$u2 = new U2(); +$repo = new Repo(); + +$cases = [ + 'bind' => static function () use ($outer) { + return Closure::bind($outer, null, A::class)(); + }, + 'bindTo' => static function () use ($outer) { + return $outer->bindTo(null, A::class)(); + }, + 'call' => static function () use ($outer, $obj) { + return $outer->call($obj); + }, + 'generator_bind' => static function () use ($gen) { + return Closure::bind($gen, null, A::class)()->current(); + }, + 'trait_second_class' => static function () use ($u2) { + return $u2->m(41); + }, + 'method_closure' => static function () use ($repo) { + return $repo->get([21])[0]; + }, +]; + +// Warm up before measuring +$u1->m(0); +foreach ($cases as $fn) { + $fn(); +} + +foreach ($cases as $name => $fn) { + $before = memory_get_usage(); + for ($i = 0; $i < 20000; $i++) { + $result = $fn(); + } + $growth = memory_get_usage() - $before; + var_dump($name, $result, $growth < 4096); +} + +?> +--EXPECT-- +string(4) "bind" +string(1) "A" +bool(true) +string(6) "bindTo" +string(1) "A" +bool(true) +string(4) "call" +string(1) "A" +bool(true) +string(14) "generator_bind" +string(1) "A" +bool(true) +string(18) "trait_second_class" +int(42) +bool(true) +string(14) "method_closure" +int(42) +bool(true) diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index d10c891a730f..c730a3b1231e 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8454,6 +8454,7 @@ ZEND_VM_HANDLER(142, ZEND_DECLARE_LAMBDA_FUNCTION, UNUSED, NUM, NUM|CACHE_SLOT) zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object); if (cache_slot != (uint32_t)-1 + && !(EX(func)->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) && !CACHED_PTR(cache_slot + sizeof(void *))) { zend_object *closure = Z_OBJ_P(EX_VAR(opline->result.var)); GC_ADDREF(closure); diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 8679330b2d0a..2fb575ea1852 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -32975,6 +32975,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_DECLARE_LAMBD zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object); if (cache_slot != (uint32_t)-1 + && !(EX(func)->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) && !CACHED_PTR(cache_slot + sizeof(void *))) { zend_object *closure = Z_OBJ_P(EX_VAR(opline->result.var)); GC_ADDREF(closure); @@ -85668,6 +85669,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DECLARE_LAMBDA_FUN zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object); if (cache_slot != (uint32_t)-1 + && !(EX(func)->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) && !CACHED_PTR(cache_slot + sizeof(void *))) { zend_object *closure = Z_OBJ_P(EX_VAR(opline->result.var)); GC_ADDREF(closure);