Skip to content

php_reflection.c: make a bunch of pointers const #15927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Changes from 2 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
48 changes: 24 additions & 24 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ static inline reflection_object *reflection_object_from_obj(zend_object *obj) {

static zend_object_handlers reflection_object_handlers;

static zend_always_inline uint32_t prop_get_flags(property_reference *ref) {
static zend_always_inline uint32_t prop_get_flags(const property_reference *ref) {
return ref->prop ? ref->prop->flags : ZEND_ACC_PUBLIC;
}

static inline bool is_closure_invoke(zend_class_entry *ce, zend_string *lcname) {
static inline bool is_closure_invoke(const zend_class_entry *ce, const zend_string *lcname) {
return ce == zend_ce_closure
&& zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME);
}
Expand Down Expand Up @@ -302,16 +302,16 @@ static zval *reflection_instantiate(zend_class_entry *pce, zval *object) /* {{{
}
/* }}} */

static void _const_string(smart_str *str, char *name, zval *value, char *indent);
static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent);
static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent);
static void _class_const_string(smart_str *str, zend_string *name, zend_class_constant *c, char* indent);
static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent);
static void _extension_string(smart_str *str, zend_module_entry *module, char *indent);
static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent);
static void _const_string(smart_str *str, const char *name, zval *value, const char *indent);
static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, const char* indent);
Copy link
Member

@iluuu1994 iluuu1994 Sep 17, 2024

Choose a reason for hiding this comment

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

One more question: Based on what rules did you add const? It seems a bit arbitrary to add it for zend_extension, but not zend_function, zval, etc. I don't object about these changes in general, but I'm not sure it makes sense to touch all functions just to add them, at least not unless we can show that there's some code-generation benefit from it. In this isolated context, the changes look fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added as many as I could

E.g. for _const_string, the zval *value gets passed to zval_get_tmp_string() which requires a non-const zval, since (digging deeper into the call stack) for IS_STRING values the reference count is increased, which needs non-constant inputs

For _function_string, the function pointer is passed to _function_parameter_string() and from there to _parameter_string() which needs to cast it to (zend_op_array*), so a non-constant input is needed

If this (and a few other smaller test patches I was going to send, the next being for zend_enum.c) were accepted, I would then file a task/start a discussion for doing this all over the place. E.g., if zend_string_dup() gets changed to get a constant pointer, then anything that calls (and doesn't need a non-constant pointer for anything else), and so on, bubbling up.

static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, const char* indent);
static void _class_const_string(smart_str *str, const zend_string *name, zend_class_constant *c, const char* indent);
static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const char *indent);
static void _extension_string(smart_str *str, const zend_module_entry *module, const char *indent);
static void _zend_extension_string(smart_str *str, const zend_extension *extension, const char *indent);

