Repository navigation
Do not exhaust the coroutine stack when sending or receiving a deeply nested Dynamic value - #127880
Conversation
A remote query sends its scalars and reads the remote blocks inside the
coroutine of RemoteQueryExecutorReadContext, whose stack is 320 KiB. There,
checkStackSize() returned immediately, and SerializationDynamic::serializeBinary
had no check at all. A Dynamic/JSON value nested a few thousand levels deep
(the depth comes from the data) therefore overflowed the coroutine stack:
SIGSEGV on the guard page in debug and sanitizer builds, and in release builds,
which have no guard page, the recursion continued below the heap allocation
("stack smashing detected" in the AST fuzzer, STID 3920-0f50).
checkStackSize() now measures the frame against the stack of the current
StackfulCoroutine: CoroutineStack records the usable range of each stack it
allocates, the coroutine keeps it (and clears it before a forced unwind, so a
destructor cannot throw during forced_unwind), and checkStackSize() throws
TOO_DEEP_RECURSION when less than 64 KiB is left. A fixed reserve is used
instead of STACK_SIZE_FREE_RATIO, which is calibrated for 8 MiB thread stacks
(its TSan value would leave a 320 KiB coroutine 16 KiB). Frames outside the
coroutine stack (the parent stack, between publishing the coroutine and
switching to it) are not checked.
Both SerializationDynamic::serializeBinary overloads get the same guard the
deserialization, hashing and text paths already have.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The receive arm accepted empty output at every depth, so a regressed receive-side guard could pass silently on a release build, where the coroutine stack has no guard page. At 4000 levels the value exceeds the coroutine stack on every build, so require the exception there as the send arm already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop design narration (why an lvalue ctor, why the frame address is taken early, the size check) and shorten the coroutine-reserve, teardown, parent-stack and Silk comments to one line each. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 rounds, 5 findings) plus my own cold review.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261026-172000 |
|
cc @Avogar @mstetsyuk, could you review this and add the |
|
Workflow [PR], commit [c969382] Summary: ✅
AI ReviewSummaryThe PR makes Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
CI finish ledger - 7948036Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first runs of this head stopped at Session id: cron:our-pr-ci-monitor:20261027-213006 |
…in comments A depth of 5 must pass through `remote` on every build, so a coroutine stack check that fires too early now shows up as a test diff instead of being accepted as `TOO_DEEP_RECURSION`. Review: ClickHouse#127880
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units185 translation units recompiled, 1684 s compile time in total, 184 of them have a recent master baseline. Translation units without a recent master baseline:
|
Related: #113835
Related: #118531
Related: #121441
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a crash when a deeply nested
DynamicorJSONvalue is sent to or received from a remote server, for example as a constant in aremote()query or as a column read throughremote(). Such a value now raisesTOO_DEEP_RECURSION.Description
On 26.8.28.14 and 26.9.19.9 (official builds),
SELECT CAST(unhex(concat('01', repeat('3000800820000000010161', 1000), '300080082000000000')), 'AggregateFunction(any, Dynamic)') FROM remote('127.0.0.2', system.one)kills the server with SIGSEGV. The AST fuzzer hit it asstack smashing detectedinSerializationObject::serializeBinary: https://github.com/ClickHouse/ClickHouse/actions/runs/40328492327/job/121652452311A remote query sends its scalars and reads the remote blocks inside the
RemoteQueryExecutorReadContextcoroutine, whose stack is 320 KiB. TherecheckStackSize()returned immediately, andSerializationDynamic::serializeBinaryhad no check at all, so the data-drivenJSON->Dynamicrecursion ran past the stack (in release builds, which have no guard page, below the heap allocation).checkStackSize()now measures the frame against the current coroutine's stack:CoroutineStackrecords the range it allocated,StackfulCoroutinekeeps it (cleared before a forced unwind), and less than 64 KiB left throwsTOO_DEEP_RECURSION. This is the fiber-awarecheckStackSizerequested in AST fuzzer (arm_asan_ubsan): server dies with SIGSEGV and no sanitizer report while a fuzzed query connects viaremoteSecureto a loopback address #113835; MakecheckStackSizeaware of the running coroutine's stack #118531 implemented it and was closed because that crash was in libunwind and never reachedcheckStackSize. This one does.SerializationDynamic::serializeBinaryoverloads get the guard the read, hash and text paths already have.Either part alone still crashes. The new test sends and receives values 1000, 2000 and 4000 levels deep through
remote(): an unpatched debug or ASan server dies at the first line, the patched one reportsTOO_DEEP_RECURSION. I also ran it with bothuse_hedged_requestsvalues and withserialize_query_plan. Reading a deepDynamiccolumn throughremote()crashed the same way and now hits the existingIDataType::getSerializationguard. Silk fibers keep their bypass: they expose no stack bounds.Workflow [PR]
Sync PR [sync-upstream/pr/127880]
Version info
26.11.1.1313-master(included in26.11and later)