Segfault during class linking when a constant or property default that uses `parent` is compared with the one of a trait (8.5+)

オープン
#25,645 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
18/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, php
領域
compilers

調査の方向性

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::class and no second definition of the constant (class C extends P { use T; }): the value is not evaluated while the class is linked, and C::X is "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::Y crashes in zend_hash_find_bucket(), called from zend_get_class_constant_ex() (Zend/zend_constants.c:352) and zend_ast_evaluate_inner();
  • property defaults reach check_trait_property_or_constant_value_compatibility() from zend_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_NAME with ZEND_FETCH_CLASS_PARENT:

    ZVAL_STR_COPY(result, scope->parent->name);
    

    name is at the offset of h in a zend_string, so this takes the hash of "P" (0x800000000002b5f5) as a zend_string * and increments its refcount.

  • zend_get_class_constant_ex() for parent::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() in Zend/zend_inheritance.c handles the same state by checking ZEND_ACC_RESOLVED_PARENT and using scope->parent_name when it is not set.
  • parent_name has the letter case of the extends clause. With class C extends p, parent::class is "P" on 8.4 and on 8.5 (when the value is evaluated after linking), so using parent_name for parent::class would give another string in this check.
  • zend_do_link_class() has the parent class entry in its local variable parent before 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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

php/php-src のほかの issue

php/php-src の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。