Segfault during class linking when a constant or property default that uses `parent` is compared with the one of a trait (8.5+)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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)
- 主要言語
- C
- スター
- 41.8k
- フォーク
- 8.6k
- 平均マージ
- 2日 15時間
- マージ済み PR(30日)
- 125
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
php/php-src のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)オープンExtension: openssl Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
php/php-src#25402 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
php/php-src#25397 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
SNMP accepts malformed remote ports対応中かも @somethingwithproof が 21 日前に担当しました。 オープン
難易度 2/5 半日 初心者へのやさしさ 62/100
php/php-src#25137 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
AcademySoftwareFoundation/openexr#2884 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
python-pillow/Pillow#10590 ·
メンテナーはふだん 1 日以内に返信
-
status: needs-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
openwrt/packages#31407 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信