Commit da929e011aa for php

commit da929e011aa59be61c248dfdd69669d6f8c7772d
Author: Arnaud Le Blanc <365207+arnaud-lb@users.noreply.github.com>
Date:   Mon Nov 30 18:23:59 2026 +0100

    Fix PFA caching with file_cache_only after priming from SHM (#25709)

    PFAs that will not be cached in SHM are cached in
    EG(partial_function_application_cache) instead. The cache key is
    concat(PFA name addr, called fun addr) with the assumption that the name is not
    interned, therefore has a unique address.

    A file cache primed from SHM breaks this assumption: In that case the name is
    interned before being persisted to the file cache. On a subsequent
    file_cache_only request the PFA will not be SHM-cacheable, but its name will
    not be unique either.

    Fix by using &ast->lineno as key, which is unique as long as the AST is live.
    Invalidate related cache entries when destroying the AST.

    Co-authored-by: Jorg Adam Sowa <jorg.sowa@gmail.com>

diff --git a/NEWS b/NEWS
index b61e1048112..3f9ab4f6549 100644
--- a/NEWS
+++ b/NEWS
@@ -29,7 +29,7 @@ PHP                                                                        NEWS
     OPcache during concurrent compilation). (Nicolas Grekas)
   . Fixed use-after-free when the error handler releases a string while an
     "Illegal string offset" warning is raised for its offset in a read, ?? or
-    nested isset()/empty(). (Jorg Sowa)
+    nested isset()/empty(). (Jorg Sowa, Arnaud)
   . Fixed partial function applications in eval'd or included code, or in
     class constants, reusing a stale cached function with wrong bound
     arguments. (Jorg Sowa)
