Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Zend/tests/partial_application/constexpr_017.phpt
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions Zend/tests/partial_application/constexpr_018.phpt
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions Zend/tests/partial_application/constexpr_019.phpt
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 19 additions & 7 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 5 additions & 1 deletion Zend/zend_partial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion Zend/zend_partial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!declaring_op_array) {
if (!declaring_op_array && !zend_accel_in_shm((void*)declaring_lineno_ptr)) {

It's dead entry when declaring_lineno_ptr is in SHM

/* 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();
Expand Down
Loading