[php-src] PR #25787: Zend: Fix refcount info for clone()
| From: | thunderer | Date: | Thu, 26 Nov 2026 15:00:20 +0000 |
| Subject: | [php-src] PR #25787: Zend: Fix refcount info for clone() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41056@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25787
Author: thunderer
clone() with properties calls __clone() on the object it returns, and
__clone() can keep a reference to $this. The stub still declared
@refcount 1, so function JIT and tracing JIT destroyed the object when the result was
discarded, while that reference still used it.
```php
<?php
class Row
{
public static array $rows = [];
public int $id = 1;
public function __clone()
{
self::$rows[] = $this;
}
}
function duplicate(Row $row): void
{
clone($row, ['id' => 2]);
}
duplicate(new Row());
var_dump(Row::$rows);
```
With -d opcache.enable_cli=1 -d opcache.jit=function, PHP 8.5.12 prints
*RECURSION* for the element instead of the Row object, PHP 8.6.0 prints
["id"]=> UNKNOWN:0, and a debug build aborts with
zend_objects_store_del: Assertion 'zend_gc_refcount(&(object)->gc) == 0'
failed. With opcache.jit=tracing, the JIT frees the clones once
duplicate() is hot: after 1,000 calls, Row::$rows holds only 61 distinct
objects. A debug build with ZEND_VERIFY_FUNC_INFO reports clone() missing
rcn.
clone(object: $row) with a named argument is affected in the same way.
clone($row) with one positional argument is not, because it compiles to the
ZEND_CLONE opcode.
This drops the annotation and regenerates the arginfo and optimizer info, as c7265742d06 did for
pg_fetch_object(). The test fails without the change.
The merge into PHP-8.6 conflicts only in the generated header of
Zend/zend_builtin_functions_arginfo.h, which holds the stub hash line;
build/gen_stub.php regenerates it.