[clang] [analyzer] Don't keep a reclaimable node in TrackControlDependencyCondBRVisitor (PR #242711)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 14 13:59:13 PST 2026


https://github.com/mark-zilber created https://github.com/llvm/llvm-project/pull/242711

`trackExpressionValue()` may run when a checker emits a report, while the exploded graph is still being built. The `ControlDependencyHandler` then creates a `TrackControlDependencyCondBRVisitor`, which stored a pointer to an `ExplodedNode` of that graph. Node reclamation (`ExplodedGraph::reclaimRecentlyAllocatedNodes`) can free such a node before the report is processed and reuse its memory for a node of another function. The visitor then took the CFG block of the wrong function and hit in `ControlDependencyCalculator::getControlDependencies` and without assertions this is a use-after-free

```
GenericDomTree.h:400: Assertion `(!BB || Parent == NodeTrait::getParent(const_cast<NodeT *>(BB))) && "cannot get DomTreeNode of block with different parent"' failed.
```

The visitor only needs the stack frame and the CFG block of the origin node, so it now stores them when it is created. `ExplodedNode::getCFGBlock()` depends only on the node's own program point and stack frame, so the result is the same as before

Found while analyzing `llvm/lib/Target/X86/X86InterleavedAccess.cpp` with an extra checker enabled, which changed which nodes got reclaimed; the report there comes from `core.CallAndMessage`. With `-analyzer-config graph-trim-interval=0` the crash goes away

### Testing

- `clang/test/Analysis/track-control-dependency-reclaimed-node.cpp` crashes without the fix for each of its `graph-trim-interval` values (10, 15, 19). Whether the freed node is reused by a node of another function depends on the exploration order, so the test may stop reproducing the crash after unrelated engine changes


>From d54a215bfbcf7f65697d2308d9e74e2d5682efea Mon Sep 17 00:00:00 2001
From: Mark Zilber <markusbusiness69 at gmail.com>
Date: Sat, 14 Nov 2026 22:44:58 +0200
Subject: [PATCH] [analyzer] Don't keep a reclaimable node in
 TrackControlDependencyCondBRVisitor

trackExpressionValue() may run when a checker emits a report, while the
exploded graph is still being built. The ControlDependencyHandler then
creates a TrackControlDependencyCondBRVisitor that stored a pointer to
an ExplodedNode of that graph. Node reclamation can free such a node
before the report is processed and reuse its memory for a node of
another function. The visitor then queried a CFG block of the wrong
function and hit the "cannot get DomTreeNode of block with different
parent" assertion; without assertions this is a use-after-free.

Store the stack frame and the CFG block of the origin node when the
visitor is created, since these are the only things it needs from it
---
 .../Core/BugReporterVisitors.cpp              | 16 ++++++-----
 ...rack-control-dependency-reclaimed-node.cpp | 27 +++++++++++++++++++
 2 files changed, 36 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Analysis/track-control-dependency-reclaimed-node.cpp

diff --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
index 38d1cab8ff71e9..c9543965b80b94 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
@@ -1934,14 +1934,18 @@ namespace {
 /// trackExpressionValue().
 class TrackControlDependencyCondBRVisitor final
     : public TrackingBugReporterVisitor {
-  const ExplodedNode *Origin;
+  // The origin node itself may be reclaimed before the visitor runs.
+  const StackFrame *OriginFrame;
+  CFGBlock *OriginBlock;
   ControlDependencyCalculator ControlDeps;
   llvm::SmallPtrSet<const CFGBlock *, 32> VisitedBlocks;
 
 public:
   TrackControlDependencyCondBRVisitor(TrackerRef ParentTracker,
                                       const ExplodedNode *O)
-      : TrackingBugReporterVisitor(ParentTracker), Origin(O),
+      : TrackingBugReporterVisitor(ParentTracker),
+        OriginFrame(O->getStackFrame()),
+        OriginBlock(const_cast<CFGBlock *>(O->getCFGBlock())),
         ControlDeps(&O->getCFG()) {}
 
   void Profile(llvm::FoldingSetNodeID &ID) const override {
@@ -2011,7 +2015,7 @@ TrackControlDependencyCondBRVisitor::VisitNode(const ExplodedNode *N,
                                                BugReporterContext &BRC,
                                                PathSensitiveBugReport &BR) {
   // We can only reason about control dependencies within the same stack frame.
-  if (Origin->getStackFrame() != N->getStackFrame())
+  if (OriginFrame != N->getStackFrame())
     return nullptr;
 
   CFGBlock *NB = const_cast<CFGBlock *>(N->getCFGBlock());
@@ -2020,16 +2024,14 @@ TrackControlDependencyCondBRVisitor::VisitNode(const ExplodedNode *N,
   if (!VisitedBlocks.insert(NB).second)
     return nullptr;
 
-  CFGBlock *OriginB = const_cast<CFGBlock *>(Origin->getCFGBlock());
-
   // TODO: Cache CFGBlocks for each ExplodedNode.
-  if (!OriginB || !NB)
+  if (!OriginBlock || !NB)
     return nullptr;
 
   if (isAssertlikeBlock(NB, BRC.getASTContext()))
     return nullptr;
 
-  if (ControlDeps.isControlDependent(OriginB, NB)) {
+  if (ControlDeps.isControlDependent(OriginBlock, NB)) {
     // We don't really want to explain for range loops. Evidence suggests that
     // the only thing that leads to is the addition of calls to operator!=.
     if (llvm::isa_and_nonnull<CXXForRangeStmt>(NB->getTerminatorStmt()))
diff --git a/clang/test/Analysis/track-control-dependency-reclaimed-node.cpp b/clang/test/Analysis/track-control-dependency-reclaimed-node.cpp
new file mode 100644
index 00000000000000..6e737bd18baaa1
--- /dev/null
+++ b/clang/test/Analysis/track-control-dependency-reclaimed-node.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config graph-trim-interval=10 -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config graph-trim-interval=15 -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config graph-trim-interval=19 -verify %s
+
+// Condition tracking must not use a node reclaimed before the report is built.
+
+void use(int *);
+
+static unsigned elems(unsigned k) {
+  switch (k) {
+  case 0: return 2;
+  case 1: return 4;
+  default: return 16;
+  }
+}
+
+static void reorder(int **in, unsigned n, unsigned s) {
+  int *t[8];
+  for (unsigned i = 0; i < (n / 16) * s; i += 2)
+    t[i / 2] = in[i];
+  for (unsigned i = 0; i < s; i++)
+    use(t[2 * i]); // expected-warning{{1st function call argument is an uninitialized value}}
+}
+
+void top(int **in, unsigned k, unsigned s) {
+  reorder(in, elems(k) * 16, s);
+}



More information about the cfe-commits mailing list