Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
52cab0a
Initial zend_class_alias
DanielEScherzer Jun 6, 2025
32afb73
Add to windows build
DanielEScherzer Jun 6, 2025
1adf20b
Add #[ClassAlias]
DanielEScherzer Jun 7, 2025
cb19a98
Reflection fixes
DanielEScherzer Jun 7, 2025
aa60367
Save reflection work
DanielEScherzer Jun 7, 2025
b3e707d
Basic working version!
DanielEScherzer Jun 7, 2025
858a9a2
goto
DanielEScherzer Jun 7, 2025
34c6009
zend_apply_internal_attribute_validation()
DanielEScherzer Jun 7, 2025
a6c5b8f
zend_attribute_populate_arguments()
DanielEScherzer Jun 7, 2025
270d2f6
Fixes from tests
DanielEScherzer Jun 7, 2025
16541e6
Fix windows file
DanielEScherzer Jun 7, 2025
b4561db
Add deprecation
DanielEScherzer Jun 7, 2025
138f8cf
Autoload with zvals
DanielEScherzer Jun 7, 2025
98c10c6
Emit deprecation warnings!
DanielEScherzer Jun 7, 2025
11a3eb6
Names in deprecation warnings
DanielEScherzer Jun 7, 2025
3912c85
Fixes
DanielEScherzer Jun 8, 2025
b713a6a
Unused variable
DanielEScherzer Jun 8, 2025
8cc3ac3
Missing free
DanielEScherzer Jun 9, 2025
a2246d5
More tests
DanielEScherzer Jun 9, 2025
3c66371
Store attributes
DanielEScherzer Jun 9, 2025
e8614ca
Try
DanielEScherzer Jun 10, 2025
7d8b221
Revert attributes/name/etc.
DanielEScherzer Jun 15, 2025
545bb6d
Z_TYPE_P
DanielEScherzer Jun 16, 2025
4d74ef1
Cache
DanielEScherzer Jun 16, 2025
0c4fd12
Rebase issues
DanielEScherzer Dec 2, 2025
1a4cbd0
Arginfo
DanielEScherzer Dec 2, 2025
8816f09
const
DanielEScherzer Dec 2, 2025
ab8bf66
accel_reset_arena_info
DanielEScherzer Dec 2, 2025
faf3cc0
xfail
DanielEScherzer Dec 2, 2025
47ccf8c
Rebase fixes
DanielEScherzer Mar 20, 2026
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) {
if (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE) {
Expand Down Expand Up @@ -818,14 +820,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 @@ -868,7 +878,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 @@ -35,6 +35,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 @@ -3296,8 +3300,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 @@ -3610,7 +3615,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 @@ -3621,6 +3628,8 @@ ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_
}
return SUCCESS;
}

free(alias);
return FAILURE;
}
/* }}} */
Expand Down
21 changes: 13 additions & 8 deletions Zend/zend_autoload.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "zend.h"
#include "zend_class_alias.h"
#include "zend_API.h"
#include "zend_autoload.h"
#include "zend_hash.h"
Expand Down Expand Up @@ -44,7 +45,7 @@ static Bucket *autoload_find_registered_function(const HashTable *autoloader_tab
return NULL;
}

ZEND_API zend_class_entry *zend_perform_class_autoload(zend_string *class_name, zend_string *lc_name)
ZEND_API zval *zend_perform_class_autoload(zend_string *class_name, zend_string *lc_name)
{
if (!zend_class_autoload_functions) {
return NULL;
Expand All @@ -69,13 +70,17 @@ ZEND_API zend_class_entry *zend_perform_class_autoload(zend_string *class_name,
if (EG(exception)) {
return NULL;
}
if (ZSTR_HAS_CE_CACHE(class_name) && ZSTR_GET_CE_CACHE(class_name)) {
return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
}

zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
if (ce) {
return ce;
// if (ZSTR_HAS_CE_CACHE(class_name) && ZSTR_GET_CE_CACHE(class_name)) {
// return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
// }

// zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
zval *ce_zv = zend_hash_find(EG(class_table), lc_name);
if (ce_zv) {
return ce_zv;
// zend_class_entry *ce;
// Z_CE_FROM_ZVAL_P(ce, ce_zv);
// return ce;
}

zend_hash_move_forward_ex(class_autoload_functions, &pos);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_autoload.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "zend_API.h"
#include "zend.h"

ZEND_API zend_class_entry *zend_perform_class_autoload(zend_string *class_name, zend_string *lc_name);
ZEND_API zval *zend_perform_class_autoload(zend_string *class_name, zend_string *lc_name);
ZEND_API void zend_autoload_register_class_loader(zend_fcall_info_cache *fcc, bool prepend);
ZEND_API bool zend_autoload_unregister_class_loader(const zend_fcall_info_cache *fcc);
ZEND_API void zend_autoload_fcc_map_to_callable_zval_map(zval *return_value);
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 @@ -1433,7 +1434,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 @@ -1885,8 +1886,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 @@ -5634,7 +5640,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 @@ -326,7 +327,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 @@ -1198,7 +1204,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 @@ -1259,7 +1265,17 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
zend_long previous_lineno = EG(lineno_override);
EG(filename_override) = NULL;
EG(lineno_override) = -1;
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);
}
}
EG(filename_override) = previous_filename;
EG(lineno_override) = previous_lineno;

Expand Down
Loading
Loading