1 //===- DWARFVerifier.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/DebugInfo/DWARF/DWARFAcceleratorTable.h"
18 #include "llvm/Support/raw_ostream.h"
24 using namespace dwarf;
25 using namespace object;
27 void DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
28 DWARFAttribute &AttrValue) {
29 const auto Attr = AttrValue.Attr;
32 // Make sure the offset in the DW_AT_ranges attribute is valid.
33 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
34 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
36 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
43 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
49 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
50 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
51 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
53 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
55 << format("0x%08" PRIx32, *SectionOffset) << "\n";
61 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
72 void DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
73 DWARFAttribute &AttrValue) {
74 const auto Form = AttrValue.Value.getForm();
80 case DW_FORM_ref_udata: {
81 // Verify all CU relative references are valid CU offsets.
82 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
85 auto DieCU = Die.getDwarfUnit();
86 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
87 auto CUOffset = AttrValue.Value.getRawUValue();
88 if (CUOffset >= CUSize) {
90 OS << "error: " << FormEncodingString(Form) << " CU offset "
91 << format("0x%08" PRIx32, CUOffset)
92 << " is invalid (must be less than CU size of "
93 << format("0x%08" PRIx32, CUSize) << "):\n";
97 // Valid reference, but we will verify it points to an actual
99 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
104 case DW_FORM_ref_addr: {
105 // Verify all absolute DIE references have valid offsets in the
106 // .debug_info section.
107 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
110 if (*RefVal >= DCtx.getInfoSection().Data.size()) {
111 ++NumDebugInfoErrors;
112 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
117 // Valid reference, but we will verify it points to an actual
119 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
125 auto SecOffset = AttrValue.Value.getAsSectionOffset();
126 assert(SecOffset); // DW_FORM_strp is a section offset.
127 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
128 ++NumDebugInfoErrors;
129 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
140 void DWARFVerifier::verifyDebugInfoReferences() {
141 // Take all references and make sure they point to an actual DIE by
142 // getting the DIE by offset and emitting an error
143 OS << "Verifying .debug_info references...\n";
144 for (auto Pair : ReferenceToDIEOffsets) {
145 auto Die = DCtx.getDIEForOffset(Pair.first);
148 ++NumDebugInfoErrors;
149 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
150 << ". Offset is in between DIEs:\n";
151 for (auto Offset : Pair.second) {
152 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
153 ReferencingDie.dump(OS, 0);
160 bool DWARFVerifier::handleDebugInfo() {
161 NumDebugInfoErrors = 0;
162 OS << "Verifying .debug_info...\n";
163 for (const auto &CU : DCtx.compile_units()) {
164 unsigned NumDies = CU->getNumDIEs();
165 for (unsigned I = 0; I < NumDies; ++I) {
166 auto Die = CU->getDIEAtIndex(I);
167 const auto Tag = Die.getTag();
168 if (Tag == DW_TAG_null)
170 for (auto AttrValue : Die.attributes()) {
171 verifyDebugInfoAttribute(Die, AttrValue);
172 verifyDebugInfoForm(Die, AttrValue);
176 verifyDebugInfoReferences();
177 return NumDebugInfoErrors == 0;
180 void DWARFVerifier::verifyDebugLineStmtOffsets() {
181 std::map<uint64_t, DWARFDie> StmtListToDie;
182 for (const auto &CU : DCtx.compile_units()) {
183 auto Die = CU->getUnitDIE();
184 // 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.
187 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
188 if (!StmtSectionOffset)
190 const uint32_t LineTableOffset = *StmtSectionOffset;
191 auto LineTable = DCtx.getLineTableForUnit(CU.get());
192 if (LineTableOffset < DCtx.getLineSection().Data.size()) {
194 ++NumDebugLineErrors;
195 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
196 << "] was not able to be parsed for CU:\n";
202 // Make sure we don't get a valid line table back if the offset is wrong.
203 assert(LineTable == nullptr);
204 // 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.
208 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 "
213 << format("0x%08" PRIx32, Die.getOffset())
214 << ", have the same DW_AT_stmt_list section offset:\n";
215 Iter->second.dump(OS, 0);
218 // Already verified this line table before, no need to do it again.
221 StmtListToDie[LineTableOffset] = Die;
225 void DWARFVerifier::verifyDebugLineRows() {
226 for (const auto &CU : DCtx.compile_units()) {
227 auto Die = CU->getUnitDIE();
228 auto LineTable = DCtx.getLineTableForUnit(CU.get());
229 // If there is no line table we will have created an error in the
230 // .debug_info verifier or in verifyDebugLineStmtOffsets().
233 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;
239 OS << "error: .debug_line["
240 << format("0x%08" PRIx32,
241 *toSectionOffset(Die.find(DW_AT_stmt_list)))
242 << "] row[" << RowIndex
243 << "] decreases in address from previous row:\n";
245 DWARFDebugLine::Row::dumpTableHeader(OS);
247 LineTable->Rows[RowIndex - 1].dump(OS);
252 if (Row.File > MaxFileIndex) {
253 ++NumDebugLineErrors;
254 OS << "error: .debug_line["
255 << format("0x%08" PRIx32,
256 *toSectionOffset(Die.find(DW_AT_stmt_list)))
257 << "][" << RowIndex << "] has invalid file index " << Row.File
258 << " (valid values are [1," << MaxFileIndex << "]):\n";
259 DWARFDebugLine::Row::dumpTableHeader(OS);
266 PrevAddress = Row.Address;
272 bool DWARFVerifier::handleDebugLine() {
273 NumDebugLineErrors = 0;
274 OS << "Verifying .debug_line...\n";
275 verifyDebugLineStmtOffsets();
276 verifyDebugLineRows();
277 return NumDebugLineErrors == 0;
280 bool DWARFVerifier::handleAppleNames() {
281 NumAppleNamesErrors = 0;
283 DataExtractor AppleNamesSection(DCtx.getAppleNamesSection().Data,
284 DCtx.isLittleEndian(), 0);
285 DataExtractor StrData(DCtx.getStringSection(), DCtx.isLittleEndian(), 0);
286 DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData,
287 DCtx.getAppleNamesSection().Relocs);
289 if (!AppleNames.extract()) {
293 OS << "Verifying .apple_names...\n";
295 // Verify that all buckets have a valid hash index or are empty
296 uint32_t NumBuckets = AppleNames.getNumBuckets();
297 uint32_t NumHashes = AppleNames.getNumHashes();
299 uint32_t BucketsOffset =
300 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
302 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
303 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
304 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
305 OS << format("error: Bucket[%d] has invalid hash index: [%d]\n",
307 ++NumAppleNamesErrors;
310 return NumAppleNamesErrors == 0;