Coverage Report

Created: 2018-09-17 19:50

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/DebugInfo/DWARF/DWARFVerifier.h
Line
Count
Source (jump to first uncovered line)
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 "llvm/DebugInfo/DIContext.h"
14
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
15
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
16
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
17
#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
18
19
#include <cstdint>
20
#include <map>
21
#include <set>
22
23
namespace llvm {
24
class raw_ostream;
25
struct DWARFAttribute;
26
class DWARFContext;
27
class DWARFDie;
28
class DWARFUnit;
29
class DWARFCompileUnit;
30
class DWARFDataExtractor;
31
class DWARFDebugAbbrev;
32
class DataExtractor;
33
struct DWARFSection;
34
35
/// A class that verifies DWARF debug information given a DWARF Context.
36
class DWARFVerifier {
37
public:
38
  /// A class that keeps the address range information for a single DIE.
39
  struct DieRangeInfo {
40
    DWARFDie Die;
41
42
    /// Sorted DWARFAddressRanges.
43
    std::vector<DWARFAddressRange> Ranges;
44
45
    /// Sorted DWARFAddressRangeInfo.
46
    std::set<DieRangeInfo> Children;
47
48
    DieRangeInfo() = default;
49
    DieRangeInfo(DWARFDie Die) : Die(Die) {}
50
51
    /// Used for unit testing.
52
    DieRangeInfo(std::vector<DWARFAddressRange> Ranges)
53
        : Ranges(std::move(Ranges)) {}
54
55
    typedef std::vector<DWARFAddressRange>::const_iterator
56
        address_range_iterator;
57
    typedef std::set<DieRangeInfo>::const_iterator die_range_info_iterator;
58
59
    /// Inserts the address range. If the range overlaps with an existing
60
    /// range, the range is *not* added and an iterator to the overlapping
61
    /// range is returned.
62
    ///
63
    /// This is used for finding overlapping ranges within the same DIE.
64
    address_range_iterator insert(const DWARFAddressRange &R);
65
66
    /// Finds an address range in the sorted vector of ranges.
67
    address_range_iterator findRange(const DWARFAddressRange &R) const {
68
      auto Begin = Ranges.begin();
69
      auto End = Ranges.end();
70
      auto Iter = std::upper_bound(Begin, End, R);
71
      if (Iter != Begin)
72
        --Iter;
73
      return Iter;
74
    }
75
76
    /// Inserts the address range info. If any of its ranges overlaps with a
77
    /// range in an existing range info, the range info is *not* added and an
78
    /// iterator to the overlapping range info.
79
    ///
80
    /// This is used for finding overlapping children of the same DIE.
81
    die_range_info_iterator insert(const DieRangeInfo &RI);
82
83
    /// Return true if ranges in this object contains all ranges within RHS.
84
    bool contains(const DieRangeInfo &RHS) const;
85
86
    /// Return true if any range in this object intersects with any range in
87
    /// RHS.
88
    bool intersects(const DieRangeInfo &RHS) const;
89
  };
90
91
private:
92
  raw_ostream &OS;
93
  DWARFContext &DCtx;
94
  DIDumpOptions DumpOpts;
95
  /// A map that tracks all references (converted absolute references) so we
96
  /// can verify each reference points to a valid DIE and not an offset that
97
  /// lies between to valid DIEs.
98
  std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
99
  uint32_t NumDebugLineErrors = 0;
100
101
  raw_ostream &error() const;
102
  raw_ostream &warn() const;
103
  raw_ostream &note() const;
104
105
  /// Verifies the abbreviations section.
106
  ///
107
  /// This function currently checks that:
108
  /// --No abbreviation declaration has more than one attributes with the same
109
  /// name.
110
  ///
111
  /// \param Abbrev Pointer to the abbreviations section we are verifying
112
  /// Abbrev can be a pointer to either .debug_abbrev or debug_abbrev.dwo.
113
  ///
114
  /// \returns The number of errors that occurred during verification.
115
  unsigned verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev);
116
117
  /// Verifies the header of a unit in a .debug_info or .debug_types section.
118
  ///
119
  /// This function currently checks for:
120
  /// - Unit is in 32-bit DWARF format. The function can be modified to
121
  /// support 64-bit format.
122
  /// - The DWARF version is valid
123
  /// - The unit type is valid (if unit is in version >=5)
124
  /// - The unit doesn't extend beyond the containing section
125
  /// - The address size is valid
126
  /// - The offset in the .debug_abbrev section is valid
127
  ///
128
  /// \param DebugInfoData The section data
129
  /// \param Offset A reference to the offset start of the unit. The offset will
130
  /// be updated to point to the next unit in the section
131
  /// \param UnitIndex The index of the unit to be verified
132
  /// \param UnitType A reference to the type of the unit
133
  /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is
134
  /// in 64-bit format.
135
  ///
136
  /// \returns true if the header is verified successfully, false otherwise.
137
  bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
138
                        uint32_t *Offset, unsigned UnitIndex, uint8_t &UnitType,
139
                        bool &isUnitDWARF64);
