Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
83456e8
Initial zend_class_alias
DanielEScherzer Jun 6, 2025
e8dd3a0
Add to windows build
DanielEScherzer Jun 6, 2025
2aaf8be
Add #[ClassAlias]
DanielEScherzer Jun 7, 2025
494cf52
Reflection fixes
DanielEScherzer Jun 7, 2025
1dcaea1
Save reflection work
DanielEScherzer Jun 7, 2025
9c77726
Basic working version!
DanielEScherzer Jun 7, 2025
2581b54
goto
DanielEScherzer Jun 7, 2025
f89fc26
zend_apply_internal_attribute_validation()
DanielEScherzer Jun 7, 2025
e58b96b
zend_attribute_populate_arguments()
DanielEScherzer Jun 7, 2025
187fe29
Fixes from tests
DanielEScherzer Jun 7, 2025
ce7b4c7
Fix windows file
DanielEScherzer Jun 7, 2025
ffea1e3
Add deprecation
DanielEScherzer Jun 7, 2025
ea1f8e2
Autoload with zvals
DanielEScherzer Jun 7, 2025
94eec4c
Emit deprecation warnings!
DanielEScherzer Jun 7, 2025
2af6f6a
Names in deprecation warnings
DanielEScherzer Jun 7, 2025
39fea5d
Fixes
DanielEScherzer Jun 8, 2025
88977d1
Unused variable
DanielEScherzer Jun 8, 2025
1ac9b21
Missing free
DanielEScherzer Jun 9, 2025
54b9ace
More tests
DanielEScherzer Jun 9, 2025
dcef3d4
Store attributes
DanielEScherzer Jun 9, 2025
fee66bf
Try
DanielEScherzer Jun 10, 2025
da6a7d6
Revert attributes/name/etc.
DanielEScherzer Jun 15, 2025
9bf677f
Z_TYPE_P
DanielEScherzer Jun 16, 2025
574df7c
Cache
DanielEScherzer Jun 16, 2025
c178ca6
Rebase issues
DanielEScherzer Dec 2, 2025
1b964b8
Arginfo
DanielEScherzer Dec 2, 2025
3f78263
const
DanielEScherzer Dec 2, 2025
210fab0
accel_reset_arena_info
DanielEScherzer Dec 2, 2025
afbd321
xfail
DanielEScherzer Dec 2, 2025
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 @@ -776,7 +777,8 @@ void zend_optimizer_shift_jump(const zend_op_array *op_array, zend_op *opline, c

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

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

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 @@ -862,7 +872,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
17 changes: 13 additions & 4 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 @@ -2537,7 +2538,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 @@ -2551,7 +2554,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 @@ -3276,8 +3280,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) {
const 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 @@ -3590,7 +3595,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 @@ -3601,6 +3608,8 @@ ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_
}
return SUCCESS;
}

free(alias);
return FAILURE;
}
/* }}} */
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 @@ -1431,7 +1432,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 |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Scherzer <daniel.e.scherzer@gmail.com> |
+----------------------------------------------------------------------+
*/

#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 |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Scherzer <daniel.e.scherzer@gmail.com> |
+----------------------------------------------------------------------+
*/

#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 @@ -39,6 +39,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 @@ -1867,8 +1868,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)) {
const 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 @@ -5449,7 +5455,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
24 changes: 20 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 @@ -1206,7 +1212,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 @@ -1273,7 +1279,17 @@ 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) {
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