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

From: Date: Mon, 02 Nov 2026 15:18:43 +0000
Subject: [php-src] master: Merge branch 'PHP-8.6'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143212@lists.php.net to get a copy of this message
Author: Ilija Tovilo (iluuu1994) Date: 2026-11-02T16:18:20+01:00 Commit: https://github.com/php/php-src/commit/60dfecc52ae67ab4f880e6b5611bf49a49ee7d18 Raw diff: https://github.com/php/php-src/commit/60dfecc52ae67ab4f880e6b5611bf49a49ee7d18.diff Merge branch 'PHP-8.6' * PHP-8.6: Don't use static closure cache when called_scope diverges Changed paths: A Zend/tests/closures/gh23911.phpt M Zend/Optimizer/compact_literals.c M Zend/zend_compile.c M Zend/zend_vm_def.h M Zend/zend_vm_execute.h Diff: diff --git a/Zend/Optimizer/compact_literals.c b/Zend/Optimizer/compact_literals.c index a73b46c0e332..6ed7e870da61 100644 --- a/Zend/Optimizer/compact_literals.c +++ b/Zend/Optimizer/compact_literals.c @@ -757,12 +757,17 @@ void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx } break; case ZEND_CALLABLE_CONVERT: - case ZEND_DECLARE_LAMBDA_FUNCTION: if (opline->extended_value != (uint32_t)-1) { opline->extended_value = cache_size; cache_size += sizeof(void *); } break; + case ZEND_DECLARE_LAMBDA_FUNCTION: + if (opline->extended_value != (uint32_t)-1) { + opline->extended_value = cache_size; + cache_size += 2 * sizeof(void *); + } + break; case ZEND_CALLABLE_CONVERT_PARTIAL: opline->extended_value = cache_size | (opline->extended_value & ZEND_PARTIAL_FLAGS); cache_size += 2 * sizeof(void *); diff --git a/Zend/tests/closures/gh23911.phpt b/Zend/tests/closures/gh23911.phpt new file mode 100644 index 000000000000..d522a9e8cbbc --- /dev/null +++ b/Zend/tests/closures/gh23911.phpt @@ -0,0 +1,86 @@ +--TEST-- +GH-23911: Cached static closure keeps static:: of the first called class +--CREDITS-- +Sander Muller (SanderMuller) +--FILE-- +<?php + +class A { + public static function name(): string { + return (static fn () => static::class)(); + } + + public static function create(): static { + return (static fn () => new static())(); + } + + public function calledClass(): string { + return (static function () { + return get_called_class(); + })(); + } +} +class B extends A {} +class C extends A {} + +var_dump(B::name(), C::name(), B::name(), A::name()); +var_dump(B::create()::class, C::create()::class); +var_dump((new B)->calledClass(), (new C)->calledClass()); + +function f() { + return static fn () => 42; +} +var_dump(f() === f()); +var_dump(B::name(...) !== C::name(...)); + +trait T { + public static function names(): array { + return (static fn () => [self::class, static::class])(); + } +} +class U1 { use T; } +class U2 { use T; } +class U3 extends U1 {} +var_dump(U1::names(), U2::names(), U3::names()); + +class N { + public static function nested(): string { + return (static fn () => (static fn () => static::class)())(); + } +} +class N2 extends N {} +var_dump(N::nested(), N2::nested(), N::nested()); + +?> +--EXPECT-- +string(1) "B" +string(1) "C" +string(1) "B" +string(1) "A" +string(1) "B" +string(1) "C" +string(1) "B" +string(1) "C" +bool(true) +bool(true) +array(2) { + [0]=> + string(2) "U1" + [1]=> + string(2) "U1" +} +array(2) { + [0]=> + string(2) "U2" + [1]=> + string(2) "U2" +} +array(2) { + [0]=> + string(2) "U1" + [1]=> + string(2) "U3" +} +string(1) "N" +string(2) "N2" +string(1) "N" diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 4ba5944db06b..834393686b31 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -9343,8 +9343,9 @@ static zend_op_array *zend_compile_func_decl_ex( && declaring_op_array->last) { zend_op *declare_lambda_op = &declaring_op_array->opcodes[declaring_op_array->last - 1]; if (declare_lambda_op->opcode == ZEND_DECLARE_LAMBDA_FUNCTION) { + /* Cache slot pair of called scope and closure. */ declare_lambda_op->extended_value = declaring_op_array->cache_size; - declaring_op_array->cache_size += sizeof(void *); + declaring_op_array->cache_size += 2 * sizeof(void *); } } } diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 6ec38be52216..1e3d9984832d 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8423,8 +8423,16 @@ ZEND_VM_HANDLER(142, ZEND_DECLARE_LAMBDA_FUNCTION, UNUSED, NUM, NUM|CACHE_SLOT) zend_class_entry *called_scope; uint32_t cache_slot = opline->extended_value; + if (Z_TYPE(EX(This)) == IS_OBJECT) { + called_scope = Z_OBJCE(EX(This)); + } else { + called_scope = Z_CE(EX(This)); + } + if (cache_slot != (uint32_t)-1) { - zend_object *closure = CACHED_PTR(cache_slot); + /* The closure is bound to the called scope, so it may only be reused + * for the same called scope. */ + zend_object *closure = CACHED_POLYMORPHIC_PTR(cache_slot, called_scope); if (closure) { ZVAL_OBJ_COPY(EX_VAR(opline->result.var), closure); ZEND_VM_NEXT_OPCODE(); @@ -8432,25 +8440,21 @@ ZEND_VM_HANDLER(142, ZEND_DECLARE_LAMBDA_FUNCTION, UNUSED, NUM, NUM|CACHE_SLOT) } func = (zend_function *) EX(func)->op_array.dynamic_func_defs[opline->op2.num]; - if (Z_TYPE(EX(This)) == IS_OBJECT) { - called_scope = Z_OBJCE(EX(This)); - if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_STATIC) || - (EX(func)->common.fn_flags & ZEND_ACC_STATIC))) { - object = NULL; - } else { - object = Z_OBJ(EX(This)); - } + if (Z_TYPE(EX(This)) == IS_OBJECT + && EXPECTED(!(func->common.fn_flags & ZEND_ACC_STATIC)) + && EXPECTED(!(EX(func)->common.fn_flags & ZEND_ACC_STATIC))) { + object = Z_OBJ(EX(This)); } else { - called_scope = Z_CE(EX(This)); object = NULL; } SAVE_OPLINE(); zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object); - if (cache_slot != (uint32_t)-1) { + if (cache_slot != (uint32_t)-1 + && !CACHED_PTR(cache_slot + sizeof(void *))) { zend_object *closure = Z_OBJ_P(EX_VAR(opline->result.var)); GC_ADDREF(closure); - CACHE_PTR(cache_slot, closure); + CACHE_POLYMORPHIC_PTR(cache_slot, called_scope, closure); zend_stack_push(&EG(lambda_cache), &closure); } ZEND_VM_NEXT_OPCODE(); diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index f85378a99531..b1d7d3d80823 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -32930,8 +32930,16 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_DECLARE_LAMBD zend_class_entry *called_scope; uint32_t cache_slot = opline->extended_value; + if (Z_TYPE(EX(This)) == IS_OBJECT) { + called_scope = Z_OBJCE(EX(This)); + } else { + called_scope = Z_CE(EX(This)); + } + if (cache_slot != (uint32_t)-1) { - zend_object *closure = CACHED_PTR(cache_slot); + /* The closure is bound to the called scope, so it may only be reused + * for the same called scope. */ + zend_object *closure = CACHED_POLYMORPHIC_PTR(cache_slot, called_scope); if (closure) { ZVAL_OBJ_COPY(EX_VAR(opline->result.var), closure); ZEND_VM_NEXT_OPCODE(); @@ -32939,25 +32947,21 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_DECLARE_LAMBD } func = (zend_function *) EX(func)->op_array.dynamic_func_defs[opline->op2.num]; - if (Z_TYPE(EX(This)) == IS_OBJECT) { - called_scope = Z_OBJCE(EX(This)); - if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_STATIC) || - (EX(func)->common.fn_flags & ZEND_ACC_STATIC))) { - object = NULL; - } else { - object = Z_OBJ(EX(This)); - } + if (Z_TYPE(EX(This)) == IS_OBJECT + && EXPECTED(!(func->common.fn_flags & ZEND_ACC_STATIC)) + && EXPECTED(!(EX(func)->common.fn_flags & ZEND_ACC_STATIC))) { + object = Z_OBJ(EX(This)); } else { - called_scope = Z_CE(EX(This)); object = NULL; } SAVE_OPLINE(); zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object); - if (cache_slot != (uint32_t)-1) { + if (cache_slot != (uint32_t)-1 + && !CACHED_PTR(cache_slot + sizeof(void *))) { zend_object *closure = Z_OBJ_P(EX_VAR(opline->result.var)); GC_ADDREF(closure); - CACHE_PTR(cache_slot, closure); + CACHE_POLYMORPHIC_PTR(cache_slot, called_scope, closure); zend_stack_push(&EG(lambda_cache), &closure); } ZEND_VM_NEXT_OPCODE(); @@ -85598,8 +85602,16 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DECLARE_LAMBDA_FUN zend_class_entry *called_scope; uint32_t cache_slot = opline->extended_value; + if (Z_TYPE(EX(This)) == IS_OBJECT) { + called_scope = Z_OBJCE(EX(This)); + } else { + called_scope = Z_CE(EX(This)); + } + if (cache_slot != (uint32_t)-1) { - zend_object *closure = CACHED_PTR(cache_slot); + /* The closure is bound to the called scope, so it may only be reused + * for the same called scope. */ + zend_object *closure = CACHED_POLYMORPHIC_PTR(cache_slot, called_scope); if (closure) { ZVAL_OBJ_COPY(EX_VAR(opline->result.var), closure); ZEND_VM_NEXT_OPCODE(); @@ -85607,25 +85619,21 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DECLARE_LAMBDA_FUN } func = (zend_function *) EX(func)->op_array.dynamic_func_defs[opline->op2.num]; - if (Z_TYPE(EX(This)) == IS_OBJECT) { - called_scope = Z_OBJCE(EX(This)); - if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_STATIC) || - (EX(func)->common.fn_flags & ZEND_ACC_STATIC))) { - object = NULL; - } else { - object = Z_OBJ(EX(This)); - } + if (Z_TYPE(EX(This)) == IS_OBJECT + && EXPECTED(!(func->common.fn_flags & ZEND_ACC_STATIC)) + && EXPECTED(!(EX(func)->common.fn_flags & ZEND_ACC_STATIC))) { + object = Z_OBJ(EX(This)); } else { - called_scope = Z_CE(EX(This)); object = NULL; } SAVE_OPLINE(); zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object); - if (cache_slot != (uint32_t)-1) { + if (cache_slot != (uint32_t)-1 + && !CACHED_PTR(cache_slot + sizeof(void *))) { zend_object *closure = Z_OBJ_P(EX_VAR(opline->result.var)); GC_ADDREF(closure); - CACHE_PTR(cache_slot, closure); + CACHE_POLYMORPHIC_PTR(cache_slot, called_scope, closure); zend_stack_push(&EG(lambda_cache), &closure); } ZEND_VM_NEXT_OPCODE();

« previous php.cvs (#143212) next »