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

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
arnaud-lb added a commit to arnaud-lb/php-src that referenced this pull request Nov 30, 2026
* PHP-8.6: (58 commits)
  Fix phpGH-24701: Use-after-free on typed trait constants during opcache preload (php#24710)
  ext/dom: Guard namespace prefix lookup for non-element nodes
  ext/pdo_sqlite: crash when a collation callback closes the statement cursor
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
  ext/pgsql: pg_meta_data() numeric column names not integer keys with opcache
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and the result is discarded (php#25787)
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix partial application treating methods named assert() as the global assert() (php#25771)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  Fix phar_set_inode() using the zend_string header instead of the filename (php#25768)
  Fix phpGH-25360: ZipArchive assertion when error handler throws at shutdown (php#25361)
  ...
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