[llvm] [DWARFVerifier] Verify that DW_AT_LLVM_stmt_sequence is set correctly (PR #152807)

Peter Rong via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 8 15:23:09 PDT 2025


https://github.com/DataCorrupted created https://github.com/llvm/llvm-project/pull/152807

None

>From cedce2128dc872a2f1024c9907fd78bdee4b7fe7 Mon Sep 17 00:00:00 2001
From: Peter Rong <PeterRong at meta.com>
Date: Fri, 8 Aug 2025 15:22:40 -0700
Subject: [PATCH] [DWARFVerifier] Verify that DW_AT_LLVM_stmt_sequence is set
 correctly

Signed-off-by: Peter Rong <PeterRong at meta.com>
---
 llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp | 46 ++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
index 8ec3f1729b974..7d6a643560643 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
@@ -851,6 +851,52 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
     }
     break;
   }
+  case DW_AT_LLVM_stmt_sequence: {
+    // Make sure the offset in the DW_AT_LLVM_stmt_sequence attribute is valid
+    // and points to a valid sequence start in the line table.
+    auto SectionOffset = AttrValue.Value.getAsSectionOffset();
+    if (!SectionOffset) {
+      ReportError("Invalid DW_AT_LLVM_stmt_sequence encoding",
+                  "DIE has invalid DW_AT_LLVM_stmt_sequence encoding:");
+      break;
+    }
+    if (*SectionOffset >= U->getLineSection().Data.size()) {
+      ReportError(
+          "DW_AT_LLVM_stmt_sequence offset out of bounds",
+          "DW_AT_LLVM_stmt_sequence offset is beyond .debug_line bounds: " +
+              llvm::formatv("{0:x8}", *SectionOffset));
+      break;
+    }
+
+    // Check if the offset points to a valid sequence start
+    const auto *LineTable = DCtx.getLineTableForUnit(U);
+    if (!LineTable) {
+      ReportError("DW_AT_LLVM_stmt_sequence without line table",
+                  "DIE has DW_AT_LLVM_stmt_sequence but compile unit has no "
+                  "line table");
+      break;
+    }
+    bool ValidSequenceOffset = false;
+    // Check if the offset matches any of the sequence start offsets using
+    // binary search
+    auto it = std::lower_bound(LineTable->Sequences.begin(),
+                               LineTable->Sequences.end(), *SectionOffset,
+                               [](const auto &Sequence, const uint64_t Offset) {
+                                 return Sequence.StmtSeqOffset < Offset;
+                               });
+    if (it != LineTable->Sequences.end() &&
+        it->StmtSeqOffset == *SectionOffset) {
+      ValidSequenceOffset = true;
+    }
+
+    if (!ValidSequenceOffset)
+      ReportError(
+          "Invalid DW_AT_LLVM_stmt_sequence offset",
+          "DW_AT_LLVM_stmt_sequence offset " +
+              llvm::formatv("{0:x8}", *SectionOffset) +
+              " does not point to a valid sequence start in the line table");
+    break;
+  }
   default:
     break;
   }



More information about the llvm-commits mailing list