Skip to content

Commit 03c83bc

Browse files
committed
Avoid non-immutable map_ptr indirection
1 parent 116fa65 commit 03c83bc

11 files changed

+84
-15
lines changed

Zend/zend_API.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4067,9 +4067,11 @@ ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, z
40674067
ce->info.internal.module->type == MODULE_PERSISTENT) {
40684068
ZEND_MAP_PTR_NEW(ce->static_members_table);
40694069
} else {
4070+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
40704071
ZEND_MAP_PTR_INIT(ce->static_members_table,
40714072
zend_arena_alloc(&CG(arena), sizeof(zval **)));
40724073
ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
4074+
#endif
40734075
}
40744076
}
40754077
} else {

Zend/zend_closures.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,15 @@ ZEND_METHOD(Closure, call)
161161
void *ptr;
162162

163163
my_function.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
164+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
164165
ptr = emalloc(sizeof(void*) + my_function.op_array.cache_size);
165166
ZEND_MAP_PTR_INIT(my_function.op_array.run_time_cache, ptr);
166167
ptr = (char*)ptr + sizeof(void*);
167168
ZEND_MAP_PTR_SET(my_function.op_array.run_time_cache, ptr);
169+
#else
170+
ptr = emalloc(my_function.op_array.cache_size);
171+
ZEND_MAP_PTR_INIT(my_function.op_array.run_time_cache, ptr);
172+
#endif
168173
memset(ptr, 0, my_function.op_array.cache_size);
169174
}
170175
}
@@ -689,7 +694,7 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
689694
zend_array_dup(closure->func.op_array.static_variables);
690695
}
691696
ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr,
692-
&closure->func.op_array.static_variables);
697+
closure->func.op_array.static_variables);
693698
}
694699

695700
/* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
@@ -715,10 +720,15 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
715720
} else {
716721
/* Otherwise, we use a non-shared runtime cache */
717722
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
723+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
718724
ptr = emalloc(sizeof(void*) + func->op_array.cache_size);
719725
ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
720726
ptr = (char*)ptr + sizeof(void*);
721727
ZEND_MAP_PTR_SET(closure->func.op_array.run_time_cache, ptr);
728+
#else
729+
ptr = emalloc(func->op_array.cache_size);
730+
ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
731+
#endif
722732
}
723733
memset(ptr, 0, func->op_array.cache_size);
724734
}

Zend/zend_compile.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,13 @@ static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
10361036
void zend_init_static_variables_map_ptr(zend_op_array *op_array)
10371037
{
10381038
if (op_array->static_variables) {
1039+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
10391040
ZEND_MAP_PTR_INIT(op_array->static_variables_ptr,
10401041
zend_arena_alloc(&CG(arena), sizeof(HashTable *)));
10411042
ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
1043+
#else
1044+
ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1045+
#endif
10421046
}
10431047
}
10441048

@@ -1050,8 +1054,12 @@ ZEND_API void function_add_ref(zend_function *function) /* {{{ */
10501054
(*op_array->refcount)++;
10511055
}
10521056

1057+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
10531058
ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void *)));
10541059
ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
1060+
#else
1061+
ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1062+
#endif
10551063

