Skip to content

Let the embedder stop the schema compiler before it overflows the stack - #3

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:bound-schema-compiler-recursion
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:bound-schema-compiler-recursion

Conversation

@groeneai

@groeneai groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown

The schema compiler recurses once per type-expression suffix (Int32.a.a..., Int32()()...), once per link of a chain of declarations that refer to each other by name (aliases, constants, annotations, struct field types, superclasses, imports), and once per level of the eager dependency traversal. A long flat chain exhausts the thread stack: in ClickHouse one DESC format(CapnProto, '') with such a schema kills the server with SIGSEGV (ClickHouse/ClickHouse#125782). ClickHouse's existing check bounds only the bracket nesting of the text, and these chains are only visible after name resolution.

This adds capnp::compiler::setStackCheck(void (*)()), a process-wide hook that is unset by default, so behaviour without it is unchanged. The compiler runs it at two places:

  • the top of BrandScope::compileDeclExpression, which bounds every chain whose next link is compiled by name;
  • the top of Compiler::Node::traverse, which bounds the eager dependency traversal. It can walk links that were already compiled elsewhere without compiling any name (for example the types of struct default values, compiled to bootstrap from the defining struct), so the first check alone does not see that depth.

When the hook throws, compilation fails with an OVERLOADED "Schema is nested too deeply." exception; the hook's own exception never travels through capnp frames. resolveBootstrapType rethrows OVERLOADED unchanged: the lazy bootstrap load it guards can re-enter the compiler, and wrapping the error at every level would turn it into a quadratic "Internal compiler bug" message.

ClickHouse installs its checkStackSize as the hook: ClickHouse/ClickHouse#125900.

🤖 Generated with Claude Code

The compiler recurses once per type-expression suffix (`Int32.a.a...`,
`Int32()()...`) and once per link of a chain of declarations that refer to
each other (aliases, constants, annotations, struct field types, superclasses,
imports). A long flat chain therefore exhausts the thread stack, and no
lexical bound on the schema text can see those chains.

Add `setStackCheck()`, a process-wide hook (unset by default, so behaviour is
unchanged) that the compiler runs at the top of
`BrandScope::compileDeclExpression` and of `Compiler::Node::traverse`. The
first bounds every chain whose links are compiled by name; the second bounds
the eager dependency traversal, which can walk links that were already
compiled elsewhere (for example the types of struct default values). If the
hook throws, compilation fails with an OVERLOADED "Schema is nested too
deeply." exception.

`resolveBootstrapType` rethrows OVERLOADED unchanged: the lazy bootstrap load
it guards can re-enter the compiler, and wrapping the error at every level
would turn it into a quadratic "Internal compiler bug" message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member

Using https://github.com/ClickHouse/capnproto/tree/ClickHouse/v1.5.0 instead of master (updated the branch, cherry-picked the commits there)

@Algunenano Algunenano closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants