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