[php-src] PHP-8.5: Fix GH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and the result is discarded
(#25787)
| From: | Tomasz Kowalczyk via GitHub | Date: | Thu, 26 Nov 2026 18:49:35 +0000 |
| Subject: | [php-src] PHP-8.5: Fix GH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and the result is discarded (#25787) |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144232@lists.php.net to get a copy of this message | ||
Author: Tomasz Kowalczyk (thunderer)
Committer: GitHub (web-flow)
Pusher: kamil-tekiela
Date: 2026-11-26T18:49:31Z
Commit: https://github.com/php/php-src/commit/b4b741140001bb667d85142c769db23279684ac5
Raw diff: https://github.com/php/php-src/commit/b4b741140001bb667d85142c769db23279684ac5.diff
Fix GH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and
the result is discarded (#25787)
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.
Changed paths:
A Zend/tests/clone/gh25788.phpt
M NEWS
M Zend/Optimizer/zend_func_infos.h
M Zend/zend_builtin_functions.stub.php
M Zend/zend_builtin_functions_arginfo.h
Diff:
diff --git a/NEWS b/NEWS
index 3b3e0159984c..5af0b74016a9 100644
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,8 @@ PHP NEWS
. Fixed bug GH-25313 (Assertion !EG(exception) in ZEND_FE_FREE when unpacking
a generator into a by-ref parameter throws). (ndossche)
. Fix OSS-Fuzz #588264417. (ndossche)
+ . Fixed bug GH-25788 (clone() with properties corrupts memory with JIT when
+ __clone() keeps a reference and the result is discarded). (Tomasz Kowalczyk)
- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h
index 76d63862cbc8..b7b8ef6952da 100644
--- a/Zend/Optimizer/zend_func_infos.h
+++ b/Zend/Optimizer/zend_func_infos.h
@@ -1,7 +1,6 @@
/* This is a generated file, edit the .stub.php files instead. */
static const func_info_t func_infos[] = {
- F1("clone", MAY_BE_OBJECT),
F1("zend_version", MAY_BE_STRING),
FN("func_get_args", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY),
F1("get_class_vars",
MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
diff --git a/Zend/tests/clone/gh25788.phpt b/Zend/tests/clone/gh25788.phpt
new file mode 100644
index 000000000000..2c388c6512e1
--- /dev/null
+++ b/Zend/tests/clone/gh25788.phpt
@@ -0,0 +1,42 @@
+--TEST--
+GH-25788: clone() with properties corrupts memory with JIT when __clone() keeps a reference and the
result is discarded
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.jit=function
+opcache.jit_buffer_size=64M
+opcache.file_update_protection=0
+--FILE--
+<?php
+
+class Row {
+ public static array $rows = [];
+
+ public int $id = 1;
+
+ public function __clone() {
+ self::$rows[] = $this;
+ }
+
+ public function __destruct() { echo "destruct\n"; }
+}
+
+function test(Row $row) {
+ clone($row, ['id' => 2]);
+}
+
+test(new Row());
+var_dump(Row::$rows);
+?>
+--EXPECTF--
+destruct
+array(1) {
+ [0]=>
+ object(Row)#%d (1) {
+ ["id"]=>
+ int(2)
+ }
+}
+destruct
diff --git a/Zend/zend_builtin_functions.stub.php b/Zend/zend_builtin_functions.stub.php
index 9b2267b531eb..59340a15d474 100644
--- a/Zend/zend_builtin_functions.stub.php
+++ b/Zend/zend_builtin_functions.stub.php
@@ -7,7 +7,6 @@ class stdClass
{
}
-/** @refcount 1 */
function clone(object $object, array $withProperties = []): object {}
function exit(string|int $status = 0): never {}
diff --git a/Zend/zend_builtin_functions_arginfo.h b/Zend/zend_builtin_functions_arginfo.h
index cf349b551ac2..8dba24e1ecd5 100644
--- a/Zend/zend_builtin_functions_arginfo.h
+++ b/Zend/zend_builtin_functions_arginfo.h
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
- * Stub hash: 9b49f527064695c812cd204d9efc63c13681d942 */
+ * Stub hash: 224d318c4732573fc63a3b49930a1d2f8a760e3d */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_clone, 0, 1, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, object, IS_OBJECT, 0)