Skip to content

Commit

Permalink
Add line table verification to lldb-dwarfdump --verify
Browse files Browse the repository at this point in the history
This patch verifies the .debug_line:
- verify all addresses in a line table sequence have ascending addresses
- verify that all line table file indexes are valid

Unit tests added for both cases.

Differential Revision: https://reviews.llvm.org/D32765

llvm-svn: 301984
  • Loading branch information
Greg Clayton committed May 2, 2017
1 parent 4a01ffb commit 6707046
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 77 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLine.h
Expand Up @@ -104,7 +104,7 @@ class DWARFDebugLine {
void postAppend();
void reset(bool DefaultIsStmt);
void dump(raw_ostream &OS) const;

static void dumpTableHeader(raw_ostream &OS);
static bool orderByAddress(const Row &LHS, const Row &RHS) {
return LHS.Address < RHS.Address;
}
Expand Down
73 changes: 73 additions & 0 deletions llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
Expand Up @@ -445,6 +445,75 @@ class Verifier {
}
return Success;
}

bool HandleDebugLine() {
bool Success = true;
OS << "Verifying .debug_line...\n";
for (const auto &CU : DCtx.compile_units()) {
uint32_t LineTableOffset = 0;
auto StmtFormValue = CU->getUnitDIE().find(DW_AT_stmt_list);
if (!StmtFormValue) {
// No line table for this compile unit.
continue;
}
// Get the attribute value as a section offset. No need to produce an
// error here if the encoding isn't correct because we validate this in
// the .debug_info verifier.
if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
LineTableOffset = *StmtSectionOffset;
if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
// Make sure we don't get a valid line table back if the offset
// is wrong.
assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
// Skip this line table as it isn't valid. No need to create an error
// here because we validate this in the .debug_info verifier.
continue;
}
}
auto LineTable = DCtx.getLineTableForUnit(CU.get());
if (!LineTable) {
Success = false;
OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
<< "] was not able to be parsed for CU:\n";
CU->getUnitDIE().dump(OS, 0);
OS << '\n';
continue;
}
uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
uint64_t PrevAddress = 0;
uint32_t RowIndex = 0;
for (const auto &Row : LineTable->Rows) {
if (Row.Address < PrevAddress) {
Success = false;
OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
<< "] row[" << RowIndex
<< "] decreases in address from previous row:\n";

DWARFDebugLine::Row::dumpTableHeader(OS);
if (RowIndex > 0)
LineTable->Rows[RowIndex - 1].dump(OS);
Row.dump(OS);
OS << '\n';
}

if (Row.File > MaxFileIndex) {
Success = false;
OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
<< "][" << RowIndex << "] has invalid file index " << Row.File
<< " (valid values are [1," << MaxFileIndex << "]):\n";
DWARFDebugLine::Row::dumpTableHeader(OS);
Row.dump(OS);
OS << '\n';
}
if (Row.EndSequence)
PrevAddress = 0;
else
PrevAddress = Row.Address;
++RowIndex;
}
}
return Success;
}
};

} // anonymous namespace
Expand All @@ -456,6 +525,10 @@ bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
if (!verifier.HandleDebugInfo())
Success = false;
}
if (DumpType == DIDT_All || DumpType == DIDT_Line) {
if (!verifier.HandleDebugLine())
Success = false;
}
return Success;
}
const DWARFUnitIndex &DWARFContext::getCUIndex() {
Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
Expand Up @@ -287,6 +287,12 @@ void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
EpilogueBegin = false;
}

void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
OS << "Address Line Column File ISA Discriminator Flags\n"
<< "------------------ ------ ------ ------ --- ------------- "
"-------------\n";
}

void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
<< format(" %6u %3u %13u ", File, Isa, Discriminator)
Expand All @@ -313,9 +319,7 @@ void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
OS << '\n';

if (!Rows.empty()) {
OS << "Address Line Column File ISA Discriminator Flags\n"
<< "------------------ ------ ------ ------ --- ------------- "
"-------------\n";
Row::dumpTableHeader(OS);
for (const Row &R : Rows) {
R.dump(OS);
}
Expand Down

0 comments on commit 6707046

Please sign in to comment.