Tracing JIT: a bool read from three object properties is stale right after read()/write()/unread() warm their own traces (opcache.jit=1254)
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Description
Reduced from testing the read/write/unread/eof sequence of a string-buffer class (no external code; the class below only keeps a few private fields and does the same bookkeeping, nothing phasync-specific is left).
The following code:
<?php
final class StringBuffer
{
private \SplDoublyLinkedList $queue;
private string $buffer = '';
private int $length = 0;
private int $offset = 0;
private int $totalRead = 0;
private int $totalWritten = 0;
private bool $ended = false;
public function __construct()
{
$this->queue = new \SplDoublyLinkedList();
}
public function isEmpty(): bool
{
return $this->offset === $this->length && $this->queue->isEmpty();
}
public function write(string $chunk): void
{
if ($this->ended) {
throw new \RuntimeException('Buffer has been ended');
}
$this->totalWritten += \strlen($chunk);
$this->queue->push($chunk);
}
public function read(int $maxLength): string
{
if ($maxLength < 0) {
throw new \OutOfBoundsException("Can't read negative lengths");
}
$this->fill($maxLength);
$chunk = \substr($this->buffer, $this->offset, $maxLength);
$length = \strlen($chunk);
$this->offset += $length;
$this->totalRead += $length;
return $chunk;
}
public function end(): void
{
if ($this->ended) {
throw new \LogicException('StringBuffer already ended');
}
$this->ended = true;
}
public function eof(): bool
{
return $this->ended && $this->offset === $this->length && $this->queue->isEmpty();
}
public function unread(string $chunk): void
{
if ($this->ended && $this->offset === $this->length && $this->queue->isEmpty()) {
throw new \LogicException("Can't unread to an ended and empty StringBuffer");
}
$chunkLength = \strlen($chunk);
$this->totalRead -= $chunkLength;
if ($this->length === $this->offset) {
$this->queue->unshift($chunk);
} else {
$this->buffer = $chunk . \substr($this->buffer, $this->offset);
$this->length += $chunkLength - $this->offset;
$this->offset = 0;
}
}
protected function fill(int $requiredLength): bool
{
if ($this->offset > 4096) {
$this->buffer = \substr($this->buffer, $this->offset);
$this->length -= $this->offset;
$this->offset = 0;
}
while ($this->length < $this->offset + $requiredLength && !$this->queue->isEmpty()) {
$chunk = $this->queue->shift();
$chunkLength = \strlen($chunk);
if ($this->length === $this->offset) {
$this->buffer = $chunk;
$this->length = $chunkLength;
$this->offset = 0;
} else {
$this->buffer .= $chunk;
$this->length += $chunkLength;
}
}
return $this->length >= $this->offset + $requiredLength;
}
}
$sb = new StringBuffer();
$sb->write('A');
assert($sb->read(1) === 'A');
assert($sb->isEmpty() === true);
$sb->write('B');
$sb->unread('X');
assert($sb->read(2) === 'XB');
$sb->end();
assert($sb->eof() === true);
$sb = new StringBuffer();
$count = 0;
for ($i = 0; $i < 1000; ++$i) { $sb->write('x'); }
$sb->end();
while (!$sb->eof()) { if ('' !== $sb->read(1)) { ++$count; } }
assert($count === 1000);
$sb = new StringBuffer();
$totalReceived = 0;
$chunk = str_repeat('x', 1024);
for ($i = 0; $i < 100; ++$i) { $sb->write($chunk); }
$sb->end();
while (!$sb->eof()) { $totalReceived += strlen($sb->read(8192)); }
assert($totalReceived === 102400);
$sb = new StringBuffer();
assert($sb->eof() === false);
$sb->write('Hello');
assert($sb->read(10000) === 'Hello');
assert($sb->read(100) === '');
assert($sb->write('ab') === null);
assert($sb->read(1) === 'a');
assert($sb->read(2) === 'b');
$sb->write('abc');
assert($sb->eof() === false);
$sb->write('def');
$sb->end();
$sb->unread('b');
assert($sb->read(5) === 'babcd');
var_dump($sb->eof()); // bug: prints true, must be false (2 bytes unread: "ef")
assert($sb->read(10000) === 'ef');
var_dump($sb->eof()); // bug: prints false, must be true
run with:
php -d zend.assertions=1 -d opcache.enable_cli=1 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M repro.php
Resulted in this output (same every run):
bool(true)
bool(false)
But I expected this output instead:
bool(false)
bool(true)
Details
opcache.jit=1254(the default tracing JIT),1255and1154all give the wrong output above.opcache.jit=1054(tracing without register allocation),opcache.jit=1005,opcache.jit=functionandopcache.jit=disableall give the right output.- No warning or notice is printed either way;
eof()simply returns the wrong boolean. eof()is a plain&&over three already-set object properties ($this->ended,$this->offset === $this->length,$this->queue->isEmpty()); nothing about the two failing calls themselves is unusual.- The two failing
eof()calls are each their own call site, executed once; by the time they run,read(),write(),fill()andunread()have already gone through two earlier loops (1000 and 100 iterations) that make the tracing JIT record several hot traces for those methods. Deleting either loop, or any of the four call groups above, makes the whole script print the right output, so this is specific to the exact trace shapes those loops leave behind, not to any one call in isolation. - The object itself is never shared between the loops and the final sequence (
$sb = new StringBuffer()starts a new one each time), so this isn't about one instance's property values surviving incorrectly; the suspicion is that one of the already-compiled traces forread()/write()/fill()/unread()keeps a value (destined for$this->offset,$this->lengthor$this->buffer) only in a register on some exit path, and the next, cold call into that same compiled trace (from the new object, in the final sequence) resumes with that register's value never having been stored to the property it belongs to.
PHP Version
PHP 8.5.12 (cli) (built: Oct 21 2026 08:06:50) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.5.12, Copyright (c) Zend Technologies
with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies
Operating System
Ubuntu 24.04.2 LTS (Noble Numbat), x86_64
- Ngôn ngữ chính
- C
- Star
- 40.7k
- Fork
- 8.3k
- Merge trung bình
- 2 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 159
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của php/php-src
-
ext/odbc, ext/pdo_odbc: php_odbc_connstr_is_quoted() treats a value with no closing brace as "already quoted," skipping UID/PWD escapingCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug Status: Needs Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
zend_set_local_var() overwrites existing compiled variable (CV) zval without calling zval_ptr_dtor()Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởBug Status: Needs Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Case-insensitive substr_compare, ksort, krsort are still locale-dependentCó thể đã có người làm @lazerg đã nhận 5 ngày trước. Đang mởBug Extension: standard Status: Verified
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
php/php-src#24552 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
bug intrinsic medium priority rt_lib
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[CLI] Clarify distinction between 'capture camera' and 'capture screen' in help text and outputĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
espressif/esp-vision#18 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
sumatrapdfreader/sumatrapdf#6417 · 3 bình luận ·
-
[BUG] Cython cache evicts most recent instead of oldest entry on cleanupCó thể đã có người làm @scoder đã nhận hôm nay. Đang mởBuild System defect
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 61/100
HexaCluster/credcheck#94 ·