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" 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 #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. 35 class DWARFVerifier { 36 public: 37 /// A class that keeps the address range information for a single DIE. 38 struct DieRangeInfo { 39 DWARFDie Die; 40 41 /// Sorted DWARFAddressRanges. 42 std::vector<DWARFAddressRange> Ranges; 43 44 /// Sorted DWARFAddressRangeInfo. 45 std::set<DieRangeInfo> Children; 46 47 DieRangeInfo() = default; 48 DieRangeInfo(DWARFDie Die) : Die(Die) {} 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. 63 Optional<DWARFAddressRange> insert(const DWARFAddressRange &R); 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. 70 die_range_info_iterator insert(const DieRangeInfo &RI); 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 ¬e() 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, 260 DWARFDebugNames::AttributeEncoding AttrEnc); 261 unsigned verifyNameIndexEntries(const DWARFDebugNames::NameIndex &NI, 262 const DWARFDebugNames::NameTableEntry &NTE); 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: 287 DWARFVerifier(raw_ostream &S, DWARFContext &D, 288 DIDumpOptions DumpOpts = DIDumpOptions::getForSingleDIE()); 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, 345 const DWARFVerifier::DieRangeInfo &RHS) { 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 352