xref: /llvm-project/llvm/include/llvm/DebugInfo/DWARF/DWARFVerifier.h (revision ad32576cffc88bf7c359a528afbed7c2ae7ddb2d)
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/DebugInfo/DIContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
14 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
16 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
17 #include <cstdint>
18 #include <map>
19 #include <set>
20 
21 namespace llvm {
22 class raw_ostream;
23 struct DWARFAddressRange;
24 class DWARFUnit;
25 class DWARFUnitVector;
26 struct DWARFAttribute;
27 class DWARFContext;
28 class DWARFDataExtractor;
29 class DWARFDebugAbbrev;
30 class DataExtractor;
31 struct DWARFSection;
32 
33 class OutputCategoryAggregator {
34 private:
35   std::map<std::string, unsigned> Aggregation;
36   bool IncludeDetail;
37 
38 public:
39   OutputCategoryAggregator(bool includeDetail = false)
40       : IncludeDetail(includeDetail) {}
41   void ShowDetail(bool showDetail) { IncludeDetail = showDetail; }
42   size_t GetNumCategories() const { return Aggregation.size(); }
43   void Report(StringRef s, std::function<void()> detailCallback);
44   void EnumerateResults(std::function<void(StringRef, unsigned)> handleCounts);
45 };
46 
47 /// A class that verifies DWARF debug information given a DWARF Context.
48 class DWARFVerifier {
49 public:
50   /// A class that keeps the address range information for a single DIE.
51   struct DieRangeInfo {
52     DWARFDie Die;
53 
54     /// Sorted DWARFAddressRanges.
55     std::vector<DWARFAddressRange> Ranges;
56 
57     /// Sorted DWARFAddressRangeInfo.
58     std::set<DieRangeInfo> Children;
59 
60     DieRangeInfo() = default;
61     DieRangeInfo(DWARFDie Die) : Die(Die) {}
62 
63     /// Used for unit testing.
64     DieRangeInfo(std::vector<DWARFAddressRange> Ranges)
65         : Ranges(std::move(Ranges)) {}
66 
67     typedef std::set<DieRangeInfo>::const_iterator die_range_info_iterator;
68 
69     /// Inserts the address range. If the range overlaps with an existing
70     /// range, the range that it overlaps with will be returned and the two
71     /// address ranges will be unioned together in "Ranges". If a duplicate
72     /// entry is attempted to be added, the duplicate range will not actually be
73     /// added and the returned iterator will point to end().
74     ///
75     /// This is used for finding overlapping ranges in the DW_AT_ranges
76     /// attribute of a DIE. It is also used as a set of address ranges that
77     /// children address ranges must all be contained in.
78     std::optional<DWARFAddressRange> insert(const DWARFAddressRange &R);
79 
80     /// Inserts the address range info. If any of its ranges overlaps with a
81     /// range in an existing range info, the range info is *not* added and an
82     /// iterator to the overlapping range info. If a duplicate entry is
83     /// attempted to be added, the duplicate range will not actually be added
84     /// and the returned iterator will point to end().
85     ///
86     /// This is used for finding overlapping children of the same DIE.
87     die_range_info_iterator insert(const DieRangeInfo &RI);
88 
89     /// Return true if ranges in this object contains all ranges within RHS.
90     bool contains(const DieRangeInfo &RHS) const;
91 
92     /// Return true if any range in this object intersects with any range in
93     /// RHS. Identical ranges are not considered to be intersecting.
94     bool intersects(const DieRangeInfo &RHS) const;
95   };
96 
97 private:
98   raw_ostream &OS;
99   DWARFContext &DCtx;
100   DIDumpOptions DumpOpts;
101   uint32_t NumDebugLineErrors = 0;
102   OutputCategoryAggregator ErrorCategory;
103   // Used to relax some checks that do not currently work portably
104   bool IsObjectFile;
105   bool IsMachOObject;
106   using ReferenceMap = std::map<uint64_t, std::set<uint64_t>>;
107 
108   raw_ostream &error() const;
109   raw_ostream &warn() const;
110   raw_ostream &note() const;
111   raw_ostream &dump(const DWARFDie &Die, unsigned indent = 0) const;
112 
113   /// Verifies the abbreviations section.
114   ///
115   /// This function currently checks that:
116   /// --No abbreviation declaration has more than one attributes with the same
117   /// name.
118   ///
119   /// \param Abbrev Pointer to the abbreviations section we are verifying
120   /// Abbrev can be a pointer to either .debug_abbrev or debug_abbrev.dwo.
121   ///
122   /// \returns The number of errors that occurred during verification.
123   unsigned verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev);
124 
125   /// Verifies the header of a unit in a .debug_info or .debug_types section.
126   ///
127   /// This function currently checks for:
128   /// - Unit is in 32-bit DWARF format. The function can be modified to
129   /// support 64-bit format.
130   /// - The DWARF version is valid
131   /// - The unit type is valid (if unit is in version >=5)
132   /// - The unit doesn't extend beyond the containing section
133   /// - The address size is valid
134   /// - The offset in the .debug_abbrev section is valid
135   ///
136   /// \param DebugInfoData The section data
137   /// \param Offset A reference to the offset start of the unit. The offset will
138   /// be updated to point to the next unit in the section
139   /// \param UnitIndex The index of the unit to be verified
140   /// \param UnitType A reference to the type of the unit
141   /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is
142   /// in 64-bit format.
143   ///
144   /// \returns true if the header is verified successfully, false otherwise.
145   bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
146                         uint64_t *Offset, unsigned UnitIndex, uint8_t &UnitType,
147                         bool &isUnitDWARF64);
148   bool verifyName(const DWARFDie &Die);
149 
150   /// Verifies the header of a unit in a .debug_info or .debug_types section.
151   ///
152   /// This function currently verifies:
153   ///  - The debug info attributes.
154   ///  - The debug info form=s.
155   ///  - The presence of a root DIE.
156   ///  - That the root DIE is a unit DIE.
157   ///  - If a unit type is provided, that the unit DIE matches the unit type.
158   ///  - The DIE ranges.
159   ///  - That call site entries are only nested within subprograms with a
160   ///    DW_AT_call attribute.
161   ///
162   /// \param Unit      The DWARF Unit to verify.
163   ///
164   /// \returns The number of errors that occurred during verification.
165   unsigned verifyUnitContents(DWARFUnit &Unit,
166                               ReferenceMap &UnitLocalReferences,
167                               ReferenceMap &CrossUnitReferences);
168 
169   /// Verifies the unit headers and contents in a .debug_info or .debug_types
170   /// section.
171   ///
172   /// \param S           The DWARF Section to verify.
173   ///
174   /// \returns The number of errors that occurred during verification.
175   unsigned verifyUnitSection(const DWARFSection &S);
176   unsigned verifyUnits(const DWARFUnitVector &Units);
177 
178   unsigned verifyIndex(StringRef Name, DWARFSectionKind SectionKind,
179                        StringRef Index);
180 
181   /// Verifies that a call site entry is nested within a subprogram with a
182   /// DW_AT_call attribute.
183   ///
184   /// \returns Number of errors that occurred during verification.
185   unsigned verifyDebugInfoCallSite(const DWARFDie &Die);
186 
187   /// Verify that all Die ranges are valid.
188   ///
189   /// This function currently checks for:
190   /// - cases in which lowPC >= highPC
191   ///
192   /// \returns Number of errors that occurred during verification.
193   unsigned verifyDieRanges(const DWARFDie &Die, DieRangeInfo &ParentRI);
194 
195   /// Verifies the attribute's DWARF attribute and its value.
196   ///
197   /// This function currently checks for:
198   /// - DW_AT_ranges values is a valid .debug_ranges offset
199   /// - DW_AT_stmt_list is a valid .debug_line offset
200   ///
201   /// \param Die          The DWARF DIE that owns the attribute value
202   /// \param AttrValue    The DWARF attribute value to check
203   ///
204   /// \returns NumErrors The number of errors occurred during verification of
205   /// attributes' values in a unit
206   unsigned verifyDebugInfoAttribute(const DWARFDie &Die,
207                                     DWARFAttribute &AttrValue);
208 
209   /// Verifies the attribute's DWARF form.
210   ///
211   /// This function currently checks for:
212   /// - All DW_FORM_ref values that are CU relative have valid CU offsets
213   /// - All DW_FORM_ref_addr values have valid section offsets
214   /// - All DW_FORM_strp values have valid .debug_str offsets
215   ///
216   /// \param Die          The DWARF DIE that owns the attribute value
217   /// \param AttrValue    The DWARF attribute value to check
218   ///
219   /// \returns NumErrors The number of errors occurred during verification of
220   /// attributes' forms in a unit
221   unsigned verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue,
222                                ReferenceMap &UnitLocalReferences,
223                                ReferenceMap &CrossUnitReferences);
224 
225   /// Verifies the all valid references that were found when iterating through
226   /// all of the DIE attributes.
227   ///
228   /// This function will verify that all references point to DIEs whose DIE
229   /// offset matches. This helps to ensure if a DWARF link phase moved things
230   /// around, that it doesn't create invalid references by failing to relocate
231   /// CU relative and absolute references.
232   ///
233   /// \returns NumErrors The number of errors occurred during verification of
234   /// references for the .debug_info and .debug_types sections
235   unsigned verifyDebugInfoReferences(
236       const ReferenceMap &,
237       llvm::function_ref<DWARFUnit *(uint64_t)> GetUnitForDieOffset);
238 
239   /// Verify the DW_AT_stmt_list encoding and value and ensure that no
240   /// compile units that have the same DW_AT_stmt_list value.
241   void verifyDebugLineStmtOffsets();
242 
243   /// Verify that all of the rows in the line table are valid.
244   ///
245   /// This function currently checks for:
246   /// - addresses within a sequence that decrease in value
247   /// - invalid file indexes
248   void verifyDebugLineRows();
249 
250   /// Verify that an Apple-style accelerator table is valid.
251   ///
252   /// This function currently checks that:
253   /// - The fixed part of the header fits in the section
254   /// - The size of the section is as large as what the header describes
255   /// - There is at least one atom
256   /// - The form for each atom is valid
257   /// - The tag for each DIE in the table is valid
258   /// - The buckets have a valid index, or they are empty
259   /// - Each hashdata offset is valid
260   /// - Each DIE is valid
261   ///
262   /// \param AccelSection pointer to the section containing the acceleration table
263   /// \param StrData pointer to the string section
264   /// \param SectionName the name of the table we're verifying
265   ///
266   /// \returns The number of errors occurred during verification
267   unsigned verifyAppleAccelTable(const DWARFSection *AccelSection,
268                                  DataExtractor *StrData,
269                                  const char *SectionName);
270 
271   unsigned verifyDebugNamesCULists(const DWARFDebugNames &AccelTable);
272   unsigned verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
273                                   const DataExtractor &StrData);
274   unsigned verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI);
275   unsigned verifyNameIndexAttribute(const DWARFDebugNames::NameIndex &NI,
276                                     const DWARFDebugNames::Abbrev &Abbr,
277                                     DWARFDebugNames::AttributeEncoding AttrEnc);
278   unsigned verifyNameIndexEntries(const DWARFDebugNames::NameIndex &NI,
279                                   const DWARFDebugNames::NameTableEntry &NTE);
280   unsigned verifyNameIndexCompleteness(const DWARFDie &Die,
281                                        const DWARFDebugNames::NameIndex &NI);
282 
283   /// Verify that the DWARF v5 accelerator table is valid.
284   ///
285   /// This function currently checks that:
286   /// - Headers individual Name Indices fit into the section and can be parsed.
287   /// - Abbreviation tables can be parsed and contain valid index attributes
288   ///   with correct form encodings.
289   /// - The CU lists reference existing compile units.
290   /// - The buckets have a valid index, or they are empty.
291   /// - All names are reachable via the hash table (they have the correct hash,
292   ///   and the hash is in the correct bucket).
293   /// - Information in the index entries is complete (all required entries are
294   ///   present) and consistent with the debug_info section DIEs.
295   ///
296   /// \param AccelSection section containing the acceleration table
297   /// \param StrData string section
298   ///
299   /// \returns The number of errors occurred during verification
300   unsigned verifyDebugNames(const DWARFSection &AccelSection,
301                             const DataExtractor &StrData);
302 
303 public:
304   DWARFVerifier(raw_ostream &S, DWARFContext &D,
305                 DIDumpOptions DumpOpts = DIDumpOptions::getForSingleDIE());
306 
307   /// Verify the information in any of the following sections, if available:
308   /// .debug_abbrev, debug_abbrev.dwo
309   ///
310   /// Any errors are reported to the stream that was this object was
311   /// constructed with.
312   ///
313   /// \returns true if .debug_abbrev and .debug_abbrev.dwo verify successfully,
314   /// false otherwise.
315   bool handleDebugAbbrev();
316 
317   /// Verify the information in the .debug_info and .debug_types sections.
318   ///
319   /// Any errors are reported to the stream that this object was
320   /// constructed with.
321   ///
322   /// \returns true if all sections verify successfully, false otherwise.
323   bool handleDebugInfo();
324 
325   /// Verify the information in the .debug_cu_index section.
326   ///
327   /// Any errors are reported to the stream that was this object was
328   /// constructed with.
329   ///
330   /// \returns true if the .debug_cu_index verifies successfully, false
331   /// otherwise.
332   bool handleDebugCUIndex();
333 
334   /// Verify the information in the .debug_tu_index section.
335   ///
336   /// Any errors are reported to the stream that was this object was
337   /// constructed with.
338   ///
339   /// \returns true if the .debug_tu_index verifies successfully, false
340   /// otherwise.
341   bool handleDebugTUIndex();
342 
343   /// Verify the information in the .debug_line section.
344   ///
345   /// Any errors are reported to the stream that was this object was
346   /// constructed with.
347   ///
348   /// \returns true if the .debug_line verifies successfully, false otherwise.
349   bool handleDebugLine();
350 
351   /// Verify the information in accelerator tables, if they exist.
352   ///
353   /// Any errors are reported to the stream that was this object was
354   /// constructed with.
355   ///
356   /// \returns true if the existing Apple-style accelerator tables verify
357   /// successfully, false otherwise.
358   bool handleAccelTables();
359 
360   /// Verify the information in the .debug_str_offsets[.dwo].
361   ///
362   /// Any errors are reported to the stream that was this object was
363   /// constructed with.
364   ///
365   /// \returns true if the .debug_line verifies successfully, false otherwise.
366   bool handleDebugStrOffsets();
367   bool verifyDebugStrOffsets(std::optional<dwarf::DwarfFormat> LegacyFormat,
368                              StringRef SectionName, const DWARFSection &Section,
369                              StringRef StrData);
370 
371   /// Emits any aggregate information collected, depending on the dump options
372   void summarize();
373 };
374 
375 static inline bool operator<(const DWARFVerifier::DieRangeInfo &LHS,
376                              const DWARFVerifier::DieRangeInfo &RHS) {
377   return std::tie(LHS.Ranges, LHS.Die) < std::tie(RHS.Ranges, RHS.Die);
378 }
379 
380 } // end namespace llvm
381 
382 #endif // LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
383