Skip to content

Commit 7815ab9

Browse files
authored
Zend: add const qualifiers when possible for _zend_execute_data.func related uses (php#20263)
The initial motivation was to see if it is possible to make the `func` field of `_zend_execute_data` constant. For various reasons, this is not possible, but the added `const` qualifiers during this exploration remain useful.
1 parent 336fbf0 commit 7815ab9

File tree

11 files changed

+62
-67
lines changed

11 files changed

+62
-67
lines changed

Zend/zend.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,12 +1501,10 @@ ZEND_API ZEND_COLD void zend_error_zstr_at(
15011501

15021502
/* Report about uncaught exception in case of fatal errors */
15031503
if (EG(exception)) {
1504-
zend_execute_data *ex;
1505-
const zend_op *opline;
1506-
15071504
if (type & E_FATAL_ERRORS) {
1508-
ex = EG(current_execute_data);
1509-
opline = NULL;
1505+
zend_execute_data *ex = EG(current_execute_data);
1506+
const zend_op *opline = NULL;
1507+
15101508
while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
15111509
ex = ex->prev_execute_data;
15121510
}

Zend/zend_API.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,15 @@ ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **p
480480
/* }}} */
481481

482482
static ZEND_COLD bool zend_null_arg_deprecated(const char *fallback_type, uint32_t arg_num) {
483-
zend_function *func = zend_active_function();
483+
const zend_function *func = zend_active_function();
484484
ZEND_ASSERT(arg_num > 0);
485485
uint32_t arg_offset = arg_num - 1;
486486
if (arg_offset >= func->common.num_args) {
487487
ZEND_ASSERT(func->common.fn_flags & ZEND_ACC_VARIADIC);
488488
arg_offset = func->common.num_args;
489489
}
490490

491-
zend_arg_info *arg_info = &func->common.arg_info[arg_offset];
491+
const zend_arg_info *arg_info = &func->common.arg_info[arg_offset];
492492
zend_string *func_name = get_active_function_or_method_name();
493493
const char *arg_name = get_active_function_arg_name(arg_num);
494494

@@ -1118,7 +1118,7 @@ ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg
11181118
}
11191119

11201120
static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
1121-
zend_function *active_function = EG(current_execute_data)->func;
1121+
const zend_function *active_function = EG(current_execute_data)->func;
11221122
const char *class_name = active_function->common.scope
11231123
? ZSTR_VAL(active_function->common.scope->name) : "";
11241124
zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
@@ -3681,12 +3681,12 @@ ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
36813681
}
36823682
/* }}} */
36833683

3684-
static zend_always_inline zend_class_entry *get_scope(zend_execute_data *frame)
3684+
static zend_always_inline zend_class_entry *get_scope(const zend_execute_data *frame)
36853685
{
36863686
return frame && frame->func ? frame->func->common.scope : NULL;
36873687
}
36883688

3689-
static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error, bool suppress_deprecation) /* {{{ */
3689+
static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, const zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error, bool suppress_deprecation) /* {{{ */
36903690
{
36913691
bool ret = false;
36923692
zend_class_entry *ce;
@@ -3790,7 +3790,7 @@ ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
37903790
}
37913791
}
37923792

