Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions Zend/tests/gc_054.phpt
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 18 additions & 2 deletions Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down