You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stack overflow when destroying deeply nested arrays or objects #15869
Ah, nevermind. I can see it now. If the memory_limit is higher, this can trigger a stack-overflow when freeing the memory.
Fatal error: Allowed memory size of 134217728 bytes exhausted at file:0 (tried to allocate 224 bytes) in /home/ilutov/Developer/php-src/test.php on line 17
AddressSanitizer:DEADLYSIGNAL
=================================================================
==2207238==ERROR: AddressSanitizer: stack-overflow on address 0x7fff20c31ff8 (pc 0x0000012e7936 bp 0x7fff20c32030 sp 0x7fff20c31ff0 T0)
#0 0x12e7936 in zend_array_destroy /home/ilutov/Developer/php-src/Zend/zend_hash.c:1747
#1 0x1268336 in rc_dtor_func /home/ilutov/Developer/php-src/Zend/zend_variables.c:57
#2 0x12cc9b1 in i_zval_ptr_dtor /home/ilutov/Developer/php-src/Zend/zend_variables.h:44
#3 0x12e7ec2 in zend_array_destroy /home/ilutov/Developer/php-src/Zend/zend_hash.c:1769
#4 0x1268336 in rc_dtor_func /home/ilutov/Developer/php-src/Zend/zend_variables.c:57
#5 0x12cc9b1 in i_zval_ptr_dtor /home/ilutov/Developer/php-src/Zend/zend_variables.h:44
#6 0x12e7ec2 in zend_array_destroy /home/ilutov/Developer/php-src/Zend/zend_hash.c:1769
#7 0x1268336 in rc_dtor_func /home/ilutov/Developer/php-src/Zend/zend_variables.c:57
#8 0x12cc9b1 in i_zval_ptr_dtor /home/ilutov/Developer/php-src/Zend/zend_variables.h:44
#9 0x12e7ec2 in zend_array_destroy /home/ilutov/Developer/php-src/Zend/zend_hash.c:1769
#10 0x1268336 in rc_dtor_func /home/ilutov/Developer/php-src/Zend/zend_variables.c:57
#11 0x12cc9b1 in i_zval_ptr_dtor /home/ilutov/Developer/php-src/Zend/zend_variables.h:44
#12 0x12e7ec2 in zend_array_destroy /home/ilutov/Developer/php-src/Zend/zend_hash.c:1769
#13 0x1268336 in rc_dtor_func /home/ilutov/Developer/php-src/Zend/zend_variables.c:57
#14 0x12cc9b1 in i_zval_ptr_dtor /home/ilutov/Developer/php-src/Zend/zend_variables.h:44
#15 0x12e7ec2 in zend_array_destroy /home/ilutov/Developer/php-src/Zend/zend_hash.c:1769
I'm not sure this is worth fixing. It realistically only occurs in maliciously crafted data. /cc @arnaud-lb
It would be useful to fix this (it can happen with real code due to bugs - at least it happened to me during development), but I'm not sure we have a good way to do it. The same problem exists with nested objects.
changed the title [-]Segmentation fault (stack overflow) in Zend[/-][+]Stack overflow when destroying deeply nested arrays or objects[/+]on Nov 9, 2026
Some data points on current code, plus a prototype with measurements, in case they help decide whether this is worth fixing.
Still reproducible on master (930dacc, 8.7.0-dev), 8.6.0 and 8.5.10, and it does not need crafted code. A plain linked list released from its head is enough:
The array form is the same loop with $a = [$a]; followed by $a = null;. Both print built and then die.
First failing depth, 8 MiB stack, memory_limit=-1:
Build
Arrays
Objects
master 930dacc, NTS, Linux aarch64 (--disable-all --enable-cli)
174,805
75,209
8.6.0 ZTS, Linux aarch64 (official php:8.6-cli image)
~174,800
~75,200
8.5.10 NTS, macOS arm64
~87,400
~87,400
zend.max_allowed_stack_size does not cover this path. Same crash with the limit set to 4M on master and 8.6.0, and 1M on 8.5.10. The comparison path already has a check: $a == $b and $a === $b on the same structures throw Maximum call stack size reached during comparison. Destruction is the path that is left unguarded.
On "artificially crafted data structures" (from the discussion on #21494): 75k nodes is within reach of ordinary code. A queue, a parsed token stream or a degenerate tree built from input and held as a singly linked list hits it. Objects fail earlier than arrays, so a fix that only handles array tails leaves the more common shape broken.
A prototype that covers arrays and objects
The patch below (against 930dacc, 8 files, +105 lines) does this:
zend_array_destroy() and zend_objects_store_del() test the stack position on entry, against EG(stack_limit) plus a 128 KiB margin.
With room to spare, nothing changes.
Near the limit, the value is pushed onto a pending list instead of being destroyed. The frame that hit the limit then destroys the pending values in a loop; values deferred while it runs join the same list, so the rest of the structure is released at a constant stack depth.
A pending object holds one reference, like an object whose destructor is running, so it can still be resurrected.
Destruction order is unchanged until the stack is nearly exhausted. For a chain released from its head it stays head-to-tail even then.
What I checked on master, patched against unpatched:
Both reproducers pass at 1,000,000 levels.
A script covering destructor order on a linked list, an exception thrown by a deferred destructor, mixed array/object nesting, two independent deep branches, a collected cycle and a live list surviving a collection prints the same output as the unpatched build at 20,000 levels, and the same again at 400,000.
A 300,000-level release inside a Fiber passes.
Zend/tests: 5569 passed, 0 failed, identical to the unpatched build (minimal --disable-all build, so extension tests did not run).
Cost
Instructions executed, measured with callgrind:
Script
Unpatched
Patched
Diff
Zend/bench.php
3,841,001,532
3,841,006,156
+0.000%
Destruction-heavy loop (below)
1,364,865,608
1,377,465,733
+0.92%
That is about 4 instructions per array or object destruction. The second script is a worst case built for this path: 300,000 iterations that each create and destroy a nested array and two objects. I have no WordPress or Symfony numbers; your CI benchmark would say more than mine.
The "draining" flag is per executor, not per fiber: if a destructor suspends a fiber while the list is being drained, values deferred elsewhere wait until it resumes.
Destructors of deferred objects run with the margin as their stack budget.
Is a cost in this range acceptable for this bug? If so I can turn the prototype into a proper PR.
Activity
iluuu1994 commentedon Sep 13, 2024
I cannot reproduce this on PHP-8.2 or master. @YuanchengJiang Did you only test this on 5.5? If so, we no longer support that. See https://www.php.net/supported-versions.php.
iluuu1994 commentedon Sep 13, 2024
Ah, nevermind. I can see it now. If the memory_limit is higher, this can trigger a stack-overflow when freeing the memory.
I'm not sure this is worth fixing. It realistically only occurs in maliciously crafted data. /cc @arnaud-lb
arnaud-lb commentedon Sep 13, 2024
It would be useful to fix this (it can happen with real code due to bugs - at least it happened to me during development), but I'm not sure we have a good way to do it. The same problem exists with nested objects.
[-]Segmentation fault (stack overflow) in Zend[/-][+]Stack overflow when destroying deeply nested arrays or objects[/+]Guikingone commentedon Nov 29, 2026
Some data points on current code, plus a prototype with measurements, in case they help decide whether this is worth fixing.
Still reproducible on master (930dacc, 8.7.0-dev), 8.6.0 and 8.5.10, and it does not need crafted code. A plain linked list released from its head is enough:
The array form is the same loop with
$a = [$a];followed by$a = null;. Both printbuiltand then die.First failing depth, 8 MiB stack,
memory_limit=-1:--disable-all --enable-cli)php:8.6-cliimage)zend.max_allowed_stack_sizedoes not cover this path. Same crash with the limit set to4Mon master and 8.6.0, and1Mon 8.5.10. The comparison path already has a check:$a == $band$a === $bon the same structures throwMaximum call stack size reached during comparison. Destruction is the path that is left unguarded.On "artificially crafted data structures" (from the discussion on #21494): 75k nodes is within reach of ordinary code. A queue, a parsed token stream or a degenerate tree built from input and held as a singly linked list hits it. Objects fail earlier than arrays, so a fix that only handles array tails leaves the more common shape broken.
A prototype that covers arrays and objects
The patch below (against 930dacc, 8 files, +105 lines) does this:
zend_array_destroy()andzend_objects_store_del()test the stack position on entry, againstEG(stack_limit)plus a 128 KiB margin.Destruction order is unchanged until the stack is nearly exhausted. For a chain released from its head it stays head-to-tail even then.
What I checked on master, patched against unpatched:
Zend/tests: 5569 passed, 0 failed, identical to the unpatched build (minimal--disable-allbuild, so extension tests did not run).Cost
Instructions executed, measured with callgrind:
Zend/bench.phpThat is about 4 instructions per array or object destruction. The second script is a worst case built for this path: 300,000 iterations that each create and destroy a nested array and two objects. I have no WordPress or Symfony numbers; your CI benchmark would say more than mine.
Known gaps
.phpttests yet; not built on ZTS or Windows.Is a cost in this range acceptable for this bug? If so I can turn the prototype into a proper PR.
Prototype patch against 930dacc
arnaud-lb commentedon Nov 30, 2026
@Guikingone the patch makes sense to me. Feel free to create a PR against master.
The fiber gap would be fixed by #20001 later.
Nit: please rename "deferred dtors" to "deferred garbage" to avoid the confusion with destructors.