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