]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302069, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / DebugInfo / DWARF / DWARFVerifier.cpp
1 //===- 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
22 using namespace llvm;
23 using namespace dwarf;
24 using namespace object;
25
26 void DWARFVerifier::verifyDebugInfoAttribute(DWARFDie &Die,
27                                              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;
65
66   default:
67     break;
68   }
69 }
70
71 void DWARFVerifier::verifyDebugInfoForm(DWARFDie &Die,
72                                         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());
99       }
100     }
101     break;
102   }
103   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 }
138
139 void DWARFVerifier::veifyDebugInfoReferences() {
140   // 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   }
157 }
158
159 bool 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   }
175   veifyDebugInfoReferences();
176   return NumDebugInfoErrors == 0;
177 }
178
179 void DWARFVerifier::verifyDebugLineStmtOffsets() {
180   std::map<uint64_t, DWARFDie> StmtListToDie;
181   for (const auto &CU : DCtx.compile_units()) {
182     auto Die = CU->getUnitDIE();
183     // 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.
186     auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
187     if (!StmtSectionOffset)
188       continue;
189     const uint32_t LineTableOffset = *StmtSectionOffset;
190     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);
203       // 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     }
207     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 "
212          << format("0x%08" PRIx32, Die.getOffset())
213          << ", have the same DW_AT_stmt_list section offset:\n";
214       Iter->second.dump(OS, 0);
215       Die.dump(OS, 0);
216       OS << '\n';
217       // Already verified this line table before, no need to do it again.
218       continue;
219     }
220     StmtListToDie[LineTableOffset] = Die;
221   }
222 }
223
224 void DWARFVerifier::verifyDebugLineRows() {
225   for (const auto &CU : DCtx.compile_units()) {
226     auto Die = CU->getUnitDIE();
227     auto LineTable = DCtx.getLineTableForUnit(CU.get());
228     // If there is no line table we will have created an error in the
229     // .debug_info verifier or in verifyDebugLineStmtOffsets().
230     if (!LineTable)
231       continue;
232     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;
238         OS << "error: .debug_line["
239            << format("0x%08" PRIx32,
240                      *toSectionOffset(Die.find(DW_AT_stmt_list)))
241            << "] 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;
253         OS << "error: .debug_line["
254            << format("0x%08" PRIx32,
255                      *toSectionOffset(Die.find(DW_AT_stmt_list)))
256            << "][" << 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   }
269 }
270
271 bool DWARFVerifier::handleDebugLine() {
272   NumDebugLineErrors = 0;
273   OS << "Verifying .debug_line...\n";
274   verifyDebugLineStmtOffsets();
275   verifyDebugLineRows();
276   return NumDebugLineErrors == 0;
277 }