blob: 8a544296f65cbc71e9eee942a5489b26a1690772 [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"
17#include "llvm/Support/raw_ostream.h"
18#include <map>
19#include <set>
20#include <vector>
21
22using namespace llvm;
23using namespace dwarf;
24using namespace object;
25
Greg Clayton58a2e0d2017-05-08 21:29:17 +000026void DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
Greg Claytonc5b2d562017-05-03 18:25:46 +000027 DWARFAttribute &AttrValue) {
28 const auto Attr = AttrValue.Attr;
29 switch (Attr) {
30 case DW_AT_ranges:
31 // Make sure the offset in the DW_AT_ranges attribute is valid.
32 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
33 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
34 ++NumDebugInfoErrors;
35 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
36 "bounds:\n";
37 Die.dump(OS, 0);
38 OS << "\n";
39 }
40 } else {
41 ++NumDebugInfoErrors;
42 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
43 Die.dump(OS, 0);
44 OS << "\n";
45 }
46 break;
47 case DW_AT_stmt_list:
48 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
49 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
50 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
51 ++NumDebugInfoErrors;
52 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
53 "bounds: "
54 << format("0x%08" PRIx32, *SectionOffset) << "\n";
55 Die.dump(OS, 0);
56 OS << "\n";
57 }
58 } else {
59 ++NumDebugInfoErrors;
60 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
61 Die.dump(OS, 0);
62 OS << "\n";
63 }
64 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +000065
Greg Claytonc5b2d562017-05-03 18:25:46 +000066 default:
67 break;
68 }
69}
Greg Claytonb8c162b2017-05-03 16:02:29 +000070
Greg Clayton58a2e0d2017-05-08 21:29:17 +000071void DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
Greg Claytonc5b2d562017-05-03 18:25:46 +000072 DWARFAttribute &AttrValue) {
73 const auto Form = AttrValue.Value.getForm();
74 switch (Form) {
75 case DW_FORM_ref1:
76 case DW_FORM_ref2:
77 case DW_FORM_ref4:
78 case DW_FORM_ref8:
79 case DW_FORM_ref_udata: {
80 // Verify all CU relative references are valid CU offsets.
81 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
82 assert(RefVal);
83 if (RefVal) {
84 auto DieCU = Die.getDwarfUnit();
85 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
86 auto CUOffset = AttrValue.Value.getRawUValue();
87 if (CUOffset >= CUSize) {
88 ++NumDebugInfoErrors;
89 OS << "error: " << FormEncodingString(Form) << " CU offset "
90 << format("0x%08" PRIx32, CUOffset)
91 << " is invalid (must be less than CU size of "
92 << format("0x%08" PRIx32, CUSize) << "):\n";
93 Die.dump(OS, 0);
94 OS << "\n";
95 } else {
96 // Valid reference, but we will verify it points to an actual
97 // DIE later.
98 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +000099 }
100 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000101 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000102 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000103 case DW_FORM_ref_addr: {
104 // Verify all absolute DIE references have valid offsets in the
105 // .debug_info section.
106 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
107 assert(RefVal);
108 if (RefVal) {
109 if (*RefVal >= DCtx.getInfoSection().Data.size()) {
110 ++NumDebugInfoErrors;
111 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
112 "bounds:\n";
113 Die.dump(OS, 0);
114 OS << "\n";
115 } else {
116 // Valid reference, but we will verify it points to an actual
117 // DIE later.
118 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
119 }
120 }
121 break;
122 }
123 case DW_FORM_strp: {
124 auto SecOffset = AttrValue.Value.getAsSectionOffset();
125 assert(SecOffset); // DW_FORM_strp is a section offset.
126 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
127 ++NumDebugInfoErrors;
128 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
129 Die.dump(OS, 0);
130 OS << "\n";
131 }
132 break;
133 }
134 default:
135 break;
136 }
137}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000138
Greg Clayton5404f112017-05-08 20:53:00 +0000139void DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000140 // Take all references and make sure they point to an actual DIE by
141 // getting the DIE by offset and emitting an error
142 OS << "Verifying .debug_info references...\n";
143 for (auto Pair : ReferenceToDIEOffsets) {
144 auto Die = DCtx.getDIEForOffset(Pair.first);
145 if (Die)
146 continue;
147 ++NumDebugInfoErrors;
148 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
149 << ". Offset is in between DIEs:\n";
150 for (auto Offset : Pair.second) {
151 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
152 ReferencingDie.dump(OS, 0);
153 OS << "\n";
154 }
155 OS << "\n";
156 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000157}
158
159bool DWARFVerifier::handleDebugInfo() {
160 NumDebugInfoErrors = 0;
161 OS << "Verifying .debug_info...\n";
162 for (const auto &CU : DCtx.compile_units()) {
163 unsigned NumDies = CU->getNumDIEs();
164 for (unsigned I = 0; I < NumDies; ++I) {
165 auto Die = CU->getDIEAtIndex(I);
166 const auto Tag = Die.getTag();
167 if (Tag == DW_TAG_null)
168 continue;
169 for (auto AttrValue : Die.attributes()) {
170 verifyDebugInfoAttribute(Die, AttrValue);
171 verifyDebugInfoForm(Die, AttrValue);
172 }
173 }
174 }
Eugene Zemtsov3b52dbd2017-05-08 21:20:53 +0000175 verifyDebugInfoReferences();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000176 return NumDebugInfoErrors == 0;
177}
178
Greg Claytonc5b2d562017-05-03 18:25:46 +0000179void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000180 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000181 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000182 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000183 // Get the attribute value as a section offset. No need to produce an
184 // error here if the encoding isn't correct because we validate this in
185 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000186 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000187 if (!StmtSectionOffset)
188 continue;
189 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000190 auto LineTable = DCtx.getLineTableForUnit(CU.get());
191 if (LineTableOffset < DCtx.getLineSection().Data.size()) {
192 if (!LineTable) {
193 ++NumDebugLineErrors;
194 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
195 << "] was not able to be parsed for CU:\n";
196 Die.dump(OS, 0);
197 OS << '\n';
198 continue;
199 }
200 } else {
201 // Make sure we don't get a valid line table back if the offset is wrong.
202 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000203 // Skip this line table as it isn't valid. No need to create an error
204 // here because we validate this in the .debug_info verifier.
205 continue;
206 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000207 auto Iter = StmtListToDie.find(LineTableOffset);
208 if (Iter != StmtListToDie.end()) {
209 ++NumDebugLineErrors;
210 OS << "error: two compile unit DIEs, "
211 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000212 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000213 << ", have the same DW_AT_stmt_list section offset:\n";
214 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000215 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000216 OS << '\n';
217 // Already verified this line table before, no need to do it again.
218 continue;
219 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000220 StmtListToDie[LineTableOffset] = Die;
221 }
222}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000223
Greg Claytonc5b2d562017-05-03 18:25:46 +0000224void DWARFVerifier::verifyDebugLineRows() {
225 for (const auto &CU : DCtx.compile_units()) {
226 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000227 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000228 // If there is no line table we will have created an error in the
229 // .debug_info verifier or in verifyDebugLineStmtOffsets().
230 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000231 continue;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000232 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
233 uint64_t PrevAddress = 0;
234 uint32_t RowIndex = 0;
235 for (const auto &Row : LineTable->Rows) {
236 if (Row.Address < PrevAddress) {
237 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000238 OS << "error: .debug_line["
239 << format("0x%08" PRIx32,
240 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000241 << "] row[" << RowIndex
242 << "] decreases in address from previous row:\n";
243
244 DWARFDebugLine::Row::dumpTableHeader(OS);
245 if (RowIndex > 0)
246 LineTable->Rows[RowIndex - 1].dump(OS);
247 Row.dump(OS);
248 OS << '\n';
249 }
250
251 if (Row.File > MaxFileIndex) {
252 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000253 OS << "error: .debug_line["
254 << format("0x%08" PRIx32,
255 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000256 << "][" << RowIndex << "] has invalid file index " << Row.File
257 << " (valid values are [1," << MaxFileIndex << "]):\n";
258 DWARFDebugLine::Row::dumpTableHeader(OS);
259 Row.dump(OS);
260 OS << '\n';
261 }
262 if (Row.EndSequence)
263 PrevAddress = 0;
264 else
265 PrevAddress = Row.Address;
266 ++RowIndex;
267 }
268 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000269}
270
271bool DWARFVerifier::handleDebugLine() {
272 NumDebugLineErrors = 0;
273 OS << "Verifying .debug_line...\n";
274 verifyDebugLineStmtOffsets();
275 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000276 return NumDebugLineErrors == 0;
277}