Segfault during class linking when a constant or property default that uses `parent` is compared with the one of a trait (8.5+)
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 18/100
Rechercherichtung
Start by reproducing the first snippet (trait constant parent::class plus a same-named class constant) on a debug build of PHP 8.5. The crash path runs through zend_do_link_class() and check_trait_property_or_constant_value_compatibility() in Zend/zend_inheritance.c, which calls zval_update_constant_ex(), reaching zend_ast_evaluate_inner() in Zend/zend_ast.c and zend_get_class_constant_ex() in Zend/zend_constants.c. Done means the reproducers run without a crash and match 8.4 output, with parent resolved safely before the parent entry is linked.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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 2b8d3a3a404 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 011795bcbebc ("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)
- Vorherrschende Sprache
- C
- Sterne
- 41.8k
- Forks
- 8.6k
- Ø Merge
- 2 T. 15 Std.
- Gemergte PRs (30 T.)
- 125
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus php/php-src
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)OffenExtension: openssl Status: Needs Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
php/php-src#25402 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug Status: Needs Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
php/php-src#25397 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
SNMP accepts malformed remote portsEvtl. vergeben @somethingwithproof hat das vor 21 Tagen übernommen. Offen
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 62/100
php/php-src#25137 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug Status: Needs Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)Evtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
AcademySoftwareFoundation/openexr#2884 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
python-pillow/Pillow#10590 ·
Maintainer antworten meist innerhalb von 1 Tag
-
status: needs-triage type: bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
openwrt/packages#31407 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag