blob: 16e0d8aaf6b1615ece40c39c50cde7fc9997579c [file] [log] [blame]
Greg Claytonb8c162b2017-05-03 16:02:29 +00001//===- DWARFVerifier.cpp --------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000011#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000012#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
13#include "llvm/DebugInfo/DWARF/DWARFContext.h"
14#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
15#include "llvm/DebugInfo/DWARF/DWARFDie.h"
George Rimar144e4c52017-10-27 10:42:04 +000016#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000017#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
18#include "llvm/DebugInfo/DWARF/DWARFSection.h"
George Rimar144e4c52017-10-27 10:42:04 +000019#include "llvm/Support/FormatVariadic.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000020#include "llvm/Support/WithColor.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000021#include "llvm/Support/raw_ostream.h"
22#include <map>
23#include <set>
24#include <vector>
25
26using namespace llvm;
27using namespace dwarf;
28using namespace object;
29
Jonas Devlieghere58910602017-09-14 11:33:42 +000030DWARFVerifier::DieRangeInfo::address_range_iterator
31DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
32 auto Begin = Ranges.begin();
33 auto End = Ranges.end();
34 auto Pos = std::lower_bound(Begin, End, R);
35
36 if (Pos != End) {
37 if (Pos->intersects(R))
38 return Pos;
39 if (Pos != Begin) {
40 auto Iter = Pos - 1;
41 if (Iter->intersects(R))
42 return Iter;
43 }
44 }
45
46 Ranges.insert(Pos, R);
47 return Ranges.end();
48}
49
50DWARFVerifier::DieRangeInfo::die_range_info_iterator
51DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) {
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
63bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
64 // Both list of ranges are sorted so we can make this fast.
65
66 if (Ranges.empty() || RHS.Ranges.empty())
67 return false;
68
69 // Since the ranges are sorted we can advance where we start searching with
70 // this object's ranges as we traverse RHS.Ranges.
71 auto End = Ranges.end();
72 auto Iter = findRange(RHS.Ranges.front());
73
74 // Now linearly walk the ranges in this object and see if they contain each
75 // ranges from RHS.Ranges.
76 for (const auto &R : RHS.Ranges) {
77 while (Iter != End) {
78 if (Iter->contains(R))
79 break;
80 ++Iter;
81 }
82 if (Iter == End)
83 return false;
84 }
85 return true;
86}
87
88bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
89 if (Ranges.empty() || RHS.Ranges.empty())
90 return false;
91
92 auto End = Ranges.end();
93 auto Iter = findRange(RHS.Ranges.front());
94 for (const auto &R : RHS.Ranges) {
Jonas Devlieghered585a202017-09-14 17:46:23 +000095 if(Iter == End)
96 return false;
Jonas Devlieghere58910602017-09-14 11:33:42 +000097 if (R.HighPC <= Iter->LowPC)
98 continue;
99 while (Iter != End) {
100 if (Iter->intersects(R))
101 return true;
102 ++Iter;
103 }
104 }
105
106 return false;
107}
108
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000109bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000110 uint32_t *Offset, unsigned UnitIndex,
111 uint8_t &UnitType, bool &isUnitDWARF64) {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000112 uint32_t AbbrOffset, Length;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000113 uint8_t AddrSize = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000114 uint16_t Version;
115 bool Success = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000116
117 bool ValidLength = false;
118 bool ValidVersion = false;
119 bool ValidAddrSize = false;
120 bool ValidType = true;
121 bool ValidAbbrevOffset = true;
122
123 uint32_t OffsetStart = *Offset;
124 Length = DebugInfoData.getU32(Offset);
125 if (Length == UINT32_MAX) {
126 isUnitDWARF64 = true;
127 OS << format(
128 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
129 UnitIndex);
130 return false;
131 }
132 Version = DebugInfoData.getU16(Offset);
133
134 if (Version >= 5) {
135 UnitType = DebugInfoData.getU8(Offset);
136 AddrSize = DebugInfoData.getU8(Offset);
137 AbbrOffset = DebugInfoData.getU32(Offset);
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000138 ValidType = dwarf::isUnitType(UnitType);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000139 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000140 UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000141 AbbrOffset = DebugInfoData.getU32(Offset);
142 AddrSize = DebugInfoData.getU8(Offset);
143 }
144
145 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
146 ValidAbbrevOffset = false;
147
148 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
149 ValidVersion = DWARFContext::isSupportedVersion(Version);
150 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
151 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
152 !ValidType) {
153 Success = false;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000154 error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex,
155 OffsetStart);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000156 if (!ValidLength)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000157 note() << "The length for this unit is too "
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000158 "large for the .debug_info provided.\n";
159 if (!ValidVersion)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000160 note() << "The 16 bit unit header version is not valid.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000161 if (!ValidType)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000162 note() << "The unit type encoding is not valid.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000163 if (!ValidAbbrevOffset)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000164 note() << "The offset into the .debug_abbrev section is "
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000165 "not valid.\n";
166 if (!ValidAddrSize)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000167 note() << "The address size is unsupported.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000168 }
169 *Offset = OffsetStart + Length + 4;
170 return Success;
171}
172
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000173bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit, uint8_t UnitType) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000174 uint32_t NumUnitErrors = 0;
175 unsigned NumDies = Unit.getNumDIEs();
176 for (unsigned I = 0; I < NumDies; ++I) {
177 auto Die = Unit.getDIEAtIndex(I);
178 if (Die.getTag() == DW_TAG_null)
179 continue;
180 for (auto AttrValue : Die.attributes()) {
181 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
182 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
183 }
184 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000185
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000186 DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
187 if (!Die) {
188 error() << "Compilation unit without DIE.\n";
189 NumUnitErrors++;
190 return NumUnitErrors == 0;
191 }
192
193 if (!dwarf::isUnitType(Die.getTag())) {
194 error() << "Compilation unit root DIE is not a unit DIE: "
195 << dwarf::TagString(Die.getTag()) << ".\n";
Jonas Devlieghere35fdaa92017-09-28 15:57:50 +0000196 NumUnitErrors++;
197 }
198
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000199 if (UnitType != 0 &&
200 !DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Die.getTag())) {
201 error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType)
202 << ") and root DIE (" << dwarf::TagString(Die.getTag())
203 << ") do not match.\n";
204 NumUnitErrors++;
205 }
206
207 DieRangeInfo RI;
208 NumUnitErrors += verifyDieRanges(Die, RI);
209
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000210 return NumUnitErrors == 0;
211}
212
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000213unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000214 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000215 if (Abbrev) {
216 const DWARFAbbreviationDeclarationSet *AbbrDecls =
217 Abbrev->getAbbreviationDeclarationSet(0);
218 for (auto AbbrDecl : *AbbrDecls) {
219 SmallDenseSet<uint16_t> AttributeSet;
220 for (auto Attribute : AbbrDecl.attributes()) {
221 auto Result = AttributeSet.insert(Attribute.Attr);
222 if (!Result.second) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000223 error() << "Abbreviation declaration contains multiple "
224 << AttributeString(Attribute.Attr) << " attributes.\n";
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000225 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000226 ++NumErrors;
227 }
228 }
229 }
230 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000231 return NumErrors;
232}
233
234bool DWARFVerifier::handleDebugAbbrev() {
235 OS << "Verifying .debug_abbrev...\n";
236
237 const DWARFObject &DObj = DCtx.getDWARFObj();
238 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
239 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
240
241 if (noDebugAbbrev && noDebugAbbrevDWO) {
242 return true;
243 }
244
245 unsigned NumErrors = 0;
246 if (!noDebugAbbrev)
247 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
248
249 if (!noDebugAbbrevDWO)
250 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000251 return NumErrors == 0;
252}
253
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000254bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000255 OS << "Verifying .debug_info Unit Header Chain...\n";
256
Rafael Espindolac398e672017-07-19 22:27:28 +0000257 const DWARFObject &DObj = DCtx.getDWARFObj();
258 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
259 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000260 uint32_t NumDebugInfoErrors = 0;
261 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
262 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000263 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000264 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000265 bool hasDIE = DebugInfoData.isValidOffset(Offset);
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000266 DWARFUnitSection<DWARFTypeUnit> TUSection{};
267 DWARFUnitSection<DWARFCompileUnit> CUSection{};
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000268 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000269 OffsetStart = Offset;
270 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
271 isUnitDWARF64)) {
272 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000273 if (isUnitDWARF64)
274 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000275 } else {
276 std::unique_ptr<DWARFUnit> Unit;
277 switch (UnitType) {
278 case dwarf::DW_UT_type:
279 case dwarf::DW_UT_split_type: {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000280 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000281 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
282 &DObj.getRangeSection(), DObj.getStringSection(),
283 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
Paul Robinsond0c89f82018-01-29 22:02:56 +0000284 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
285 nullptr));
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000286 break;
287 }
288 case dwarf::DW_UT_skeleton:
289 case dwarf::DW_UT_split_compile:
290 case dwarf::DW_UT_compile:
291 case dwarf::DW_UT_partial:
292 // UnitType = 0 means that we are
293 // verifying a compile unit in DWARF v4.
294 case 0: {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000295 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000296 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
297 &DObj.getRangeSection(), DObj.getStringSection(),
298 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
Paul Robinsond0c89f82018-01-29 22:02:56 +0000299 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
300 nullptr));
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000301 break;
302 }
303 default: { llvm_unreachable("Invalid UnitType."); }
304 }
305 Unit->extract(DebugInfoData, &OffsetStart);
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000306 if (!verifyUnitContents(*Unit, UnitType))
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000307 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000308 }
309 hasDIE = DebugInfoData.isValidOffset(Offset);
310 ++UnitIdx;
311 }
312 if (UnitIdx == 0 && !hasDIE) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000313 warn() << ".debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000314 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000315 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000316 NumDebugInfoErrors += verifyDebugInfoReferences();
317 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000318}
319
Jonas Devlieghere58910602017-09-14 11:33:42 +0000320unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
321 DieRangeInfo &ParentRI) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000322 unsigned NumErrors = 0;
Jonas Devlieghere58910602017-09-14 11:33:42 +0000323
324 if (!Die.isValid())
325 return NumErrors;
326
327 DWARFAddressRangesVector Ranges = Die.getAddressRanges();
328
329 // Build RI for this DIE and check that ranges within this DIE do not
330 // overlap.
331 DieRangeInfo RI(Die);
332 for (auto Range : Ranges) {
333 if (!Range.valid()) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000334 ++NumErrors;
Jonas Devliegherea15f25d32017-09-29 15:41:22 +0000335 error() << "Invalid address range " << Range << "\n";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000336 continue;
337 }
338
339 // Verify that ranges don't intersect.
340 const auto IntersectingRange = RI.insert(Range);
341 if (IntersectingRange != RI.Ranges.end()) {
342 ++NumErrors;
Jonas Devliegherea15f25d32017-09-29 15:41:22 +0000343 error() << "DIE has overlapping address ranges: " << Range << " and "
344 << *IntersectingRange << "\n";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000345 break;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000346 }
347 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000348
349 // Verify that children don't intersect.
350 const auto IntersectingChild = ParentRI.insert(RI);
351 if (IntersectingChild != ParentRI.Children.end()) {
352 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000353 error() << "DIEs have overlapping address ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000354 Die.dump(OS, 0);
355 IntersectingChild->Die.dump(OS, 0);
356 OS << "\n";
357 }
358
359 // Verify that ranges are contained within their parent.
360 bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
361 !(Die.getTag() == DW_TAG_subprogram &&
362 ParentRI.Die.getTag() == DW_TAG_subprogram);
363 if (ShouldBeContained && !ParentRI.contains(RI)) {
364 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000365 error() << "DIE address ranges are not "
366 "contained in its parent's ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000367 Die.dump(OS, 0);
368 ParentRI.Die.dump(OS, 0);
369 OS << "\n";
370 }
371
372 // Recursively check children.
373 for (DWARFDie Child : Die)
374 NumErrors += verifyDieRanges(Child, RI);
375
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000376 return NumErrors;
377}
378
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000379unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
380 DWARFAttribute &AttrValue) {
381 unsigned NumErrors = 0;
George Rimar144e4c52017-10-27 10:42:04 +0000382 auto ReportError = [&](const Twine &TitleMsg) {
383 ++NumErrors;
384 error() << TitleMsg << '\n';
385 Die.dump(OS, 0, DumpOpts);
386 OS << "\n";
387 };
388
389 const DWARFObject &DObj = DCtx.getDWARFObj();
Greg Claytonc5b2d562017-05-03 18:25:46 +0000390 const auto Attr = AttrValue.Attr;
391 switch (Attr) {
392 case DW_AT_ranges:
393 // Make sure the offset in the DW_AT_ranges attribute is valid.
394 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
George Rimar144e4c52017-10-27 10:42:04 +0000395 if (*SectionOffset >= DObj.getRangeSection().Data.size())
396 ReportError("DW_AT_ranges offset is beyond .debug_ranges bounds:");
397 break;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000398 }
George Rimar144e4c52017-10-27 10:42:04 +0000399 ReportError("DIE has invalid DW_AT_ranges encoding:");
Greg Claytonc5b2d562017-05-03 18:25:46 +0000400 break;
401 case DW_AT_stmt_list:
402 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
403 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
George Rimar144e4c52017-10-27 10:42:04 +0000404 if (*SectionOffset >= DObj.getLineSection().Data.size())
405 ReportError("DW_AT_stmt_list offset is beyond .debug_line bounds: " +
George Rimar3d07f602017-10-27 10:58:04 +0000406 llvm::formatv("{0:x8}", *SectionOffset));
George Rimar144e4c52017-10-27 10:42:04 +0000407 break;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000408 }
George Rimar144e4c52017-10-27 10:42:04 +0000409 ReportError("DIE has invalid DW_AT_stmt_list encoding:");
Greg Claytonc5b2d562017-05-03 18:25:46 +0000410 break;
George Rimar144e4c52017-10-27 10:42:04 +0000411 case DW_AT_location: {
Jonas Devlieghere7d4a9742018-02-17 13:06:37 +0000412 auto VerifyLocation = [&](StringRef D) {
413 DWARFUnit *U = Die.getDwarfUnit();
414 DataExtractor Data(D, DCtx.isLittleEndian(), 0);
415 DWARFExpression Expression(Data, U->getVersion(),
416 U->getAddressByteSize());
417 bool Error = llvm::any_of(Expression, [](DWARFExpression::Operation &Op) {
418 return Op.isError();
419 });
420 if (Error)
421 ReportError("DIE contains invalid DWARF expression:");
422 };
423 if (Optional<ArrayRef<uint8_t>> Expr = AttrValue.Value.getAsBlock()) {
424 // Verify inlined location.
425 VerifyLocation(llvm::toStringRef(*Expr));
426 } else if (auto LocOffset = AttrValue.Value.getAsUnsignedConstant()) {
427 // Verify location list.
428 if (auto DebugLoc = DCtx.getDebugLoc())
429 if (auto LocList = DebugLoc->getLocationListAtOffset(*LocOffset))
430 for (const auto &Entry : LocList->Entries)
431 VerifyLocation({Entry.Loc.data(), Entry.Loc.size()});
George Rimar144e4c52017-10-27 10:42:04 +0000432 }
George Rimar144e4c52017-10-27 10:42:04 +0000433 break;
434 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000435
Greg Claytonc5b2d562017-05-03 18:25:46 +0000436 default:
437 break;
438 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000439 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000440}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000441
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000442unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
443 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000444 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000445 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000446 const auto Form = AttrValue.Value.getForm();
447 switch (Form) {
448 case DW_FORM_ref1:
449 case DW_FORM_ref2:
450 case DW_FORM_ref4:
451 case DW_FORM_ref8:
452 case DW_FORM_ref_udata: {
453 // Verify all CU relative references are valid CU offsets.
454 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
455 assert(RefVal);
456 if (RefVal) {
457 auto DieCU = Die.getDwarfUnit();
458 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
459 auto CUOffset = AttrValue.Value.getRawUValue();
460 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000461 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000462 error() << FormEncodingString(Form) << " CU offset "
463 << format("0x%08" PRIx64, CUOffset)
464 << " is invalid (must be less than CU size of "
465 << format("0x%08" PRIx32, CUSize) << "):\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000466 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000467 OS << "\n";
468 } else {
469 // Valid reference, but we will verify it points to an actual
470 // DIE later.
471 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000472 }
473 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000474 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000475 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000476 case DW_FORM_ref_addr: {
477 // Verify all absolute DIE references have valid offsets in the
478 // .debug_info section.
479 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
480 assert(RefVal);
481 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000482 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000483 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000484 error() << "DW_FORM_ref_addr offset beyond .debug_info "
485 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000486 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000487 OS << "\n";
488 } else {
489 // Valid reference, but we will verify it points to an actual
490 // DIE later.
491 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
492 }
493 }
494 break;
495 }
496 case DW_FORM_strp: {
497 auto SecOffset = AttrValue.Value.getAsSectionOffset();
498 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000499 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000500 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000501 error() << "DW_FORM_strp offset beyond .debug_str bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000502 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000503 OS << "\n";
504 }
505 break;
506 }
507 default:
508 break;
509 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000510 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000511}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000512
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000513unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000514 // Take all references and make sure they point to an actual DIE by
515 // getting the DIE by offset and emitting an error
516 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000517 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000518 for (auto Pair : ReferenceToDIEOffsets) {
519 auto Die = DCtx.getDIEForOffset(Pair.first);
520 if (Die)
521 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000522 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000523 error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
524 << ". Offset is in between DIEs:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000525 for (auto Offset : Pair.second) {
526 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000527 ReferencingDie.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000528 OS << "\n";
529 }
530 OS << "\n";
531 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000532 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000533}
534
Greg Claytonc5b2d562017-05-03 18:25:46 +0000535void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000536 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000537 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000538 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000539 // Get the attribute value as a section offset. No need to produce an
540 // error here if the encoding isn't correct because we validate this in
541 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000542 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000543 if (!StmtSectionOffset)
544 continue;
545 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000546 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000547 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000548 if (!LineTable) {
549 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000550 error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset)
551 << "] was not able to be parsed for CU:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000552 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000553 OS << '\n';
554 continue;
555 }
556 } else {
557 // Make sure we don't get a valid line table back if the offset is wrong.
558 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000559 // Skip this line table as it isn't valid. No need to create an error
560 // here because we validate this in the .debug_info verifier.
561 continue;
562 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000563 auto Iter = StmtListToDie.find(LineTableOffset);
564 if (Iter != StmtListToDie.end()) {
565 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000566 error() << "two compile unit DIEs, "
567 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
568 << format("0x%08" PRIx32, Die.getOffset())
569 << ", have the same DW_AT_stmt_list section offset:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000570 Iter->second.dump(OS, 0, DumpOpts);
571 Die.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000572 OS << '\n';
573 // Already verified this line table before, no need to do it again.
574 continue;
575 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000576 StmtListToDie[LineTableOffset] = Die;
577 }
578}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000579
Greg Claytonc5b2d562017-05-03 18:25:46 +0000580void DWARFVerifier::verifyDebugLineRows() {
581 for (const auto &CU : DCtx.compile_units()) {
582 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000583 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000584 // If there is no line table we will have created an error in the
585 // .debug_info verifier or in verifyDebugLineStmtOffsets().
586 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000587 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000588
589 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000590 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000591 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
592 uint32_t FileIndex = 1;
593 StringMap<uint16_t> FullPathMap;
594 for (const auto &FileName : LineTable->Prologue.FileNames) {
595 // Verify directory index.
596 if (FileName.DirIdx > MaxDirIndex) {
597 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000598 error() << ".debug_line["
599 << format("0x%08" PRIx64,
600 *toSectionOffset(Die.find(DW_AT_stmt_list)))
601 << "].prologue.file_names[" << FileIndex
602 << "].dir_idx contains an invalid index: " << FileName.DirIdx
603 << "\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000604 }
605
606 // Check file paths for duplicates.
607 std::string FullPath;
608 const bool HasFullPath = LineTable->getFileNameByIndex(
609 FileIndex, CU->getCompilationDir(),
610 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
611 assert(HasFullPath && "Invalid index?");
612 (void)HasFullPath;
613 auto It = FullPathMap.find(FullPath);
614 if (It == FullPathMap.end())
615 FullPathMap[FullPath] = FileIndex;
616 else if (It->second != FileIndex) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000617 warn() << ".debug_line["
618 << format("0x%08" PRIx64,
619 *toSectionOffset(Die.find(DW_AT_stmt_list)))
620 << "].prologue.file_names[" << FileIndex
621 << "] is a duplicate of file_names[" << It->second << "]\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000622 }
623
624 FileIndex++;
625 }
626
627 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000628 uint64_t PrevAddress = 0;
629 uint32_t RowIndex = 0;
630 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000631 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000632 if (Row.Address < PrevAddress) {
633 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000634 error() << ".debug_line["
635 << format("0x%08" PRIx64,
636 *toSectionOffset(Die.find(DW_AT_stmt_list)))
637 << "] row[" << RowIndex
638 << "] decreases in address from previous row:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000639
640 DWARFDebugLine::Row::dumpTableHeader(OS);
641 if (RowIndex > 0)
642 LineTable->Rows[RowIndex - 1].dump(OS);
643 Row.dump(OS);
644 OS << '\n';
645 }
646
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000647 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000648 if (Row.File > MaxFileIndex) {
649 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000650 error() << ".debug_line["
651 << format("0x%08" PRIx64,
652 *toSectionOffset(Die.find(DW_AT_stmt_list)))
653 << "][" << RowIndex << "] has invalid file index " << Row.File
654 << " (valid values are [1," << MaxFileIndex << "]):\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000655 DWARFDebugLine::Row::dumpTableHeader(OS);
656 Row.dump(OS);
657 OS << '\n';
658 }
659 if (Row.EndSequence)
660 PrevAddress = 0;
661 else
662 PrevAddress = Row.Address;
663 ++RowIndex;
664 }
665 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000666}
667
668bool DWARFVerifier::handleDebugLine() {
669 NumDebugLineErrors = 0;
670 OS << "Verifying .debug_line...\n";
671 verifyDebugLineStmtOffsets();
672 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000673 return NumDebugLineErrors == 0;
674}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000675
Pavel Labath9b36fd22018-01-22 13:17:23 +0000676unsigned DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
677 DataExtractor *StrData,
678 const char *SectionName) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000679 unsigned NumErrors = 0;
680 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
681 DCtx.isLittleEndian(), 0);
Pavel Labath9b36fd22018-01-22 13:17:23 +0000682 AppleAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000683
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000684 OS << "Verifying " << SectionName << "...\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000685
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000686 // Verify that the fixed part of the header is not too short.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000687 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000688 error() << "Section is too small to fit a section header.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000689 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000690 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000691
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000692 // Verify that the section is not too short.
Jonas Devlieghereba915892017-12-11 18:22:47 +0000693 if (Error E = AccelTable.extract()) {
694 error() << toString(std::move(E)) << '\n';
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000695 return 1;
696 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000697
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000698 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000699 uint32_t NumBuckets = AccelTable.getNumBuckets();
700 uint32_t NumHashes = AccelTable.getNumHashes();
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000701
702 uint32_t BucketsOffset =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000703 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000704 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
705 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000706 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000707 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000708 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000709 error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
710 HashIdx);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000711 ++NumErrors;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000712 }
713 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000714 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000715 if (NumAtoms == 0) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000716 error() << "No atoms: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000717 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000718 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000719 if (!AccelTable.validateForms()) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000720 error() << "Unsupported form: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000721 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000722 }
723
724 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
725 uint32_t HashOffset = HashesBase + 4 * HashIdx;
726 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000727 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
728 uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
729 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
730 sizeof(uint64_t))) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000731 error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n",
732 HashIdx, HashDataOffset);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000733 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000734 }
735
736 uint32_t StrpOffset;
737 uint32_t StringOffset;
738 uint32_t StringCount = 0;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000739 unsigned Offset;
740 unsigned Tag;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000741 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000742 const uint32_t NumHashDataObjects =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000743 AccelSectionData.getU32(&HashDataOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000744 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
745 ++HashDataIdx) {
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000746 std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
747 auto Die = DCtx.getDIEForOffset(Offset);
748 if (!Die) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000749 const uint32_t BucketIdx =
750 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
751 StringOffset = StrpOffset;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000752 const char *Name = StrData->getCStr(&StringOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000753 if (!Name)
754 Name = "<NULL>";
755
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000756 error() << format(
757 "%s Bucket[%d] Hash[%d] = 0x%08x "
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000758 "Str[%u] = 0x%08x "
759 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000760 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000761 HashDataIdx, Offset, Name);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000762
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000763 ++NumErrors;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000764 continue;
765 }
766 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000767 error() << "Tag " << dwarf::TagString(Tag)
768 << " in accelerator table does not match Tag "
769 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
770 << "].\n";
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000771 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000772 }
773 }
774 ++StringCount;
775 }
776 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000777 return NumErrors;
778}
779
Pavel Labathb136c392018-03-08 15:34:42 +0000780unsigned
781DWARFVerifier::verifyDebugNamesCULists(const DWARFDebugNames &AccelTable) {
782 // A map from CU offset to the (first) Name Index offset which claims to index
783 // this CU.
784 DenseMap<uint32_t, uint32_t> CUMap;
785 const uint32_t NotIndexed = std::numeric_limits<uint32_t>::max();
786
787 CUMap.reserve(DCtx.getNumCompileUnits());
788 for (const auto &CU : DCtx.compile_units())
789 CUMap[CU->getOffset()] = NotIndexed;
790
791 unsigned NumErrors = 0;
792 for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
793 if (NI.getCUCount() == 0) {
794 error() << formatv("Name Index @ {0:x} does not index any CU\n",
795 NI.getUnitOffset());
796 ++NumErrors;
797 continue;
798 }
799 for (uint32_t CU = 0, End = NI.getCUCount(); CU < End; ++CU) {
800 uint32_t Offset = NI.getCUOffset(CU);
801 auto Iter = CUMap.find(Offset);
802
803 if (Iter == CUMap.end()) {
804 error() << formatv(
805 "Name Index @ {0:x} references a non-existing CU @ {1:x}\n",
806 NI.getUnitOffset(), Offset);
807 ++NumErrors;
808 continue;
809 }
810
811 if (Iter->second != NotIndexed) {
812 error() << formatv("Name Index @ {0:x} references a CU @ {1:x}, but "
813 "this CU is already indexed by Name Index @ {2:x}\n",
814 NI.getUnitOffset(), Offset, Iter->second);
815 continue;
816 }
817 Iter->second = NI.getUnitOffset();
818 }
819 }
820
821 for (const auto &KV : CUMap) {
822 if (KV.second == NotIndexed)
823 warn() << formatv("CU @ {0:x} not covered by any Name Index\n", KV.first);
824 }
825
826 return NumErrors;
827}
828
829unsigned DWARFVerifier::verifyDebugNames(const DWARFSection &AccelSection,
830 const DataExtractor &StrData) {
831 unsigned NumErrors = 0;
832 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), AccelSection,
833 DCtx.isLittleEndian(), 0);
834 DWARFDebugNames AccelTable(AccelSectionData, StrData);
835
836 OS << "Verifying .debug_names...\n";
837
838 // This verifies that we can read individual name indices and their
839 // abbreviation tables.
840 if (Error E = AccelTable.extract()) {
841 error() << toString(std::move(E)) << '\n';
842 return 1;
843 }
844
845 NumErrors += verifyDebugNamesCULists(AccelTable);
846
847 for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
848 for (uint32_t Bucket = 0, End = NI.getBucketCount(); Bucket < End;
849 ++Bucket) {
850 uint32_t Index = NI.getBucketArrayEntry(Bucket);
851 if (Index > NI.getNameCount()) {
852 error() << formatv("Bucket {0} of Name Index @ {1:x} contains invalid "
853 "value {2}. Valid range is [0, {3}].\n",
854 Bucket, NI.getUnitOffset(), Index,
855 NI.getNameCount());
856 ++NumErrors;
857 }
858 }
859 }
860
861 return NumErrors;
862}
863
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000864bool DWARFVerifier::handleAccelTables() {
865 const DWARFObject &D = DCtx.getDWARFObj();
866 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
867 unsigned NumErrors = 0;
868 if (!D.getAppleNamesSection().Data.empty())
869 NumErrors +=
Pavel Labath9b36fd22018-01-22 13:17:23 +0000870 verifyAppleAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000871 if (!D.getAppleTypesSection().Data.empty())
872 NumErrors +=
Pavel Labath9b36fd22018-01-22 13:17:23 +0000873 verifyAppleAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000874 if (!D.getAppleNamespacesSection().Data.empty())
Pavel Labath9b36fd22018-01-22 13:17:23 +0000875 NumErrors += verifyAppleAccelTable(&D.getAppleNamespacesSection(), &StrData,
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000876 ".apple_namespaces");
877 if (!D.getAppleObjCSection().Data.empty())
878 NumErrors +=
Pavel Labath9b36fd22018-01-22 13:17:23 +0000879 verifyAppleAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
Pavel Labathb136c392018-03-08 15:34:42 +0000880
881 if (!D.getDebugNamesSection().Data.empty())
882 NumErrors += verifyDebugNames(D.getDebugNamesSection(), StrData);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000883 return NumErrors == 0;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000884}
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000885
886raw_ostream &DWARFVerifier::error() const {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000887 return WithColor(OS, HighlightColor::Error).get() << "error: ";
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000888}
889
890raw_ostream &DWARFVerifier::warn() const {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000891 return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000892}
893
894raw_ostream &DWARFVerifier::note() const {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000895 return WithColor(OS, HighlightColor::Note).get() << "note: ";
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000896}