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

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 18 09:50:25 PDT 2025


================
@@ -851,6 +851,86 @@ 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 offset 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;
+    }
+
+    // Get the line table for this unit to validate bounds
+    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;
+    }
+
+    // Get the DW_AT_stmt_list offset from the compile unit DIE
+    DWARFDie CUDie = U->getUnitDIE();
+    auto StmtListOffset = toSectionOffset(CUDie.find(DW_AT_stmt_list));
+    if (!StmtListOffset) {
+      ReportError("DW_AT_LLVM_stmt_sequence without DW_AT_stmt_list",
+                  "DIE has DW_AT_LLVM_stmt_sequence but compile unit has no "
+                  "DW_AT_stmt_list");
+      break;
+    }
+
+    // Calculate the bounds of this specific line table
+    uint64_t LineTableStart = *StmtListOffset;
+    uint64_t PrologueLength = LineTable->Prologue.PrologueLength;
+    uint64_t TotalLength = LineTable->Prologue.TotalLength;
+    uint64_t LineTableEnd =
+        LineTableStart + TotalLength +
+        (LineTable->Prologue.getFormParams().Format == dwarf::DWARF64 ? 12 : 4);
+    uint64_t SequencesStart =
+        LineTableStart + PrologueLength +
+        (LineTable->Prologue.getFormParams().Format == dwarf::DWARF64 ? 12 : 4);
+
+    // Check if the offset is within the bounds of this specific line table
+    if (*SectionOffset < SequencesStart || *SectionOffset >= LineTableEnd) {
+      ReportError("DW_AT_LLVM_stmt_sequence offset out of line table bounds",
+                  "DW_AT_LLVM_stmt_sequence offset " +
+                      llvm::formatv("{0:x8}", *SectionOffset) +
+                      " is not within the line table bounds [" +
+                      llvm::formatv("{0:x8}", SequencesStart) + ", " +
+                      llvm::formatv("{0:x8}", LineTableEnd) + ")");
+      break;
+    }
+
+    // Check if the offset points to a valid sequence offset
+    bool ValidSequenceOffset = false;
+    // Check if the offset matches any of the sequence offset 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;
+    }
----------------
ellishg wrote:

```suggestion
    ValidSequenceOffset = it != LineTable->Sequences.end() && it->StmtSeqOffset == *SectionOffset;
```

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


More information about the llvm-commits mailing list