diff --git a/Zend/tests/partial_application/constexpr_017.phpt b/Zend/tests/partial_application/constexpr_017.phpt new file mode 100644 index 000000000000..4d0628bcacd9 --- /dev/null +++ b/Zend/tests/partial_application/constexpr_017.phpt @@ -0,0 +1,41 @@ +--TEST-- +PFA in constexpr: shared AST +--FILE-- + +--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 000000000000..6c8418df8d03 --- /dev/null +++ b/Zend/tests/partial_application/constexpr_018.phpt @@ -0,0 +1,40 @@ +--TEST-- +PFA in constexpr: shared AST (closures) +--FILE-- +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 000000000000..fd82e20ddaa3 --- /dev/null +++ b/Zend/tests/partial_application/constexpr_019.phpt @@ -0,0 +1,21 @@ +--TEST-- +PFA in constexpr: cache retained until shutdown +--FILE-- + +--EXPECT-- +int(3) +int(5) diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index e884ba897d86..e36923913ad6 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 abed76366229..98732f060170 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -12292,7 +12292,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 b227c1f9e257..138d3f06b48a 100644 --- a/Zend/zend_partial.c +++ b/Zend/zend_partial.c @@ -1188,5 +1188,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 43b4c8ab5615..647eeeaf52dd 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 daa5e07e8924..301789829b15 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -2212,6 +2212,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); } @@ -2309,8 +2310,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) { + /* 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();