blob: 7b7b29bb1d7da7ed78e26ad8d72a6135a34874df [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
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000027bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000028 uint32_t *Offset, unsigned UnitIndex,
29 uint8_t &UnitType, bool &isUnitDWARF64) {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000030 uint32_t AbbrOffset, Length;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000031 uint8_t AddrSize = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000032 uint16_t Version;
33 bool Success = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000034
35 bool ValidLength = false;
36 bool ValidVersion = false;
37 bool ValidAddrSize = false;
38 bool ValidType = true;
39 bool ValidAbbrevOffset = true;
40
41 uint32_t OffsetStart = *Offset;
42 Length = DebugInfoData.getU32(Offset);
43 if (Length == UINT32_MAX) {
44 isUnitDWARF64 = true;
45 OS << format(
46 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
47 UnitIndex);
48 return false;
49 }
50 Version = DebugInfoData.getU16(Offset);
51
52 if (Version >= 5) {
53 UnitType = DebugInfoData.getU8(Offset);
54 AddrSize = DebugInfoData.getU8(Offset);
55 AbbrOffset = DebugInfoData.getU32(Offset);
56 ValidType = DWARFUnit::isValidUnitType(UnitType);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000057 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000058 UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000059 AbbrOffset = DebugInfoData.getU32(Offset);
60 AddrSize = DebugInfoData.getU8(Offset);
61 }
62
63 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
64 ValidAbbrevOffset = false;
65
66 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
67 ValidVersion = DWARFContext::isSupportedVersion(Version);
68 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
69 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
70 !ValidType) {
71 Success = false;
72 OS << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart);
73 if (!ValidLength)
74 OS << "\tError: The length for this unit is too "
75 "large for the .debug_info provided.\n";
76 if (!ValidVersion)
77 OS << "\tError: The 16 bit unit header version is not valid.\n";
78 if (!ValidType)
79 OS << "\tError: The unit type encoding is not valid.\n";
80 if (!ValidAbbrevOffset)
81 OS << "\tError: The offset into the .debug_abbrev section is "
82 "not valid.\n";
83 if (!ValidAddrSize)
84 OS << "\tError: The address size is unsupported.\n";
85 }
86 *Offset = OffsetStart + Length + 4;
87 return Success;
88}
89
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000090bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) {
91 uint32_t NumUnitErrors = 0;
92 unsigned NumDies = Unit.getNumDIEs();
93 for (unsigned I = 0; I < NumDies; ++I) {
94 auto Die = Unit.getDIEAtIndex(I);
95 if (Die.getTag() == DW_TAG_null)
96 continue;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +000097 NumUnitErrors += verifyDieRanges(Die);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000098 for (auto AttrValue : Die.attributes()) {
99 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
100 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
101 }
102 }
103 return NumUnitErrors == 0;
104}
105
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000106unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000107 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000108 if (Abbrev) {
109 const DWARFAbbreviationDeclarationSet *AbbrDecls =
110 Abbrev->getAbbreviationDeclarationSet(0);
111 for (auto AbbrDecl : *AbbrDecls) {
112 SmallDenseSet<uint16_t> AttributeSet;
113 for (auto Attribute : AbbrDecl.attributes()) {
114 auto Result = AttributeSet.insert(Attribute.Attr);
115 if (!Result.second) {
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000116 OS << "Error: Abbreviation declaration contains multiple "
117 << AttributeString(Attribute.Attr) << " attributes.\n";
118 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000119 ++NumErrors;
120 }
121 }
122 }
123 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000124 return NumErrors;
125}
126
127bool DWARFVerifier::handleDebugAbbrev() {
128 OS << "Verifying .debug_abbrev...\n";
129
130 const DWARFObject &DObj = DCtx.getDWARFObj();
131 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
132 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
133
134 if (noDebugAbbrev && noDebugAbbrevDWO) {
135 return true;
136 }
137
138 unsigned NumErrors = 0;
139 if (!noDebugAbbrev)
140 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
141
142 if (!noDebugAbbrevDWO)
143 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000144 return NumErrors == 0;
145}
146
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000147bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000148 OS << "Verifying .debug_info Unit Header Chain...\n";
149
Rafael Espindolac398e672017-07-19 22:27:28 +0000150 const DWARFObject &DObj = DCtx.getDWARFObj();
151 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
152 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000153 uint32_t NumDebugInfoErrors = 0;
154 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
155 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000156 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000157 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000158 bool hasDIE = DebugInfoData.isValidOffset(Offset);
159 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000160 OffsetStart = Offset;
161 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
162 isUnitDWARF64)) {
163 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000164 if (isUnitDWARF64)
165 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000166 } else {
167 std::unique_ptr<DWARFUnit> Unit;
168 switch (UnitType) {
169 case dwarf::DW_UT_type:
170 case dwarf::DW_UT_split_type: {
171 DWARFUnitSection<DWARFTypeUnit> TUSection{};
172 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000173 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
174 &DObj.getRangeSection(), DObj.getStringSection(),
175 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
176 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000177 nullptr));
178 break;
179 }
180 case dwarf::DW_UT_skeleton:
181 case dwarf::DW_UT_split_compile:
182 case dwarf::DW_UT_compile:
183 case dwarf::DW_UT_partial:
184 // UnitType = 0 means that we are
185 // verifying a compile unit in DWARF v4.
186 case 0: {
187 DWARFUnitSection<DWARFCompileUnit> CUSection{};
188 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000189 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
190 &DObj.getRangeSection(), DObj.getStringSection(),
191 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
192 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000193 nullptr));
194 break;
195 }
196 default: { llvm_unreachable("Invalid UnitType."); }
197 }
198 Unit->extract(DebugInfoData, &OffsetStart);
199 if (!verifyUnitContents(*Unit))
200 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000201 }
202 hasDIE = DebugInfoData.isValidOffset(Offset);
203 ++UnitIdx;
204 }
205 if (UnitIdx == 0 && !hasDIE) {
206 OS << "Warning: .debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000207 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000208 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000209 NumDebugInfoErrors += verifyDebugInfoReferences();
210 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000211}
212
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000213unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die) {
214 unsigned NumErrors = 0;
215 for (auto Range : Die.getAddressRanges()) {
216 if (Range.LowPC >= Range.HighPC) {
217 ++NumErrors;
218 OS << format("error: Invalid address range [0x%08x - 0x%08x].\n",
219 Range.LowPC, Range.HighPC);
220 }
221 }
222 return NumErrors;
223}
224
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000225unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
226 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000227 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000228 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000229 const auto Attr = AttrValue.Attr;
230 switch (Attr) {
231 case DW_AT_ranges:
232 // Make sure the offset in the DW_AT_ranges attribute is valid.
233 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000234 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000235 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000236 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
237 "bounds:\n";
238 Die.dump(OS, 0);
239 OS << "\n";
240 }
241 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000242 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000243 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
244 Die.dump(OS, 0);
245 OS << "\n";
246 }
247 break;
248 case DW_AT_stmt_list:
249 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
250 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000251 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000252 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000253 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
254 "bounds: "
255 << format("0x%08" PRIx32, *SectionOffset) << "\n";
256 Die.dump(OS, 0);
257 OS << "\n";
258 }
259 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000260 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000261 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
262 Die.dump(OS, 0);
263 OS << "\n";
264 }
265 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000266
Greg Claytonc5b2d562017-05-03 18:25:46 +0000267 default:
268 break;
269 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000270 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000271}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000272
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000273unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
274 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000275 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000276 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000277 const auto Form = AttrValue.Value.getForm();
278 switch (Form) {
279 case DW_FORM_ref1:
280 case DW_FORM_ref2:
281 case DW_FORM_ref4:
282 case DW_FORM_ref8:
283 case DW_FORM_ref_udata: {
284 // Verify all CU relative references are valid CU offsets.
285 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
286 assert(RefVal);
287 if (RefVal) {
288 auto DieCU = Die.getDwarfUnit();
289 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
290 auto CUOffset = AttrValue.Value.getRawUValue();
291 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000292 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000293 OS << "error: " << FormEncodingString(Form) << " CU offset "
294 << format("0x%08" PRIx32, CUOffset)
295 << " is invalid (must be less than CU size of "
296 << format("0x%08" PRIx32, CUSize) << "):\n";
297 Die.dump(OS, 0);
298 OS << "\n";
299 } else {
300 // Valid reference, but we will verify it points to an actual
301 // DIE later.
302 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000303 }
304 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000305 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000306 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000307 case DW_FORM_ref_addr: {
308 // Verify all absolute DIE references have valid offsets in the
309 // .debug_info section.
310 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
311 assert(RefVal);
312 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000313 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000314 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000315 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
316 "bounds:\n";
317 Die.dump(OS, 0);
318 OS << "\n";
319 } else {
320 // Valid reference, but we will verify it points to an actual
321 // DIE later.
322 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
323 }
324 }
325 break;
326 }
327 case DW_FORM_strp: {
328 auto SecOffset = AttrValue.Value.getAsSectionOffset();
329 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000330 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000331 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000332 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
333 Die.dump(OS, 0);
334 OS << "\n";
335 }
336 break;
337 }
338 default:
339 break;
340 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000341 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000342}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000343
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000344unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000345 // Take all references and make sure they point to an actual DIE by
346 // getting the DIE by offset and emitting an error
347 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000348 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000349 for (auto Pair : ReferenceToDIEOffsets) {
350 auto Die = DCtx.getDIEForOffset(Pair.first);
351 if (Die)
352 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000353 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000354 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
355 << ". Offset is in between DIEs:\n";
356 for (auto Offset : Pair.second) {
357 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
358 ReferencingDie.dump(OS, 0);
359 OS << "\n";
360 }
361 OS << "\n";
362 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000363 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000364}
365
Greg Claytonc5b2d562017-05-03 18:25:46 +0000366void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000367 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000368 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000369 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000370 // Get the attribute value as a section offset. No need to produce an
371 // error here if the encoding isn't correct because we validate this in
372 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000373 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000374 if (!StmtSectionOffset)
375 continue;
376 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000377 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000378 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000379 if (!LineTable) {
380 ++NumDebugLineErrors;
381 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
382 << "] was not able to be parsed for CU:\n";
383 Die.dump(OS, 0);
384 OS << '\n';
385 continue;
386 }
387 } else {
388 // Make sure we don't get a valid line table back if the offset is wrong.
389 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000390 // Skip this line table as it isn't valid. No need to create an error
391 // here because we validate this in the .debug_info verifier.
392 continue;
393 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000394 auto Iter = StmtListToDie.find(LineTableOffset);
395 if (Iter != StmtListToDie.end()) {
396 ++NumDebugLineErrors;
397 OS << "error: two compile unit DIEs, "
398 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000399 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000400 << ", have the same DW_AT_stmt_list section offset:\n";
401 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000402 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000403 OS << '\n';
404 // Already verified this line table before, no need to do it again.
405 continue;
406 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000407 StmtListToDie[LineTableOffset] = Die;
408 }
409}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000410
Greg Claytonc5b2d562017-05-03 18:25:46 +0000411void DWARFVerifier::verifyDebugLineRows() {
412 for (const auto &CU : DCtx.compile_units()) {
413 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000414 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000415 // If there is no line table we will have created an error in the
416 // .debug_info verifier or in verifyDebugLineStmtOffsets().
417 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000418 continue;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000419 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
420 uint64_t PrevAddress = 0;
421 uint32_t RowIndex = 0;
422 for (const auto &Row : LineTable->Rows) {
423 if (Row.Address < PrevAddress) {
424 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000425 OS << "error: .debug_line["
426 << format("0x%08" PRIx32,
427 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000428 << "] row[" << RowIndex
429 << "] decreases in address from previous row:\n";
430
431 DWARFDebugLine::Row::dumpTableHeader(OS);
432 if (RowIndex > 0)
433 LineTable->Rows[RowIndex - 1].dump(OS);
434 Row.dump(OS);
435 OS << '\n';
436 }
437
438 if (Row.File > MaxFileIndex) {
439 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000440 OS << "error: .debug_line["
441 << format("0x%08" PRIx32,
442 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000443 << "][" << RowIndex << "] has invalid file index " << Row.File
444 << " (valid values are [1," << MaxFileIndex << "]):\n";
445 DWARFDebugLine::Row::dumpTableHeader(OS);
446 Row.dump(OS);
447 OS << '\n';
448 }
449 if (Row.EndSequence)
450 PrevAddress = 0;
451 else
452 PrevAddress = Row.Address;
453 ++RowIndex;
454 }
455 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000456}
457
458bool DWARFVerifier::handleDebugLine() {
459 NumDebugLineErrors = 0;
460 OS << "Verifying .debug_line...\n";
461 verifyDebugLineStmtOffsets();
462 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000463 return NumDebugLineErrors == 0;
464}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000465
466bool DWARFVerifier::handleAppleNames() {
467 NumAppleNamesErrors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000468 const DWARFObject &D = DCtx.getDWARFObj();
469 DWARFDataExtractor AppleNamesSection(D, D.getAppleNamesSection(),
Paul Robinson17536b92017-06-29 16:52:08 +0000470 DCtx.isLittleEndian(), 0);
Rafael Espindolac398e672017-07-19 22:27:28 +0000471 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
Paul Robinson17536b92017-06-29 16:52:08 +0000472 DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000473
474 if (!AppleNames.extract()) {
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000475 return true;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000476 }
477
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000478 OS << "Verifying .apple_names...\n";
479
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000480 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000481 uint32_t NumBuckets = AppleNames.getNumBuckets();
482 uint32_t NumHashes = AppleNames.getNumHashes();
483
484 uint32_t BucketsOffset =
485 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000486 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
487 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000488
489 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
490 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
491 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000492 OS << format("error: Bucket[%d] has invalid hash index: %u\n", BucketIdx,
493 HashIdx);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000494 ++NumAppleNamesErrors;
495 }
496 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000497
498 uint32_t NumAtoms = AppleNames.getAtomsDesc().size();
499 if (NumAtoms == 0) {
500 OS << "error: no atoms; failed to read HashData\n";
501 ++NumAppleNamesErrors;
502 return false;
503 }
504
505 if (!AppleNames.validateForms()) {
506 OS << "error: unsupported form; failed to read HashData\n";
507 ++NumAppleNamesErrors;
508 return false;
509 }
510
511 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
512 uint32_t HashOffset = HashesBase + 4 * HashIdx;
513 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
514 uint32_t Hash = AppleNamesSection.getU32(&HashOffset);
515 uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset);
516 if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset,
517 sizeof(uint64_t))) {
518 OS << format("error: Hash[%d] has invalid HashData offset: 0x%08x\n",
519 HashIdx, HashDataOffset);
520 ++NumAppleNamesErrors;
521 }
522
523 uint32_t StrpOffset;
524 uint32_t StringOffset;
525 uint32_t StringCount = 0;
526 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
527
528 while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) {
529 const uint32_t NumHashDataObjects =
530 AppleNamesSection.getU32(&HashDataOffset);
531 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
532 ++HashDataIdx) {
533 DieOffset = AppleNames.readAtoms(HashDataOffset);
534 if (!DCtx.getDIEForOffset(DieOffset)) {
535 const uint32_t BucketIdx =
536 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
537 StringOffset = StrpOffset;
538 const char *Name = StrData.getCStr(&StringOffset);
539 if (!Name)
540 Name = "<NULL>";
541
542 OS << format(
543 "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x "
544 "Str[%u] = 0x%08x "
545 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
546 BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx,
547 DieOffset, Name);
548
549 ++NumAppleNamesErrors;
550 }
551 }
552 ++StringCount;
553 }
554 }
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000555 return NumAppleNamesErrors == 0;
556}