10561064
zend_init_static_variables_map_ptr(op_array);
10571065
}
@@ -1350,10 +1358,15 @@ ZEND_API void zend_do_delayed_early_binding(zend_op_array *op_array, uint32_t fi
13501358
void *ptr;
13511359

13521360
ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
1361+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
13531362
ptr = emalloc(op_array->cache_size + sizeof(void*));
13541363
ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
13551364
ptr = (char*)ptr + sizeof(void*);
13561365
ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
1366+
#else
1367+
ptr = emalloc(op_array->cache_size);
1368+
ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
1369+
#endif
13571370
memset(ptr, 0, op_array->cache_size);
13581371
}
13591372
run_time_cache = RUN_TIME_CACHE(op_array);
@@ -7157,8 +7170,12 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel) /* {{{
71577170
op_array->fn_flags |= ZEND_ACC_PRELOADED;
71587171
}
71597172

7173+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
71607174
ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void *)));
71617175
ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
7176+
#else
7177+
ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
7178+
#endif
71627179

71637180
op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
71647181
op_array->fn_flags |= decl->flags;

Zend/zend_execute.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,10 +3787,15 @@ static zend_always_inline void i_init_code_execute_data(zend_execute_data *execu
37873787
void *ptr;
37883788

37893789
ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
3790+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
37903791
ptr = emalloc(op_array->cache_size + sizeof(void*));
37913792
ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
37923793
ptr = (char*)ptr + sizeof(void*);
37933794
ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
3795+
#else
3796+
ptr = emalloc(op_array->cache_size);
3797+
ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
3798+
#endif
37943799
memset(ptr, 0, op_array->cache_size);
37953800
}
37963801
EX(run_time_cache) = RUN_TIME_CACHE(op_array);

Zend/zend_inheritance.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,9 +1526,11 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
15261526
ce->info.internal.module->type == MODULE_PERSISTENT) {
15271527
ZEND_MAP_PTR_NEW(ce->static_members_table);
15281528
} else {
1529+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
15291530
ZEND_MAP_PTR_INIT(ce->static_members_table,
15301531
zend_arena_alloc(&CG(arena), sizeof(zval *)));
15311532
ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
1533+
#endif
15321534
}
15331535
}
15341536
}
@@ -2610,30 +2612,43 @@ static zend_class_entry *zend_lazy_class_load(zend_class_entry *pce)
26102612
end = p + ce->function_table.nNumUsed;
26112613
for (; p != end; p++) {
26122614
zend_op_array *op_array, *new_op_array;
2615+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
26132616
void ***run_time_cache_ptr;
2617+
#endif
26142618
size_t alloc_size;
26152619

26162620
op_array = Z_PTR(p->val);
26172621
ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
26182622
ZEND_ASSERT(op_array->scope == pce);
26192623
ZEND_ASSERT(op_array->prototype == NULL);
2624+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
26202625
alloc_size = sizeof(zend_op_array) + sizeof(void *);
26212626
if (op_array->static_variables) {
26222627
alloc_size += sizeof(HashTable *);
26232628
}
2629+
#else
2630+
alloc_size = sizeof(zend_op_array);
2631+
#endif
26242632
new_op_array = zend_arena_alloc(&CG(arena), alloc_size);
26252633
Z_PTR(p->val) = new_op_array;
26262634
memcpy(new_op_array, op_array, sizeof(zend_op_array));
2627-
run_time_cache_ptr = (void***)(new_op_array + 1);
2628-
*run_time_cache_ptr = NULL;
26292635
new_op_array->fn_flags &= ~ZEND_ACC_IMMUTABLE;
26302636
new_op_array->scope = ce;
2637+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
2638+
run_time_cache_ptr = (void***)(new_op_array + 1);
2639+
*run_time_cache_ptr = NULL;
26312640
ZEND_MAP_PTR_INIT(new_op_array->run_time_cache, run_time_cache_ptr);
26322641
if (op_array->static_variables) {
26332642
HashTable **static_variables_ptr = (HashTable **) (run_time_cache_ptr + 1);
26342643
*static_variables_ptr = NULL;
26352644
ZEND_MAP_PTR_INIT(new_op_array->static_variables_ptr, static_variables_ptr);
26362645
}
2646+
#else
2647+
ZEND_MAP_PTR_INIT(new_op_array->run_time_cache, NULL);
2648+
if (op_array->static_variables) {
2649+
ZEND_MAP_PTR_INIT(new_op_array->static_variables_ptr, NULL);
2650+
}
2651+
#endif
26372652

26382653
zend_update_inherited_handler(constructor);
26392654
zend_update_inherited_handler(destructor);
@@ -2662,8 +2677,12 @@ static zend_class_entry *zend_lazy_class_load(zend_class_entry *pce)
26622677
ZVAL_COPY_VALUE(dst, src);
26632678
}
26642679
}
2680+
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
26652681
ZEND_MAP_PTR_INIT(ce->static_members_table, zend_arena_alloc(&CG(arena), sizeof(zval *)));
26662682
ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
2683+
#else
2684+
ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2685+
#endif
26672686

26682687
/* properties_info */
26692688
if (!(HT_FLAGS(&ce->properties_info) & HASH_FLAG_UNINITIALIZED)) {

Zend/zend_map_ptr.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@
6868
# define ZEND_MAP_PTR_IS_OFFSET(ptr) \
6969
(((uintptr_t)ZEND_MAP_PTR(ptr)) & 1L)
7070
# define ZEND_MAP_PTR_GET(ptr) \
71-
(*(ZEND_MAP_PTR_IS_OFFSET(ptr) ? \
72-
ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)) : \
73-
((void**)(ZEND_MAP_PTR(ptr)))))
71+
((ZEND_MAP_PTR_IS_OFFSET(ptr) ? \
72+
ZEND_MAP_PTR_GET_IMM(ptr) : \
73+
((void*)(ZEND_MAP_PTR(ptr)))))
7474
# define ZEND_MAP_PTR_GET_IMM(ptr) \
7575
(*ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)))
7676
# define ZEND_MAP_PTR_SET(ptr, val) do { \
77-
void **__p = (void**)(ZEND_MAP_PTR(ptr)); \
7877
if (ZEND_MAP_PTR_IS_OFFSET(ptr)) { \
79-
__p = ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)); \
78+
ZEND_MAP_PTR_SET_IMM(ptr, val); \
79+
} else { \
80+
ZEND_MAP_PTR_INIT(ptr, (void*)val); \
8081
} \
81-
*__p = (val); \
8282
} while (0)
8383
# define ZEND_MAP_PTR_SET_IMM(ptr, val) do { \
8484
void **__p = ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)); \

