Skip to content

Commit b9dc33a

Browse files
committed
Fix GH-23586: Keep GC traversal consistent under memory exhaustion
1 parent 1256242 commit b9dc33a

5 files changed

Lines changed: 131 additions & 14 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ PHP NEWS
33
?? ??? ????, PHP 8.4.28
44

55
- Core:
6+
. Fixed bug GH-23586 (Assertion failure during garbage collection upon memory
7+
exhaustion). (Alessio Attilio)
68
. Fixed bug GH-24184 (System ID not being reinitialized after an Apache
79
graceful restart, potentially causing incompatible OPcache file cache
810
entries to be reused). (Levi Morrison)

‎Zend/tests/gh23586.phpt‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
GH-23586 (GC traversal stack allocation must not exhaust the memory limit)
3+
--SKIPIF--
4+
<?php
5+
if (getenv('USE_ZEND_ALLOC') === '0') {
6+
die('skip Zend MM required');
7+
}
8+
?>
9+
--INI--
10+
memory_limit=93M
11+
--FILE--
12+
<?php
13+
class Node {
14+
public $children = [];
15+
16+
public function __construct(?Node $parent = null) {
17+
if ($parent) {
18+
$parent->children[] = $this;
19+
}
20+
$this->children[] = $this;
21+
}
22+
23+
public function __destruct() {
24+
$this->children = null;
25+
}
26+
}
27+
28+
for ($n = 0; $n < 2; $n++) {
29+
$top = new Node();
30+
for ($i = 0; $i < 16; $i++) {
31+
$ci = new Node($top);
32+
for ($j = 0; $j < 16; $j++) {
33+
$cj = new Node($ci);
34+
while ($n) {
35+
$ck = new Node($cj);
36+
}
37+
}
38+
}
39+
}
40+
?>
41+
--EXPECTF--
42+
Fatal error: Allowed memory size of %d bytes exhausted%S (tried to allocate %d bytes) in %s on line %d

‎Zend/tests/gh23586_002.phpt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-23586 (GC can finish a wide traversal at the memory limit)
3+
--SKIPIF--
4+
<?php
5+
if (getenv('USE_ZEND_ALLOC') === '0') {
6+
die('skip Zend MM required');
7+
}
8+
?>
9+
--INI--
10+
memory_limit=256M
11+
--FILE--
12+
<?php
13+
gc_disable();
14+
$root = new stdClass();
15+
for ($i = 0; $i < 300000; $i++) {
16+
$root->children[] = (object) ['parent' => $root];
17+
}
18+
unset($root);
19+
ini_set('memory_limit', (string) memory_get_usage(true));
20+
var_dump(gc_collect_cycles());
21+
?>
22+
--EXPECT--
23+
int(300002)

‎Zend/tests/gh23586_003.phpt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-23586 (GC traversal storage is released when a destructor bails out)
3+
--SKIPIF--
4+
<?php
5+
if (getenv('USE_ZEND_ALLOC') === '0') {
6+
die('skip Zend MM required');
7+
}
8+
?>
9+
--INI--
10+
error_reporting=E_ALL & ~E_DEPRECATED
11+
--FILE--
12+
<?php
13+
class Node {
14+
public $children = [];
15+
16+
public function __destruct() {
17+
trigger_error('Destructor bailout', E_USER_ERROR);
18+
}
19+
}
20+
21+
gc_disable();
22+
$root = new Node();
23+
for ($i = 0; $i < 2000; $i++) {
24+
$root->children[] = (object) ['parent' => $root];
25+
}
26+
unset($root);
27+
gc_collect_cycles();
28+
?>
29+
--EXPECTF--
30+
Fatal error: Destructor bailout in %s on line %d

‎Zend/zend_gc.c‎

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ struct _gc_stack {
333333
static zend_never_inline gc_stack* gc_stack_next(gc_stack *stack)
334334
{
335335
if (UNEXPECTED(!stack->next)) {
336-
gc_stack *segment = emalloc(sizeof(gc_stack));
336+
/* Traversal must not bail out on the request memory limit after changing refcounts. */
337+
gc_stack *segment = pemalloc(sizeof(gc_stack), true);
337338
segment->prev = stack;
338339
segment->next = NULL;
339340
stack->next = segment;
@@ -371,7 +372,7 @@ static void gc_stack_free(gc_stack *stack)
371372

372373
while (p) {
373374
stack = p->next;
374-
efree(p);
375+
pefree(p, true);
375376
p = stack;
376377
}
377378
}
@@ -1943,7 +1944,7 @@ static zend_never_inline void gc_call_destructors_in_fiber(uint32_t end)
19431944
}
19441945
}
19451946