3793-
static zend_always_inline bool zend_is_callable_check_func(zval *callable, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error, bool suppress_deprecation) /* {{{ */
3793+
static zend_always_inline bool zend_is_callable_check_func(zval *callable, const zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error, bool suppress_deprecation) /* {{{ */
37943794
{
37953795
zend_class_entry *ce_org = fcc->calling_scope;
37963796
bool retval = false;
@@ -4036,7 +4036,7 @@ static zend_always_inline bool zend_is_callable_check_func(zval *callable, zend_
40364036
}
40374037
/* }}} */
40384038

4039-
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
4039+
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, const zend_object *object) /* {{{ */
40404040
{
40414041
try_again:
40424042
switch (Z_TYPE_P(callable)) {
@@ -4070,7 +4070,7 @@ ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *obj
40704070
}
40714071
case IS_OBJECT:
40724072
{
4073-
zend_class_entry *ce = Z_OBJCE_P(callable);
4073+
const zend_class_entry *ce = Z_OBJCE_P(callable);
40744074

40754075
if (ce == zend_ce_closure) {
40764076
const zend_function *fn = zend_get_closure_method_def(Z_OBJ_P(callable));
@@ -4101,7 +4101,7 @@ ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
41014101
/* }}} */
41024102

41034103
ZEND_API bool zend_is_callable_at_frame(
4104-
zval *callable, zend_object *object, zend_execute_data *frame,
4104+
zval *callable, zend_object *object, const zend_execute_data *frame,
41054105
uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
41064106
{
41074107
bool ret;
@@ -4214,7 +4214,7 @@ ZEND_API bool zend_is_callable_at_frame(
42144214
ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
42154215
{
42164216
/* Determine callability at the first parent user frame. */
4217-
zend_execute_data *frame = EG(current_execute_data);
4217+
const zend_execute_data *frame = EG(current_execute_data);
42184218
while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
42194219
frame = frame->prev_execute_data;
42204220
}
@@ -5274,7 +5274,7 @@ static zend_string *try_parse_string(const char *str, size_t len, char quote) {
52745274
}
52755275

52765276
ZEND_API zend_result zend_get_default_from_internal_arg_info(
5277-
zval *default_value_zval, zend_internal_arg_info *arg_info)
5277+
zval *default_value_zval, const zend_internal_arg_info *arg_info)
52785278
{
52795279
const char *default_value = arg_info->default_value;
52805280
if (!default_value) {

Zend/zend_API.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,10 @@ ZEND_API ZEND_COLD void zend_wrong_property_read(zval *object, zval *property);
410410
#define IS_CALLABLE_SUPPRESS_DEPRECATIONS (1<<1)
411411

412412
ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc);
413-
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object);
413+
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, const zend_object *object);
414414
ZEND_API zend_string *zend_get_callable_name(zval *callable);
415415
ZEND_API bool zend_is_callable_at_frame(
416-
zval *callable, zend_object *object, zend_execute_data *frame,
416+
zval *callable, zend_object *object, const zend_execute_data *frame,
417417
uint32_t check_flags, zend_fcall_info_cache *fcc, char **error);
418418
ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error);
419419
ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name);
@@ -896,7 +896,7 @@ ZEND_API zend_result zend_set_local_var_str(const char *name, size_t len, zval *
896896

897897
static zend_always_inline zend_result zend_forbid_dynamic_call(void)
898898
{
899-
zend_execute_data *ex = EG(current_execute_data);
899+
const zend_execute_data *ex = EG(current_execute_data);
900900
ZEND_ASSERT(ex != NULL && ex->func != NULL);
901901

902902
if (ZEND_CALL_INFO(ex) & ZEND_CALL_DYNAMIC) {
@@ -927,7 +927,7 @@ ZEND_API bool zend_is_iterable(const zval *iterable);
927927
ZEND_API bool zend_is_countable(const zval *countable);
928928

929929
ZEND_API zend_result zend_get_default_from_internal_arg_info(
930-
zval *default_value_zval, zend_internal_arg_info *arg_info);
930+
zval *default_value_zval, const zend_internal_arg_info *arg_info);
931931

932932
END_EXTERN_C()
933933

Zend/zend_closures.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_clas
888888
/* __call and __callStatic name the arguments "$arguments" in the docs. */
889889
static zend_internal_arg_info trampoline_arg_info[] = {ZEND_ARG_VARIADIC_TYPE_INFO(false, arguments, IS_MIXED, false)};
890890

891-
void zend_closure_from_frame(zval *return_value, zend_execute_data *call) { /* {{{ */
891+
void zend_closure_from_frame(zval *return_value, const zend_execute_data *call) { /* {{{ */
892892
zval instance;
893893
zend_internal_function trampoline;
894894
zend_function *mptr = call->func;

Zend/zend_closures.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ BEGIN_EXTERN_C()
3131
void zend_register_closure_ce(void);
3232
void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var);
3333
void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val);
34-
void zend_closure_from_frame(zval *closure_zv, zend_execute_data *frame);
34+
void zend_closure_from_frame(zval *closure_zv, const zend_execute_data *frame);
3535

3636
extern ZEND_API zend_class_entry *zend_ce_closure;
3737

Zend/zend_execute.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,7 +4346,7 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_fcall_interrupt(zend_execute_data *ca
43464346
*/
43474347
static zend_never_inline void zend_copy_extra_args(EXECUTE_DATA_D)
43484348
{
4349-
zend_op_array *op_array = &EX(func)->op_array;
4349+
const zend_op_array *op_array = &EX(func)->op_array;
43504350
uint32_t first_extra_arg = op_array->num_args;
43514351
uint32_t num_args = EX_NUM_ARGS();
43524352
zval *src;
@@ -4926,7 +4926,7 @@ static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num,
49264926
zval_ptr_dtor_nogc(var);
49274927
} else if (kind == ZEND_LIVE_ROPE) {
49284928
zend_string **rope = (zend_string **)var;
4929-
zend_op *last = EX(func)->op_array.opcodes + op_num;
4929+
const zend_op *last = EX(func)->op_array.opcodes + op_num;
49304930
while ((last->opcode != ZEND_ROPE_ADD && last->opcode != ZEND_ROPE_INIT)
49314931
|| last->result.var != var_num) {
49324932
ZEND_ASSERT(last >= EX(func)->op_array.opcodes);
@@ -4982,7 +4982,7 @@ ZEND_API HashTable *zend_unfinished_execution_gc_ex(zend_execute_data *execute_d
49824982
return NULL;
49834983
}
49844984

4985-
zend_op_array *op_array = &EX(func)->op_array;
4985+
const zend_op_array *op_array = &EX(func)->op_array;
49864986

49874987
if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
49884988
uint32_t i, num_cvs = EX(func)->op_array.last_var;
@@ -4993,7 +4993,7 @@ ZEND_API HashTable *zend_unfinished_execution_gc_ex(zend_execute_data *execute_d
49934993

49944994
if (EX_CALL_INFO() & ZEND_CALL_FREE_EXTRA_ARGS) {
49954995
zval *zv = EX_VAR_NUM(op_array->last_var + op_array->T);
4996-
zval *end = zv + (EX_NUM_ARGS() - op_array->num_args);
4996+
const zval *end = zv + (EX_NUM_ARGS() - op_array->num_args);
49974997
while (zv != end) {
49984998
zend_get_gc_buffer_add_zval(gc_buffer, zv++);
49994999
}
@@ -5462,7 +5462,7 @@ static zend_never_inline zend_result ZEND_FASTCALL zend_quick_check_constant(
54625462
} /* }}} */
54635463

54645464
static zend_always_inline uint32_t zend_get_arg_offset_by_name(
5465-
zend_function *fbc, zend_string *arg_name, void **cache_slot) {
5465+
const zend_function *fbc, const zend_string *arg_name, void **cache_slot) {
54665466
/* Due to closures, the `fbc` address isn't unique if the memory address is reused.
54675467
* The argument info will be however and uniquely positions the arguments.
54685468
* We do support NULL arg_info, so we have to distinguish that from an uninitialized cache slot. */
@@ -5477,7 +5477,7 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name(
54775477
if (EXPECTED(fbc->type == ZEND_USER_FUNCTION)
54785478
|| EXPECTED(fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
54795479
for (uint32_t i = 0; i < num_args; i++) {
5480-
zend_arg_info *arg_info = &fbc->common.arg_info[i];
5480+
const zend_arg_info *arg_info = &fbc->common.arg_info[i];
54815481
if (zend_string_equals(arg_name, arg_info->name)) {
54825482
if (fbc->type == ZEND_USER_FUNCTION && (!fbc->op_array.refcount || !(fbc->op_array.fn_flags & ZEND_ACC_CLOSURE))) {
54835483
*cache_slot = unique_id;
@@ -5489,7 +5489,7 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name(
54895489
} else {
54905490
ZEND_ASSERT(num_args == 0 || fbc->internal_function.arg_info);
54915491
for (uint32_t i = 0; i < num_args; i++) {
5492-
zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
5492+
const zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
54935493
size_t len = strlen(arg_info->name);
54945494
if (zend_string_equals_cstr(arg_name, arg_info->name, len)) {
54955495
*cache_slot = unique_id;
@@ -5517,7 +5517,7 @@ zval * ZEND_FASTCALL zend_handle_named_arg(
55175517
zend_execute_data **call_ptr, zend_string *arg_name,
55185518
uint32_t *arg_num_ptr, void **cache_slot) {
55195519
zend_execute_data *call = *call_ptr;
5520-
zend_function *fbc = call->func;
5520+
const zend_function *fbc = call->func;
55215521
uint32_t arg_offset = zend_get_arg_offset_by_name(fbc, arg_name, cache_slot);
55225522
if (UNEXPECTED(arg_offset == (uint32_t) -1)) {
55235523
zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name));
@@ -5603,7 +5603,7 @@ ZEND_API zend_result ZEND_FASTCALL zend_handle_undef_args(zend_execute_data *cal
56035603
continue;
56045604
}
56055605

5606-
zend_op *opline = &op_array->opcodes[i];
5606+
const zend_op *opline = &op_array->opcodes[i];
56075607
if (EXPECTED(opline->opcode == ZEND_RECV_INIT)) {
56085608
zval *default_value = RT_CONSTANT(opline, opline->op2);
56095609
if (Z_OPT_TYPE_P(default_value) == IS_CONSTANT_AST) {

Zend/zend_execute.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ ZEND_API const char *get_active_class_name(const char **space);
453453
ZEND_API const char *get_active_function_name(void);
454454
ZEND_API const char *get_active_function_arg_name(uint32_t arg_num);
455455
ZEND_API const char *get_function_arg_name(const zend_function *func, uint32_t arg_num);
456-
ZEND_API zend_function *zend_active_function_ex(const zend_execute_data *execute_data);
456+
ZEND_API const zend_function *zend_active_function_ex(const zend_execute_data *execute_data);
457457

458-
static zend_always_inline zend_function *zend_active_function(void)
458+
static zend_always_inline const zend_function *zend_active_function(void)
459459
{
460-
zend_function *func = EG(current_execute_data)->func;
460+
const zend_function *func = EG(current_execute_data)->func;
461461
if (ZEND_USER_CODE(func->type)) {
462462
return zend_active_function_ex(EG(current_execute_data));
463463
} else {

Zend/zend_execute_API.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,9 @@ ZEND_API const char *get_active_function_name(void) /* {{{ */
601601
}
602602
/* }}} */
603603

604-
ZEND_API zend_function *zend_active_function_ex(const zend_execute_data *execute_data)
604+
ZEND_API const zend_function *zend_active_function_ex(const zend_execute_data *execute_data)
605605
{
606-
zend_function *func = EX(func);
606+
const zend_function *func = EX(func);
607607

608608
/* Resolve function if op is a frameless call. */
609609
if (ZEND_USER_CODE(func->type)) {
@@ -717,7 +717,7 @@ ZEND_API uint32_t zend_get_executed_lineno(void) /* {{{ */
717717

718718
ZEND_API zend_class_entry *zend_get_executed_scope(void) /* {{{ */
719719
{
720-
zend_execute_data *ex = EG(current_execute_data);
720+
const zend_execute_data *ex = EG(current_execute_data);
721721

722722
while (1) {
723723
if (!ex) {

Zend/zend_object_handlers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ static ZEND_FUNCTION(zend_parent_hook_set_trampoline);
680680

681681
static bool zend_is_in_hook(const zend_property_info *prop_info)
682682
{
683-
zend_execute_data *execute_data = EG(current_execute_data);
683+
const zend_execute_data *execute_data = EG(current_execute_data);
684684
if (!execute_data || !EX(func) || !EX(func)->common.prop_info) {
685685
return false;
686686
}
@@ -996,7 +996,7 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
996996
/* }}} */
997997

998998
static zend_always_inline bool property_uses_strict_types(void) {
999-
zend_execute_data *execute_data = EG(current_execute_data);
999+
const zend_execute_data *execute_data = EG(current_execute_data);
10001000
return execute_data
10011001
&& execute_data->func
10021002
&& ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data));

0 commit comments

Comments
 (0)