|
|
|||
File indexing completed on 2026-05-10 08:43:42
0001 //===- DWARFVerifier.h ----------------------------------------------------===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H 0010 #define LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H 0011 0012 #include "llvm/DebugInfo/DIContext.h" 0013 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 0014 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h" 0015 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 0016 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" 0017 #include <cstdint> 0018 #include <map> 0019 #include <set> 0020 0021 namespace llvm { 0022 class raw_ostream; 0023 struct DWARFAddressRange; 0024 class DWARFUnit; 0025 class DWARFUnitVector; 0026 struct DWARFAttribute; 0027 class DWARFContext; 0028 class DWARFDataExtractor; 0029 class DWARFDebugAbbrev; 0030 class DataExtractor; 0031 struct DWARFSection; 0032 0033 class OutputCategoryAggregator { 0034 private: 0035 std::map<std::string, unsigned> Aggregation; 0036 bool IncludeDetail; 0037 0038 public: 0039 OutputCategoryAggregator(bool includeDetail = false) 0040 : IncludeDetail(includeDetail) {} 0041 void ShowDetail(bool showDetail) { IncludeDetail = showDetail; } 0042 size_t GetNumCategories() const { return Aggregation.size(); } 0043 void Report(StringRef s, std::function<void()> detailCallback); 0044 void EnumerateResults(std::function<void(StringRef, unsigned)> handleCounts); 0045 }; 0046 0047 /// A class that verifies DWARF debug information given a DWARF Context. 0048 class DWARFVerifier { 0049 public: 0050 /// A class that keeps the address range information for a single DIE. 0051 struct DieRangeInfo { 0052 DWARFDie Die; 0053 0054 /// Sorted DWARFAddressRanges. 0055 std::vector<DWARFAddressRange> Ranges; 0056 0057 /// Sorted DWARFAddressRangeInfo. 0058 std::set<DieRangeInfo> Children; 0059 0060 DieRangeInfo() = default; 0061 DieRangeInfo(DWARFDie Die) : Die(Die) {} 0062 0063 /// Used for unit testing. 0064 DieRangeInfo(std::vector<DWARFAddressRange> Ranges) 0065 : Ranges(std::move(Ranges)) {} 0066 0067 typedef std::set<DieRangeInfo>::const_iterator die_range_info_iterator; 0068 0069 /// Inserts the address range. If the range overlaps with an existing 0070 /// range, the range that it overlaps with will be returned and the two 0071 /// address ranges will be unioned together in "Ranges". If a duplicate 0072 /// entry is attempted to be added, the duplicate range will not actually be 0073 /// added and the returned iterator will point to end(). 0074 /// 0075 /// This is used for finding overlapping ranges in the DW_AT_ranges 0076 /// attribute of a DIE. It is also used as a set of address ranges that 0077 /// children address ranges must all be contained in. 0078 std::optional<DWARFAddressRange> insert(const DWARFAddressRange &R); 0079 0080 /// Inserts the address range info. If any of its ranges overlaps with a 0081 /// range in an existing range info, the range info is *not* added and an 0082 /// iterator to the overlapping range info. If a duplicate entry is 0083 /// attempted to be added, the duplicate range will not actually be added 0084 /// and the returned iterator will point to end(). 0085 /// 0086 /// This is used for finding overlapping children of the same DIE. 0087 die_range_info_iterator insert(const DieRangeInfo &RI); 0088 0089 /// Return true if ranges in this object contains all ranges within RHS. 0090 bool contains(const DieRangeInfo &RHS) const; 0091 0092 /// Return true if any range in this object intersects with any range in 0093 /// RHS. Identical ranges are not considered to be intersecting. 0094 bool intersects(const DieRangeInfo &RHS) const; 0095 }; 0096 0097 private: 0098 raw_ostream &OS; 0099 DWARFContext &DCtx; 0100 DIDumpOptions DumpOpts; 0101 uint32_t NumDebugLineErrors = 0; 0102 OutputCategoryAggregator ErrorCategory; 0103 // Used to relax some checks that do not currently work portably 0104 bool IsObjectFile; 0105 bool IsMachOObject; 0106 using ReferenceMap = std::map<uint64_t, std::set<uint64_t>>; 0107 0108 raw_ostream &error() const; 0109 raw_ostream &warn() const; 0110 raw_ostream ¬e() const; 0111 raw_ostream &dump(const DWARFDie &Die, unsigned indent = 0) const; 0112 0113 /// Verifies the abbreviations section. 0114 /// 0115 /// This function currently checks that: 0116 /// --No abbreviation declaration has more than one attributes with the same 0117 /// name. 0118 /// 0119 /// \param Abbrev Pointer to the abbreviations section we are verifying 0120 /// Abbrev can be a pointer to either .debug_abbrev or debug_abbrev.dwo. 0121 /// 0122 /// \returns The number of errors that occurred during verification. 0123 unsigned verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev); 0124 0125 /// Verifies the header of a unit in a .debug_info or .debug_types section. 0126 /// 0127 /// This function currently checks for: 0128 /// - Unit is in 32-bit DWARF format. The function can be modified to 0129 /// support 64-bit format. 0130 /// - The DWARF version is valid 0131 /// - The unit type is valid (if unit is in version >=5) 0132 /// - The unit doesn't extend beyond the containing section 0133 /// - The address size is valid 0134 /// - The offset in the .debug_abbrev section is valid 0135 /// 0136 /// \param DebugInfoData The section data 0137 /// \param Offset A reference to the offset start of the unit. The offset will 0138 /// be updated to point to the next unit in the section 0139 /// \param UnitIndex The index of the unit to be verified 0140 /// \param UnitType A reference to the type of the unit 0141 /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is 0142 /// in 64-bit format. 0143 /// 0144 /// \returns true if the header is verified successfully, false otherwise. 0145 bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData, 0146 uint64_t *Offset, unsigned UnitIndex, uint8_t &UnitType, 0147 bool &isUnitDWARF64); 0148 bool verifyName(const DWARFDie &Die); 0149 0150 /// Verifies the header of a unit in a .debug_info or .debug_types section. 0151 /// 0152 /// This function currently verifies: 0153 /// - The debug info attributes. 0154 /// - The debug info form=s. 0155 /// - The presence of a root DIE. 0156 /// - That the root DIE is a unit DIE. 0157 /// - If a unit type is provided, that the unit DIE matches the unit type. 0158 /// - The DIE ranges. 0159 /// - That call site entries are only nested within subprograms with a 0160 /// DW_AT_call attribute. 0161 /// 0162 /// \param Unit The DWARF Unit to verify. 0163 /// 0164 /// \returns The number of errors that occurred during verification. 0165 unsigned verifyUnitContents(DWARFUnit &Unit, 0166 ReferenceMap &UnitLocalReferences, 0167 ReferenceMap &CrossUnitReferences); 0168 0169 /// Verifies the unit headers and contents in a .debug_info or .debug_types 0170 /// section. 0171 /// 0172 /// \param S The DWARF Section to verify. 0173 /// 0174 /// \returns The number of errors that occurred during verification. 0175 unsigned verifyUnitSection(const DWARFSection &S); 0176 unsigned verifyUnits(const DWARFUnitVector &Units); 0177 0178 unsigned verifyIndex(StringRef Name, DWARFSectionKind SectionKind, 0179 StringRef Index); 0180 0181 /// Verifies that a call site entry is nested within a subprogram with a 0182 /// DW_AT_call attribute. 0183 /// 0184 /// \returns Number of errors that occurred during verification. 0185 unsigned verifyDebugInfoCallSite(const DWARFDie &Die); 0186 0187 /// Verify that all Die ranges are valid. 0188 /// 0189 /// This function currently checks for: 0190 /// - cases in which lowPC >= highPC 0191 /// 0192 /// \returns Number of errors that occurred during verification. 0193 unsigned verifyDieRanges(const DWARFDie &Die, DieRangeInfo &ParentRI); 0194 0195 /// Verifies the attribute's DWARF attribute and its value. 0196 /// 0197 /// This function currently checks for: 0198 /// - DW_AT_ranges values is a valid .debug_ranges offset 0199 /// - DW_AT_stmt_list is a valid .debug_line offset 0200 /// 0201 /// \param Die The DWARF DIE that owns the attribute value 0202 /// \param AttrValue The DWARF attribute value to check 0203 /// 0204 /// \returns NumErrors The number of errors occurred during verification of 0205 /// attributes' values in a unit 0206 unsigned verifyDebugInfoAttribute(const DWARFDie &Die, 0207 DWARFAttribute &AttrValue); 0208 0209 /// Verifies the attribute's DWARF form. 0210 /// 0211 /// This function currently checks for: 0212 /// - All DW_FORM_ref values that are CU relative have valid CU offsets 0213 /// - All DW_FORM_ref_addr values have valid section offsets 0214 /// - All DW_FORM_strp values have valid .debug_str offsets 0215 /// 0216 /// \param Die The DWARF DIE that owns the attribute value 0217 /// \param AttrValue The DWARF attribute value to check 0218 /// 0219 /// \returns NumErrors The number of errors occurred during verification of 0220 /// attributes' forms in a unit 0221 unsigned verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue, 0222 ReferenceMap &UnitLocalReferences, 0223 ReferenceMap &CrossUnitReferences); 0224 0225 /// Verifies the all valid references that were found when iterating through 0226 /// all of the DIE attributes. 0227 /// 0228 /// This function will verify that all references point to DIEs whose DIE 0229 /// offset matches. This helps to ensure if a DWARF link phase moved things 0230 /// around, that it doesn't create invalid references by failing to relocate 0231 /// CU relative and absolute references. 0232 /// 0233 /// \returns NumErrors The number of errors occurred during verification of 0234 /// references for the .debug_info and .debug_types sections 0235 unsigned verifyDebugInfoReferences( 0236 const ReferenceMap &, 0237 llvm::function_ref<DWARFUnit *(uint64_t)> GetUnitForDieOffset); 0238 0239 /// Verify the DW_AT_stmt_list encoding and value and ensure that no 0240 /// compile units that have the same DW_AT_stmt_list value. 0241 void verifyDebugLineStmtOffsets(); 0242 0243 /// Verify that all of the rows in the line table are valid. 0244 /// 0245 /// This function currently checks for: 0246 /// - addresses within a sequence that decrease in value 0247 /// - invalid file indexes 0248 void verifyDebugLineRows(); 0249 0250 /// Verify that an Apple-style accelerator table is valid. 0251 /// 0252 /// This function currently checks that: 0253 /// - The fixed part of the header fits in the section 0254 /// - The size of the section is as large as what the header describes 0255 /// - There is at least one atom 0256 /// - The form for each atom is valid 0257 /// - The tag for each DIE in the table is valid 0258 /// - The buckets have a valid index, or they are empty 0259 /// - Each hashdata offset is valid 0260 /// - Each DIE is valid 0261 /// 0262 /// \param AccelSection pointer to the section containing the acceleration table 0263 /// \param StrData pointer to the string section 0264 /// \param SectionName the name of the table we're verifying 0265 /// 0266 /// \returns The number of errors occurred during verification 0267 unsigned verifyAppleAccelTable(const DWARFSection *AccelSection, 0268 DataExtractor *StrData, 0269 const char *SectionName); 0270 0271 unsigned verifyDebugNamesCULists(const DWARFDebugNames &AccelTable); 0272 unsigned verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI, 0273 const DataExtractor &StrData); 0274 unsigned verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI); 0275 unsigned verifyNameIndexAttribute(const DWARFDebugNames::NameIndex &NI, 0276 const DWARFDebugNames::Abbrev &Abbr, 0277 DWARFDebugNames::AttributeEncoding AttrEnc); 0278 unsigned verifyNameIndexEntries(const DWARFDebugNames::NameIndex &NI, 0279 const DWARFDebugNames::NameTableEntry &NTE); 0280 unsigned verifyNameIndexCompleteness(const DWARFDie &Die, 0281 const DWARFDebugNames::NameIndex &NI); 0282 0283 /// Verify that the DWARF v5 accelerator table is valid. 0284 /// 0285 /// This function currently checks that: 0286 /// - Headers individual Name Indices fit into the section and can be parsed. 0287 /// - Abbreviation tables can be parsed and contain valid index attributes 0288 /// with correct form encodings. 0289 /// - The CU lists reference existing compile units. 0290 /// - The buckets have a valid index, or they are empty. 0291 /// - All names are reachable via the hash table (they have the correct hash, 0292 /// and the hash is in the correct bucket). 0293 /// - Information in the index entries is complete (all required entries are 0294 /// present) and consistent with the debug_info section DIEs. 0295 /// 0296 /// \param AccelSection section containing the acceleration table 0297 /// \param StrData string section 0298 /// 0299 /// \returns The number of errors occurred during verification 0300 unsigned verifyDebugNames(const DWARFSection &AccelSection, 0301 const DataExtractor &StrData); 0302 0303 public: 0304 DWARFVerifier(raw_ostream &S, DWARFContext &D, 0305 DIDumpOptions DumpOpts = DIDumpOptions::getForSingleDIE()); 0306 0307 /// Verify the information in any of the following sections, if available: 0308 /// .debug_abbrev, debug_abbrev.dwo 0309 /// 0310 /// Any errors are reported to the stream that was this object was 0311 /// constructed with. 0312 /// 0313 /// \returns true if .debug_abbrev and .debug_abbrev.dwo verify successfully, 0314 /// false otherwise. 0315 bool handleDebugAbbrev(); 0316 0317 /// Verify the information in the .debug_info and .debug_types sections. 0318 /// 0319 /// Any errors are reported to the stream that this object was 0320 /// constructed with. 0321 /// 0322 /// \returns true if all sections verify successfully, false otherwise. 0323 bool handleDebugInfo(); 0324 0325 /// Verify the information in the .debug_cu_index section. 0326 /// 0327 /// Any errors are reported to the stream that was this object was 0328 /// constructed with. 0329 /// 0330 /// \returns true if the .debug_cu_index verifies successfully, false 0331 /// otherwise. 0332 bool handleDebugCUIndex(); 0333 0334 /// Verify the information in the .debug_tu_index section. 0335 /// 0336 /// Any errors are reported to the stream that was this object was 0337 /// constructed with. 0338 /// 0339 /// \returns true if the .debug_tu_index verifies successfully, false 0340 /// otherwise. 0341 bool handleDebugTUIndex(); 0342 0343 /// Verify the information in the .debug_line section. 0344 /// 0345 /// Any errors are reported to the stream that was this object was 0346 /// constructed with. 0347 /// 0348 /// \returns true if the .debug_line verifies successfully, false otherwise. 0349 bool handleDebugLine(); 0350 0351 /// Verify the information in accelerator tables, if they exist. 0352 /// 0353 /// Any errors are reported to the stream that was this object was 0354 /// constructed with. 0355 /// 0356 /// \returns true if the existing Apple-style accelerator tables verify 0357 /// successfully, false otherwise. 0358 bool handleAccelTables(); 0359 0360 /// Verify the information in the .debug_str_offsets[.dwo]. 0361 /// 0362 /// Any errors are reported to the stream that was this object was 0363 /// constructed with. 0364 /// 0365 /// \returns true if the .debug_line verifies successfully, false otherwise. 0366 bool handleDebugStrOffsets(); 0367 bool verifyDebugStrOffsets(std::optional<dwarf::DwarfFormat> LegacyFormat, 0368 StringRef SectionName, const DWARFSection &Section, 0369 StringRef StrData); 0370 0371 /// Emits any aggregate information collected, depending on the dump options 0372 void summarize(); 0373 }; 0374 0375 static inline bool operator<(const DWARFVerifier::DieRangeInfo &LHS, 0376 const DWARFVerifier::DieRangeInfo &RHS) { 0377 return std::tie(LHS.Ranges, LHS.Die) < std::tie(RHS.Ranges, RHS.Die); 0378 } 0379 0380 } // end namespace llvm 0381 0382 #endif // LLVM_DEBUGINFO_DWARF_DWARFVERIFIER_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|