Skip to content

[BUG]: segfault with enabled JIT #17767

Open
zephir-lang/zephir
#2849

Description

@elcreator

Describe the bug
With opcache tracing JIT enabled, any Phalcon code path that does a Zephir require (view rendering with the Php or Volt engine, Phalcon\Config\Adapter\Php, ...) crashes PHP after it has run a few dozen times in one process. Under PHP-FPM, workers segfault on the heaviest pages (nginx: recv() failed (104: Connection reset by peer) while reading response header from upstream → 502). On another machine the same loop doesn't segfault but resumes the wrong function: values get corrupted, e.g. a TypeError from str_starts_with() reported at a line that never calls it. With opcache.jit=disable (opcache still on) everything is stable.

It is not a regression. 5.9.3 (release zip and the ondrej php8.4-phalcon package), 5.16.0 and 5.23.0 all fail the same way. The cause is in the Zephir kernel (see Additional context), and a one-line fix is verified.

To Reproduce

Provide output if related. Provide coredump if any.

$ php -d opcache.enable_cli=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M mini.php 200
Segmentation fault (core dumped)        # every run once N >= ~130; exit 139
$ php -d opcache.enable_cli=1 -d opcache.jit=off mini.php 200
done 201

Backtrace (5.23.0 release zip, PHP 8.4.28 + opcache dbgsym):

SIGSEGV in zend_jit_trace_exit (exit_num=4, regs=...) at ext/opcache/jit/zend_jit_trace.c:8631
#1 0x0000555551400630 in ?? ()          <- JIT code
=> mov 0x10(%rax),%ecx   rax=0xc0        (t->exit_info == NULL)
zbacktrace: (main) mini.php:3

In the variant without a segfault (a full MVC app looping $app->handle()), the error frame is the loop function, but its arguments show another function's code (jsLink(), a Volt helper) running on it:

#1 zflf_str_starts_with_2 (ext/standard/string.c:1913)
#2 ZEND_FRAMELESS_ICALL_2_SPEC_HANDLER
#3 execute_ex  #4 zend_execute  #5 zend_execute_script
zbacktrace: runLoop(object(App), "/~en", "text/javascript") req-loop-fn.php:12

Provide minimal script to reproduce the issue

<?php // cfg.php
return ['a' => 1];
<?php // mini.php
for ($i = 1; $i <= (int) ($argv[1] ?? 2000); $i++) {
    $c = new \Phalcon\Config\Adapter\Php(__DIR__ . '/cfg.php');
}
echo "done $i\n";

The same happens through Phalcon\Mvc\View\Simple with the Php engine (corrupted values) or Volt (segfault), even for a template containing just x. JIT modes tracing and 1255 fail; function and 1205 pass. Keeping the templates out of opcache (opcache.blacklist_filename) doesn't help.

Expected behavior
Tracing JIT behaves like JIT off.

// done 201, exit code 0

Screenshots
n/a

Details

  • Phalcon version: 5.23.0 (Build Date Oct 15 2026 21:43:49, "Powered by Zephir => Version 1.6.1"); also 5.22.1, 5.16.0 (Zephir 0.23.0) and 5.9.3 (Zephir 0.18.0)
  • PHP Version: PHP 8.4.28 (cli) (built: Nov 19 2026 20:03:32) (NTS), Ubuntu build (ppa:ondrej/php)
  • Operating System: Ubuntu 24.04.5 LTS, x86_64
  • Installation type: prebuilt binaries from the GitHub release assets (phalcon-php8.4-nts-ubuntu-gcc-x64.zip); same result with the ondrej package and a build from the v5.23.0 tag
  • Zephir version (if any): 1.6.1 (as reported by php --ri phalcon)
  • Server: Nginx 1.30.7 + PHP-FPM 8.4
  • Other related info: opcache: opcache.enable=1, opcache.jit=tracing, opcache.jit_buffer_size=64M

Additional context
Cause: zephir_require_ret() / zephir_require_once_ret() (ext/kernel/require.c, from Zephir's kernel/require.c) run the compiled file with zend_execute().

  1. When this happens inside a running tracing-JIT trace, the nested execute_ex enters other traces, which overwrite EG(jit_trace_num).
  2. When the outer trace takes its next side exit, zend_jit_trace_exit() uses zend_jit_traces[EG(jit_trace_num)], which is the wrong trace.
  3. If that trace's exit table is NULL, PHP segfaults (above). If it belongs to another function, the frame resumes at that function's opline.

zend_call_function() saves and restores this value (orig_jit_trace_num in Zend/zend_execute_API.c); zend_execute() does not, so it is unsafe to call from internal code under tracing JIT. Plain PHP include never hits this. It reproduces without Phalcon, with a ~50-line extension that does zend_compile_file() + zend_execute() (segfaults 3/3; with the save/restore it's clean 3/3).

Fix, around both zend_execute() calls in require.c:

uint32_t orig_jit_trace_num = EG(jit_trace_num);
zend_execute(new_op_array, &local_retval);
EG(jit_trace_num) = orig_jit_trace_num;

5.23.0 built from the v5.23.0 tag with this change passes mini.php, the View/Volt variants and the full application loop (200 iterations); the same tag built without it fails. Since ext/kernel is generated, the fix belongs in Zephir: elcreator/zephir@c0008b4db (branch fix-require-jit-trace-num, against development), with a regression test that runs a Zephir require in a hot loop under tracing JIT. The test fails on Zephir's own stub extension without the fix (child PHP segfaults, exit 139) and passes with it; the full phpunit.ext.xml suite has no new failures. cphalcon picks it up when ext/ is regenerated with a Zephir that has it.

Workaround: opcache.jit=disable (or function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstatus: unverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions