DWARFVerifier.cpp 56 KB

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