Repository navigation
Fix heap-use-after-free in ExecutingGraph::updateNode on runtime node removal - #111017
alexey-milovidov merged 9 commits into
Conversation
… removal updateNode keeps a local work-list of Edge* (updated_edges), drained only after the processor work-list empties. When a runtime updatePipeline removes a node, removeNode destroys that node's own edge lists and removeAffectedEdges erases surviving nodes' edges pointing to removed nodes; any of those freed Edge* still sitting in updated_edges become dangling, so a later pop reads edge->to on freed memory (heap-use-after-free). Commit 1ef2990 scrubbed the persistent post_updated_*_ports lists for this hazard but missed the transient local work-list. Thread the ids of edges freed during updatePipeline out to updateNode and drop them from updated_edges by pointer identity (update_info.id == the Edge's own address, so no freed Edge is dereferenced). No behavior change when nothing is removed. Found by the AST fuzzer on arm_asan_ubsan. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-0:20260719-195400 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-0:20260719-195400 |
|
cc @davenger @vdimir — could you review this? It fixes a heap-use-after-free in |
|
Workflow [PR], commit [493a3ba] Summary: ✅
AI ReviewSummaryThis PR teaches PR Metadata
Findings❌ Blockers
Final VerdictChanges requested: the current patch fixes the local reproducer but leaves the multi-threaded runtime-removal use-after-free unresolved. |
…ne contract Addresses review feedback on PR ClickHouse#111017: IProcessor::updatePipeline requires to_remove to hold disconnected processors (IProcessor.h:250). The regression test now disconnects each fan-out output from the coordinator inputs before pushing the source into to_remove, mirroring the supported runtime-removal path (MergeTreeCommitOrderSequentialSource::updatePipeline), instead of relying on dangling port metadata. disconnect() only severs port linkage; removeNode still destroys the source node's graph Edge objects, so the freed edges still dangle in updateNode's work-list. The test therefore still reproduces the exact heap-use-after-free (STID 2837-40ba): without the scrub it crashes under ASAN (READ of size 8 in an 80-byte Edge region), with the scrub it drains cleanly. Full Processors.* suite passes. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — ee9d0b7CI fully finished (Finish Workflow = SUCCESS, 15:31 UTC; no jobs queued/in_progress/pending). This P0 executor heap-use-after-free fix touches only the pipeline executor (
Session id: cron:our-pr-ci-monitor:20260720-160000 |
|
Completing the It removes the source rather than repairing the divergence after the fact: a comment-only / settings-only Our #110030 and #110145 (recovery-repair approaches) were both closed in favor of #111029; it |
CI finish ledger — 5b2bc24Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The earlier partial fix #111528 (pin Session id: cron:our-pr-ci-monitor:20260729-103000 |
CI finish ledger - 2153690Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
All 27 Two notes for whoever picks the branch up next, neither of which is a CI failure: The new stateless test is numbered The no-repro verdict is worth a look on its own terms. This is a runtime node-removal use-after-free, Session id: cron:our-pr-ci-monitor:20260731-173000 |
| /// updatePipeline may have removed locally recorded edges. | ||
| if (!update_result.removed_edges.empty()) | ||
| { | ||
| auto removed_range = std::ranges::remove_if(updated_edges, [&](const void * edge) { return update_result.removed_edges.contains(edge); }); |
There was a problem hiding this comment.
This only scrubs updated_edges for the current updateNode frame. Another executor thread can already be parked at the same shared->unique nodes_mutex upgrade with its own local updated_edges: it releases read_lock, waits on nodes_mutex, then resumes with whatever Edge * it queued earlier. If this thread wins the race and removes one of those edges in the meantime, the waiter never sees update_result.removed_edges and still dereferences edge->to on the freed Edge after it re-locks.
So the single-threaded reproducer is fixed, but the same use-after-free is still reachable under multi-threaded runtime removals. This invalidation needs to be visible across all concurrent updateNode frames, not just the caller's local deque.
There was a problem hiding this comment.
Agreed on the mechanism, and I do not think it should be fixed here.
I checked the three things that decide it rather than assuming, at head 3628df1:
The gap is real. updated_edges is frame-local (ExecutingGraph.cpp:301; all 9 references in the .cpp and .h are inside updateNode, no member or static spelling), removed_edges is read only at the scrub on :489, and there is no shared edge-lifetime state on the graph to consult. A second frame parked at :479 between read_lock.unlock() on :475 and read_lock.lock() on :503 does keep its own queued Edge *, and it is reachable outside the unit test: MergeTreeCommitOrderSequentialSource::updatePipeline is the only override on master that returns a non-empty to_remove, and the new .sql runs 16 of those sources concurrently.
But it is pre-existing, not introduced. Diffing this file against master, the updated_edges declaration is unchanged, read_lock.unlock() and std::unique_lock lock(nodes_mutex) are unchanged, and the dereference auto & node = *edge->to is master line 311, which is the exact frame in the report this PR exists for. So the cross-frame window is reachable on master today; this patch only shrinks the reachable set by adding a scrub that did not exist before, and widens nothing.
And every observed failure is inside a single frame, which the scrub does cover. For STID 2837-40ba over 60 days CIDB has 4 rows, and in all 4 the freeing thread is the reading thread: #111572 T303/T303, #111099 T396/T396, #111189 T271/T271, #100535 T276/T276. All 4 carry removeNode and updatePipeline; none carries removeAffectedEdges, so the freed edges are the removed node's own list. That is exactly the accumulate-then-free case in one frame.
Making the invalidation visible to frames that are already parked needs shared edge-lifetime state on the graph plus revalidation after every lock re-acquisition. That is an executor-wide concurrency change with its own risk, so it is a separate PR rather than something to fold into a fix for the single-frame use-after-free that is actually reproducing. I am not pushing to this branch; the call is the assignee's.
One note on the scrub itself, since I looked: :489 compares the work-list pointer against update_info.id, which the Edge constructor sets to this, so it is pointer identity and never dereferences freed memory. removeNode contributes the removed node's own edges and removeAffectedEdges the surviving nodes' edges into it, so the union covers every freed edge.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 90/94 (95.74%) · Uncovered code |
CI finish ledger - 493a3baEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task No failures on this head. All 174 check-runs completed, Session id: cron:our-pr-ci-monitor:20260801-203000 |
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 in the pipeline executor that could crash the server when a query removes processors at runtime (for example
FINALwith lazy reads or external sort), found by the AST fuzzer.Description
ExecutingGraph::updateNodekeeps a local work-list ofEdge *(updated_edges), drained only after the processor work-list empties. When a runtimeupdatePipelineremoves a node,removeNodedestroys that node's own edge lists andremoveAffectedEdgeserases surviving nodes' edges that point to removed nodes. Any of those freedEdge *still queued inupdated_edgesbecome dangling, so a later pop readsedge->toon freed memory:Commit 1ef2990 already scrubbed the persistent
post_updated_*_portslists for exactly this hazard, but missed the transient local work-list held by the enclosingupdateNodeframe.Fix: thread the ids of edges freed during
updatePipeline(bothremoveNode's destroyed edge lists andremoveAffectedEdges's erased edges) out toupdateNode, and drop them fromupdated_edgesright afterupdatePipelinereturns, by pointer identity (update_info.idis theEdge's own address), so no freedEdgeis ever dereferenced. The scrub is a no-op when nothing was removed.Found by the AST fuzzer on
arm_asan_ubsan. Added a deterministic ASAN regression test (Processors.UpdatePipelineFanInRemovalNoUseAfterFree) that reproduces the crash without the fix and passes with it.CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100535&sha=0fb86507fb4817cb2418012cce69069f92e74972&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29