Zend/zend_object_handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend
12331233
func->fn_flags |= ZEND_ACC_STATIC;
12341234
}
12351235
func->opcodes = &EG(call_trampoline_op);
1236-
ZEND_MAP_PTR_INIT(func->run_time_cache, (void***)&dummy);
1236+
ZEND_MAP_PTR_INIT(func->run_time_cache, (void**)dummy);
12371237
func->scope = fbc->common.scope;
12381238
/* reserve space for arguments, local and temporary variables */
12391239
func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, 2) : 2;

ext/opcache/jit/zend_jit_arm64.dasc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9036,23 +9036,25 @@ static int zend_jit_do_fcall(dasm_State **Dst, const zend_op *opline, const zend
90369036
if (func && !(func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
90379037
if (ZEND_MAP_PTR_IS_OFFSET(func->op_array.run_time_cache)) {
90389038
| MEM_LOAD_OP_ZTS add, ldr, REG2, compiler_globals, map_ptr_base, REG1, TMP1
9039+
| ldr REG2, [REG2]
90399040
} else if ((func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)
90409041
&& (!func->op_array.scope || (func->op_array.scope->ce_flags & ZEND_ACC_LINKED))) {
90419042
| MEM_LOAD_OP_ZTS add, ldr, REG2, compiler_globals, map_ptr_base, REG1, TMP1
9043+
| ldr REG2, [REG2]
90429044
} else {
90439045
/* the called op_array may be not persisted yet */
90449046
| TST_64_WITH_ONE REG2
90459047
| beq >1
90469048
| MEM_LOAD_OP_ZTS add, ldr, REG2, compiler_globals, map_ptr_base, REG1, TMP1
9049+
| ldr REG2, [REG2]
90479050
|1:
90489051
}
9049-
| ldr REG2, [REG2]
90509052
} else {
90519053
| TST_64_WITH_ONE REG2
90529054
| beq >1
90539055
| MEM_LOAD_OP_ZTS add, ldr, REG2, compiler_globals, map_ptr_base, REG1, TMP1
9054-
|1:
90559056
| ldr REG2, [REG2]
9057+
|1:
90569058
}
90579059
#else
90589060
# error "Unknown ZEND_MAP_PTR_KIND"

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static ZEND_COLD void zend_jit_illegal_string_offset(zval *offset)
3737
zend_type_error("Cannot access offset of type %s on string", zend_zval_type_name(offset));
3838
}
3939

40-
static zend_never_inline zend_function* ZEND_FASTCALL _zend_jit_init_func_run_time_cache(const zend_op_array *op_array) /* {{{ */
40+
static zend_never_inline zend_function* ZEND_FASTCALL _zend_jit_init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
4141
{
4242
void **run_time_cache;
4343

ext/opcache/jit/zend_jit_x86.dasc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9658,23 +9658,25 @@ static int zend_jit_do_fcall(dasm_State **Dst, const zend_op *opline, const zend
96589658
if (func && !(func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
96599659
if (ZEND_MAP_PTR_IS_OFFSET(func->op_array.run_time_cache)) {
96609660
| MEM_LOAD_OP_ZTS add, r2, aword, compiler_globals, map_ptr_base, r1
9661+
| mov r2, aword [r2]
96619662
} else if ((func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)
96629663
&& (!func->op_array.scope || (func->op_array.scope->ce_flags & ZEND_ACC_LINKED))) {
96639664
| MEM_LOAD_OP_ZTS add, r2, aword, compiler_globals, map_ptr_base, r1
9665+
| mov r2, aword [r2]
96649666
} else {
96659667
/* the called op_array may be not persisted yet */
96669668
| test r2, 1
96679669
| jz >1
96689670
| MEM_LOAD_OP_ZTS add, r2, aword, compiler_globals, map_ptr_base, r1
9671+
| mov r2, aword [r2]
96699672
|1:
96709673
}
9671-
| mov r2, aword [r2]
96729674
} else {
96739675
| test r2, 1
96749676
| jz >1
96759677
| MEM_LOAD_OP_ZTS add, r2, aword, compiler_globals, map_ptr_base, r1
9676-
|1:
96779678
| mov r2, aword [r2]
9679+
|1:
96789680
}
96799681
#else
96809682
# error "Unknown ZEND_MAP_PTR_KIND"

0 commit comments

Comments
 (0)