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