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

From: Date: Wed, 11 Nov 2026 15:26:14 +0000
Subject: [php-src] master: Merge branch 'PHP-8.6'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143583@lists.php.net to get a copy of this message
Author: Arnaud Le Blanc (arnaud-lb) Date: 2026-11-11T16:25:54+01:00 Commit: https://github.com/php/php-src/commit/a9b50b747f247c6b3c8900ce6ba8fa3b1cff7fc1 Raw diff: https://github.com/php/php-src/commit/a9b50b747f247c6b3c8900ce6ba8fa3b1cff7fc1.diff Merge branch 'PHP-8.6' * PHP-8.6: Fix stale PFA cache entries after the declaring site is freed (#25374) Changed paths: A Zend/tests/partial_application/cache_freed_declaring_site_001.phpt A Zend/tests/partial_application/cache_freed_declaring_site_002.phpt A Zend/tests/partial_application/cache_freed_declaring_site_003.phpt A Zend/tests/partial_application/cache_freed_declaring_site_004.phpt A Zend/tests/partial_application/cache_freed_declaring_site_005.phpt M Zend/zend_ast.c M Zend/zend_compile.c M Zend/zend_partial.c M Zend/zend_partial.h M Zend/zend_vm_def.h M Zend/zend_vm_execute.h M ext/opcache/ZendAccelerator.c M ext/opcache/ZendAccelerator.h Diff: diff --git a/Zend/tests/partial_application/cache_freed_declaring_site_001.phpt b/Zend/tests/partial_application/cache_freed_declaring_site_001.phpt new file mode 100644 index 000000000000..56b4edfc932a --- /dev/null +++ b/Zend/tests/partial_application/cache_freed_declaring_site_001.phpt @@ -0,0 +1,37 @@ +--TEST-- +PFA in class constant: request cache must not outlive the freed declaring AST +--FILE-- +<?php +function f($a = 'A', $b = 'B', $c = 'C', $d = 'D', $e = 'E', $f = 'F', $g = 'G') { return "$a|$b|$c|$d|$e|$f|$g"; } + +$n = 0; +for ($round = 0; $round < 2; $round++) { + for ($argc = 1; $argc <= 7; $argc++) { + for ($mask = 1; $mask < (1 << $argc); $mask++) { + $args = []; + $expected = ['A', 'B', 'C', 'D', 'E', 'F', 'G']; + $placeholders = 0; + for ($i = 0; $i < $argc; $i++) { + if ($mask & (1 << $i)) { + $args[] = '?'; + $expected[$i] = ++$placeholders; + } else { + $args[] = "'v$i'"; + $expected[$i] = "v$i"; + } + } + $shape = 'f(' . implode(', ', $args) . ')'; + $n++; + eval("class C$n { const X = $shape; }"); + $pfa = constant("C$n::X"); + $result = $pfa(...range(1, $placeholders)); + if ($result !== implode('|', $expected)) { + echo "$shape: got $result\n"; + } + } + } +} +echo "Done\n"; +?> +--EXPECT-- +Done diff --git a/Zend/tests/partial_application/cache_freed_declaring_site_002.phpt b/Zend/tests/partial_application/cache_freed_declaring_site_002.phpt new file mode 100644 index 000000000000..56959d460c35 --- /dev/null +++ b/Zend/tests/partial_application/cache_freed_declaring_site_002.phpt @@ -0,0 +1,36 @@ +--TEST-- +PFA in eval(): request cache must not outlive the freed declaring op_array +--FILE-- +<?php +function f($a = 'A', $b = 'B', $c = 'C', $d = 'D', $e = 'E', $f = 'F', $g = 'G') { return "$a|$b|$c|$d|$e|$f|$g"; } + +$n = 0; +for ($round = 0; $round < 2; $round++) { + for ($argc = 1; $argc <= 7; $argc++) { + for ($mask = 1; $mask < (1 << $argc); $mask++) { + $args = []; + $expected = ['A', 'B', 'C', 'D', 'E', 'F', 'G']; + $placeholders = 0; + for ($i = 0; $i < $argc; $i++) { + if ($mask & (1 << $i)) { + $args[] = '?'; + $expected[$i] = ++$placeholders; + } else { + $args[] = "'v$i'"; + $expected[$i] = "v$i"; + } + } + $shape = 'f(' . implode(', ', $args) . ')'; + $n++; + $pfa = eval("return $shape;"); + $result = $pfa(...range(1, $placeholders)); + if ($result !== implode('|', $expected)) { + echo "$shape: got $result\n"; + } + } + } +} +echo "Done\n"; +?> +--EXPECT-- +Done diff --git a/Zend/tests/partial_application/cache_freed_declaring_site_003.phpt b/Zend/tests/partial_application/cache_freed_declaring_site_003.phpt new file mode 100644 index 000000000000..aa85ad6c9480 --- /dev/null +++ b/Zend/tests/partial_application/cache_freed_declaring_site_003.phpt @@ -0,0 +1,37 @@ +--TEST-- +PFA in constant declared in eval(): request cache must not outlive the freed declaring AST +--FILE-- +<?php +function f($a = 'A', $b = 'B', $c = 'C', $d = 'D', $e = 'E', $f = 'F', $g = 'G') { return "$a|$b|$c|$d|$e|$f|$g"; } + +$n = 0; +for ($round = 0; $round < 2; $round++) { + for ($argc = 1; $argc <= 7; $argc++) { + for ($mask = 1; $mask < (1 << $argc); $mask++) { + $args = []; + $expected = ['A', 'B', 'C', 'D', 'E', 'F', 'G']; + $placeholders = 0; + for ($i = 0; $i < $argc; $i++) { + if ($mask & (1 << $i)) { + $args[] = '?'; + $expected[$i] = ++$placeholders; + } else { + $args[] = "'v$i'"; + $expected[$i] = "v$i"; + } + } + $shape = 'f(' . implode(', ', $args) . ')'; + $n++; + eval("const K$n = $shape;"); + $pfa = constant("K$n"); + $result = $pfa(...range(1, $placeholders)); + if ($result !== implode('|', $expected)) { + echo "$shape: got $result\n"; + } + } + } +} +echo "Done\n"; +?> +--EXPECT-- +Done diff --git a/Zend/tests/partial_application/cache_freed_declaring_site_004.phpt b/Zend/tests/partial_application/cache_freed_declaring_site_004.phpt new file mode 100644 index 000000000000..aaa63866acae --- /dev/null +++ b/Zend/tests/partial_application/cache_freed_declaring_site_004.phpt @@ -0,0 +1,16 @@ +--TEST-- +PFAs in constants declared on the same line must not share a cache entry +--FILE-- +<?php +function f($a, $b) { return "$a|$b"; } + +class C { const X = f(?, 'x'), Y = f('y', ?); } +const K1 = f(?, 'k1'), K2 = f('k2', ?); + +var_dump((C::X)(1), (C::Y)(2), (K1)(3), (K2)(4)); +?> +--EXPECT-- +string(3) "1|x" +string(3) "y|2" +string(4) "3|k1" +string(4) "k2|4" diff --git a/Zend/tests/partial_application/cache_freed_declaring_site_005.phpt b/Zend/tests/partial_application/cache_freed_declaring_site_005.phpt new file mode 100644 index 000000000000..37212bfd1f0c --- /dev/null +++ b/Zend/tests/partial_application/cache_freed_declaring_site_005.phpt @@ -0,0 +1,42 @@ +--TEST-- +PFA in included file: request cache must not outlive the freed declaring op_array +--INI-- +opcache.enable_cli=0 +--FILE-- +<?php +function f($a = 'A', $b = 'B', $c = 'C', $d = 'D') { return "$a|$b|$c|$d"; } + +$file = __DIR__ . '/cache_freed_declaring_site_005.inc'; +for ($round = 0; $round < 2; $round++) { + for ($argc = 1; $argc <= 4; $argc++) { + for ($mask = 1; $mask < (1 << $argc); $mask++) { + $args = []; + $expected = ['A', 'B', 'C', 'D']; + $placeholders = 0; + for ($i = 0; $i < $argc; $i++) { + if ($mask & (1 << $i)) { + $args[] = '?'; + $expected[$i] = ++$placeholders; + } else { + $args[] = "'v$i'"; + $expected[$i] = "v$i"; + } + } + $shape = 'f(' . implode(', ', $args) . ')'; + file_put_contents($file, "<?php return $shape;"); + $pfa = include $file; + $result = $pfa(...range(1, $placeholders)); + if ($result !== implode('|', $expected)) { + echo "$shape: got $result\n"; + } + } + } +} +echo "Done\n"; +?> +--CLEAN-- +<?php +@unlink(__DIR__ . '/cache_freed_declaring_site_005.inc'); +?> +--EXPECT-- +Done diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 411272e5a698..8af2f60e8a0f 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -1362,10 +1362,16 @@ static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( if (uses_variadic_placeholder) { flags |= ZEND_PARTIAL_USES_VARIADIC_PLACEHOLDER; } + /* SHM names are shared by same-line PFAs, so they can't be a cache key */ + ZEND_ASSERT((flags & ZEND_PARTIAL_CACHEABLE_IN_SHM) + || !(GC_FLAGS(fcc_ast->name) & IS_STR_PERMANENT)); + /* A non-SHM AST may be freed, but the cached PFA keeps its name alive */ + const uint32_t *cache_key = (flags & ZEND_PARTIAL_CACHEABLE_IN_SHM) + ? &ast->lineno : (const uint32_t *) fcc_ast->name; zend_partial_create(result, scope, &frame->This, fptr, ZEND_CALL_NUM_ARGS(frame), ZEND_CALL_ARG(frame, 1), extra_named_params, named_positions, - fcc_ast->filename, &ast->lineno, + fcc_ast->filename, cache_key, /* declaring_op_array */ NULL, (void**)cache_slot, fcc_ast->name, flags, /* const_args */ 0); if (named_positions) { diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index c2fa556c8c11..42395006c3fb 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -12345,6 +12345,7 @@ static void zend_compile_const_expr_fcc(zend_ast **ast_ptr) if (!is_fcc) { fcc->filename = zend_string_copy(CG(active_op_array)->filename); + /* Not unique by content, but its address is a cache key until persisted */ fcc->name = zend_compile_partial_name(CG(active_op_array), fcc->lineno); } diff --git a/Zend/zend_partial.c b/Zend/zend_partial.c index 90830d1d1623..346b94400567 100644 --- a/Zend/zend_partial.c +++ b/Zend/zend_partial.c @@ -701,7 +701,8 @@ static zend_op_array *zp_compile(zval *this_ptr, zend_function *function, uint32_t argc, zval *argv, zend_array *extra_named_params, const zend_array *named_positions, zend_string *declaring_filename, - const uint32_t *declaring_lineno_ptr, void **cache_slot, + const uint32_t *declaring_lineno_ptr, + const zend_op_array *declaring_op_array, void **cache_slot, zend_string *pfa_name, uint32_t flags, uint32_t const_args) { zend_op_array *op_array = NULL; @@ -1016,7 +1017,8 @@ static zend_op_array *zp_compile(zval *this_ptr, zend_function *function, /* Takes ownership of closure_ast */ op_array = zend_accel_compile_pfa(closure_ast, declaring_filename, - declaring_lineno_ptr, function, pfa_name, flags & ZEND_PARTIAL_CACHEABLE_IN_SHM); + declaring_lineno_ptr, declaring_op_array, function, pfa_name, + flags & ZEND_PARTIAL_CACHEABLE_IN_SHM); clean: zp_names_dtor(var_names, argc); @@ -1040,7 +1042,8 @@ static const zend_op_array *zp_get_op_array(zval *this_ptr, zend_function *funct uint32_t argc, zval *argv, zend_array *extra_named_params, const zend_array *named_positions, zend_string *declaring_filename, - const uint32_t *declaring_lineno_ptr, void **cache_slot, + const uint32_t *declaring_lineno_ptr, + const zend_op_array *declaring_op_array, void **cache_slot, zend_string *pfa_name, uint32_t flags, uint32_t const_args) { if (EXPECTED(function->type == ZEND_INTERNAL_FUNCTION @@ -1056,7 +1059,7 @@ static const zend_op_array *zp_get_op_array(zval *this_ptr, zend_function *funct if (UNEXPECTED(!op_array)) { op_array = zp_compile(this_ptr, function, argc, argv, extra_named_params, named_positions, declaring_filename, declaring_lineno_ptr, - cache_slot, pfa_name, flags, const_args); + declaring_op_array, cache_slot, pfa_name, flags, const_args); } if (EXPECTED(op_array) && !(function->common.fn_flags & ZEND_ACC_NEVER_CACHE)) { @@ -1135,7 +1138,8 @@ void zend_partial_create(zval *result, zend_class_entry *scope, zval *this_ptr, uint32_t argc, zval *argv, zend_array *extra_named_params, const zend_array *named_positions, zend_string *declaring_filename, - const uint32_t *declaring_lineno_ptr, void **cache_slot, + const uint32_t *declaring_lineno_ptr, + const zend_op_array *declaring_op_array, void **cache_slot, zend_string *pfa_name, uint32_t flags, uint32_t const_args) { ZEND_ASSERT(pfa_name); @@ -1143,7 +1147,7 @@ void zend_partial_create(zval *result, zend_class_entry *scope, zval *this_ptr, const zend_op_array *op_array = zp_get_op_array(this_ptr, function, argc, argv, extra_named_params, named_positions, declaring_filename, declaring_lineno_ptr, - cache_slot, pfa_name, flags, const_args); + declaring_op_array, cache_slot, pfa_name, flags, const_args); if (UNEXPECTED(!op_array)) { ZEND_ASSERT(EG(exception)); diff --git a/Zend/zend_partial.h b/Zend/zend_partial.h index 285db1161e78..43b4c8ab5615 100644 --- a/Zend/zend_partial.h +++ b/Zend/zend_partial.h @@ -30,12 +30,14 @@ BEGIN_EXTERN_C() /* Create a partial application of 'function' * 'declaring_lineno_ptr' should be a pointer the zend_op.lineno or * zend_ast.lineno that declares the PFA. The address is used to build a cache - * key. */ + * key, so it must not be reused while cached. 'declaring_op_array' is retained + * for that purpose. */ void zend_partial_create(zval *result, zend_class_entry *scope, zval *this_ptr, zend_function *function, uint32_t argc, zval *argv, zend_array *extra_named_params, const zend_array *named_positions, zend_string *declaring_filename, - const uint32_t *declaring_lineno_ptr, void **cache_slot, + const uint32_t *declaring_lineno_ptr, + const zend_op_array *declaring_op_array, void **cache_slot, zend_string *pfa_name, uint32_t flags, uint32_t const_args); void zend_partial_op_array_dtor(zval *pDest); diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index ca8471d3d790..625ded380096 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -9921,7 +9921,7 @@ ZEND_VM_HANDLER(212, ZEND_CALLABLE_CONVERT_PARTIAL, CONST, CONST|UNUSED, NUM) (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) ? call->extra_named_params : NULL, OP2_TYPE == IS_CONST ? Z_ARRVAL_P(named_positions) : NULL, - EX(func)->op_array.filename, &opline->lineno, cache_slot, + EX(func)->op_array.filename, &opline->lineno, &EX(func)->op_array, cache_slot, pfa_name, opline->extended_value & ZEND_PARTIAL_FLAGS, const_args); if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index e75546b90787..df67ee29c612 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -8690,7 +8690,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_CALLABLE_CONV (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) ? call->extra_named_params : NULL, IS_CONST == IS_CONST ? Z_ARRVAL_P(named_positions) : NULL, - EX(func)->op_array.filename, &opline->lineno, cache_slot, + EX(func)->op_array.filename, &opline->lineno, &EX(func)->op_array, cache_slot, pfa_name, opline->extended_value & ZEND_PARTIAL_FLAGS, const_args); if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { @@ -12060,7 +12060,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_CALLABLE_CONV (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) ? call->extra_named_params : NULL, IS_UNUSED == IS_CONST ? Z_ARRVAL_P(named_positions) : NULL, - EX(func)->op_array.filename, &opline->lineno, cache_slot, + EX(func)->op_array.filename, &opline->lineno, &EX(func)->op_array, cache_slot, pfa_name, opline->extended_value & ZEND_PARTIAL_FLAGS, const_args); if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { @@ -61569,7 +61569,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_CALLABLE_CONVERT_P (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) ? call->extra_named_params : NULL, IS_CONST == IS_CONST ? Z_ARRVAL_P(named_positions) : NULL, - EX(func)->op_array.filename, &opline->lineno, cache_slot, + EX(func)->op_array.filename, &opline->lineno, &EX(func)->op_array, cache_slot, pfa_name, opline->extended_value & ZEND_PARTIAL_FLAGS, const_args); if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { @@ -64837,7 +64837,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_CALLABLE_CONVERT_P (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) ? call->extra_named_params : NULL, IS_UNUSED == IS_CONST ? Z_ARRVAL_P(named_positions) : NULL, - EX(func)->op_array.filename, &opline->lineno, cache_slot, + EX(func)->op_array.filename, &opline->lineno, &EX(func)->op_array, cache_slot, pfa_name, opline->extended_value & ZEND_PARTIAL_FLAGS, const_args); if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 7ea59be43821..495af12a5fcf 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -2113,6 +2113,7 @@ const zend_op_array *zend_accel_pfa_cache_get( zend_op_array *zend_accel_compile_pfa(zend_ast *ast, zend_string *declaring_filename, const uint32_t *declaring_lineno_ptr, + const zend_op_array *declaring_op_array, const zend_function *called_function, zend_string *pfa_func_name, bool cacheable_in_shm) { @@ -2169,21 +2170,33 @@ zend_op_array *zend_accel_compile_pfa(zend_ast *ast, destroy_op_array(script_op_array); efree(script_op_array); + const zend_op_array *retained[2]; + uint32_t num_retained = 0; if ((called_function->common.fn_flags & ZEND_ACC_CLOSURE) && called_function->type == ZEND_USER_FUNCTION && called_function->op_array.refcount) { /* Extend the lifetime of the called opcodes if * the called function is a closure. * See comment in zend_accel_pfa_key(). */ - zend_op_array *copy = zend_arena_alloc(&CG(arena), sizeof(*copy)); - memcpy(copy, called_function, sizeof(*copy)); - function_add_ref((zend_function *) copy); - /* Reference the copy in op_array->dynamic_func_defs so that it's - * destroyed when op_array is destroyed. */ + retained[num_retained++] = &called_function->op_array; + } + if (declaring_op_array && declaring_op_array->refcount) { + /* The key contains a declaring opline address, which eval() code may free and reuse. */ + retained[num_retained++] = declaring_op_array; + } + if (num_retained) { + /* Copies in dynamic_func_defs are released with op_array. */ ZEND_ASSERT(!op_array->dynamic_func_defs && !op_array->num_dynamic_func_defs); - op_array->dynamic_func_defs = safe_emalloc(1, sizeof(*op_array->dynamic_func_defs), 0); - op_array->dynamic_func_defs[0] = copy; - op_array->num_dynamic_func_defs = 1; + op_array->dynamic_func_defs = safe_emalloc(num_retained, sizeof(*op_array->dynamic_func_defs), 0); + for (uint32_t i = 0; i < num_retained; i++) { + zend_op_array *copy = zend_arena_alloc(&CG(arena), sizeof(*copy)); + memcpy(copy, retained[i], sizeof(*copy)); + /* function_add_ref() ignores ZEND_EVAL_CODE */ + copy->type = ZEND_USER_FUNCTION; + function_add_ref((zend_function *) copy); + op_array->dynamic_func_defs[i] = copy; + } + op_array->num_dynamic_func_defs = num_retained; } zend_hash_add_new_ptr(&EG(partial_function_application_cache), key, op_array); diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index 1eedee04f02b..27a810adc0d9 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -353,6 +353,7 @@ const zend_op_array *zend_accel_pfa_cache_get( zend_op_array *zend_accel_compile_pfa(zend_ast *ast, zend_string *declaring_filename, const uint32_t *declaring_lineno_ptr, + const zend_op_array *declaring_op_array, const zend_function *called_function, zend_string *pfa_func_name, bool cacheable_in_shm);

« previous php.cvs (#143583) next »