/* {{{ _class_string */
static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent)
static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const char *indent)
{
int count, count_static_props = 0, count_static_funcs = 0, count_shadow_props = 0;
zend_string *sub_indent = strpprintf(0, "%s ", indent);
Expand Down Expand Up @@ -543,7 +543,7 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char
/* }}} */

/* {{{ _const_string */
static void _const_string(smart_str *str, char *name, zval *value, char *indent)
static void _const_string(smart_str *str, const char *name, zval *value, const char *indent)
{
const char *type = zend_zval_type_name(value);
uint32_t flags = Z_CONSTANT_FLAGS_P(value);
Expand Down Expand Up @@ -592,7 +592,7 @@ static void _const_string(smart_str *str, char *name, zval *value, char *indent)
/* }}} */

/* {{{ _class_const_string */
static void _class_const_string(smart_str *str, zend_string *name, zend_class_constant *c, char *indent)
static void _class_const_string(smart_str *str, const zend_string *name, zend_class_constant *c, const char *indent)
{
if (Z_TYPE(c->value) == IS_CONSTANT_AST && zend_update_class_constant(c, name, c->ce) == FAILURE) {
return;
Expand Down Expand Up @@ -626,10 +626,10 @@ static void _class_const_string(smart_str *str, zend_string *name, zend_class_co
}
/* }}} */

static zend_op *get_recv_op(zend_op_array *op_array, uint32_t offset)
static zend_op *get_recv_op(const zend_op_array *op_array, uint32_t offset)
{
zend_op *op = op_array->opcodes;
zend_op *end = op + op_array->last;
const zend_op *end = op + op_array->last;

++offset;
while (op < end) {
Expand Down Expand Up @@ -772,11 +772,11 @@ static void _function_parameter_string(smart_str *str, zend_function *fptr, char
/* }}} */

/* {{{ _function_closure_string */
static void _function_closure_string(smart_str *str, zend_function *fptr, char* indent)
static void _function_closure_string(smart_str *str, const zend_function *fptr, const char* indent)
{
uint32_t i, count;
zend_string *key;
HashTable *static_variables;
const zend_string *key;
const HashTable *static_variables;

if (fptr->type != ZEND_USER_FUNCTION || !fptr->op_array.static_variables) {
return;
Expand All @@ -790,7 +790,7 @@ static void _function_closure_string(smart_str *str, zend_function *fptr, char*
}

smart_str_append_printf(str, "\n");
smart_str_append_printf(str, "%s- Bound Variables [%d] {\n", indent, zend_hash_num_elements(static_variables));
smart_str_append_printf(str, "%s- Bound Variables [%u] {\n", indent, count);
i = 0;
ZEND_HASH_MAP_FOREACH_STR_KEY(static_variables, key) {
smart_str_append_printf(str, "%s Variable #%d [ $%s ]\n", indent, i++, ZSTR_VAL(key));
Expand All @@ -800,7 +800,7 @@ static void _function_closure_string(smart_str *str, zend_function *fptr, char*
/* }}} */

/* {{{ _function_string */
static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent)
static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, const char* indent)
{
smart_str param_indent = {0};
zend_function *overwrites;
Expand Down Expand Up @@ -923,7 +923,7 @@ static zval *property_get_default(zend_property_info *prop_info) {
}

/* {{{ _property_string */
static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, const char* indent)
{
if (prop && prop->doc_comment) {
smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(prop->doc_comment));
Expand Down Expand Up @@ -986,7 +986,7 @@ static void _property_string(smart_str *str, zend_property_info *prop, const cha
}
/* }}} */

static void _extension_ini_string(zend_ini_entry *ini_entry, smart_str *str, char *indent, int number) /* {{{ */
static void _extension_ini_string(const zend_ini_entry *ini_entry, smart_str *str, const char *indent, int number) /* {{{ */
{
char *comma = "";

Expand Down Expand Up @@ -1018,7 +1018,7 @@ static void _extension_ini_string(zend_ini_entry *ini_entry, smart_str *str, cha
}
/* }}} */

static void _extension_class_string(zend_class_entry *ce, zend_string *key, smart_str *str, char *indent, zend_module_entry *module, int *num_classes) /* {{{ */
static void _extension_class_string(zend_class_entry *ce, zend_string *key, smart_str *str, char *indent, const zend_module_entry *module, int *num_classes) /* {{{ */
{
if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
/* dump class if it is not an alias */
Expand All @@ -1031,7 +1031,7 @@ static void _extension_class_string(zend_class_entry *ce, zend_string *key, smar
}
/* }}} */

static void _extension_string(smart_str *str, zend_module_entry *module, char *indent) /* {{{ */
static void _extension_string(smart_str *str, const zend_module_entry *module, const char *indent) /* {{{ */
{
smart_str_append_printf(str, "%sExtension [ ", indent);
if (module->type == MODULE_PERSISTENT) {
Expand Down Expand Up @@ -1270,7 +1270,7 @@ static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attribut
}
/* }}} */

static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent) /* {{{ */
static void _zend_extension_string(smart_str *str, const zend_extension *extension, const char *indent) /* {{{ */
{
smart_str_append_printf(str, "%sZend Extension [ %s ", indent, extension->name);

Expand Down
Loading