diff --git a/Zend/tests/partial_application/constexpr_017.phpt b/Zend/tests/partial_application/constexpr_017.phpt
new file mode 100644
index 00000000000..4d0628bcacd
--- /dev/null
+++ b/Zend/tests/partial_application/constexpr_017.phpt
@@ -0,0 +1,41 @@
+--TEST--
+PFA in constexpr: shared AST
+--FILE--
+<?php
+
+trait T {
+    static function f($a = self::g(?)) {
+        return $a;
+    }
+}
+
+class C {
+    use T;
+    static function g($a) {
+        printf("%s: %s\n", __CLASS__, $a);
+    }
+}
+
+class D {
+    use T;
+    static function g($a) {
+        printf("%s: %s\n", __CLASS__, $a);
+    }
+}
+
+class E {
+    use T;
+    static function g($a) {
+        printf("%s: %s\n", __CLASS__, $a);
+    }
+}
+
+C::f()(1);
+D::f()(2);
+E::f()(3);
+
+?>
+--EXPECT--
+C: 1
+D: 2
+E: 3
diff --git a/Zend/tests/partial_application/constexpr_018.phpt b/Zend/tests/partial_application/constexpr_018.phpt
new file mode 100644
index 00000000000..6c8418df8d0
--- /dev/null
+++ b/Zend/tests/partial_application/constexpr_018.phpt
@@ -0,0 +1,40 @@
+--TEST--
+PFA in constexpr: shared AST (closures)
+--FILE--
+<?php
+
+$f = eval(<<<'PHP'
+return function ($a = self::f(?)) {
+    return $a;
+};
+PHP);
+
+class C {
+    static function f($a) {
+        printf("%s: %s\n", __CLASS__, $a);
+    }
+}
+
+class D {
+    static function f($a) {
+        printf("%s: %s\n", __CLASS__, $a);
+    }
+}
+
+class E {
+    static function f($a) {
+        printf("%s: %s\n", __CLASS__, $a);
+    }
+}
+
+$f->bindTo(null, C::class)()(1);
+$f->bindTo(null, D::class)()(2);
+$f->bindTo(null, E::class)()(3);
+
+$f = null;
+
+?>
+--EXPECT--
+C: 1
+D: 2
+E: 3
diff --git a/Zend/tests/partial_application/constexpr_019.phpt b/Zend/tests/partial_application/constexpr_019.phpt
new file mode 100644
index 00000000000..fd82e20ddaa
--- /dev/null
+++ b/Zend/tests/partial_application/constexpr_019.phpt
@@ -0,0 +1,21 @@
+--TEST--
+PFA in constexpr: cache retained until shutdown
+--FILE--
+<?php
+
+function g($a, $b) {
+    return $a + $b;
+}
+
+$f = function ($x = g(1, ?)) {
+    return $x;
+};
+
+var_dump($f()(2));
+$p = $f(?);
+var_dump($p(5));
+
+?>
+--EXPECT--
+int(3)
+int(5)
diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c
index a7763a262d5..bd8d87f241c 100644
--- a/Zend/zend_ast.c
+++ b/Zend/zend_ast.c
@@ -1361,16 +1361,10 @@ 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, cache_key, /* declaring_op_array */ NULL,
+					fcc_ast->filename, &fcc_ast->lineno, /* declaring_op_array */ NULL,
 					(void**)cache_slot, fcc_ast->name, flags, /* const_args */ 0);

 			if (named_positions) {
@@ -1672,6 +1666,24 @@ ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
 		if (fcc_ast->name) {
 			zend_string_release_ex(fcc_ast->name, 0);
 		}
+		if (EG(active)) {
+			/* Invalidate related PFAs as their cache key will be invalid.
+			 * Ignore when executor is not active as the cache can not be used
+			 * at this point. */
+			zval *zv = zend_hash_index_find(&EG(partial_function_application_cache), (zend_ulong)(uintptr_t)&ast->lineno);
+			if (zv) {
+				if (Z_TYPE_P(zv) == IS_STRING) {
+					zend_hash_del(&EG(partial_function_application_cache), Z_STR_P(zv));
+				} else {
+					ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
+					ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zv), zval *val) {
+						ZEND_ASSERT(Z_TYPE_P(val) == IS_STRING);
+						zend_hash_del(&EG(partial_function_application_cache), Z_STR_P(val));
+					} ZEND_HASH_FOREACH_END();
+				}
+				zend_hash_index_del(&EG(partial_function_application_cache), (zend_ulong)(uintptr_t)&ast->lineno);
+			}
+		}
 		ast = fcc_ast->args;
 		goto tail_call;
 	}
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index f9ea5c1163e..70779f291fc 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -12291,7 +12291,6 @@ 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 5c980d76e4a..1e70f01d524 100644
--- a/Zend/zend_partial.c
+++ b/Zend/zend_partial.c
@@ -1190,5 +1190,9 @@ void zend_partial_create(zval *result, zend_class_entry *scope, zval *this_ptr,

 void zend_partial_op_array_dtor(zval *pDest)
 {
-	destroy_op_array(Z_PTR_P(pDest));
+	if (Z_TYPE_P(pDest) == IS_PTR) {
+		destroy_op_array(Z_PTR_P(pDest));
+	} else {
+		zval_ptr_dtor(pDest);
+	}
 }
diff --git a/Zend/zend_partial.h b/Zend/zend_partial.h
index 43b4c8ab561..647eeeaf52d 100644
--- a/Zend/zend_partial.h
+++ b/Zend/zend_partial.h
@@ -31,7 +31,7 @@ BEGIN_EXTERN_C()
  * '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, so it must not be reused while cached. 'declaring_op_array' is retained
- * for that purpose. */
+ * for that purpose, in the zend_op case. */
 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,
diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c
index d9513cdf54d..3c1bdc1741b 100644
--- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@ -2233,6 +2233,7 @@ const zend_op_array *zend_accel_pfa_cache_get(
 			}
 		}
 	} else {
+		ZEND_ASSERT(EG(active));
 		op_array = zend_hash_find_ptr(&EG(partial_function_application_cache), key);
 	}

@@ -2330,8 +2331,33 @@ zend_op_array *zend_accel_compile_pfa(zend_ast *ast,
 			op_array->num_dynamic_func_defs = num_retained;
 		}

+		ZEND_ASSERT(EG(active));
 		zend_hash_add_new_ptr(&EG(partial_function_application_cache), key, op_array);
-		zend_string_release(key);
+
+		if (!declaring_op_array && !zend_accel_in_shm((void*)declaring_lineno_ptr)) {
+			/* Also index the op_array by declaring_lineno_ptr so that zend_ast_destroy() can find and invalidate the
+			 * entry when declaring_lineno_ptr is going to be freed. */
+			zval *zv = zend_hash_index_lookup(&EG(partial_function_application_cache), (zend_ulong)(uintptr_t)declaring_lineno_ptr);
+			if (Z_TYPE_P(zv) == IS_NULL) {
+				ZVAL_STR(zv, key);
+			} else {
+				zend_array *ht;
+				if (Z_TYPE_P(zv) == IS_STRING) {
+					ht = zend_new_array(2);
+					zend_hash_next_index_insert(ht, zv);
+					ZVAL_ARR(zv, ht);
+				} else {
+					ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
+					ht = Z_ARRVAL_P(zv);
+				}
+
+				zval value;
+				ZVAL_STR(&value, key);
+				zend_hash_next_index_insert(ht, &value);
+			}
+		} else {
+			zend_string_release(key);
+		}

 		zend_emit_recorded_errors();
 		zend_free_recorded_errors();