Tracing JIT: a catch reads an int local as undefined after an internal call throws (8.4 regression)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- backend, performance
調査の方向性
Start with the OPcache tracing JIT and register allocation paths implicated by the issue, then read the related issue #24186 for context. Reproduce using the supplied PHP script and tracing JIT command; compare the exception path with side exits, checking whether the local is written back before the catch runs. Done means the catch reads the assigned value correctly under tracing JIT while the supplied comparison modes remain correct.
索引モデルが issue の本文から書いたものです。
説明
Description
Description
The following code:
<?php
function loop(array $p): void { foreach ($p as $v) {} }
function id(int $n): int { return $n; }
function run(array $p, int $d): int {
loop($p);
$n = count($p);
try {
intdiv(1, $d);
} catch (DivisionByZeroError $e) {
var_dump($n);
return -1;
}
return id($n);
}
for ($i = 0; $i < 200; $i++) {
run([1], 1);
}
run([1], 0);
run with OPcache as the only extension:
php -n -d zend_extension=opcache -d opcache.enable_cli=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M -d opcache.file_update_protection=0 test.php
Resulted in this output:
Warning: Undefined variable $n in /up/test.php on line 10
NULL
But I expected this output instead:
int(1)
$n is assigned before the try and nothing touches it afterwards, yet the catch block sees it as undefined once the tracing JIT has compiled run()
Some more details:
opcache.jit=1254,1255and1154give the same wrong output.1054(tracing without register allocation),functionanddisableall printint(1)- PHP 8.3.35 and 8.2.33 print
int(1)with the same command intracing,1254,1255,functionanddisablemode. We checked withopcache_get_status()that the JIT was on and had compiled code, so this looks like a regression in 8.4 - With the default hot counters it takes 135 warm-up calls to trigger it; with 130 the script still prints
int(1). With-d opcache.jit_hot_loop=1 -d opcache.jit_hot_func=1, 10 calls are enough opcache.file_update_protection=0is only there because OPcache does not cache a script written less than 2 seconds earlier, which would hide the bug when the file has just been created
If $n is an int parameter that the function assigns again, the catch block does not see it as undefined but reads the parameter's old value, without any warning:
<?php
function loop(array $p): void { foreach ($p as $v) {} }
function id(int $n): int { return $n; }
function run(array $p, int $d, int $n = 5): int {
loop($p);
$n = count($p);
try {
intdiv(1, $d);
} catch (DivisionByZeroError $e) {
var_dump($n);
return -1;
}
return id($n);
}
for ($i = 0; $i < 200; $i++) {
run([1], 1);
}
run([1], 0);
This prints int(5) with the tracing JIT on 8.4.18, and int(1) with the function JIT and on 8.3.35 and 8.2.33
What changes the result
Starting from the first script, we changed one thing at a time and ran it with the tracing JIT on 8.4.18. Where $n is not an int, id() and run() were changed to take and return mixed
- Removing the
loop($p)call, or theforeachinsideloop(), makes it printint(1) - Replacing
return id($n);withreturn 0;, so that only the catch block reads$n, also makes it printint(1) - A float (
$n = count($p) + 0.5) and an int returned by an internal function ($n = hrtime(true)) are lost in the same way. A bool, a string or an array (count($p) > 0,str_repeat('a', count($p)),[count($p)]) is kept - An internal method behaves like
intdiv():$it->seek($d)on anArrayIterator, with the last call passing 9 so that it throwsOutOfBoundsException, loses$nas well. With a condition on$din the argument,$it->seek($d === 0 ? 9 : 0),$nis kept - If the exception comes from a user function instead, either one that throws
DivisionByZeroErroritself or one that callsintdiv(),$nis kept
What the JIT does
With opcache.jit_debug=0x1C0000 on 8.4.18, the call that throws runs in a trace that starts where loop() returns:
---- TRACE 5 TSSA start (return) run() /up/test.php:6
0005 #5.T4 [long] RANGE[MIN..MAX] = COUNT #0.CV0($p) [!rc1, rcn, array of [any, ref]] ; op1(packed array)
0006 ASSIGN #2.CV2($n) [!undef] -> #6.CV2($n) [long] RANGE[MIN..MAX] #5.T4 [long] RANGE[MIN..MAX] ; op1(undef) op2(int)
0007 INIT_FCALL 2 112 string("intdiv")
0008 SEND_VAL int(1) 1
0009 SEND_VAR #1.CV1($d) [!long] -> #7.CV1($d) [long] 2 ; op1(int)
0010 DO_ICALL
---- TRACE 5 exit info
exit_2: 0007/0005/5/VM CV0($p):array CV2($n):int(rbx) X4:int
exit_3: 0009/0010/5/CALL CV0($p):array CV2($n):int(rbx) X4:int
exit_4: 0011/0015/5/VM CV0($p):array CV1($d):int CV2($n):int(rbx) X4:int
exit_5: 0017/0020/5/VM CV0($p):array CV1($d):int CV2($n):int(rbx) X4:int
$n is held only in rbx, and the exit info shows that every side exit after the assignment knows this. When intdiv() throws inside the trace, though, the catch block (run by the VM) finds the slot of $n still undefined. It looks as if the exception path out of the trace does not write rbx back to the slot of $n, the way a side exit would. In the parameter variant the slot still holds the earlier value, the default 5, which is why the catch block reads a stale value there instead of an undefined one
This may be related to #24186, where a value held only in a register is lost when a tracing JIT loop exits to handle an interrupt
How we found it
We hit this in a long-running Swoole server that uses opcache.preload with the tracing JIT. A catch block passed such an int to a function with an int parameter, so a TypeError was thrown instead of the original PDOException. Our application then did not see that the database connection had died, kept reusing it, and every request on it failed until the connection was recycled. In our application the bug only showed with preload, while the scripts above do not need it. We worked around it by computing the value again inside the catch block
PHP Version
PHP 8.4.18 (cli) (built: Feb 13 2026 18:37:49) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.18, Copyright (c) Zend Technologies
with Zend OPcache v8.4.18, Copyright (c), by Zend Technologies
Also reproduced on 8.4.26, the latest 8.4 release (NTS, x86_64). Not reproduced on 8.3.35 or 8.2.33 (the docker-library php:8.3-cli and php:8.2-cli images)
PHP Version
PHP 8.4.18 (cli) (built: Feb 13 2026 18:37:49) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.18, Copyright (c) Zend Technologies
with Zend OPcache v8.4.18, Copyright (c), by Zend Technologies
Operating System
Alpine Linux v3.23 on x86_64 for 8.4.18, and Debian GNU/Linux 13 (trixie) on x86_64 for 8.3.35 and 8.2.33, all in Docker
- 主要言語
- C
- スター
- 40.7k
- フォーク
- 8.3k
- 平均マージ
- 2日 17時間
- マージ済み PR(30日)
- 159
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
php/php-src のほかの issue
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
ext/mbstring: mb_regex_substitute() reads past the replacement string's end on a truncated trailing multibyte lead byte対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
php/php-src#24769 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
zend_set_local_var() overwrites existing compiled variable (CV) zval without calling zval_ptr_dtor()対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンBug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
master: part of the mbstring bailout fix got lost in a merge対応中かも @lazerg が 6 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
Abort (SIGABRT) when stderr is a closed pipe: uncaught fmt::system_error in ccache_main's catch対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 3 日以内に返信
-
common/amount: parse_amount_sat misparses sub-satoshi msat with leading zero and accepts invalid suffixes対応中かも @bhuvan-somisetty が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ElementsProject/lightning#9674 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
colbymchenry/codegraph#2982 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信