Skip to content

Fix null pointer dereference when logging a distributed plan local task - #107079

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-distributed-plan-local-null-ast-formatimpl
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-distributed-plan-local-null-ast-formatimpl

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a crash (null pointer dereference) that could happen when a distributed query plan was executed locally (make_distributed_plan + distributed_plan_execute_locally) with log_formatted_queries = 1.

Description

A local distributed-plan task fragment is built from a serialized query plan, not parsed, so it has no query AST. doExecuteTask passed an empty ASTSelectQuery() to logQueryStart only to carry query_kind. An empty ASTSelectQuery has no SELECT expression list, so with log_formatted_queries = 1 the log path formatted it and ASTSelectQuery::formatImpl dereferenced the absent SELECT (select()->format()), a member call on a null IAST.

Fix: pass a null ASTPtr instead of the malformed stub. logQueryStart, logQueryFinish and logQueryException already treat a null AST as QueryKind::Select (the kind the stub was constructed to provide), so behavior is unchanged for query_kind while the formatting crash is avoided.

Found by the BuzzHouse fuzzer (arm_asan_ubsan), STID 2791-2e42. The UBSAN report was member call on null pointer of type 'DB::IAST' at ASTSelectQuery.cpp:104.

Regression test 04327_distributed_plan_local_log_formatted_queries runs the affected path; it aborts the server before this change and passes after.

A local distributed-plan task fragment (make_distributed_plan +
distributed_plan_execute_locally) is built from a serialized query plan,
not parsed, so it has no query AST. doExecuteTask passed an empty
ASTSelectQuery() to logQueryStart purely to carry query_kind. An empty
ASTSelectQuery has no SELECT expression list, so with
log_formatted_queries = 1 logQueryStart formatted it and
ASTSelectQuery::formatImpl dereferenced the absent SELECT
(select()->format()), a member call on a null IAST. UBSAN reports it as
"member call on null pointer of type 'DB::IAST'"; release builds hit
undefined behavior / a crash.

Fix: pass a null ASTPtr instead of the malformed stub. logQueryStart,
logQueryFinish and logQueryException already treat a null AST as
QueryKind::Select (the kind the stub was constructed to provide) and skip
the interpreter-specific extension, so behavior is unchanged for query_kind
while the formatting crash is avoided.

Found by the BuzzHouse fuzzer (arm_asan_ubsan), STID 2791-2e42.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. SELECT count() FROM t AS x, t AS y WHERE x.id = y.a SETTINGS make_distributed_plan=1, enable_parallel_replicas=0, distributed_plan_execute_locally=1, query_plan_use_new_logical_join_step=1, log_formatted_queries=1 aborts the server on every run on unpatched master (fc73f46).
b Root cause explained? A local distributed-plan task fragment is built from a serialized query plan, not parsed, so it has no AST. doExecuteTask passed an empty ASTSelectQuery() to logQueryStart only to carry query_kind. An empty ASTSelectQuery has no SELECT expression list, so with log_formatted_queries=1 logQueryStart formats it and ASTSelectQuery::formatImpl line 104 does select()->format(...) = member call on a null IAST. UBSAN: "member call on null pointer of type 'DB::IAST'"; debug abort / release UB.
c Fix matches root cause? Yes. Drop the malformed empty stub and pass a null ASTPtr. The bug is the stub itself, not a missing guard; passing null removes it at the source.
d Test intent preserved / new tests added? New regression test 04327_distributed_plan_local_log_formatted_queries exercising the exact path. No existing test sets log_formatted_queries with the local distributed executor, so this path was uncovered.
e Both directions demonstrated? Yes. Unpatched binary aborts with the exact CIDB stack (ASTSelectQuery::formatImpl -> IAST::format -> formatWithSecretsOneLine -> logQueryStart -> doExecuteTask line 656). Patched binary: the new test passes 30/30 (randomization off) and 20/20 final; the manual repro returns 1000. (Some randomized runs fail only on Invalid time zone: zoneinfo/UTC, a local tzdata gap unrelated to this change.)
f Fix is general, not a narrow patch? All three stub consumers (logQueryStart, logQueryFinish, logQueryException via logQueryFinishImpl/incrementFailedQueryProfileEvents/addStatusInfoToQueryLogElement) are null-AST-safe and default query_kind to Select, so all three now use the null AST. Surveyed every other make_intrusive<ASTSelectQuery>() site: they all populate the SELECT expression before use; this was the unique site that handed an empty stub to the formatter.

