Skip to content

Commit c0008b4

Browse files
elcreatorclaude
andcommitted
Fix require under the tracing JIT resuming the wrong trace (phalcon/cphalcon#17767)
zephir_require_ret() and zephir_require_once_ret() run the required file with zend_execute(), which leaves EG(jit_trace_num) at whatever trace the file's code entered last. When the require itself runs inside a tracing-JIT trace, that trace's next side exit deoptimizes with another trace's exit table: a crash in zend_jit_trace_exit() or a frame resumed at a wrong opline. zend_call_function() saves and restores the value for the same reason; the require now does too. Seen as segfaults in Phalcon (views, Config\Adapter\Php) with opcache.jit=tracing. The new test runs a Zephir require in a hot loop in a child PHP with the tracing JIT; it segfaults without the fix and is skipped where the JIT is unavailable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9766cd9 commit c0008b4

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).
66

77
## [Unreleased]
88

9+
### Fixed
10+
- Fixed `require` and `require_once` crashing the tracing JIT or making it resume the wrong code when they run inside a JIT trace: `zend_execute()` left `EG(jit_trace_num)` at another trace [phalcon/cphalcon#17767](https://github.com/phalcon/cphalcon/issues/17767)
11+
912
## [1.9.0] - 2026-11-20
1013

1114
### Added

‎kernel/require.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@
2525
#define ENFORCE_SAFE_MODE 0
2626
#endif
2727

28+
/**
29+
* Runs a required file's op_array. zend_execute() leaves EG(jit_trace_num) at
30+
* whatever trace the file's code entered last; when the require itself runs
31+
* inside a tracing-JIT trace, that trace's next side exit then deoptimizes with
32+
* another trace's exit table (a crash in zend_jit_trace_exit() or a frame resumed
33+
* at a wrong opline). zend_call_function() restores it the same way.
34+
*/
35+
static void zephir_require_execute(zend_op_array *op_array, zval *retval)
36+
{
37+
uint32_t orig_jit_trace_num = EG(jit_trace_num);
38+
39+
zend_execute(op_array, retval);
40+
EG(jit_trace_num) = orig_jit_trace_num;
41+
}
42+
2843
/**
2944
* Do an internal require to a plain php file taking care of the value returned by the file
3045
*/
@@ -75,7 +90,7 @@ int zephir_require_ret(zval *return_value_ptr, const char *require_path)
7590
* made `const zend_class_entry *` in PHP 8.5. The cast is safe: the
7691
* class entry itself is the same object — only the access path differs. */
7792
new_op_array->scope = (zend_class_entry *) (EG(fake_scope) ? EG(fake_scope) : zend_get_executed_scope());
78-
zend_execute(new_op_array, &local_retval);
93+
zephir_require_execute(new_op_array, &local_retval);
7994

8095
if (return_value_ptr) {
8196
zval_ptr_dtor(return_value_ptr);
@@ -166,7 +181,7 @@ int zephir_require_once_ret(zval *return_value_ptr, const char *require_path)
166181
* made `const zend_class_entry *` in PHP 8.5. The cast is safe: the
167182
* class entry itself is the same object — only the access path differs. */
168183
new_op_array->scope = (zend_class_entry *) (EG(fake_scope) ? EG(fake_scope) : zend_get_executed_scope());
169-
zend_execute(new_op_array, &local_retval);
184+
zephir_require_execute(new_op_array, &local_retval);
170185

171186
if (return_value_ptr) {
172187
zval_ptr_dtor(return_value_ptr);

‎tests/Extension/RequiresTest.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,39 @@ public function testRequireOnce(): void
9292
$this->test->requireOnce(__DIR__.'/../fixtures/require-me-once.php');
9393
$this->assertTrue(\defined('REQUIRE_ONCE_ME'));
9494
}
95+
96+
/**
97+
* A require from inside a tracing-JIT trace must leave EG(jit_trace_num) as
98+
* it found it, or the trace's next side exit crashes (SIGSEGV in
99+
* zend_jit_trace_exit) or resumes the wrong code.
100+
*
101+
* @issue https://github.com/phalcon/cphalcon/issues/17767
102+
*/
103+
public function testShouldRequireInsideTracingJitTrace(): void
104+
{
105+
$php = \constant('PHP_BINARY');
106+
$jit = '-d opcache.enable_cli=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M';
107+
// No double quotes: escapeshellarg() blanks them out on Windows.
108+
$probe = escapeshellarg(<<<'PHP'
109+
echo class_exists('Stub\Requires') && (opcache_get_status(false)['jit']['on'] ?? false) ? 'yes' : 'no';
110+
PHP);
111+
112+
// OPcache may be loaded by php.ini or not at all: try both ways.
113+
$opcache = null;
114+
foreach (['', '-d zend_extension=opcache'] as $load) {
115+
if ('yes' === trim((string) shell_exec("$php $load $jit -r $probe 2>&1"))) {
116+
$opcache = $load;
117+
break;
118+
}
119+
}
120+
if (null === $opcache) {
121+
$this->markTestSkipped('A child PHP with the stub extension and the tracing JIT is not available');
122+
}
123+
124+
$command = "$php $opcache $jit ".escapeshellarg(__DIR__.'/../fixtures/require-jit-loop.php').' 2000 2>&1';
125+
exec($command, $output, $status);
126+
127+
$this->assertSame(0, $status, "Exit status of: $command\n".implode("\n", $output));
128+
$this->assertSame(['ok'], $output, $command);
129+
}
95130
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Zephir.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* A Zephir `require` in a hot loop. Run with the tracing JIT: the loop becomes
14+
* a trace, and a require that loses EG(jit_trace_num) makes its next side exit
15+
* crash or resume the wrong code.
16+
*/
17+
$requires = new \Stub\Requires();
18+
$file = __DIR__ . '/require-me-1.php';
19+
$iterations = (int) ($argv[1] ?? 2000);
20+
21+
for ($i = 0; $i < $iterations; $i++) {
22+
if ($requires->requireExternal1($file) !== [1, 2, 3]) {
23+
echo "unexpected result at iteration {$i}";
24+
exit(2);
25+
}
26+
}
27+
28+
echo 'ok';

0 commit comments

Comments
 (0)