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

Tracing JIT (8.4, 8.5, 8.6): wrong values after an inner loop; fixed on master by the IR update for dstogov/ir GH-302, not backported

Open frodeborli opened this issue 2 weeks ago • 0 comments

Description

With the tracing JIT, PHP 8.4, 8.5 and 8.6 compute wrong values in this function once its loop is compiled. $avail goes from 30 to 54 in the first iteration and stays 54, and $codes[0] is always 4, so every element must be (0x123456789ABCDEF >> 54) & 15 = 4. The two inner while loops after the computation never run (their condition is false), but the bug needs them.

<?php
declare(strict_types=1);

function f(int $bits, int $avail, array $codes, int $n): array
{
    $out = [];
    for ($i = 0; $i < $n; ++$i) {
        while ($avail <= 48) {
            $avail += 8;
        }
        $code = $codes[0];
        $val = ($bits >> $avail) & ((1 << $code) - 1);
        if ($code > 16) {
            while ($avail <= 48) {
            }
        }
        if ($i < $n - 1) {
            while ($avail <= 48) {
            }
        }
        $out[] = $val;
    }
    return $out;
}

echo implode(',', array_unique(f(0x123456789ABCDEF, 30, [4], 300))), "\n";
php -d opcache.enable_cli=1 -d opcache.jit_buffer_size=64M -d opcache.jit=tracing -d opcache.file_update_protection=0 repro.php

Resulted in this output:

4,6

But I expected this output instead:

4

It is already fixed on master (8.7.0-dev). git bisect between b30219a44 (2026-09-24, wrong) and master 9c86e0433c (correct) names the first fixed commit as 54ec8c487 "Update IR" (IR commit 281a66a87). The relevant part is dstogov/ir GH-302 ("x86-64 de-SSA temporary reuse"), fixed upstream by ea66717 "Fix GH-302: DESSA temporary reuse during resolving the remaining windmill blades" and 70ae5f1 "Fix GH-302: possible clobbering of second operand":

build tracing JIT
8.4.27, 8.5.12 (releases) wrong
PHP-8.4 head c0e07742 (8.4.29-dev), PHP-8.5 head 9afb52d0 (8.5.14-dev), PHP-8.6 head 820ec869 (8.6.1-dev) wrong
master 9c86e0433c (8.7.0-dev) correct
PHP-8.6 with master's ext/opcache/jit/ir from 54ec8c487^ wrong
PHP-8.6 with master's ext/opcache/jit/ir from 54ec8c487 correct
PHP-8.6 with master's ext/opcache/jit/ir from 54ec8c487^ plus only ea66717 and 70ae5f1 correct
PHP-8.6 with only the ir_ra.c dead-PHI change of 54ec8c487, or only the ir_sccp.c change wrong

The two GH-302 commits do not apply cleanly to the IR copies in PHP-8.4 and PHP-8.5 (older IR), so those need an adapted backport. GH-302 itself mentions no PHP impact; it was found in rv32emu.

Other details:

  • Needs register allocation and the type-inference optimization level: opcache.jit = tracing / 1254 / 1255 / 1253 / 1154 wrong; 1054 (no register allocation), 1252, 1251, function / 1205 and off are correct.
  • Only one trace is compiled (the outer for loop). The first wrong element is at iteration 66; 234 of 300 are wrong.
  • Each of these hides it: $codes[0] replaced by a plain int parameter, & ((1 << $code) - 1) replaced by & 15, or either later while loop removed.
  • Found in a real program: the bitstream reader of a pure-PHP Zstandard decoder (refill loops while ($bitsAvailable <= 48 && $cursor >= $start) { $container = ($container << 8) | ord($src[$cursor--]); $bitsAvailable += 8; }) decodes valid frames wrongly on 8.4 and 8.5 with the default tracing JIT. The function above was reduced from it.

PHP Version

PHP 8.4.27, 8.5.12 (cli, NTS, distribution packages); branch heads PHP-8.4, PHP-8.5, PHP-8.6 as of 2026-11-13

Operating System

Linux x86_64 (6.17); branch heads built in a debian:trixie container

frodeborli avatar Nov 14 '26 08:11 frodeborli