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