1 //===- DWARFVerifier.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
11 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
15 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
17 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
18 #include "llvm/Support/raw_ostream.h"
24 using namespace dwarf;
25 using namespace object;
27 bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
28 uint32_t *Offset, unsigned UnitIndex,
29 uint8_t &UnitType, bool &isUnitDWARF64) {
30 uint32_t AbbrOffset, Length;
35 bool ValidLength = false;
36 bool ValidVersion = false;
37 bool ValidAddrSize = false;
38 bool ValidType = true;
39 bool ValidAbbrevOffset = true;
41 uint32_t OffsetStart = *Offset;
42 Length = DebugInfoData.getU32(Offset);
43 if (Length == UINT32_MAX) {
46 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
50 Version = DebugInfoData.getU16(Offset);
53 UnitType = DebugInfoData.getU8(Offset);
54 AddrSize = DebugInfoData.getU8(Offset);
55 AbbrOffset = DebugInfoData.getU32(Offset);
56 ValidType = DWARFUnit::isValidUnitType(UnitType);
59 AbbrOffset = DebugInfoData.getU32(Offset);
60 AddrSize = DebugInfoData.getU8(Offset);
63 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
64 ValidAbbrevOffset = false;
66 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
67 ValidVersion = DWARFContext::isSupportedVersion(Version);
68 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
69 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
72 OS << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart);
74 OS << "\tError: The length for this unit is too "
75 "large for the .debug_info provided.\n";
77 OS << "\tError: The 16 bit unit header version is not valid.\n";
79 OS << "\tError: The unit type encoding is not valid.\n";
80 if (!ValidAbbrevOffset)
81 OS << "\tError: The offset into the .debug_abbrev section is "
84 OS << "\tError: The address size is unsupported.\n";
86 *Offset = OffsetStart + Length + 4;
90 bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) {
91 uint32_t NumUnitErrors = 0;
92 unsigned NumDies = Unit.getNumDIEs();
93 for (unsigned I = 0; I < NumDies; ++I) {
94 auto Die = Unit.getDIEAtIndex(I);
95 if (Die.getTag() == DW_TAG_null)
97 for (auto AttrValue : Die.attributes()) {
98 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
99 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
102 return NumUnitErrors == 0;
105 bool DWARFVerifier::handleDebugInfo() {
106 OS << "Verifying .debug_info Unit Header Chain...\n";
108 DWARFDataExtractor DebugInfoData(DCtx.getInfoSection(), DCtx.isLittleEndian(),
110 uint32_t NumDebugInfoErrors = 0;
111 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
112 uint8_t UnitType = 0;
113 bool isUnitDWARF64 = false;
114 bool isHeaderChainValid = true;
115 bool hasDIE = DebugInfoData.isValidOffset(Offset);
117 OffsetStart = Offset;
118 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
120 isHeaderChainValid = false;
124 std::unique_ptr<DWARFUnit> Unit;
126 case dwarf::DW_UT_type:
127 case dwarf::DW_UT_split_type: {
128 DWARFUnitSection<DWARFTypeUnit> TUSection{};
129 Unit.reset(new DWARFTypeUnit(
130 DCtx, DCtx.getInfoSection(), DCtx.getDebugAbbrev(),
131 &DCtx.getRangeSection(), DCtx.getStringSection(),
132 DCtx.getStringOffsetSection(), &DCtx.getAppleObjCSection(),
133 DCtx.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
137 case dwarf::DW_UT_skeleton:
138 case dwarf::DW_UT_split_compile:
139 case dwarf::DW_UT_compile:
140 case dwarf::DW_UT_partial:
141 // UnitType = 0 means that we are
142 // verifying a compile unit in DWARF v4.
144 DWARFUnitSection<DWARFCompileUnit> CUSection{};
145 Unit.reset(new DWARFCompileUnit(
146 DCtx, DCtx.getInfoSection(), DCtx.getDebugAbbrev(),
147 &DCtx.getRangeSection(), DCtx.getStringSection(),
148 DCtx.getStringOffsetSection(), &DCtx.getAppleObjCSection(),
149 DCtx.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
153 default: { llvm_unreachable("Invalid UnitType."); }
155 Unit->extract(DebugInfoData, &OffsetStart);
156 if (!verifyUnitContents(*Unit))
157 ++NumDebugInfoErrors;
159 hasDIE = DebugInfoData.isValidOffset(Offset);
162 if (UnitIdx == 0 && !hasDIE) {
163 OS << "Warning: .debug_info is empty.\n";
164 isHeaderChainValid = true;
166 NumDebugInfoErrors += verifyDebugInfoReferences();
167 return (isHeaderChainValid && NumDebugInfoErrors == 0);
170 unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
171 DWARFAttribute &AttrValue) {
172 unsigned NumErrors = 0;
173 const auto Attr = AttrValue.Attr;
176 // Make sure the offset in the DW_AT_ranges attribute is valid.
177 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
178 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
180 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
187 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
192 case DW_AT_stmt_list:
193 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
194 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
195 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
197 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
199 << format("0x%08" PRIx32, *SectionOffset) << "\n";
205 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
217 unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
218 DWARFAttribute &AttrValue) {
219 unsigned NumErrors = 0;
220 const auto Form = AttrValue.Value.getForm();
226 case DW_FORM_ref_udata: {
227 // Verify all CU relative references are valid CU offsets.
228 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
231 auto DieCU = Die.getDwarfUnit();
232 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
233 auto CUOffset = AttrValue.Value.getRawUValue();
234 if (CUOffset >= CUSize) {
236 OS << "error: " << FormEncodingString(Form) << " CU offset "
237 << format("0x%08" PRIx32, CUOffset)
238 << " is invalid (must be less than CU size of "
239 << format("0x%08" PRIx32, CUSize) << "):\n";
243 // Valid reference, but we will verify it points to an actual
245 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
250 case DW_FORM_ref_addr: {
251 // Verify all absolute DIE references have valid offsets in the
252 // .debug_info section.
253 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
256 if (*RefVal >= DCtx.getInfoSection().Data.size()) {
258 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
263 // Valid reference, but we will verify it points to an actual
265 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
271 auto SecOffset = AttrValue.Value.getAsSectionOffset();
272 assert(SecOffset); // DW_FORM_strp is a section offset.
273 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
275 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
287 unsigned DWARFVerifier::verifyDebugInfoReferences() {
288 // Take all references and make sure they point to an actual DIE by
289 // getting the DIE by offset and emitting an error
290 OS << "Verifying .debug_info references...\n";
291 unsigned NumErrors = 0;
292 for (auto Pair : ReferenceToDIEOffsets) {
293 auto Die = DCtx.getDIEForOffset(Pair.first);
297 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
298 << ". Offset is in between DIEs:\n";
299 for (auto Offset : Pair.second) {
300 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
301 ReferencingDie.dump(OS, 0);
309 void DWARFVerifier::verifyDebugLineStmtOffsets() {
310 std::map<uint64_t, DWARFDie> StmtListToDie;
311 for (const auto &CU : DCtx.compile_units()) {
312 auto Die = CU->getUnitDIE();
313 // Get the attribute value as a section offset. No need to produce an
314 // error here if the encoding isn't correct because we validate this in
315 // the .debug_info verifier.
316 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
317 if (!StmtSectionOffset)
319 const uint32_t LineTableOffset = *StmtSectionOffset;
320 auto LineTable = DCtx.getLineTableForUnit(CU.get());
321 if (LineTableOffset < DCtx.getLineSection().Data.size()) {
323 ++NumDebugLineErrors;
324 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
325 << "] was not able to be parsed for CU:\n";
331 // Make sure we don't get a valid line table back if the offset is wrong.
332 assert(LineTable == nullptr);
333 // Skip this line table as it isn't valid. No need to create an error
334 // here because we validate this in the .debug_info verifier.
337 auto Iter = StmtListToDie.find(LineTableOffset);
338 if (Iter != StmtListToDie.end()) {
339 ++NumDebugLineErrors;
340 OS << "error: two compile unit DIEs, "
341 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
342 << format("0x%08" PRIx32, Die.getOffset())
343 << ", have the same DW_AT_stmt_list section offset:\n";
344 Iter->second.dump(OS, 0);
347 // Already verified this line table before, no need to do it again.
350 StmtListToDie[LineTableOffset] = Die;
354 void DWARFVerifier::verifyDebugLineRows() {
355 for (const auto &CU : DCtx.compile_units()) {
356 auto Die = CU->getUnitDIE();
357 auto LineTable = DCtx.getLineTableForUnit(CU.get());
358 // If there is no line table we will have created an error in the
359 // .debug_info verifier or in verifyDebugLineStmtOffsets().
362 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
363 uint64_t PrevAddress = 0;
364 uint32_t RowIndex = 0;
365 for (const auto &Row : LineTable->Rows) {
366 if (Row.Address < PrevAddress) {
367 ++NumDebugLineErrors;
368 OS << "error: .debug_line["
369 << format("0x%08" PRIx32,
370 *toSectionOffset(Die.find(DW_AT_stmt_list)))
371 << "] row[" << RowIndex
372 << "] decreases in address from previous row:\n";
374 DWARFDebugLine::Row::dumpTableHeader(OS);
376 LineTable->Rows[RowIndex - 1].dump(OS);
381 if (Row.File > MaxFileIndex) {
382 ++NumDebugLineErrors;
383 OS << "error: .debug_line["
384 << format("0x%08" PRIx32,
385 *toSectionOffset(Die.find(DW_AT_stmt_list)))
386 << "][" << RowIndex << "] has invalid file index " << Row.File
387 << " (valid values are [1," << MaxFileIndex << "]):\n";
388 DWARFDebugLine::Row::dumpTableHeader(OS);
395 PrevAddress = Row.Address;
401 bool DWARFVerifier::handleDebugLine() {
402 NumDebugLineErrors = 0;
403 OS << "Verifying .debug_line...\n";
404 verifyDebugLineStmtOffsets();
405 verifyDebugLineRows();
406 return NumDebugLineErrors == 0;
409 bool DWARFVerifier::handleAppleNames() {
410 NumAppleNamesErrors = 0;
412 DWARFDataExtractor AppleNamesSection(DCtx.getAppleNamesSection(),
413 DCtx.isLittleEndian(), 0);
414 DataExtractor StrData(DCtx.getStringSection(), DCtx.isLittleEndian(), 0);
415 DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData);
417 if (!AppleNames.extract()) {
421 OS << "Verifying .apple_names...\n";
423 // Verify that all buckets have a valid hash index or are empty.
424 uint32_t NumBuckets = AppleNames.getNumBuckets();
425 uint32_t NumHashes = AppleNames.getNumHashes();
427 uint32_t BucketsOffset =
428 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
429 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
430 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
432 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
433 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
434 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
435 OS << format("error: Bucket[%d] has invalid hash index: %u\n", BucketIdx,
437 ++NumAppleNamesErrors;
441 uint32_t NumAtoms = AppleNames.getAtomsDesc().size();
443 OS << "error: no atoms; failed to read HashData\n";
444 ++NumAppleNamesErrors;
448 if (!AppleNames.validateForms()) {
449 OS << "error: unsupported form; failed to read HashData\n";
450 ++NumAppleNamesErrors;
454 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
455 uint32_t HashOffset = HashesBase + 4 * HashIdx;
456 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
457 uint32_t Hash = AppleNamesSection.getU32(&HashOffset);
458 uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset);
459 if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset,
461 OS << format("error: Hash[%d] has invalid HashData offset: 0x%08x\n",
462 HashIdx, HashDataOffset);
463 ++NumAppleNamesErrors;
467 uint32_t StringOffset;
468 uint32_t StringCount = 0;
469 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
471 while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) {
472 const uint32_t NumHashDataObjects =
473 AppleNamesSection.getU32(&HashDataOffset);
474 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
476 DieOffset = AppleNames.readAtoms(HashDataOffset);
477 if (!DCtx.getDIEForOffset(DieOffset)) {
478 const uint32_t BucketIdx =
479 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
480 StringOffset = StrpOffset;
481 const char *Name = StrData.getCStr(&StringOffset);
486 "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x "
488 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
489 BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx,
492 ++NumAppleNamesErrors;
498 return NumAppleNamesErrors == 0;