blob: eadb982ba72aec83f886e53740f0cd27df71c9b4 [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"
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/DWARFFormValue.h"
16#include "llvm/DebugInfo/DWARF/DWARFSection.h"
Spyridoula Gravanie41823b2017-06-14 00:17:55 +000017#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000018#include "llvm/Support/raw_ostream.h"
19#include <map>
20#include <set>
21#include <vector>
22
23using namespace llvm;
24using namespace dwarf;
25using namespace object;
26
Jonas Devlieghere58910602017-09-14 11:33:42 +000027DWARFVerifier::DieRangeInfo::address_range_iterator
28DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
29 auto Begin = Ranges.begin();
30 auto End = Ranges.end();
31 auto Pos = std::lower_bound(Begin, End, R);
32
33 if (Pos != End) {
34 if (Pos->intersects(R))
35 return Pos;
36 if (Pos != Begin) {
37 auto Iter = Pos - 1;
38 if (Iter->intersects(R))
39 return Iter;
40 }
41 }
42
43 Ranges.insert(Pos, R);
44 return Ranges.end();
45}
46
47DWARFVerifier::DieRangeInfo::die_range_info_iterator
48DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) {
49 auto End = Children.end();
50 auto Iter = Children.begin();
51 while (Iter != End) {
52 if (Iter->intersects(RI))
53 return Iter;
54 ++Iter;
55 }
56 Children.insert(RI);
57 return Children.end();
58}
59
60bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
61 // Both list of ranges are sorted so we can make this fast.
62
63 if (Ranges.empty() || RHS.Ranges.empty())
64 return false;
65
66 // Since the ranges are sorted we can advance where we start searching with
67 // this object's ranges as we traverse RHS.Ranges.
68 auto End = Ranges.end();
69 auto Iter = findRange(RHS.Ranges.front());
70
71 // Now linearly walk the ranges in this object and see if they contain each
72 // ranges from RHS.Ranges.
73 for (const auto &R : RHS.Ranges) {
74 while (Iter != End) {
75 if (Iter->contains(R))
76 break;
77 ++Iter;
78 }
79 if (Iter == End)
80 return false;
81 }
82 return true;
83}
84
85bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
86 if (Ranges.empty() || RHS.Ranges.empty())
87 return false;
88
89 auto End = Ranges.end();
90 auto Iter = findRange(RHS.Ranges.front());
91 for (const auto &R : RHS.Ranges) {
Jonas Devlieghered585a202017-09-14 17:46:23 +000092 if(Iter == End)
93 return false;
Jonas Devlieghere58910602017-09-14 11:33:42 +000094 if (R.HighPC <= Iter->LowPC)
95 continue;
96 while (Iter != End) {
97 if (Iter->intersects(R))
98 return true;
99 ++Iter;
100 }
101 }
102
103 return false;
104}
105
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000106bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000107 uint32_t *Offset, unsigned UnitIndex,
108 uint8_t &UnitType, bool &isUnitDWARF64) {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000109 uint32_t AbbrOffset, Length;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000110 uint8_t AddrSize = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000111 uint16_t Version;
112 bool Success = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000113
114 bool ValidLength = false;
115 bool ValidVersion = false;
116 bool ValidAddrSize = false;
117 bool ValidType = true;
118 bool ValidAbbrevOffset = true;
119
120 uint32_t OffsetStart = *Offset;
121 Length = DebugInfoData.getU32(Offset);
122 if (Length == UINT32_MAX) {
123 isUnitDWARF64 = true;
124 OS << format(
125 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
126 UnitIndex);
127 return false;
128 }
129 Version = DebugInfoData.getU16(Offset);
130
131 if (Version >= 5) {
132 UnitType = DebugInfoData.getU8(Offset);
133 AddrSize = DebugInfoData.getU8(Offset);
134 AbbrOffset = DebugInfoData.getU32(Offset);
135 ValidType = DWARFUnit::isValidUnitType(UnitType);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000136 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000137 UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000138 AbbrOffset = DebugInfoData.getU32(Offset);
139 AddrSize = DebugInfoData.getU8(Offset);
140 }
141
142 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
143 ValidAbbrevOffset = false;
144
145 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
146 ValidVersion = DWARFContext::isSupportedVersion(Version);
147 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
148 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
149 !ValidType) {
150 Success = false;
151 OS << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart);
152 if (!ValidLength)
153 OS << "\tError: The length for this unit is too "
154 "large for the .debug_info provided.\n";
155 if (!ValidVersion)
156 OS << "\tError: The 16 bit unit header version is not valid.\n";
157 if (!ValidType)
158 OS << "\tError: The unit type encoding is not valid.\n";
159 if (!ValidAbbrevOffset)
160 OS << "\tError: The offset into the .debug_abbrev section is "
161 "not valid.\n";
162 if (!ValidAddrSize)
163 OS << "\tError: The address size is unsupported.\n";
164 }
165 *Offset = OffsetStart + Length + 4;
166 return Success;
167}
168
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000169bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) {
170 uint32_t NumUnitErrors = 0;
171 unsigned NumDies = Unit.getNumDIEs();
172 for (unsigned I = 0; I < NumDies; ++I) {
173 auto Die = Unit.getDIEAtIndex(I);
174 if (Die.getTag() == DW_TAG_null)
175 continue;
176 for (auto AttrValue : Die.attributes()) {
177 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
178 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
179 }
180 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000181
Jonas Devlieghere35fdaa92017-09-28 15:57:50 +0000182 if (DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false)) {
183 DieRangeInfo RI;
184 NumUnitErrors += verifyDieRanges(Die, RI);
185 } else {
186 OS << "error: Compilation unit without unit DIE.\n";
187 NumUnitErrors++;
188 }
189
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000190 return NumUnitErrors == 0;
191}
192
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000193unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000194 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000195 if (Abbrev) {
196 const DWARFAbbreviationDeclarationSet *AbbrDecls =
197 Abbrev->getAbbreviationDeclarationSet(0);
198 for (auto AbbrDecl : *AbbrDecls) {
199 SmallDenseSet<uint16_t> AttributeSet;
200 for (auto Attribute : AbbrDecl.attributes()) {
201 auto Result = AttributeSet.insert(Attribute.Attr);
202 if (!Result.second) {
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000203 OS << "Error: Abbreviation declaration contains multiple "
204 << AttributeString(Attribute.Attr) << " attributes.\n";
205 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000206 ++NumErrors;
207 }
208 }
209 }
210 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000211 return NumErrors;
212}
213
214bool DWARFVerifier::handleDebugAbbrev() {
215 OS << "Verifying .debug_abbrev...\n";
216
217 const DWARFObject &DObj = DCtx.getDWARFObj();
218 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
219 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
220
221 if (noDebugAbbrev && noDebugAbbrevDWO) {
222 return true;
223 }
224
225 unsigned NumErrors = 0;
226 if (!noDebugAbbrev)
227 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
228
229 if (!noDebugAbbrevDWO)
230 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000231 return NumErrors == 0;
232}
233
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000234bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000235 OS << "Verifying .debug_info Unit Header Chain...\n";
236
Rafael Espindolac398e672017-07-19 22:27:28 +0000237 const DWARFObject &DObj = DCtx.getDWARFObj();
238 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
239 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000240 uint32_t NumDebugInfoErrors = 0;
241 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
242 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000243 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000244 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000245 bool hasDIE = DebugInfoData.isValidOffset(Offset);
246 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000247 OffsetStart = Offset;
248 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
249 isUnitDWARF64)) {
250 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000251 if (isUnitDWARF64)
252 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000253 } else {
254 std::unique_ptr<DWARFUnit> Unit;
255 switch (UnitType) {
256 case dwarf::DW_UT_type:
257 case dwarf::DW_UT_split_type: {
258 DWARFUnitSection<DWARFTypeUnit> TUSection{};
259 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000260 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
261 &DObj.getRangeSection(), DObj.getStringSection(),
262 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
263 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000264 nullptr));
265 break;
266 }
267 case dwarf::DW_UT_skeleton:
268 case dwarf::DW_UT_split_compile:
269 case dwarf::DW_UT_compile:
270 case dwarf::DW_UT_partial:
271 // UnitType = 0 means that we are
272 // verifying a compile unit in DWARF v4.
273 case 0: {
274 DWARFUnitSection<DWARFCompileUnit> CUSection{};
275 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000276 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
277 &DObj.getRangeSection(), DObj.getStringSection(),
278 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
279 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000280 nullptr));
281 break;
282 }
283 default: { llvm_unreachable("Invalid UnitType."); }
284 }
285 Unit->extract(DebugInfoData, &OffsetStart);
286 if (!verifyUnitContents(*Unit))
287 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000288 }
289 hasDIE = DebugInfoData.isValidOffset(Offset);
290 ++UnitIdx;
291 }
292 if (UnitIdx == 0 && !hasDIE) {
293 OS << "Warning: .debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000294 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000295 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000296 NumDebugInfoErrors += verifyDebugInfoReferences();
297 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000298}
299
Jonas Devlieghere58910602017-09-14 11:33:42 +0000300unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
301 DieRangeInfo &ParentRI) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000302 unsigned NumErrors = 0;
Jonas Devlieghere58910602017-09-14 11:33:42 +0000303
304 if (!Die.isValid())
305 return NumErrors;
306
307 DWARFAddressRangesVector Ranges = Die.getAddressRanges();
308
309 // Build RI for this DIE and check that ranges within this DIE do not
310 // overlap.
311 DieRangeInfo RI(Die);
312 for (auto Range : Ranges) {
313 if (!Range.valid()) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000314 ++NumErrors;
NAKAMURA Takumi7ddaf3c2017-07-25 05:03:17 +0000315 OS << format("error: Invalid address range [0x%08" PRIx64
316 " - 0x%08" PRIx64 "].\n",
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000317 Range.LowPC, Range.HighPC);
Jonas Devlieghere58910602017-09-14 11:33:42 +0000318 continue;
319 }
320
321 // Verify that ranges don't intersect.
322 const auto IntersectingRange = RI.insert(Range);
323 if (IntersectingRange != RI.Ranges.end()) {
324 ++NumErrors;
325 OS << format("error: DIE has overlapping address ranges: [0x%08" PRIx64
326 " - 0x%08" PRIx64 "] and [0x%08" PRIx64 " - 0x%08" PRIx64
327 "].\n",
328 Range.LowPC, Range.HighPC, IntersectingRange->LowPC,
329 IntersectingRange->HighPC);
330 break;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000331 }
332 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000333
334 // Verify that children don't intersect.
335 const auto IntersectingChild = ParentRI.insert(RI);
336 if (IntersectingChild != ParentRI.Children.end()) {
337 ++NumErrors;
338 OS << "error: DIEs have overlapping address ranges:";
339 Die.dump(OS, 0);
340 IntersectingChild->Die.dump(OS, 0);
341 OS << "\n";
342 }
343
344 // Verify that ranges are contained within their parent.
345 bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
346 !(Die.getTag() == DW_TAG_subprogram &&
347 ParentRI.Die.getTag() == DW_TAG_subprogram);
348 if (ShouldBeContained && !ParentRI.contains(RI)) {
349 ++NumErrors;
350 OS << "error: DIE address ranges are not "
351 "contained in its parent's ranges:";
352 Die.dump(OS, 0);
353 ParentRI.Die.dump(OS, 0);
354 OS << "\n";
355 }
356
357 // Recursively check children.
358 for (DWARFDie Child : Die)
359 NumErrors += verifyDieRanges(Child, RI);
360
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000361 return NumErrors;
362}
363
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000364unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
365 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000366 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000367 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000368 const auto Attr = AttrValue.Attr;
369 switch (Attr) {
370 case DW_AT_ranges:
371 // Make sure the offset in the DW_AT_ranges attribute is valid.
372 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000373 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000374 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000375 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
376 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000377 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000378 OS << "\n";
379 }
380 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000381 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000382 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000383 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000384 OS << "\n";
385 }
386 break;
387 case DW_AT_stmt_list:
388 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
389 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000390 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000391 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000392 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
393 "bounds: "
Simon Dardis02d99452017-08-07 15:37:57 +0000394 << format("0x%08" PRIx64, *SectionOffset) << "\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000395 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000396 OS << "\n";
397 }
398 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000399 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000400 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000401 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000402 OS << "\n";
403 }
404 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000405
Greg Claytonc5b2d562017-05-03 18:25:46 +0000406 default:
407 break;
408 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000409 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000410}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000411
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000412unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
413 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000414 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000415 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000416 const auto Form = AttrValue.Value.getForm();
417 switch (Form) {
418 case DW_FORM_ref1:
419 case DW_FORM_ref2:
420 case DW_FORM_ref4:
421 case DW_FORM_ref8:
422 case DW_FORM_ref_udata: {
423 // Verify all CU relative references are valid CU offsets.
424 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
425 assert(RefVal);
426 if (RefVal) {
427 auto DieCU = Die.getDwarfUnit();
428 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
429 auto CUOffset = AttrValue.Value.getRawUValue();
430 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000431 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000432 OS << "error: " << FormEncodingString(Form) << " CU offset "
Simon Dardis02d99452017-08-07 15:37:57 +0000433 << format("0x%08" PRIx64, CUOffset)
Greg Claytonc5b2d562017-05-03 18:25:46 +0000434 << " is invalid (must be less than CU size of "
435 << format("0x%08" PRIx32, CUSize) << "):\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000436 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000437 OS << "\n";
438 } else {
439 // Valid reference, but we will verify it points to an actual
440 // DIE later.
441 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000442 }
443 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000444 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000445 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000446 case DW_FORM_ref_addr: {
447 // Verify all absolute DIE references have valid offsets in the
448 // .debug_info section.
449 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
450 assert(RefVal);
451 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000452 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000453 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000454 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
455 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000456 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000457 OS << "\n";
458 } else {
459 // Valid reference, but we will verify it points to an actual
460 // DIE later.
461 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
462 }
463 }
464 break;
465 }
466 case DW_FORM_strp: {
467 auto SecOffset = AttrValue.Value.getAsSectionOffset();
468 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000469 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000470 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000471 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000472 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000473 OS << "\n";
474 }
475 break;
476 }
477 default:
478 break;
479 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000480 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000481}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000482
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000483unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000484 // Take all references and make sure they point to an actual DIE by
485 // getting the DIE by offset and emitting an error
486 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000487 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000488 for (auto Pair : ReferenceToDIEOffsets) {
489 auto Die = DCtx.getDIEForOffset(Pair.first);
490 if (Die)
491 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000492 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000493 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
494 << ". Offset is in between DIEs:\n";
495 for (auto Offset : Pair.second) {
496 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000497 ReferencingDie.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000498 OS << "\n";
499 }
500 OS << "\n";
501 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000502 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000503}
504
Greg Claytonc5b2d562017-05-03 18:25:46 +0000505void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000506 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000507 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000508 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000509 // Get the attribute value as a section offset. No need to produce an
510 // error here if the encoding isn't correct because we validate this in
511 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000512 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000513 if (!StmtSectionOffset)
514 continue;
515 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000516 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000517 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000518 if (!LineTable) {
519 ++NumDebugLineErrors;
520 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
521 << "] was not able to be parsed for CU:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000522 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000523 OS << '\n';
524 continue;
525 }
526 } else {
527 // Make sure we don't get a valid line table back if the offset is wrong.
528 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000529 // Skip this line table as it isn't valid. No need to create an error
530 // here because we validate this in the .debug_info verifier.
531 continue;
532 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000533 auto Iter = StmtListToDie.find(LineTableOffset);
534 if (Iter != StmtListToDie.end()) {
535 ++NumDebugLineErrors;
536 OS << "error: two compile unit DIEs, "
537 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000538 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000539 << ", have the same DW_AT_stmt_list section offset:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000540 Iter->second.dump(OS, 0, DumpOpts);
541 Die.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000542 OS << '\n';
543 // Already verified this line table before, no need to do it again.
544 continue;
545 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000546 StmtListToDie[LineTableOffset] = Die;
547 }
548}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000549
Greg Claytonc5b2d562017-05-03 18:25:46 +0000550void DWARFVerifier::verifyDebugLineRows() {
551 for (const auto &CU : DCtx.compile_units()) {
552 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000553 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000554 // If there is no line table we will have created an error in the
555 // .debug_info verifier or in verifyDebugLineStmtOffsets().
556 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000557 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000558
559 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000560 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000561 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
562 uint32_t FileIndex = 1;
563 StringMap<uint16_t> FullPathMap;
564 for (const auto &FileName : LineTable->Prologue.FileNames) {
565 // Verify directory index.
566 if (FileName.DirIdx > MaxDirIndex) {
567 ++NumDebugLineErrors;
568 OS << "error: .debug_line["
569 << format("0x%08" PRIx64,
570 *toSectionOffset(Die.find(DW_AT_stmt_list)))
571 << "].prologue.file_names[" << FileIndex
572 << "].dir_idx contains an invalid index: " << FileName.DirIdx
573 << "\n";
574 }
575
576 // Check file paths for duplicates.
577 std::string FullPath;
578 const bool HasFullPath = LineTable->getFileNameByIndex(
579 FileIndex, CU->getCompilationDir(),
580 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
581 assert(HasFullPath && "Invalid index?");
582 (void)HasFullPath;
583 auto It = FullPathMap.find(FullPath);
584 if (It == FullPathMap.end())
585 FullPathMap[FullPath] = FileIndex;
586 else if (It->second != FileIndex) {
587 OS << "warning: .debug_line["
588 << format("0x%08" PRIx64,
589 *toSectionOffset(Die.find(DW_AT_stmt_list)))
590 << "].prologue.file_names[" << FileIndex
591 << "] is a duplicate of file_names[" << It->second << "]\n";
592 }
593
594 FileIndex++;
595 }
596
597 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000598 uint64_t PrevAddress = 0;
599 uint32_t RowIndex = 0;
600 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000601 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000602 if (Row.Address < PrevAddress) {
603 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000604 OS << "error: .debug_line["
Simon Dardis02d99452017-08-07 15:37:57 +0000605 << format("0x%08" PRIx64,
Greg Claytonc5b2d562017-05-03 18:25:46 +0000606 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000607 << "] row[" << RowIndex
608 << "] decreases in address from previous row:\n";
609
610 DWARFDebugLine::Row::dumpTableHeader(OS);
611 if (RowIndex > 0)
612 LineTable->Rows[RowIndex - 1].dump(OS);
613 Row.dump(OS);
614 OS << '\n';
615 }
616
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000617 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000618 if (Row.File > MaxFileIndex) {
619 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000620 OS << "error: .debug_line["
Simon Dardis02d99452017-08-07 15:37:57 +0000621 << format("0x%08" PRIx64,
Greg Claytonc5b2d562017-05-03 18:25:46 +0000622 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000623 << "][" << RowIndex << "] has invalid file index " << Row.File
624 << " (valid values are [1," << MaxFileIndex << "]):\n";
625 DWARFDebugLine::Row::dumpTableHeader(OS);
626 Row.dump(OS);
627 OS << '\n';
628 }
629 if (Row.EndSequence)
630 PrevAddress = 0;
631 else
632 PrevAddress = Row.Address;
633 ++RowIndex;
634 }
635 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000636}
637
638bool DWARFVerifier::handleDebugLine() {
639 NumDebugLineErrors = 0;
640 OS << "Verifying .debug_line...\n";
641 verifyDebugLineStmtOffsets();
642 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000643 return NumDebugLineErrors == 0;
644}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000645
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000646unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection,
647 DataExtractor *StrData,
648 const char *SectionName) {
649 unsigned NumErrors = 0;
650 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
651 DCtx.isLittleEndian(), 0);
652 DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000653
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000654 OS << "Verifying " << SectionName << "...\n";
655 // Verify that the fixed part of the header is not too short.
656
657 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
658 OS << "\terror: Section is too small to fit a section header.\n";
659 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000660 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000661 // Verify that the section is not too short.
662 if (!AccelTable.extract()) {
663 OS << "\terror: Section is smaller than size described in section header.\n";
664 return 1;
665 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000666 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000667 uint32_t NumBuckets = AccelTable.getNumBuckets();
668 uint32_t NumHashes = AccelTable.getNumHashes();
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000669
670 uint32_t BucketsOffset =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000671 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000672 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
673 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000674 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000675 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000676 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000677 OS << format("\terror: Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000678 HashIdx);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000679 ++NumErrors;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000680 }
681 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000682 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000683 if (NumAtoms == 0) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000684 OS << "\terror: no atoms; failed to read HashData.\n";
685 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000686 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000687 if (!AccelTable.validateForms()) {
688 OS << "\terror: unsupported form; failed to read HashData.\n";
689 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000690 }
691
692 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
693 uint32_t HashOffset = HashesBase + 4 * HashIdx;
694 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000695 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
696 uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
697 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
698 sizeof(uint64_t))) {
699 OS << format("\terror: Hash[%d] has invalid HashData offset: 0x%08x.\n",
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000700 HashIdx, HashDataOffset);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000701 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000702 }
703
704 uint32_t StrpOffset;
705 uint32_t StringOffset;
706 uint32_t StringCount = 0;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000707 unsigned Offset;
708 unsigned Tag;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000709 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000710 const uint32_t NumHashDataObjects =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000711 AccelSectionData.getU32(&HashDataOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000712 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
713 ++HashDataIdx) {
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000714 std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
715 auto Die = DCtx.getDIEForOffset(Offset);
716 if (!Die) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000717 const uint32_t BucketIdx =
718 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
719 StringOffset = StrpOffset;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000720 const char *Name = StrData->getCStr(&StringOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000721 if (!Name)
722 Name = "<NULL>";
723
724 OS << format(
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000725 "\terror: %s Bucket[%d] Hash[%d] = 0x%08x "
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000726 "Str[%u] = 0x%08x "
727 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000728 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000729 HashDataIdx, Offset, Name);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000730
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000731 ++NumErrors;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000732 continue;
733 }
734 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
735 OS << "\terror: Tag " << dwarf::TagString(Tag)
736 << " in accelerator table does not match Tag "
737 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
738 << "].\n";
739 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000740 }
741 }
742 ++StringCount;
743 }
744 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000745 return NumErrors;
746}
747
748bool DWARFVerifier::handleAccelTables() {
749 const DWARFObject &D = DCtx.getDWARFObj();
750 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
751 unsigned NumErrors = 0;
752 if (!D.getAppleNamesSection().Data.empty())
753 NumErrors +=
754 verifyAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
755 if (!D.getAppleTypesSection().Data.empty())
756 NumErrors +=
757 verifyAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
758 if (!D.getAppleNamespacesSection().Data.empty())
759 NumErrors += verifyAccelTable(&D.getAppleNamespacesSection(), &StrData,
760 ".apple_namespaces");
761 if (!D.getAppleObjCSection().Data.empty())
762 NumErrors +=
763 verifyAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
764 return NumErrors == 0;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000765}