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 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Initial zend_class_alias
Probably going to have a bunch of failures but I can't find any more locally
  • Loading branch information
DanielEScherzer committed Jun 9, 2025
commit 993a5e9543b7feaff69285f541559e3dfeda26ea
20 changes: 14 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 Down Expand Up @@ -3723,11 +3730,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
29 changes: 29 additions & 0 deletions Zend/zend_class_alias.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
+----------------------------------------------------------------------+
| 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"

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;

return alias;
}
53 changes: 53 additions & 0 deletions Zend/zend_class_alias.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
+----------------------------------------------------------------------+
| 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;
};

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
6 changes: 4 additions & 2 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 @@ -327,7 +328,8 @@ 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);
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 +1203,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
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
7 changes: 6 additions & 1 deletion Zend/zend_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ typedef struct _zend_resource zend_resource;
typedef struct _zend_reference zend_reference;
typedef struct _zend_ast_ref zend_ast_ref;
typedef struct _zend_ast zend_ast;
typedef struct _zend_class_alias zend_class_alias;

typedef int (*compare_func_t)(const void *, const void *);
typedef void (*swap_func_t)(void *, void *);
Expand Down Expand Up @@ -346,6 +347,7 @@ typedef union _zend_value {
void *ptr;
zend_class_entry *ce;
zend_function *func;
zend_class_alias *class_alias;
struct {
uint32_t w1;
uint32_t w2;
Expand Down Expand Up @@ -1065,6 +1067,9 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {
#define Z_PTR(zval) (zval).value.ptr
#define Z_PTR_P(zval_p) Z_PTR(*(zval_p))

#define Z_CLASS_ALIAS(zval) (zval).value.class_alias
#define Z_CLASS_ALIAS_P(zval_p) Z_CLASS_ALIAS(*(zval_p))

#define ZVAL_UNDEF(z) do { \
Z_TYPE_INFO_P(z) = IS_UNDEF; \
} while (0)
Expand Down Expand Up @@ -1277,7 +1282,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {
} while (0)

#define ZVAL_ALIAS_PTR(z, p) do { \
Z_PTR_P(z) = (p); \
Z_CLASS_ALIAS_P(z) = (p); \
Z_TYPE_INFO_P(z) = IS_ALIAS_PTR; \
} while (0)

Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,7 @@ PHP_ADD_SOURCES([Zend], m4_normalize([
zend_attributes.c
zend_builtin_functions.c
zend_call_stack.c
zend_class_alias.c
zend_closures.c
zend_compile.c
zend_constants.c
Expand Down
Loading