gh-151535: Bound _remote_debugging asyncio awaited_by graph recursion#151536
Open
tonghuaroot wants to merge 2 commits into
Open
gh-151535: Bound _remote_debugging asyncio awaited_by graph recursion#151536tonghuaroot wants to merge 2 commits into
tonghuaroot wants to merge 2 commits into
Conversation
…verflow The depth-bounded recursion still overflowed the debugger's C stack on platforms with a small default thread stack (Windows uses 1 MiB): every level keeps a SIZEOF_TASK_OBJ buffer alive in process_task_awaited_by, so the MAX_TASK_AWAITED_BY_DEPTH limit of 1000 was only reached after several MiB of stack had already been consumed, and the process aborted with a stack overflow before the limit could fire. Walk the awaited_by graph with an explicit, heap-allocated work-stack instead of mutual recursion, so the C stack depth stays constant no matter how deep the graph is. The depth limit is retained as a cycle guard for corrupted or concurrently-mutated remote memory. Also make the regression test deterministic under load: signal readiness from the leaf task itself, immediately before it busy-spins, so the observer always inspects while the full chain is built and rooted at a running task. The previous handshake was sent before the leaf started running and could race, letting the observer see a shallow graph.
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.
_remote_debugging.RemoteUnwinder.get_async_stack_trace()walks the targetprocess's asyncio
awaited_bygraph with an unbounded three-function C recursion(
process_task_and_waiters→process_task_awaited_by→process_waiter_task→process_task_and_waiters, inModules/_remote_debugging/asyncio.c). Each levelalso stack-allocates
char task_obj[SIZEOF_TASK_OBJ](4096 bytes), so a deep orcyclic graph in the target overflows the debugger's C stack and crashes it with
SIGSEGV. This is asymmetric with the iterative sibling path
(
append_awaited_by_for_thread), which already bounds its walk withMAX_ITERATIONS.This PR bounds the recursion with an explicit depth cap
(
MAX_TASK_AWAITED_BY_DEPTH, defined next to the existingMAX_ITERATIONS/MAX_SET_TABLE_SIZE/MAX_*constants in_remote_debugging.h). The depth isthreaded through the existing generic
processor/contextcallback via a smallwaiter_context_t { result, depth }, so the publicprocess_task_and_waiterssignature is unchanged (it stays a
depth == 0wrapper over the newprocess_task_and_waiters_impl). On overflow the code raises aRuntimeErrorthrough the module's existing
set_exception_causeerror path, matching how themodule reports every other "corrupted remote memory" condition. A depth counter
(rather than a visited-set) also covers a cyclic
awaited_bygraph from corruptedremote memory, since each recursion still increments the depth.
The module already documents that the target's memory is untrusted input
(
debug_offsets_validation.h), so a debugger reading an attacker-controlled ormerely deeply-nested target must not be crashable by the target's graph shape;
bounding this traversal is consistent with the module's existing invariants.
Adds
test_async_deep_awaited_by_chain_is_boundedtoLib/test/test_external_inspection.py: it spawns a target with a deep linearawaited_bychain whose leaf is the running task and asserts thatget_async_stack_trace()raisesRuntimeError("too deep or cyclic") instead ofcrashing.
Verified that the pre-fix interpreter segfaults (SIGSEGV) on a deep chain while the
patched interpreter raises a clean
RuntimeError, and that a shallow chain stillreturns a stack trace normally (negative control). The existing asyncio
stack-trace tests continue to pass.
Closes #151535
This change was prepared with AI assistance; I have reviewed and verified the code,
the reasoning, and the test myself.