Repository navigation
Fix stack overflow in the CapnProto schema compiler on long chains of declarations - #125900
Algunenano merged 4 commits into
Conversation
… declarations `checkCapnProtoSchemaNestingDepth` bounds the bracket nesting of the schema text, which drives the recursion of the Cap'n Proto lexer and parser. The schema compiler recurses along a different axis: once per `.member` or `()` suffix of a type expression, and once per link of a chain of declarations that refer to each other by name (aliases, constants, annotations, struct field types, superclasses, imports). Such a chain has no deep nesting, so a `DESC format(CapnProto, '')` with a schema of 20000 chained declarations crashed the process with SIGSEGV. Bump `contrib/capnproto` to a fork commit that lets the embedder plug a stack check into the compiler's recursion, install `checkStackSize` there, and report the result as `The CapnProto schema is nested too deeply` (CANNOT_PARSE_CAPN_PROTO_SCHEMA), the same error as the bracket check. The parser also builds one level of its parse tree per `.member` or `()` suffix, and that tree is torn down recursively (`OrphanBuilder::euthanize` -> `WireHelpers::zeroObject`) when the parser is destroyed or a statement fails to parse. That recursion runs in destructors, where no throwing check can stop it, so 100000 suffixes still crashed after the compiler check had fired. The text check therefore also tracks the depth of the expression tree (`.` and closing brackets add a level, `,` `;` `}` end an expression) and rejects more than DBMS_DEFAULT_MAX_PARSER_DEPTH levels with the same error. Closes ClickHouse#125782 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; 2 Gate B runs, 1 nit in total) plus my own review of each round; one fix round.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261014-221600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261014-214900 |
|
Workflow [PR], commit [fa69861] Summary: ❌
AI ReviewSummaryThis PR rebases the embedded Final Verdict
|
|
cc @alexey-milovidov @vitlibar, could you review this and add |
A chain of 20000 `using A{i} = A{i+1}` aliases resolved from a field type
SIGSEGVs on master and on 26.8/26.9 release images (crashes from between
2000 and 5000 links there) and gives Code 622 with the fix. Each hop goes
through `Alias::compile` -> `compileDecl` -> `compileDeclExpression`, so the
existing check covers it; the case pins the changelog's alias claim.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…parser `contrib/capnproto` now points to `ClickHouse/v1.5.0`: upstream v1.5.0 with the internals patch, the `std::nullptr_t` fix and the stack check hook. Its parser rejects an expression with more than 64 `.member` or `(...)` suffixes before building the tree, so the text check no longer tracks them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I've updated the Capnproto contrib to v1.5.0 and added the commit there, plus bringing the needed changes we already had and an extra fix to simplify our own code checks (control the recursion in the contrib instead of externally ourselves) |
The msan flaky check failed with "Test runs too long (> 180s)": 05397 took 196-203s per run there (140s on 762ff37). Now that the parser rejects more than 64 suffixes, the 200000-suffix chains only add lexing time, and the declaration chains were about 6-35 times longer than needed. The CI binaries of ac2531f reject the chains at these lengths: amd_binary alias 1412, const 575, enum annotation 614, struct with defaults 3478 (tsan and debug at or below that), suffixes at 65. The new lengths (suffixes 20000, declaration chains 5000, struct chain 12000) still give Code 622 on amd_binary, msan, tsan and debug, and still SIGSEGV on master and 26.8.18.2. One uncontended msan run: 37.2s -> 16.9s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - fa69861Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261015-150015 |
ccebf85
Backport #125900 to 26.8: Fix stack overflow in the CapnProto schema compiler on long chains of declarations
Backport #125900 to 26.9: Fix stack overflow in the CapnProto schema compiler on long chains of declarations
Backport #125900 to 26.7: Fix stack overflow in the CapnProto schema compiler on long chains of declarations
Backport #125900 to 26.3: Fix stack overflow in the CapnProto schema compiler on long chains of declarations
…to schema compiler on long chains of declarations
Closes: #125782
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash (stack overflow) on a CapnProto schema with a long chain of type expression suffixes (
Int32.a.a...,Int32()()...) or of declarations that refer to each other (constants, aliases, struct fields, annotations, imports). Such a schema is now rejected withThe CapnProto schema is nested too deeply. Closes #125782.Description
On 26.8.21.12, one
DESC format(CapnProto, '')with a schema of 20000 chained constants, aliases, structs or.asuffixes crashes the process with SIGSEGV (stack overflow). Master crashes too: the bracket check from #115700 and #125487 does not see these chains.That check bounds the bracket nesting of the schema text. The schema compiler recurses along other axes: once per
.memberor()suffix, once per link of a chain of declarations that refer to each other by name, and once per level of its dependency traversal.contrib/capnprotonow points to the fork'sClickHouse/v1.5.0branch: upstream v1.5.0, whose lexer limits bracket nesting to 64 levels, plus the fork's patches and two new commits:.memberor(...)suffixes before building its tree. That tree is torn down recursively in destructors, so no throwing check can stop it later.BrandScope::compileDeclExpressionandCompiler::Node::traverse, and its error passes through the compiler's catch sites. A depth counter cannot bound these chains: one level's stack cost differs about 10x across chain kinds and builds.ClickHouse installs
checkStackSizeas the hook and reports both errors asThe CapnProto schema is nested too deeply(CANNOT_PARSE_CAPN_PROTO_SCHEMA).The new test has one deep case per path (
member_chain,application_chain,nested_suffix_chains,alias_chain,const_chain,struct_chain_with_defaults,enum_annotation_chain) plus a short valid control. All deep cases fail on master and 26.8 and give Code 622 with the fix. On the final tree, a sweep of 21 chain shapes up to 50000 links, suffix chains of up to 1000000 links, and a 5000-file import chain shows no crash; the 16 upstream Cap'n Proto schemas give unchanged results.Workflow [PR]
Sync PR [sync-upstream/pr/125900]
Version info
26.10.1.2493-master(included in26.10and later)26.9.20.20,26.8.29.6,26.7.32.6,26.3.53.7