Skip to content

[DRAFT] [WIP] Initial zend_class_alias #18789

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

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
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
22 changes: 16 additions & 6 deletions Zend/Optimizer/zend_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "zend_call_graph.h"
#include "zend_inference.h"
#include "zend_dump.h"
#include "zend_class_alias.h"
#include "php.h"

#ifndef ZEND_OPTIMIZER_MAX_REGISTERED_PASSES
Expand Down Expand Up @@ -773,7 +774,8 @@ void zend_optimizer_shift_jump(zend_op_array *op_array, zend_op *opline, uint32_

static bool zend_optimizer_ignore_class(zval *ce_zv, zend_string *filename)
{
zend_class_entry *ce = Z_PTR_P(ce_zv);
zend_class_entry *ce;
Z_CE_FROM_ZVAL_P(ce, ce_zv);

if (ce->ce_flags & ZEND_ACC_PRELOADED) {
Bucket *ce_bucket = (Bucket*)((uintptr_t)ce_zv - XtOffsetOf(Bucket, val));
Expand Down Expand Up @@ -809,14 +811,22 @@ static bool zend_optimizer_ignore_function(zval *fbc_zv, zend_string *filename)

zend_class_entry *zend_optimizer_get_class_entry(
const zend_script *script, const zend_op_array *op_array, zend_string *lcname) {
zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
if (ce) {
return ce;
zval *ce_or_alias = script ? zend_hash_find(&script->class_table, lcname) : NULL;
if (ce_or_alias) {
if (EXPECTED(Z_TYPE_P(ce_or_alias) == IS_PTR)) {
return Z_PTR_P(ce_or_alias);
}
ZEND_ASSERT(Z_TYPE_P(ce_or_alias) == IS_ALIAS_PTR);
return Z_CLASS_ALIAS_P(ce_or_alias)->ce;
}

zval *ce_zv = zend_hash_find(CG(class_table), lcname);
if (ce_zv && !zend_optimizer_ignore_class(ce_zv, op_array ? op_array->filename : NULL)) {
return Z_PTR_P(ce_zv);
if (EXPECTED(Z_TYPE_P(ce_zv) == IS_PTR)) {
return Z_PTR_P(ce_zv);
}
ZEND_ASSERT(Z_TYPE_P(ce_zv) == IS_ALIAS_PTR);
return Z_CLASS_ALIAS_P(ce_zv)->ce;
}

if (op_array && op_array->scope && zend_string_equals_ci(op_array->scope->name, lcname)) {
Expand Down Expand Up @@ -859,7 +869,7 @@ const zend_class_constant *zend_fetch_class_const_info(
} else {
zval *ce_zv = zend_hash_find(EG(class_table), Z_STR_P(op1 + 1));
if (ce_zv && !zend_optimizer_ignore_class(ce_zv, op_array->filename)) {
ce = Z_PTR_P(ce_zv);
Z_CE_FROM_ZVAL_P(ce, ce_zv);
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "zend_enum.h"
#include "zend_object_handlers.h"
#include "zend_observer.h"
#include "zend_class_alias.h"

#include <stdarg.h>

Expand Down Expand Up @@ -2543,7 +2544,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
} ZEND_HASH_FOREACH_END();

/* Collect internal classes with static members */
ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
zval *ce_or_alias;
ZEND_HASH_MAP_FOREACH_VAL(CG(class_table), ce_or_alias) {
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
if (ce->type == ZEND_INTERNAL_CLASS &&
ce->default_static_members_count > 0) {
class_count++;
Expand All @@ -2557,7 +2560,8 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
class_cleanup_handlers[class_count] = NULL;

if (class_count) {
ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
ZEND_HASH_MAP_FOREACH_VAL(CG(class_table), ce_or_alias) {
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
if (ce->type == ZEND_INTERNAL_CLASS &&
ce->default_static_members_count > 0) {
class_cleanup_handlers[--class_count] = ce;
Expand Down Expand Up @@ -3282,8 +3286,9 @@ static void clean_module_classes(int module_number) /* {{{ */
{
/* Child classes may reuse structures from parent classes, so destroy in reverse order. */
Bucket *bucket;
zend_class_entry *ce;
ZEND_HASH_REVERSE_FOREACH_BUCKET(EG(class_table), bucket) {
zend_class_entry *ce = Z_CE(bucket->val);
Z_CE_FROM_ZVAL(ce, bucket->val);
if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
zend_hash_del_bucket(EG(class_table), bucket);
}
Expand Down Expand Up @@ -3596,7 +3601,9 @@ ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_
* Instead of having to deal with differentiating between class types and lifetimes,
* we simply don't increase the refcount of a class entry for aliases.
*/
ZVAL_ALIAS_PTR(&zv, ce);
zend_class_alias *alias = zend_class_alias_init(ce);

ZVAL_ALIAS_PTR(&zv, alias);

ret = zend_hash_add(CG(class_table), lcname, &zv);
zend_string_release_ex(lcname, 0);
Expand All @@ -3607,6 +3614,8 @@ ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_
}
return SUCCESS;
}

free(alias);
return FAILURE;
}
/* }}} */
Expand Down Expand Up @@ -3723,11 +3732,12 @@ ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_nam

key = zend_string_alloc(class_name_length, 0);
zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
disabled_class = zend_hash_find_ptr(CG(class_table), key);
zval *disabled_class_or_alias = zend_hash_find(CG(class_table), key);
zend_string_release_ex(key, 0);
if (!disabled_class) {
if (!disabled_class_or_alias) {
return FAILURE;
}
Z_CE_FROM_ZVAL_P(disabled_class, disabled_class_or_alias);

/* Will be reset by INIT_CLASS_ENTRY. */
free(disabled_class->interfaces);
Expand Down
3 changes: 2 additions & 1 deletion Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "zend.h"
#include "zend_class_alias.h"
#include "zend_API.h"
#include "zend_attributes.h"
#include "zend_gc.h"
Expand Down Expand Up @@ -1398,7 +1399,7 @@ static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int fla
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
ce = Z_PTR_P(zv);
Z_CE_FROM_ZVAL_P(ce, zv);
if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
&& key
&& ZSTR_VAL(key)[0] != 0) {
Expand Down
31 changes: 31 additions & 0 deletions Zend/zend_class_alias.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Scherzer <[email protected]> |
+----------------------------------------------------------------------+
*/

#include "zend_class_alias.h"
#include "zend.h"

zend_class_alias * zend_class_alias_init(zend_class_entry *ce) {
zend_class_alias *alias = malloc(sizeof(zend_class_alias));
// refcount field is only there for compatibility with other structures
GC_SET_REFCOUNT(alias, 1);

alias->ce = ce;
alias->alias_flags = 0;

return alias;
}
54 changes: 54 additions & 0 deletions Zend/zend_class_alias.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Scherzer <[email protected]> |
+----------------------------------------------------------------------+
*/

#ifndef ZEND_CLASS_ALIAS_H
#define ZEND_CLASS_ALIAS_H

#include "zend_types.h"

struct _zend_class_alias {
zend_refcounted_h gc;
zend_class_entry *ce;
uint32_t alias_flags;
};

typedef struct _zend_class_alias zend_class_alias;

#define Z_CE_FROM_ZVAL_P(_ce, _zv) do { \
if (EXPECTED(Z_TYPE_P(_zv) == IS_PTR)) { \
_ce = Z_PTR_P(_zv); \
} else { \
ZEND_ASSERT(Z_TYPE_P(_zv) == IS_ALIAS_PTR); \
_ce = Z_CLASS_ALIAS_P(_zv)->ce; \
} \
} while (0) \


#define Z_CE_FROM_ZVAL(_ce, _zv) do { \
if (EXPECTED(Z_TYPE(_zv) == IS_PTR)) { \
_ce = Z_PTR(_zv); \
} else { \
ZEND_ASSERT(Z_TYPE(_zv) == IS_ALIAS_PTR); \
_ce = Z_CLASS_ALIAS(_zv)->ce; \
} \
} while (0) \


zend_class_alias * zend_class_alias_init(zend_class_entry *ce);

#endif
15 changes: 12 additions & 3 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "zend_call_stack.h"
#include "zend_frameless_function.h"
#include "zend_property_hooks.h"
#include "zend_class_alias.h"

#define SET_NODE(target, src) do { \
target ## _type = (src)->op_type; \
Expand Down Expand Up @@ -1875,8 +1876,13 @@ static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend
if (class_name_refers_to_active_ce(class_name, fetch_type)) {
cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
} else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
if (ce) {
zend_string *lc_key = zend_string_tolower(class_name);
zval *ce_or_alias = zend_hash_find(CG(class_table), lc_key);
zend_string_release(lc_key);

if (ce_or_alias) {
zend_class_entry *ce;
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
cc = zend_hash_find_ptr(&ce->constants_table, name);
} else {
return 0;
Expand Down Expand Up @@ -5340,7 +5346,10 @@ static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type
zend_class_entry *ce = NULL;
if (opline->op1_type == IS_CONST) {
zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
ce = zend_hash_find_ptr(CG(class_table), lcname);
zval *ce_or_alias = zend_hash_find(CG(class_table), lcname);
if (ce_or_alias) {
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
}
if (ce) {
if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
ce = NULL;
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ZEND_API extern void (*zend_execute_ex)(zend_execute_data *execute_data);
ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);

/* The lc_name may be stack allocated! */
ZEND_API extern zend_class_entry *(*zend_autoload)(zend_string *name, zend_string *lc_name);
ZEND_API extern zval *(*zend_autoload)(zend_string *name, zend_string *lc_name);

void init_executor(void);
void shutdown_executor(void);
Expand Down
25 changes: 21 additions & 4 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <signal.h>

#include "zend.h"
#include "zend_class_alias.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_API.h"
Expand Down Expand Up @@ -51,7 +52,7 @@

ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data);
ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
ZEND_API zend_class_entry *(*zend_autoload)(zend_string *name, zend_string *lc_name);
ZEND_API zval *(*zend_autoload)(zend_string *name, zend_string *lc_name);

#ifdef ZEND_WIN32
ZEND_TLS HANDLE tq_timer = NULL;
Expand Down Expand Up @@ -327,7 +328,12 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown)
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_MAP_REVERSE_FOREACH_VAL(EG(class_table), zv) {
zend_class_entry *ce = Z_PTR_P(zv);
// CHECK
// if (Z_TYPE_P(zv) == IS_ALIAS_PTR) {
// continue;
// }
zend_class_entry *ce;
Z_CE_FROM_ZVAL_P(ce, zv);

if (ce->default_static_members_count) {
zend_cleanup_internal_class_data(ce);
Expand Down Expand Up @@ -1201,7 +1207,7 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
if (!key) {
zend_string_release_ex(lc_name, 0);
}
ce = (zend_class_entry*)Z_PTR_P(zv);
Z_CE_FROM_ZVAL_P(ce, zv);
if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_LINKED))) {
if ((flags & ZEND_FETCH_CLASS_ALLOW_UNLINKED) ||
((flags & ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED) &&
Expand Down Expand Up @@ -1268,7 +1274,18 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
EG(filename_override) = NULL;
EG(lineno_override) = -1;
zend_exception_save();
ce = zend_autoload(autoload_name, lc_name);
zval *ce_zval = zend_autoload(autoload_name, lc_name);
zend_class_alias *alias = NULL;
if (ce_zval) {
if (Z_TYPE_P(ce_zval) == IS_ALIAS_PTR) {
ce_cache = 0;
alias = Z_CLASS_ALIAS_P(ce_zval);
ce = alias->ce;
} else {
ZEND_ASSERT(Z_TYPE_P(ce_zval) == IS_PTR);
ce = Z_PTR_P(ce_zval);
}
}
zend_exception_restore();
EG(filename_override) = previous_filename;
EG(lineno_override) = previous_lineno;
Expand Down
8 changes: 6 additions & 2 deletions Zend/zend_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
+----------------------------------------------------------------------+
*/

#include "zend_class_alias.h"
#include "zend_extensions.h"
#include "zend_system_id.h"

Expand Down Expand Up @@ -327,7 +328,9 @@ ZEND_API void zend_init_internal_run_time_cache(void) {
if (rt_size) {
size_t functions = zend_hash_num_elements(CG(function_table));
zend_class_entry *ce;
ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
zval *ce_or_alias;
ZEND_HASH_MAP_FOREACH_VAL(CG(class_table), ce_or_alias) {
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
functions += zend_hash_num_elements(&ce->function_table);
} ZEND_HASH_FOREACH_END();

Expand All @@ -344,7 +347,8 @@ ZEND_API void zend_init_internal_run_time_cache(void) {
ptr += rt_size;
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
ZEND_HASH_MAP_FOREACH_VAL(CG(class_table), ce_or_alias) {
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, zif) {
if (!ZEND_USER_CODE(zif->type) && ZEND_MAP_PTR_GET(zif->run_time_cache) == NULL) {
ZEND_MAP_PTR_SET(zif->run_time_cache, (void *)ptr);
Expand Down
5 changes: 4 additions & 1 deletion Zend/zend_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "zend_observer.h"

#include "zend_class_alias.h"
#include "zend_extensions.h"
#include "zend_llist.h"
#include "zend_vm.h"
Expand Down Expand Up @@ -89,7 +90,9 @@ ZEND_API void zend_observer_post_startup(void)
++zif->T;
} ZEND_HASH_FOREACH_END();
zend_class_entry *ce;
ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
zval *ce_or_alias;
ZEND_HASH_MAP_FOREACH_VAL(CG(class_table), ce_or_alias) {
Z_CE_FROM_ZVAL_P(ce, ce_or_alias);
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, zif) {
++zif->T;
} ZEND_HASH_FOREACH_END();
Expand Down
Loading
Loading