blob: 096d5ff8c33f4993e4c2011d885b52b1c10bba88 [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
182 DieRangeInfo RI;
183 DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
184 NumUnitErrors += verifyDieRanges(Die, RI);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000185 return NumUnitErrors == 0;
186}
187
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000188unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000189 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000190 if (Abbrev) {
191 const DWARFAbbreviationDeclarationSet *AbbrDecls =
192 Abbrev->getAbbreviationDeclarationSet(0);
193 for (auto AbbrDecl : *AbbrDecls) {
194 SmallDenseSet<uint16_t> AttributeSet;
195 for (auto Attribute : AbbrDecl.attributes()) {
196 auto Result = AttributeSet.insert(Attribute.Attr);
197 if (!Result.second) {
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000198 OS << "Error: Abbreviation declaration contains multiple "
199 << AttributeString(Attribute.Attr) << " attributes.\n";
200 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000201 ++NumErrors;
202 }
203 }
204 }
205 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000206 return NumErrors;
207}
208
209bool DWARFVerifier::handleDebugAbbrev() {
210 OS << "Verifying .debug_abbrev...\n";
211
212 const DWARFObject &DObj = DCtx.getDWARFObj();
213 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
214 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
215
216 if (noDebugAbbrev && noDebugAbbrevDWO) {
217 return true;
218 }
219
220 unsigned NumErrors = 0;
221 if (!noDebugAbbrev)
222 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
223
224 if (!noDebugAbbrevDWO)
225 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000226 return NumErrors == 0;
227}
228
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000229bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000230 OS << "Verifying .debug_info Unit Header Chain...\n";
231
Rafael Espindolac398e672017-07-19 22:27:28 +0000232 const DWARFObject &DObj = DCtx.getDWARFObj();
233 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
234 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000235 uint32_t NumDebugInfoErrors = 0;
236 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
237 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000238 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000239 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000240 bool hasDIE = DebugInfoData.isValidOffset(Offset);
241 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000242 OffsetStart = Offset;
243 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
244 isUnitDWARF64)) {
245 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000246 if (isUnitDWARF64)
247 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000248 } else {
249 std::unique_ptr<DWARFUnit> Unit;
250 switch (UnitType) {
251 case dwarf::DW_UT_type:
252 case dwarf::DW_UT_split_type: {
253 DWARFUnitSection<DWARFTypeUnit> TUSection{};
254 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000255 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
256 &DObj.getRangeSection(), DObj.getStringSection(),
257 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
258 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000259 nullptr));
260 break;
261 }
262 case dwarf::DW_UT_skeleton:
263 case dwarf::DW_UT_split_compile:
264 case dwarf::DW_UT_compile:
265 case dwarf::DW_UT_partial:
266 // UnitType = 0 means that we are
267 // verifying a compile unit in DWARF v4.
268 case 0: {
269 DWARFUnitSection<DWARFCompileUnit> CUSection{};
270 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000271 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
272 &DObj.getRangeSection(), DObj.getStringSection(),
273 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
274 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000275 nullptr));
276 break;
277 }
278 default: { llvm_unreachable("Invalid UnitType."); }
279 }
280 Unit->extract(DebugInfoData, &OffsetStart);
281 if (!verifyUnitContents(*Unit))
282 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000283 }
284 hasDIE = DebugInfoData.isValidOffset(Offset);
285 ++UnitIdx;
286 }
287 if (UnitIdx == 0 && !hasDIE) {
288 OS << "Warning: .debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000289 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000290 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000291 NumDebugInfoErrors += verifyDebugInfoReferences();
292 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000293}
294
Jonas Devlieghere58910602017-09-14 11:33:42 +0000295unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
296 DieRangeInfo &ParentRI) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000297 unsigned NumErrors = 0;
Jonas Devlieghere58910602017-09-14 11:33:42 +0000298
299 if (!Die.isValid())
300 return NumErrors;
301
302 DWARFAddressRangesVector Ranges = Die.getAddressRanges();
303
304 // Build RI for this DIE and check that ranges within this DIE do not
305 // overlap.
306 DieRangeInfo RI(Die);
307 for (auto Range : Ranges) {
308 if (!Range.valid()) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000309 ++NumErrors;
NAKAMURA Takumi7ddaf3c2017-07-25 05:03:17 +0000310 OS << format("error: Invalid address range [0x%08" PRIx64
311 " - 0x%08" PRIx64 "].\n",
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000312 Range.LowPC, Range.HighPC);
Jonas Devlieghere58910602017-09-14 11:33:42 +0000313 continue;
314 }
315
316 // Verify that ranges don't intersect.
317 const auto IntersectingRange = RI.insert(Range);
318 if (IntersectingRange != RI.Ranges.end()) {
319 ++NumErrors;
320 OS << format("error: DIE has overlapping address ranges: [0x%08" PRIx64
321 " - 0x%08" PRIx64 "] and [0x%08" PRIx64 " - 0x%08" PRIx64
322 "].\n",
323 Range.LowPC, Range.HighPC, IntersectingRange->LowPC,
324 IntersectingRange->HighPC);
325 break;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000326 }
327 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000328
329 // Verify that children don't intersect.
330 const auto IntersectingChild = ParentRI.insert(RI);
331 if (IntersectingChild != ParentRI.Children.end()) {
332 ++NumErrors;
333 OS << "error: DIEs have overlapping address ranges:";
334 Die.dump(OS, 0);
335 IntersectingChild->Die.dump(OS, 0);
336 OS << "\n";
337 }
338
339 // Verify that ranges are contained within their parent.
340 bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
341 !(Die.getTag() == DW_TAG_subprogram &&
342 ParentRI.Die.getTag() == DW_TAG_subprogram);
343 if (ShouldBeContained && !ParentRI.contains(RI)) {
344 ++NumErrors;
345 OS << "error: DIE address ranges are not "
346 "contained in its parent's ranges:";
347 Die.dump(OS, 0);
348 ParentRI.Die.dump(OS, 0);
349 OS << "\n";
350 }
351
352 // Recursively check children.
353 for (DWARFDie Child : Die)
354 NumErrors += verifyDieRanges(Child, RI);
355
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000356 return NumErrors;
357}
358
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000359unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
360 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000361 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000362 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000363 const auto Attr = AttrValue.Attr;
364 switch (Attr) {
365 case DW_AT_ranges:
366 // Make sure the offset in the DW_AT_ranges attribute is valid.
367 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000368 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000369 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000370 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
371 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000372 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000373 OS << "\n";
374 }
375 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000376 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000377 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000378 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000379 OS << "\n";
380 }
381 break;
382 case DW_AT_stmt_list:
383 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
384 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000385 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000386 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000387 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
388 "bounds: "
Simon Dardis02d99452017-08-07 15:37:57 +0000389 << format("0x%08" PRIx64, *SectionOffset) << "\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000390 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000391 OS << "\n";
392 }
393 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000394 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000395 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000396 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000397 OS << "\n";
398 }
399 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000400
Greg Claytonc5b2d562017-05-03 18:25:46 +0000401 default:
402 break;
403 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000404 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000405}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000406
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000407unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
408 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000409 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000410 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000411 const auto Form = AttrValue.Value.getForm();
412 switch (Form) {
413 case DW_FORM_ref1:
414 case DW_FORM_ref2:
415 case DW_FORM_ref4:
416 case DW_FORM_ref8:
417 case DW_FORM_ref_udata: {
418 // Verify all CU relative references are valid CU offsets.
419 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
420 assert(RefVal);
421 if (RefVal) {
422 auto DieCU = Die.getDwarfUnit();
423 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
424 auto CUOffset = AttrValue.Value.getRawUValue();
425 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000426 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000427 OS << "error: " << FormEncodingString(Form) << " CU offset "
Simon Dardis02d99452017-08-07 15:37:57 +0000428 << format("0x%08" PRIx64, CUOffset)
Greg Claytonc5b2d562017-05-03 18:25:46 +0000429 << " is invalid (must be less than CU size of "
430 << format("0x%08" PRIx32, CUSize) << "):\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000431 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000432 OS << "\n";
433 } else {
434 // Valid reference, but we will verify it points to an actual
435 // DIE later.
436 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000437 }
438 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000439 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000440 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000441 case DW_FORM_ref_addr: {
442 // Verify all absolute DIE references have valid offsets in the
443 // .debug_info section.
444 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
445 assert(RefVal);
446 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000447 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000448 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000449 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
450 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000451 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000452 OS << "\n";
453 } else {
454 // Valid reference, but we will verify it points to an actual
455 // DIE later.
456 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
457 }
458 }
459 break;
460 }
461 case DW_FORM_strp: {
462 auto SecOffset = AttrValue.Value.getAsSectionOffset();
463 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000464 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000465 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000466 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000467 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000468 OS << "\n";
469 }
470 break;
471 }
472 default:
473 break;
474 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000475 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000476}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000477
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000478unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000479 // Take all references and make sure they point to an actual DIE by
480 // getting the DIE by offset and emitting an error
481 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000482 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000483 for (auto Pair : ReferenceToDIEOffsets) {
484 auto Die = DCtx.getDIEForOffset(Pair.first);
485 if (Die)
486 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000487 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000488 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
489 << ". Offset is in between DIEs:\n";
490 for (auto Offset : Pair.second) {
491 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000492 ReferencingDie.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000493 OS << "\n";
494 }
495 OS << "\n";
496 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000497 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000498}
499
Greg Claytonc5b2d562017-05-03 18:25:46 +0000500void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000501 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000502 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000503 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000504 // Get the attribute value as a section offset. No need to produce an
505 // error here if the encoding isn't correct because we validate this in
506 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000507 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000508 if (!StmtSectionOffset)
509 continue;
510 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000511 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000512 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000513 if (!LineTable) {
514 ++NumDebugLineErrors;
515 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
516 << "] was not able to be parsed for CU:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000517 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000518 OS << '\n';
519 continue;
520 }
521 } else {
522 // Make sure we don't get a valid line table back if the offset is wrong.
523 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000524 // Skip this line table as it isn't valid. No need to create an error
525 // here because we validate this in the .debug_info verifier.
526 continue;
527 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000528 auto Iter = StmtListToDie.find(LineTableOffset);
529 if (Iter != StmtListToDie.end()) {
530 ++NumDebugLineErrors;
531 OS << "error: two compile unit DIEs, "
532 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000533 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000534 << ", have the same DW_AT_stmt_list section offset:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000535 Iter->second.dump(OS, 0, DumpOpts);
536 Die.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000537 OS << '\n';
538 // Already verified this line table before, no need to do it again.
539 continue;
540 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000541 StmtListToDie[LineTableOffset] = Die;
542 }
543}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000544
Greg Claytonc5b2d562017-05-03 18:25:46 +0000545void DWARFVerifier::verifyDebugLineRows() {
546 for (const auto &CU : DCtx.compile_units()) {
547 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000548 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000549 // If there is no line table we will have created an error in the
550 // .debug_info verifier or in verifyDebugLineStmtOffsets().
551 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000552 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000553
554 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000555 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000556 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
557 uint32_t FileIndex = 1;
558 StringMap<uint16_t> FullPathMap;
559 for (const auto &FileName : LineTable->Prologue.FileNames) {
560 // Verify directory index.
561 if (FileName.DirIdx > MaxDirIndex) {
562 ++NumDebugLineErrors;
563 OS << "error: .debug_line["
564 << format("0x%08" PRIx64,
565 *toSectionOffset(Die.find(DW_AT_stmt_list)))
566 << "].prologue.file_names[" << FileIndex
567 << "].dir_idx contains an invalid index: " << FileName.DirIdx
568 << "\n";
569 }
570
571 // Check file paths for duplicates.
572 std::string FullPath;
573 const bool HasFullPath = LineTable->getFileNameByIndex(
574 FileIndex, CU->getCompilationDir(),
575 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
576 assert(HasFullPath && "Invalid index?");
577 (void)HasFullPath;
578 auto It = FullPathMap.find(FullPath);
579 if (It == FullPathMap.end())
580 FullPathMap[FullPath] = FileIndex;
581 else if (It->second != FileIndex) {
582 OS << "warning: .debug_line["
583 << format("0x%08" PRIx64,
584 *toSectionOffset(Die.find(DW_AT_stmt_list)))
585 << "].prologue.file_names[" << FileIndex
586 << "] is a duplicate of file_names[" << It->second << "]\n";
587 }
588
589 FileIndex++;
590 }
591
592 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000593 uint64_t PrevAddress = 0;
594 uint32_t RowIndex = 0;
595 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000596 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000597 if (Row.Address < PrevAddress) {
598 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000599 OS << "error: .debug_line["
Simon Dardis02d99452017-08-07 15:37:57 +0000600 << format("0x%08" PRIx64,
Greg Claytonc5b2d562017-05-03 18:25:46 +0000601 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000602 << "] row[" << RowIndex
603 << "] decreases in address from previous row:\n";
604
605 DWARFDebugLine::Row::dumpTableHeader(OS);
606 if (RowIndex > 0)
607 LineTable->Rows[RowIndex - 1].dump(OS);
608 Row.dump(OS);
609 OS << '\n';
610 }
611
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000612 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000613 if (Row.File > MaxFileIndex) {
614 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000615 OS << "error: .debug_line["
Simon Dardis02d99452017-08-07 15:37:57 +0000616 << format("0x%08" PRIx64,
Greg Claytonc5b2d562017-05-03 18:25:46 +0000617 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000618 << "][" << RowIndex << "] has invalid file index " << Row.File
619 << " (valid values are [1," << MaxFileIndex << "]):\n";
620 DWARFDebugLine::Row::dumpTableHeader(OS);
621 Row.dump(OS);
622 OS << '\n';
623 }
624 if (Row.EndSequence)
625 PrevAddress = 0;
626 else
627 PrevAddress = Row.Address;
628 ++RowIndex;
629 }
630 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000631}
632
633bool DWARFVerifier::handleDebugLine() {
634 NumDebugLineErrors = 0;
635 OS << "Verifying .debug_line...\n";
636 verifyDebugLineStmtOffsets();
637 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000638 return NumDebugLineErrors == 0;
639}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000640
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000641unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection,
642 DataExtractor *StrData,
643 const char *SectionName) {
644 unsigned NumErrors = 0;
645 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
646 DCtx.isLittleEndian(), 0);
647 DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000648
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000649 OS << "Verifying " << SectionName << "...\n";
650 // Verify that the fixed part of the header is not too short.
651
652 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
653 OS << "\terror: Section is too small to fit a section header.\n";
654 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000655 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000656 // Verify that the section is not too short.
657 if (!AccelTable.extract()) {
658 OS << "\terror: Section is smaller than size described in section header.\n";
659 return 1;
660 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000661 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000662 uint32_t NumBuckets = AccelTable.getNumBuckets();
663 uint32_t NumHashes = AccelTable.getNumHashes();
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000664
665 uint32_t BucketsOffset =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000666 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000667 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
668 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000669 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000670 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000671 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000672 OS << format("\terror: Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000673 HashIdx);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000674 ++NumErrors;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000675 }
676 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000677 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000678 if (NumAtoms == 0) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000679 OS << "\terror: no atoms; failed to read HashData.\n";
680 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000681 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000682 if (!AccelTable.validateForms()) {
683 OS << "\terror: unsupported form; failed to read HashData.\n";
684 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000685 }
686
687 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
688 uint32_t HashOffset = HashesBase + 4 * HashIdx;
689 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000690 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
691 uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
692 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
693 sizeof(uint64_t))) {
694 OS << format("\terror: Hash[%d] has invalid HashData offset: 0x%08x.\n",
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000695 HashIdx, HashDataOffset);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000696 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000697 }
698
699 uint32_t StrpOffset;
700 uint32_t StringOffset;
701 uint32_t StringCount = 0;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000702 unsigned Offset;
703 unsigned Tag;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000704 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000705 const uint32_t NumHashDataObjects =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000706 AccelSectionData.getU32(&HashDataOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000707 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
708 ++HashDataIdx) {
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000709 std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
710 auto Die = DCtx.getDIEForOffset(Offset);
711 if (!Die) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000712 const uint32_t BucketIdx =
713 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
714 StringOffset = StrpOffset;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000715 const char *Name = StrData->getCStr(&StringOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000716 if (!Name)
717 Name = "<NULL>";
718
719 OS << format(
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000720 "\terror: %s Bucket[%d] Hash[%d] = 0x%08x "
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000721 "Str[%u] = 0x%08x "
722 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000723 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000724 HashDataIdx, Offset, Name);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000725
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000726 ++NumErrors;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000727 continue;
728 }
729 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
730 OS << "\terror: Tag " << dwarf::TagString(Tag)
731 << " in accelerator table does not match Tag "
732 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
733 << "].\n";
734 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000735 }
736 }
737 ++StringCount;
738 }
739 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000740 return NumErrors;
741}
742
743bool DWARFVerifier::handleAccelTables() {
744 const DWARFObject &D = DCtx.getDWARFObj();
745 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
746 unsigned NumErrors = 0;
747 if (!D.getAppleNamesSection().Data.empty())
748 NumErrors +=
749 verifyAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
750 if (!D.getAppleTypesSection().Data.empty())
751 NumErrors +=
752 verifyAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
753 if (!D.getAppleNamespacesSection().Data.empty())
754 NumErrors += verifyAccelTable(&D.getAppleNamespacesSection(), &StrData,
755 ".apple_namespaces");
756 if (!D.getAppleObjCSection().Data.empty())
757 NumErrors +=
758 verifyAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
759 return NumErrors == 0;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000760}