Skip to content

Commit 464b2ea

Browse files
committed
Keep GC traversal consistent under memory exhaustion
Fixes #23586.
1 parent d5facaf commit 464b2ea

7 files changed

Lines changed: 104 additions & 31 deletions

File tree

‎.github/actions/apt-x32/action.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ runs:
99
OPCACHE_TLS_TESTS_DEPS="gcc clang lld"
1010
1111
export DEBIAN_FRONTEND=noninteractive
12-
dpkg --add-architecture i386
13-
apt-get update -y | true
12+
sudo dpkg --add-architecture i386
13+
sudo env DEBIAN_FRONTEND=noninteractive apt-get update -y | true
1414
# TODO: Reenable postgresql + postgresql-contrib packages once they work again.
15-
apt-get install -y \
15+
sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y \
1616
autoconf \
1717
bison \
1818
curl \

‎.github/actions/install-linux-x32/action.yml‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ runs:
55
- shell: bash
66
run: |
77
set -x
8-
make install
9-
mkdir -p /etc/php.d
10-
chmod 777 /etc/php.d
11-
echo mysqli.default_socket=/var/run/mysqld/mysqld.sock > /etc/php.d/mysqli.ini
12-
echo pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock > /etc/php.d/pdo_mysql.ini
13-
echo opcache.enable_cli=1 >> /etc/php.d/opcache.ini
14-
echo opcache.protect_memory=1 >> /etc/php.d/opcache.ini
15-
echo opcache.preload_user=root >> /etc/php.d/opcache.ini
8+
sudo make install
9+
sudo mkdir -p /etc/php.d
10+
sudo chmod 777 /etc/php.d
11+
echo mysqli.default_socket=/var/run/mysqld/mysqld.sock | sudo tee /etc/php.d/mysqli.ini > /dev/null
12+
echo pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock | sudo tee /etc/php.d/pdo_mysql.ini > /dev/null
13+
echo opcache.enable_cli=1 | sudo tee /etc/php.d/opcache.ini > /dev/null
14+
echo opcache.protect_memory=1 | sudo tee -a /etc/php.d/opcache.ini > /dev/null
15+
echo opcache.preload_user=root | sudo tee -a /etc/php.d/opcache.ini > /dev/null

‎.github/workflows/test-suite.yml‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,11 @@ jobs:
232232
name: "LINUX_X32_${{ matrix.debug && 'DEBUG' || 'RELEASE' }}_${{ matrix.zts && 'ZTS' || 'NTS' }}"
233233
runs-on: ubuntu-24.04
234234
timeout-minutes: 180
235-
container:
236-
image: ubuntu:${{ fromJson(inputs.branch).config.ubuntu_version }}
237235
env:
238-
MYSQL_TEST_HOST: mysql
239-
PDO_MYSQL_TEST_DSN: mysql:host=mysql;dbname=test
240-
PDO_MYSQL_TEST_HOST: mysql
241-
PDO_FIREBIRD_TEST_DSN: firebird:dbname=firebird:test.fdb
236+
MYSQL_TEST_HOST: 127.0.0.1
237+
PDO_MYSQL_TEST_DSN: mysql:host=127.0.0.1;dbname=test
238+
PDO_MYSQL_TEST_HOST: 127.0.0.1
239+
PDO_FIREBIRD_TEST_DSN: firebird:dbname=localhost:test.fdb
242240
services:
243241
mysql:
244242
image: mysql:8.4

‎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+
. Garbage collection can complete wide graph traversals after the request
7+
memory limit has been reached. (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_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 < 350000; $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(350002)

‎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)