From 6e4d54ef07aec593cf7cb21f549ca09b174d994f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 27 Nov 2026 14:51:16 +0100 Subject: [PATCH 1/2] JIT: Mark counters stopped once they are stopped When the JIT runs out of root traces, zend_jit_stop_counter_handlers() restores the original VM handlers of every cached script. It set jit_counters_stopped before doing so. If the process was killed in the loop, by SIGKILL or the OOM killer, later calls returned early and the remaining counting handlers stayed until the next restart: each hot loop or function still entered the tracer once before being blacklisted, and the cold ones kept paying for the counters. Set the flag after the loop, which can safely run again. Reported by Arnaud Le Blanc in https://github.com/php/php-src/pull/25785. --- ext/opcache/jit/zend_jit_trace.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/opcache/jit/zend_jit_trace.c b/ext/opcache/jit/zend_jit_trace.c index 36a0baa8c9ff..2c5970f178e2 100644 --- a/ext/opcache/jit/zend_jit_trace.c +++ b/ext/opcache/jit/zend_jit_trace.c @@ -7672,7 +7672,6 @@ static void zend_jit_stop_counter_handlers(void) /* mprotect has an extreme overhead, avoid calls to it for every function. */ SHM_UNPROTECT(); if (!ZCSG(jit_counters_stopped)) { - ZCSG(jit_counters_stopped) = true; for (uint32_t i = 0; i < ZCSG(hash).max_num_entries; i++) { zend_accel_hash_entry *cache_entry; for (cache_entry = ZCSG(hash).hash_table[i]; cache_entry; cache_entry = cache_entry->next) { @@ -7682,6 +7681,9 @@ static void zend_jit_stop_counter_handlers(void) zend_jit_stop_persistent_script(script); } } + /* Set last, so the next call redoes a loop that a killed process left half done */ + ZEND_ATOMIC_FENCE_RELEASE(); + ZCSG(jit_counters_stopped) = true; } SHM_PROTECT(); zend_shared_alloc_unlock(); From 7684399abbf2cf7405bc09dd5fb1980d8c01713c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 27 Nov 2026 23:06:36 +0100 Subject: [PATCH 2/2] JIT: Reset func infos left by a killed compile The JIT builds the SSA, call graph and return info of the functions it compiles in their func_info in shared memory, from process-local arena memory, and clears them once done. The function JIT does so with the triggers that compile at run time (first execution, profiling or hot counters), the tracing JIT for every function a trace goes through. If the process was killed in between, by SIGKILL or the OOM killer, these func_infos kept pointers into the arena of the dead process until the server was restarted. Compiling a caller of such a function crashed, a trace entering it took its SSA as already built, and type inference read its stale return info. Record each func_info in a list in shared memory before writing to it, and reset the listed ones at the start and at the end of every compilation, in all JIT modes. With ZEND_CALL_TREE, the function JIT writes only to the func_info of the function it compiles, so its cleanup no longer walks the callees. That walk also cleared ZEND_FUNC_JITED on every callee, so a worker that waited for the lock compiled again a callee compiled meanwhile. Reported by Arnaud Le Blanc in https://github.com/php/php-src/pull/25785. --- ext/opcache/ZendAccelerator.h | 3 + ext/opcache/jit/zend_jit.c | 96 ++++++++++++++++++++++++++++---- ext/opcache/jit/zend_jit_trace.c | 24 ++------ 3 files changed, 93 insertions(+), 30 deletions(-) diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index e82a1d443986..d15b65dd7249 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -287,6 +287,9 @@ typedef struct _zend_accel_shared_globals { /* uninitialized HashTable Support */ uint32_t uninitialized_bucket[-HT_MIN_MASK]; + /* JIT */ + void *jit_dirty_func_infos; + /* Tracing JIT */ void *jit_traces; const void **jit_exit_groups; diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c index 980bf2d3eaba..15fe3d748813 100644 --- a/ext/opcache/jit/zend_jit.c +++ b/ext/opcache/jit/zend_jit.c @@ -2848,6 +2848,48 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op return FAILURE; } +/* Shared func_infos holding process-local data of the current compilation */ +typedef struct _zend_jit_func_info_list { + uint32_t count; + zend_func_info *func_info[ZEND_JIT_TRACE_MAX_FUNCS]; +} zend_jit_func_info_list; + +static zend_jit_func_info_list *zend_jit_dirty_func_infos = NULL; + +static void zend_jit_reset_func_info(zend_func_info *func_info) +{ + func_info->num = 0; + func_info->flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC + | ZEND_FUNC_JIT_ON_PROF_REQUEST + | ZEND_FUNC_JIT_ON_HOT_COUNTERS + | ZEND_FUNC_JIT_ON_HOT_TRACE + | ZEND_FUNC_JITED; + memset(&func_info->ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); +} + +static void zend_jit_record_dirty_func_info(zend_func_info *func_info) +{ + uint32_t count = zend_jit_dirty_func_infos->count; + + if (UNEXPECTED(count >= ZEND_JIT_TRACE_MAX_FUNCS)) { + abort(); + } + zend_jit_dirty_func_infos->func_info[count] = func_info; + ZEND_ATOMIC_FENCE_RELEASE(); + zend_jit_dirty_func_infos->count = count + 1; + /* Record before writing, so the next compilation can reset what a killed process left */ + ZEND_ATOMIC_FENCE_RELEASE(); +} + +static void zend_jit_reset_dirty_func_infos(void) +{ + for (uint32_t i = 0; i < zend_jit_dirty_func_infos->count; i++) { + zend_jit_reset_func_info(zend_jit_dirty_func_infos->func_info[i]); + } + ZEND_ATOMIC_FENCE_RELEASE(); + zend_jit_dirty_func_infos->count = 0; +} + static void zend_jit_collect_calls(zend_op_array *op_array, zend_script *script) { zend_func_info *func_info; @@ -2857,6 +2899,8 @@ static void zend_jit_collect_calls(zend_op_array *op_array, zend_script *script) JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) { func_info = ZEND_FUNC_INFO(op_array); } else { + /* Runs before the script is visible to other processes, so a kill leaves nothing they can read */ + ZEND_ASSERT(JIT_G(trigger) == ZEND_JIT_ON_SCRIPT_LOAD); func_info = zend_arena_calloc(&CG(arena), 1, sizeof(zend_func_info)); ZEND_SET_FUNC_INFO(op_array, func_info); } @@ -2868,22 +2912,19 @@ static void zend_jit_cleanup_func_info(zend_op_array *op_array) zend_func_info *func_info = ZEND_FUNC_INFO(op_array); zend_call_info *caller_info, *callee_info; + if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC || + JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST || + JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) { + /* With ZEND_CALL_TREE, the compilation wrote only to the recorded func_info */ + zend_jit_reset_dirty_func_infos(); + return; + } + if (func_info) { caller_info = func_info->caller_info; callee_info = func_info->callee_info; - if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC || - JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST || - JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) { - func_info->num = 0; - func_info->flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC - | ZEND_FUNC_JIT_ON_PROF_REQUEST - | ZEND_FUNC_JIT_ON_HOT_COUNTERS - | ZEND_FUNC_JIT_ON_HOT_TRACE; - memset(&func_info->ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); - } else { - ZEND_SET_FUNC_INFO(op_array, NULL); - } + ZEND_SET_FUNC_INFO(op_array, NULL); while (caller_info) { if (caller_info->caller_op_array) { @@ -2930,6 +2971,17 @@ static int zend_real_jit_func(zend_op_array *op_array, zend_script *script, cons } } + /* A process killed while compiling may have left process-local data in shared func_infos */ + zend_jit_reset_dirty_func_infos(); + if (trigger == ZEND_JIT_ON_FIRST_EXEC || + trigger == ZEND_JIT_ON_PROF_REQUEST || + trigger == ZEND_JIT_ON_HOT_COUNTERS) { + func_info = ZEND_FUNC_INFO(op_array); + if (func_info) { + zend_jit_record_dirty_func_info(func_info); + } + } + if (zend_jit_op_array_analyze1(op_array, script, &ssa) != SUCCESS) { goto jit_failure; } @@ -3276,6 +3328,9 @@ int zend_jit_script(zend_script *script) return FAILURE; } + /* The analysis reads the func_infos of preloaded callees */ + zend_jit_reset_dirty_func_infos(); + checkpoint = zend_arena_checkpoint(CG(arena)); call_graph.op_arrays_count = 0; @@ -3735,6 +3790,20 @@ void zend_jit_startup(void *buf, size_t size, bool reattached) zend_jit_init_handlers(); } + if (!reattached) { + zend_jit_dirty_func_infos = (zend_jit_func_info_list*)zend_shared_alloc(sizeof(zend_jit_func_info_list)); + if (!zend_jit_dirty_func_infos) { + zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT func info buffer!"); + } + zend_jit_dirty_func_infos->count = 0; + ZCSG(jit_dirty_func_infos) = zend_jit_dirty_func_infos; + } else { + zend_jit_dirty_func_infos = ZCSG(jit_dirty_func_infos); + if (!zend_jit_dirty_func_infos) { + zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not obtain JIT func info buffer!"); + } + } + zend_jit_trace_startup(reattached); zend_jit_save_restart_point(); @@ -3901,6 +3970,9 @@ void zend_jit_restart(void) /* restore JIT buffer pos */ dasm_ptr[0] = dasm_ptr[1]; + /* Preloaded functions survive the restart, freed ones are not reused yet */ + zend_jit_reset_dirty_func_infos(); + zend_jit_trace_restart(); if (ZCSG(preload_script)) { diff --git a/ext/opcache/jit/zend_jit_trace.c b/ext/opcache/jit/zend_jit_trace.c index 2c5970f178e2..fb4488803d45 100644 --- a/ext/opcache/jit/zend_jit_trace.c +++ b/ext/opcache/jit/zend_jit_trace.c @@ -538,12 +538,8 @@ static zend_ssa *zend_jit_trace_build_ssa(const zend_op_array *op_array, zend_sc jit_extension = (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array); - jit_extension->func_info.num = 0; - jit_extension->func_info.flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC - | ZEND_FUNC_JIT_ON_PROF_REQUEST - | ZEND_FUNC_JIT_ON_HOT_COUNTERS - | ZEND_FUNC_JIT_ON_HOT_TRACE; - memset(&jit_extension->func_info.ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); + zend_jit_record_dirty_func_info(&jit_extension->func_info); + zend_jit_reset_func_info(&jit_extension->func_info); ssa = &jit_extension->func_info.ssa; if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNC) { @@ -4132,6 +4128,9 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par JIT_G(current_trace) = trace_buffer; + /* A process killed while compiling may have left process-local data in shared func_infos */ + zend_jit_reset_dirty_func_infos(); + checkpoint = zend_arena_checkpoint(CG(arena)); zend_try { @@ -7345,18 +7344,7 @@ jit_cleanup:; } zend_end_try(); /* Clean up used op_arrays */ - while (num_op_arrays > 0) { - op_array = op_arrays[--num_op_arrays]; - jit_extension = - (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array); - - jit_extension->func_info.num = 0; - jit_extension->func_info.flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC - | ZEND_FUNC_JIT_ON_PROF_REQUEST - | ZEND_FUNC_JIT_ON_HOT_COUNTERS - | ZEND_FUNC_JIT_ON_HOT_TRACE; - memset(&jit_extension->func_info.ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); - } + zend_jit_reset_dirty_func_infos(); zend_arena_release(&CG(arena), checkpoint);