LLVM  15.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 
12 #include "llvm/ADT/Optional.h"
18 #include <cstdint>
19 #include <map>
20 #include <set>
21 
22 namespace llvm {
23 class raw_ostream;
24 struct DWARFAddressRange;
25 class DWARFUnit;
26 class DWARFUnitVector;
27 struct DWARFAttribute;
28 class DWARFContext;
29 class DWARFDataExtractor;
30 class DWARFDebugAbbrev;
31 class DataExtractor;
32 struct DWARFSection;
33 
34 /// A class that verifies DWARF debug information given a DWARF Context.
36 public:
37  /// A class that keeps the address range information for a single DIE.
38  struct DieRangeInfo {
40 
41  /// Sorted DWARFAddressRanges.
42  std::vector<DWARFAddressRange> Ranges;
43 
44  /// Sorted DWARFAddressRangeInfo.
45  std::set<DieRangeInfo> Children;
46 
47  DieRangeInfo() = default;
49 
50  /// Used for unit testing.
51  DieRangeInfo(std::vector<DWARFAddressRange> Ranges)
52  : Ranges(std::move(Ranges)) {}
53 
54  typedef std::set<DieRangeInfo>::const_iterator die_range_info_iterator;
55 
56  /// Inserts the address range. If the range overlaps with an existing
57  /// range, the range that it overlaps with will be returned and the two
58  /// address ranges will be unioned together in "Ranges".
59  ///
60  /// This is used for finding overlapping ranges in the DW_AT_ranges
61  /// attribute of a DIE. It is also used as a set of address ranges that
62  /// children address ranges must all be contained in.
64 
65  /// Inserts the address range info. If any of its ranges overlaps with a
66  /// range in an existing range info, the range info is *not* added and an
67  /// iterator to the overlapping range info.
68  ///
69  /// This is used for finding overlapping children of the same DIE.
71 
72  /// Return true if ranges in this object contains all ranges within RHS.
73  bool contains(const DieRangeInfo &RHS) const;
74 
75  /// Return true if any range in this object intersects with any range in
76  /// RHS.
77  bool intersects(const DieRangeInfo &RHS) const;
78  };
79 
80 private:
81  raw_ostream &OS;
82  DWARFContext &DCtx;
83  DIDumpOptions DumpOpts;
84  uint32_t NumDebugLineErrors = 0;
85  // Used to relax some checks that do not currently work portably
86  bool IsObjectFile;
87  bool IsMachOObject;
88  using ReferenceMap = std::map<uint64_t, std::set<uint64_t>>;
89 
90  raw_ostream &error() const;
91  raw_ostream &warn() const;
92  raw_ostream &note() const;
93  raw_ostream &dump(const DWARFDie &Die, unsigned indent = 0) const;
94 
95  /// Verifies the abbreviations section.
96  ///
97  /// This function currently checks that:
98  /// --No abbreviation declaration has more than one attributes with the same
99  /// name.
100  ///
101  /// \param Abbrev Pointer to the abbreviations section we are verifying
102  /// Abbrev can be a pointer to either .debug_abbrev or debug_abbrev.dwo.
103  ///
104  /// \returns The number of errors that occurred during verification.
105  unsigned verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev);
106 
107  /// Verifies the header of a unit in a .debug_info or .debug_types section.
108  ///
109  /// This function currently checks for:
110  /// - Unit is in 32-bit DWARF format. The function can be modified to
111  /// support 64-bit format.
112  /// - The DWARF version is valid
113  /// - The unit type is valid (if unit is in version >=5)
114  /// - The unit doesn't extend beyond the containing section
115  /// - The address size is valid
116  /// - The offset in the .debug_abbrev section is valid
117  ///
118  /// \param DebugInfoData The section data
119  /// \param Offset A reference to the offset start of the unit. The offset will
120  /// be updated to point to the next unit in the section
121  /// \param UnitIndex The index of the unit to be verified
122  /// \param UnitType A reference to the type of the unit
123  /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is
124  /// in 64-bit format.
125  ///
126  /// \returns true if the header is verified successfully, false otherwise.
127  bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
128  uint64_t *Offset, unsigned UnitIndex, uint8_t &UnitType,
129  bool &isUnitDWARF64);
130  bool verifyName(const DWARFDie &Die);
131 
132  /// Verifies the header of a unit in a .debug_info or .debug_types section.
133  ///
134  /// This function currently verifies:
135  /// - The debug info attributes.
136  /// - The debug info form=s.
137  /// - The presence of a root DIE.
138  /// - That the root DIE is a unit DIE.
139  /// - If a unit type is provided, that the unit DIE matches the unit type.
140  /// - The DIE ranges.
141  /// - That call site entries are only nested within subprograms with a
142  /// DW_AT_call attribute.
143  ///
144  /// \param Unit The DWARF Unit to verify.
145  ///
146  /// \returns The number of errors that occurred during verification.
147  unsigned verifyUnitContents(DWARFUnit &Unit,
148  ReferenceMap &UnitLocalReferences,
149  ReferenceMap &CrossUnitReferences);
150 
151  /// Verifies the unit headers and contents in a .debug_info or .debug_types
152  /// section.
153  ///
154  /// \param S The DWARF Section to verify.
155  ///
156  /// \returns The number of errors that occurred during verification.
157  unsigned verifyUnitSection(const DWARFSection &S);
158  unsigned verifyUnits(const DWARFUnitVector &Units);
159 
160  unsigned verifyIndexes(const DWARFObject &DObj);
161  unsigned verifyIndex(StringRef Name, DWARFSectionKind SectionKind,
162  StringRef Index);
163 
164  /// Verifies that a call site entry is nested within a subprogram with a
165  /// DW_AT_call attribute.
166  ///
167  /// \returns Number of errors that occurred during verification.
168  unsigned verifyDebugInfoCallSite(const DWARFDie &Die);
169 
170  /// Verify that all Die ranges are valid.
171  ///
172  /// This function currently checks for:
173  /// - cases in which lowPC >= highPC
174  ///
175  /// \returns Number of errors that occurred during verification.
176  unsigned verifyDieRanges(const DWARFDie &Die, DieRangeInfo &ParentRI);
177 
178  /// Verifies the attribute's DWARF attribute and its value.
179  ///
180  /// This function currently checks for:
181  /// - DW_AT_ranges values is a valid .debug_ranges offset
182  /// - DW_AT_stmt_list is a valid .debug_line offset
183  ///
184  /// \param Die The DWARF DIE that owns the attribute value
185  /// \param AttrValue The DWARF attribute value to check
186  ///
187  /// \returns NumErrors The number of errors occurred during verification of
188  /// attributes' values in a unit
189  unsigned verifyDebugInfoAttribute(const DWARFDie &Die,
190  DWARFAttribute &AttrValue);
191 
192  /// Verifies the attribute's DWARF form.
193  ///
194  /// This function currently checks for:
195  /// - All DW_FORM_ref values that are CU relative have valid CU offsets
196  /// - All DW_FORM_ref_addr values have valid section offsets
197  /// - All DW_FORM_strp values have valid .debug_str offsets
198  ///
199  /// \param Die The DWARF DIE that owns the attribute value
200  /// \param AttrValue The DWARF attribute value to check
201  ///
202  /// \returns NumErrors The number of errors occurred during verification of
203  /// attributes' forms in a unit
204  unsigned verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue,
205  ReferenceMap &UnitLocalReferences,
206  ReferenceMap &CrossUnitReferences);
207 
208  /// Verifies the all valid references that were found when iterating through
209  /// all of the DIE attributes.
210  ///
211  /// This function will verify that all references point to DIEs whose DIE
212  /// offset matches. This helps to ensure if a DWARF link phase moved things
213  /// around, that it doesn't create invalid references by failing to relocate
214  /// CU relative and absolute references.
215  ///
216  /// \returns NumErrors The number of errors occurred during verification of
217  /// references for the .debug_info and .debug_types sections
218  unsigned verifyDebugInfoReferences(
219  const ReferenceMap &,
220  llvm::function_ref<DWARFUnit *(uint64_t)> GetUnitForDieOffset);
221 
222  /// Verify the DW_AT_stmt_list encoding and value and ensure that no
223  /// compile units that have the same DW_AT_stmt_list value.
224  void verifyDebugLineStmtOffsets();
225 
226  /// Verify that all of the rows in the line table are valid.
227  ///
228  /// This function currently checks for:
229  /// - addresses within a sequence that decrease in value
230  /// - invalid file indexes
231  void verifyDebugLineRows();
232 
233  /// Verify that an Apple-style accelerator table is valid.
234  ///
235  /// This function currently checks that:
236  /// - The fixed part of the header fits in the section
237  /// - The size of the section is as large as what the header describes
238  /// - There is at least one atom
239  /// - The form for each atom is valid
240  /// - The tag for each DIE in the table is valid
241  /// - The buckets have a valid index, or they are empty
242  /// - Each hashdata offset is valid
243  /// - Each DIE is valid
244  ///
245  /// \param AccelSection pointer to the section containing the acceleration table
246  /// \param StrData pointer to the string section
247  /// \param SectionName the name of the table we're verifying
248  ///
249  /// \returns The number of errors occurred during verification
250  unsigned verifyAppleAccelTable(const DWARFSection *AccelSection,
251  DataExtractor *StrData,
252  const char *SectionName);
253 
254  unsigned verifyDebugNamesCULists(const DWARFDebugNames &AccelTable);
255  unsigned verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
256  const DataExtractor &StrData);
257  unsigned verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI);
258  unsigned verifyNameIndexAttribute(const DWARFDebugNames::NameIndex &NI,
259  const DWARFDebugNames::Abbrev &Abbr,
261  unsigned verifyNameIndexEntries(const DWARFDebugNames::NameIndex &NI,
263  unsigned verifyNameIndexCompleteness(const DWARFDie &Die,
264  const DWARFDebugNames::NameIndex &NI);
265 
266  /// Verify that the DWARF v5 accelerator table is valid.
267  ///
268  /// This function currently checks that:
269  /// - Headers individual Name Indices fit into the section and can be parsed.
270  /// - Abbreviation tables can be parsed and contain valid index attributes
271  /// with correct form encodings.
272  /// - The CU lists reference existing compile units.
273  /// - The buckets have a valid index, or they are empty.
274  /// - All names are reachable via the hash table (they have the correct hash,
275  /// and the hash is in the correct bucket).
276  /// - Information in the index entries is complete (all required entries are
277  /// present) and consistent with the debug_info section DIEs.
278  ///
279  /// \param AccelSection section containing the acceleration table
280  /// \param StrData string section
281  ///
282  /// \returns The number of errors occurred during verification
283  unsigned verifyDebugNames(const DWARFSection &AccelSection,
284  const DataExtractor &StrData);
285 
286 public:
289 
290  /// Verify the information in any of the following sections, if available:
291  /// .debug_abbrev, debug_abbrev.dwo
292  ///
293  /// Any errors are reported to the stream that was this object was
294  /// constructed with.
295  ///
296  /// \returns true if .debug_abbrev and .debug_abbrev.dwo verify successfully,
297  /// false otherwise.
298  bool handleDebugAbbrev();
299 
300  /// Verify the information in the .debug_info and .debug_types sections.
301  ///
302  /// Any errors are reported to the stream that this object was
303  /// constructed with.
304  ///
305  /// \returns true if all sections verify successfully, false otherwise.
306  bool handleDebugInfo();
307 
308  /// Verify the information in the .debug_cu_index section.
309  ///
310  /// Any errors are reported to the stream that was this object was
311  /// constructed with.
312  ///
313  /// \returns true if the .debug_cu_index verifies successfully, false
314  /// otherwise.
315  bool handleDebugCUIndex();
316 
317  /// Verify the information in the .debug_tu_index section.
318  ///
319  /// Any errors are reported to the stream that was this object was
320  /// constructed with.
321  ///
322  /// \returns true if the .debug_tu_index verifies successfully, false
323  /// otherwise.
324  bool handleDebugTUIndex();
325 
326  /// Verify the information in the .debug_line section.
327  ///
328  /// Any errors are reported to the stream that was this object was
329  /// constructed with.
330  ///
331  /// \returns true if the .debug_line verifies successfully, false otherwise.
332  bool handleDebugLine();
333 
334  /// Verify the information in accelerator tables, if they exist.
335  ///
336  /// Any errors are reported to the stream that was this object was
337  /// constructed with.
338  ///
339  /// \returns true if the existing Apple-style accelerator tables verify
340  /// successfully, false otherwise.
341  bool handleAccelTables();
342 };
343 
344 static inline bool operator<(const DWARFVerifier::DieRangeInfo &LHS,
346  return std::tie(LHS.Ranges, LHS.Die) < std::tie(RHS.Ranges, RHS.Die);
347 }
348 
349 } // end namespace llvm
350 
351 #endif // LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
llvm::DWARFSection
Definition: DWARFSection.h:16
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DWARFVerifier
A class that verifies DWARF debug information given a DWARF Context.
Definition: DWARFVerifier.h:35
llvm::DWARFVerifier::DieRangeInfo::Ranges
std::vector< DWARFAddressRange > Ranges
Sorted DWARFAddressRanges.
Definition: DWARFVerifier.h:42
Optional.h
DWARFAcceleratorTable.h
llvm::DWARFVerifier::DWARFVerifier
DWARFVerifier(raw_ostream &S, DWARFContext &D, DIDumpOptions DumpOpts=DIDumpOptions::getForSingleDIE())
Definition: DWARFVerifier.cpp:940
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:47
DWARFAddressRange.h
llvm::DWARFDebugNames::NameTableEntry
A single entry in the Name Table (DWARF v5 sect.
Definition: DWARFAcceleratorTable.h:356
llvm::Optional
Definition: APInt.h:33
llvm::AccelTable
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:195
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
llvm::DWARFVerifier::handleDebugInfo
bool handleDebugInfo()
Verify the information in the .debug_info and .debug_types sections.
Definition: DWARFVerifier.cpp:450
llvm::DWARFDebugNames::Abbrev
Abbreviation describing the encoding of Name Index entries.
Definition: DWARFAcceleratorTable.h:270
llvm::DWARFDebugNames::AttributeEncoding
Index attribute and its encoding.
Definition: DWARFAcceleratorTable.h:256
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::DWARFAttribute
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value.
Definition: DWARFAttribute.h:24
llvm::DIDumpOptions::getForSingleDIE
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition: DIContext.h:201
llvm::DWARFDebugAbbrev
Definition: DWARFDebugAbbrev.h:56
llvm::DWARFDebugNames::NameIndex
Represents a single accelerator table within the DWARF v5 .debug_names section.
Definition: DWARFAcceleratorTable.h:388
llvm::DWARFSectionKind
DWARFSectionKind
The enum of section identifiers to be used in internal interfaces.
Definition: DWARFUnitIndex.h:56
DWARFDie.h
llvm::DWARFVerifier::handleDebugLine
bool handleDebugLine()
Verify the information in the .debug_line section.
Definition: DWARFVerifier.cpp:950
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::DWARFObject
Definition: DWARFObject.h:25
llvm::DWARFVerifier::handleAccelTables
bool handleAccelTables()
Verify the information in accelerator tables, if they exist.
Definition: DWARFVerifier.cpp:1598
llvm::DWARFVerifier::DieRangeInfo::die_range_info_iterator
std::set< DieRangeInfo >::const_iterator die_range_info_iterator
Definition: DWARFVerifier.h:54
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::DWARFVerifier::handleDebugAbbrev
bool handleDebugAbbrev()
Verify the information in any of the following sections, if available: .debug_abbrev,...
Definition: DWARFVerifier.cpp:323
llvm::DWARFVerifier::DieRangeInfo::intersects
bool intersects(const DieRangeInfo &RHS) const
Return true if any range in this object intersects with any range in RHS.
Definition: DWARFVerifier.cpp:108
llvm::operator<
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:339
llvm::DWARFVerifier::DieRangeInfo::Die
DWARFDie Die
Definition: DWARFVerifier.h:39
llvm::dwarf::UnitType
UnitType
Constants for unit types in DWARF v5.
Definition: Dwarf.h:465
llvm::DWARFVerifier::DieRangeInfo::DieRangeInfo
DieRangeInfo()=default
llvm::move
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:1665
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::DWARFVerifier::DieRangeInfo::DieRangeInfo
DieRangeInfo(std::vector< DWARFAddressRange > Ranges)
Used for unit testing.
Definition: DWARFVerifier.h:51
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::DWARFVerifier::handleDebugCUIndex
bool handleDebugCUIndex()
Verify the information in the .debug_cu_index section.
Definition: DWARFVerifier.cpp:440
llvm::DWARFVerifier::handleDebugTUIndex
bool handleDebugTUIndex()
Verify the information in the .debug_tu_index section.
Definition: DWARFVerifier.cpp:445
llvm::DWARFVerifier::DieRangeInfo::Children
std::set< DieRangeInfo > Children
Sorted DWARFAddressRangeInfo.
Definition: DWARFVerifier.h:45
DIContext.h
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
std
Definition: BitVector.h:851
llvm::DWARFUnit
Definition: DWARFUnit.h:205
llvm::SectionName
Definition: DWARFSection.h:21
llvm::DWARFVerifier::DieRangeInfo
A class that keeps the address range information for a single DIE.
Definition: DWARFVerifier.h:38
DWARFUnitIndex.h
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFVerifier::DieRangeInfo::contains
bool contains(const DieRangeInfo &RHS) const
Return true if ranges in this object contains all ranges within RHS.
Definition: DWARFVerifier.cpp:84
llvm::DWARFDie
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:43
llvm::DWARFAddressRange
Definition: DWARFAddressRange.h:25
llvm::DWARFDebugNames
.debug_names section consists of one or more units.
Definition: DWARFAcceleratorTable.h:231
llvm::DWARFVerifier::DieRangeInfo::DieRangeInfo
DieRangeInfo(DWARFDie Die)
Definition: DWARFVerifier.h:48
llvm::DWARFVerifier::DieRangeInfo::insert
Optional< DWARFAddressRange > insert(const DWARFAddressRange &R)
Inserts the address range.
Definition: DWARFVerifier.cpp:47
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:186
llvm::DWARFUnitVector
Describe a collection of units.
Definition: DWARFUnit.h:122