1946-
ZEND_API int zend_gc_collect_cycles(void)
1947+
static int zend_gc_collect_cycles_impl(gc_stack *stack)
19471948
{
19481949
int total_count = 0;
19491950
bool should_rerun_gc = 0;
@@ -1961,10 +1962,6 @@ ZEND_API int zend_gc_collect_cycles(void)
19611962
zend_refcounted *p;
19621963
uint32_t gc_flags = 0;
19631964
uint32_t idx, end;
1964-
gc_stack stack;
1965-
1966-
stack.prev = NULL;
1967-
stack.next = NULL;
19681965

19691966
if (GC_G(gc_active)) {
19701967
GC_G(collector_time) += zend_hrtime() - start_time;
@@ -1976,17 +1973,16 @@ ZEND_API int zend_gc_collect_cycles(void)
19761973
GC_G(gc_active) = 1;
19771974

19781975
GC_TRACE("Marking roots");
1979-
gc_mark_roots(&stack);
1976+
gc_mark_roots(stack);
19801977
GC_TRACE("Scanning roots");
1981-
gc_scan_roots(&stack);
1978+
gc_scan_roots(stack);
19821979

19831980
GC_TRACE("Collecting roots");
1984-
count = gc_collect_roots(&gc_flags, &stack);
1981+
count = gc_collect_roots(&gc_flags, stack);
19851982

19861983
if (!GC_G(num_roots)) {
19871984
/* nothing to free */
19881985
GC_TRACE("Nothing to free");
1989-
gc_stack_free(&stack);
19901986
GC_G(gc_active) = 0;
19911987
goto finish;
19921988
}
@@ -2035,7 +2031,7 @@ ZEND_API int zend_gc_collect_cycles(void)
20352031
while (idx != end) {
20362032
if (GC_IS_DTOR_GARBAGE(current->ref)) {
20372033
p = GC_GET_PTR(current->ref);
2038-
count -= gc_remove_nested_data_from_buffer(p, current, &stack);
2034+
count -= gc_remove_nested_data_from_buffer(p, current, stack);
20392035
}
20402036
current++;
20412037
idx++;
@@ -2058,8 +2054,6 @@ ZEND_API int zend_gc_collect_cycles(void)
20582054
}
20592055
}
20602056

2061-
gc_stack_free(&stack);
2062-
20632057
/* Destroy zvals. The root buffer may be reallocated. */
20642058
GC_TRACE("Destroying zvals");
20652059
zend_hrtime_t free_start_time = zend_hrtime();
@@ -2144,6 +2138,32 @@ ZEND_API int zend_gc_collect_cycles(void)
21442138
return total_count;
21452139
}
21462140

2141+
static int zend_gc_collect_cycles_with_stack(gc_stack *stack)
2142+
{
2143+
int count;
2144+
2145+
zend_try {
2146+
count = zend_gc_collect_cycles_impl(stack);
2147+
} zend_catch {
2148+
gc_stack_free(stack);
2149+
zend_bailout();
2150+
} zend_end_try();
2151+
2152+
gc_stack_free(stack);
2153+
return count;
2154+
}
2155+
2156+
ZEND_API int zend_gc_collect_cycles(void)
2157+
{
2158+
/* Keep the modified stack outside the function containing setjmp(), so it remains
2159+
* valid when freeing traversal storage after a bailout. */
2160+
gc_stack stack;
2161+
2162+
stack.prev = NULL;
2163+
stack.next = NULL;
2164+
return zend_gc_collect_cycles_with_stack(&stack);
2165+
}
2166+
21472167
ZEND_API void zend_gc_get_status(zend_gc_status *status)
21482168
{
21492169
status->active = GC_G(gc_active);

0 commit comments

Comments
 (0)