1//===- DWARFVerifier.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
9#include "llvm/ADT/IntervalMap.h"
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallSet.h"
12#include "llvm/BinaryFormat/Dwarf.h"
13#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
14#include "llvm/DebugInfo/DWARF/DWARFAttribute.h"
15#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
16#include "llvm/DebugInfo/DWARF/DWARFContext.h"
17#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
18#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
20#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
21#include "llvm/DebugInfo/DWARF/DWARFDie.h"
22#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
23#include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
24#include "llvm/DebugInfo/DWARF/DWARFObject.h"
25#include "llvm/DebugInfo/DWARF/DWARFSection.h"
26#include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
27#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
28#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
29#include "llvm/Object/Error.h"
30#include "llvm/Support/DJB.h"
31#include "llvm/Support/Error.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/FormatVariadic.h"
35#include "llvm/Support/JSON.h"
36#include "llvm/Support/Parallel.h"
37#include "llvm/Support/WithColor.h"
38#include "llvm/Support/raw_ostream.h"
39#include <map>
40#include <set>
41#include <vector>
42
43using namespace llvm;
44using namespace dwarf;
45using namespace object;
46
47namespace llvm {
48class DWARFDebugInfoEntry;
49}
50
51std::optional<DWARFAddressRange>
52DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
53 auto Begin = Ranges.begin();
54 auto End = Ranges.end();
55 auto Pos = std::lower_bound(first: Begin, last: End, val: R);
56
57 // Check for exact duplicates which is an allowed special case
58 if (Pos != End && *Pos == R) {
59 return std::nullopt;
60 }
61
62 if (Pos != End) {
63 DWARFAddressRange Range(*Pos);
64 if (Pos->merge(RHS: R))
65 return Range;
66 }
67 if (Pos != Begin) {
68 auto Iter = Pos - 1;
69 DWARFAddressRange Range(*Iter);
70 if (Iter->merge(RHS: R))
71 return Range;
72 }
73
74 Ranges.insert(position: Pos, x: R);
75 return std::nullopt;
76}
77
78DWARFVerifier::DieRangeInfo::die_range_info_iterator
79DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) {
80 if (RI.Ranges.empty())
81 return Children.end();
82
83 // Use lower_bound to find the insertion point in O(log N), then check
84 // only the immediate neighbors for overlap. Since children are verified
85 // to be non-overlapping as they are inserted, only adjacent entries in
86 // the sorted set can intersect with a newly inserted entry.
87 auto It = Children.lower_bound(x: RI);
88
89 // Check the predecessor for overlap.
90 if (It != Children.begin()) {
91 auto Prev = std::prev(x: It);
92 if (Prev->intersects(RHS: RI))
93 return Prev;
94 }
95
96 // Check the element at the lower_bound position for overlap or duplicate.
97 if (It != Children.end()) {
98 // We only override "smaller than", so "not smaller than" (RI >= It) plus
99 // the semantics of `lower_bound` (It >= RI) says this is exact duplicate
100 // (equivalent key), which is allowed and doesn't need reinsertion.
101 if (!(RI < *It))
102 return Children.end();
103 if (It->intersects(RHS: RI))
104 return It;
105 }
106
107 // No overlap — insert with hint for O(1) amortized insertion.
108 Children.insert(position: It, x: RI);
109 return Children.end();
110}
111bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
112 auto I1 = Ranges.begin(), E1 = Ranges.end();
113 auto I2 = RHS.Ranges.begin(), E2 = RHS.Ranges.end();
114 if (I2 == E2)
115 return true;
116
117 DWARFAddressRange R = *I2;
118 while (I1 != E1) {
119 bool Covered = I1->LowPC <= R.LowPC;
120 if (R.LowPC == R.HighPC || (Covered && R.HighPC <= I1->HighPC)) {
121 if (++I2 == E2)
122 return true;
123 R = *I2;
124 continue;
125 }
126 if (!Covered)
127 return false;
128 if (R.LowPC < I1->HighPC)
129 R.LowPC = I1->HighPC;
130 ++I1;
131 }
132 return false;
133}
134
135bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
136 auto I1 = Ranges.begin(), E1 = Ranges.end();
137 auto I2 = RHS.Ranges.begin(), E2 = RHS.Ranges.end();
138 while (I1 != E1 && I2 != E2) {
139 if (I1->intersects(RHS: *I2)) {
140 // Exact duplicates are allowed
141 if (!(*I1 == *I2))
142 return true;
143 }
144 if (I1->LowPC < I2->LowPC)
145 ++I1;
146 else
147 ++I2;
148 }
149 return false;
150}
151
152bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
153 uint64_t *Offset, unsigned UnitIndex,
154 uint8_t &UnitType, bool &isUnitDWARF64) {
155 uint64_t AbbrOffset, Length;
156 uint8_t AddrSize = 0;
157 uint16_t Version;
158 bool Success = true;
159
160 bool ValidLength = false;
161 bool ValidVersion = false;
162 bool ValidAddrSize = false;
163 bool ValidType = true;
164 bool ValidAbbrevOffset = true;
165
166 uint64_t OffsetStart = *Offset;
167 DwarfFormat Format;
168 std::tie(args&: Length, args&: Format) = DebugInfoData.getInitialLength(Off: Offset);
169 isUnitDWARF64 = Format == DWARF64;
170 Version = DebugInfoData.getU16(offset_ptr: Offset);
171
172 if (Version >= 5) {
173 UnitType = DebugInfoData.getU8(offset_ptr: Offset);
174 AddrSize = DebugInfoData.getU8(offset_ptr: Offset);
175 AbbrOffset = isUnitDWARF64 ? DebugInfoData.getU64(offset_ptr: Offset)
176 : DebugInfoData.getU32(offset_ptr: Offset);
177 ValidType = dwarf::isUnitType(UnitType);
178 } else {
179 UnitType = 0;
180 AbbrOffset = isUnitDWARF64 ? DebugInfoData.getU64(offset_ptr: Offset)
181 : DebugInfoData.getU32(offset_ptr: Offset);
182 AddrSize = DebugInfoData.getU8(offset_ptr: Offset);
183 }
184
185 Expected<const DWARFAbbreviationDeclarationSet *> AbbrevSetOrErr =
186 DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(CUAbbrOffset: AbbrOffset);
187 if (!AbbrevSetOrErr) {
188 ValidAbbrevOffset = false;
189 // FIXME: A problematic debug_abbrev section is reported below in the form
190 // of a `note:`. We should propagate this error there (or elsewhere) to
191 // avoid losing the specific problem with the debug_abbrev section.
192 consumeError(Err: AbbrevSetOrErr.takeError());
193 }
194
195 ValidLength = DebugInfoData.isValidOffset(offset: OffsetStart + Length + 3);
196 ValidVersion = DWARFContext::isSupportedVersion(version: Version);
197 ValidAddrSize = DWARFContext::isAddressSizeSupported(AddressSize: AddrSize);
198 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
199 !ValidType) {
200 Success = false;
201 bool HeaderShown = false;
202 auto ShowHeaderOnce = [&]() {
203 if (!HeaderShown) {
204 error() << formatv(Fmt: "Units[{0}] - start offset: {1:x+8}\n", Vals&: UnitIndex,
205 Vals&: OffsetStart);
206 HeaderShown = true;
207 }
208 };
209 if (!ValidLength)
210 ErrorCategory.Report(
211 category: "Unit Header Length: Unit too large for .debug_info provided", detailCallback: [&]() {
212 ShowHeaderOnce();
213 note() << "The length for this unit is too "
214 "large for the .debug_info provided.\n";
215 });
216 if (!ValidVersion)
217 ErrorCategory.Report(
218 category: "Unit Header Length: 16 bit unit header version is not valid", detailCallback: [&]() {
219 ShowHeaderOnce();
220 note() << "The 16 bit unit header version is not valid.\n";
221 });
222 if (!ValidType)
223 ErrorCategory.Report(
224 category: "Unit Header Length: Unit type encoding is not valid", detailCallback: [&]() {
225 ShowHeaderOnce();
226 note() << "The unit type encoding is not valid.\n";
227 });
228 if (!ValidAbbrevOffset)
229 ErrorCategory.Report(
230 category: "Unit Header Length: Offset into the .debug_abbrev section is not "
231 "valid",
232 detailCallback: [&]() {
233 ShowHeaderOnce();
234 note() << "The offset into the .debug_abbrev section is "
235 "not valid.\n";
236 });
237 if (!ValidAddrSize)
238 ErrorCategory.Report(category: "Unit Header Length: Address size is unsupported",
239 detailCallback: [&]() {
240 ShowHeaderOnce();
241 note() << "The address size is unsupported.\n";
242 });
243 }
244 *Offset = OffsetStart + Length + (isUnitDWARF64 ? 12 : 4);
245 return Success;
246}
247
248bool DWARFVerifier::verifyName(const DWARFDie &Die) {
249 // FIXME Add some kind of record of which DIE names have already failed and
250 // don't bother checking a DIE that uses an already failed DIE.
251
252 std::string ReconstructedName;
253 raw_string_ostream OS(ReconstructedName);
254 std::string OriginalFullName;
255 Die.getFullName(OS, OriginalFullName: &OriginalFullName);
256 if (OriginalFullName.empty() || OriginalFullName == ReconstructedName)
257 return false;
258
259 ErrorCategory.Report(
260 category: "Simplified template DW_AT_name could not be reconstituted", detailCallback: [&]() {
261 error()
262 << "Simplified template DW_AT_name could not be reconstituted:\n"
263 << formatv(Fmt: " original: {0}\n"
264 " reconstituted: {1}\n",
265 Vals&: OriginalFullName, Vals&: ReconstructedName);
266 dump(Die) << '\n';
267 dump(Die: Die.getDwarfUnit()->getUnitDIE()) << '\n';
268 });
269 return true;
270}
271
272unsigned DWARFVerifier::verifyUnitContents(DWARFUnit &Unit,
273 ReferenceMap &UnitLocalReferences,
274 ReferenceMap &CrossUnitReferences) {
275 unsigned NumUnitErrors = 0;
276 unsigned NumDies = Unit.getNumDIEs();
277 for (unsigned I = 0; I < NumDies; ++I) {
278 auto Die = Unit.getDIEAtIndex(Index: I);
279
280 if (Die.getTag() == DW_TAG_null)
281 continue;
282
283 for (auto AttrValue : Die.attributes()) {
284 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
285 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue, UnitLocalReferences,
286 CrossUnitReferences);
287 }
288
289 NumUnitErrors += verifyName(Die);
290
291 if (Die.hasChildren()) {
292 if (Die.getFirstChild().isValid() &&
293 Die.getFirstChild().getTag() == DW_TAG_null) {
294 warn() << formatv(Fmt: "{0} has DW_CHILDREN_yes but DIE has no children: ",
295 Vals: dwarf::TagString(Tag: Die.getTag()));
296 Die.dump(OS);
297 }
298 }
299
300 NumUnitErrors += verifyDebugInfoCallSite(Die);
301 }
302
303 DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
304 if (!Die) {
305 ErrorCategory.Report(category: "Compilation unit missing DIE", detailCallback: [&]() {
306 error() << "Compilation unit without DIE.\n";
307 });
308 NumUnitErrors++;
309 return NumUnitErrors;
310 }
311
312 if (!dwarf::isUnitType(T: Die.getTag())) {
313 ErrorCategory.Report(category: "Compilation unit root DIE is not a unit DIE", detailCallback: [&]() {
314 error() << formatv(Fmt: "Compilation unit root DIE is not a unit DIE: {0}.\n",
315 Vals: dwarf::TagString(Tag: Die.getTag()));
316 });
317 NumUnitErrors++;
318 }
319
320 uint8_t UnitType = Unit.getUnitType();
321 if (!DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Tag: Die.getTag())) {
322 ErrorCategory.Report(category: "Mismatched unit type", detailCallback: [&]() {
323 error() << formatv(
324 Fmt: "Compilation unit type ({0}) and root DIE ({1}) do not match.\n",
325 Vals: dwarf::UnitTypeString(UnitType), Vals: dwarf::TagString(Tag: Die.getTag()));
326 });
327 NumUnitErrors++;
328 }
329
330 // According to DWARF Debugging Information Format Version 5,
331 // 3.1.2 Skeleton Compilation Unit Entries:
332 // "A skeleton compilation unit has no children."
333 if (Die.getTag() == dwarf::DW_TAG_skeleton_unit && Die.hasChildren()) {
334 ErrorCategory.Report(category: "Skeleton CU has children", detailCallback: [&]() {
335 error() << "Skeleton compilation unit has children.\n";
336 });
337 NumUnitErrors++;
338 }
339
340 DieRangeInfo RI;
341 NumUnitErrors += verifyDieRanges(Die, ParentRI&: RI);
342
343 return NumUnitErrors;
344}
345
346unsigned DWARFVerifier::verifyDebugInfoCallSite(const DWARFDie &Die) {
347 if (Die.getTag() != DW_TAG_call_site && Die.getTag() != DW_TAG_GNU_call_site)
348 return 0;
349
350 DWARFDie Curr = Die.getParent();
351 for (; Curr.isValid() && !Curr.isSubprogramDIE(); Curr = Curr.getParent()) {
352 if (Curr.getTag() == DW_TAG_inlined_subroutine) {
353 ErrorCategory.Report(
354 category: "Call site nested entry within inlined subroutine", detailCallback: [&]() {
355 error() << "Call site entry nested within inlined subroutine:";
356 Curr.dump(OS);
357 });
358 return 1;
359 }
360 }
361
362 if (!Curr.isValid()) {
363 ErrorCategory.Report(
364 category: "Call site entry not nested within valid subprogram", detailCallback: [&]() {
365 error() << "Call site entry not nested within a valid subprogram:";
366 Die.dump(OS);
367 });
368 return 1;
369 }
370
371 std::optional<DWARFFormValue> CallAttr = Curr.find(
372 Attrs: {DW_AT_call_all_calls, DW_AT_call_all_source_calls,
373 DW_AT_call_all_tail_calls, DW_AT_GNU_all_call_sites,
374 DW_AT_GNU_all_source_call_sites, DW_AT_GNU_all_tail_call_sites});
375 if (!CallAttr) {
376 ErrorCategory.Report(
377 category: "Subprogram with call site entry has no DW_AT_call attribute", detailCallback: [&]() {
378 error()
379 << "Subprogram with call site entry has no DW_AT_call attribute:";
380 Curr.dump(OS);
381 Die.dump(OS, /*indent*/ 1);
382 });
383 return 1;
384 }
385
386 return 0;
387}
388
389unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
390 if (!Abbrev)
391 return 0;
392
393 Expected<const DWARFAbbreviationDeclarationSet *> AbbrDeclsOrErr =
394 Abbrev->getAbbreviationDeclarationSet(CUAbbrOffset: 0);
395 if (!AbbrDeclsOrErr) {
396 std::string ErrMsg = toString(E: AbbrDeclsOrErr.takeError());
397 ErrorCategory.Report(category: "Abbreviation Declaration error",
398 detailCallback: [&]() { error() << ErrMsg << "\n"; });
399 return 1;
400 }
401
402 const auto *AbbrDecls = *AbbrDeclsOrErr;
403 unsigned NumErrors = 0;
404 for (auto AbbrDecl : *AbbrDecls) {
405 SmallDenseSet<uint16_t> AttributeSet;
406 for (auto Attribute : AbbrDecl.attributes()) {
407 auto Result = AttributeSet.insert(V: Attribute.Attr);
408 if (!Result.second) {
409 ErrorCategory.Report(
410 category: "Abbreviation declartion contains multiple attributes", detailCallback: [&]() {
411 error() << formatv(Fmt: "Abbreviation declaration contains multiple "
412 "{0} attributes.\n",
413 Vals: AttributeString(Attribute: Attribute.Attr));
414 AbbrDecl.dump(OS);
415 });
416 ++NumErrors;
417 }
418 }
419 }
420 return NumErrors;
421}
422
423bool DWARFVerifier::handleDebugAbbrev() {
424 OS << "Verifying .debug_abbrev...\n";
425
426 const DWARFObject &DObj = DCtx.getDWARFObj();
427 unsigned NumErrors = 0;
428 if (!DObj.getAbbrevSection().empty())
429 NumErrors += verifyAbbrevSection(Abbrev: DCtx.getDebugAbbrev());
430 if (!DObj.getAbbrevDWOSection().empty())
431 NumErrors += verifyAbbrevSection(Abbrev: DCtx.getDebugAbbrevDWO());
432
433 return NumErrors == 0;
434}
435
436unsigned DWARFVerifier::verifyUnits(const DWARFUnitVector &Units) {
437 unsigned NumDebugInfoErrors = 0;
438 ReferenceMap CrossUnitReferences;
439
440 unsigned Index = 1;
441
442 for (const auto &Unit : Units) {
443 OS << formatv(Fmt: "Verifying unit: {0} / {1}", Vals&: Index, Vals: Units.getNumUnits());
444 if (const char *Name = Unit->getUnitDIE(ExtractUnitDIEOnly: true).getShortName())
445 OS << formatv(Fmt: ", \"{0}\"", Vals&: Name);
446 OS << '\n';
447 OS.flush();
448 ReferenceMap UnitLocalReferences;
449 NumDebugInfoErrors +=
450 verifyUnitContents(Unit&: *Unit, UnitLocalReferences, CrossUnitReferences);
451 NumDebugInfoErrors += verifyDebugInfoReferences(
452 UnitLocalReferences, GetUnitForDieOffset: [&](uint64_t Offset) { return Unit.get(); });
453 ++Index;
454 }
455
456 NumDebugInfoErrors += verifyDebugInfoReferences(
457 CrossUnitReferences, GetUnitForDieOffset: [&](uint64_t Offset) -> DWARFUnit * {
458 if (DWARFUnit *U = Units.getUnitForOffset(Offset))
459 return U;
460 return nullptr;
461 });
462
463 return NumDebugInfoErrors;
464}
465
466unsigned DWARFVerifier::verifyUnitSection(const DWARFSection &S) {
467 const DWARFObject &DObj = DCtx.getDWARFObj();
468 DWARFDataExtractor DebugInfoData(DObj, S, DCtx.isLittleEndian(), 0);
469 unsigned NumDebugInfoErrors = 0;
470 uint64_t Offset = 0, UnitIdx = 0;
471 uint8_t UnitType = 0;
472 bool isUnitDWARF64 = false;
473 bool isHeaderChainValid = true;
474 bool hasDIE = DebugInfoData.isValidOffset(offset: Offset);
475 DWARFUnitVector TypeUnitVector;
476 DWARFUnitVector CompileUnitVector;
477 while (hasDIE) {
478 if (!verifyUnitHeader(DebugInfoData, Offset: &Offset, UnitIndex: UnitIdx, UnitType,
479 isUnitDWARF64)) {
480 isHeaderChainValid = false;
481 if (isUnitDWARF64)
482 break;
483 }
484 hasDIE = DebugInfoData.isValidOffset(offset: Offset);
485 ++UnitIdx;
486 }
487 if (UnitIdx == 0 && !hasDIE) {
488 warn() << "Section is empty.\n";
489 isHeaderChainValid = true;
490 }
491 if (!isHeaderChainValid)
492 ++NumDebugInfoErrors;
493 return NumDebugInfoErrors;
494}
495
496unsigned DWARFVerifier::verifyIndex(StringRef Name,
497 DWARFSectionKind InfoColumnKind,
498 StringRef IndexStr) {
499 if (IndexStr.empty())
500 return 0;
501 OS << "Verifying " << Name << "...\n";
502 DWARFUnitIndex Index(InfoColumnKind);
503 DataExtractor D(IndexStr, DCtx.isLittleEndian(), 0);
504 if (!Index.parse(IndexData: D))
505 return 1;
506 using MapType = IntervalMap<uint64_t, uint64_t>;
507 MapType::Allocator Alloc;
508 std::vector<std::unique_ptr<MapType>> Sections(Index.getColumnKinds().size());
509 for (const DWARFUnitIndex::Entry &E : Index.getRows()) {
510 uint64_t Sig = E.getSignature();
511 if (!E.getContributions())
512 continue;
513 for (auto E : enumerate(
514 First: InfoColumnKind == DW_SECT_INFO
515 ? ArrayRef(E.getContributions(), Index.getColumnKinds().size())
516 : ArrayRef(E.getContribution(), 1))) {
517 const DWARFUnitIndex::Entry::SectionContribution &SC = E.value();
518 int Col = E.index();
519 if (SC.getLength() == 0)
520 continue;
521 if (!Sections[Col])
522 Sections[Col] = std::make_unique<MapType>(args&: Alloc);
523 auto &M = *Sections[Col];
524 auto I = M.find(x: SC.getOffset());
525 if (I != M.end() && I.start() < (SC.getOffset() + SC.getLength())) {
526 StringRef Category = InfoColumnKind == DWARFSectionKind::DW_SECT_INFO
527 ? "Overlapping CU index entries"
528 : "Overlapping TU index entries";
529 ErrorCategory.Report(category: Category, detailCallback: [&]() {
530 error() << llvm::formatv(
531 Fmt: "overlapping index entries for entries {0:x16} "
532 "and {1:x16} for column {2}\n",
533 Vals: *I, Vals&: Sig, Vals: toString(Kind: Index.getColumnKinds()[Col]));
534 });
535 return 1;
536 }
537 M.insert(a: SC.getOffset(), b: SC.getOffset() + SC.getLength() - 1, y: Sig);
538 }
539 }
540
541 return 0;
542}
543
544bool DWARFVerifier::handleDebugCUIndex() {
545 return verifyIndex(Name: ".debug_cu_index", InfoColumnKind: DWARFSectionKind::DW_SECT_INFO,
546 IndexStr: DCtx.getDWARFObj().getCUIndexSection()) == 0;
547}
548
549bool DWARFVerifier::handleDebugTUIndex() {
550 return verifyIndex(Name: ".debug_tu_index", InfoColumnKind: DWARFSectionKind::DW_SECT_EXT_TYPES,
551 IndexStr: DCtx.getDWARFObj().getTUIndexSection()) == 0;
552}
553
554bool DWARFVerifier::handleDebugInfo() {
555 const DWARFObject &DObj = DCtx.getDWARFObj();
556 unsigned NumErrors = 0;
557
558 OS << "Verifying .debug_info Unit Header Chain...\n";
559 DObj.forEachInfoSections(
560 F: [&](const DWARFSection &S) { NumErrors += verifyUnitSection(S); });
561
562 OS << "Verifying .debug_types Unit Header Chain...\n";
563 DObj.forEachTypesSections(
564 F: [&](const DWARFSection &S) { NumErrors += verifyUnitSection(S); });
565
566 OS << "Verifying non-dwo Units...\n";
567 NumErrors += verifyUnits(Units: DCtx.getNormalUnitsVector());
568
569 OS << "Verifying dwo Units...\n";
570 NumErrors += verifyUnits(Units: DCtx.getDWOUnitsVector());
571 return NumErrors == 0;
572}
573
574unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
575 DieRangeInfo &ParentRI) {
576 unsigned NumErrors = 0;
577
578 if (!Die.isValid())
579 return NumErrors;
580
581 DWARFUnit *Unit = Die.getDwarfUnit();
582
583 auto RangesOrError = Die.getAddressRanges();
584 if (!RangesOrError) {
585 // FIXME: Report the error.
586 if (!Unit->isDWOUnit())
587 ++NumErrors;
588 llvm::consumeError(Err: RangesOrError.takeError());
589 return NumErrors;
590 }
591
592 const DWARFAddressRangesVector &Ranges = RangesOrError.get();
593 // Build RI for this DIE and check that ranges within this DIE do not
594 // overlap.
595 DieRangeInfo RI(Die);
596
597 // TODO support object files better
598 //
599 // Some object file formats (i.e. non-MachO) support COMDAT. ELF in
600 // particular does so by placing each function into a section. The DWARF data
601 // for the function at that point uses a section relative DW_FORM_addrp for
602 // the DW_AT_low_pc and a DW_FORM_data4 for the offset as the DW_AT_high_pc.
603 // In such a case, when the Die is the CU, the ranges will overlap, and we
604 // will flag valid conflicting ranges as invalid.
605 //
606 // For such targets, we should read the ranges from the CU and partition them
607 // by the section id. The ranges within a particular section should be
608 // disjoint, although the ranges across sections may overlap. We would map
609 // the child die to the entity that it references and the section with which
610 // it is associated. The child would then be checked against the range
611 // information for the associated section.
612 //
613 // For now, simply elide the range verification for the CU DIEs if we are
614 // processing an object file.
615
616 if (!IsObjectFile || IsMachOObject || Die.getTag() != DW_TAG_compile_unit) {
617 bool DumpDieAfterError = false;
618 for (const auto &Range : Ranges) {
619 if (!Range.valid()) {
620 ++NumErrors;
621 ErrorCategory.Report(category: "Invalid address range", detailCallback: [&]() {
622 error() << formatv(Fmt: "Invalid address range {0}\n", Vals: Range);
623 DumpDieAfterError = true;
624 });
625 continue;
626 }
627
628 // Verify that ranges don't intersect and also build up the DieRangeInfo
629 // address ranges. Don't break out of the loop below early, or we will
630 // think this DIE doesn't have all of the address ranges it is supposed
631 // to have. Compile units often have DW_AT_ranges that can contain one or
632 // more dead stripped address ranges which tend to all be at the same
633 // address: 0 or -1.
634 if (auto PrevRange = RI.insert(R: Range)) {
635 ++NumErrors;
636 ErrorCategory.Report(category: "DIE has overlapping DW_AT_ranges", detailCallback: [&]() {
637 error() << formatv(Fmt: "DIE has overlapping ranges in DW_AT_ranges "
638 "attribute: {0} and {1}\n",
639 Vals&: *PrevRange, Vals: Range);
640 DumpDieAfterError = true;
641 });
642 }
643 }
644 if (DumpDieAfterError)
645 dump(Die, indent: 2) << '\n';
646 }
647
648 // Verify that children don't intersect.
649 const auto IntersectingChild = ParentRI.insert(RI);
650 if (IntersectingChild != ParentRI.Children.end()) {
651 ++NumErrors;
652 ErrorCategory.Report(category: "DIEs have overlapping address ranges", detailCallback: [&]() {
653 error() << "DIEs have overlapping address ranges:";
654 dump(Die);
655 dump(Die: IntersectingChild->Die) << '\n';
656 });
657 }
658
659 // Verify that ranges are contained within their parent.
660 bool ShouldBeContained = !RI.Ranges.empty() && !ParentRI.Ranges.empty() &&
661 !(Die.getTag() == DW_TAG_subprogram &&
662 ParentRI.Die.getTag() == DW_TAG_subprogram);
663 if (ShouldBeContained && !ParentRI.contains(RHS: RI)) {
664 ++NumErrors;
665 ErrorCategory.Report(
666 category: "DIE address ranges are not contained by parent ranges", detailCallback: [&]() {
667 error()
668 << "DIE address ranges are not contained in its parent's ranges:";
669 dump(Die: ParentRI.Die);
670 dump(Die, indent: 2) << '\n';
671 });
672 }
673
674 // Recursively check children.
675 for (DWARFDie Child : Die)
676 NumErrors += verifyDieRanges(Die: Child, ParentRI&: RI);
677
678 return NumErrors;
679}
680
681bool DWARFVerifier::verifyExpressionOp(const DWARFExpression::Operation &Op,
682 DWARFUnit *U) {
683 for (unsigned Operand = 0; Operand < Op.Desc.Op.size(); ++Operand) {
684 unsigned Size = Op.Desc.Op[Operand];
685
686 if (Size == DWARFExpression::Operation::BaseTypeRef) {
687 // For DW_OP_convert the operand may be 0 to indicate that conversion to
688 // the generic type should be done, so don't look up a base type in that
689 // case. The same holds for DW_OP_reinterpret, which is currently not
690 // supported.
691 if (Op.Opcode == DW_OP_convert && Op.Operands[Operand] == 0)
692 continue;
693 auto Die = U->getDIEForOffset(Offset: U->getOffset() + Op.Operands[Operand]);
694 if (!Die || Die.getTag() != dwarf::DW_TAG_base_type)
695 return false;
696 }
697 }
698
699 return true;
700}
701
702bool DWARFVerifier::verifyExpression(const DWARFExpression &E, DWARFUnit *U) {
703 for (auto &Op : E)
704 if (!verifyExpressionOp(Op, U))
705 return false;
706
707 return true;
708}
709
710unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
711 DWARFAttribute &AttrValue) {
712 unsigned NumErrors = 0;
713 auto ReportError = [&](StringRef category, const Twine &TitleMsg) {
714 ++NumErrors;
715 ErrorCategory.Report(category, detailCallback: [&]() {
716 error() << formatv(Fmt: "{0}\n", Vals: TitleMsg);
717 dump(Die) << '\n';
718 });
719 };
720
721 const DWARFObject &DObj = DCtx.getDWARFObj();
722 DWARFUnit *U = Die.getDwarfUnit();
723 const auto Attr = AttrValue.Attr;
724 switch (Attr) {
725 case DW_AT_ranges:
726 // Make sure the offset in the DW_AT_ranges attribute is valid.
727 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
728 unsigned DwarfVersion = U->getVersion();
729 const DWARFSection &RangeSection = DwarfVersion < 5
730 ? DObj.getRangesSection()
731 : DObj.getRnglistsSection();
732 if (U->isDWOUnit() && RangeSection.Data.empty())
733 break;
734 if (*SectionOffset >= RangeSection.Data.size())
735 ReportError(
736 "DW_AT_ranges offset out of bounds",
737 llvm::formatv(Fmt: "DW_AT_ranges offset is beyond {0} bounds: {1:x8}",
738 Vals: StringRef(DwarfVersion < 5 ? ".debug_ranges"
739 : ".debug_rnglists"),
740 Vals&: *SectionOffset));
741 break;
742 }
743 ReportError("Invalid DW_AT_ranges encoding",
744 "DIE has invalid DW_AT_ranges encoding:");
745 break;
746 case DW_AT_stmt_list:
747 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
748 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
749 if (*SectionOffset >= U->getLineSection().Data.size())
750 ReportError(
751 "DW_AT_stmt_list offset out of bounds",
752 llvm::formatv(
753 Fmt: "DW_AT_stmt_list offset is beyond .debug_line bounds: {0:x8}",
754 Vals&: *SectionOffset));
755 break;
756 }
757 ReportError("Invalid DW_AT_stmt_list encoding",
758 "DIE has invalid DW_AT_stmt_list encoding:");
759 break;
760 case DW_AT_location: {
761 // FIXME: It might be nice if there's a way to walk location expressions
762 // without trying to resolve the address ranges - it'd be a more efficient
763 // API (since the API is currently unnecessarily resolving addresses for
764 // this use case which only wants to validate the expressions themselves) &
765 // then the expressions could be validated even if the addresses can't be
766 // resolved.
767 // That sort of API would probably look like a callback "for each
768 // expression" with some way to lazily resolve the address ranges when
769 // needed (& then the existing API used here could be built on top of that -
770 // using the callback API to build the data structure and return it).
771 if (Expected<std::vector<DWARFLocationExpression>> Loc =
772 Die.getLocations(Attr: DW_AT_location)) {
773 for (const auto &Entry : *Loc) {
774 DataExtractor Data(toStringRef(Input: Entry.Expr), DCtx.isLittleEndian(), 0);
775 DWARFExpression Expression(Data, U->getAddressByteSize(),
776 U->getFormParams().Format);
777 bool Error =
778 any_of(Range&: Expression, P: [](const DWARFExpression::Operation &Op) {
779 return Op.isError();
780 });
781 if (Error || !verifyExpression(E: Expression, U))
782 ReportError("Invalid DWARF expressions",
783 "DIE contains invalid DWARF expression:");
784 }
785 } else if (Error Err = handleErrors(
786 E: Loc.takeError(), Hs: [&](std::unique_ptr<ResolverError> E) {
787 return U->isDWOUnit() ? Error::success()
788 : Error(std::move(E));
789 }))
790 ReportError("Invalid DW_AT_location", toString(E: std::move(Err)));
791 break;
792 }
793 case DW_AT_specification:
794 case DW_AT_abstract_origin: {
795 if (auto ReferencedDie = Die.getAttributeValueAsReferencedDie(Attr)) {
796 auto DieTag = Die.getTag();
797 auto RefTag = ReferencedDie.getTag();
798 if (DieTag == RefTag)
799 break;
800 if (DieTag == DW_TAG_inlined_subroutine && RefTag == DW_TAG_subprogram)
801 break;
802 if (DieTag == DW_TAG_variable && RefTag == DW_TAG_member)
803 break;
804 // This might be reference to a function declaration.
805 if (DieTag == DW_TAG_GNU_call_site && RefTag == DW_TAG_subprogram)
806 break;
807 ReportError("Incompatible DW_AT_abstract_origin tag reference",
808 formatv(Fmt: "DIE with tag {0} has {1} that points to DIE with "
809 "incompatible tag {2}",
810 Vals: TagString(Tag: DieTag), Vals: AttributeString(Attribute: Attr),
811 Vals: TagString(Tag: RefTag)));
812 }
813 break;
814 }
815 case DW_AT_type: {
816 DWARFDie TypeDie = Die.getAttributeValueAsReferencedDie(Attr: DW_AT_type);
817 if (TypeDie && !isType(T: TypeDie.getTag())) {
818 ReportError("Incompatible DW_AT_type attribute tag",
819 formatv(Fmt: "DIE has {0} with incompatible tag {1}",
820 Vals: AttributeString(Attribute: Attr), Vals: TagString(Tag: TypeDie.getTag())));
821 }
822 break;
823 }
824 case DW_AT_call_file:
825 case DW_AT_decl_file: {
826 if (auto FileIdx = AttrValue.Value.getAsUnsignedConstant()) {
827 if (U->isDWOUnit() && !U->isTypeUnit())
828 break;
829 const auto *LT = U->getContext().getLineTableForUnit(U);
830 if (LT) {
831 if (!LT->hasFileAtIndex(FileIndex: *FileIdx)) {
832 bool IsZeroIndexed = LT->Prologue.getVersion() >= 5;
833 if (std::optional<uint64_t> LastFileIdx =
834 LT->getLastValidFileIndex()) {
835 ReportError("Invalid file index in DW_AT_decl_file",
836 llvm::formatv(Fmt: "DIE has {0} with an invalid file index "
837 "{1} (valid values are [{2}-{3}])",
838 Vals: AttributeString(Attribute: Attr), Vals&: *FileIdx,
839 Vals: (IsZeroIndexed ? "0" : "1"),
840 Vals&: *LastFileIdx));
841 } else {
842 ReportError(
843 "Invalid file index in DW_AT_decl_file",
844 llvm::formatv(Fmt: "DIE has {0} with an invalid file index {1} (the "
845 "file table in the prologue is empty)",
846 Vals: AttributeString(Attribute: Attr), Vals&: *FileIdx));
847 }
848 }
849 } else {
850 ReportError(
851 "File index in DW_AT_decl_file reference CU with no line table",
852 llvm::formatv(Fmt: "DIE has {0} that references a file with index {1} "
853 "and the compile unit has no line table",
854 Vals: AttributeString(Attribute: Attr), Vals&: *FileIdx));
855 }
856 } else {
857 ReportError("Invalid encoding in DW_AT_decl_file",
858 llvm::formatv(Fmt: "DIE has {0} with invalid encoding",
859 Vals: AttributeString(Attribute: Attr)));
860 }
861 break;
862 }
863 case DW_AT_call_line:
864 case DW_AT_decl_line: {
865 if (!AttrValue.Value.getAsUnsignedConstant()) {
866 ReportError(
867 Attr == DW_AT_call_line ? "Invalid file index in DW_AT_decl_line"
868 : "Invalid file index in DW_AT_call_line",
869 formatv(Fmt: "DIE has {0} with invalid encoding", Vals: AttributeString(Attribute: Attr)));
870 }
871 break;
872 }
873 case DW_AT_LLVM_stmt_sequence: {
874 // Make sure the offset in the DW_AT_LLVM_stmt_sequence attribute is valid
875 // and points to a valid sequence offset in the line table.
876 auto SectionOffset = AttrValue.Value.getAsSectionOffset();
877 if (!SectionOffset) {
878 ReportError("Invalid DW_AT_LLVM_stmt_sequence encoding",
879 "DIE has invalid DW_AT_LLVM_stmt_sequence encoding");
880 break;
881 }
882 if (*SectionOffset >= U->getLineSection().Data.size()) {
883 ReportError(
884 "DW_AT_LLVM_stmt_sequence offset out of bounds",
885 "DW_AT_LLVM_stmt_sequence offset is beyond .debug_line bounds: " +
886 llvm::formatv(Fmt: "{0:x8}", Vals&: *SectionOffset));
887 break;
888 }
889
890 // Get the line table for this unit to validate bounds
891 const auto *LineTable = DCtx.getLineTableForUnit(U);
892 if (!LineTable) {
893 ReportError("DW_AT_LLVM_stmt_sequence without line table",
894 "DIE has DW_AT_LLVM_stmt_sequence but compile unit has no "
895 "line table");
896 break;
897 }
898
899 // Get the DW_AT_stmt_list offset from the compile unit DIE
900 DWARFDie CUDie = U->getUnitDIE();
901 auto StmtListOffset = toSectionOffset(V: CUDie.find(Attr: DW_AT_stmt_list));
902 if (!StmtListOffset) {
903 ReportError("DW_AT_LLVM_stmt_sequence without DW_AT_stmt_list",
904 "DIE has DW_AT_LLVM_stmt_sequence but compile unit has no "
905 "DW_AT_stmt_list");
906 break;
907 }
908
909 const int8_t DwarfOffset =
910 LineTable->Prologue.getFormParams().getDwarfOffsetByteSize();
911 // Calculate the bounds of this specific line table
912 uint64_t LineTableStart = *StmtListOffset;
913 uint64_t PrologueLength = LineTable->Prologue.PrologueLength;
914 uint64_t TotalLength = LineTable->Prologue.TotalLength;
915 uint64_t LineTableEnd = LineTableStart + TotalLength + DwarfOffset;
916
917 // See DWARF definition for this, the following three do not
918 // count toward prologue length. Calculate SequencesStart correctly
919 // according to DWARF specification:
920 uint64_t InitialLengthSize = DwarfOffset;
921 // Version field is always 2 bytes
922 uint64_t VersionSize = 2;
923 uint64_t PrologueLengthSize = DwarfOffset;
924 uint64_t SequencesStart = LineTableStart + InitialLengthSize + VersionSize +
925 PrologueLengthSize + PrologueLength;
926
927 // Check if the offset is within the bounds of this specific line table
928 if (*SectionOffset < SequencesStart || *SectionOffset >= LineTableEnd) {
929 ReportError("DW_AT_LLVM_stmt_sequence offset out of line table bounds",
930 llvm::formatv(Fmt: "DW_AT_LLVM_stmt_sequence offset {0:x8} is not "
931 "within the line table bounds [{1:x8}, {2:x8})",
932 Vals&: *SectionOffset, Vals&: SequencesStart, Vals&: LineTableEnd));
933 break;
934 }
935
936 // Check if the offset matches any of the sequence offset.
937 auto It = llvm::find_if(Range: LineTable->Sequences,
938 P: [SectionOffset](const auto &Sequence) {
939 return Sequence.StmtSeqOffset == *SectionOffset;
940 });
941
942 if (It == LineTable->Sequences.end())
943 ReportError(
944 "Invalid DW_AT_LLVM_stmt_sequence offset",
945 llvm::formatv(Fmt: "DW_AT_LLVM_stmt_sequence offset {0:x8} does not point "
946 "to a valid sequence offset in the line table",
947 Vals&: *SectionOffset));
948 break;
949 }
950 default:
951 break;
952 }
953 return NumErrors;
954}
955
956unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
957 DWARFAttribute &AttrValue,
958 ReferenceMap &LocalReferences,
959 ReferenceMap &CrossUnitReferences) {
960 auto DieCU = Die.getDwarfUnit();
961 unsigned NumErrors = 0;
962 const auto Form = AttrValue.Value.getForm();
963 switch (Form) {
964 case DW_FORM_ref1:
965 case DW_FORM_ref2:
966 case DW_FORM_ref4:
967 case DW_FORM_ref8:
968 case DW_FORM_ref_udata: {
969 // Verify all CU relative references are valid CU offsets.
970 std::optional<uint64_t> RefVal = AttrValue.Value.getAsRelativeReference();
971 assert(RefVal);
972 if (RefVal) {
973 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
974 auto CUOffset = AttrValue.Value.getRawUValue();
975 if (CUOffset >= CUSize) {
976 ++NumErrors;
977 ErrorCategory.Report(category: "Invalid CU offset", detailCallback: [&]() {
978 error() << formatv(Fmt: "{0} CU offset {1:x+8} is invalid (must be less "
979 "than CU size of {2:x+8}):\n",
980 Vals: FormEncodingString(Encoding: Form), Vals&: CUOffset, Vals&: CUSize);
981 Die.dump(OS, indent: 0, DumpOpts);
982 dump(Die) << '\n';
983 });
984 } else {
985 // Valid reference, but we will verify it points to an actual
986 // DIE later.
987 LocalReferences[AttrValue.Value.getUnit()->getOffset() + *RefVal]
988 .insert(x: Die.getOffset());
989 }
990 }
991 break;
992 }
993 case DW_FORM_ref_addr: {
994 // Verify all absolute DIE references have valid offsets in the
995 // .debug_info section.
996 std::optional<uint64_t> RefVal = AttrValue.Value.getAsDebugInfoReference();
997 assert(RefVal);
998 if (RefVal) {
999 if (*RefVal >= DieCU->getInfoSection().Data.size()) {
1000 ++NumErrors;
1001 ErrorCategory.Report(category: "DW_FORM_ref_addr offset out of bounds", detailCallback: [&]() {
1002 error() << "DW_FORM_ref_addr offset beyond .debug_info "
1003 "bounds:\n";
1004 dump(Die) << '\n';
1005 });
1006 } else {
1007 // Valid reference, but we will verify it points to an actual
1008 // DIE later.
1009 CrossUnitReferences[*RefVal].insert(x: Die.getOffset());
1010 }
1011 }
1012 break;
1013 }
1014 case DW_FORM_strp:
1015 case DW_FORM_strx:
1016 case DW_FORM_strx1:
1017 case DW_FORM_strx2:
1018 case DW_FORM_strx3:
1019 case DW_FORM_strx4:
1020 case DW_FORM_line_strp: {
1021 if (Error E = AttrValue.Value.getAsCString().takeError()) {
1022 ++NumErrors;
1023 std::string ErrMsg = toString(E: std::move(E));
1024 ErrorCategory.Report(category: "Invalid DW_FORM attribute", detailCallback: [&]() {
1025 error() << formatv(Fmt: "{0}:\n", Vals&: ErrMsg);
1026 dump(Die) << '\n';
1027 });
1028 }
1029 break;
1030 }
1031 default:
1032 break;
1033 }
1034 return NumErrors;
1035}
1036
1037unsigned DWARFVerifier::verifyDebugInfoReferences(
1038 const ReferenceMap &References,
1039 llvm::function_ref<DWARFUnit *(uint64_t)> GetUnitForOffset) {
1040 auto GetDIEForOffset = [&](uint64_t Offset) {
1041 if (DWARFUnit *U = GetUnitForOffset(Offset))
1042 return U->getDIEForOffset(Offset);
1043 return DWARFDie();
1044 };
1045 unsigned NumErrors = 0;
1046 for (const std::pair<const uint64_t, std::set<uint64_t>> &Pair : References) {
1047 if (GetDIEForOffset(Pair.first))
1048 continue;
1049 ++NumErrors;
1050 ErrorCategory.Report(category: "Invalid DIE reference", detailCallback: [&]() {
1051 error() << formatv(
1052 Fmt: "invalid DIE reference {0:x+8}. Offset is in between DIEs:\n",
1053 Vals: Pair.first);
1054 for (auto Offset : Pair.second)
1055 dump(Die: GetDIEForOffset(Offset)) << '\n';
1056 OS << "\n";
1057 });
1058 }
1059 return NumErrors;
1060}
1061
1062void DWARFVerifier::verifyDebugLineStmtOffsets() {
1063 std::map<uint64_t, DWARFDie> StmtListToDie;
1064 for (const auto &CU : DCtx.compile_units()) {
1065 auto Die = CU->getUnitDIE();
1066 // Get the attribute value as a section offset. No need to produce an
1067 // error here if the encoding isn't correct because we validate this in
1068 // the .debug_info verifier.
1069 auto StmtSectionOffset = toSectionOffset(V: Die.find(Attr: DW_AT_stmt_list));
1070 if (!StmtSectionOffset)
1071 continue;
1072 const uint64_t LineTableOffset = *StmtSectionOffset;
1073 auto LineTable = DCtx.getLineTableForUnit(U: CU.get());
1074 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
1075 if (!LineTable) {
1076 ++NumDebugLineErrors;
1077 ErrorCategory.Report(category: "Unparsable .debug_line entry", detailCallback: [&]() {
1078 error() << formatv(
1079 Fmt: ".debug_line[{0:x+8}] was not able to be parsed for CU:\n",
1080 Vals: LineTableOffset);
1081 dump(Die) << '\n';
1082 });
1083 continue;
1084 }
1085 } else {
1086 // Make sure we don't get a valid line table back if the offset is wrong.
1087 assert(LineTable == nullptr);
1088 // Skip this line table as it isn't valid. No need to create an error
1089 // here because we validate this in the .debug_info verifier.
1090 continue;
1091 }
1092 auto [Iter, Inserted] = StmtListToDie.try_emplace(k: LineTableOffset, args&: Die);
1093 if (!Inserted) {
1094 ++NumDebugLineErrors;
1095 const auto &OldDie = Iter->second;
1096 ErrorCategory.Report(category: "Identical DW_AT_stmt_list section offset", detailCallback: [&]() {
1097 error() << formatv(Fmt: "two compile unit DIEs, {0:x+8} and {1:x+8}, have "
1098 "the same DW_AT_stmt_list section offset:\n",
1099 Vals: OldDie.getOffset(), Vals: Die.getOffset());
1100 dump(Die: OldDie);
1101 dump(Die) << '\n';
1102 });
1103 // Already verified this line table before, no need to do it again.
1104 }
1105 }
1106}
1107
1108void DWARFVerifier::verifyDebugLineRows() {
1109 for (const auto &CU : DCtx.compile_units()) {
1110 auto Die = CU->getUnitDIE();
1111 auto LineTable = DCtx.getLineTableForUnit(U: CU.get());
1112 // If there is no line table we will have created an error in the
1113 // .debug_info verifier or in verifyDebugLineStmtOffsets().
1114 if (!LineTable)
1115 continue;
1116
1117 // Verify prologue.
1118 bool isDWARF5 = LineTable->Prologue.getVersion() >= 5;
1119 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
1120 uint32_t MinFileIndex = isDWARF5 ? 0 : 1;
1121 uint32_t FileIndex = MinFileIndex;
1122 StringMap<uint16_t> FullPathMap;
1123 for (const auto &FileName : LineTable->Prologue.FileNames) {
1124 // Verify directory index.
1125 if (FileName.DirIdx > MaxDirIndex) {
1126 ++NumDebugLineErrors;
1127 ErrorCategory.Report(
1128 category: "Invalid index in .debug_line->prologue.file_names->dir_idx",
1129 detailCallback: [&]() {
1130 error() << formatv(Fmt: ".debug_line[{0:x+8}].prologue.file_names[{1}]"
1131 ".dir_idx contains an invalid index: {2}\n",
1132 Vals: *toSectionOffset(V: Die.find(Attr: DW_AT_stmt_list)),
1133 Vals&: FileIndex, Vals: FileName.DirIdx);
1134 });
1135 }
1136
1137 // Check file paths for duplicates.
1138 std::string FullPath;
1139 const bool HasFullPath = LineTable->getFileNameByIndex(
1140 FileIndex, CompDir: CU->getCompilationDir(),
1141 Kind: DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Result&: FullPath);
1142 assert(HasFullPath && "Invalid index?");
1143 (void)HasFullPath;
1144 auto [It, Inserted] = FullPathMap.try_emplace(Key: FullPath, Args&: FileIndex);
1145 if (!Inserted && It->second != FileIndex && DumpOpts.Verbose) {
1146 warn() << formatv(Fmt: ".debug_line[{0:x+8}].prologue.file_names[{1}] is a "
1147 "duplicate of file_names[{2}]\n",
1148 Vals: *toSectionOffset(V: Die.find(Attr: DW_AT_stmt_list)),
1149 Vals&: FileIndex, Vals&: It->second);
1150 }
1151
1152 FileIndex++;
1153 }
1154
1155 // Nothing to verify in a line table with a single row containing the end
1156 // sequence.
1157 if (LineTable->Rows.size() == 1 && LineTable->Rows.front().EndSequence)
1158 continue;
1159
1160 // Verify rows.
1161 uint64_t PrevAddress = 0;
1162 uint32_t RowIndex = 0;
1163 for (const auto &Row : LineTable->Rows) {
1164 // Verify row address.
1165 if (Row.Address.Address < PrevAddress) {
1166 ++NumDebugLineErrors;
1167 ErrorCategory.Report(
1168 category: "decreasing address between debug_line rows", detailCallback: [&]() {
1169 error() << formatv(Fmt: ".debug_line[{0:x+8}] row[{1}] decreases in "
1170 "address from previous row:\n",
1171 Vals: *toSectionOffset(V: Die.find(Attr: DW_AT_stmt_list)),
1172 Vals&: RowIndex);
1173
1174 DWARFDebugLine::Row::dumpTableHeader(OS, Indent: 0);
1175 if (RowIndex > 0)
1176 LineTable->Rows[RowIndex - 1].dump(OS);
1177 Row.dump(OS);
1178 OS << '\n';
1179 });
1180 }
1181
1182 if (!LineTable->hasFileAtIndex(FileIndex: Row.File)) {
1183 ++NumDebugLineErrors;
1184 ErrorCategory.Report(category: "Invalid file index in debug_line", detailCallback: [&]() {
1185 error() << formatv(Fmt: ".debug_line[{0:x+8}][{1}] has invalid file index "
1186 "{2} (valid values are [{3},{4}{5}):\n",
1187 Vals: *toSectionOffset(V: Die.find(Attr: DW_AT_stmt_list)),
1188 Vals&: RowIndex, Vals: Row.File, Vals&: MinFileIndex,
1189 Vals: LineTable->Prologue.FileNames.size(),
1190 Vals: (isDWARF5 ? ")" : "]"));
1191 DWARFDebugLine::Row::dumpTableHeader(OS, Indent: 0);
1192 Row.dump(OS);
1193 OS << '\n';
1194 });
1195 }
1196 if (Row.EndSequence)
1197 PrevAddress = 0;
1198 else
1199 PrevAddress = Row.Address.Address;
1200 ++RowIndex;
1201 }
1202 }
1203}
1204
1205DWARFVerifier::DWARFVerifier(raw_ostream &S, DWARFContext &D,
1206 DIDumpOptions DumpOpts)
1207 : OS(S), DCtx(D), DumpOpts(std::move(DumpOpts)), IsObjectFile(false),
1208 IsMachOObject(false) {
1209 ErrorCategory.ShowDetail(showDetail: this->DumpOpts.Verbose ||
1210 !this->DumpOpts.ShowAggregateErrors);
1211 if (const auto *F = DCtx.getDWARFObj().getFile()) {
1212 IsObjectFile = F->isRelocatableObject();
1213 IsMachOObject = F->isMachO();
1214 }
1215}
1216
1217bool DWARFVerifier::handleDebugLine() {
1218 NumDebugLineErrors = 0;
1219 OS << "Verifying .debug_line...\n";
1220 verifyDebugLineStmtOffsets();
1221 verifyDebugLineRows();
1222 return NumDebugLineErrors == 0;
1223}
1224
1225void DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
1226 DataExtractor *StrData,
1227 const char *SectionName) {
1228 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
1229 DCtx.isLittleEndian(), 0);
1230 AppleAcceleratorTable AccelTable(AccelSectionData, *StrData);
1231
1232 OS << "Verifying " << SectionName << "...\n";
1233
1234 // Verify that the fixed part of the header is not too short.
1235 if (!AccelSectionData.isValidOffset(offset: AccelTable.getSizeHdr())) {
1236 ErrorCategory.Report(category: "Section is too small to fit a section header", detailCallback: [&]() {
1237 error() << "Section is too small to fit a section header.\n";
1238 });
1239 return;
1240 }
1241
1242 // Verify that the section is not too short.
1243 if (Error E = AccelTable.extract()) {
1244 std::string Msg = toString(E: std::move(E));
1245 ErrorCategory.Report(category: "Section is too small to fit a section header",
1246 detailCallback: [&]() { error() << Msg << '\n'; });
1247 return;
1248 }
1249
1250 // Verify that all buckets have a valid hash index or are empty.
1251 uint32_t NumBuckets = AccelTable.getNumBuckets();
1252 uint32_t NumHashes = AccelTable.getNumHashes();
1253
1254 uint64_t BucketsOffset =
1255 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
1256 uint64_t HashesBase = BucketsOffset + NumBuckets * 4;
1257 uint64_t OffsetsBase = HashesBase + NumHashes * 4;
1258 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
1259 uint32_t HashIdx = AccelSectionData.getU32(offset_ptr: &BucketsOffset);
1260 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
1261 ErrorCategory.Report(category: "Invalid hash index", detailCallback: [&]() {
1262 error() << formatv(Fmt: "Bucket[{0}] has invalid hash index: {1}.\n",
1263 Vals&: BucketIdx, Vals&: HashIdx);
1264 });
1265 }
1266 }
1267 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
1268 if (NumAtoms == 0) {
1269 ErrorCategory.Report(category: "No atoms", detailCallback: [&]() {
1270 error() << "No atoms: failed to read HashData.\n";
1271 });
1272 return;
1273 }
1274 if (!AccelTable.validateForms()) {
1275 ErrorCategory.Report(category: "Unsupported form", detailCallback: [&]() {
1276 error() << "Unsupported form: failed to read HashData.\n";
1277 });
1278 return;
1279 }
1280
1281 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
1282 uint64_t HashOffset = HashesBase + 4 * HashIdx;
1283 uint64_t DataOffset = OffsetsBase + 4 * HashIdx;
1284 uint32_t Hash = AccelSectionData.getU32(offset_ptr: &HashOffset);
1285 uint64_t HashDataOffset = AccelSectionData.getU32(offset_ptr: &DataOffset);
1286 if (!AccelSectionData.isValidOffsetForDataOfSize(offset: HashDataOffset,
1287 length: sizeof(uint64_t))) {
1288 ErrorCategory.Report(category: "Invalid HashData offset", detailCallback: [&]() {
1289 error() << formatv(Fmt: "Hash[{0}] has invalid HashData offset: {1:x+8}.\n",
1290 Vals&: HashIdx, Vals&: HashDataOffset);
1291 });
1292 }
1293
1294 uint64_t StrpOffset;
1295 uint64_t StringOffset;
1296 uint32_t StringCount = 0;
1297 uint64_t Offset;
1298 unsigned Tag;
1299 while ((StrpOffset = AccelSectionData.getU32(offset_ptr: &HashDataOffset)) != 0) {
1300 const uint32_t NumHashDataObjects =
1301 AccelSectionData.getU32(offset_ptr: &HashDataOffset);
1302 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
1303 ++HashDataIdx) {
1304 std::tie(args&: Offset, args&: Tag) = AccelTable.readAtoms(HashDataOffset: &HashDataOffset);
1305 auto Die = DCtx.getDIEForOffset(Offset);
1306 if (!Die) {
1307 const uint32_t BucketIdx =
1308 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
1309 StringOffset = StrpOffset;
1310 const char *Name = StrData->getCStr(OffsetPtr: &StringOffset);
1311 if (!Name)
1312 Name = "<NULL>";
1313
1314 ErrorCategory.Report(category: "Invalid DIE offset", detailCallback: [&]() {
1315 error() << formatv(Fmt: "{0} Bucket[{1}] Hash[{2}] = {3:x+8} "
1316 "Str[{4}] = {5:x+8} DIE[{6}] = {7:x+8} "
1317 "is not a valid DIE offset for \"{8}\".\n",
1318 Vals&: SectionName, Vals: BucketIdx, Vals&: HashIdx, Vals&: Hash,
1319 Vals&: StringCount, Vals&: StrpOffset, Vals&: HashDataIdx, Vals&: Offset,
1320 Vals&: Name);
1321 });
1322 continue;
1323 }
1324 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
1325 ErrorCategory.Report(category: "Mismatched Tag in accellerator table", detailCallback: [&]() {
1326 error() << formatv(Fmt: "Tag {0} in accelerator table does not match "
1327 "Tag {1} of DIE[{2}].\n",
1328 Vals: dwarf::TagString(Tag),
1329 Vals: dwarf::TagString(Tag: Die.getTag()), Vals&: HashDataIdx);
1330 });
1331 }
1332 }
1333 }
1334 }
1335}
1336
1337void DWARFVerifier::verifyDebugNamesCULists(const DWARFDebugNames &AccelTable) {
1338 // A map from CU offset to the (first) Name Index offset which claims to index
1339 // this CU.
1340 DenseMap<uint64_t, uint64_t> CUMap;
1341 CUMap.reserve(NumEntries: DCtx.getNumCompileUnits());
1342
1343 DenseSet<uint64_t> CUOffsets;
1344 for (const auto &CU : DCtx.compile_units())
1345 CUOffsets.insert(V: CU->getOffset());
1346
1347 parallelForEach(R: AccelTable, Fn: [&](const DWARFDebugNames::NameIndex &NI) {
1348 if (NI.getCUCount() == 0) {
1349 ErrorCategory.Report(category: "Name Index doesn't index any CU", detailCallback: [&]() {
1350 error() << formatv(Fmt: "Name Index @ {0:x} does not index any CU\n",
1351 Vals: NI.getUnitOffset());
1352 });
1353 return;
1354 }
1355 for (uint32_t CU = 0, End = NI.getCUCount(); CU < End; ++CU) {
1356 uint64_t Offset = NI.getCUOffset(CU);
1357 if (!CUOffsets.count(V: Offset)) {
1358 ErrorCategory.Report(category: "Name Index references non-existing CU", detailCallback: [&]() {
1359 error() << formatv(
1360 Fmt: "Name Index @ {0:x} references a non-existing CU @ {1:x}\n",
1361 Vals: NI.getUnitOffset(), Vals&: Offset);
1362 });
1363 continue;
1364 }
1365 uint64_t DuplicateCUOffset = 0;
1366 {
1367 std::lock_guard<std::mutex> Lock(AccessMutex);
1368 auto Iter = CUMap.find(Val: Offset);
1369 if (Iter != CUMap.end())
1370 DuplicateCUOffset = Iter->second;
1371 else
1372 CUMap[Offset] = NI.getUnitOffset();
1373 }
1374 if (DuplicateCUOffset) {
1375 ErrorCategory.Report(category: "Duplicate Name Index", detailCallback: [&]() {
1376 error() << formatv(
1377 Fmt: "Name Index @ {0:x} references a CU @ {1:x}, but "
1378 "this CU is already indexed by Name Index @ {2:x}\n",
1379 Vals: NI.getUnitOffset(), Vals&: Offset, Vals&: DuplicateCUOffset);
1380 });
1381 continue;
1382 }
1383 }
1384 });
1385
1386 for (const auto &CU : DCtx.compile_units()) {
1387 if (CUMap.count(Val: CU->getOffset()) == 0)
1388 warn() << formatv(Fmt: "CU @ {0:x} not covered by any Name Index\n",
1389 Vals: CU->getOffset());
1390 }
1391}
1392
1393void DWARFVerifier::verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
1394 const DataExtractor &StrData) {
1395 struct BucketInfo {
1396 uint32_t Bucket;
1397 uint32_t Index;
1398
1399 constexpr BucketInfo(uint32_t Bucket, uint32_t Index)
1400 : Bucket(Bucket), Index(Index) {}
1401 bool operator<(const BucketInfo &RHS) const { return Index < RHS.Index; }
1402 };
1403
1404 if (NI.getBucketCount() == 0) {
1405 warn() << formatv(Fmt: "Name Index @ {0:x} does not contain a hash table.\n",
1406 Vals: NI.getUnitOffset());
1407 return;
1408 }
1409
1410 // Build up a list of (Bucket, Index) pairs. We use this later to verify that
1411 // each Name is reachable from the appropriate bucket.
1412 std::vector<BucketInfo> BucketStarts;
1413 BucketStarts.reserve(n: NI.getBucketCount() + 1);
1414 const uint64_t OrigNumberOfErrors = ErrorCategory.GetNumErrors();
1415 for (uint32_t Bucket = 0, End = NI.getBucketCount(); Bucket < End; ++Bucket) {
1416 uint32_t Index = NI.getBucketArrayEntry(Bucket);
1417 if (Index > NI.getNameCount()) {
1418 ErrorCategory.Report(category: "Name Index Bucket contains invalid value", detailCallback: [&]() {
1419 error() << formatv(Fmt: "Bucket {0} of Name Index @ {1:x} contains invalid "
1420 "value {2}. Valid range is [0, {3}].\n",
1421 Vals&: Bucket, Vals: NI.getUnitOffset(), Vals&: Index,
1422 Vals: NI.getNameCount());
1423 });
1424 continue;
1425 }
1426 if (Index > 0)
1427 BucketStarts.emplace_back(args&: Bucket, args&: Index);
1428 }
1429
1430 // If there were any buckets with invalid values, skip further checks as they
1431 // will likely produce many errors which will only confuse the actual root
1432 // problem.
1433 if (OrigNumberOfErrors != ErrorCategory.GetNumErrors())
1434 return;
1435
1436 // Sort the list in the order of increasing "Index" entries.
1437 array_pod_sort(Start: BucketStarts.begin(), End: BucketStarts.end());
1438
1439 // Insert a sentinel entry at the end, so we can check that the end of the
1440 // table is covered in the loop below.
1441 BucketStarts.emplace_back(args: NI.getBucketCount(), args: NI.getNameCount() + 1);
1442
1443 // Loop invariant: NextUncovered is the (1-based) index of the first Name
1444 // which is not reachable by any of the buckets we processed so far (and
1445 // hasn't been reported as uncovered).
1446 uint32_t NextUncovered = 1;
1447 for (const BucketInfo &B : BucketStarts) {
1448 // Under normal circumstances B.Index be equal to NextUncovered, but it can
1449 // be less if a bucket points to names which are already known to be in some
1450 // bucket we processed earlier. In that case, we won't trigger this error,
1451 // but report the mismatched hash value error instead. (We know the hash
1452 // will not match because we have already verified that the name's hash
1453 // puts it into the previous bucket.)
1454 if (B.Index > NextUncovered) {
1455 ErrorCategory.Report(category: "Name table entries uncovered by hash table", detailCallback: [&]() {
1456 error() << formatv(Fmt: "Name Index @ {0:x}: Name table entries [{1}, {2}] "
1457 "are not covered by the hash table.\n",
1458 Vals: NI.getUnitOffset(), Vals&: NextUncovered, Vals: B.Index - 1);
1459 });
1460 }
1461 uint32_t Idx = B.Index;
1462
1463 // The rest of the checks apply only to non-sentinel entries.
1464 if (B.Bucket == NI.getBucketCount())
1465 break;
1466
1467 // This triggers if a non-empty bucket points to a name with a mismatched
1468 // hash. Clients are likely to interpret this as an empty bucket, because a
1469 // mismatched hash signals the end of a bucket, but if this is indeed an
1470 // empty bucket, the producer should have signalled this by marking the
1471 // bucket as empty.
1472 uint32_t FirstHash = NI.getHashArrayEntry(Index: Idx);
1473 if (FirstHash % NI.getBucketCount() != B.Bucket) {
1474 ErrorCategory.Report(category: "Name Index point to mismatched hash value", detailCallback: [&]() {
1475 error() << formatv(
1476 Fmt: "Name Index @ {0:x}: Bucket {1} is not empty but points to a "
1477 "mismatched hash value {2:x} (belonging to bucket {3}).\n",
1478 Vals: NI.getUnitOffset(), Vals: B.Bucket, Vals&: FirstHash,
1479 Vals: FirstHash % NI.getBucketCount());
1480 });
1481 }
1482
1483 // This find the end of this bucket and also verifies that all the hashes in
1484 // this bucket are correct by comparing the stored hashes to the ones we
1485 // compute ourselves.
1486 while (Idx <= NI.getNameCount()) {
1487 uint32_t Hash = NI.getHashArrayEntry(Index: Idx);
1488 if (Hash % NI.getBucketCount() != B.Bucket)
1489 break;
1490
1491 const char *Str = NI.getNameTableEntry(Index: Idx).getString();
1492 if (caseFoldingDjbHash(Buffer: Str) != Hash) {
1493 ErrorCategory.Report(
1494 category: "String hash doesn't match Name Index hash", detailCallback: [&]() {
1495 error() << formatv(
1496 Fmt: "Name Index @ {0:x}: String ({1}) at index {2} "
1497 "hashes to {3:x}, but "
1498 "the Name Index hash is {4:x}\n",
1499 Vals: NI.getUnitOffset(), Vals&: Str, Vals&: Idx, Vals: caseFoldingDjbHash(Buffer: Str), Vals&: Hash);
1500 });
1501 }
1502 ++Idx;
1503 }
1504 NextUncovered = std::max(a: NextUncovered, b: Idx);
1505 }
1506}
1507
1508void DWARFVerifier::verifyNameIndexAttribute(
1509 const DWARFDebugNames::NameIndex &NI, const DWARFDebugNames::Abbrev &Abbr,
1510 DWARFDebugNames::AttributeEncoding AttrEnc) {
1511 StringRef FormName = dwarf::FormEncodingString(Encoding: AttrEnc.Form);
1512 if (FormName.empty()) {
1513 ErrorCategory.Report(category: "Unknown NameIndex Abbreviation", detailCallback: [&]() {
1514 error() << formatv(Fmt: "NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
1515 "unknown form: {3}.\n",
1516 Vals: NI.getUnitOffset(), Vals: Abbr.Code, Vals&: AttrEnc.Index,
1517 Vals&: AttrEnc.Form);
1518 });
1519 return;
1520 }
1521
1522 if (AttrEnc.Index == DW_IDX_type_hash) {
1523 if (AttrEnc.Form != dwarf::DW_FORM_data8) {
1524 ErrorCategory.Report(category: "Unexpected NameIndex Abbreviation", detailCallback: [&]() {
1525 error() << formatv(
1526 Fmt: "NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_type_hash "
1527 "uses an unexpected form {2} (should be {3}).\n",
1528 Vals: NI.getUnitOffset(), Vals: Abbr.Code, Vals&: AttrEnc.Form, Vals: dwarf::DW_FORM_data8);
1529 });
1530 return;
1531 }
1532 return;
1533 }
1534
1535 if (AttrEnc.Index == dwarf::DW_IDX_parent) {
1536 constexpr static auto AllowedForms = {dwarf::Form::DW_FORM_flag_present,
1537 dwarf::Form::DW_FORM_ref4};
1538 if (!is_contained(Set: AllowedForms, Element: AttrEnc.Form)) {
1539 ErrorCategory.Report(category: "Unexpected NameIndex Abbreviation", detailCallback: [&]() {
1540 error() << formatv(
1541 Fmt: "NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_parent "
1542 "uses an unexpected form {2} (should be "
1543 "DW_FORM_ref4 or DW_FORM_flag_present).\n",
1544 Vals: NI.getUnitOffset(), Vals: Abbr.Code, Vals&: AttrEnc.Form);
1545 });
1546 return;
1547 }
1548 return;
1549 }
1550
1551 // A list of known index attributes and their expected form classes.
1552 // DW_IDX_type_hash is handled specially in the check above, as it has a
1553 // specific form (not just a form class) we should expect.
1554 struct FormClassTable {
1555 dwarf::Index Index;
1556 DWARFFormValue::FormClass Class;
1557 StringLiteral ClassName;
1558 };
1559 static constexpr FormClassTable Table[] = {
1560 {.Index: dwarf::DW_IDX_compile_unit, .Class: DWARFFormValue::FC_Constant, .ClassName: {"constant"}},
1561 {.Index: dwarf::DW_IDX_type_unit, .Class: DWARFFormValue::FC_Constant, .ClassName: {"constant"}},
1562 {.Index: dwarf::DW_IDX_die_offset, .Class: DWARFFormValue::FC_Reference, .ClassName: {"reference"}},
1563 };
1564
1565 ArrayRef<FormClassTable> TableRef(Table);
1566 auto Iter = find_if(Range&: TableRef, P: [AttrEnc](const FormClassTable &T) {
1567 return T.Index == AttrEnc.Index;
1568 });
1569 if (Iter == TableRef.end()) {
1570 warn() << formatv(Fmt: "NameIndex @ {0:x}: Abbreviation {1:x} contains an "
1571 "unknown index attribute: {2}.\n",
1572 Vals: NI.getUnitOffset(), Vals: Abbr.Code, Vals&: AttrEnc.Index);
1573 return;
1574 }
1575
1576 if (!DWARFFormValue(AttrEnc.Form).isFormClass(FC: Iter->Class)) {
1577 ErrorCategory.Report(category: "Unexpected NameIndex Abbreviation", detailCallback: [&]() {
1578 error() << formatv(Fmt: "NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
1579 "unexpected form {3} (expected form class {4}).\n",
1580 Vals: NI.getUnitOffset(), Vals: Abbr.Code, Vals&: AttrEnc.Index,
1581 Vals&: AttrEnc.Form, Vals: Iter->ClassName);
1582 });
1583 return;
1584 }
1585}
1586
1587void DWARFVerifier::verifyNameIndexAbbrevs(
1588 const DWARFDebugNames::NameIndex &NI) {
1589 for (const auto &Abbrev : NI.getAbbrevs()) {
1590 StringRef TagName = dwarf::TagString(Tag: Abbrev.Tag);
1591 if (TagName.empty()) {
1592 warn() << formatv(Fmt: "NameIndex @ {0:x}: Abbreviation {1:x} references an "
1593 "unknown tag: {2}.\n",
1594 Vals: NI.getUnitOffset(), Vals: Abbrev.Code, Vals: Abbrev.Tag);
1595 }
1596 SmallSet<unsigned, 5> Attributes;
1597 for (const auto &AttrEnc : Abbrev.Attributes) {
1598 if (!Attributes.insert(V: AttrEnc.Index).second) {
1599 ErrorCategory.Report(
1600 category: "NameIndex Abbreviateion contains multiple attributes", detailCallback: [&]() {
1601 error() << formatv(
1602 Fmt: "NameIndex @ {0:x}: Abbreviation {1:x} contains "
1603 "multiple {2} attributes.\n",
1604 Vals: NI.getUnitOffset(), Vals: Abbrev.Code, Vals: AttrEnc.Index);
1605 });
1606 continue;
1607 }
1608 verifyNameIndexAttribute(NI, Abbr: Abbrev, AttrEnc);
1609 }
1610
1611 if (NI.getCUCount() > 1 && !Attributes.count(V: dwarf::DW_IDX_compile_unit) &&
1612 !Attributes.count(V: dwarf::DW_IDX_type_unit)) {
1613 ErrorCategory.Report(category: "Abbreviation contains no attribute", detailCallback: [&]() {
1614 error() << formatv(Fmt: "NameIndex @ {0:x}: Indexing multiple compile units "
1615 "and abbreviation {1:x} has no DW_IDX_compile_unit "
1616 "or DW_IDX_type_unit attribute.\n",
1617 Vals: NI.getUnitOffset(), Vals: Abbrev.Code);
1618 });
1619 }
1620 if (!Attributes.count(V: dwarf::DW_IDX_die_offset)) {
1621 ErrorCategory.Report(category: "Abbreviate in NameIndex missing attribute", detailCallback: [&]() {
1622 error() << formatv(
1623 Fmt: "NameIndex @ {0:x}: Abbreviation {1:x} has no {2} attribute.\n",
1624 Vals: NI.getUnitOffset(), Vals: Abbrev.Code, Vals: dwarf::DW_IDX_die_offset);
1625 });
1626 }
1627 }
1628}
1629
1630/// Constructs a full name for a DIE. Potentially it does recursive lookup on
1631/// DIEs. This can lead to extraction of DIEs in a different CU or TU.
1632static SmallVector<std::string, 3> getNames(const DWARFDie &DIE,
1633 bool IncludeStrippedTemplateNames,
1634 bool IncludeObjCNames = true,
1635 bool IncludeLinkageName = true) {
1636 SmallVector<std::string, 3> Result;
1637 if (const char *Str = DIE.getShortName()) {
1638 StringRef Name(Str);
1639 Result.emplace_back(Args&: Name);
1640 if (IncludeStrippedTemplateNames) {
1641 if (std::optional<StringRef> StrippedName =
1642 StripTemplateParameters(Name: Result.back()))
1643 // Convert to std::string and push; emplacing the StringRef may trigger
1644 // a vector resize which may destroy the StringRef memory.
1645 Result.push_back(Elt: StrippedName->str());
1646 }
1647
1648 if (IncludeObjCNames) {
1649 if (std::optional<ObjCSelectorNames> ObjCNames =
1650 getObjCNamesIfSelector(Name)) {
1651 Result.emplace_back(Args&: ObjCNames->ClassName);
1652 Result.emplace_back(Args&: ObjCNames->Selector);
1653 if (ObjCNames->ClassNameNoCategory)
1654 Result.emplace_back(Args&: *ObjCNames->ClassNameNoCategory);
1655 if (ObjCNames->MethodNameNoCategory)
1656 Result.push_back(Elt: std::move(*ObjCNames->MethodNameNoCategory));
1657 }
1658 }
1659 } else if (DIE.getTag() == dwarf::DW_TAG_namespace)
1660 Result.emplace_back(Args: "(anonymous namespace)");
1661
1662 if (IncludeLinkageName) {
1663 if (const char *Str = DIE.getLinkageName())
1664 Result.emplace_back(Args&: Str);
1665 }
1666
1667 return Result;
1668}
1669
1670void DWARFVerifier::verifyNameIndexEntries(
1671 const DWARFDebugNames::NameIndex &NI,
1672 const DWARFDebugNames::NameTableEntry &NTE,
1673 const DenseMap<uint64_t, DWARFUnit *> &CUOffsetsToDUMap) {
1674 const char *CStr = NTE.getString();
1675 if (!CStr) {
1676 ErrorCategory.Report(category: "Unable to get string associated with name", detailCallback: [&]() {
1677 error() << formatv(Fmt: "Name Index @ {0:x}: Unable to get string associated "
1678 "with name {1}.\n",
1679 Vals: NI.getUnitOffset(), Vals: NTE.getIndex());
1680 });
1681 return;
1682 }
1683 StringRef Str(CStr);
1684 unsigned NumEntries = 0;
1685 uint64_t EntryID = NTE.getEntryOffset();
1686 uint64_t NextEntryID = EntryID;
1687 Expected<DWARFDebugNames::Entry> EntryOr = NI.getEntry(Offset: &NextEntryID);
1688 for (; EntryOr; ++NumEntries, EntryID = NextEntryID,
1689 EntryOr = NI.getEntry(Offset: &NextEntryID)) {
1690
1691 std::optional<uint64_t> CUIndex = EntryOr->getRelatedCUIndex();
1692 std::optional<uint64_t> TUIndex = EntryOr->getTUIndex();
1693 if (CUIndex && *CUIndex >= NI.getCUCount()) {
1694 ErrorCategory.Report(category: "Name Index entry contains invalid CU index", detailCallback: [&]() {
1695 error() << formatv(Fmt: "Name Index @ {0:x}: Entry @ {1:x} contains an "
1696 "invalid CU index ({2}).\n",
1697 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: *CUIndex);
1698 });
1699 continue;
1700 }
1701 const uint32_t NumLocalTUs = NI.getLocalTUCount();
1702 const uint32_t NumForeignTUs = NI.getForeignTUCount();
1703 if (TUIndex && *TUIndex >= (NumLocalTUs + NumForeignTUs)) {
1704 ErrorCategory.Report(category: "Name Index entry contains invalid TU index", detailCallback: [&]() {
1705 error() << formatv(Fmt: "Name Index @ {0:x}: Entry @ {1:x} contains an "
1706 "invalid TU index ({2}).\n",
1707 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: *TUIndex);
1708 });
1709 continue;
1710 }
1711 std::optional<uint64_t> UnitOffset;
1712 if (TUIndex) {
1713 // We have a local or foreign type unit.
1714 if (*TUIndex >= NumLocalTUs) {
1715 // This is a foreign type unit, we will find the right type unit by
1716 // type unit signature later in this function.
1717
1718 // Foreign type units must have a valid CU index, either from a
1719 // DW_IDX_comp_unit attribute value or from the .debug_names table only
1720 // having a single compile unit. We need the originating compile unit
1721 // because foreign type units can come from any .dwo file, yet only one
1722 // copy of the type unit will end up in the .dwp file.
1723 if (CUIndex) {
1724 // We need the local skeleton unit offset for the code below.
1725 UnitOffset = NI.getCUOffset(CU: *CUIndex);
1726 } else {
1727 ErrorCategory.Report(
1728 category: "Name Index entry contains foreign TU index with invalid CU "
1729 "index",
1730 detailCallback: [&]() {
1731 error() << formatv(
1732 Fmt: "Name Index @ {0:x}: Entry @ {1:x} contains an "
1733 "foreign TU index ({2}) with no CU index.\n",
1734 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: *TUIndex);
1735 });
1736 continue;
1737 }
1738 } else {
1739 // Local type unit, get the DWARF unit offset for the type unit.
1740 UnitOffset = NI.getLocalTUOffset(TU: *TUIndex);
1741 }
1742 } else if (CUIndex) {
1743 // Local CU entry, get the DWARF unit offset for the CU.
1744 UnitOffset = NI.getCUOffset(CU: *CUIndex);
1745 }
1746
1747 // Watch for tombstoned type unit entries.
1748 if (!UnitOffset || UnitOffset == UINT32_MAX)
1749 continue;
1750 // For split DWARF entries we need to make sure we find the non skeleton
1751 // DWARF unit that is needed and use that's DWARF unit offset as the
1752 // DIE offset to add the DW_IDX_die_offset to.
1753 DWARFUnit *DU = DCtx.getUnitForOffset(Offset: *UnitOffset);
1754 if (DU == nullptr || DU->getOffset() != *UnitOffset) {
1755 // If we didn't find a DWARF Unit from the UnitOffset, or if the offset
1756 // of the unit doesn't match exactly, report an error.
1757 ErrorCategory.Report(
1758 category: "Name Index entry contains invalid CU or TU offset", detailCallback: [&]() {
1759 error() << formatv(Fmt: "Name Index @ {0:x}: Entry @ {1:x} contains an "
1760 "invalid CU or TU offset {2:x}.\n",
1761 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: *UnitOffset);
1762 });
1763 continue;
1764 }
1765 // This function will try to get the non skeleton unit DIE, but if it is
1766 // unable to load the .dwo file from the .dwo or .dwp, it will return the
1767 // unit DIE of the DWARFUnit in "DU". So we need to check if the DWARFUnit
1768 // has a .dwo file, but we couldn't load it.
1769
1770 // FIXME: Need a follow up patch to fix usage of
1771 // DWARFUnit::getNonSkeletonUnitDIE() so that it returns an empty DWARFDie
1772 // if the .dwo file isn't available and clean up other uses of this function
1773 // call to properly deal with it. It isn't clear that getNonSkeletonUnitDIE
1774 // will return the unit DIE of DU if we aren't able to get the .dwo file,
1775 // but that is what the function currently does.
1776 DWARFUnit *NonSkeletonUnit = nullptr;
1777 if (DU->getDWOId()) {
1778 auto Iter = CUOffsetsToDUMap.find(Val: DU->getOffset());
1779 NonSkeletonUnit = Iter->second;
1780 } else {
1781 NonSkeletonUnit = DU;
1782 }
1783 DWARFDie UnitDie = DU->getUnitDIE();
1784 if (DU->getDWOId() && !NonSkeletonUnit->isDWOUnit()) {
1785 ErrorCategory.Report(category: "Unable to get load .dwo file", detailCallback: [&]() {
1786 error() << formatv(
1787 Fmt: "Name Index @ {0:x}: Entry @ {1:x} unable to load "
1788 ".dwo file \"{2}\" for DWARF unit @ {3:x}.\n",
1789 Vals: NI.getUnitOffset(), Vals&: EntryID,
1790 Vals: dwarf::toString(V: UnitDie.find(Attrs: {DW_AT_dwo_name, DW_AT_GNU_dwo_name})),
1791 Vals&: *UnitOffset);
1792 });
1793 continue;
1794 }
1795
1796 if (TUIndex && *TUIndex >= NumLocalTUs) {
1797 // We have a foreign TU index, which either means we have a .dwo file
1798 // that has one or more type units, or we have a .dwp file with one or
1799 // more type units. We need to get the type unit from the DWARFContext
1800 // of the .dwo. We got the NonSkeletonUnitDie above that has the .dwo
1801 // or .dwp DWARF context, so we have to get the type unit from that file.
1802 // We have also verified that NonSkeletonUnitDie points to a DWO file
1803 // above, so we know we have the right file.
1804 const uint32_t ForeignTUIdx = *TUIndex - NumLocalTUs;
1805 const uint64_t TypeSig = NI.getForeignTUSignature(TU: ForeignTUIdx);
1806 llvm::DWARFContext &NonSkeletonDCtx = NonSkeletonUnit->getContext();
1807 // Now find the type unit from the type signature and then update the
1808 // NonSkeletonUnitDie to point to the actual type unit in the .dwo/.dwp.
1809 NonSkeletonUnit =
1810 NonSkeletonDCtx.getTypeUnitForHash(Hash: TypeSig, /*IsDWO=*/true);
1811 // If we have foreign type unit in a DWP file, then we need to ignore
1812 // any entries from type units that don't match the one that made it into
1813 // the .dwp file.
1814 if (NonSkeletonDCtx.isDWP()) {
1815 DWARFDie NonSkeletonUnitDie = NonSkeletonUnit->getUnitDIE(ExtractUnitDIEOnly: true);
1816 StringRef DUDwoName = dwarf::toStringRef(
1817 V: UnitDie.find(Attrs: {DW_AT_dwo_name, DW_AT_GNU_dwo_name}));
1818 StringRef TUDwoName = dwarf::toStringRef(
1819 V: NonSkeletonUnitDie.find(Attrs: {DW_AT_dwo_name, DW_AT_GNU_dwo_name}));
1820 if (DUDwoName != TUDwoName)
1821 continue; // Skip this TU, it isn't the one in the .dwp file.
1822 }
1823 }
1824 uint64_t DIEOffset =
1825 NonSkeletonUnit->getOffset() + *EntryOr->getDIEUnitOffset();
1826 const uint64_t NextUnitOffset = NonSkeletonUnit->getNextUnitOffset();
1827 // DIE offsets are relative to the specified CU or TU. Make sure the DIE
1828 // offsets is a valid relative offset.
1829 if (DIEOffset >= NextUnitOffset) {
1830 ErrorCategory.Report(category: "NameIndex relative DIE offset too large", detailCallback: [&]() {
1831 error() << formatv(Fmt: "Name Index @ {0:x}: Entry @ {1:x} references a "
1832 "DIE @ {2:x} when CU or TU ends at {3:x}.\n",
1833 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: DIEOffset,
1834 Vals: NextUnitOffset);
1835 });
1836 continue;
1837 }
1838 DWARFDie DIE = NonSkeletonUnit->getDIEForOffset(Offset: DIEOffset);
1839 if (!DIE) {
1840 ErrorCategory.Report(category: "NameIndex references nonexistent DIE", detailCallback: [&]() {
1841 error() << formatv(Fmt: "Name Index @ {0:x}: Entry @ {1:x} references a "
1842 "non-existing DIE @ {2:x}.\n",
1843 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: DIEOffset);
1844 });
1845 continue;
1846 }
1847 // Only compare the DIE we found's DWARFUnit offset if the DIE lives in
1848 // the DWARFUnit from the DW_IDX_comp_unit or DW_IDX_type_unit. If we are
1849 // using split DWARF, then the DIE's DWARFUnit doesn't need to match the
1850 // skeleton unit.
1851 if (DIE.getDwarfUnit() == DU &&
1852 DIE.getDwarfUnit()->getOffset() != *UnitOffset) {
1853 ErrorCategory.Report(category: "Name index contains mismatched CU of DIE", detailCallback: [&]() {
1854 error() << formatv(
1855 Fmt: "Name Index @ {0:x}: Entry @ {1:x}: mismatched CU of "
1856 "DIE @ {2:x}: index - {3:x}; debug_info - {4:x}.\n",
1857 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: DIEOffset, Vals&: *UnitOffset,
1858 Vals: DIE.getDwarfUnit()->getOffset());
1859 });
1860 }
1861 if (DIE.getTag() != EntryOr->tag()) {
1862 ErrorCategory.Report(category: "Name Index contains mismatched Tag of DIE", detailCallback: [&]() {
1863 error() << formatv(
1864 Fmt: "Name Index @ {0:x}: Entry @ {1:x}: mismatched Tag of "
1865 "DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1866 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: DIEOffset, Vals: EntryOr->tag(),
1867 Vals: DIE.getTag());
1868 });
1869 }
1870
1871 // We allow an extra name for functions: their name without any template
1872 // parameters.
1873 auto IncludeStrippedTemplateNames =
1874 DIE.getTag() == DW_TAG_subprogram ||
1875 DIE.getTag() == DW_TAG_inlined_subroutine;
1876 auto EntryNames = getNames(DIE, IncludeStrippedTemplateNames);
1877 if (!is_contained(Range&: EntryNames, Element: Str)) {
1878 ErrorCategory.Report(category: "Name Index contains mismatched name of DIE", detailCallback: [&]() {
1879 error() << formatv(Fmt: "Name Index @ {0:x}: Entry @ {1:x}: mismatched Name "
1880 "of DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1881 Vals: NI.getUnitOffset(), Vals&: EntryID, Vals&: DIEOffset, Vals&: Str,
1882 Vals: make_range(x: EntryNames.begin(), y: EntryNames.end()));
1883 });
1884 }
1885 }
1886 handleAllErrors(
1887 E: EntryOr.takeError(),
1888 Handlers: [&](const DWARFDebugNames::SentinelError &) {
1889 if (NumEntries > 0)
1890 return;
1891 ErrorCategory.Report(
1892 category: "NameIndex Name is not associated with any entries", detailCallback: [&]() {
1893 error() << formatv(Fmt: "Name Index @ {0:x}: Name {1} ({2}) is "
1894 "not associated with any entries.\n",
1895 Vals: NI.getUnitOffset(), Vals: NTE.getIndex(), Vals&: Str);
1896 });
1897 },
1898 Handlers: [&](const ErrorInfoBase &Info) {
1899 ErrorCategory.Report(category: "Uncategorized NameIndex error", detailCallback: [&]() {
1900 error() << formatv(Fmt: "Name Index @ {0:x}: Name {1} ({2}): {3}\n",
1901 Vals: NI.getUnitOffset(), Vals: NTE.getIndex(), Vals&: Str,
1902 Vals: Info.message());
1903 });
1904 });
1905}
1906
1907static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx) {
1908 Expected<std::vector<DWARFLocationExpression>> Loc =
1909 Die.getLocations(Attr: DW_AT_location);
1910 if (!Loc) {
1911 consumeError(Err: Loc.takeError());
1912 return false;
1913 }
1914 DWARFUnit *U = Die.getDwarfUnit();
1915 for (const auto &Entry : *Loc) {
1916 DataExtractor Data(toStringRef(Input: Entry.Expr), DCtx.isLittleEndian(),
1917 U->getAddressByteSize());
1918 DWARFExpression Expression(Data, U->getAddressByteSize(),
1919 U->getFormParams().Format);
1920 bool IsInteresting =
1921 any_of(Range&: Expression, P: [](const DWARFExpression::Operation &Op) {
1922 return !Op.isError() && (Op.getCode() == DW_OP_addr ||
1923 Op.getCode() == DW_OP_form_tls_address ||
1924 Op.getCode() == DW_OP_GNU_push_tls_address);
1925 });
1926 if (IsInteresting)
1927 return true;
1928 }
1929 return false;
1930}
1931
1932void DWARFVerifier::verifyNameIndexCompleteness(
1933 const DWARFDie &Die, const DWARFDebugNames::NameIndex &NI,
1934 const StringMap<DenseSet<uint64_t>> &NamesToDieOffsets) {
1935
1936 // First check, if the Die should be indexed. The code follows the DWARF v5
1937 // wording as closely as possible.
1938
1939 // "All non-defining declarations (that is, debugging information entries
1940 // with a DW_AT_declaration attribute) are excluded."
1941 if (Die.find(Attr: DW_AT_declaration))
1942 return;
1943
1944 // "DW_TAG_namespace debugging information entries without a DW_AT_name
1945 // attribute are included with the name “(anonymous namespace)”.
1946 // All other debugging information entries without a DW_AT_name attribute
1947 // are excluded."
1948 // "If a subprogram or inlined subroutine is included, and has a
1949 // DW_AT_linkage_name attribute, there will be an additional index entry for
1950 // the linkage name."
1951 auto IncludeLinkageName = Die.getTag() == DW_TAG_subprogram ||
1952 Die.getTag() == DW_TAG_inlined_subroutine;
1953 // We *allow* stripped template names / ObjectiveC names as extra entries into
1954 // the table, but we don't *require* them to pass the completeness test.
1955 auto IncludeStrippedTemplateNames = false;
1956 auto IncludeObjCNames = false;
1957 auto EntryNames = getNames(DIE: Die, IncludeStrippedTemplateNames,
1958 IncludeObjCNames, IncludeLinkageName);
1959 if (EntryNames.empty())
1960 return;
1961
1962 // We deviate from the specification here, which says:
1963 // "The name index must contain an entry for each debugging information entry
1964 // that defines a named subprogram, label, variable, type, or namespace,
1965 // subject to ..."
1966 // Explicitly exclude all TAGs that we know shouldn't be indexed.
1967 switch (Die.getTag()) {
1968 // Compile units and modules have names but shouldn't be indexed.
1969 case DW_TAG_compile_unit:
1970 case DW_TAG_module:
1971 return;
1972
1973 // Function and template parameters are not globally visible, so we shouldn't
1974 // index them.
1975 case DW_TAG_formal_parameter:
1976 case DW_TAG_template_value_parameter:
1977 case DW_TAG_template_type_parameter:
1978 case DW_TAG_GNU_template_parameter_pack:
1979 case DW_TAG_GNU_template_template_param:
1980 return;
1981
1982 // Object members aren't globally visible.
1983 case DW_TAG_member:
1984 return;
1985
1986 // According to a strict reading of the specification, enumerators should not
1987 // be indexed (and LLVM currently does not do that). However, this causes
1988 // problems for the debuggers, so we may need to reconsider this.
1989 case DW_TAG_enumerator:
1990 return;
1991
1992 // Imported declarations should not be indexed according to the specification
1993 // and LLVM currently does not do that.
1994 case DW_TAG_imported_declaration:
1995 return;
1996
1997 // "DW_TAG_subprogram, DW_TAG_inlined_subroutine, and DW_TAG_label debugging
1998 // information entries without an address attribute (DW_AT_low_pc,
1999 // DW_AT_high_pc, DW_AT_ranges, or DW_AT_entry_pc) are excluded."
2000 case DW_TAG_subprogram:
2001 case DW_TAG_inlined_subroutine:
2002 case DW_TAG_label:
2003 if (Die.findRecursively(
2004 Attrs: {DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_entry_pc}))
2005 break;
2006 return;
2007
2008 // "DW_TAG_variable debugging information entries with a DW_AT_location
2009 // attribute that includes a DW_OP_addr or DW_OP_form_tls_address operator are
2010 // included; otherwise, they are excluded."
2011 //
2012 // LLVM extension: We also add DW_OP_GNU_push_tls_address to this list.
2013 case DW_TAG_variable:
2014 if (isVariableIndexable(Die, DCtx))
2015 break;
2016 return;
2017
2018 default:
2019 break;
2020 }
2021
2022 // Now we know that our Die should be present in the Index. Let's check if
2023 // that's the case.
2024 uint64_t DieUnitOffset = Die.getOffset() - Die.getDwarfUnit()->getOffset();
2025 for (StringRef Name : EntryNames) {
2026 auto iter = NamesToDieOffsets.find(Key: Name);
2027 if (iter == NamesToDieOffsets.end() || !iter->second.count(V: DieUnitOffset)) {
2028 ErrorCategory.Report(
2029 category: "Name Index DIE entry missing name",
2030 sub_category: llvm::dwarf::TagString(Tag: Die.getTag()), detailCallback: [&]() {
2031 error() << formatv(
2032 Fmt: "Name Index @ {0:x}: Entry for DIE @ {1:x} ({2}) with "
2033 "name {3} missing.\n",
2034 Vals: NI.getUnitOffset(), Vals: Die.getOffset(), Vals: Die.getTag(), Vals&: Name);
2035 });
2036 }
2037 }
2038}
2039
2040/// Extracts all the data for CU/TUs so we can access it in parallel without
2041/// locks.
2042static void extractCUsTus(DWARFContext &DCtx) {
2043 // Abbrev DeclSet is shared beween the units.
2044 for (auto &CUTU : DCtx.normal_units()) {
2045 CUTU->getUnitDIE();
2046 CUTU->getBaseAddress();
2047 }
2048 parallelForEach(R: DCtx.normal_units(), Fn: [&](const auto &CUTU) {
2049 if (Error E = CUTU->tryExtractDIEsIfNeeded(false))
2050 DCtx.getRecoverableErrorHandler()(std::move(E));
2051 });
2052
2053 // Invoking getNonSkeletonUnitDIE() sets up all the base pointers for DWO
2054 // Units. This is needed for getBaseAddress().
2055 for (const auto &CU : DCtx.compile_units()) {
2056 if (!CU->getDWOId())
2057 continue;
2058 DWARFContext &NonSkeletonContext =
2059 CU->getNonSkeletonUnitDIE().getDwarfUnit()->getContext();
2060 // Iterates over CUs and TUs.
2061 for (auto &CUTU : NonSkeletonContext.dwo_units()) {
2062 CUTU->getUnitDIE();
2063 CUTU->getBaseAddress();
2064 }
2065 parallelForEach(R: NonSkeletonContext.dwo_units(), Fn: [&](const auto &CUTU) {
2066 if (Error E = CUTU->tryExtractDIEsIfNeeded(false))
2067 DCtx.getRecoverableErrorHandler()(std::move(E));
2068 });
2069 // If context is for DWP we only need to extract once.
2070 if (NonSkeletonContext.isDWP())
2071 break;
2072 }
2073}
2074
2075void DWARFVerifier::verifyDebugNames(const DWARFSection &AccelSection,
2076 const DataExtractor &StrData) {
2077 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), AccelSection,
2078 DCtx.isLittleEndian(), 0);
2079 DWARFDebugNames AccelTable(AccelSectionData, StrData);
2080
2081 OS << "Verifying .debug_names...\n";
2082
2083 // This verifies that we can read individual name indices and their
2084 // abbreviation tables.
2085 if (Error E = AccelTable.extract()) {
2086 std::string Msg = toString(E: std::move(E));
2087 ErrorCategory.Report(category: "Accelerator Table Error",
2088 detailCallback: [&]() { error() << Msg << '\n'; });
2089 return;
2090 }
2091 const uint64_t OriginalNumErrors = ErrorCategory.GetNumErrors();
2092 verifyDebugNamesCULists(AccelTable);
2093 for (const auto &NI : AccelTable)
2094 verifyNameIndexBuckets(NI, StrData);
2095 parallelForEach(R&: AccelTable, Fn: [&](const DWARFDebugNames::NameIndex &NI) {
2096 verifyNameIndexAbbrevs(NI);
2097 });
2098
2099 // Don't attempt Entry validation if any of the previous checks found errors
2100 if (OriginalNumErrors != ErrorCategory.GetNumErrors())
2101 return;
2102 DenseMap<uint64_t, DWARFUnit *> CUOffsetsToDUMap;
2103 for (const auto &CU : DCtx.compile_units()) {
2104 if (!(CU->getVersion() >= 5 && CU->getDWOId()))
2105 continue;
2106 CUOffsetsToDUMap[CU->getOffset()] =
2107 CU->getNonSkeletonUnitDIE().getDwarfUnit();
2108 }
2109 extractCUsTus(DCtx);
2110 for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
2111 parallelForEach(R: NI, Fn: [&](DWARFDebugNames::NameTableEntry NTE) {
2112 verifyNameIndexEntries(NI, NTE, CUOffsetsToDUMap);
2113 });
2114 }
2115
2116 auto populateNameToOffset =
2117 [&](const DWARFDebugNames::NameIndex &NI,
2118 StringMap<DenseSet<uint64_t>> &NamesToDieOffsets) {
2119 for (const DWARFDebugNames::NameTableEntry &NTE : NI) {
2120 const char *tName = NTE.getString();
2121 const std::string Name = tName ? std::string(tName) : "";
2122 uint64_t EntryID = NTE.getEntryOffset();
2123 Expected<DWARFDebugNames::Entry> EntryOr = NI.getEntry(Offset: &EntryID);
2124 auto Iter = NamesToDieOffsets.insert(KV: {Name, DenseSet<uint64_t>(3)});
2125 for (; EntryOr; EntryOr = NI.getEntry(Offset: &EntryID)) {
2126 if (std::optional<uint64_t> DieOffset = EntryOr->getDIEUnitOffset())
2127 Iter.first->second.insert(V: *DieOffset);
2128 }
2129 handleAllErrors(
2130 E: EntryOr.takeError(),
2131 Handlers: [&](const DWARFDebugNames::SentinelError &) {
2132 if (!NamesToDieOffsets.empty())
2133 return;
2134 ErrorCategory.Report(
2135 category: "NameIndex Name is not associated with any entries", detailCallback: [&]() {
2136 error()
2137 << formatv(Fmt: "Name Index @ {0:x}: Name {1} ({2}) is "
2138 "not associated with any entries.\n",
2139 Vals: NI.getUnitOffset(), Vals: NTE.getIndex(), Vals: Name);
2140 });
2141 },
2142 Handlers: [&](const ErrorInfoBase &Info) {
2143 ErrorCategory.Report(category: "Uncategorized NameIndex error", detailCallback: [&]() {
2144 error() << formatv(
2145 Fmt: "Name Index @ {0:x}: Name {1} ({2}): {3}\n",
2146 Vals: NI.getUnitOffset(), Vals: NTE.getIndex(), Vals: Name, Vals: Info.message());
2147 });
2148 });
2149 }
2150 };
2151 // NameIndex can have multiple CUs. For example if it was created by BOLT.
2152 // So better to iterate over NI, and then over CUs in it.
2153 for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
2154 StringMap<DenseSet<uint64_t>> NamesToDieOffsets(NI.getNameCount());
2155 populateNameToOffset(NI, NamesToDieOffsets);
2156 for (uint32_t i = 0, iEnd = NI.getCUCount(); i < iEnd; ++i) {
2157 const uint64_t CUOffset = NI.getCUOffset(CU: i);
2158 DWARFUnit *U = DCtx.getUnitForOffset(Offset: CUOffset);
2159 DWARFCompileUnit *CU = dyn_cast<DWARFCompileUnit>(Val: U);
2160 if (CU) {
2161 if (CU->getDWOId()) {
2162 DWARFDie CUDie = CU->getUnitDIE(ExtractUnitDIEOnly: true);
2163 DWARFDie NonSkeletonUnitDie =
2164 CUDie.getDwarfUnit()->getNonSkeletonUnitDIE(ExtractUnitDIEOnly: false);
2165 if (CUDie != NonSkeletonUnitDie) {
2166 parallelForEach(
2167 R: NonSkeletonUnitDie.getDwarfUnit()->dies(),
2168 Fn: [&](const DWARFDebugInfoEntry &Die) {
2169 verifyNameIndexCompleteness(
2170 Die: DWARFDie(NonSkeletonUnitDie.getDwarfUnit(), &Die), NI,
2171 NamesToDieOffsets);
2172 });
2173 }
2174 } else {
2175 parallelForEach(R: CU->dies(), Fn: [&](const DWARFDebugInfoEntry &Die) {
2176 verifyNameIndexCompleteness(Die: DWARFDie(CU, &Die), NI,
2177 NamesToDieOffsets);
2178 });
2179 }
2180 }
2181 }
2182 }
2183}
2184
2185bool DWARFVerifier::handleAccelTables() {
2186 const DWARFObject &D = DCtx.getDWARFObj();
2187 DataExtractor StrData(D.getStrSection(), DCtx.isLittleEndian(), 0);
2188 if (!D.getAppleNamesSection().Data.empty())
2189 verifyAppleAccelTable(AccelSection: &D.getAppleNamesSection(), StrData: &StrData, SectionName: ".apple_names");
2190 if (!D.getAppleTypesSection().Data.empty())
2191 verifyAppleAccelTable(AccelSection: &D.getAppleTypesSection(), StrData: &StrData, SectionName: ".apple_types");
2192 if (!D.getAppleNamespacesSection().Data.empty())
2193 verifyAppleAccelTable(AccelSection: &D.getAppleNamespacesSection(), StrData: &StrData,
2194 SectionName: ".apple_namespaces");
2195 if (!D.getAppleObjCSection().Data.empty())
2196 verifyAppleAccelTable(AccelSection: &D.getAppleObjCSection(), StrData: &StrData, SectionName: ".apple_objc");
2197
2198 if (!D.getNamesSection().Data.empty())
2199 verifyDebugNames(AccelSection: D.getNamesSection(), StrData);
2200 return ErrorCategory.GetNumErrors() == 0;
2201}
2202
2203bool DWARFVerifier::handleDebugStrOffsets() {
2204 OS << "Verifying .debug_str_offsets...\n";
2205 const DWARFObject &DObj = DCtx.getDWARFObj();
2206 bool Success = true;
2207
2208 // dwo sections may contain the legacy debug_str_offsets format (and they
2209 // can't be mixed with dwarf 5's format). This section format contains no
2210 // header.
2211 // As such, check the version from debug_info and, if we are in the legacy
2212 // mode (Dwarf <= 4), extract Dwarf32/Dwarf64.
2213 std::optional<DwarfFormat> DwoLegacyDwarf4Format;
2214 DObj.forEachInfoDWOSections(F: [&](const DWARFSection &S) {
2215 if (DwoLegacyDwarf4Format)
2216 return;
2217 DWARFDataExtractor DebugInfoData(DObj, S, DCtx.isLittleEndian(), 0);
2218 uint64_t Offset = 0;
2219 DwarfFormat InfoFormat = DebugInfoData.getInitialLength(Off: &Offset).second;
2220 if (uint16_t InfoVersion = DebugInfoData.getU16(offset_ptr: &Offset); InfoVersion <= 4)
2221 DwoLegacyDwarf4Format = InfoFormat;
2222 });
2223
2224 Success &= verifyDebugStrOffsets(
2225 LegacyFormat: DwoLegacyDwarf4Format, SectionName: ".debug_str_offsets.dwo",
2226 Section: DObj.getStrOffsetsDWOSection(), StrData: DObj.getStrDWOSection());
2227 Success &= verifyDebugStrOffsets(
2228 /*LegacyFormat=*/std::nullopt, SectionName: ".debug_str_offsets",
2229 Section: DObj.getStrOffsetsSection(), StrData: DObj.getStrSection());
2230 return Success;
2231}
2232
2233bool DWARFVerifier::verifyDebugStrOffsets(
2234 std::optional<DwarfFormat> LegacyFormat, StringRef SectionName,
2235 const DWARFSection &Section, StringRef StrData) {
2236 const DWARFObject &DObj = DCtx.getDWARFObj();
2237
2238 DWARFDataExtractor DA(DObj, Section, DCtx.isLittleEndian(), 0);
2239 DataExtractor::Cursor C(0);
2240 uint64_t NextUnit = 0;
2241 bool Success = true;
2242 while (C.seek(NewOffSet: NextUnit), C.tell() < DA.getData().size()) {
2243 DwarfFormat Format;
2244 uint64_t Length;
2245 uint64_t StartOffset = C.tell();
2246 if (LegacyFormat) {
2247 Format = *LegacyFormat;
2248 Length = DA.getData().size();
2249 NextUnit = C.tell() + Length;
2250 } else {
2251 std::tie(args&: Length, args&: Format) = DA.getInitialLength(C);
2252 if (!C)
2253 break;
2254 if (C.tell() + Length > DA.getData().size()) {
2255 ErrorCategory.Report(
2256 category: "Section contribution length exceeds available space", detailCallback: [&]() {
2257 error() << formatv(
2258 Fmt: "{0}: contribution {1:X}: length exceeds available space "
2259 "(contribution "
2260 "offset ({1:X}) + length field space ({2:X}) + length "
2261 "({3:X}) == "
2262 "{4:X} > section size {5:X})\n",
2263 Vals&: SectionName, Vals&: StartOffset, Vals: C.tell() - StartOffset, Vals&: Length,
2264 Vals: C.tell() + Length, Vals: DA.getData().size());
2265 });
2266 Success = false;
2267 // Nothing more to do - no other contributions to try.
2268 break;
2269 }
2270 NextUnit = C.tell() + Length;
2271 uint8_t Version = DA.getU16(C);
2272 if (C && Version != 5) {
2273 ErrorCategory.Report(category: "Invalid Section version", detailCallback: [&]() {
2274 error() << formatv(Fmt: "{0}: contribution {1:X}: invalid version {2}\n",
2275 Vals&: SectionName, Vals&: StartOffset, Vals&: Version);
2276 });
2277 Success = false;
2278 // Can't parse the rest of this contribution, since we don't know the
2279 // version, but we can pick up with the next contribution.
2280 continue;
2281 }
2282 (void)DA.getU16(C); // padding
2283 }
2284 uint64_t OffsetByteSize = getDwarfOffsetByteSize(Format);
2285 DA.setAddressSize(OffsetByteSize);
2286 uint64_t Remainder = (Length - 4) % OffsetByteSize;
2287 if (Remainder != 0) {
2288 ErrorCategory.Report(category: "Invalid section contribution length", detailCallback: [&]() {
2289 error() << formatv(
2290 Fmt: "{0}: contribution {1:X}: invalid length ((length ({2:X}) "
2291 "- header (0x4)) % offset size {3:X} == {4:X} != 0)\n",
2292 Vals&: SectionName, Vals&: StartOffset, Vals&: Length, Vals&: OffsetByteSize, Vals&: Remainder);
2293 });
2294 Success = false;
2295 }
2296 for (uint64_t Index = 0; C && C.tell() + OffsetByteSize <= NextUnit;
2297 ++Index) {
2298 uint64_t OffOff = C.tell();
2299 uint64_t StrOff = DA.getAddress(C);
2300 // check StrOff refers to the start of a string
2301 if (StrOff == 0)
2302 continue;
2303 if (StrData.size() <= StrOff) {
2304 ErrorCategory.Report(
2305 category: "String offset out of bounds of string section", detailCallback: [&]() {
2306 error() << formatv(
2307 Fmt: "{0}: contribution {1:X}: index {2:X}: invalid string "
2308 "offset *{3:X} == {4:X}, is beyond the bounds of the string "
2309 "section of length {5:X}\n",
2310 Vals&: SectionName, Vals&: StartOffset, Vals&: Index, Vals&: OffOff, Vals&: StrOff,
2311 Vals: StrData.size());
2312 });
2313 continue;
2314 }
2315 if (StrData[StrOff - 1] == '\0')
2316 continue;
2317 ErrorCategory.Report(
2318 category: "Section contribution contains invalid string offset", detailCallback: [&]() {
2319 error() << formatv(
2320 Fmt: "{0}: contribution {1:X}: index {2:X}: invalid string "
2321 "offset *{3:X} == {4:X}, is neither zero nor "
2322 "immediately following a null character\n",
2323 Vals&: SectionName, Vals&: StartOffset, Vals&: Index, Vals&: OffOff, Vals&: StrOff);
2324 });
2325 Success = false;
2326 }
2327 }
2328
2329 if (Error E = C.takeError()) {
2330 std::string Msg = toString(E: std::move(E));
2331 ErrorCategory.Report(category: "String offset error", detailCallback: [&]() {
2332 error() << formatv(Fmt: "{0}: {1}\n", Vals&: SectionName, Vals&: Msg);
2333 return false;
2334 });
2335 }
2336 return Success;
2337}
2338
2339void OutputCategoryAggregator::Report(
2340 StringRef s, std::function<void(void)> detailCallback) {
2341 this->Report(category: s, sub_category: "", detailCallback);
2342}
2343
2344void OutputCategoryAggregator::Report(
2345 StringRef category, StringRef sub_category,
2346 std::function<void(void)> detailCallback) {
2347 std::lock_guard<std::mutex> Lock(WriteMutex);
2348 ++NumErrors;
2349 std::string category_str = std::string(category);
2350 AggregationData &Agg = Aggregation[category_str];
2351 Agg.OverallCount++;
2352 if (!sub_category.empty()) {
2353 Agg.DetailedCounts[std::string(sub_category)]++;
2354 }
2355 if (IncludeDetail)
2356 detailCallback();
2357}
2358
2359void OutputCategoryAggregator::EnumerateResults(
2360 std::function<void(StringRef, unsigned)> handleCounts) {
2361 for (const auto &[name, aggData] : Aggregation) {
2362 handleCounts(name, aggData.OverallCount);
2363 }
2364}
2365void OutputCategoryAggregator::EnumerateDetailedResultsFor(
2366 StringRef category, std::function<void(StringRef, unsigned)> handleCounts) {
2367 const auto Agg = Aggregation.find(x: category);
2368 if (Agg != Aggregation.end()) {
2369 for (const auto &[name, aggData] : Agg->second.DetailedCounts) {
2370 handleCounts(name, aggData);
2371 }
2372 }
2373}
2374
2375void DWARFVerifier::summarize() {
2376 if (DumpOpts.ShowAggregateErrors && ErrorCategory.GetNumCategories()) {
2377 error() << "Aggregated error counts:\n";
2378 ErrorCategory.EnumerateResults(handleCounts: [&](StringRef s, unsigned count) {
2379 error() << formatv(Fmt: "{0} occurred {1} time(s).\n", Vals&: s, Vals&: count);
2380 });
2381 }
2382 if (!DumpOpts.JsonErrSummaryFile.empty()) {
2383 std::error_code EC;
2384 raw_fd_ostream JsonStream(DumpOpts.JsonErrSummaryFile, EC,
2385 sys::fs::OF_Text);
2386 if (EC) {
2387 error() << formatv(
2388 Fmt: "unable to open json summary file {0} for writing: {1}\n",
2389 Vals&: DumpOpts.JsonErrSummaryFile, Vals: EC.message());
2390 return;
2391 }
2392
2393 llvm::json::Object Categories;
2394 uint64_t ErrorCount = 0;
2395 ErrorCategory.EnumerateResults(handleCounts: [&](StringRef Category, unsigned Count) {
2396 llvm::json::Object Val;
2397 Val.try_emplace(K: "count", Args&: Count);
2398 llvm::json::Object Details;
2399 ErrorCategory.EnumerateDetailedResultsFor(
2400 category: Category, handleCounts: [&](StringRef SubCategory, unsigned SubCount) {
2401 Details.try_emplace(K: SubCategory, Args&: SubCount);
2402 });
2403 Val.try_emplace(K: "details", Args: std::move(Details));
2404 Categories.try_emplace(K: Category, Args: std::move(Val));
2405 ErrorCount += Count;
2406 });
2407 llvm::json::Object RootNode;
2408 RootNode.try_emplace(K: "error-categories", Args: std::move(Categories));
2409 RootNode.try_emplace(K: "error-count", Args&: ErrorCount);
2410
2411 JsonStream << llvm::json::Value(std::move(RootNode));
2412 }
2413}
2414
2415raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
2416
2417raw_ostream &DWARFVerifier::warn() const { return WithColor::warning(OS); }
2418
2419raw_ostream &DWARFVerifier::note() const { return WithColor::note(OS); }
2420
2421raw_ostream &DWARFVerifier::dump(const DWARFDie &Die, unsigned indent) const {
2422 Die.dump(OS, indent, DumpOpts);
2423 return OS;
2424}
2425