Skip to content

Conversation

@usx95
Copy link
Contributor

@usx95 usx95 commented Jul 25, 2025

Fix a crash in the lifetime safety dataflow analysis when handling null CFG blocks.

Added a null check for adjacent blocks in the dataflow analysis algorithm to prevent dereferencing null pointers. This occurs when processing CFG blocks with unreachable successors or predecessors.

Original crash: https://compiler-explorer.com/z/qfzfqG5vM

Fixes #150095

Copy link
Contributor Author

usx95 commented Jul 25, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@usx95 usx95 changed the title fix-pruned-edges [LifetimeSafety] Handle pruned-edges (null blocks) in dataflow Jul 25, 2025
@usx95 usx95 marked this pull request as ready for review July 25, 2025 18:14
@usx95 usx95 requested review from Xazax-hun and ymand July 25, 2025 18:14
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:analysis labels Jul 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2025

@llvm/pr-subscribers-clang-temporal-safety
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-analysis

Author: Utkarsh Saxena (usx95)

Changes

Fix a crash in the lifetime safety dataflow analysis when handling null CFG blocks.

Added a null check for adjacent blocks in the dataflow analysis algorithm to prevent dereferencing null pointers. This occurs when processing CFG blocks with unreachable successors or predecessors.

Original crash: https://compiler-explorer.com/z/qfzfqG5vM

Fixes #150095


Full diff: https://github.com/llvm/llvm-project/pull/150670.diff

2 Files Affected:

  • (modified) clang/lib/Analysis/LifetimeSafety.cpp (+2)
  • (modified) clang/unittests/Analysis/LifetimeSafetyTest.cpp (+19)
diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp
index 94b8197bbf6f3..f39998cca56fe 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -603,6 +603,8 @@ class DataflowAnalysis {
       OutStates[B] = StateOut;
       Visited.set(B->getBlockID());
       for (const CFGBlock *AdjacentB : isForward() ? B->succs() : B->preds()) {
+        if (!AdjacentB)
+          continue;
         Lattice OldInState = getInState(AdjacentB);
         Lattice NewInState = D.join(OldInState, StateOut);
         // Enqueue the adjacent block if its in-state has changed or if we have
diff --git a/clang/unittests/Analysis/LifetimeSafetyTest.cpp b/clang/unittests/Analysis/LifetimeSafetyTest.cpp
index a48dc45e4f806..7cd679e184f6c 100644
--- a/clang/unittests/Analysis/LifetimeSafetyTest.cpp
+++ b/clang/unittests/Analysis/LifetimeSafetyTest.cpp
@@ -512,6 +512,25 @@ TEST_F(LifetimeAnalysisTest, PointersAndExpirationInACycle) {
   EXPECT_THAT(LoansTo({"temp"}), AreExpiredAt("after_loop"));
 }
 
+TEST_F(LifetimeAnalysisTest, InfiniteLoopPrunesEdges) {
+  SetupTest(R"(
+    void target(MyObj out) {
+      MyObj *p = &out;
+      POINT(before_loop);
+
+      for (;;) {
+        POINT(begin);
+        MyObj in;
+        p = ∈
+        POINT(end);
+      }
+    }
+  )");
+  EXPECT_THAT(Origin("p"), HasLoansTo({"out"}, "before_loop"));
+  EXPECT_THAT(Origin("p"), HasLoansTo({"in", "out"}, "begin"));
+  EXPECT_THAT(Origin("p"), HasLoansTo({"in"}, "end"));
+}
+
 TEST_F(LifetimeAnalysisTest, NestedScopes) {
   SetupTest(R"(
     void target() {

Copy link
Contributor Author

usx95 commented Aug 3, 2025

Merge activity

  • Aug 3, 7:43 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Aug 3, 7:45 PM UTC: @usx95 merged this pull request with Graphite.

@usx95 usx95 merged commit f9be391 into main Aug 3, 2025
14 checks passed
@usx95 usx95 deleted the users/usx95/07-25-fix-pruned-edges branch August 3, 2025 19:45
@usx95 usx95 added the clang:temporal-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) label Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang:temporal-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) clang Clang issues not falling into any other category

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Lifetime Safety] Analysis crashes on an infinite for loop

4 participants