Commit 17b28765c2e for php
commit 17b28765c2e956ed83032d04e65c431d7da3914b
Author: Florian Engelhardt <florian.engelhardt@datadoghq.com>
Date: Tue Dec 1 18:05:13 2026 +0100
Zend: Use opline_before_exception in GC tmpvar helpers (#25944)
When an exception is pending on the active frame, ex->opline points to
EG(exception_op) rather than into ex->func->op_array.opcodes. Subtracting
opcodes from ex->opline in zend_gc_check_root_tmpvars() and
zend_gc_remove_root_tmpvars() was undefined behavior (caught by ASan's
-fsanitize=pointer-subtract) and produced an invalid op_num.
Use EG(opline_before_exception) when ex->opline->opcode is
ZEND_HANDLE_EXCEPTION, matching zend_unfinished_execution_gc_ex() in
zend_execute.c.
diff --git a/NEWS b/NEWS
index 642e623f996..f58c44353ec 100644
--- a/NEWS
+++ b/NEWS
@@ -49,6 +49,8 @@ PHP NEWS
path has an absolute path exceeding MAXPATHLEN. (Jorg Sowa)
. Fixed bug GH-25304 (JIT: SEGV in exception handler stub after OOM inside
an output-buffer callback). (David Carlier)
+ . Fixed leak when exception is thrown from a destructor during GC.
+ (realFlowControl)
- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
diff --git a/Zend/tests/gc_054.phpt b/Zend/tests/gc_054.phpt
new file mode 100644
index 00000000000..2325c1320ff
--- /dev/null
+++ b/Zend/tests/gc_054.phpt
@@ -0,0 +1,39 @@
+--TEST--
+GC 054: Re-root live loop tmpvars when a GC destructor throws an exception
+--FILE--
+<?php
+
+// Cyclic garbage whose destructor throws during GC.
+$dtor = new class {
+ public $self;
+ public function __destruct() {
+ throw new Exception();
+ }
+};
+$dtor->self = $dtor;
+unset($dtor);
+
+// Self-referencing array that will leak if not re-rooted after GC.
+$arr = [];
+$arr[] = &$arr;
+
+try {
+ foreach ($arr as $v) {
+ // Only the foreach loop tmpvar keeps $arr alive now.
+ unset($arr, $v);
+
+ // Fill the 10k root buffer to trigger GC directly from this user frame.
+ $keep = [];
+ for ($i = 0; $i < 10000; $i++) {
+ $a = new stdClass();
+ $keep[] = $a;
+ }
+ }
+} catch (Exception) {
+}
+
+// $arr must have been re-rooted and collected here (0 means $arr leaked).
+var_dump(gc_collect_cycles());
+?>
+--EXPECT--
+int(1)
diff --git a/Zend/zend_gc.c b/Zend/zend_gc.c
index eb54ead7b11..3b3895298d2 100644
--- a/Zend/zend_gc.c
+++ b/Zend/zend_gc.c
@@ -2194,7 +2194,15 @@ static void zend_gc_check_root_tmpvars(void) {
continue;
}
- uint32_t op_num = ex->opline - ex->func->op_array.opcodes;
+ const zend_op *opline = ex->opline;
+ if (UNEXPECTED(opline->opcode == ZEND_HANDLE_EXCEPTION)) {
+ opline = EG(opline_before_exception);
+ if (!opline) {
+ continue;
+ }
+ }
+
+ uint32_t op_num = opline - ex->func->op_array.opcodes;
for (uint32_t i = 0; i < func->op_array.last_live_range; i++) {
const zend_live_range *range = &func->op_array.live_range[i];
if (range->start > op_num) {
@@ -2224,7 +2232,15 @@ static void zend_gc_remove_root_tmpvars(void) {
continue;
}
- uint32_t op_num = ex->opline - ex->func->op_array.opcodes;
+ const zend_op *opline = ex->opline;
+ if (UNEXPECTED(opline->opcode == ZEND_HANDLE_EXCEPTION)) {
+ opline = EG(opline_before_exception);
+ if (!opline) {
+ continue;
+ }
+ }
+
+ uint32_t op_num = opline - ex->func->op_array.opcodes;
for (uint32_t i = 0; i < func->op_array.last_live_range; i++) {
const zend_live_range *range = &func->op_array.live_range[i];
if (range->start > op_num) {