LLVM  5.0.0
DWARFVerifier.h
Go to the documentation of this file.
1 //===- DWARFVerifier.h ----------------------------------------------------===//
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 #ifndef LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
11 #define LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
12 
13 #include <cstdint>
14 #include <map>
15 #include <set>
16 
17 namespace llvm {
18 class raw_ostream;
19 struct DWARFAttribute;
20 class DWARFContext;
21 class DWARFDie;
22 class DWARFUnit;
23 class DWARFAcceleratorTable;
24 class DWARFDataExtractor;
25 
26 /// A class that verifies DWARF debug information given a DWARF Context.
28  raw_ostream &OS;
29  DWARFContext &DCtx;
30  /// A map that tracks all references (converted absolute references) so we
31  /// can verify each reference points to a valid DIE and not an offset that
32  /// lies between to valid DIEs.
33  std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
34  uint32_t NumDebugLineErrors = 0;
35  uint32_t NumAppleNamesErrors = 0;
36 
37  /// Verifies the header of a unit in the .debug_info section.
38  ///
39  /// This function currently checks for:
40  /// - Unit is in 32-bit DWARF format. The function can be modified to
41  /// support 64-bit format.
42  /// - The DWARF version is valid
43  /// - The unit type is valid (if unit is in version >=5)
44  /// - The unit doesn't extend beyond .debug_info section
45  /// - The address size is valid
46  /// - The offset in the .debug_abbrev section is valid
47  ///
48  /// \param DebugInfoData The .debug_info section data
49  /// \param Offset A reference to the offset start of the unit. The offset will
50  /// be updated to point to the next unit in .debug_info
51  /// \param UnitIndex The index of the unit to be verified
52  /// \param UnitType A reference to the type of the unit
53  /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is
54  /// in 64-bit format.
55  ///
56  /// \returns true if the header is verified successfully, false otherwise.
57  bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
58  uint32_t *Offset, unsigned UnitIndex, uint8_t &UnitType,
59  bool &isUnitDWARF64);
60 
61 
62  bool verifyUnitContents(DWARFUnit Unit);
63  /// Verifies the attribute's DWARF attribute and its value.
64  ///
65  /// This function currently checks for:
66  /// - DW_AT_ranges values is a valid .debug_ranges offset
67  /// - DW_AT_stmt_list is a valid .debug_line offset
68  ///
69  /// \param Die The DWARF DIE that owns the attribute value
70  /// \param AttrValue The DWARF attribute value to check
71  ///
72  /// \returns NumErrors The number of errors occured during verification of
73  /// attributes' values in a .debug_info section unit
74  unsigned verifyDebugInfoAttribute(const DWARFDie &Die,
75  DWARFAttribute &AttrValue);
76 
77  /// Verifies the attribute's DWARF form.
78  ///
79  /// This function currently checks for:
80  /// - All DW_FORM_ref values that are CU relative have valid CU offsets
81  /// - All DW_FORM_ref_addr values have valid .debug_info offsets
82  /// - All DW_FORM_strp values have valid .debug_str offsets
83  ///
84  /// \param Die The DWARF DIE that owns the attribute value
85  /// \param AttrValue The DWARF attribute value to check
86  ///
87  /// \returns NumErrors The number of errors occured during verification of
88  /// attributes' forms in a .debug_info section unit
89  unsigned verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue);
90 
91  /// Verifies the all valid references that were found when iterating through
92  /// all of the DIE attributes.
93  ///
94  /// This function will verify that all references point to DIEs whose DIE
95  /// offset matches. This helps to ensure if a DWARF link phase moved things
96  /// around, that it doesn't create invalid references by failing to relocate
97  /// CU relative and absolute references.
98  ///
99  /// \returns NumErrors The number of errors occured during verification of
100  /// references for the .debug_info section
101  unsigned verifyDebugInfoReferences();
102 
103  /// Verify the the DW_AT_stmt_list encoding and value and ensure that no
104  /// compile units that have the same DW_AT_stmt_list value.
105  void verifyDebugLineStmtOffsets();
106 
107  /// Verify that all of the rows in the line table are valid.
108  ///
109  /// This function currently checks for:
110  /// - addresses within a sequence that decrease in value
111  /// - invalid file indexes
112  void verifyDebugLineRows();
113 
114 public:
116  : OS(S), DCtx(D) {}
117  /// Verify the information in the .debug_info section.
118  ///
119  /// Any errors are reported to the stream that was this object was
120  /// constructed with.
121  ///
122  /// \returns true if the .debug_info verifies successfully, false otherwise.
123  bool handleDebugInfo();
124 
125  /// Verify the information in the .debug_line section.
126  ///
127  /// Any errors are reported to the stream that was this object was
128  /// constructed with.
129  ///
130  /// \returns true if the .debug_line verifies successfully, false otherwise.
131  bool handleDebugLine();
132 
133  /// Verify the information in the .apple_names accelerator table.
134  ///
135  /// Any errors are reported to the stream that was this object was
136  /// constructed with.
137  ///
138  /// \returns true if the .apple_names verifies successfully, false otherwise.
139  bool handleAppleNames();
140 };
141 
142 } // end namespace llvm
143 
144 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
bool handleDebugLine()
Verify the information in the .debug_line section.
bool handleDebugInfo()
Verify the information in the .debug_info section.
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value...
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
UnitType
Constants for unit types in DWARF v5.
Definition: Dwarf.h:319
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:52
A class that verifies DWARF debug information given a DWARF Context.
Definition: DWARFVerifier.h:27
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:105
bool handleAppleNames()
Verify the information in the .apple_names accelerator table.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
DWARFVerifier(raw_ostream &S, DWARFContext &D)
#define D
Definition: LargeTest.cpp:26