140
141
  /// Verifies the header of a unit in a .debug_info or .debug_types section.
142
  ///
143
  /// This function currently verifies:
144
  ///  - The debug info attributes.
145
  ///  - The debug info form=s.
146
  ///  - The presence of a root DIE.
147
  ///  - That the root DIE is a unit DIE.
148
  ///  - If a unit type is provided, that the unit DIE matches the unit type.
149
  ///  - The DIE ranges.
150
  ///
151
  /// \param Unit      The DWARF Unit to verify.
152
  ///
153
  /// \returns The number of errors that occurred during verification.
154
  unsigned verifyUnitContents(DWARFUnit &Unit);
155
156
  /// Verifies the unit headers and contents in a .debug_info or .debug_types
157
  /// section.
158
  ///
159
  /// \param S           The DWARF Section to verify.
160
  /// \param SectionKind The object-file section kind that S comes from.
161
  ///
162
  /// \returns The number of errors that occurred during verification.
163
  unsigned verifyUnitSection(const DWARFSection &S,
164
                             DWARFSectionKind SectionKind);
165
166
  /// Verify that all Die ranges are valid.
167
  ///
168
  /// This function currently checks for:
169
  /// - cases in which lowPC >= highPC
170
  ///
171
  /// \returns Number of errors that occurred during verification.
172
  unsigned verifyDieRanges(const DWARFDie &Die, DieRangeInfo &ParentRI);
173
174
  /// Verifies the attribute's DWARF attribute and its value.
175
  ///
176
  /// This function currently checks for:
177
  /// - DW_AT_ranges values is a valid .debug_ranges offset
178
  /// - DW_AT_stmt_list is a valid .debug_line offset
179
  ///
180
  /// \param Die          The DWARF DIE that owns the attribute value
181
  /// \param AttrValue    The DWARF attribute value to check
182
  ///
183
  /// \returns NumErrors The number of errors occurred during verification of
184
  /// attributes' values in a unit
185
  unsigned verifyDebugInfoAttribute(const DWARFDie &Die,
186
                                    DWARFAttribute &AttrValue);
187
188
  /// Verifies the attribute's DWARF form.
189
  ///
190
  /// This function currently checks for:
191
  /// - All DW_FORM_ref values that are CU relative have valid CU offsets
192
  /// - All DW_FORM_ref_addr values have valid section offsets
193
  /// - All DW_FORM_strp values have valid .debug_str offsets
194
  ///
195
  /// \param Die          The DWARF DIE that owns the attribute value
196
  /// \param AttrValue    The DWARF attribute value to check
197
  ///
198
  /// \returns NumErrors The number of errors occurred during verification of
199
  /// attributes' forms in a unit
200
  unsigned verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue);
201
202
  /// Verifies the all valid references that were found when iterating through
203
  /// all of the DIE attributes.
204
  ///
205
  /// This function will verify that all references point to DIEs whose DIE
206
  /// offset matches. This helps to ensure if a DWARF link phase moved things
207
  /// around, that it doesn't create invalid references by failing to relocate
208
  /// CU relative and absolute references.
209
  ///
210
  /// \returns NumErrors The number of errors occurred during verification of
211
  /// references for the .debug_info and .debug_types sections
212
  unsigned verifyDebugInfoReferences();
213
214
  /// Verify the DW_AT_stmt_list encoding and value and ensure that no
215
  /// compile units that have the same DW_AT_stmt_list value.
216
  void verifyDebugLineStmtOffsets();
217
218
  /// Verify that all of the rows in the line table are valid.
219
  ///
220
  /// This function currently checks for:
221
  /// - addresses within a sequence that decrease in value
222
  /// - invalid file indexes
223
  void verifyDebugLineRows();
224
225
  /// Verify that an Apple-style accelerator table is valid.
226
  ///
227
  /// This function currently checks that:
