blob: 438764a88700ea1cfe5e0ea823546e0a1c2ea062 [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;
NAKAMURA Takumi7ddaf3c2017-07-25 05:03:17 +0000218 OS << format("error: Invalid address range [0x%08" PRIx64
219 " - 0x%08" PRIx64 "].\n",
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000220 Range.LowPC, Range.HighPC);
221 }
222 }
223 return NumErrors;
224}
225
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000226unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
227 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000228 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000229 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000230 const auto Attr = AttrValue.Attr;
231 switch (Attr) {
232 case DW_AT_ranges:
233 // Make sure the offset in the DW_AT_ranges attribute is valid.
234 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000235 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000236 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000237 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
238 "bounds:\n";
239 Die.dump(OS, 0);
240 OS << "\n";
241 }
242 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000243 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000244 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
245 Die.dump(OS, 0);
246 OS << "\n";
247 }
248 break;
249 case DW_AT_stmt_list:
250 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
251 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000252 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000253 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000254 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
255 "bounds: "
Simon Dardis02d99452017-08-07 15:37:57 +0000256 << format("0x%08" PRIx64, *SectionOffset) << "\n";
Greg Claytonc5b2d562017-05-03 18:25:46 +0000257 Die.dump(OS, 0);
258 OS << "\n";
259 }
260 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000261 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000262 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
263 Die.dump(OS, 0);
264 OS << "\n";
265 }
266 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000267
Greg Claytonc5b2d562017-05-03 18:25:46 +0000268 default:
269 break;
270 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000271 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000272}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000273
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000274unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
275 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000276 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000277 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000278 const auto Form = AttrValue.Value.getForm();
279 switch (Form) {
280 case DW_FORM_ref1:
281 case DW_FORM_ref2:
282 case DW_FORM_ref4:
283 case DW_FORM_ref8:
284 case DW_FORM_ref_udata: {
285 // Verify all CU relative references are valid CU offsets.
286 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
287 assert(RefVal);
288 if (RefVal) {
289 auto DieCU = Die.getDwarfUnit();
290 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
291 auto CUOffset = AttrValue.Value.getRawUValue();
292 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000293 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000294 OS << "error: " << FormEncodingString(Form) << " CU offset "
Simon Dardis02d99452017-08-07 15:37:57 +0000295 << format("0x%08" PRIx64, CUOffset)
Greg Claytonc5b2d562017-05-03 18:25:46 +0000296 << " is invalid (must be less than CU size of "
297 << format("0x%08" PRIx32, CUSize) << "):\n";
298 Die.dump(OS, 0);
299 OS << "\n";
300 } else {
301 // Valid reference, but we will verify it points to an actual
302 // DIE later.
303 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000304 }
305 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000306 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000307 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000308 case DW_FORM_ref_addr: {
309 // Verify all absolute DIE references have valid offsets in the
310 // .debug_info section.
311 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
312 assert(RefVal);
313 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000314 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000315 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000316 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
317 "bounds:\n";
318 Die.dump(OS, 0);
319 OS << "\n";
320 } else {
321 // Valid reference, but we will verify it points to an actual
322 // DIE later.
323 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
324 }
325 }
326 break;
327 }
328 case DW_FORM_strp: {
329 auto SecOffset = AttrValue.Value.getAsSectionOffset();
330 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000331 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000332 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000333 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
334 Die.dump(OS, 0);
335 OS << "\n";
336 }
337 break;
338 }
339 default:
340 break;
341 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000342 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000343}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000344
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000345unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000346 // Take all references and make sure they point to an actual DIE by
347 // getting the DIE by offset and emitting an error
348 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000349 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000350 for (auto Pair : ReferenceToDIEOffsets) {
351 auto Die = DCtx.getDIEForOffset(Pair.first);
352 if (Die)
353 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000354 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000355 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
356 << ". Offset is in between DIEs:\n";
357 for (auto Offset : Pair.second) {
358 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
359 ReferencingDie.dump(OS, 0);
360 OS << "\n";
361 }
362 OS << "\n";
363 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000364 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000365}
366
Greg Claytonc5b2d562017-05-03 18:25:46 +0000367void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000368 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000369 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000370 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000371 // Get the attribute value as a section offset. No need to produce an
372 // error here if the encoding isn't correct because we validate this in
373 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000374 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000375 if (!StmtSectionOffset)
376 continue;
377 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000378 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000379 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000380 if (!LineTable) {
381 ++NumDebugLineErrors;
382 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
383 << "] was not able to be parsed for CU:\n";
384 Die.dump(OS, 0);
385 OS << '\n';
386 continue;
387 }
388 } else {
389 // Make sure we don't get a valid line table back if the offset is wrong.
390 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000391 // Skip this line table as it isn't valid. No need to create an error
392 // here because we validate this in the .debug_info verifier.
393 continue;
394 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000395 auto Iter = StmtListToDie.find(LineTableOffset);
396 if (Iter != StmtListToDie.end()) {
397 ++NumDebugLineErrors;
398 OS << "error: two compile unit DIEs, "
399 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000400 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000401 << ", have the same DW_AT_stmt_list section offset:\n";
402 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000403 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000404 OS << '\n';
405 // Already verified this line table before, no need to do it again.
406 continue;
407 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000408 StmtListToDie[LineTableOffset] = Die;
409 }
410}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000411
Greg Claytonc5b2d562017-05-03 18:25:46 +0000412void DWARFVerifier::verifyDebugLineRows() {
413 for (const auto &CU : DCtx.compile_units()) {
414 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000415 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000416 // If there is no line table we will have created an error in the
417 // .debug_info verifier or in verifyDebugLineStmtOffsets().
418 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000419 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000420
421 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000422 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000423 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
424 uint32_t FileIndex = 1;
425 StringMap<uint16_t> FullPathMap;
426 for (const auto &FileName : LineTable->Prologue.FileNames) {
427 // Verify directory index.
428 if (FileName.DirIdx > MaxDirIndex) {
429 ++NumDebugLineErrors;
430 OS << "error: .debug_line["
431 << format("0x%08" PRIx64,
432 *toSectionOffset(Die.find(DW_AT_stmt_list)))
433 << "].prologue.file_names[" << FileIndex
434 << "].dir_idx contains an invalid index: " << FileName.DirIdx
435 << "\n";
436 }
437
438 // Check file paths for duplicates.
439 std::string FullPath;
440 const bool HasFullPath = LineTable->getFileNameByIndex(
441 FileIndex, CU->getCompilationDir(),
442 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
443 assert(HasFullPath && "Invalid index?");
444 (void)HasFullPath;
445 auto It = FullPathMap.find(FullPath);
446 if (It == FullPathMap.end())
447 FullPathMap[FullPath] = FileIndex;
448 else if (It->second != FileIndex) {
449 OS << "warning: .debug_line["
450 << format("0x%08" PRIx64,
451 *toSectionOffset(Die.find(DW_AT_stmt_list)))
452 << "].prologue.file_names[" << FileIndex
453 << "] is a duplicate of file_names[" << It->second << "]\n";
454 }
455
456 FileIndex++;
457 }
458
459 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000460 uint64_t PrevAddress = 0;
461 uint32_t RowIndex = 0;
462 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000463 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000464 if (Row.Address < PrevAddress) {
465 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000466 OS << "error: .debug_line["
Simon Dardis02d99452017-08-07 15:37:57 +0000467 << format("0x%08" PRIx64,
Greg Claytonc5b2d562017-05-03 18:25:46 +0000468 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000469 << "] row[" << RowIndex
470 << "] decreases in address from previous row:\n";
471
472 DWARFDebugLine::Row::dumpTableHeader(OS);
473 if (RowIndex > 0)
474 LineTable->Rows[RowIndex - 1].dump(OS);
475 Row.dump(OS);
476 OS << '\n';
477 }
478
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000479 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000480 if (Row.File > MaxFileIndex) {
481 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000482 OS << "error: .debug_line["
Simon Dardis02d99452017-08-07 15:37:57 +0000483 << format("0x%08" PRIx64,
Greg Claytonc5b2d562017-05-03 18:25:46 +0000484 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000485 << "][" << RowIndex << "] has invalid file index " << Row.File
486 << " (valid values are [1," << MaxFileIndex << "]):\n";
487 DWARFDebugLine::Row::dumpTableHeader(OS);
488 Row.dump(OS);
489 OS << '\n';
490 }
491 if (Row.EndSequence)
492 PrevAddress = 0;
493 else
494 PrevAddress = Row.Address;
495 ++RowIndex;
496 }
497 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000498}
499
500bool DWARFVerifier::handleDebugLine() {
501 NumDebugLineErrors = 0;
502 OS << "Verifying .debug_line...\n";
503 verifyDebugLineStmtOffsets();
504 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000505 return NumDebugLineErrors == 0;
506}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000507
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000508unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection,
509 DataExtractor *StrData,
510 const char *SectionName) {
511 unsigned NumErrors = 0;
512 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
513 DCtx.isLittleEndian(), 0);
514 DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000515
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000516 OS << "Verifying " << SectionName << "...\n";
517 // Verify that the fixed part of the header is not too short.
518
519 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
520 OS << "\terror: Section is too small to fit a section header.\n";
521 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000522 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000523 // Verify that the section is not too short.
524 if (!AccelTable.extract()) {
525 OS << "\terror: Section is smaller than size described in section header.\n";
526 return 1;
527 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000528 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000529 uint32_t NumBuckets = AccelTable.getNumBuckets();
530 uint32_t NumHashes = AccelTable.getNumHashes();
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000531
532 uint32_t BucketsOffset =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000533 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000534 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
535 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000536 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000537 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000538 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000539 OS << format("\terror: Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000540 HashIdx);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000541 ++NumErrors;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000542 }
543 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000544 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000545 if (NumAtoms == 0) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000546 OS << "\terror: no atoms; failed to read HashData.\n";
547 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000548 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000549 if (!AccelTable.validateForms()) {
550 OS << "\terror: unsupported form; failed to read HashData.\n";
551 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000552 }
553
554 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
555 uint32_t HashOffset = HashesBase + 4 * HashIdx;
556 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000557 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
558 uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
559 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
560 sizeof(uint64_t))) {
561 OS << format("\terror: Hash[%d] has invalid HashData offset: 0x%08x.\n",
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000562 HashIdx, HashDataOffset);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000563 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000564 }
565
566 uint32_t StrpOffset;
567 uint32_t StringOffset;
568 uint32_t StringCount = 0;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000569 unsigned Offset;
570 unsigned Tag;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000571 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000572 const uint32_t NumHashDataObjects =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000573 AccelSectionData.getU32(&HashDataOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000574 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
575 ++HashDataIdx) {
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000576 std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
577 auto Die = DCtx.getDIEForOffset(Offset);
578 if (!Die) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000579 const uint32_t BucketIdx =
580 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
581 StringOffset = StrpOffset;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000582 const char *Name = StrData->getCStr(&StringOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000583 if (!Name)
584 Name = "<NULL>";
585
586 OS << format(
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000587 "\terror: %s Bucket[%d] Hash[%d] = 0x%08x "
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000588 "Str[%u] = 0x%08x "
589 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000590 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000591 HashDataIdx, Offset, Name);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000592
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000593 ++NumErrors;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000594 continue;
595 }
596 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
597 OS << "\terror: Tag " << dwarf::TagString(Tag)
598 << " in accelerator table does not match Tag "
599 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
600 << "].\n";
601 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000602 }
603 }
604 ++StringCount;
605 }
606 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000607 return NumErrors;
608}
609
610bool DWARFVerifier::handleAccelTables() {
611 const DWARFObject &D = DCtx.getDWARFObj();
612 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
613 unsigned NumErrors = 0;
614 if (!D.getAppleNamesSection().Data.empty())
615 NumErrors +=
616 verifyAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
617 if (!D.getAppleTypesSection().Data.empty())
618 NumErrors +=
619 verifyAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
620 if (!D.getAppleNamespacesSection().Data.empty())
621 NumErrors += verifyAccelTable(&D.getAppleNamespacesSection(), &StrData,
622 ".apple_namespaces");
623 if (!D.getAppleObjCSection().Data.empty())
624 NumErrors +=
625 verifyAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
626 return NumErrors == 0;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000627}