blob: 7f5ef1567ca7c1b1b494c8e52c0e673cd72107ed [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 Devlieghere19fc4d92017-09-29 09:33:31 +0000318 error() << format("Invalid address range [0x%08" PRIx64 " - 0x%08" PRIx64
319 "].\n",
320 Range.LowPC, Range.HighPC);
Jonas Devlieghere58910602017-09-14 11:33:42 +0000321 continue;
322 }
323
324 // Verify that ranges don't intersect.
325 const auto IntersectingRange = RI.insert(Range);
326 if (IntersectingRange != RI.Ranges.end()) {
327 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000328 error() << format("DIE has overlapping address ranges: [0x%08" PRIx64
329 " - 0x%08" PRIx64 "] and [0x%08" PRIx64
330 " - 0x%08" PRIx64 "].\n",
331 Range.LowPC, Range.HighPC, IntersectingRange->LowPC,
332 IntersectingRange->HighPC);
Jonas Devlieghere58910602017-09-14 11:33:42 +0000333 break;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000334 }
335 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000336
337 // Verify that children don't intersect.
338 const auto IntersectingChild = ParentRI.insert(RI);
339 if (IntersectingChild != ParentRI.Children.end()) {
340 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000341 error() << "DIEs have overlapping address ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000342 Die.dump(OS, 0);
343 IntersectingChild->Die.dump(OS, 0);
344 OS << "\n";
345 }
346
347 // Verify that ranges are contained within their parent.
348 bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
349 !(Die.getTag() == DW_TAG_subprogram &&
350 ParentRI.Die.getTag() == DW_TAG_subprogram);
351 if (ShouldBeContained && !ParentRI.contains(RI)) {
352 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000353 error() << "DIE address ranges are not "
354 "contained in its parent's ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000355 Die.dump(OS, 0);
356 ParentRI.Die.dump(OS, 0);
357 OS << "\n";
358 }
359
360 // Recursively check children.
361 for (DWARFDie Child : Die)
362 NumErrors += verifyDieRanges(Child, RI);
363
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000364 return NumErrors;
365}
366
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000367unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
368 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000369 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000370 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000371 const auto Attr = AttrValue.Attr;
372 switch (Attr) {
373 case DW_AT_ranges:
374 // Make sure the offset in the DW_AT_ranges attribute is valid.
375 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000376 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000377 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000378 error() << "DW_AT_ranges offset is beyond .debug_ranges "
379 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000380 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000381 OS << "\n";
382 }
383 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000384 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000385 error() << "DIE has invalid DW_AT_ranges encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000386 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000387 OS << "\n";
388 }
389 break;
390 case DW_AT_stmt_list:
391 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
392 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000393 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000394 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000395 error() << "DW_AT_stmt_list offset is beyond .debug_line "
396 "bounds: "
397 << format("0x%08" PRIx64, *SectionOffset) << "\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000398 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000399 OS << "\n";
400 }
401 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000402 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000403 error() << "DIE has invalid DW_AT_stmt_list encoding:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000404 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000405 OS << "\n";
406 }
407 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000408
Greg Claytonc5b2d562017-05-03 18:25:46 +0000409 default:
410 break;
411 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000412 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000413}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000414
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000415unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
416 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000417 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000418 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000419 const auto Form = AttrValue.Value.getForm();
420 switch (Form) {
421 case DW_FORM_ref1:
422 case DW_FORM_ref2:
423 case DW_FORM_ref4:
424 case DW_FORM_ref8:
425 case DW_FORM_ref_udata: {
426 // Verify all CU relative references are valid CU offsets.
427 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
428 assert(RefVal);
429 if (RefVal) {
430 auto DieCU = Die.getDwarfUnit();
431 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
432 auto CUOffset = AttrValue.Value.getRawUValue();
433 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000434 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000435 error() << FormEncodingString(Form) << " CU offset "
436 << format("0x%08" PRIx64, CUOffset)
437 << " is invalid (must be less than CU size of "
438 << format("0x%08" PRIx32, CUSize) << "):\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000439 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000440 OS << "\n";
441 } else {
442 // Valid reference, but we will verify it points to an actual
443 // DIE later.
444 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000445 }
446 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000447 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000448 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000449 case DW_FORM_ref_addr: {
450 // Verify all absolute DIE references have valid offsets in the
451 // .debug_info section.
452 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
453 assert(RefVal);
454 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000455 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000456 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000457 error() << "DW_FORM_ref_addr offset beyond .debug_info "
458 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000459 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000460 OS << "\n";
461 } else {
462 // Valid reference, but we will verify it points to an actual
463 // DIE later.
464 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
465 }
466 }
467 break;
468 }
469 case DW_FORM_strp: {
470 auto SecOffset = AttrValue.Value.getAsSectionOffset();
471 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000472 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000473 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000474 error() << "DW_FORM_strp offset beyond .debug_str bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000475 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000476 OS << "\n";
477 }
478 break;
479 }
480 default:
481 break;
482 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000483 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000484}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000485
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000486unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000487 // Take all references and make sure they point to an actual DIE by
488 // getting the DIE by offset and emitting an error
489 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000490 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000491 for (auto Pair : ReferenceToDIEOffsets) {
492 auto Die = DCtx.getDIEForOffset(Pair.first);
493 if (Die)
494 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000495 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000496 error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
497 << ". Offset is in between DIEs:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000498 for (auto Offset : Pair.second) {
499 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000500 ReferencingDie.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000501 OS << "\n";
502 }
503 OS << "\n";
504 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000505 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000506}
507
Greg Claytonc5b2d562017-05-03 18:25:46 +0000508void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000509 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000510 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000511 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000512 // Get the attribute value as a section offset. No need to produce an
513 // error here if the encoding isn't correct because we validate this in
514 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000515 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000516 if (!StmtSectionOffset)
517 continue;
518 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000519 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000520 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000521 if (!LineTable) {
522 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000523 error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset)
524 << "] was not able to be parsed for CU:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000525 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000526 OS << '\n';
527 continue;
528 }
529 } else {
530 // Make sure we don't get a valid line table back if the offset is wrong.
531 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000532 // Skip this line table as it isn't valid. No need to create an error
533 // here because we validate this in the .debug_info verifier.
534 continue;
535 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000536 auto Iter = StmtListToDie.find(LineTableOffset);
537 if (Iter != StmtListToDie.end()) {
538 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000539 error() << "two compile unit DIEs, "
540 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
541 << format("0x%08" PRIx32, Die.getOffset())
542 << ", have the same DW_AT_stmt_list section offset:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000543 Iter->second.dump(OS, 0, DumpOpts);
544 Die.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000545 OS << '\n';
546 // Already verified this line table before, no need to do it again.
547 continue;
548 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000549 StmtListToDie[LineTableOffset] = Die;
550 }
551}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000552
Greg Claytonc5b2d562017-05-03 18:25:46 +0000553void DWARFVerifier::verifyDebugLineRows() {
554 for (const auto &CU : DCtx.compile_units()) {
555 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000556 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000557 // If there is no line table we will have created an error in the
558 // .debug_info verifier or in verifyDebugLineStmtOffsets().
559 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000560 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000561
562 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000563 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000564 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
565 uint32_t FileIndex = 1;
566 StringMap<uint16_t> FullPathMap;
567 for (const auto &FileName : LineTable->Prologue.FileNames) {
568 // Verify directory index.
569 if (FileName.DirIdx > MaxDirIndex) {
570 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000571 error() << ".debug_line["
572 << format("0x%08" PRIx64,
573 *toSectionOffset(Die.find(DW_AT_stmt_list)))
574 << "].prologue.file_names[" << FileIndex
575 << "].dir_idx contains an invalid index: " << FileName.DirIdx
576 << "\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000577 }
578
579 // Check file paths for duplicates.
580 std::string FullPath;
581 const bool HasFullPath = LineTable->getFileNameByIndex(
582 FileIndex, CU->getCompilationDir(),
583 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
584 assert(HasFullPath && "Invalid index?");
585 (void)HasFullPath;
586 auto It = FullPathMap.find(FullPath);
587 if (It == FullPathMap.end())
588 FullPathMap[FullPath] = FileIndex;
589 else if (It->second != FileIndex) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000590 warn() << ".debug_line["
591 << format("0x%08" PRIx64,
592 *toSectionOffset(Die.find(DW_AT_stmt_list)))
593 << "].prologue.file_names[" << FileIndex
594 << "] is a duplicate of file_names[" << It->second << "]\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000595 }
596
597 FileIndex++;
598 }
599
600 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000601 uint64_t PrevAddress = 0;
602 uint32_t RowIndex = 0;
603 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000604 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000605 if (Row.Address < PrevAddress) {
606 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000607 error() << ".debug_line["
608 << format("0x%08" PRIx64,
609 *toSectionOffset(Die.find(DW_AT_stmt_list)))
610 << "] row[" << RowIndex
611 << "] decreases in address from previous row:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000612
613 DWARFDebugLine::Row::dumpTableHeader(OS);
614 if (RowIndex > 0)
615 LineTable->Rows[RowIndex - 1].dump(OS);
616 Row.dump(OS);
617 OS << '\n';
618 }
619
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000620 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000621 if (Row.File > MaxFileIndex) {
622 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000623 error() << ".debug_line["
624 << format("0x%08" PRIx64,
625 *toSectionOffset(Die.find(DW_AT_stmt_list)))
626 << "][" << RowIndex << "] has invalid file index " << Row.File
627 << " (valid values are [1," << MaxFileIndex << "]):\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000628 DWARFDebugLine::Row::dumpTableHeader(OS);
629 Row.dump(OS);
630 OS << '\n';
631 }
632 if (Row.EndSequence)
633 PrevAddress = 0;
634 else
635 PrevAddress = Row.Address;
636 ++RowIndex;
637 }
638 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000639}
640
641bool DWARFVerifier::handleDebugLine() {
642 NumDebugLineErrors = 0;
643 OS << "Verifying .debug_line...\n";
644 verifyDebugLineStmtOffsets();
645 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000646 return NumDebugLineErrors == 0;
647}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000648
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000649unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection,
650 DataExtractor *StrData,
651 const char *SectionName) {
652 unsigned NumErrors = 0;
653 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
654 DCtx.isLittleEndian(), 0);
655 DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000656
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000657 OS << "Verifying " << SectionName << "...\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000658
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000659 // Verify that the fixed part of the header is not too short.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000660 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000661 error() << "Section is too small to fit a section header.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000662 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000663 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000664
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000665 // Verify that the section is not too short.
666 if (!AccelTable.extract()) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000667 error() << "Section is smaller than size described in section header.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000668 return 1;
669 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000670
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000671 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000672 uint32_t NumBuckets = AccelTable.getNumBuckets();
673 uint32_t NumHashes = AccelTable.getNumHashes();
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000674
675 uint32_t BucketsOffset =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000676 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000677 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
678 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000679 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000680 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000681 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000682 error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
683 HashIdx);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000684 ++NumErrors;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000685 }
686 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000687 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000688 if (NumAtoms == 0) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000689 error() << "No atoms: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000690 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000691 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000692 if (!AccelTable.validateForms()) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000693 error() << "Unsupported form: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000694 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000695 }
696
697 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
698 uint32_t HashOffset = HashesBase + 4 * HashIdx;
699 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000700 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
701 uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
702 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
703 sizeof(uint64_t))) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000704 error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n",
705 HashIdx, HashDataOffset);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000706 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000707 }
708
709 uint32_t StrpOffset;
710 uint32_t StringOffset;
711 uint32_t StringCount = 0;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000712 unsigned Offset;
713 unsigned Tag;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000714 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000715 const uint32_t NumHashDataObjects =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000716 AccelSectionData.getU32(&HashDataOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000717 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
718 ++HashDataIdx) {
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000719 std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
720 auto Die = DCtx.getDIEForOffset(Offset);
721 if (!Die) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000722 const uint32_t BucketIdx =
723 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
724 StringOffset = StrpOffset;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000725 const char *Name = StrData->getCStr(&StringOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000726 if (!Name)
727 Name = "<NULL>";
728
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000729 error() << format(
730 "%s Bucket[%d] Hash[%d] = 0x%08x "
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000731 "Str[%u] = 0x%08x "
732 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000733 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000734 HashDataIdx, Offset, Name);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000735
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000736 ++NumErrors;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000737 continue;
738 }
739 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000740 error() << "Tag " << dwarf::TagString(Tag)
741 << " in accelerator table does not match Tag "
742 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
743 << "].\n";
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000744 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000745 }
746 }
747 ++StringCount;
748 }
749 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000750 return NumErrors;
751}
752
753bool DWARFVerifier::handleAccelTables() {
754 const DWARFObject &D = DCtx.getDWARFObj();
755 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
756 unsigned NumErrors = 0;
757 if (!D.getAppleNamesSection().Data.empty())
758 NumErrors +=
759 verifyAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
760 if (!D.getAppleTypesSection().Data.empty())
761 NumErrors +=
762 verifyAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
763 if (!D.getAppleNamespacesSection().Data.empty())
764 NumErrors += verifyAccelTable(&D.getAppleNamespacesSection(), &StrData,
765 ".apple_namespaces");
766 if (!D.getAppleObjCSection().Data.empty())
767 NumErrors +=
768 verifyAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
769 return NumErrors == 0;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000770}
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000771
772raw_ostream &DWARFVerifier::error() const {
773 return WithColor(OS, syntax::Error).get() << "error: ";
774}
775
776raw_ostream &DWARFVerifier::warn() const {
777 return WithColor(OS, syntax::Warning).get() << "warning: ";
778}
779
780raw_ostream &DWARFVerifier::note() const {
781 return WithColor(OS, syntax::Note).get() << "note: ";
782}