Skip to content

Commit cedce21

Browse files
committed
[DWARFVerifier] Verify that DW_AT_LLVM_stmt_sequence is set correctly
Signed-off-by: Peter Rong <[email protected]>
1 parent 37bcd93 commit cedce21

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,52 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
851851
}
852852
break;
853853
}
854+
case DW_AT_LLVM_stmt_sequence: {
855+
// Make sure the offset in the DW_AT_LLVM_stmt_sequence attribute is valid
856+
// and points to a valid sequence start in the line table.
857+
auto SectionOffset = AttrValue.Value.getAsSectionOffset();
858+
if (!SectionOffset) {
859+
ReportError("Invalid DW_AT_LLVM_stmt_sequence encoding",
860+
"DIE has invalid DW_AT_LLVM_stmt_sequence encoding:");
861+
break;
862+
}
863+
if (*SectionOffset >= U->getLineSection().Data.size()) {
864+
ReportError(
865+
"DW_AT_LLVM_stmt_sequence offset out of bounds",
866+
"DW_AT_LLVM_stmt_sequence offset is beyond .debug_line bounds: " +
867+
llvm::formatv("{0:x8}", *SectionOffset));
868+
break;
869+
}
870+
871+
// Check if the offset points to a valid sequence start
872+
const auto *LineTable = DCtx.getLineTableForUnit(U);
873+
if (!LineTable) {
874+
ReportError("DW_AT_LLVM_stmt_sequence without line table",
875+
"DIE has DW_AT_LLVM_stmt_sequence but compile unit has no "
876+
"line table");
877+
break;
878+
}
879+
bool ValidSequenceOffset = false;
880+
// Check if the offset matches any of the sequence start offsets using
881+
// binary search
882+
auto it = std::lower_bound(LineTable->Sequences.begin(),
883+
LineTable->Sequences.end(), *SectionOffset,
884+
[](const auto &Sequence, const uint64_t Offset) {
885+
return Sequence.StmtSeqOffset < Offset;
886+
});
887+
if (it != LineTable->Sequences.end() &&
888+
it->StmtSeqOffset == *SectionOffset) {
889+
ValidSequenceOffset = true;
890+
}
891+
892+
if (!ValidSequenceOffset)
893+
ReportError(
894+
"Invalid DW_AT_LLVM_stmt_sequence offset",
895+
"DW_AT_LLVM_stmt_sequence offset " +
896+
llvm::formatv("{0:x8}", *SectionOffset) +
897+
" does not point to a valid sequence start in the line table");
898+
break;
899+
}
854900
default:
855901
break;
856902
}

0 commit comments

Comments
 (0)