228
  /// - The fixed part of the header fits in the section
229
  /// - The size of the section is as large as what the header describes
230
  /// - There is at least one atom
231
  /// - The form for each atom is valid
232
  /// - The tag for each DIE in the table is valid
233
  /// - The buckets have a valid index, or they are empty
234
  /// - Each hashdata offset is valid
235
  /// - Each DIE is valid
236
  ///
237
  /// \param AccelSection pointer to the section containing the acceleration table
238
  /// \param StrData pointer to the string section
239
  /// \param SectionName the name of the table we're verifying
240
  ///
241
  /// \returns The number of errors occurred during verification
242
  unsigned verifyAppleAccelTable(const DWARFSection *AccelSection,
243
                                 DataExtractor *StrData,
244
                                 const char *SectionName);
245
246
  unsigned verifyDebugNamesCULists(const DWARFDebugNames &AccelTable);
247
  unsigned verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
248
                                  const DataExtractor &StrData);
249
  unsigned verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI);
250
  unsigned verifyNameIndexAttribute(const DWARFDebugNames::NameIndex &NI,
251
                                    const DWARFDebugNames::Abbrev &Abbr,
252
                                    DWARFDebugNames::AttributeEncoding AttrEnc);
253
  unsigned verifyNameIndexEntries(const DWARFDebugNames::NameIndex &NI,
254
                                  const DWARFDebugNames::NameTableEntry &NTE);
255
  unsigned verifyNameIndexCompleteness(const DWARFDie &Die,
256
                                       const DWARFDebugNames::NameIndex &NI);
257
258
  /// Verify that the DWARF v5 accelerator table is valid.
259
  ///
260
  /// This function currently checks that:
261
  /// - Headers individual Name Indices fit into the section and can be parsed.
262
  /// - Abbreviation tables can be parsed and contain valid index attributes
263
  ///   with correct form encodings.
264
  /// - The CU lists reference existing compile units.
265
  /// - The buckets have a valid index, or they are empty.
266
  /// - All names are reachable via the hash table (they have the correct hash,
267
  ///   and the hash is in the correct bucket).
268
  /// - Information in the index entries is complete (all required entries are
269
  ///   present) and consistent with the debug_info section DIEs.
270
  ///
271
  /// \param AccelSection section containing the acceleration table
272
  /// \param StrData string section
273
  ///
274
  /// \returns The number of errors occurred during verification
275
  unsigned verifyDebugNames(const DWARFSection &AccelSection,
276
                            const DataExtractor &StrData);
277
278
public:
279
  DWARFVerifier(raw_ostream &S, DWARFContext &D,
280
                DIDumpOptions DumpOpts = DIDumpOptions::getForSingleDIE())
281
      : OS(S), DCtx(D), DumpOpts(std::move(DumpOpts)) {}
282
  /// Verify the information in any of the following sections, if available:
283
  /// .debug_abbrev, debug_abbrev.dwo
284
  ///
285
  /// Any errors are reported to the stream that was this object was
286
  /// constructed with.
287
  ///
288
  /// \returns true if .debug_abbrev and .debug_abbrev.dwo verify successfully,
289
  /// false otherwise.
290
  bool handleDebugAbbrev();
291
292
  /// Verify the information in the .debug_info and .debug_types sections.
293
  ///
294
  /// Any errors are reported to the stream that this object was
295
  /// constructed with.
296
  ///
297
  /// \returns true if all sections verify successfully, false otherwise.
298
  bool handleDebugInfo();
299
300
  /// Verify the information in the .debug_line section.
301
  ///
302
  /// Any errors are reported to the stream that was this object was
303
  /// constructed with.
304
  ///
305
  /// \returns true if the .debug_line verifies successfully, false otherwise.
306
  bool handleDebugLine();
307
308
  /// Verify the information in accelerator tables, if they exist.
309
  ///
310
  /// Any errors are reported to the stream that was this object was
311
  /// constructed with.
312
  ///
313
  /// \returns true if the existing Apple-style accelerator tables verify
314
  /// successfully, false otherwise.
315
  bool handleAccelTables();
316
};
317
318
static inline bool operator<(const DWARFVerifier::DieRangeInfo &LHS,
319
0
                             const DWARFVerifier::DieRangeInfo &RHS) {
320
0
  return std::tie(LHS.Ranges, LHS.Die) < std::tie(RHS.Ranges, RHS.Die);
321
0
}
322
323
} // end namespace llvm
324
325
#endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H