Repository navigation
Make Field copy and destroy iterative to survive deeply nested literals - #110393
Algunenano merged 3 commits into
Conversation
A deeply nested collection literal (e.g. `[[[ ... ]]]`) is parsed into a single `Field` nested one `Array`/`Tuple`/`Map` per level. Since the collection parser became iterative, that `Field` can be built to an arbitrary depth (bounded only by the client-settable `max_parser_depth`) without tripping the parser's periodic `checkStackSize` guard, which relies on native recursion to fire. The recursive `Field` copy constructor (reached via `ASTLiteral::clone` when the analyzer clones the AST) and the `Field` destructor then recurse once per nesting level with no stack guard and overflow the native stack, crashing the server. A `checkStackSize` guard cannot fix the destructor because throwing from a destructor calls `std::terminate`. Make the copy and destroy of `Array`/`Tuple`/`Map`/`Object` fields (plus the copy- and move-assignment paths that route through them) iterative, using an explicit worklist so the native stack depth stays bounded regardless of the nesting depth. Guarded type-inference/formatting walks still reject genuinely too-deep structures cleanly with `TOO_DEEP_RECURSION`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [1f7d034] Summary: ✅
AI ReviewSummaryThis PR makes Tests
Final VerdictStatus: |
The iterative destroy runs from `~Field` (noexcept), but its worklist can allocate, and the global `operator new` throws `MEMORY_LIMIT_EXCEEDED` on the soft memory limit like any other allocation. The old recursive destructor never allocated, so this would be a new way for teardown to `std::terminate` under memory pressure. Wrap the worklist in `LockMemoryExceptionInThread`, which suppresses the memory-limit exception while still tracking memory, so the memory freed during teardown is still credited (no tracker drift) and only a genuine out-of-memory `bad_alloc` remains. Also use an inline worklist buffer so the common (deep and therefore narrow) case does not allocate at all. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…alues Guards the iterative Field copy/destroy against regressions and covers the container-lvalue entry points (the templated Field(T&&) constructor and assignment, e.g. Field(object) in the analyzer): they forward to the underlying container copy, whose elements are copied via the now-iterative Field copy, so a deeply nested container lvalue must not overflow the native stack either. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 120/126 (95.24%) · Uncovered code |
…rvive deeply nested literals
Backport #110393 to 26.6: Make Field copy and destroy iterative to survive deeply nested literals
Backport #110393 to 26.5: Make Field copy and destroy iterative to survive deeply nested literals
Backport #110393 to 26.4: Make Field copy and destroy iterative to survive deeply nested literals
Backport #110393 to 26.3: Make Field copy and destroy iterative to survive deeply nested literals
The backport of #110393 changed the Array/Tuple/Map/Object cases of Field::destroy to call destroyContainerIteratively(old_which), but kept the pre-existing 25.8 structure that switched on which and set which = Types::Null at the end, so old_which was never declared. Match master: capture old_which = which and reset which = Types::Null before the switch, then switch on old_which. This also satisfies the destroyContainerIteratively contract that which is already Null on entry. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Backport #110393 to 25.8: Make Field copy and destroy iterative to survive deeply nested literals
…tive to survive deeply nested literals
…tive to survive deeply nested literals
…tive to survive deeply nested literals
…tive to survive deeply nested literals
…literal-stack-overflow Make Field copy and destroy iterative to survive deeply nested literals
OTHER: deeply nested literal copy/destroy -> crash. Effect in the production build a394e92: its upstream test(s) 04538_deep_nested_literal_clone_no_crash fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv). Upstream patch unchanged, with its test(s): 04538_deep_nested_literal_clone_no_crash. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`AsyncLoader::worker`, `remove` and `schedule` run their bookkeeping under `DENY_ALLOCATIONS_IN_SCOPE`, and `LoadJob::finish` resets `func` and `dependency_failure` inside it, destroying whatever the job captured. A table-load job from `DatabaseOrdinary::loadTableFromMetadataAsync` holds the last reference to the CREATE query AST. Since ClickHouse#110393, destroying a `Field` with more than 16 non-empty nested containers allocates (the inline worklist of `Field::destroyContainerIteratively`), so a debug server aborts with `Memory tracker: allocations not allowed` at startup when a table whose definition holds such a literal (e.g. `DEFAULT [[1], [2], ..., [17]]`) finishes loading. Seen in `La Casa Del Dolor (amd_debug)` (STID 4176-5176). Destroy the two callbacks under `ALLOW_ALLOCATIONS_IN_SCOPE`, as `ThreadPool` does for its jobs (ClickHouse#63829). `LoadJob::finish` is the only place where the loader destroys job callbacks, so the worker, `remove()` and the cancel in `schedule()` are all covered, and the callbacks are still destroyed before `finished.notify_all()`. Both macros are no-ops with `NDEBUG`. The new gtests cover the three paths; each aborts without the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A deeply nested collection literal such as
SELECT [[[ ... ]]]is parsed into a singleASTLiteralwhoseFieldnests oneArray/Tuple/Mapper level. Since the collection-of-literals parser is iterative, thatFieldcan be built to an arbitrary depth (bounded only by the client-settablemax_parser_depth) without tripping the parser's periodiccheckStackSizeguard, which relies on native recursion to fire.The recursive
Fieldcopy constructor (reached viaASTLiteral::clonewhen the analyzer clones the AST) and theFielddestructor then recurse once per nesting level with no stack guard and overflow the native stack, crashing the server. AcheckStackSizeguard cannot cover the destructor, because throwing from a destructor callsstd::terminate.This makes the copy and destroy of
Array/Tuple/Map/Objectfields (and the copy- and move-assignment paths that route through them) iterative, using an explicit worklist so the native stack depth stays bounded regardless of the nesting depth. The stack-guarded type-inference and formatting walks still reject genuinely too-deep structures cleanly withTOO_DEEP_RECURSION.Related: #108892
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server crash (native stack overflow) when a deeply nested
Array/Tuple/Map/Objectliteral is copied or destroyed, for example a query with a very deeply nested literal at a raisedmax_parser_depth.Version info
26.6.2.87,26.5.6.59,26.4.5.147,26.3.17.60,25.8.29.27