Repository navigation
Fix client crash when the AST fuzzer formats an EXECUTE AS query - #111725
PedroTadim merged 4 commits into
Conversation
ASTExecuteAsQuery held its target_user and subquery sub-nodes as raw non-owning pointers that are also entries of the base children vector. When a child slot is replaced in place (the AST fuzzer does this, e.g. its ALTER -> lightweight-mutation rewrite reassigns the children slot to a fresh node), the previous node was freed while the raw member kept pointing at it. The client formats the tree after every fuzzer mutation, so the next formatWithSecretsOneLine() dereferenced freed memory and the client crashed with a SIGSEGV in ASTExecuteAsQuery::formatQueryImpl. Make target_user and subquery owning ASTPtr (they stay in children), matching how ASTExplainQuery and other query-with-output carriers store their children. This is the ownership contract documented in QueryFuzzer.cpp for members that alias children. Found by the AST fuzzer (amd_debug) on the CI run of ClickHouse#109433, unrelated to that PR: https://s3.amazonaws.com/clickhouse-test-reports/PRs/109433/3a999136a8f2392093f8d3ee7491ae7f04de9fb2/ast_fuzzer_amd_debug/fuzzer.log.zst Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-0:20260724-042300 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; Gate A approach review + Gate B code review across full pass + fix round + delta rechecks).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-0:20260724-042300 |
|
cc @pufit @george-larionov — could you review this? |
|
@groeneai is the issue from the AST fuzzer? If so, you should fix there |
|
@PedroTadim The crash is triggered by the fuzzer, but the defect is in the AST class, so the fix belongs there.
Fixing it in the fuzzer would mean resyncing every AST's aliasing members after each child replacement, but Happy to do a fuzzer-side change instead if you prefer, but I believe the ownership fix is the right layer. |
|
Workflow [PR], commit [384ba11] Summary: ❌
AI ReviewSummaryThis PR fixes the lifetime bug behind the Findings
Final Verdict |
The two catch (...) blocks in gtest_query_fuzzer_execute_as.cpp were empty (or comment-only), which the CI style check (check_catch_all: silently swallows exceptions) and clang-tidy bugprone-empty-catch (-Werror, Build arm_tidy) reject. Read the current exception message so each block contains a real, used statement. A fuzzer/format exception is expected and tolerated; the tests only assert no crash. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 36/39 (92.31%) · Uncovered code |
CI finish ledger — 3a42b94Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task. Only
Session id: cron:our-pr-ci-monitor:20260724-203000 |
…e-as-fuzzer-format-crash
| IAST * subquery = nullptr; | ||
| /// Owning: these are also kept in `children`, and must own them (a non-owning pointer would | ||
| /// dangle if a child slot is replaced, e.g. by the AST fuzzer). | ||
| ASTPtr target_user; |
There was a problem hiding this comment.
Making these members owning fixes the use-after-free, but it still leaves ASTExecuteAsQuery outside the updatePointerToChild contract. The generic visitor stack already does in-place child replacement and then calls updatePointerToChild, but this node still does not expose target_user / subquery via forEachPointerToChild. After a direct child replacement, children sees the new subtree while formatQueryImpl() and clone() still use the old member, and the new deterministic test below actually asserts that stale-alias state. That turns the SIGSEGV into a silent semantic split instead of a real fix.
Could we add a forEachPointerToChild override here and make the test verify that the alias members track the replacement, rather than just keeping the old node alive?
There was a problem hiding this comment.
Verified the mechanism against the code before replying.
Two facts fix the layer at ownership, not the override:
-
The crash is a use-after-free, and ownership alone resolves it.
QueryFuzzerreassigns child slots in place viafuzz(ast->children)and calls neitherupdatePointerToChildnorforEachPointerToChild(grep = 0 inQueryFuzzer.cpp), so no override could have prevented the SIGSEGV. OwningASTPtrmembers are the sanctioned mechanism:QueryFuzzer.cpp:5975-5982states child-aliasingASTPtris "redundant ownership, but hides the error if the child field is replaced ... others can beASTLiteral *or the like, which leads to segfault". -
The divergence you describe is carried, unchanged, by the direct shipping precedent.
ASTExplainQuery(sameASTQueryWithOutputbase) stores its wrapped query as an owningASTPtraliasingchildren, rebuildschildrenfrom that member inclone(), and does not overrideforEachPointerToChild. The pattern is categorical: owning-ASTPtr-aliasing nodes (ASTExplainQuery,ASTUserNameWithHost) omit the override, while only raw/typed-pointer members (ASTAlterQuery,ASTBackupQuery,ASTCreateQuery, ...) carry it, precisely because a raw pointer can dangle and an owningASTPtrcannot.
On reachability: the generic visitors that call updatePointerToChild (InDepthNodeVisitor, AddDefaultDatabaseVisitor, ReplaceQueryParameterVisitor) rewrite nested nodes and resync the immediate parent of the replaced node, not ASTExecuteAsQuery's direct target_user/subquery slots. InterpreterExecuteAsQuery reads the members and re-parses the subquery from its formatted text. Member and children only diverge under the fuzzer's blind in-place slot swap, exactly as in ASTExplainQuery.
So I am keeping the fix at ownership-parity with ASTExplainQuery (no override). On the test: it asserts the property that is the fix, ownership keeps the aliased node alive across an in-place children replacement (deterministic oracle aborts on the raw-pointer HEAD~1, passes on the fix). I am not asserting that the member tracks the replacement, because this AST class deliberately does not provide that behavior (again, per ASTExplainQuery).
If a maintainer prefers a belt-and-suspenders forEachPointerToChild override here for future-proofing, I will add it, kept consistent with the rest of the owning-ASTPtr family rather than diverging for this one node.
CI finish ledger — 384ba11The PR's own fix (owning-
Session id: cron:our-pr-ci-monitor:20260725-020000 |
CI finish ledger - 384ba11Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Fast test, Style check and Bugfix validation (unit tests) are green on this head. Session id: cron:our-pr-ci-monitor:20260726-163000 |
efccb01
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a client crash when formatting an
EXECUTE ASquery whose AST has a child replaced (as the AST fuzzer does).ASTExecuteAsQuerynow owns itstarget_userandsubquerychildren instead of holding non-owning pointers intochildren.Description
ASTExecuteAsQueryheld itstarget_userandsubquerysub-nodes as raw non-owning pointers that are also entries of the basechildrenvector. When a child slot is replaced in place (the AST fuzzer does this, e.g. itsALTER-> lightweight-mutation rewrite), the previous node was freed while the raw member kept pointing at it. The client formats the tree after every fuzzer mutation, so the nextformatWithSecretsOneLine()dereferenced freed memory and the client crashed with a SIGSEGV inASTExecuteAsQuery::formatQueryImpl.Fix: make
target_userandsubqueryowningASTPtr(they remain inchildren), matching howASTExplainQueryand other query-with-output carriers store their children. This is the ownership contract documented inQueryFuzzer.cppfor members that aliaschildren.Found by the AST fuzzer on the CI run of #109433 (unrelated to that PR):
ast_fuzzer (amd_debug), https://s3.amazonaws.com/clickhouse-test-reports/PRs/109433/3a999136a8f2392093f8d3ee7491ae7f04de9fb2/ast_fuzzer_amd_debug/fuzzer.log.zstNo related open issue found.
Version info
26.8.1.185(included in26.8and later)