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
Prev Previous commit
Next Next commit
Try
  • Loading branch information
DanielEScherzer committed Jun 10, 2025
commit 31ecd01d89b9a7560a8778d66f596afd4e3f3adc
11 changes: 8 additions & 3 deletions Zend/Optimizer/zend_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ static bool zend_optimizer_ignore_class(zval *ce_zv, zend_string *filename)
{
zend_class_entry *ce;
Z_CE_FROM_ZVAL_P(ce, ce_zv);
if (Z_TYPE_P(ce_zv) == IS_ALIAS_PTR) {
return true;
}

if (ce->ce_flags & ZEND_ACC_PRELOADED) {
Bucket *ce_bucket = (Bucket*)((uintptr_t)ce_zv - XtOffsetOf(Bucket, val));
Expand Down Expand Up @@ -830,7 +833,8 @@ zend_class_entry *zend_optimizer_get_class_entry(
// up at runtime
return NULL;
}
return Z_CLASS_ALIAS_P(ce_or_alias)->ce;
return NULL;
// return Z_CLASS_ALIAS_P(ce_or_alias)->ce;
}

zval *ce_zv = zend_hash_find(CG(class_table), lcname);
Expand All @@ -839,7 +843,8 @@ zend_class_entry *zend_optimizer_get_class_entry(
return Z_PTR_P(ce_zv);
}
ZEND_ASSERT(Z_TYPE_P(ce_zv) == IS_ALIAS_PTR);
return Z_CLASS_ALIAS_P(ce_zv)->ce;
return NULL;
// 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 @@ -882,7 +887,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
2 changes: 2 additions & 0 deletions Zend/tests/attributes/class_alias/attributes_dict.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Alias attributes must not be associative
#[ClassAlias('Other', ['test' => new Deprecated()])]
class Demo {}

class_alias( 'Demo', 'Other2' );

$attr = new ReflectionClass( Demo::class )->getAttributes()[0];
$attr->newInstance();

Expand Down
1 change: 1 addition & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -3619,6 +3619,7 @@ ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_
}

zend_string_release(original_name);
free(alias);
return FAILURE;
}
/* }}} */
Expand Down
9 changes: 5 additions & 4 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ static void validate_class_alias(
goto restore_execution_data;
}

// if (CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION) {
// // Opcache, don't register the alias
// goto restore_execution_data;
// }

zend_result result = zend_register_class_alias_ex(
ZSTR_VAL(alias),
ZSTR_LEN(alias),
Expand Down Expand Up @@ -612,10 +617,6 @@ ZEND_API zend_result zend_get_attribute_value(zval *ret, zend_attribute *attr, u
ZVAL_COPY_OR_DUP(ret, &attr->args[i].value);

if (Z_TYPE_P(ret) == IS_CONSTANT_AST) {
// Delayed validation for attributes in class aliases
if (CG(in_compilation) && i == 1 && zend_string_equals(attr->name, zend_ce_class_alias->name)) {
return SUCCESS;
}
if (SUCCESS != zval_update_constant_ex(ret, scope)) {
zval_ptr_dtor(ret);
return FAILURE;
Expand Down
7 changes: 4 additions & 3 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown)
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_MAP_REVERSE_FOREACH_VAL(EG(class_table), zv) {
if (Z_TYPE_P(zv) == IS_ALIAS_PTR) {
continue;
}
// CHECK
// if (Z_TYPE_P(zv) == IS_ALIAS_PTR) {
// continue;
// }
zend_class_entry *ce;
Z_CE_FROM_ZVAL_P(ce, zv);

Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ ZEND_API void destroy_zend_class(zval *zv)
if (UNEXPECTED(Z_TYPE_INFO_P(zv) == IS_ALIAS_PTR)) {
zend_class_alias *class_alias = Z_CLASS_ALIAS_P(zv);

if (class_alias->alias_flags & ZEND_ACC_IMMUTABLE) {
return;
}

if (class_alias->attributes) {
zend_hash_release(class_alias->attributes);
// class_alias->attributes = NULL;
Expand Down
13 changes: 9 additions & 4 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,16 @@ static void accel_copy_permanent_strings(zend_new_interned_string_func_t new_int

/* class table hash keys, class names, properties, methods, constants, etc */
ZEND_HASH_MAP_FOREACH_BUCKET(CG(class_table), p) {
zend_class_entry *ce;
zend_class_entry *ce = NULL;

Z_CE_FROM_ZVAL(ce, p->val);
if (EXPECTED(Z_TYPE(p->val) == IS_PTR)) {
ce = Z_PTR(p->val);
} else {
ZEND_ASSERT(Z_TYPE(p->val) == IS_ALIAS_PTR);
zend_class_alias *alias = Z_PTR(p->val);
alias->name = new_interned_string(alias->name);
ce = alias->ce;
}

if (p->key) {
p->key = new_interned_string(p->key);
Expand Down Expand Up @@ -4104,8 +4111,6 @@ static void preload_link(void)
zend_hash_index_del(
CG(delayed_variance_obligations), (uintptr_t) Z_CE_P(zv));
}
zend_hash_index_del(
CG(delayed_variance_obligations), (uintptr_t) Z_CE_P(zv));
}

/* Restore the original class. */
Expand Down
1 change: 1 addition & 0 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ static void zend_file_cache_unserialize_class(zval *zv,
zend_persistent_script *script,
void *buf)
{
ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
zend_class_entry *ce;

UNSERIALIZE_PTR(Z_PTR_P(zv));
Expand Down
6 changes: 6 additions & 0 deletions ext/opcache/zend_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,12 @@ zend_class_alias *zend_persist_class_alias_entry(zend_class_alias *orig_alias)
alias->attributes = zend_persist_attributes(alias->attributes);
}

if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
alias->alias_flags |= ZEND_ACC_IMMUTABLE;
} else {
alias->alias_flags |= ZEND_ACC_FILE_CACHED;
}

return alias;
}

Expand Down
Loading