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