Session id: cron:clickhouse-worker-slot-30:20260610-203300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @davenger @KochetovNicolai — could you review this? It fixes a null pointer dereference in the local distributed-plan executor: doExecuteTask handed an empty ASTSelectQuery() stub to logQueryStart, and with log_formatted_queries=1 formatting that stub dereferenced the absent SELECT list. The fix passes a null ASTPtr (the log helpers already default query_kind to Select for a null AST). Found by BuzzHouse (arm_asan_ubsan), STID 2791-2e42.

@nikitamikhaylov

Copy link
Copy Markdown
Member

@groeneai a link to the failure in CI, please. Remember this and do it all the time.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a3e77c7]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, azure, sequential) FAIL
Server died FAIL cidb
01921_concurrent_ttl_and_normal_merges_zookeeper_long UNKNOWN cidb
01915_create_or_replace_dictionary UNKNOWN cidb
AddressSanitizer (STID: 0883-4595) FAIL cidb
Integration tests (amd_llvm_coverage, 1/5) ERROR
LLVM Coverage ERROR

AI Review

Summary

This PR fixes the local distributed-plan task logging path by passing a null ASTPtr to the query-log helpers instead of a malformed empty ASTSelectQuery, and adds a focused stateless regression with log_formatted_queries = 1. The code change matches the described root cause; I found no code-level blockers.

PR Metadata
  • The Changelog category should be Critical Bug Fix (crash, data loss, RBAC), not Bug Fix (user-visible misbehavior in an official stable release), because the PR description and regression evidence describe a null pointer dereference that aborts the server.
  • The Changelog entry is required and is specific enough. Suggested category replacement:
- Critical Bug Fix (crash, data loss, RBAC)
Findings
  • 💡 Nits
    • [PR description] The changelog category should be Critical Bug Fix (crash, data loss, RBAC) for this server abort/null pointer dereference.
Final Verdict

Status: ✅ Approve

No code changes required; update the changelog category before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 10, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Closing as obsolete. This fix patched src/QueryPipeline/DistributedPlanExecutor.cpp, which #107122 (revert of #106020, "Multistage distributed queries") removed from master entirely on 2026-06-11. The empty ASTSelectQuery stub that caused the UBSAN null-deref (STID 2791-2e42) no longer exists, so the crash can't recur. If #106020 is re-landed, doExecuteTask should pass a null ASTPtr (not an unpopulated make_intrusive<ASTSelectQuery>()) to the query-log helpers, which already default query_kind to Select on a null AST.

session: cron:clickhouse-worker-slot-4:20260611-070900

@groeneai groeneai closed this Jun 11, 2026
alexey-milovidov pushed a commit that referenced this pull request Jun 16, 2026
A local distributed-plan task fragment is built from a serialized query
plan, not parsed, so it has no query AST. doExecuteTask passed an empty
ASTSelectQuery() to logQueryStart only to carry query_kind. An empty
ASTSelectQuery has no SELECT expression list, so with
log_formatted_queries = 1 the log path formatted it and
ASTSelectQuery::formatImpl dereferenced the absent SELECT
(select()->format()), a member call on a null IAST. The server aborts.

Fix: pass a null ASTPtr instead of the malformed stub. logQueryStart,
logQueryFinish and logQueryException already treat a null AST as
QueryKind::Select (the kind the stub was constructed to provide), so
behavior is unchanged for query_kind while the formatting crash is
avoided. Remove the now-dead include of Parsers/ASTSelectQuery.h.

Found by the BuzzHouse fuzzer (amd_msan), STID 2257-2710, on the
make_distributed_plan + distributed_plan_execute_locally path. The same
bug was fixed once before (PR #107079) but that fix was closed obsolete
when the feature was reverted, and the feature was then re-landed with
the buggy stub intact, so the fix is re-applied here.

Regression test 04327_distributed_plan_local_log_formatted_queries runs
the affected path; it aborts the server before this change and passes
after.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants