LLVM 23.0.0git
DWARFVerifier.h
Go to the documentation of this file.
1//===- DWARFVerifier.h ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
10#define LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
11
13#include "llvm/ADT/StringMap.h"
21#include <cstdint>
22#include <map>
23#include <mutex>
24#include <set>
25
26namespace llvm {
27class raw_ostream;
29class DWARFUnit;
30class DWARFUnitVector;
31struct DWARFAttribute;
32class DWARFContext;
35class DataExtractor;
36struct DWARFSection;
37
39 unsigned OverallCount;
40 std::map<std::string, unsigned> DetailedCounts;
41};
42
44private:
45 std::mutex WriteMutex;
46 std::map<std::string, AggregationData, std::less<>> Aggregation;
47 uint64_t NumErrors = 0;
48 bool IncludeDetail;
49
50public:
51 OutputCategoryAggregator(bool includeDetail = false)
52 : IncludeDetail(includeDetail) {}
53 void ShowDetail(bool showDetail) { IncludeDetail = showDetail; }
54 size_t GetNumCategories() const { return Aggregation.size(); }
55 LLVM_ABI void Report(StringRef category, function_ref<void()> detailCallback);
56 LLVM_ABI void Report(StringRef category, StringRef sub_category,
57 function_ref<void()> detailCallback);
58 LLVM_ABI void
59 EnumerateResults(function_ref<void(StringRef, unsigned)> handleCounts);
61 StringRef category, function_ref<void(StringRef, unsigned)> handleCounts);
62 /// Return the number of errors that have been reported.
63 uint64_t GetNumErrors() const { return NumErrors; }
64};
65
66/// A class that verifies DWARF debug information given a DWARF Context.
68public:
69 /// A class that keeps the address range information for a single DIE.
70 struct DieRangeInfo {
72
73 /// Sorted DWARFAddressRanges.
74 std::vector<DWARFAddressRange> Ranges;
75
76 /// Sorted DWARFAddressRangeInfo.
77 std::set<DieRangeInfo> Children;
78
79 DieRangeInfo() = default;
81
82 /// Used for unit testing.
83 DieRangeInfo(std::vector<DWARFAddressRange> Ranges)
84 : Ranges(std::move(Ranges)) {}
85
86 typedef std::set<DieRangeInfo>::const_iterator die_range_info_iterator;
87
88 /// Inserts the address range. If the range overlaps with an existing
89 /// range, the range that it overlaps with will be returned and the two
90 /// address ranges will be unioned together in "Ranges". If a duplicate
91 /// entry is attempted to be added, the duplicate range will not actually be
92 /// added and the returned iterator will point to end().
93 ///
94 /// This is used for finding overlapping ranges in the DW_AT_ranges
95 /// attribute of a DIE. It is also used as a set of address ranges that
96 /// children address ranges must all be contained in.
97 LLVM_ABI std::optional<DWARFAddressRange>
98 insert(const DWARFAddressRange &R);
99
100 /// Inserts the address range info. If any of its ranges overlaps with a
101 /// range in an existing range info, the range info is *not* added and an
102 /// iterator to the overlapping range info. If a duplicate entry is
103 /// attempted to be added, the duplicate range will not actually be added
104 /// and the returned iterator will point to end().
105 ///
106 /// This is used for finding overlapping children of the same DIE.
108
109 /// Return true if ranges in this object contains all ranges within RHS.
110 LLVM_ABI bool contains(const DieRangeInfo &RHS) const;
111
112 /// Return true if any range in this object intersects with any range in
113 /// RHS. Identical ranges are not considered to be intersecting.
114 LLVM_ABI bool intersects(const DieRangeInfo &RHS) const;
115 };
116
117private:
118 raw_ostream &OS;
119 DWARFContext &DCtx;
120 DIDumpOptions DumpOpts;
121 uint32_t NumDebugLineErrors = 0;
122 OutputCategoryAggregator ErrorCategory;
123 // Used to relax some checks that do not currently work portably
124 bool IsObjectFile;
125 bool IsMachOObject;
126 using ReferenceMap = std::map<uint64_t, std::set<uint64_t>>;
127 std::mutex AccessMutex;
128
129 raw_ostream &error() const;
130 raw_ostream &warn() const;
131 raw_ostream &note() const;
132 raw_ostream &dump(const DWARFDie &Die, unsigned indent = 0) const;
133
134 /// Verifies the abbreviations section.
135 ///
136 /// This function currently checks that:
137 /// --No abbreviation declaration has more than one attributes with the same
138 /// name.
139 ///
140 /// \param Abbrev Pointer to the abbreviations section we are verifying
141 /// Abbrev can be a pointer to either .debug_abbrev or debug_abbrev.dwo.
142 ///
143 /// \returns The number of errors that occurred during verification.
144 unsigned verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev);
145
146 /// Verifies the header of a unit in a .debug_info or .debug_types section.
147 ///
148 /// This function currently checks for:
149 /// - Unit is in 32-bit DWARF format. The function can be modified to
150 /// support 64-bit format.
151 /// - The DWARF version is valid
152 /// - The unit type is valid (if unit is in version >=5)
153 /// - The unit doesn't extend beyond the containing section
154 /// - The address size is valid
155 /// - The offset in the .debug_abbrev section is valid
156 ///
157 /// \param DebugInfoData The section data
158 /// \param Offset A reference to the offset start of the unit. The offset will
159 /// be updated to point to the next unit in the section
160 /// \param UnitIndex The index of the unit to be verified
161 /// \param UnitType A reference to the type of the unit
162 /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is
163 /// in 64-bit format.
164 ///
165 /// \returns true if the header is verified successfully, false otherwise.
166 bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
167 uint64_t *Offset, unsigned UnitIndex, uint8_t &UnitType,
168 bool &isUnitDWARF64);
169 bool verifyName(const DWARFDie &Die);
170
171 /// Verifies the header of a unit in a .debug_info or .debug_types section.
172 ///
173 /// This function currently verifies:
174 /// - The debug info attributes.
175 /// - The debug info form=s.
176 /// - The presence of a root DIE.
177 /// - That the root DIE is a unit DIE.
178 /// - If a unit type is provided, that the unit DIE matches the unit type.
179 /// - The DIE ranges.
180 /// - That call site entries are only nested within subprograms with a
181 /// DW_AT_call attribute.
182 ///
183 /// \param Unit The DWARF Unit to verify.
184 ///
185 /// \returns The number of errors that occurred during verification.
186 unsigned verifyUnitContents(DWARFUnit &Unit,
187 ReferenceMap &UnitLocalReferences,
188 ReferenceMap &CrossUnitReferences);
189
190 /// Verifies the unit headers and contents in a .debug_info or .debug_types
191 /// section.
192 ///
193 /// \param S The DWARF Section to verify.
194 ///
195 /// \returns The number of errors that occurred during verification.
196 unsigned verifyUnitSection(const DWARFSection &S);
197 unsigned verifyUnits(const DWARFUnitVector &Units);
198
199 unsigned verifyIndex(StringRef Name, DWARFSectionKind SectionKind,
200 StringRef Index);
201
202 /// Verifies that a call site entry is nested within a subprogram with a
203 /// DW_AT_call attribute.
204 ///
205 /// \returns Number of errors that occurred during verification.
206 unsigned verifyDebugInfoCallSite(const DWARFDie &Die);
207
208 /// Verify that all Die ranges are valid.
209 ///
210 /// This function currently checks for:
211 /// - cases in which lowPC >= highPC
212 ///
213 /// \returns Number of errors that occurred during verification.
214 unsigned verifyDieRanges(const DWARFDie &Die, DieRangeInfo &ParentRI);
215
216 /// Verifies the attribute's DWARF attribute and its value.
217 ///
218 /// This function currently checks for:
219 /// - DW_AT_ranges values is a valid .debug_ranges offset
220 /// - DW_AT_stmt_list is a valid .debug_line offset
221 ///
222 /// \param Die The DWARF DIE that owns the attribute value
223 /// \param AttrValue The DWARF attribute value to check
224 ///
225 /// \returns NumErrors The number of errors occurred during verification of
226 /// attributes' values in a unit
227 unsigned verifyDebugInfoAttribute(const DWARFDie &Die,
228 DWARFAttribute &AttrValue);
229
230 /// Verifies the attribute's DWARF form.
231 ///
232 /// This function currently checks for:
233 /// - All DW_FORM_ref values that are CU relative have valid CU offsets
234 /// - All DW_FORM_ref_addr values have valid section offsets
235 /// - All DW_FORM_strp values have valid .debug_str offsets
236 ///
237 /// \param Die The DWARF DIE that owns the attribute value
238 /// \param AttrValue The DWARF attribute value to check
239 ///
240 /// \returns NumErrors The number of errors occurred during verification of
241 /// attributes' forms in a unit
242 unsigned verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue,
243 ReferenceMap &UnitLocalReferences,
244 ReferenceMap &CrossUnitReferences);
245
246 /// Verifies the all valid references that were found when iterating through
247 /// all of the DIE attributes.
248 ///
249 /// This function will verify that all references point to DIEs whose DIE
250 /// offset matches. This helps to ensure if a DWARF link phase moved things
251 /// around, that it doesn't create invalid references by failing to relocate
252 /// CU relative and absolute references.
253 ///
254 /// \returns NumErrors The number of errors occurred during verification of
255 /// references for the .debug_info and .debug_types sections
256 unsigned verifyDebugInfoReferences(
257 const ReferenceMap &,
258 llvm::function_ref<DWARFUnit *(uint64_t)> GetUnitForDieOffset);
259
260 /// Verify the DW_AT_stmt_list encoding and value and ensure that no
261 /// compile units that have the same DW_AT_stmt_list value.
262 void verifyDebugLineStmtOffsets();
263
264 /// Verify that all of the rows in the line table are valid.
265 ///
266 /// This function currently checks for:
267 /// - addresses within a sequence that decrease in value
268 /// - invalid file indexes
269 void verifyDebugLineRows();
270
271 /// Verify that an Apple-style accelerator table is valid.
272 ///
273 /// This function currently checks that:
274 /// - The fixed part of the header fits in the section
275 /// - The size of the section is as large as what the header describes
276 /// - There is at least one atom
277 /// - The form for each atom is valid
278 /// - The tag for each DIE in the table is valid
279 /// - The buckets have a valid index, or they are empty
280 /// - Each hashdata offset is valid
281 /// - Each DIE is valid
282 ///
283 /// \param AccelSection pointer to the section containing the acceleration table
284 /// \param StrData pointer to the string section
285 /// \param SectionName the name of the table we're verifying
286 ///
287 /// \returns The number of errors occurred during verification
288 void verifyAppleAccelTable(const DWARFSection *AccelSection,
289 DataExtractor *StrData, const char *SectionName);
290
291 void verifyDebugNamesCULists(const DWARFDebugNames &AccelTable);
292 void verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
293 const DataExtractor &StrData);
294 void verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI);
295 void verifyNameIndexAttribute(const DWARFDebugNames::NameIndex &NI,
296 const DWARFDebugNames::Abbrev &Abbr,
298 void verifyNameIndexEntries(
301 const DenseMap<uint64_t, DWARFUnit *> &CUOffsetsToDUMap);
302 void verifyNameIndexCompleteness(
303 const DWARFDie &Die, const DWARFDebugNames::NameIndex &NI,
304 const StringMap<DenseSet<uint64_t>> &NamesToDieOffsets);
305
306 /// Verify that the DWARF v5 accelerator table is valid.
307 ///
308 /// This function currently checks that:
309 /// - Headers individual Name Indices fit into the section and can be parsed.
310 /// - Abbreviation tables can be parsed and contain valid index attributes
311 /// with correct form encodings.
312 /// - The CU lists reference existing compile units.
313 /// - The buckets have a valid index, or they are empty.
314 /// - All names are reachable via the hash table (they have the correct hash,
315 /// and the hash is in the correct bucket).
316 /// - Information in the index entries is complete (all required entries are
317 /// present) and consistent with the debug_info section DIEs.
318 ///
319 /// \param AccelSection section containing the acceleration table
320 /// \param StrData string section
321 ///
322 /// \returns The number of errors occurred during verification
323 void verifyDebugNames(const DWARFSection &AccelSection,
324 const DataExtractor &StrData);
325
326 /// Verify that the the expression is valid within the context of unit U.
327 ///
328 /// \param E expression to verify.
329 /// \param U containing DWARFUnit, if any.
330 ///
331 /// returns true if E is a valid expression.
332 bool verifyExpression(const DWARFExpression &E, DWARFUnit *U);
333
334 /// Verify that the the expression operation is valid within the context of
335 /// unit U.
336 ///
337 /// \param Op operation to verify
338 /// \param U containing DWARFUnit, if any
339 ///
340 /// returns true if Op is a valid Dwarf operation
341 bool verifyExpressionOp(const DWARFExpression::Operation &Op, DWARFUnit *U);
342
343public:
347
348 /// Verify the information in any of the following sections, if available:
349 /// .debug_abbrev, debug_abbrev.dwo
350 ///
351 /// Any errors are reported to the stream that was this object was
352 /// constructed with.
353 ///
354 /// \returns true if .debug_abbrev and .debug_abbrev.dwo verify successfully,
355 /// false otherwise.
357
358 /// Verify the information in the .debug_info and .debug_types sections.
359 ///
360 /// Any errors are reported to the stream that this object was
361 /// constructed with.
362 ///
363 /// \returns true if all sections verify successfully, false otherwise.
365
366 /// Verify the information in the .debug_cu_index section.
367 ///
368 /// Any errors are reported to the stream that was this object was
369 /// constructed with.
370 ///
371 /// \returns true if the .debug_cu_index verifies successfully, false
372 /// otherwise.
374
375 /// Verify the information in the .debug_tu_index section.
376 ///
377 /// Any errors are reported to the stream that was this object was
378 /// constructed with.
379 ///
380 /// \returns true if the .debug_tu_index verifies successfully, false
381 /// otherwise.
383
384 /// Verify the information in the .debug_line section.
385 ///
386 /// Any errors are reported to the stream that was this object was
387 /// constructed with.
388 ///
389 /// \returns true if the .debug_line verifies successfully, false otherwise.
391
392 /// Verify the information in accelerator tables, if they exist.
393 ///
394 /// Any errors are reported to the stream that was this object was
395 /// constructed with.
396 ///
397 /// \returns true if the existing Apple-style accelerator tables verify
398 /// successfully, false otherwise.
400
401 /// Verify the information in the .debug_str_offsets[.dwo].
402 ///
403 /// Any errors are reported to the stream that was this object was
404 /// constructed with.
405 ///
406 /// \returns true if the .debug_line verifies successfully, false otherwise.
408 LLVM_ABI bool
409 verifyDebugStrOffsets(std::optional<dwarf::DwarfFormat> LegacyFormat,
410 StringRef SectionName, const DWARFSection &Section,
411 StringRef StrData);
412
413 /// Emits any aggregate information collected, depending on the dump options
414 LLVM_ABI void summarize();
415};
416
417static inline bool operator<(const DWARFVerifier::DieRangeInfo &LHS,
419 return std::tie(LHS.Ranges, LHS.Die) < std::tie(RHS.Ranges, RHS.Die);
420}
421
422} // end namespace llvm
423
424#endif // LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
This file defines the StringMap class.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define error(X)
Value * RHS
Value * LHS
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition AccelTable.h:203
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
Represents a single accelerator table within the DWARF v5 .debug_names section.
A single entry in the Name Table (DWARF v5 sect.
.debug_names section consists of one or more units.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
This class represents an Operation in the Expression.
Describe a collection of units.
Definition DWARFUnit.h:129
LLVM_ABI bool handleAccelTables()
Verify the information in accelerator tables, if they exist.
LLVM_ABI bool verifyDebugStrOffsets(std::optional< dwarf::DwarfFormat > LegacyFormat, StringRef SectionName, const DWARFSection &Section, StringRef StrData)
LLVM_ABI bool handleDebugTUIndex()
Verify the information in the .debug_tu_index section.
LLVM_ABI bool handleDebugStrOffsets()
Verify the information in the .debug_str_offsets[.dwo].
LLVM_ABI bool handleDebugCUIndex()
Verify the information in the .debug_cu_index section.
LLVM_ABI DWARFVerifier(raw_ostream &S, DWARFContext &D, DIDumpOptions DumpOpts=DIDumpOptions::getForSingleDIE())
LLVM_ABI bool handleDebugInfo()
Verify the information in the .debug_info and .debug_types sections.
LLVM_ABI bool handleDebugLine()
Verify the information in the .debug_line section.
LLVM_ABI void summarize()
Emits any aggregate information collected, depending on the dump options.
LLVM_ABI bool handleDebugAbbrev()
Verify the information in any of the following sections, if available: .debug_abbrev,...
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
void ShowDetail(bool showDetail)
LLVM_ABI void Report(StringRef category, function_ref< void()> detailCallback)
LLVM_ABI void EnumerateDetailedResultsFor(StringRef category, function_ref< void(StringRef, unsigned)> handleCounts)
LLVM_ABI void EnumerateResults(function_ref< void(StringRef, unsigned)> handleCounts)
OutputCategoryAggregator(bool includeDetail=false)
LLVM_ABI void Report(StringRef category, StringRef sub_category, function_ref< void()> detailCallback)
uint64_t GetNumErrors() const
Return the number of errors that have been reported.
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition SectionKind.h:22
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:128
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:573
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
DWARFSectionKind
The enum of section identifiers to be used in internal interfaces.
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
std::map< std::string, unsigned > DetailedCounts
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition DIContext.h:220
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value.
Abbreviation describing the encoding of Name Index entries.
A class that keeps the address range information for a single DIE.
std::vector< DWARFAddressRange > Ranges
Sorted DWARFAddressRanges.
LLVM_ABI bool contains(const DieRangeInfo &RHS) const
Return true if ranges in this object contains all ranges within RHS.
std::set< DieRangeInfo >::const_iterator die_range_info_iterator
LLVM_ABI bool intersects(const DieRangeInfo &RHS) const
Return true if any range in this object intersects with any range in RHS.
DieRangeInfo(std::vector< DWARFAddressRange > Ranges)
Used for unit testing.
std::set< DieRangeInfo > Children
Sorted DWARFAddressRangeInfo.
LLVM_ABI std::optional< DWARFAddressRange > insert(const DWARFAddressRange &R)
Inserts the address range.