Repository navigation
Fix use-after-free in a correlated subquery over a Merge table inside EXPLAIN or a subquery - #122475
Conversation
… EXPLAIN or a subquery A correlated subquery is planned with its own `GlobalPlannerContext`, and `collectTableExpressionDataForCorrelatedColumns` stores raw pointers to the enclosing query's `TableExpressionData` in it, without keeping the enclosing `PlannerContext` alive. Every context derived from the subquery falls back to that map when it looks up the correlated column's source. `ReadFromMerge` plans its source tables lazily (from `applyFilters` during `QueryPlan::optimize`, or from `initializePipeline`). For a non-empty source table narrower than the `Merge` header, `convertAndFilterSourceStream` builds the fan-out added in ClickHouse#119561 and names the correlated column through that map. `EXPLAIN`, a subquery in `FROM`, a view and an `IN` subquery destroy the enclosing `Planner` before that point, so the lookup reads freed memory (AST fuzzer, arm_asan_ubsan, STID 4877-28b1). `GlobalPlannerContext` now holds a `PlannerContextPtr` to every planner context whose data it points into. It is the only writer of the map, so this covers all readers and every place that drops the enclosing planner early, without changing each of them. No reference cycle: the subquery's plan is united into the enclosing `QueryPlan`, never stored in the enclosing `PlannerContext` (correlated subqueries are neither `IN` set sources nor materialized CTEs). Related: ClickHouse#115238 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
A view is planned by its own interpreter in `StorageView::readImpl`, which destroys the enclosing `Planner` before `ReadFromMerge` plans its source tables, like `EXPLAIN` and the `FROM` and `IN` subqueries the test already covers. CI report of the heap-use-after-free fixed in the previous commit: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=115238&sha=28023e4bdf69d407d579274765a2573ac6baa9db&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29 https://github.com/ClickHouse/ClickHouse/actions/runs/35807917921/job/107176161332 Related: ClickHouse#115238 (comment) 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; 1 finding in round 1, none in round 2 on the final head) and my own cold review in both rounds.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260926-204900 |
|
Workflow [PR], commit [526b0ee] Summary: ✅
AI ReviewSummaryThis PR fixes the correlated-subquery lifetime bug by making the subquery Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 526b0ee.
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units52 translation units recompiled, 460 s compile time in total, 52 of them have a recent master baseline. |
… over a `Merge` table inside `EXPLAIN` or a subquery
Backport #122475 to 26.9: Fix use-after-free in a correlated subquery over a `Merge` table inside `EXPLAIN` or a subquery
Related: #115238 (comment)
Related: #119561
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a heap-use-after-free when a correlated subquery reading a
Mergetable was planned insideEXPLAIN, a subquery inFROM, a view or anINsubquery.Description
On 26.9 (v26.9.3.38-stable), a correlated subquery over a
Mergetable reads freed planner memory (heap-use-after-free) when it is planned insideEXPLAIN, a subquery inFROM, a view or anINsubquery, and a non-empty source table of theMergetable lacks a column the subquery reads.Found by
AST fuzzer (arm_asan_ubsan)on #115238, STID 4877-28b1: https://github.com/ClickHouse/ClickHouse/actions/runs/35807917921/job/107176161332 (report).A correlated subquery is planned with its own
GlobalPlannerContext, which stores raw pointers to the enclosing query'sTableExpressionDatabut does not own the enclosingPlannerContext.ReadFromMergeplans its source tables lazily, duringQueryPlan::optimizeor pipeline building, and for a source table narrower than theMergeheader the fan-out added in #119561 resolves the correlated column through those pointers.EXPLAIN, aFROMsubquery, a view and anINsubquery destroy the enclosingPlannerbefore that point, so the pointers dangle.The fix makes
GlobalPlannerContexthold aPlannerContextPtrto each context it points into. Those pointers are written in one place, so this one change covers every reader and every path that drops the enclosing planner early. There is no reference cycle: the subquery's plan goes into the enclosingQueryPlan, never into the enclosingPlannerContext, and LeakSanitizer reports nothing at exit.Each of the six queries in the new test aborts an unfixed ASan build on its own and passes with the fix, including 50 randomized runs, the parallel replicas settings and a
Replicateddatabase.Workflow [PR]
Sync PR [sync-upstream/pr/122475]
Version info
26.10.1.792-master(included in26.10and later)26.9.4.3