Skip to content
Open
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
42 changes: 24 additions & 18 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,28 +906,34 @@ static void zend_create_closure_ex(

/* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
ptr = ZEND_MAP_PTR_GET(func->op_array.run_time_cache);
if (!ptr
if ((func->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) == ZEND_ACC_CLOSURE) {
/* Closures cannot store their scope in func->common.scope, as it may be immutable.
* To work around this, we store the scope associated with a shared runtime
* cache in a slot just before the op_array's cache.
* This allows closures created in the same scope to share a single runtime cache.
* Invariant: A closure's cache is always either such a shared cache or a heap cache. */
if (!ptr) {
void **mem = zend_arena_alloc(&CG(arena), sizeof(void *) + func->op_array.cache_size);
mem[0] = scope;
ptr = mem + 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On ARMv7, mem + 1 shifts the cache by four bytes, misaligning the eight-byte zval slots used by ZEND_RECV_INIT. Round the prefix up to ZEND_MM_ALIGNMENT and store the scope immediately before the aligned cache.

memset(ptr, 0, func->op_array.cache_size);
ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With OPcache on, two closures from the same factory partially apply a trait method and then its alias. The alias calls Child::main() here but calls the trait method on the merge base. zp_get_op_array() keys both names by their shared opcodes. Please key named methods by function identity, as zend_accel_pfa_key() does, and add a PHPT.

} else if ((func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE)
|| ((zend_class_entry **)ptr)[-1] != scope) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Extensions can initialize this cache through zend_init_func_run_time_cache(), which allocates exactly cache_size bytes. That leaves no scope slot at ptr[-1]. Both allocation paths need the same layout.

ptr = NULL;
}
} else if (!ptr
|| func->common.scope != scope
|| (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE)
) {
if (!ptr
&& (func->common.fn_flags & ZEND_ACC_CLOSURE)
&& (func->common.scope == scope ||
!(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) {
/* If a real closure is used for the first time, we create a shared runtime cache
* and remember which scope it is for. */
if (func->common.scope != scope) {
func->common.scope = scope;
}
ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);
closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
} else {
/* Otherwise, we use a non-shared runtime cache */
ptr = emalloc(func->op_array.cache_size);
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
}
/* Functions and methods share their own runtime cache only for their own scope */
ptr = NULL;
}
if (!ptr) {
/* Otherwise, we use a non-shared runtime cache */
ptr = emalloc(func->op_array.cache_size);
memset(ptr, 0, func->op_array.cache_size);
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
}
ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
} else {
Expand Down
Loading