php-src icon indicating copy to clipboard operation
php-src copied to clipboard

JIT: zend_get_known_class() reads ce->parent of an unlinked class (parent_name) -> SIGSEGV in zend_hash_find while compiling into a cold opcache (php-fpm, 8.5.4 to 8.5.14-dev)

Open mlenarciak opened this issue 3 weeks ago • 0 comments

Description

Prepared with an AI assistant (Claude). The investigation, builds and test runs were executed for real on our servers and the numbers below are the actual outputs; the wording of this report is AI-assisted.

With opcache.jit=function (we set it explicitly; the built-in default is disable) php-fpm workers die with SIGSEGV while a script is being compiled into a cold shared opcache. It reproduces on demand with our application, and it is still present in the current PHP-8.5 branch.

Backtrace (core dump, symbolised; PHP 8.5.13RC1 built from the php.net tag, -O2 -g):

#0  zend_hash_find_bucket (ht=..., key=...)                       Zend/zend_hash.c:909
#1  zend_hash_find                                                Zend/zend_hash.c:2900
#2  zend_jit_init_static_method_call (jit=..., opline=..., b=7, op_array=..., ssa=..., ssa_op=0x0, call_level=1, trace=0x0, ...)
#3  zend_jit                                                      ext/opcache/jit/zend_jit.c
#4  zend_jit_script                                               ext/opcache/jit/zend_jit.c
#5  zend_accel_script_persist (for_shm=1)                         ext/opcache/zend_persist.c
#6  cache_script_in_shared_memory / persistent_compile_file       ext/opcache/ZendAccelerator.c
#7  zend_include_or_eval                                          Zend/zend_execute.c

In the core: ht->nTableMask == 0xffffffff, ht->arData == 0xffffffff00000001.

Analysis

zend_get_known_class() in ext/opcache/jit/zend_jit.c, the parent:: branch:

ce = op_array->scope;
if (ce) {
    if (ce->parent) {
        ce = ce->parent;
        if (ce->type == ZEND_INTERNAL_CLASS || ce->info.user.filename != op_array->filename) {

zend_class_entry.parent shares storage with parent_name (zend_string *, Zend/zend.h) until the class is linked. If the class declaring the method is not linked yet when zend_jit_script() runs (its parent class lives in another file that is only loaded at run time), ce->parent is the parent's name, not a class entry.

What the cores show. These two points come from a second core, taken with a build of the PHP-8.5 head (frame 2, zend_jit_init_static_method_call):

  • op_array->scope->ce_flags has ZEND_ACC_LINKED (1 << 3) clear (0x401280).
  • the ce handed to zend_jit_init_static_method_call() is the address of a zend_string: x/6gx at that address reads as a zend_string header (refcount, type info, hash, length) followed by the characters of the parent class's name, i.e. parent_name.

In the 8.5.13RC1 core (the backtrace above) the "class" has garbage in function_table, as shown by nTableMask/arData.

What I have not established: why the check that follows (ce->type == ZEND_INTERNAL_CLASS || ce->info.user.filename != op_array->filename) does not reject that string, since on a zend_string it should read unrelated memory and almost never match op_array->filename. I can only say what is observed: the ce returned by zend_get_known_class() is the parent_name string, the crash is in the zend_hash_find() on it, and with the ZEND_ACC_LINKED test in place the crash does not occur in any of the 20 cold starts with the patch below (two builds, 10 each). There may be a second path that reaches zend_jit_init_static_method_call() with the same pointer; a maintainer will know the JIT better than I do.

Other optimizer code already tests the flag before using ->parent: Zend/Optimizer/zend_inference.c (ZEND_FETCH_CLASS_PARENT cases) and Zend/Optimizer/zend_optimizer.c (if (op_array->scope->ce_flags & ZEND_ACC_LINKED) { ce = op_array->scope->parent; }).

Suggested fix

--- a/ext/opcache/jit/zend_jit.c
+++ b/ext/opcache/jit/zend_jit.c
@@ static zend_class_entry* zend_get_known_class(const zend_op_array *op_array, ...
 			ce = op_array->scope;
 			if (ce) {
-				if (ce->parent) {
+				/* ce->parent shares storage with ce->parent_name until the parent is resolved */
+				if ((ce->ce_flags & ZEND_ACC_LINKED) && ce->parent) {
 					ce = ce->parent;

(2 insertions, 1 deletion; applies to 8.5.4 and to the head of PHP-8.5.)

How it was tested

Application: a large ZF1-style code base (about 2,000 class files, many class X extends Y with parent::method()), php-fpm, opcache.jit=function, opcache.jit_buffer_size=100M. Test = restart php-fpm (cold cache), then 500 requests from 12 parallel clients, repeated. Crash = a worker exiting on signal 11.

build crashes / requests cold starts
php.net 8.5.4 (from source) 12 / 2,460 5
php.net 8.5.12 17 / 2,952 6
php.net 8.5.13RC1 16 / 2,952 6
PHP-8.5 @ 94eb5660 (2026-11-06) 6 / 2,952 6
PHP-8.5 @ 94eb5660 + patch 0 / 4,920 10
php.net 8.5.4 + patch 0 / 4,920 10
Ubuntu php8.5 8.5.4-0ubuntu1.5, opcache.jit=disable 0 / 3,960 8

Same configure flags for all source builds. With the JIT disabled there are no crashes.

I read the 8.5.5-8.5.12 release notes and searched this tracker for zend_get_known_class and zend_jit_init_static_method_call; I did not find an issue or fix for this.

What I could not do

  • No minimal reproducer. A standalone CLI script with 40 files (class Bn extends A, A loaded at run time via require_once, parent::init() and parent::s() calls) did not crash in 120 runs per mode (function / tracing / disable) on unpatched 8.5.4. The trigger needs php-fpm, a cold shared cache and a large number of classes; that is what I can offer.
  • I did not run the ext/opcache phpt suite with the patch and have no regression test to attach.
  • I only tested opcache.jit=function.

If it helps, I can attach a core dump or test other builds; I have the build recipe and the crash harness.

PHP Version

PHP 8.5.13RC1 (cli) (built: Nov  6 2026 10:44:08) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.13RC1, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.13RC1, Copyright (c), by Zend Technologies

(also reproduced with 8.5.4, 8.5.12 and PHP-8.5 8.5.14-dev, built from source; and with Ubuntu's php8.5 8.5.4-0ubuntu1.5 packages)

Operating System

Ubuntu 26.04 LTS (resolute), x86-64

mlenarciak avatar Nov 06 '26 22:11 mlenarciak