Skip to content

Cache method overrides of ArrayAccess in zend_class_entry #7706

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ struct _zend_class_entry {

/* allocated only if class implements Iterator or IteratorAggregate interface */
zend_class_iterator_funcs *iterator_funcs_ptr;
/* allocated only if class implements ArrayAccess interface */
zend_class_arrayaccess_funcs *arrayaccess_funcs_ptr;

/* handlers */
union {
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ typedef struct _zend_fcall_info_cache {
class_container.interfaces = NULL; \
class_container.get_iterator = NULL; \
class_container.iterator_funcs_ptr = NULL; \
class_container.arrayaccess_funcs_ptr = NULL; \
class_container.info.internal.module = NULL; \
class_container.info.internal.builtin_functions = functions; \
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,7 @@ ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_hand
ce->create_object = NULL;
ce->get_iterator = NULL;
ce->iterator_funcs_ptr = NULL;
ce->arrayaccess_funcs_ptr = NULL;
ce->get_static_method = NULL;
ce->parent = NULL;
ce->parent_name = NULL;
Expand Down
23 changes: 23 additions & 0 deletions Zend/zend_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,28 @@ static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry
}
/* }}} */

/* {{{ zend_implement_arrayaccess */
static int zend_implement_arrayaccess(zend_class_entry *interface, zend_class_entry *class_type)
{
ZEND_ASSERT(!class_type->arrayaccess_funcs_ptr && "ArrayAccess funcs already set?");
zend_class_arrayaccess_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
? pemalloc(sizeof(zend_class_arrayaccess_funcs), 1)
: zend_arena_alloc(&CG(arena), sizeof(zend_class_arrayaccess_funcs));
class_type->arrayaccess_funcs_ptr = funcs_ptr;

funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(
&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(
&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(
&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(
&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);

return SUCCESS;
}
/* }}} */

/* {{{ zend_user_serialize */
ZEND_API int zend_user_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data)
{
Expand Down Expand Up @@ -616,6 +638,7 @@ ZEND_API void zend_register_interfaces(void)
zend_ce_serializable->interface_gets_implemented = zend_implement_serializable;

zend_ce_arrayaccess = register_class_ArrayAccess();
zend_ce_arrayaccess->interface_gets_implemented = zend_implement_arrayaccess;

zend_ce_countable = register_class_Countable();

Expand Down
7 changes: 7 additions & 0 deletions Zend/zend_iterators.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ typedef struct _zend_class_iterator_funcs {
zend_function *zf_rewind;
} zend_class_iterator_funcs;

typedef struct _zend_class_arrayaccess_funcs {
zend_function *zf_offsetget;
zend_function *zf_offsetexists;
zend_function *zf_offsetset;
zend_function *zf_offsetunset;
} zend_class_arrayaccess_funcs;

BEGIN_EXTERN_C()
/* given a zval, returns stuff that can be used to iterate it. */
ZEND_API zend_object_iterator* zend_iterator_unwrap(zval *array_ptr);
Expand Down
37 changes: 15 additions & 22 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,9 @@ ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int ty
zend_class_entry *ce = object->ce;
zval tmp_offset;

if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
/* arrayaccess_funcs_ptr is set if (and only if) the class implements zend_ce_arrayaccess */
zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
if (EXPECTED(funcs)) {
if (offset == NULL) {
/* [] construct */
ZVAL_NULL(&tmp_offset);
Expand All @@ -918,9 +920,7 @@ ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int ty

GC_ADDREF(object);
if (type == BP_VAR_IS) {
zend_function *offsetexists =
zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_OFFSETEXISTS));
zend_call_known_instance_method_with_1_params(offsetexists, object, rv, &tmp_offset);
zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, rv, &tmp_offset);
if (UNEXPECTED(Z_ISUNDEF_P(rv))) {
OBJ_RELEASE(object);
zval_ptr_dtor(&tmp_offset);
Expand All @@ -935,9 +935,7 @@ ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int ty
zval_ptr_dtor(rv);
}

zend_function *offsetget =
zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_OFFSETGET));
zend_call_known_instance_method_with_1_params(offsetget, object, rv, &tmp_offset);
zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, rv, &tmp_offset);

OBJ_RELEASE(object);
zval_ptr_dtor(&tmp_offset);
Expand All @@ -961,16 +959,15 @@ ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *
zend_class_entry *ce = object->ce;
zval tmp_offset;

if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
if (EXPECTED(funcs)) {
if (!offset) {
ZVAL_NULL(&tmp_offset);
} else {
ZVAL_COPY_DEREF(&tmp_offset, offset);
}
GC_ADDREF(object);
zend_function *offsetset =
zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_OFFSETSET));
zend_call_known_instance_method_with_2_params(offsetset, object, NULL, &tmp_offset, value);
zend_call_known_instance_method_with_2_params(funcs->zf_offsetset, object, NULL, &tmp_offset, value);
OBJ_RELEASE(object);
zval_ptr_dtor(&tmp_offset);
} else {
Expand All @@ -985,18 +982,15 @@ ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check
zval retval, tmp_offset;
int result;

if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
if (EXPECTED(funcs)) {
ZVAL_COPY_DEREF(&tmp_offset, offset);
GC_ADDREF(object);
zend_function *offsetexists =
zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_OFFSETEXISTS));
zend_call_known_instance_method_with_1_params(offsetexists, object, &retval, &tmp_offset);
zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, &retval, &tmp_offset);
result = i_zend_is_true(&retval);
zval_ptr_dtor(&retval);
if (check_empty && result && EXPECTED(!EG(exception))) {
zend_function *offsetget =
zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_OFFSETGET));
zend_call_known_instance_method_with_1_params(offsetget, object, &retval, &tmp_offset);
zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, &retval, &tmp_offset);
result = i_zend_is_true(&retval);
zval_ptr_dtor(&retval);
}
Expand Down Expand Up @@ -1170,12 +1164,11 @@ ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset) /* {{{
zend_class_entry *ce = object->ce;
zval tmp_offset;

if (zend_class_implements_interface(ce, zend_ce_arrayaccess)) {
zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
if (EXPECTED(funcs)) {
ZVAL_COPY_DEREF(&tmp_offset, offset);
GC_ADDREF(object);
zend_function *offsetunset =
zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_OFFSETUNSET));
zend_call_known_instance_method_with_1_params(offsetunset, object, NULL, &tmp_offset);
zend_call_known_instance_method_with_1_params(funcs->zf_offsetunset, object, NULL, &tmp_offset);
OBJ_RELEASE(object);
zval_ptr_dtor(&tmp_offset);
} else {
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ ZEND_API void destroy_zend_class(zval *zv)
if (ce->iterator_funcs_ptr) {
free(ce->iterator_funcs_ptr);
}
if (ce->arrayaccess_funcs_ptr) {
free(ce->arrayaccess_funcs_ptr);
}
if (ce->num_interfaces > 0) {
free(ce->interfaces);
}
Expand Down
4 changes: 0 additions & 4 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,6 @@ EMPTY_SWITCH_DEFAULT_CASE()
_(ZEND_STR_AUTOGLOBAL_SERVER, "_SERVER") \
_(ZEND_STR_AUTOGLOBAL_ENV, "_ENV") \
_(ZEND_STR_AUTOGLOBAL_REQUEST, "_REQUEST") \
_(ZEND_STR_OFFSETGET, "offsetget") \
_(ZEND_STR_OFFSETSET, "offsetset") \
_(ZEND_STR_OFFSETEXISTS, "offsetexists") \
_(ZEND_STR_OFFSETUNSET, "offsetunset") \
_(ZEND_STR_COUNT, "count") \


Expand Down
15 changes: 15 additions & 0 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,14 @@ static void zend_file_cache_serialize_class(zval *zv,
SERIALIZE_PTR(ce->iterator_funcs_ptr);
}

if (ce->arrayaccess_funcs_ptr) {
SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetget);
SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetexists);
SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetset);
SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetunset);
SERIALIZE_PTR(ce->arrayaccess_funcs_ptr);
}

ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
}
Expand Down Expand Up @@ -1670,6 +1678,13 @@ static void zend_file_cache_unserialize_class(zval *zv,
UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_current);
UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_next);
}
if (ce->arrayaccess_funcs_ptr) {
UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr);
UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetget);
UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetexists);
UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetset);
UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetunset);
}

if (!(script->corrupted)) {
ce->ce_flags |= ZEND_ACC_IMMUTABLE;
Expand Down
11 changes: 11 additions & 0 deletions ext/opcache/zend_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,9 @@ zend_class_entry *zend_persist_class_entry(zend_class_entry *orig_ce)
if (ce->iterator_funcs_ptr) {
ce->iterator_funcs_ptr = zend_shared_memdup(ce->iterator_funcs_ptr, sizeof(zend_class_iterator_funcs));
}
if (ce->arrayaccess_funcs_ptr) {
ce->arrayaccess_funcs_ptr = zend_shared_memdup(ce->arrayaccess_funcs_ptr, sizeof(zend_class_arrayaccess_funcs));
}

if (ce->ce_flags & ZEND_ACC_CACHED) {
return ce;
Expand Down Expand Up @@ -1135,6 +1138,14 @@ void zend_update_parent_ce(zend_class_entry *ce)
ce->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&ce->function_table, "next", sizeof("next") - 1);
}
}

if (ce->arrayaccess_funcs_ptr) {
ZEND_ASSERT(zend_class_implements_interface(ce, zend_ce_arrayaccess));
ce->arrayaccess_funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(&ce->function_table, "offsetget", sizeof("offsetget") - 1);
ce->arrayaccess_funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(&ce->function_table, "offsetexists", sizeof("offsetexists") - 1);
ce->arrayaccess_funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(&ce->function_table, "offsetset", sizeof("offsetset") - 1);
ce->arrayaccess_funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(&ce->function_table, "offsetunset", sizeof("offsetunset") - 1);
}
}

/* update methods */
Expand Down
3 changes: 3 additions & 0 deletions ext/opcache/zend_persist_calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ void zend_persist_class_entry_calc(zend_class_entry *ce)
if (ce->iterator_funcs_ptr) {
ADD_SIZE(sizeof(zend_class_iterator_funcs));
}
if (ce->arrayaccess_funcs_ptr) {
ADD_SIZE(sizeof(zend_class_arrayaccess_funcs));
}

if (ce->ce_flags & ZEND_ACC_CACHED) {
return;
Expand Down
Loading