Repository navigation
Description
Description
The following code:
<?php
class P {}
trait T { const X = parent::class; }
class C extends P { use T; const X = 'P'; }
echo "composed\n";Resulted in this output:
Segmentation fault (core dumped)
But I expected this output instead:
composed
PHP 8.2.35, 8.3.37 and 8.4.27 print composed. PHP 8.5.12 (NTS) and 8.6.0 (ZTS) from the official Docker images crash, and so does a local non-debug NTS build of PHP-8.5 (8.5.13-dev). I did not build master, but master at 2b8d3a3 has the same code in zend_do_link_class() and zend_ast_evaluate_inner().
The crash happens when the class is linked. It needs a class with a parent and a constant or property that is defined twice in the composition (in two traits, or in a trait and in the class), with parent in one of the values, so that PHP evaluates the value to compare the two. These also crash on 8.5 and 8.6 and run on 8.2 to 8.4:
// The same value in two traits
class P {}
trait A { const X = parent::class; }
trait B { const X = parent::class; }
class C extends P { use A, B; }// A constant of the parent class
class P { const Y = 1; }
trait A { const X = parent::Y; }
trait B { const X = parent::Y; }
class C extends P { use A, B; }// Property defaults (also with `public static $x = parent::Y;`)
class P {}
trait A { public $x = parent::class; }
trait B { public $x = parent::class; }
class C extends P { use A, B; }// Inside an expression
class P {}
trait T { const X = [parent::class . '!']; }
class C extends P { use T; const X = ['P!']; }// `parent` in the constant of the class, not of the trait
class P { const Y = 1; }
trait T { const X = 1; }
class C extends P { use T; const X = parent::Y; }// Different values: 8.2 to 8.4 report "C and T define the same constant (X) in the composition of C.
// However, the definition differs and is considered incompatible."
class P {}
trait T { const X = parent::class; }
class C extends P { use T; const X = 'Q'; }The crash does not depend on the order of the declarations (class P {} after class C crashes too).
Not affected:
- a trait constant with
parent::classand no second definition of the constant (class C extends P { use T; }): the value is not evaluated while the class is linked, andC::Xis"P"; - a class without a parent: all versions throw
Cannot use "parent" when current class scope has no parent.
Backtrace
8.5.13, NTS, non-debug, gcc 11.4.0, first reproducer:
Program received signal SIGSEGV, Segmentation fault.
#0 zend_ast_evaluate_inner (result=0x7fffffff9c60, ast=0x7ffff3a5e050, scope=0x7ffff3a04460, short_circuited_ptr=0x7fffffff9c5f, ctx=0x7fffffff9ca7) at Zend/zend_ast.c:684
#1 zend_ast_evaluate_ex (...) at Zend/zend_ast.c:584
#2 zval_update_constant_with_ctx (ctx=<optimized out>, scope=<optimized out>, p=0x7fffffff9f90) at Zend/zend_execute_API.c:776
#3 zval_update_constant_with_ctx (p=0x7fffffff9f90, scope=<optimized out>, ctx=<optimized out>) at Zend/zend_execute_API.c:750
#4 zval_update_constant_ex (p=p@entry=0x7fffffff9f90, scope=scope@entry=0x7ffff3a04460) at Zend/zend_execute_API.c:794
#5 check_trait_property_or_constant_value_compatibility (op2=0x7ffff3a04668, op1=0x7ffff3a04428, ce=<optimized out>) at Zend/zend_inheritance.c:2067
#6 do_trait_constant_check (current_trait=0, traits=0x7fffffff9cc0, name=0x555557538970, trait_constant=0x7ffff3a04428, ce=<optimized out>) at Zend/zend_inheritance.c:2824
#7 zend_do_traits_constant_binding (traits=<optimized out>, ce=<optimized out>) at Zend/zend_inheritance.c:2845
#8 zend_do_link_class (ce=ce@entry=0x7ffff3a04460, lc_parent_name=lc_parent_name@entry=0x555557538df0, key=<optimized out>) at Zend/zend_inheritance.c:3653
#9 zend_bind_class_in_slot (class_table_slot=<optimized out>, lcname=lcname@entry=0x7ffff3a99370, lc_parent_name=lc_parent_name@entry=0x555557538df0) at Zend/zend_compile.c:1332
#10 do_bind_class (lcname=0x7ffff3a99370, lc_parent_name=0x555557538df0) at Zend/zend_compile.c:1365
#11 ZEND_DECLARE_CLASS_SPEC_CONST_HANDLER () at Zend/zend_vm_execute.h:5915
#12 execute_ex (ex=0x7fffffff9c60) at Zend/zend_vm_execute.h:117106
In frame 0 (scope is the class C):
(gdb) p __s
$1 = (zend_string *) 0x800000000002b5f5
(gdb) p *scope->parent_name
$2 = {gc = {refcount = 1, u = {type_info = 982}}, h = 9223372036854953461, len = 1, val = "P"}
(gdb) p/x scope->parent_name->h
$3 = 0x800000000002b5f5
(gdb) p/x scope->ce_flags & (1 << 17) # ZEND_ACC_RESOLVED_PARENT
$4 = 0x0
The other forms have the same frames from zval_update_constant_ex() down, with these differences:
parent::Ycrashes inzend_hash_find_bucket(), called fromzend_get_class_constant_ex()(Zend/zend_constants.c:352) andzend_ast_evaluate_inner();- property defaults reach
check_trait_property_or_constant_value_compatibility()fromzend_do_traits_property_binding()(Zend/zend_inheritance.c:2943).
Cause
Since 011795b ("Bind traits before parent class", GH-15878, in 8.5.0), zend_do_link_class() calls zend_do_traits_constant_binding() and zend_do_traits_property_binding() before zend_do_inheritance(ce, parent). ce->parent and ce->parent_name are a union, and zend_do_inheritance_ex() is what stores the class entry and sets ZEND_ACC_RESOLVED_PARENT. While the traits are bound, ce->parent is therefore still the zend_string *parent_name.
When the class already has a constant or property with the name of the one in the trait, check_trait_property_or_constant_value_compatibility() evaluates both values with zval_update_constant_ex(&tmp, ce). Evaluating parent with that scope uses scope->parent as a class entry:
-
zend_ast_evaluate_inner(),ZEND_AST_CLASS_NAMEwithZEND_FETCH_CLASS_PARENT:ZVAL_STR_COPY(result, scope->parent->name);
nameis at the offset ofhin azend_string, so this takes the hash of"P"(0x800000000002b5f5) as azend_string *and increments its refcount. -
zend_get_class_constant_ex()forparent::Y:ce = scope->parent;
followed by a lookup in the constants table of that
ce.
Related: self::A for a constant of the parent class
The same order of binding changes the result of this code, without a crash:
<?php
class P { const A = 1; }
trait T { const C = 1; }
class B extends P { use T; const C = self::A; }
var_dump(B::C);8.2 to 8.4 print int(1). 8.5.12 and 8.6.0 print:
Warning: Uncaught Error: Undefined constant self::A in test.php:4
Stack trace:
#0 {main}
thrown in test.php on line 4
Fatal error: B and T define the same constant (C) in the composition of B. However, the definition differs and is considered incompatible. Class was composed in test.php on line 4
The class has not inherited the constants of P when the two values of C are compared. It is the same with self::A in the constant of a trait and a second trait that declares C = 1.
Notes for a fix
resolve_class_name()inZend/zend_inheritance.chandles the same state by checkingZEND_ACC_RESOLVED_PARENTand usingscope->parent_namewhen it is not set.parent_namehas the letter case of theextendsclause. Withclass C extends p,parent::classis"P"on 8.4 and on 8.5 (when the value is evaluated after linking), so usingparent_nameforparent::classwould give another string in this check.zend_do_link_class()has the parent class entry in its local variableparentbefore it binds the traits.
PHP Version
PHP 8.5.12 (cli) (NTS), official Docker image php:8.5
PHP 8.6.0 (cli) (ZTS), official Docker image php:8.6
PHP 8.5.13 (cli) (NTS), built from PHP-8.5
Operating System
Ubuntu 22.04.5 LTS, x86_64 (local build); Debian 13 (trixie), x86_64 (Docker images)
Activity