Repository navigation
Fix GH-15869: Stack overflow when destroying deeply nested arrays or objects - #25969
Open
Guikingone wants to merge 1 commit into
Open
Guikingone wants to merge 1 commit into
Guikingone wants to merge 1 commit into
Conversation
…or objects Destroying an array or an object destroys its children from inside the same call, so a deeply nested structure, such as a long linked list, overflows the native stack when its last reference is dropped. zend_array_destroy() and zend_objects_store_del() now compare the stack position with EG(stack_limit) plus ZEND_GARBAGE_STACK_MARGIN. Near the limit the value is queued in EG(deferred_garbage) instead of being destroyed, and zend_defer_garbage() destroys the queue in a loop. Values deferred while the loop runs join the queue, so the rest of the structure is released at a constant stack depth. A deferred object holds one reference, like an object whose destructor is running, so it can still be resurrected. A value released from a destructor, which runs in a frame of its own, starts a loop of its own, so that unbounded recursion through destructors still reaches the stack limit and throws. The queue is reset when a request starts and ends, and the draining flag when a bailout happens, as a fatal error in a destructor leaves the loop with a longjmp. Nothing changes until the stack is within the margin of the limit, and the mechanism is inactive where the stack limit is unsupported or disabled with zend.max_allowed_stack_size=-1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes GH-15869.
The bug
Destroying an array or an object destroys its children from inside the same call, one native stack frame group per nesting level. A structure that is nested deeply enough overflows the native stack and the process crashes with SIGSEGV when the last reference is dropped. A singly linked list is enough, no crafted data is needed:
The same happens with
$a = [$a]in a loop and then$a = null.zend.max_allowed_stack_sizedoes not help: comparison, compilation and function calls check it, destruction does not.The change
zend_array_destroy()andzend_objects_store_del()compare the native stack position withEG(stack_limit)plus a margin (ZEND_GARBAGE_STACK_MARGIN, 128 KiB). With room to spare nothing changes. Within the margin the value is not destroyed but pushed on a list of deferred garbage (EG(deferred_garbage)), andzend_defer_garbage()destroys the values on that list in a loop. Values that become deferred while the loop runs are added to the same list, so the rest of the structure is released at a constant stack depth.EG(current_execute_data)of the frame that is draining it). Without this, unbounded recursion through destructors (for example a destructor that doesnew Foo) would no longer reach the stack limit but would loop forever at a constant depth: the existingstack_limit_001,002,003,006andgh16041_001tests catch that. It also lets a destructor release another deep structure.This is new compared with the prototype posted on the issue, which had that defect. The
Zend/testsrun I reported there was made without--enable-zend-test, so these tests were skipped and I did not see it.init_executor()andshutdown_executor(), and the draining flag in_zend_bailout(), because a fatal error in a destructor leaves the loop throughlongjmp. Without the reset a process that serves several requests (php-fpm, CLI server) keeps a dangling list pointer: with the reset removed, the second request after such a fatal error corrupted the heap on the CLI server (zend_mm_heap corrupted).zend_array_destroy_now()andzend_objects_store_del_now()are the destruction bodies without the check. They are only needed byzend_defer_garbage()in another translation unit, so they are declared inzend_variables.hnext to it, withoutZEND_API, and only exist whereZEND_CHECK_STACK_LIMITis defined. The public functions keep their signatures and symbols (JIT calls them as before).zend.max_allowed_stack_size=-1EG(stack_limit)is 0, the check is never true, and the behaviour is the one before this change (the crash stays possible, as it was).UPGRADING.INTERNALShas a note:zend_array_destroy()andzend_objects_store_del()can return before the array or object is destroyed, when the stack is nearly exhausted.What changes for users
Nothing until the stack is within 128 KiB of the limit. Past that point, structures that used to crash are released.
Cost
Measured on the prototype of this change (against 930dacc), release build, instructions executed under callgrind:
Zend/bench.phpMeasured again on the final version against current master (
2536fb681df, aarch64 Linux, GCC 13,-O2,--disable-all --enable-cli --enable-zend-test):Zend/bench.phpThat is about 4 instructions per array or object destruction (one stack comparison, no memory access beyond
EG()). The second script is a worst case built for this path. Both measurements are NTS; the cost on ZTS, whereEG()goes through the thread-local lookup, was not measured. I have no application benchmarks (WordPress, Symfony).Tests
Zend/tests/stack_limit/gh15869_001.phptto013.phpt. All use a reducedzend.max_allowed_stack_size(1M) and are skipped where the stack limit is not supported. All of them crash with SIGSEGV on unpatched master.gc_collect_cycles()called from a destructor during the releasefiber.stack_size)exit()in a deferred destructor: sameThe existing
Zend/tests/stack_limit/tests that rely on unbounded destructor recursion hitting the limit pass.Known limits
zend.max_allowed_stack_size=-1disables the mechanism, as it disables every other stack check.Error.zend.max_allowed_stack_sizeis so small that the margin pluszend.reserved_stack_sizeis more than the whole stack (seen with 128K and below on this build; 192K was not affected), every destruction is deferred and runs in the frame that requested it. This is correct, but siblings are destroyed in reverse order:[new A(1), new A(2)]destroys 2 then 1. Destructors that call functions still work.zend_hash_destroy()andzend_hash_clean()called by engine or extension code on a table of their own are not entry points of the check, but every value they hold is released throughzval_ptr_dtor(), so the next nesting level is checked. Internal objects whosefree_objhandler walks its own structure recursively without going throughzval_ptr_dtor()are not covered. The GC frees garbage without recursion and was not changed.What was tested
--enable-debug --enable-zend-test --disable-all --enable-cli), NTS and ZTS, and a debug NTS build with--enable-address-sanitizer --enable-undefined-sanitizer(GCC 13), all on aarch64 Linux in Docker.Zend/tests: 5774 tests, 67 skipped, 0 failed with the patch on NTS and ZTS; the unpatched build passes the same tests and fails the 13 new ones. In the ASAN run, 13 tests failed (oom_unserialize_*,new_oom,gh23088,gc/gh9993,runtime_compile_time_binary_operands); run again with the defaultmemory_limit, 13 of them fail on an unpatched ASAN build and 12 on the patched one. The new tests pass under ASAN.ZEND_FASTCALL; no new public symbol), 32-bit platforms, extensions other thanzend_test, opcache with the JIT enabled (the JIT reaches both functions through the same exported symbols), other allocators than the Zend MM.