blob: 024ddf54dc151d248551bff384f6be3024d0fa15 [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 }
Renato Golin94d6c8f2017-05-09 12:36:50 +0000175 // FIXME: This introduced unexplained failures on buildbots (PR32972)
176 //verifyDebugInfoReferences();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000177 return NumDebugInfoErrors == 0;
178}
179
Greg Claytonc5b2d562017-05-03 18:25:46 +0000180void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000181 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000182 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000183 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000184 // Get the attribute value as a section offset. No need to produce an
185 // error here if the encoding isn't correct because we validate this in
186 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000187 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000188 if (!StmtSectionOffset)
189 continue;
190 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000191 auto LineTable = DCtx.getLineTableForUnit(CU.get());
192 if (LineTableOffset < DCtx.getLineSection().Data.size()) {
193 if (!LineTable) {
194 ++NumDebugLineErrors;
195 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
196 << "] was not able to be parsed for CU:\n";
197 Die.dump(OS, 0);
198 OS << '\n';
199 continue;
200 }
201 } else {
202 // Make sure we don't get a valid line table back if the offset is wrong.
203 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000204 // Skip this line table as it isn't valid. No need to create an error
205 // here because we validate this in the .debug_info verifier.
206 continue;
207 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000208 auto Iter = StmtListToDie.find(LineTableOffset);
209 if (Iter != StmtListToDie.end()) {
210 ++NumDebugLineErrors;
211 OS << "error: two compile unit DIEs, "
212 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000213 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000214 << ", have the same DW_AT_stmt_list section offset:\n";
215 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000216 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000217 OS << '\n';
218 // Already verified this line table before, no need to do it again.
219 continue;
220 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000221 StmtListToDie[LineTableOffset] = Die;
222 }
223}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000224
Greg Claytonc5b2d562017-05-03 18:25:46 +0000225void DWARFVerifier::verifyDebugLineRows() {
226 for (const auto &CU : DCtx.compile_units()) {
227 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000228 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000229 // If there is no line table we will have created an error in the
230 // .debug_info verifier or in verifyDebugLineStmtOffsets().
231 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000232 continue;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000233 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
234 uint64_t PrevAddress = 0;
235 uint32_t RowIndex = 0;
236 for (const auto &Row : LineTable->Rows) {
237 if (Row.Address < PrevAddress) {
238 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000239 OS << "error: .debug_line["
240 << format("0x%08" PRIx32,
241 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000242 << "] row[" << RowIndex
243 << "] decreases in address from previous row:\n";
244
245 DWARFDebugLine::Row::dumpTableHeader(OS);
246 if (RowIndex > 0)
247 LineTable->Rows[RowIndex - 1].dump(OS);
248 Row.dump(OS);
249 OS << '\n';
250 }
251
252 if (Row.File > MaxFileIndex) {
253 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000254 OS << "error: .debug_line["
255 << format("0x%08" PRIx32,
256 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000257 << "][" << RowIndex << "] has invalid file index " << Row.File
258 << " (valid values are [1," << MaxFileIndex << "]):\n";
259 DWARFDebugLine::Row::dumpTableHeader(OS);
260 Row.dump(OS);
261 OS << '\n';
262 }
263 if (Row.EndSequence)
264 PrevAddress = 0;
265 else
266 PrevAddress = Row.Address;
267 ++RowIndex;
268 }
269 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000270}
271
272bool DWARFVerifier::handleDebugLine() {
273 NumDebugLineErrors = 0;
274 OS << "Verifying .debug_line...\n";
275 verifyDebugLineStmtOffsets();
276 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000277 return NumDebugLineErrors == 0;
278}