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

Tracing JIT: `??` reads a stale temporary after a magic `__get`, FETCH_DIM_IS skips the NULL store (wrong value / TypeError)

Open javiermarinros opened this issue 2 weeks ago • 0 comments

Description

Paragraphs in quotes were written with an LLM (Claude), per CONTRIBUTING.md's "LLM usage in GitHub comments". The analysis was checked against the source of the PHP-8.5, PHP-8.6 and master heads listed below (the two functions it names), and against the runs below. The code, commands and outputs are not edited.

With opcache.jit=tracing, $map[$id] ?? [] sometimes gives an old value (int 1) instead of [] when the key is missing. The old value comes from a magic __get read earlier in the same loop body, which used the same temporary. Here it shows as a TypeError, because the value goes to a ?array parameter. opcache.jit=function and opcache.jit=disable give the correct result.

The following code:

<?php
class Row {
    public function __get(string $name): int { return 1; }
}

function take(?array $a): void {}

function run(array $rows, array $map, ?object $none): int {
    $bad = 0;
    foreach ($rows as $id => $row) {
        if ($none?->id == $id) {}       // T = JMP_NULL: NULL stored in the temp slot
        $x = $row->a && $row->b;        // T = FETCH_OBJ_R via __get: int 1 stored in the same slot
        try {
            take($map[$id] ?? []);      // T = FETCH_DIM_IS (key not found), then COALESCE reads T
        } catch (TypeError $e) {
            $bad++;
        }
    }
    return $bad;
}

$rows = array_fill(0, 100, new Row());
$bad = 0;
for ($i = 0; $i < 10; $i++) {
    $bad += run($rows, [], null);
}
echo "TypeErrors: $bad of 1000 (expected 0)\n";
php -n -d opcache.enable_cli=1 -d opcache.file_update_protection=0 -d opcache.jit=tracing -d opcache.jit_buffer_size=16M test.php

Resulted in this output (the TypeError is take(): Argument #1 ($a) must be of type ?array, int given):

TypeErrors: 931 of 1000 (expected 0)

But I expected this output instead (as with opcache.jit=function or opcache.jit=disable):

TypeErrors: 0 of 1000 (expected 0)

Results (branch heads: 3 runs each, same output every run; 8.5.12 and 8.5.13RC1: 1 run each):

Build Arch jit=tracing jit=function jit=disable
PHP-8.5 @ 7368796d42f177e957b32bc6854c95e1572bc1d8 (8.5.14-dev) aarch64 931 0 0
PHP-8.6 @ 176f84c4114b53b4f127fadae8dc1c44a5ae52a6 (8.6.1-dev) aarch64 931 0 0
master @ 46da0fda907d44ca1705f682a286722e8966ef29 (8.7.0-dev) aarch64 931 0 0
PHP-8.5 @ 7368796d42f177e957b32bc6854c95e1572bc1d8 (8.5.14-dev) x86-64 931 0 0
PHP-8.6 @ 176f84c4114b53b4f127fadae8dc1c44a5ae52a6 (8.6.1-dev) x86-64 931 0 0
8.5.12 (release build) x86-64 931 0 0
8.5.12 (official php:8.5.12-fpm image) aarch64 931 0 0
8.5.13RC1 x86-64 931 0 0

The branch heads were built with ./configure --disable-all --enable-opcache in a Debian bookworm container. The x86-64 runs used --platform linux/amd64 on an arm64 host (qemu user mode, one CLI process). opcache_get_status()['jit']['on'] is true with jit=tracing in every branch-head build.

Analysis (read from the source, not confirmed with a debugger on the heads):

  • $none?->id (JMP_NULL, VM handler) leaves STACK_MEM_TYPE of the temporary as IS_NULL.
  • $row->a and $row->b reuse that temporary. The result is kept in a register, but the __get slow path zend_jit_fetch_obj_r_slow_ex() still passes EX_VAR(opline->result.var) as rv to read_property(). So __get writes int 1 into the memory slot of the temporary, and STACK_MEM_TYPE is not updated.
  • FETCH_DIM_IS with a missing key: jit_set_Z_TYPE_INFO() skips the IS_NULL store ("type is already set"), because the tracked memory type is still IS_NULL.
  • COALESCE runs through the VM handler, reads the old int 1 from memory and passes it on.
  • Possible fixes (not tested): mark the memory type of the result as unknown after zend_jit_fetch_obj_r_slow_ex() (and after any other helper that uses the result slot as a buffer), or give that helper a separate rv instead of the result slot.

Found in application code: models with __get, and $a[$k] ?? [] passed to a ?array parameter.

PHP Version

PHP 8.6.1-dev (cli) (built: Nov 16 2026 09:02:56) (NTS)
Copyright © The PHP Group and Contributors
Zend Engine v4.6.1-dev, Copyright © Zend by Perforce
    with Zend OPcache v8.6.1-dev, Copyright ©, by Zend by Perforce

Also PHP-8.5 head, master head, 8.5.12 and 8.5.13RC1 (see the table above).

Operating System

Linux (Debian bookworm container), aarch64 and x86-64

javiermarinros avatar Nov 16 '26 09:11 javiermarinros