Skip to content

Fix GH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and the result is discarded - #25787

Merged
kamil-tekiela merged 1 commit into
php:PHP-8.5from
thunderer:fix/clone-refcount
Nov 26, 2026
Merged

kamil-tekiela merged 1 commit into
php:PHP-8.5from
thunderer:fix/clone-refcount

Conversation

@thunderer

@thunderer thunderer commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes GH-25788.

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.

This drops the annotation and regenerates the arginfo and optimizer info, as c726574 did for pg_fetch_object(). The test fails without the change. clone($object) with one positional argument compiles to the ZEND_CLONE opcode and is not affected.

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.

@thunderer thunderer changed the title Zend: Fix refcount info for clone() Fix GH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and the result is discarded Nov 26, 2026
@kamil-tekiela

Copy link
Copy Markdown
Member

You may want to add a destructor here as well, to make the test more reliable.

…n __clone() keeps a reference and the result is discarded

clone() with properties calls __clone() on the returned object, and
__clone() can keep a reference to $this. The stub still declared
@refcount 1, which lets function JIT and tracing JIT destroy the object when
the result is discarded while that reference still uses it. Drop the
annotation.
@thunderer

Copy link
Copy Markdown
Contributor Author

@kamil-tekiela done.

@kamil-tekiela
kamil-tekiela merged commit b4b7411 into php:PHP-8.5 Nov 26, 2026
18 checks passed
@thunderer
thunderer deleted the fix/clone-refcount branch November 26, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants