blob: 27e6a05e6dd05f8805cec69715e418865d6bb844 [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
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +000010#include "SyntaxHighlighting.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000011#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
12#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"
16#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17#include "llvm/DebugInfo/DWARF/DWARFSection.h"
Spyridoula Gravanie41823b2017-06-14 00:17:55 +000018#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000019#include "llvm/Support/raw_ostream.h"
20#include <map>
21#include <set>
22#include <vector>
23
24using namespace llvm;
25using namespace dwarf;
26using namespace object;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +000027using namespace syntax;
Greg Claytonb8c162b2017-05-03 16:02:29 +000028
Jonas Devlieghere58910602017-09-14 11:33:42 +000029DWARFVerifier::DieRangeInfo::address_range_iterator
30DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
31 auto Begin = Ranges.begin();
32 auto End = Ranges.end();
33 auto Pos = std::lower_bound(Begin, End, R);
34
35 if (Pos != End) {
36 if (Pos->intersects(R))
37 return Pos;
38 if (Pos != Begin) {
39 auto Iter = Pos - 1;
40 if (Iter->intersects(R))
41 return Iter;
42 }
43 }
44
45 Ranges.insert(Pos, R);
46 return Ranges.end();
47}
48
49DWARFVerifier::DieRangeInfo::die_range_info_iterator
50DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) {
51 auto End = Children.end();
52 auto Iter = Children.begin();
53 while (Iter != End) {
54 if (Iter->intersects(RI))
55 return Iter;
56 ++Iter;
57 }
58 Children.insert(RI);
59 return Children.end();
60}
61
62bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
63 // Both list of ranges are sorted so we can make this fast.
64
65 if (Ranges.empty() || RHS.Ranges.empty())
66 return false;
67
68 // Since the ranges are sorted we can advance where we start searching with
69 // this object's ranges as we traverse RHS.Ranges.
70 auto End = Ranges.end();
71 auto Iter = findRange(RHS.Ranges.front());
72
73 // Now linearly walk the ranges in this object and see if they contain each
74 // ranges from RHS.Ranges.
75 for (const auto &R : RHS.Ranges) {
76 while (Iter != End) {
77 if (Iter->contains(R))
78 break;
79 ++Iter;
80 }
81 if (Iter == End)
82 return false;
83 }
84 return true;
85}
86
87bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
88 if (Ranges.empty() || RHS.Ranges.empty())
89 return false;
90
91 auto End = Ranges.end();
92 auto Iter = findRange(RHS.Ranges.front());
93 for (const auto &R : RHS.Ranges) {
Jonas Devlieghered585a202017-09-14 17:46:23 +000094 if(Iter == End)
95 return false;
Jonas Devlieghere58910602017-09-14 11:33:42 +000096 if (R.HighPC <= Iter->LowPC)
97 continue;
98 while (Iter != End) {
99 if (Iter->intersects(R))
100 return true;
101 ++Iter;
102 }
103 }
104
105 return false;
106}
107
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000108bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000109 uint32_t *Offset, unsigned UnitIndex,
110 uint8_t &UnitType, bool &isUnitDWARF64) {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000111 uint32_t AbbrOffset, Length;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000112 uint8_t AddrSize = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000113 uint16_t Version;
114 bool Success = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000115
116 bool ValidLength = false;
117 bool ValidVersion = false;
118 bool ValidAddrSize = false;
119 bool ValidType = true;
120 bool ValidAbbrevOffset = true;
121
122 uint32_t OffsetStart = *Offset;
123 Length = DebugInfoData.getU32(Offset);
124 if (Length == UINT32_MAX) {
125 isUnitDWARF64 = true;
126 OS << format(
127 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
128 UnitIndex);
129 return false;
130 }
131 Version = DebugInfoData.getU16(Offset);
132
133 if (Version >= 5) {
134 UnitType = DebugInfoData.getU8(Offset);
135 AddrSize = DebugInfoData.getU8(Offset);
136 AbbrOffset = DebugInfoData.getU32(Offset);
137 ValidType = DWARFUnit::isValidUnitType(UnitType);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000138 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000139 UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000140 AbbrOffset = DebugInfoData.getU32(Offset);
141 AddrSize = DebugInfoData.getU8(Offset);
142 }
143
144 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
145 ValidAbbrevOffset = false;
146
147 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
148 ValidVersion = DWARFContext::isSupportedVersion(Version);
149 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
150 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
151 !ValidType) {
152 Success = false;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000153 error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex,
154 OffsetStart);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000155 if (!ValidLength)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000156 note() << "The length for this unit is too "
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000157 "large for the .debug_info provided.\n";
158 if (!ValidVersion)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000159 note() << "The 16 bit unit header version is not valid.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000160 if (!ValidType)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000161 note() << "The unit type encoding is not valid.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000162 if (!ValidAbbrevOffset)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000163 note() << "The offset into the .debug_abbrev section is "
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000164 "not valid.\n";
165 if (!ValidAddrSize)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000166 note() << "The address size is unsupported.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000167 }
168 *Offset = OffsetStart + Length + 4;
169 return Success;
170}
171
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000172bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) {
173 uint32_t NumUnitErrors = 0;
174 unsigned NumDies = Unit.getNumDIEs();
175 for (unsigned I = 0; I < NumDies; ++I) {
176 auto Die = Unit.getDIEAtIndex(I);
177 if (Die.getTag() == DW_TAG_null)
178 continue;
179 for (auto AttrValue : Die.attributes()) {
180 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
181 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
182 }
183 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000184
Jonas Devlieghere35fdaa92017-09-28 15:57:50 +0000185 if (DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false)) {
186 DieRangeInfo RI;
187 NumUnitErrors += verifyDieRanges(Die, RI);
188 } else {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000189 error() << "Compilation unit without unit DIE.\n";
Jonas Devlieghere35fdaa92017-09-28 15:57:50 +0000190 NumUnitErrors++;
191 }
192
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000193 return NumUnitErrors == 0;
194}
195
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000196unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000197 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000198 if (Abbrev) {
199 const DWARFAbbreviationDeclarationSet *AbbrDecls =
200 Abbrev->getAbbreviationDeclarationSet(0);
201 for (auto AbbrDecl : *AbbrDecls) {
202 SmallDenseSet<uint16_t> AttributeSet;
203 for (auto Attribute : AbbrDecl.attributes()) {
204 auto Result = AttributeSet.insert(Attribute.Attr);
205 if (!Result.second) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000206 error() << "Abbreviation declaration contains multiple "
207 << AttributeString(Attribute.Attr) << " attributes.\n";
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000208 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000209 ++NumErrors;
210 }
211 }
212 }
213 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000214 return NumErrors;
215}
216
217bool DWARFVerifier::handleDebugAbbrev() {
218 OS << "Verifying .debug_abbrev...\n";
219
220 const DWARFObject &DObj = DCtx.getDWARFObj();
221 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
222 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
223
224 if (noDebugAbbrev && noDebugAbbrevDWO) {
225 return true;
226 }
227
228 unsigned NumErrors = 0;
229 if (!noDebugAbbrev)
230 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
231
232 if (!noDebugAbbrevDWO)
233 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000234 return NumErrors == 0;
235}
236
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000237bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000238 OS << "Verifying .debug_info Unit Header Chain...\n";
239
Rafael Espindolac398e672017-07-19 22:27:28 +0000240 const DWARFObject &DObj = DCtx.getDWARFObj();
241 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
242 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000243 uint32_t NumDebugInfoErrors = 0;
244 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
245 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000246 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000247 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000248 bool hasDIE = DebugInfoData.isValidOffset(Offset);
249 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000250 OffsetStart = Offset;
251 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
252 isUnitDWARF64)) {
253 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000254 if (isUnitDWARF64)
255 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000256 } else {
257 std::unique_ptr<DWARFUnit> Unit;
258 switch (UnitType) {
259 case dwarf::DW_UT_type:
260 case dwarf::DW_UT_split_type: {
261 DWARFUnitSection<DWARFTypeUnit> TUSection{};
262 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000263 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
264 &DObj.getRangeSection(), DObj.getStringSection(),
265 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
266 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000267 nullptr));
268 break;
269 }
270 case dwarf::DW_UT_skeleton:
271 case dwarf::DW_UT_split_compile:
272 case dwarf::DW_UT_compile:
273 case dwarf::DW_UT_partial:
274 // UnitType = 0 means that we are
275 // verifying a compile unit in DWARF v4.
276 case 0: {
277 DWARFUnitSection<DWARFCompileUnit> CUSection{};
278 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000279 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
280 &DObj.getRangeSection(), DObj.getStringSection(),
281 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
282 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000283 nullptr));
284 break;
285 }
286 default: { llvm_unreachable("Invalid UnitType."); }
287 }
288 Unit->extract(DebugInfoData, &OffsetStart);
289 if (!verifyUnitContents(*Unit))
290 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000291 }
292 hasDIE = DebugInfoData.isValidOffset(Offset);
293 ++UnitIdx;
294 }
295 if (UnitIdx == 0 && !hasDIE) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000296 warn() << ".debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000297 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000298 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000299 NumDebugInfoErrors += verifyDebugInfoReferences();
300 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000301}
302
Jonas Devlieghere58910602017-09-14 11:33:42 +0000303unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
304 DieRangeInfo &ParentRI) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000305 unsigned NumErrors = 0;
Jonas Devlieghere58910602017-09-14 11:33:42 +0000306
307 if (!Die.isValid())
308 return NumErrors;
309
310 DWARFAddressRangesVector Ranges = Die.getAddressRanges();
311
312 // Build RI for this DIE and check that ranges within this DIE do not
313 // overlap.
314 DieRangeInfo RI(Die);
315 for (auto Range : Ranges) {
316 if (!Range.valid()) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000317 ++NumErrors;
Jonas Devliegherea15f25d32017-09-29 15:41:22 +0000318 error() << "Invalid address range " << Range << "\n";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000319 continue;
320 }
321
322 // Verify that ranges don't intersect.
323 const auto IntersectingRange = RI.insert(Range);
324 if (IntersectingRange != RI.Ranges.end()) {
325 ++NumErrors;
Jonas Devliegherea15f25d32017-09-29 15:41:22 +0000326 error() << "DIE has overlapping address ranges: " << Range << " and "
327 << *IntersectingRange << "\n";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000328 break;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000329 }
330 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000331
332 // Verify that children don't intersect.
333 const auto IntersectingChild = ParentRI.insert(RI);
334 if (IntersectingChild != ParentRI.Children.end()) {
335 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000336 error() << "DIEs have overlapping address ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000337 Die.dump(OS, 0);
338 IntersectingChild->Die.dump(OS, 0);
339 OS << "\n";
340 }
341
342 // Verify that ranges are contained within their parent.
343 bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
344 !(Die.getTag() == DW_TAG_subprogram &&
345 ParentRI.Die.getTag() == DW_TAG_subprogram);
346 if (ShouldBeContained && !ParentRI.contains(RI)) {
347 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000348 error() << "DIE address ranges are not "
349 "contained in its parent's ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000350 Die.dump(OS, 0);
351 ParentRI.Die.dump(OS, 0);
352 OS << "\n";
353 }
354
355 // Recursively check children.
356 for (DWARFDie Child : Die)
357 NumErrors += verifyDieRanges(Child, RI);
358
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000359 return NumErrors;
360}
361
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000362unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
363 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000364 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000365 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000366 const auto Attr = AttrValue.Attr;
367 switch (Attr) {
368 case DW_AT_ranges:
369 // Make sure the offset in the DW_AT_ranges attribute is valid.
370 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000371 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000372 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000373 error() << "DW_AT_ranges offset is beyond .debug_ranges "
374 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000375 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000376 OS << "\n";
377 }
378 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000379 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000380 error() << "DIE has invalid DW_AT_ranges encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000381 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000382 OS << "\n";
383 }
384 break;
385 case DW_AT_stmt_list:
386 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
387 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000388 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000389 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000390 error() << "DW_AT_stmt_list offset is beyond .debug_line "
391 "bounds: "
392 << format("0x%08" PRIx64, *SectionOffset) << "\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000393 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000394 OS << "\n";
395 }
396 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000397 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000398 error() << "DIE has invalid DW_AT_stmt_list encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000399 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000400 OS << "\n";
401 }
402 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000403
Greg Claytonc5b2d562017-05-03 18:25:46 +0000404 default:
405 break;
406 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000407 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000408}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000409
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000410unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
411 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000412 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000413 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000414 const auto Form = AttrValue.Value.getForm();
415 switch (Form) {
416 case DW_FORM_ref1:
417 case DW_FORM_ref2:
418 case DW_FORM_ref4:
419 case DW_FORM_ref8:
420 case DW_FORM_ref_udata: {
421 // Verify all CU relative references are valid CU offsets.
422 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
423 assert(RefVal);
424 if (RefVal) {
425 auto DieCU = Die.getDwarfUnit();
426 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
427 auto CUOffset = AttrValue.Value.getRawUValue();
428 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000429 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000430 error() << FormEncodingString(Form) << " CU offset "
431 << format("0x%08" PRIx64, CUOffset)
432 << " is invalid (must be less than CU size of "
433 << format("0x%08" PRIx32, CUSize) << "):\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000434 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000435 OS << "\n";
436 } else {
437 // Valid reference, but we will verify it points to an actual
438 // DIE later.
439 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000440 }
441 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000442 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000443 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000444 case DW_FORM_ref_addr: {
445 // Verify all absolute DIE references have valid offsets in the
446 // .debug_info section.
447 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
448 assert(RefVal);
449 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000450 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000451 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000452 error() << "DW_FORM_ref_addr offset beyond .debug_info "
453 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000454 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000455 OS << "\n";
456 } else {
457 // Valid reference, but we will verify it points to an actual
458 // DIE later.
459 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
460 }
461 }
462 break;
463 }
464 case DW_FORM_strp: {
465 auto SecOffset = AttrValue.Value.getAsSectionOffset();
466 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000467 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000468 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000469 error() << "DW_FORM_strp offset beyond .debug_str bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000470 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000471 OS << "\n";
472 }
473 break;
474 }
475 default:
476 break;
477 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000478 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000479}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000480
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000481unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000482 // Take all references and make sure they point to an actual DIE by
483 // getting the DIE by offset and emitting an error
484 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000485 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000486 for (auto Pair : ReferenceToDIEOffsets) {
487 auto Die = DCtx.getDIEForOffset(Pair.first);
488 if (Die)
489 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000490 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000491 error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
492 << ". Offset is in between DIEs:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000493 for (auto Offset : Pair.second) {
494 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000495 ReferencingDie.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000496 OS << "\n";
497 }
498 OS << "\n";
499 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000500 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000501}
502
Greg Claytonc5b2d562017-05-03 18:25:46 +0000503void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000504 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000505 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000506 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000507 // Get the attribute value as a section offset. No need to produce an
508 // error here if the encoding isn't correct because we validate this in
509 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000510 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000511 if (!StmtSectionOffset)
512 continue;
513 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000514 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000515 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000516 if (!LineTable) {
517 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000518 error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset)
519 << "] was not able to be parsed for CU:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000520 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000521 OS << '\n';
522 continue;
523 }
524 } else {
525 // Make sure we don't get a valid line table back if the offset is wrong.
526 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000527 // Skip this line table as it isn't valid. No need to create an error
528 // here because we validate this in the .debug_info verifier.
529 continue;
530 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000531 auto Iter = StmtListToDie.find(LineTableOffset);
532 if (Iter != StmtListToDie.end()) {
533 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000534 error() << "two compile unit DIEs, "
535 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
536 << format("0x%08" PRIx32, Die.getOffset())
537 << ", have the same DW_AT_stmt_list section offset:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000538 Iter->second.dump(OS, 0, DumpOpts);
539 Die.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000540 OS << '\n';
541 // Already verified this line table before, no need to do it again.
542 continue;
543 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000544 StmtListToDie[LineTableOffset] = Die;
545 }
546}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000547
Greg Claytonc5b2d562017-05-03 18:25:46 +0000548void DWARFVerifier::verifyDebugLineRows() {
549 for (const auto &CU : DCtx.compile_units()) {
550 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000551 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000552 // If there is no line table we will have created an error in the
553 // .debug_info verifier or in verifyDebugLineStmtOffsets().
554 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000555 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000556
557 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000558 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000559 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
560 uint32_t FileIndex = 1;
561 StringMap<uint16_t> FullPathMap;
562 for (const auto &FileName : LineTable->Prologue.FileNames) {
563 // Verify directory index.
564 if (FileName.DirIdx > MaxDirIndex) {
565 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000566 error() << ".debug_line["
567 << format("0x%08" PRIx64,
568 *toSectionOffset(Die.find(DW_AT_stmt_list)))
569 << "].prologue.file_names[" << FileIndex
570 << "].dir_idx contains an invalid index: " << FileName.DirIdx
571 << "\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000572 }
573
574 // Check file paths for duplicates.
575 std::string FullPath;
576 const bool HasFullPath = LineTable->getFileNameByIndex(
577 FileIndex, CU->getCompilationDir(),
578 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
579 assert(HasFullPath && "Invalid index?");
580 (void)HasFullPath;
581 auto It = FullPathMap.find(FullPath);
582 if (It == FullPathMap.end())
583 FullPathMap[FullPath] = FileIndex;
584 else if (It->second != FileIndex) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000585 warn() << ".debug_line["
586 << format("0x%08" PRIx64,
587 *toSectionOffset(Die.find(DW_AT_stmt_list)))
588 << "].prologue.file_names[" << FileIndex
589 << "] is a duplicate of file_names[" << It->second << "]\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000590 }
591
592 FileIndex++;
593 }
594
595 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000596 uint64_t PrevAddress = 0;
597 uint32_t RowIndex = 0;
598 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000599 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000600 if (Row.Address < PrevAddress) {
601 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000602 error() << ".debug_line["
603 << format("0x%08" PRIx64,
604 *toSectionOffset(Die.find(DW_AT_stmt_list)))
605 << "] row[" << RowIndex
606 << "] decreases in address from previous row:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000607
608 DWARFDebugLine::Row::dumpTableHeader(OS);
609 if (RowIndex > 0)
610 LineTable->Rows[RowIndex - 1].dump(OS);
611 Row.dump(OS);
612 OS << '\n';
613 }
614
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000615 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000616 if (Row.File > MaxFileIndex) {
617 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000618 error() << ".debug_line["
619 << format("0x%08" PRIx64,
620 *toSectionOffset(Die.find(DW_AT_stmt_list)))
621 << "][" << RowIndex << "] has invalid file index " << Row.File
622 << " (valid values are [1," << MaxFileIndex << "]):\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000623 DWARFDebugLine::Row::dumpTableHeader(OS);
624 Row.dump(OS);
625 OS << '\n';
626 }
627 if (Row.EndSequence)
628 PrevAddress = 0;
629 else
630 PrevAddress = Row.Address;
631 ++RowIndex;
632 }
633 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000634}
635
636bool DWARFVerifier::handleDebugLine() {
637 NumDebugLineErrors = 0;
638 OS << "Verifying .debug_line...\n";
639 verifyDebugLineStmtOffsets();
640 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000641 return NumDebugLineErrors == 0;
642}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000643
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000644unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection,
645 DataExtractor *StrData,
646 const char *SectionName) {
647 unsigned NumErrors = 0;
648 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
649 DCtx.isLittleEndian(), 0);
650 DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000651
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000652 OS << "Verifying " << SectionName << "...\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000653
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000654 // Verify that the fixed part of the header is not too short.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000655 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000656 error() << "Section is too small to fit a section header.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000657 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000658 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000659
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000660 // Verify that the section is not too short.
661 if (!AccelTable.extract()) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000662 error() << "Section is smaller than size described in section header.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000663 return 1;
664 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000665
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) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000677 error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
678 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) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000684 error() << "No atoms: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000685 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000686 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000687 if (!AccelTable.validateForms()) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000688 error() << "Unsupported form: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000689 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))) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000699 error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n",
700 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
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000724 error() << format(
725 "%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)) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000735 error() << "Tag " << dwarf::TagString(Tag)
736 << " in accelerator table does not match Tag "
737 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
738 << "].\n";
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000739 ++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}
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000766
767raw_ostream &DWARFVerifier::error() const {
768 return WithColor(OS, syntax::Error).get() << "error: ";
769}
770
771raw_ostream &DWARFVerifier::warn() const {
772 return WithColor(OS, syntax::Warning).get() << "warning: ";
773}
774
775raw_ostream &DWARFVerifier::note() const {
776 return WithColor(OS, syntax::Note).get() << "note: ";
777}