LLVM  7.0.0
DWARFVerifier.cpp
Go to the documentation of this file.
1 //===- DWARFVerifier.cpp --------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/Support/DJB.h"
21 #include "llvm/Support/WithColor.h"
23 #include <map>
24 #include <set>
25 #include <vector>
26 
27 using namespace llvm;
28 using namespace dwarf;
29 using namespace object;
30 
33  auto Begin = Ranges.begin();
34  auto End = Ranges.end();
35  auto Pos = std::lower_bound(Begin, End, R);
36 
37  if (Pos != End) {
38  if (Pos->intersects(R))
39  return Pos;
40  if (Pos != Begin) {
41  auto Iter = Pos - 1;
42  if (Iter->intersects(R))
43  return Iter;
44  }
45  }
46 
47  Ranges.insert(Pos, R);
48  return Ranges.end();
49 }
50 
53  auto End = Children.end();
54  auto Iter = Children.begin();
55  while (Iter != End) {
56  if (Iter->intersects(RI))
57  return Iter;
58  ++Iter;
59  }
60  Children.insert(RI);
61  return Children.end();
62 }
63 
65  // Both list of ranges are sorted so we can make this fast.
66 
67  if (Ranges.empty() || RHS.Ranges.empty())
68  return false;
69 
70  // Since the ranges are sorted we can advance where we start searching with
71  // this object's ranges as we traverse RHS.Ranges.
72  auto End = Ranges.end();
73  auto Iter = findRange(RHS.Ranges.front());
74 
75  // Now linearly walk the ranges in this object and see if they contain each
76  // ranges from RHS.Ranges.
77  for (const auto &R : RHS.Ranges) {
78  while (Iter != End) {
79  if (Iter->contains(R))
80  break;
81  ++Iter;
82  }
83  if (Iter == End)
84  return false;
85  }
86  return true;
87 }
88 
90  if (Ranges.empty() || RHS.Ranges.empty())
91  return false;
92 
93  auto End = Ranges.end();
94  auto Iter = findRange(RHS.Ranges.front());
95  for (const auto &R : RHS.Ranges) {
96  if(Iter == End)
97  return false;
98  if (R.HighPC <= Iter->LowPC)
99  continue;
100  while (Iter != End) {
101  if (Iter->intersects(R))
102  return true;
103  ++Iter;
104  }
105  }
106 
107  return false;
108 }
109 
110 bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
111  uint32_t *Offset, unsigned UnitIndex,
112  uint8_t &UnitType, bool &isUnitDWARF64) {
113  uint32_t AbbrOffset, Length;
114  uint8_t AddrSize = 0;
115  uint16_t Version;
116  bool Success = true;
117 
118  bool ValidLength = false;
119  bool ValidVersion = false;
120  bool ValidAddrSize = false;
121  bool ValidType = true;
122  bool ValidAbbrevOffset = true;
123 
124  uint32_t OffsetStart = *Offset;
125  Length = DebugInfoData.getU32(Offset);
126  if (Length == UINT32_MAX) {
127  isUnitDWARF64 = true;
128  OS << format(
129  "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
130  UnitIndex);
131  return false;
132  }
133  Version = DebugInfoData.getU16(Offset);
134 
135  if (Version >= 5) {
136  UnitType = DebugInfoData.getU8(Offset);
137  AddrSize = DebugInfoData.getU8(Offset);
138  AbbrOffset = DebugInfoData.getU32(Offset);
139  ValidType = dwarf::isUnitType(UnitType);
140  } else {
141  UnitType = 0;
142  AbbrOffset = DebugInfoData.getU32(Offset);
143  AddrSize = DebugInfoData.getU8(Offset);
144  }
145 
146  if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
147  ValidAbbrevOffset = false;
148 
149  ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
150  ValidVersion = DWARFContext::isSupportedVersion(Version);
151  ValidAddrSize = AddrSize == 4 || AddrSize == 8;
152  if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
153  !ValidType) {
154  Success = false;
155  error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex,
156  OffsetStart);
157  if (!ValidLength)
158  note() << "The length for this unit is too "
159  "large for the .debug_info provided.\n";
160  if (!ValidVersion)
161  note() << "The 16 bit unit header version is not valid.\n";
162  if (!ValidType)
163  note() << "The unit type encoding is not valid.\n";
164  if (!ValidAbbrevOffset)
165  note() << "The offset into the .debug_abbrev section is "
166  "not valid.\n";
167  if (!ValidAddrSize)
168  note() << "The address size is unsupported.\n";
169  }
170  *Offset = OffsetStart + Length + 4;
171  return Success;
172 }
173 
174 bool DWARFVerifier::verifyUnitContents(DWARFUnit &Unit, uint8_t UnitType) {
175  uint32_t NumUnitErrors = 0;
176  unsigned NumDies = Unit.getNumDIEs();
177  for (unsigned I = 0; I < NumDies; ++I) {
178  auto Die = Unit.getDIEAtIndex(I);
179  if (Die.getTag() == DW_TAG_null)
180  continue;
181  for (auto AttrValue : Die.attributes()) {
182  NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
183  NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
184  }
185  }
186 
187  DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
188  if (!Die) {
189  error() << "Compilation unit without DIE.\n";
190  NumUnitErrors++;
191  return NumUnitErrors == 0;
192  }
193 
194  if (!dwarf::isUnitType(Die.getTag())) {
195  error() << "Compilation unit root DIE is not a unit DIE: "
196  << dwarf::TagString(Die.getTag()) << ".\n";
197  NumUnitErrors++;
198  }
199 
200  if (UnitType != 0 &&
201  !DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Die.getTag())) {
202  error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType)
203  << ") and root DIE (" << dwarf::TagString(Die.getTag())
204  << ") do not match.\n";
205  NumUnitErrors++;
206  }
207 
208  DieRangeInfo RI;
209  NumUnitErrors += verifyDieRanges(Die, RI);
210 
211  return NumUnitErrors == 0;
212 }
213 
214 unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
215  unsigned NumErrors = 0;
216  if (Abbrev) {
217  const DWARFAbbreviationDeclarationSet *AbbrDecls =
219  for (auto AbbrDecl : *AbbrDecls) {
221  for (auto Attribute : AbbrDecl.attributes()) {
222  auto Result = AttributeSet.insert(Attribute.Attr);
223  if (!Result.second) {
224  error() << "Abbreviation declaration contains multiple "
225  << AttributeString(Attribute.Attr) << " attributes.\n";
226  AbbrDecl.dump(OS);
227  ++NumErrors;
228  }
229  }
230  }
231  }
232  return NumErrors;
233 }
234 
236  OS << "Verifying .debug_abbrev...\n";
237 
238  const DWARFObject &DObj = DCtx.getDWARFObj();
239  bool noDebugAbbrev = DObj.getAbbrevSection().empty();
240  bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
241 
242  if (noDebugAbbrev && noDebugAbbrevDWO) {
243  return true;
244  }
245 
246  unsigned NumErrors = 0;
247  if (!noDebugAbbrev)
248  NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
249 
250  if (!noDebugAbbrevDWO)
251  NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
252  return NumErrors == 0;
253 }
254 
256  OS << "Verifying .debug_info Unit Header Chain...\n";
257 
258  const DWARFObject &DObj = DCtx.getDWARFObj();
259  DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
260  DCtx.isLittleEndian(), 0);
261  uint32_t NumDebugInfoErrors = 0;
262  uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
263  uint8_t UnitType = 0;
264  bool isUnitDWARF64 = false;
265  bool isHeaderChainValid = true;
266  bool hasDIE = DebugInfoData.isValidOffset(Offset);
269  while (hasDIE) {
270  OffsetStart = Offset;
271  if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
272  isUnitDWARF64)) {
273  isHeaderChainValid = false;
274  if (isUnitDWARF64)
275  break;
276  } else {
277  DWARFUnitHeader Header;
278  Header.extract(DCtx, DebugInfoData, &OffsetStart);
279  std::unique_ptr<DWARFUnit> Unit;
280  switch (UnitType) {
281  case dwarf::DW_UT_type:
282  case dwarf::DW_UT_split_type: {
283  Unit.reset(new DWARFTypeUnit(
284  DCtx, DObj.getInfoSection(), Header, DCtx.getDebugAbbrev(),
285  &DObj.getRangeSection(), DObj.getStringSection(),
287  DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection));
288  break;
289  }
290  case dwarf::DW_UT_skeleton:
291  case dwarf::DW_UT_split_compile:
292  case dwarf::DW_UT_compile:
293  case dwarf::DW_UT_partial:
294  // UnitType = 0 means that we are
295  // verifying a compile unit in DWARF v4.
296  case 0: {
297  Unit.reset(new DWARFCompileUnit(
298  DCtx, DObj.getInfoSection(), Header, DCtx.getDebugAbbrev(),
299  &DObj.getRangeSection(), DObj.getStringSection(),
301  DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection));
302  break;
303  }
304  default: { llvm_unreachable("Invalid UnitType."); }
305  }
306  if (!verifyUnitContents(*Unit, UnitType))
307  ++NumDebugInfoErrors;
308  }
309  hasDIE = DebugInfoData.isValidOffset(Offset);
310  ++UnitIdx;
311  }
312  if (UnitIdx == 0 && !hasDIE) {
313  warn() << ".debug_info is empty.\n";
314  isHeaderChainValid = true;
315  }
316  NumDebugInfoErrors += verifyDebugInfoReferences();
317  return (isHeaderChainValid && NumDebugInfoErrors == 0);
318 }
319 
320 unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
321  DieRangeInfo &ParentRI) {
322  unsigned NumErrors = 0;
323 
324  if (!Die.isValid())
325  return NumErrors;
326 
327  auto RangesOrError = Die.getAddressRanges();
328  if (!RangesOrError) {
329  // FIXME: Report the error.
330  ++NumErrors;
331  llvm::consumeError(RangesOrError.takeError());
332  return NumErrors;
333  }
334 
335  DWARFAddressRangesVector Ranges = RangesOrError.get();
336  // Build RI for this DIE and check that ranges within this DIE do not
337  // overlap.
338  DieRangeInfo RI(Die);
339  for (auto Range : Ranges) {
340  if (!Range.valid()) {
341  ++NumErrors;
342  error() << "Invalid address range " << Range << "\n";
343  continue;
344  }
345 
346  // Verify that ranges don't intersect.
347  const auto IntersectingRange = RI.insert(Range);
348  if (IntersectingRange != RI.Ranges.end()) {
349  ++NumErrors;
350  error() << "DIE has overlapping address ranges: " << Range << " and "
351  << *IntersectingRange << "\n";
352  break;
353  }
354  }
355 
356  // Verify that children don't intersect.
357  const auto IntersectingChild = ParentRI.insert(RI);
358  if (IntersectingChild != ParentRI.Children.end()) {
359  ++NumErrors;
360  error() << "DIEs have overlapping address ranges:";
361  Die.dump(OS, 0);
362  IntersectingChild->Die.dump(OS, 0);
363  OS << "\n";
364  }
365 
366  // Verify that ranges are contained within their parent.
367  bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
368  !(Die.getTag() == DW_TAG_subprogram &&
369  ParentRI.Die.getTag() == DW_TAG_subprogram);
370  if (ShouldBeContained && !ParentRI.contains(RI)) {
371  ++NumErrors;
372  error() << "DIE address ranges are not contained in its parent's ranges:";
373  ParentRI.Die.dump(OS, 0);
374  Die.dump(OS, 2);
375  OS << "\n";
376  }
377 
378  // Recursively check children.
379  for (DWARFDie Child : Die)
380  NumErrors += verifyDieRanges(Child, RI);
381 
382  return NumErrors;
383 }
384 
385 unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
386  DWARFAttribute &AttrValue) {
387  unsigned NumErrors = 0;
388  auto ReportError = [&](const Twine &TitleMsg) {
389  ++NumErrors;
390  error() << TitleMsg << '\n';
391  Die.dump(OS, 0, DumpOpts);
392  OS << "\n";
393  };
394 
395  const DWARFObject &DObj = DCtx.getDWARFObj();
396  const auto Attr = AttrValue.Attr;
397  switch (Attr) {
398  case DW_AT_ranges:
399  // Make sure the offset in the DW_AT_ranges attribute is valid.
400  if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
401  if (*SectionOffset >= DObj.getRangeSection().Data.size())
402  ReportError("DW_AT_ranges offset is beyond .debug_ranges bounds:");
403  break;
404  }
405  ReportError("DIE has invalid DW_AT_ranges encoding:");
406  break;
407  case DW_AT_stmt_list:
408  // Make sure the offset in the DW_AT_stmt_list attribute is valid.
409  if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
410  if (*SectionOffset >= DObj.getLineSection().Data.size())
411  ReportError("DW_AT_stmt_list offset is beyond .debug_line bounds: " +
412  llvm::formatv("{0:x8}", *SectionOffset));
413  break;
414  }
415  ReportError("DIE has invalid DW_AT_stmt_list encoding:");
416  break;
417  case DW_AT_location: {
418  auto VerifyLocationExpr = [&](StringRef D) {
419  DWARFUnit *U = Die.getDwarfUnit();
420  DataExtractor Data(D, DCtx.isLittleEndian(), 0);
421  DWARFExpression Expression(Data, U->getVersion(),
422  U->getAddressByteSize());
423  bool Error = llvm::any_of(Expression, [](DWARFExpression::Operation &Op) {
424  return Op.isError();
425  });
426  if (Error)
427  ReportError("DIE contains invalid DWARF expression:");
428  };
429  if (Optional<ArrayRef<uint8_t>> Expr = AttrValue.Value.getAsBlock()) {
430  // Verify inlined location.
431  VerifyLocationExpr(llvm::toStringRef(*Expr));
432  } else if (auto LocOffset = AttrValue.Value.getAsSectionOffset()) {
433  // Verify location list.
434  if (auto DebugLoc = DCtx.getDebugLoc())
435  if (auto LocList = DebugLoc->getLocationListAtOffset(*LocOffset))
436  for (const auto &Entry : LocList->Entries)
437  VerifyLocationExpr({Entry.Loc.data(), Entry.Loc.size()});
438  }
439  break;
440  }
441 
442  default:
443  break;
444  }
445  return NumErrors;
446 }
447 
448 unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
449  DWARFAttribute &AttrValue) {
450  const DWARFObject &DObj = DCtx.getDWARFObj();
451  unsigned NumErrors = 0;
452  const auto Form = AttrValue.Value.getForm();
453  switch (Form) {
454  case DW_FORM_ref1:
455  case DW_FORM_ref2:
456  case DW_FORM_ref4:
457  case DW_FORM_ref8:
458  case DW_FORM_ref_udata: {
459  // Verify all CU relative references are valid CU offsets.
460  Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
461  assert(RefVal);
462  if (RefVal) {
463  auto DieCU = Die.getDwarfUnit();
464  auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
465  auto CUOffset = AttrValue.Value.getRawUValue();
466  if (CUOffset >= CUSize) {
467  ++NumErrors;
468  error() << FormEncodingString(Form) << " CU offset "
469  << format("0x%08" PRIx64, CUOffset)
470  << " is invalid (must be less than CU size of "
471  << format("0x%08" PRIx32, CUSize) << "):\n";
472  Die.dump(OS, 0, DumpOpts);
473  OS << "\n";
474  } else {
475  // Valid reference, but we will verify it points to an actual
476  // DIE later.
477  ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
478  }
479  }
480  break;
481  }
482  case DW_FORM_ref_addr: {
483  // Verify all absolute DIE references have valid offsets in the
484  // .debug_info section.
485  Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
486  assert(RefVal);
487  if (RefVal) {
488  if (*RefVal >= DObj.getInfoSection().Data.size()) {
489  ++NumErrors;
490  error() << "DW_FORM_ref_addr offset beyond .debug_info "
491  "bounds:\n";
492  Die.dump(OS, 0, DumpOpts);
493  OS << "\n";
494  } else {
495  // Valid reference, but we will verify it points to an actual
496  // DIE later.
497  ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
498  }
499  }
500  break;
501  }
502  case DW_FORM_strp: {
503  auto SecOffset = AttrValue.Value.getAsSectionOffset();
504  assert(SecOffset); // DW_FORM_strp is a section offset.
505  if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
506  ++NumErrors;
507  error() << "DW_FORM_strp offset beyond .debug_str bounds:\n";
508  Die.dump(OS, 0, DumpOpts);
509  OS << "\n";
510  }
511  break;
512  }
513  default:
514  break;
515  }
516  return NumErrors;
517 }
518 
519 unsigned DWARFVerifier::verifyDebugInfoReferences() {
520  // Take all references and make sure they point to an actual DIE by
521  // getting the DIE by offset and emitting an error
522  OS << "Verifying .debug_info references...\n";
523  unsigned NumErrors = 0;
524  for (auto Pair : ReferenceToDIEOffsets) {
525  auto Die = DCtx.getDIEForOffset(Pair.first);
526  if (Die)
527  continue;
528  ++NumErrors;
529  error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
530  << ". Offset is in between DIEs:\n";
531  for (auto Offset : Pair.second) {
532  auto ReferencingDie = DCtx.getDIEForOffset(Offset);
533  ReferencingDie.dump(OS, 0, DumpOpts);
534  OS << "\n";
535  }
536  OS << "\n";
537  }
538  return NumErrors;
539 }
540 
541 void DWARFVerifier::verifyDebugLineStmtOffsets() {
542  std::map<uint64_t, DWARFDie> StmtListToDie;
543  for (const auto &CU : DCtx.compile_units()) {
544  auto Die = CU->getUnitDIE();
545  // Get the attribute value as a section offset. No need to produce an
546  // error here if the encoding isn't correct because we validate this in
547  // the .debug_info verifier.
548  auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
549  if (!StmtSectionOffset)
550  continue;
551  const uint32_t LineTableOffset = *StmtSectionOffset;
552  auto LineTable = DCtx.getLineTableForUnit(CU.get());
553  if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
554  if (!LineTable) {
555  ++NumDebugLineErrors;
556  error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset)
557  << "] was not able to be parsed for CU:\n";
558  Die.dump(OS, 0, DumpOpts);
559  OS << '\n';
560  continue;
561  }
562  } else {
563  // Make sure we don't get a valid line table back if the offset is wrong.
564  assert(LineTable == nullptr);
565  // Skip this line table as it isn't valid. No need to create an error
566  // here because we validate this in the .debug_info verifier.
567  continue;
568  }
569  auto Iter = StmtListToDie.find(LineTableOffset);
570  if (Iter != StmtListToDie.end()) {
571  ++NumDebugLineErrors;
572  error() << "two compile unit DIEs, "
573  << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
574  << format("0x%08" PRIx32, Die.getOffset())
575  << ", have the same DW_AT_stmt_list section offset:\n";
576  Iter->second.dump(OS, 0, DumpOpts);
577  Die.dump(OS, 0, DumpOpts);
578  OS << '\n';
579  // Already verified this line table before, no need to do it again.
580  continue;
581  }
582  StmtListToDie[LineTableOffset] = Die;
583  }
584 }
585 
586 void DWARFVerifier::verifyDebugLineRows() {
587  for (const auto &CU : DCtx.compile_units()) {
588  auto Die = CU->getUnitDIE();
589  auto LineTable = DCtx.getLineTableForUnit(CU.get());
590  // If there is no line table we will have created an error in the
591  // .debug_info verifier or in verifyDebugLineStmtOffsets().
592  if (!LineTable)
593  continue;
594 
595  // Verify prologue.
596  uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
597  uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
598  uint32_t FileIndex = 1;
599  StringMap<uint16_t> FullPathMap;
600  for (const auto &FileName : LineTable->Prologue.FileNames) {
601  // Verify directory index.
602  if (FileName.DirIdx > MaxDirIndex) {
603  ++NumDebugLineErrors;
604  error() << ".debug_line["
605  << format("0x%08" PRIx64,
606  *toSectionOffset(Die.find(DW_AT_stmt_list)))
607  << "].prologue.file_names[" << FileIndex
608  << "].dir_idx contains an invalid index: " << FileName.DirIdx
609  << "\n";
610  }
611 
612  // Check file paths for duplicates.
613  std::string FullPath;
614  const bool HasFullPath = LineTable->getFileNameByIndex(
615  FileIndex, CU->getCompilationDir(),
617  assert(HasFullPath && "Invalid index?");
618  (void)HasFullPath;
619  auto It = FullPathMap.find(FullPath);
620  if (It == FullPathMap.end())
621  FullPathMap[FullPath] = FileIndex;
622  else if (It->second != FileIndex) {
623  warn() << ".debug_line["
624  << format("0x%08" PRIx64,
625  *toSectionOffset(Die.find(DW_AT_stmt_list)))
626  << "].prologue.file_names[" << FileIndex
627  << "] is a duplicate of file_names[" << It->second << "]\n";
628  }
629 
630  FileIndex++;
631  }
632 
633  // Verify rows.
634  uint64_t PrevAddress = 0;
635  uint32_t RowIndex = 0;
636  for (const auto &Row : LineTable->Rows) {
637  // Verify row address.
638  if (Row.Address < PrevAddress) {
639  ++NumDebugLineErrors;
640  error() << ".debug_line["
641  << format("0x%08" PRIx64,
642  *toSectionOffset(Die.find(DW_AT_stmt_list)))
643  << "] row[" << RowIndex
644  << "] decreases in address from previous row:\n";
645 
647  if (RowIndex > 0)
648  LineTable->Rows[RowIndex - 1].dump(OS);
649  Row.dump(OS);
650  OS << '\n';
651  }
652 
653  // Verify file index.
654  if (Row.File > MaxFileIndex) {
655  ++NumDebugLineErrors;
656  error() << ".debug_line["
657  << format("0x%08" PRIx64,
658  *toSectionOffset(Die.find(DW_AT_stmt_list)))
659  << "][" << RowIndex << "] has invalid file index " << Row.File
660  << " (valid values are [1," << MaxFileIndex << "]):\n";
662  Row.dump(OS);
663  OS << '\n';
664  }
665  if (Row.EndSequence)
666  PrevAddress = 0;
667  else
668  PrevAddress = Row.Address;
669  ++RowIndex;
670  }
671  }
672 }
673 
675  NumDebugLineErrors = 0;
676  OS << "Verifying .debug_line...\n";
677  verifyDebugLineStmtOffsets();
678  verifyDebugLineRows();
679  return NumDebugLineErrors == 0;
680 }
681 
682 unsigned DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
683  DataExtractor *StrData,
684  const char *SectionName) {
685  unsigned NumErrors = 0;
686  DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
687  DCtx.isLittleEndian(), 0);
688  AppleAcceleratorTable AccelTable(AccelSectionData, *StrData);
689 
690  OS << "Verifying " << SectionName << "...\n";
691 
692  // Verify that the fixed part of the header is not too short.
693  if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
694  error() << "Section is too small to fit a section header.\n";
695  return 1;
696  }
697 
698  // Verify that the section is not too short.
699  if (Error E = AccelTable.extract()) {
700  error() << toString(std::move(E)) << '\n';
701  return 1;
702  }
703 
704  // Verify that all buckets have a valid hash index or are empty.
705  uint32_t NumBuckets = AccelTable.getNumBuckets();
706  uint32_t NumHashes = AccelTable.getNumHashes();
707 
708  uint32_t BucketsOffset =
709  AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
710  uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
711  uint32_t OffsetsBase = HashesBase + NumHashes * 4;
712  for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
713  uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
714  if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
715  error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
716  HashIdx);
717  ++NumErrors;
718  }
719  }
720  uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
721  if (NumAtoms == 0) {
722  error() << "No atoms: failed to read HashData.\n";
723  return 1;
724  }
725  if (!AccelTable.validateForms()) {
726  error() << "Unsupported form: failed to read HashData.\n";
727  return 1;
728  }
729 
730  for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
731  uint32_t HashOffset = HashesBase + 4 * HashIdx;
732  uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
733  uint32_t Hash = AccelSectionData.getU32(&HashOffset);
734  uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
735  if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
736  sizeof(uint64_t))) {
737  error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n",
738  HashIdx, HashDataOffset);
739  ++NumErrors;
740  }
741 
742  uint32_t StrpOffset;
743  uint32_t StringOffset;
744  uint32_t StringCount = 0;
745  unsigned Offset;
746  unsigned Tag;
747  while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
748  const uint32_t NumHashDataObjects =
749  AccelSectionData.getU32(&HashDataOffset);
750  for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
751  ++HashDataIdx) {
752  std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
753  auto Die = DCtx.getDIEForOffset(Offset);
754  if (!Die) {
755  const uint32_t BucketIdx =
756  NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
757  StringOffset = StrpOffset;
758  const char *Name = StrData->getCStr(&StringOffset);
759  if (!Name)
760  Name = "<NULL>";
761 
762  error() << format(
763  "%s Bucket[%d] Hash[%d] = 0x%08x "
764  "Str[%u] = 0x%08x "
765  "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
766  SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
767  HashDataIdx, Offset, Name);
768 
769  ++NumErrors;
770  continue;
771  }
772  if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
773  error() << "Tag " << dwarf::TagString(Tag)
774  << " in accelerator table does not match Tag "
775  << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
776  << "].\n";
777  ++NumErrors;
778  }
779  }
780  ++StringCount;
781  }
782  }
783  return NumErrors;
784 }
785 
786 unsigned
787 DWARFVerifier::verifyDebugNamesCULists(const DWARFDebugNames &AccelTable) {
788  // A map from CU offset to the (first) Name Index offset which claims to index
789  // this CU.
791  const uint32_t NotIndexed = std::numeric_limits<uint32_t>::max();
792 
793  CUMap.reserve(DCtx.getNumCompileUnits());
794  for (const auto &CU : DCtx.compile_units())
795  CUMap[CU->getOffset()] = NotIndexed;
796 
797  unsigned NumErrors = 0;
798  for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
799  if (NI.getCUCount() == 0) {
800  error() << formatv("Name Index @ {0:x} does not index any CU\n",
801  NI.getUnitOffset());
802  ++NumErrors;
803  continue;
804  }
805  for (uint32_t CU = 0, End = NI.getCUCount(); CU < End; ++CU) {
806  uint32_t Offset = NI.getCUOffset(CU);
807  auto Iter = CUMap.find(Offset);
808 
809  if (Iter == CUMap.end()) {
810  error() << formatv(
811  "Name Index @ {0:x} references a non-existing CU @ {1:x}\n",
812  NI.getUnitOffset(), Offset);
813  ++NumErrors;
814  continue;
815  }
816 
817  if (Iter->second != NotIndexed) {
818  error() << formatv("Name Index @ {0:x} references a CU @ {1:x}, but "
819  "this CU is already indexed by Name Index @ {2:x}\n",
820  NI.getUnitOffset(), Offset, Iter->second);
821  continue;
822  }
823  Iter->second = NI.getUnitOffset();
824  }
825  }
826 
827  for (const auto &KV : CUMap) {
828  if (KV.second == NotIndexed)
829  warn() << formatv("CU @ {0:x} not covered by any Name Index\n", KV.first);
830  }
831 
832  return NumErrors;
833 }
834 
835 unsigned
836 DWARFVerifier::verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
837  const DataExtractor &StrData) {
838  struct BucketInfo {
839  uint32_t Bucket;
840  uint32_t Index;
841 
842  constexpr BucketInfo(uint32_t Bucket, uint32_t Index)
843  : Bucket(Bucket), Index(Index) {}
844  bool operator<(const BucketInfo &RHS) const { return Index < RHS.Index; };
845  };
846 
847  uint32_t NumErrors = 0;
848  if (NI.getBucketCount() == 0) {
849  warn() << formatv("Name Index @ {0:x} does not contain a hash table.\n",
850  NI.getUnitOffset());
851  return NumErrors;
852  }
853 
854  // Build up a list of (Bucket, Index) pairs. We use this later to verify that
855  // each Name is reachable from the appropriate bucket.
856  std::vector<BucketInfo> BucketStarts;
857  BucketStarts.reserve(NI.getBucketCount() + 1);
858  for (uint32_t Bucket = 0, End = NI.getBucketCount(); Bucket < End; ++Bucket) {
859  uint32_t Index = NI.getBucketArrayEntry(Bucket);
860  if (Index > NI.getNameCount()) {
861  error() << formatv("Bucket {0} of Name Index @ {1:x} contains invalid "
862  "value {2}. Valid range is [0, {3}].\n",
863  Bucket, NI.getUnitOffset(), Index, NI.getNameCount());
864  ++NumErrors;
865  continue;
866  }
867  if (Index > 0)
868  BucketStarts.emplace_back(Bucket, Index);
869  }
870 
871  // If there were any buckets with invalid values, skip further checks as they
872  // will likely produce many errors which will only confuse the actual root
873  // problem.
874  if (NumErrors > 0)
875  return NumErrors;
876 
877  // Sort the list in the order of increasing "Index" entries.
878  array_pod_sort(BucketStarts.begin(), BucketStarts.end());
879 
880  // Insert a sentinel entry at the end, so we can check that the end of the
881  // table is covered in the loop below.
882  BucketStarts.emplace_back(NI.getBucketCount(), NI.getNameCount() + 1);
883 
884  // Loop invariant: NextUncovered is the (1-based) index of the first Name
885  // which is not reachable by any of the buckets we processed so far (and
886  // hasn't been reported as uncovered).
887  uint32_t NextUncovered = 1;
888  for (const BucketInfo &B : BucketStarts) {
889  // Under normal circumstances B.Index be equal to NextUncovered, but it can
890  // be less if a bucket points to names which are already known to be in some
891  // bucket we processed earlier. In that case, we won't trigger this error,
892  // but report the mismatched hash value error instead. (We know the hash
893  // will not match because we have already verified that the name's hash
894  // puts it into the previous bucket.)
895  if (B.Index > NextUncovered) {
896  error() << formatv("Name Index @ {0:x}: Name table entries [{1}, {2}] "
897  "are not covered by the hash table.\n",
898  NI.getUnitOffset(), NextUncovered, B.Index - 1);
899  ++NumErrors;
900  }
901  uint32_t Idx = B.Index;
902 
903  // The rest of the checks apply only to non-sentinel entries.
904  if (B.Bucket == NI.getBucketCount())
905  break;
906 
907  // This triggers if a non-empty bucket points to a name with a mismatched
908  // hash. Clients are likely to interpret this as an empty bucket, because a
909  // mismatched hash signals the end of a bucket, but if this is indeed an
910  // empty bucket, the producer should have signalled this by marking the
911  // bucket as empty.
912  uint32_t FirstHash = NI.getHashArrayEntry(Idx);
913  if (FirstHash % NI.getBucketCount() != B.Bucket) {
914  error() << formatv(
915  "Name Index @ {0:x}: Bucket {1} is not empty but points to a "
916  "mismatched hash value {2:x} (belonging to bucket {3}).\n",
917  NI.getUnitOffset(), B.Bucket, FirstHash,
918  FirstHash % NI.getBucketCount());
919  ++NumErrors;
920  }
921 
922  // This find the end of this bucket and also verifies that all the hashes in
923  // this bucket are correct by comparing the stored hashes to the ones we
924  // compute ourselves.
925  while (Idx <= NI.getNameCount()) {
926  uint32_t Hash = NI.getHashArrayEntry(Idx);
927  if (Hash % NI.getBucketCount() != B.Bucket)
928  break;
929 
930  const char *Str = NI.getNameTableEntry(Idx).getString();
931  if (caseFoldingDjbHash(Str) != Hash) {
932  error() << formatv("Name Index @ {0:x}: String ({1}) at index {2} "
933  "hashes to {3:x}, but "
934  "the Name Index hash is {4:x}\n",
935  NI.getUnitOffset(), Str, Idx,
936  caseFoldingDjbHash(Str), Hash);
937  ++NumErrors;
938  }
939 
940  ++Idx;
941  }
942  NextUncovered = std::max(NextUncovered, Idx);
943  }
944  return NumErrors;
945 }
946 
947 unsigned DWARFVerifier::verifyNameIndexAttribute(
950  StringRef FormName = dwarf::FormEncodingString(AttrEnc.Form);
951  if (FormName.empty()) {
952  error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
953  "unknown form: {3}.\n",
954  NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
955  AttrEnc.Form);
956  return 1;
957  }
958 
959  if (AttrEnc.Index == DW_IDX_type_hash) {
960  if (AttrEnc.Form != dwarf::DW_FORM_data8) {
961  error() << formatv(
962  "NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_type_hash "
963  "uses an unexpected form {2} (should be {3}).\n",
964  NI.getUnitOffset(), Abbr.Code, AttrEnc.Form, dwarf::DW_FORM_data8);
965  return 1;
966  }
967  }
968 
969  // A list of known index attributes and their expected form classes.
970  // DW_IDX_type_hash is handled specially in the check above, as it has a
971  // specific form (not just a form class) we should expect.
972  struct FormClassTable {
975  StringLiteral ClassName;
976  };
977  static constexpr FormClassTable Table[] = {
978  {dwarf::DW_IDX_compile_unit, DWARFFormValue::FC_Constant, {"constant"}},
979  {dwarf::DW_IDX_type_unit, DWARFFormValue::FC_Constant, {"constant"}},
980  {dwarf::DW_IDX_die_offset, DWARFFormValue::FC_Reference, {"reference"}},
981  {dwarf::DW_IDX_parent, DWARFFormValue::FC_Constant, {"constant"}},
982  };
983 
985  auto Iter = find_if(TableRef, [AttrEnc](const FormClassTable &T) {
986  return T.Index == AttrEnc.Index;
987  });
988  if (Iter == TableRef.end()) {
989  warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains an "
990  "unknown index attribute: {2}.\n",
991  NI.getUnitOffset(), Abbr.Code, AttrEnc.Index);
992  return 0;
993  }
994 
995  if (!DWARFFormValue(AttrEnc.Form).isFormClass(Iter->Class)) {
996  error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
997  "unexpected form {3} (expected form class {4}).\n",
998  NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
999  AttrEnc.Form, Iter->ClassName);
1000  return 1;
1001  }
1002  return 0;
1003 }
1004 
1005 unsigned
1006 DWARFVerifier::verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI) {
1007  if (NI.getLocalTUCount() + NI.getForeignTUCount() > 0) {
1008  warn() << formatv("Name Index @ {0:x}: Verifying indexes of type units is "
1009  "not currently supported.\n",
1010  NI.getUnitOffset());
1011  return 0;
1012  }
1013 
1014  unsigned NumErrors = 0;
1015  for (const auto &Abbrev : NI.getAbbrevs()) {
1016  StringRef TagName = dwarf::TagString(Abbrev.Tag);
1017  if (TagName.empty()) {
1018  warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} references an "
1019  "unknown tag: {2}.\n",
1020  NI.getUnitOffset(), Abbrev.Code, Abbrev.Tag);
1021  }
1022  SmallSet<unsigned, 5> Attributes;
1023  for (const auto &AttrEnc : Abbrev.Attributes) {
1024  if (!Attributes.insert(AttrEnc.Index).second) {
1025  error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains "
1026  "multiple {2} attributes.\n",
1027  NI.getUnitOffset(), Abbrev.Code, AttrEnc.Index);
1028  ++NumErrors;
1029  continue;
1030  }
1031  NumErrors += verifyNameIndexAttribute(NI, Abbrev, AttrEnc);
1032  }
1033 
1034  if (NI.getCUCount() > 1 && !Attributes.count(dwarf::DW_IDX_compile_unit)) {
1035  error() << formatv("NameIndex @ {0:x}: Indexing multiple compile units "
1036  "and abbreviation {1:x} has no {2} attribute.\n",
1037  NI.getUnitOffset(), Abbrev.Code,
1038  dwarf::DW_IDX_compile_unit);
1039  ++NumErrors;
1040  }
1041  if (!Attributes.count(dwarf::DW_IDX_die_offset)) {
1042  error() << formatv(
1043  "NameIndex @ {0:x}: Abbreviation {1:x} has no {2} attribute.\n",
1044  NI.getUnitOffset(), Abbrev.Code, dwarf::DW_IDX_die_offset);
1045  ++NumErrors;
1046  }
1047  }
1048  return NumErrors;
1049 }
1050 
1053  if (const char *Str = DIE.getName(DINameKind::ShortName))
1054  Result.emplace_back(Str);
1055  else if (DIE.getTag() == dwarf::DW_TAG_namespace)
1056  Result.emplace_back("(anonymous namespace)");
1057 
1058  if (const char *Str = DIE.getName(DINameKind::LinkageName)) {
1059  if (Result.empty() || Result[0] != Str)
1060  Result.emplace_back(Str);
1061  }
1062 
1063  return Result;
1064 }
1065 
1066 unsigned DWARFVerifier::verifyNameIndexEntries(
1067  const DWARFDebugNames::NameIndex &NI,
1068  const DWARFDebugNames::NameTableEntry &NTE) {
1069  // Verifying type unit indexes not supported.
1070  if (NI.getLocalTUCount() + NI.getForeignTUCount() > 0)
1071  return 0;
1072 
1073  const char *CStr = NTE.getString();
1074  if (!CStr) {
1075  error() << formatv(
1076  "Name Index @ {0:x}: Unable to get string associated with name {1}.\n",
1077  NI.getUnitOffset(), NTE.getIndex());
1078  return 1;
1079  }
1080  StringRef Str(CStr);
1081 
1082  unsigned NumErrors = 0;
1083  unsigned NumEntries = 0;
1084  uint32_t EntryID = NTE.getEntryOffset();
1085  uint32_t NextEntryID = EntryID;
1086  Expected<DWARFDebugNames::Entry> EntryOr = NI.getEntry(&NextEntryID);
1087  for (; EntryOr; ++NumEntries, EntryID = NextEntryID,
1088  EntryOr = NI.getEntry(&NextEntryID)) {
1089  uint32_t CUIndex = *EntryOr->getCUIndex();
1090  if (CUIndex > NI.getCUCount()) {
1091  error() << formatv("Name Index @ {0:x}: Entry @ {1:x} contains an "
1092  "invalid CU index ({2}).\n",
1093  NI.getUnitOffset(), EntryID, CUIndex);
1094  ++NumErrors;
1095  continue;
1096  }
1097  uint32_t CUOffset = NI.getCUOffset(CUIndex);
1098  uint64_t DIEOffset = CUOffset + *EntryOr->getDIEUnitOffset();
1099  DWARFDie DIE = DCtx.getDIEForOffset(DIEOffset);
1100  if (!DIE) {
1101  error() << formatv("Name Index @ {0:x}: Entry @ {1:x} references a "
1102  "non-existing DIE @ {2:x}.\n",
1103  NI.getUnitOffset(), EntryID, DIEOffset);
1104  ++NumErrors;
1105  continue;
1106  }
1107  if (DIE.getDwarfUnit()->getOffset() != CUOffset) {
1108  error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched CU of "
1109  "DIE @ {2:x}: index - {3:x}; debug_info - {4:x}.\n",
1110  NI.getUnitOffset(), EntryID, DIEOffset, CUOffset,
1111  DIE.getDwarfUnit()->getOffset());
1112  ++NumErrors;
1113  }
1114  if (DIE.getTag() != EntryOr->tag()) {
1115  error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Tag of "
1116  "DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1117  NI.getUnitOffset(), EntryID, DIEOffset, EntryOr->tag(),
1118  DIE.getTag());
1119  ++NumErrors;
1120  }
1121 
1122  auto EntryNames = getNames(DIE);
1123  if (!is_contained(EntryNames, Str)) {
1124  error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Name "
1125  "of DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1126  NI.getUnitOffset(), EntryID, DIEOffset, Str,
1127  make_range(EntryNames.begin(), EntryNames.end()));
1128  ++NumErrors;
1129  }
1130  }
1131  handleAllErrors(EntryOr.takeError(),
1132  [&](const DWARFDebugNames::SentinelError &) {
1133  if (NumEntries > 0)
1134  return;
1135  error() << formatv("Name Index @ {0:x}: Name {1} ({2}) is "
1136  "not associated with any entries.\n",
1137  NI.getUnitOffset(), NTE.getIndex(), Str);
1138  ++NumErrors;
1139  },
1140  [&](const ErrorInfoBase &Info) {
1141  error()
1142  << formatv("Name Index @ {0:x}: Name {1} ({2}): {3}\n",
1143  NI.getUnitOffset(), NTE.getIndex(), Str,
1144  Info.message());
1145  ++NumErrors;
1146  });
1147  return NumErrors;
1148 }
1149 
1150 static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx) {
1151  Optional<DWARFFormValue> Location = Die.findRecursively(DW_AT_location);
1152  if (!Location)
1153  return false;
1154 
1155  auto ContainsInterestingOperators = [&](StringRef D) {
1156  DWARFUnit *U = Die.getDwarfUnit();
1158  DWARFExpression Expression(Data, U->getVersion(), U->getAddressByteSize());
1159  return any_of(Expression, [](DWARFExpression::Operation &Op) {
1160  return !Op.isError() && (Op.getCode() == DW_OP_addr ||
1161  Op.getCode() == DW_OP_form_tls_address ||
1162  Op.getCode() == DW_OP_GNU_push_tls_address);
1163  });
1164  };
1165 
1166  if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
1167  // Inlined location.
1168  if (ContainsInterestingOperators(toStringRef(*Expr)))
1169  return true;
1170  } else if (Optional<uint64_t> Offset = Location->getAsSectionOffset()) {
1171  // Location list.
1172  if (const DWARFDebugLoc *DebugLoc = DCtx.getDebugLoc()) {
1173  if (const DWARFDebugLoc::LocationList *LocList =
1174  DebugLoc->getLocationListAtOffset(*Offset)) {
1175  if (any_of(LocList->Entries, [&](const DWARFDebugLoc::Entry &E) {
1176  return ContainsInterestingOperators({E.Loc.data(), E.Loc.size()});
1177  }))
1178  return true;
1179  }
1180  }
1181  }
1182  return false;
1183 }
1184 
1185 unsigned DWARFVerifier::verifyNameIndexCompleteness(
1186  const DWARFDie &Die, const DWARFDebugNames::NameIndex &NI) {
1187 
1188  // First check, if the Die should be indexed. The code follows the DWARF v5
1189  // wording as closely as possible.
1190 
1191  // "All non-defining declarations (that is, debugging information entries
1192  // with a DW_AT_declaration attribute) are excluded."
1193  if (Die.find(DW_AT_declaration))
1194  return 0;
1195 
1196  // "DW_TAG_namespace debugging information entries without a DW_AT_name
1197  // attribute are included with the name “(anonymous namespace)”.
1198  // All other debugging information entries without a DW_AT_name attribute
1199  // are excluded."
1200  // "If a subprogram or inlined subroutine is included, and has a
1201  // DW_AT_linkage_name attribute, there will be an additional index entry for
1202  // the linkage name."
1203  auto EntryNames = getNames(Die);
1204  if (EntryNames.empty())
1205  return 0;
1206 
1207  // We deviate from the specification here, which says:
1208  // "The name index must contain an entry for each debugging information entry
1209  // that defines a named subprogram, label, variable, type, or namespace,
1210  // subject to ..."
1211  // Instead whitelisting all TAGs representing a "type" or a "subprogram", to
1212  // make sure we catch any missing items, we instead blacklist all TAGs that we
1213  // know shouldn't be indexed.
1214  switch (Die.getTag()) {
1215  // Compile unit has a name but it shouldn't be indexed.
1216  case DW_TAG_compile_unit:
1217  return 0;
1218 
1219  // Function and template parameters are not globally visible, so we shouldn't
1220  // index them.
1221  case DW_TAG_formal_parameter:
1222  case DW_TAG_template_value_parameter:
1223  case DW_TAG_template_type_parameter:
1224  case DW_TAG_GNU_template_parameter_pack:
1225  case DW_TAG_GNU_template_template_param:
1226  return 0;
1227 
1228  // Object members aren't globally visible.
1229  case DW_TAG_member:
1230  return 0;
1231 
1232  // According to a strict reading of the specification, enumerators should not
1233  // be indexed (and LLVM currently does not do that). However, this causes
1234  // problems for the debuggers, so we may need to reconsider this.
1235  case DW_TAG_enumerator:
1236  return 0;
1237 
1238  // Imported declarations should not be indexed according to the specification
1239  // and LLVM currently does not do that.
1240  case DW_TAG_imported_declaration:
1241  return 0;
1242 
1243  // "DW_TAG_subprogram, DW_TAG_inlined_subroutine, and DW_TAG_label debugging
1244  // information entries without an address attribute (DW_AT_low_pc,
1245  // DW_AT_high_pc, DW_AT_ranges, or DW_AT_entry_pc) are excluded."
1246  case DW_TAG_subprogram:
1247  case DW_TAG_inlined_subroutine:
1248  case DW_TAG_label:
1249  if (Die.findRecursively(
1250  {DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_entry_pc}))
1251  break;
1252  return 0;
1253 
1254  // "DW_TAG_variable debugging information entries with a DW_AT_location
1255  // attribute that includes a DW_OP_addr or DW_OP_form_tls_address operator are
1256  // included; otherwise, they are excluded."
1257  //
1258  // LLVM extension: We also add DW_OP_GNU_push_tls_address to this list.
1259  case DW_TAG_variable:
1260  if (isVariableIndexable(Die, DCtx))
1261  break;
1262  return 0;
1263 
1264  default:
1265  break;
1266  }
1267 
1268  // Now we know that our Die should be present in the Index. Let's check if
1269  // that's the case.
1270  unsigned NumErrors = 0;
1271  uint64_t DieUnitOffset = Die.getOffset() - Die.getDwarfUnit()->getOffset();
1272  for (StringRef Name : EntryNames) {
1273  if (none_of(NI.equal_range(Name), [&](const DWARFDebugNames::Entry &E) {
1274  return E.getDIEUnitOffset() == DieUnitOffset;
1275  })) {
1276  error() << formatv("Name Index @ {0:x}: Entry for DIE @ {1:x} ({2}) with "
1277  "name {3} missing.\n",
1278  NI.getUnitOffset(), Die.getOffset(), Die.getTag(),
1279  Name);
1280  ++NumErrors;
1281  }
1282  }
1283  return NumErrors;
1284 }
1285 
1286 unsigned DWARFVerifier::verifyDebugNames(const DWARFSection &AccelSection,
1287  const DataExtractor &StrData) {
1288  unsigned NumErrors = 0;
1289  DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), AccelSection,
1290  DCtx.isLittleEndian(), 0);
1291  DWARFDebugNames AccelTable(AccelSectionData, StrData);
1292 
1293  OS << "Verifying .debug_names...\n";
1294 
1295  // This verifies that we can read individual name indices and their
1296  // abbreviation tables.
1297  if (Error E = AccelTable.extract()) {
1298  error() << toString(std::move(E)) << '\n';
1299  return 1;
1300  }
1301 
1302  NumErrors += verifyDebugNamesCULists(AccelTable);
1303  for (const auto &NI : AccelTable)
1304  NumErrors += verifyNameIndexBuckets(NI, StrData);
1305  for (const auto &NI : AccelTable)
1306  NumErrors += verifyNameIndexAbbrevs(NI);
1307 
1308  // Don't attempt Entry validation if any of the previous checks found errors
1309  if (NumErrors > 0)
1310  return NumErrors;
1311  for (const auto &NI : AccelTable)
1312  for (DWARFDebugNames::NameTableEntry NTE : NI)
1313  NumErrors += verifyNameIndexEntries(NI, NTE);
1314 
1315  if (NumErrors > 0)
1316  return NumErrors;
1317 
1318  for (const std::unique_ptr<DWARFCompileUnit> &CU : DCtx.compile_units()) {
1319  if (const DWARFDebugNames::NameIndex *NI =
1320  AccelTable.getCUNameIndex(CU->getOffset())) {
1321  for (const DWARFDebugInfoEntry &Die : CU->dies())
1322  NumErrors += verifyNameIndexCompleteness(DWARFDie(CU.get(), &Die), *NI);
1323  }
1324  }
1325  return NumErrors;
1326 }
1327 
1329  const DWARFObject &D = DCtx.getDWARFObj();
1330  DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
1331  unsigned NumErrors = 0;
1332  if (!D.getAppleNamesSection().Data.empty())
1333  NumErrors +=
1334  verifyAppleAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
1335  if (!D.getAppleTypesSection().Data.empty())
1336  NumErrors +=
1337  verifyAppleAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
1339  NumErrors += verifyAppleAccelTable(&D.getAppleNamespacesSection(), &StrData,
1340  ".apple_namespaces");
1341  if (!D.getAppleObjCSection().Data.empty())
1342  NumErrors +=
1343  verifyAppleAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
1344 
1345  if (!D.getDebugNamesSection().Data.empty())
1346  NumErrors += verifyDebugNames(D.getDebugNamesSection(), StrData);
1347  return NumErrors == 0;
1348 }
1349 
1350 raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
1351 
1352 raw_ostream &DWARFVerifier::warn() const { return WithColor::warning(OS); }
1353 
1354 raw_ostream &DWARFVerifier::note() const { return WithColor::note(OS); }
A list of locations that contain one variable.
Definition: DWARFDebugLoc.h:37
static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag)
Definition: DWARFUnit.h:399
static SmallVector< StringRef, 2 > getNames(const DWARFDie &DIE)
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
uint32_t getEntryOffset() const
Returns the offset of the first Entry in the list.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
bool isValid() const
Definition: DWARFDie.h:51
dwarf::Attribute Attr
The attribute enumeration of this attribute.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const DWARFDebugLoc * getDebugLoc()
Get a pointer to the parsed DebugLoc object.
virtual StringRef getAbbrevSection() const
Definition: DWARFObject.h:39
bool isUnitType(uint8_t UnitType)
Definition: Dwarf.h:342
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:63
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:199
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
bool handleDebugLine()
Verify the information in the .debug_line section.
iterator find(StringRef Key)
Definition: StringMap.h:333
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:65
#define error(X)
A debug info location.
Definition: DebugLoc.h:34
This class represents an Operation in the Expression.
uint16_t getU16(uint32_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
bool isFormClass(FormClass FC) const
Base class for error info classes.
Definition: Error.h:48
void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition: DWARFDie.cpp:482
virtual StringRef getAbbrevDWOSection() const
Definition: DWARFObject.h:58
Abbreviation describing the encoding of Name Index entries.
virtual const DWARFSection & getAppleTypesSection() const
Definition: DWARFObject.h:69
bool handleDebugInfo()
Verify the information in the .debug_info section.
dwarf::Form getForm() const
DWARFFormValue Value
The form and value for this attribute.
uint32_t getNextUnitOffset() const
Definition: DWARFUnit.h:345
Optional< uint64_t > getAsReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
uint64_t getRawUValue() const
Optional< ArrayRef< uint8_t > > getAsBlock() const
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:935
A single location within a location list.
Definition: DWARFDebugLoc.h:27
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:67
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Index attribute and its encoding.
uint32_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:67
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:338
std::set< DieRangeInfo > Children
Sorted DWARFAddressRangeInfo.
Definition: DWARFVerifier.h:45
unsigned getNumDIEs()
Returns the number of DIEs in the unit.
Definition: DWARFUnit.h:488
std::vector< DWARFAddressRange > Ranges
Sorted DWARFAddressRanges.
Definition: DWARFVerifier.h:42
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value...
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:72
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
const DenseSet< Abbrev, AbbrevMapInfo > & getAbbrevs() const
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1017
Represents a single accelerator table within the Dwarf 5 .debug_names section.
uint32_t getBucketArrayEntry(uint32_t Bucket) const
Reads an entry in the Bucket Array for the given Bucket.
uint16_t getVersion() const
Definition: DWARFUnit.h:337
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:146
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
virtual const DWARFSection & getAppleObjCSection() const
Definition: DWARFObject.h:74
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:869
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:839
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DWARF v5-specific implementation of an Accelerator Entry.
This implements the Apple accelerator table format, a precursor of the DWARF 5 accelerator table form...
virtual const DWARFSection & getStringOffsetSection() const
Definition: DWARFObject.h:54
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
A structured debug information entry.
Definition: DIE.h:662
Optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition: DWARFDie.cpp:315
bool handleDebugAbbrev()
Verify the information in any of the following sections, if available: .debug_abbrev, debug_abbrev.dwo.
NameTableEntry getNameTableEntry(uint32_t Index) const
Reads an entry in the Name Table for the given Index.
A class that keeps the address range information for a single DIE.
Definition: DWARFVerifier.h:38
const DWARFAbbreviationDeclarationSet * getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:928
uint32_t getHashArrayEntry(uint32_t Index) const
Reads an entry in the Hash Array for the given Index.
bool contains(const DieRangeInfo &RHS) const
Return true if ranges in this object contains all ranges within RHS.
uint8_t getU8(uint32_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
virtual StringRef getStringSection() const
Definition: DWARFObject.h:46
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again...
Definition: DenseMap.h:100
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:949
bool extract(DWARFContext &Context, const DWARFDataExtractor &debug_info, uint32_t *offset_ptr, DWARFSectionKind Kind=DW_SECT_INFO, const DWARFUnitIndex *Index=nullptr)
Parse a unit header from debug_info starting at offset_ptr.
Definition: DWARFUnit.cpp:96
Expected< Entry > getEntry(uint32_t *Offset) const
std::vector< DWARFAddressRange >::const_iterator address_range_iterator
Definition: DWARFVerifier.h:55
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:53
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:971
Base class describing the header of any kind of "unit." Some information is specific to certain unit ...
Definition: DWARFUnit.h:47
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
UnitType
Constants for unit types in DWARF v5.
Definition: Dwarf.h:328
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
const char * getString() const
Return the string referenced by this name table entry or nullptr if the string offset is not valid...
.debug_names section consists of one or more units.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:898
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:438
const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin references if necessary...
Definition: DWARFDie.cpp:444
A single entry in the Name Table (Dwarf 5 sect.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
uint32_t caseFoldingDjbHash(StringRef Buffer, uint32_t H=5381)
Computes the Bernstein hash after folding the input according to the Dwarf 5 standard case folding ru...
Definition: DJB.cpp:71
Concrete instance of DWARFUnitSection, specialized for one Unit type.
Definition: DWARFUnit.h:133
address_range_iterator insert(const DWARFAddressRange &R)
Inserts the address range.
iterator end() const
Definition: ArrayRef.h:138
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:59
uint32_t getIndex() const
Return the index of this name in the parent Name Index.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static void dumpTableHeader(raw_ostream &OS)
Optional< const char * > toString(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:239
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
StringRef UnitTypeString(unsigned)
Definition: Dwarf.cpp:480
Error returned by NameIndex::getEntry to report it has reached the end of the entry list...
virtual const DWARFSection & getAppleNamespacesSection() const
Definition: DWARFObject.h:70
#define Success
virtual const DWARFSection & getInfoSection() const
Definition: DWARFObject.h:36
virtual const DWARFSection & getLineSection() const
Definition: DWARFObject.h:44
bool intersects(const DieRangeInfo &RHS) const
Return true if any range in this object intersects with any range in RHS.
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
virtual const DWARFSection & getDebugNamesSection() const
Definition: DWARFObject.h:73
static bool isSupportedVersion(unsigned version)
Definition: DWARFContext.h:301
DWARFDie getDIEAtIndex(unsigned Index)
Return the DIE object at the given index.
Definition: DWARFUnit.h:504
void emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:652
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
bool handleAccelTables()
Verify the information in accelerator tables, if they exist.
#define I(x, y, z)
Definition: MD5.cpp:58
virtual const DWARFSection & getAppleNamesSection() const
Definition: DWARFObject.h:68
const char * getCStr(uint32_t *offset_ptr) const
Extract a C string from *offset_ptr.
iterator end()
Definition: DenseMap.h:79
uint32_t getOffset() const
Definition: DWARFUnit.h:333
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:392
std::set< DieRangeInfo >::const_iterator die_range_info_iterator
Definition: DWARFVerifier.h:56
bool isLittleEndian() const
Definition: DWARFContext.h:300
bool isValidOffset(uint32_t offset) const
Test the validity of offset.
virtual const DWARFSection & getRangeSection() const
Definition: DWARFObject.h:47
uint32_t Code
Abbreviation code.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:291
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
DWARFDebugInfoEntry - A DIE with only the minimum required data.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, const char *ErrorMsg)
uint32_t getCUOffset(uint32_t CU) const
Reads offset of compilation unit CU. CU is 0-based.
iterator_range< ValueIterator > equal_range(StringRef Key) const
Look up all entries in this Name Index matching Key.
Optional< uint64_t > getAsSectionOffset() const
const uint64_t Version
Definition: InstrProf.h:895
iterator end()
Definition: StringMap.h:318
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:980