Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 1 | //===- DWARFVerifier.cpp --------------------------------------------------===// |
| 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 | #include "llvm/DebugInfo/DWARF/DWARFVerifier.h" |
| 11 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
| 12 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 13 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
| 14 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
| 15 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFSection.h" |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | #include <map> |
| 20 | #include <set> |
| 21 | #include <vector> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace dwarf; |
| 25 | using namespace object; |
| 26 | |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 27 | bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData, |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 28 | uint32_t *Offset, unsigned UnitIndex, |
| 29 | uint8_t &UnitType, bool &isUnitDWARF64) { |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 30 | uint32_t AbbrOffset, Length; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 31 | uint8_t AddrSize = 0; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 32 | uint16_t Version; |
| 33 | bool Success = true; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 34 | |
| 35 | bool ValidLength = false; |
| 36 | bool ValidVersion = false; |
| 37 | bool ValidAddrSize = false; |
| 38 | bool ValidType = true; |
| 39 | bool ValidAbbrevOffset = true; |
| 40 | |
| 41 | uint32_t OffsetStart = *Offset; |
| 42 | Length = DebugInfoData.getU32(Offset); |
| 43 | if (Length == UINT32_MAX) { |
| 44 | isUnitDWARF64 = true; |
| 45 | OS << format( |
| 46 | "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n", |
| 47 | UnitIndex); |
| 48 | return false; |
| 49 | } |
| 50 | Version = DebugInfoData.getU16(Offset); |
| 51 | |
| 52 | if (Version >= 5) { |
| 53 | UnitType = DebugInfoData.getU8(Offset); |
| 54 | AddrSize = DebugInfoData.getU8(Offset); |
| 55 | AbbrOffset = DebugInfoData.getU32(Offset); |
| 56 | ValidType = DWARFUnit::isValidUnitType(UnitType); |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 57 | } else { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 58 | UnitType = 0; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 59 | AbbrOffset = DebugInfoData.getU32(Offset); |
| 60 | AddrSize = DebugInfoData.getU8(Offset); |
| 61 | } |
| 62 | |
| 63 | if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset)) |
| 64 | ValidAbbrevOffset = false; |
| 65 | |
| 66 | ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3); |
| 67 | ValidVersion = DWARFContext::isSupportedVersion(Version); |
| 68 | ValidAddrSize = AddrSize == 4 || AddrSize == 8; |
| 69 | if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset || |
| 70 | !ValidType) { |
| 71 | Success = false; |
| 72 | OS << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart); |
| 73 | if (!ValidLength) |
| 74 | OS << "\tError: The length for this unit is too " |
| 75 | "large for the .debug_info provided.\n"; |
| 76 | if (!ValidVersion) |
| 77 | OS << "\tError: The 16 bit unit header version is not valid.\n"; |
| 78 | if (!ValidType) |
| 79 | OS << "\tError: The unit type encoding is not valid.\n"; |
| 80 | if (!ValidAbbrevOffset) |
| 81 | OS << "\tError: The offset into the .debug_abbrev section is " |
| 82 | "not valid.\n"; |
| 83 | if (!ValidAddrSize) |
| 84 | OS << "\tError: The address size is unsupported.\n"; |
| 85 | } |
| 86 | *Offset = OffsetStart + Length + 4; |
| 87 | return Success; |
| 88 | } |
| 89 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 90 | bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) { |
| 91 | uint32_t NumUnitErrors = 0; |
| 92 | unsigned NumDies = Unit.getNumDIEs(); |
| 93 | for (unsigned I = 0; I < NumDies; ++I) { |
| 94 | auto Die = Unit.getDIEAtIndex(I); |
| 95 | if (Die.getTag() == DW_TAG_null) |
| 96 | continue; |
Spyridoula Gravani | e0ba415 | 2017-07-24 21:04:11 +0000 | [diff] [blame^] | 97 | NumUnitErrors += verifyDieRanges(Die); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 98 | for (auto AttrValue : Die.attributes()) { |
| 99 | NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue); |
| 100 | NumUnitErrors += verifyDebugInfoForm(Die, AttrValue); |
| 101 | } |
| 102 | } |
| 103 | return NumUnitErrors == 0; |
| 104 | } |
| 105 | |
Spyridoula Gravani | c6ef987 | 2017-07-21 00:51:32 +0000 | [diff] [blame] | 106 | unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) { |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 107 | unsigned NumErrors = 0; |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 108 | if (Abbrev) { |
| 109 | const DWARFAbbreviationDeclarationSet *AbbrDecls = |
| 110 | Abbrev->getAbbreviationDeclarationSet(0); |
| 111 | for (auto AbbrDecl : *AbbrDecls) { |
| 112 | SmallDenseSet<uint16_t> AttributeSet; |
| 113 | for (auto Attribute : AbbrDecl.attributes()) { |
| 114 | auto Result = AttributeSet.insert(Attribute.Attr); |
| 115 | if (!Result.second) { |
Spyridoula Gravani | c6ef987 | 2017-07-21 00:51:32 +0000 | [diff] [blame] | 116 | OS << "Error: Abbreviation declaration contains multiple " |
| 117 | << AttributeString(Attribute.Attr) << " attributes.\n"; |
| 118 | AbbrDecl.dump(OS); |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 119 | ++NumErrors; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
Spyridoula Gravani | c6ef987 | 2017-07-21 00:51:32 +0000 | [diff] [blame] | 124 | return NumErrors; |
| 125 | } |
| 126 | |
| 127 | bool DWARFVerifier::handleDebugAbbrev() { |
| 128 | OS << "Verifying .debug_abbrev...\n"; |
| 129 | |
| 130 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
| 131 | bool noDebugAbbrev = DObj.getAbbrevSection().empty(); |
| 132 | bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty(); |
| 133 | |
| 134 | if (noDebugAbbrev && noDebugAbbrevDWO) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | unsigned NumErrors = 0; |
| 139 | if (!noDebugAbbrev) |
| 140 | NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev()); |
| 141 | |
| 142 | if (!noDebugAbbrevDWO) |
| 143 | NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO()); |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 144 | return NumErrors == 0; |
| 145 | } |
| 146 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 147 | bool DWARFVerifier::handleDebugInfo() { |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 148 | OS << "Verifying .debug_info Unit Header Chain...\n"; |
| 149 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 150 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
| 151 | DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(), |
| 152 | DCtx.isLittleEndian(), 0); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 153 | uint32_t NumDebugInfoErrors = 0; |
| 154 | uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0; |
| 155 | uint8_t UnitType = 0; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 156 | bool isUnitDWARF64 = false; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 157 | bool isHeaderChainValid = true; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 158 | bool hasDIE = DebugInfoData.isValidOffset(Offset); |
| 159 | while (hasDIE) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 160 | OffsetStart = Offset; |
| 161 | if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType, |
| 162 | isUnitDWARF64)) { |
| 163 | isHeaderChainValid = false; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 164 | if (isUnitDWARF64) |
| 165 | break; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 166 | } else { |
| 167 | std::unique_ptr<DWARFUnit> Unit; |
| 168 | switch (UnitType) { |
| 169 | case dwarf::DW_UT_type: |
| 170 | case dwarf::DW_UT_split_type: { |
| 171 | DWARFUnitSection<DWARFTypeUnit> TUSection{}; |
| 172 | Unit.reset(new DWARFTypeUnit( |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 173 | DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(), |
| 174 | &DObj.getRangeSection(), DObj.getStringSection(), |
| 175 | DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(), |
| 176 | DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection, |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 177 | nullptr)); |
| 178 | break; |
| 179 | } |
| 180 | case dwarf::DW_UT_skeleton: |
| 181 | case dwarf::DW_UT_split_compile: |
| 182 | case dwarf::DW_UT_compile: |
| 183 | case dwarf::DW_UT_partial: |
| 184 | // UnitType = 0 means that we are |
| 185 | // verifying a compile unit in DWARF v4. |
| 186 | case 0: { |
| 187 | DWARFUnitSection<DWARFCompileUnit> CUSection{}; |
| 188 | Unit.reset(new DWARFCompileUnit( |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 189 | DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(), |
| 190 | &DObj.getRangeSection(), DObj.getStringSection(), |
| 191 | DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(), |
| 192 | DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection, |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 193 | nullptr)); |
| 194 | break; |
| 195 | } |
| 196 | default: { llvm_unreachable("Invalid UnitType."); } |
| 197 | } |
| 198 | Unit->extract(DebugInfoData, &OffsetStart); |
| 199 | if (!verifyUnitContents(*Unit)) |
| 200 | ++NumDebugInfoErrors; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 201 | } |
| 202 | hasDIE = DebugInfoData.isValidOffset(Offset); |
| 203 | ++UnitIdx; |
| 204 | } |
| 205 | if (UnitIdx == 0 && !hasDIE) { |
| 206 | OS << "Warning: .debug_info is empty.\n"; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 207 | isHeaderChainValid = true; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 208 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 209 | NumDebugInfoErrors += verifyDebugInfoReferences(); |
| 210 | return (isHeaderChainValid && NumDebugInfoErrors == 0); |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Spyridoula Gravani | e0ba415 | 2017-07-24 21:04:11 +0000 | [diff] [blame^] | 213 | unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die) { |
| 214 | unsigned NumErrors = 0; |
| 215 | for (auto Range : Die.getAddressRanges()) { |
| 216 | if (Range.LowPC >= Range.HighPC) { |
| 217 | ++NumErrors; |
| 218 | OS << format("error: Invalid address range [0x%08x - 0x%08x].\n", |
| 219 | Range.LowPC, Range.HighPC); |
| 220 | } |
| 221 | } |
| 222 | return NumErrors; |
| 223 | } |
| 224 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 225 | unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die, |
| 226 | DWARFAttribute &AttrValue) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 227 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 228 | unsigned NumErrors = 0; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 229 | const auto Attr = AttrValue.Attr; |
| 230 | switch (Attr) { |
| 231 | case DW_AT_ranges: |
| 232 | // Make sure the offset in the DW_AT_ranges attribute is valid. |
| 233 | if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 234 | if (*SectionOffset >= DObj.getRangeSection().Data.size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 235 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 236 | OS << "error: DW_AT_ranges offset is beyond .debug_ranges " |
| 237 | "bounds:\n"; |
| 238 | Die.dump(OS, 0); |
| 239 | OS << "\n"; |
| 240 | } |
| 241 | } else { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 242 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 243 | OS << "error: DIE has invalid DW_AT_ranges encoding:\n"; |
| 244 | Die.dump(OS, 0); |
| 245 | OS << "\n"; |
| 246 | } |
| 247 | break; |
| 248 | case DW_AT_stmt_list: |
| 249 | // Make sure the offset in the DW_AT_stmt_list attribute is valid. |
| 250 | if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 251 | if (*SectionOffset >= DObj.getLineSection().Data.size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 252 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 253 | OS << "error: DW_AT_stmt_list offset is beyond .debug_line " |
| 254 | "bounds: " |
| 255 | << format("0x%08" PRIx32, *SectionOffset) << "\n"; |
| 256 | Die.dump(OS, 0); |
| 257 | OS << "\n"; |
| 258 | } |
| 259 | } else { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 260 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 261 | OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n"; |
| 262 | Die.dump(OS, 0); |
| 263 | OS << "\n"; |
| 264 | } |
| 265 | break; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 266 | |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 267 | default: |
| 268 | break; |
| 269 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 270 | return NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 271 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 272 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 273 | unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die, |
| 274 | DWARFAttribute &AttrValue) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 275 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 276 | unsigned NumErrors = 0; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 277 | const auto Form = AttrValue.Value.getForm(); |
| 278 | switch (Form) { |
| 279 | case DW_FORM_ref1: |
| 280 | case DW_FORM_ref2: |
| 281 | case DW_FORM_ref4: |
| 282 | case DW_FORM_ref8: |
| 283 | case DW_FORM_ref_udata: { |
| 284 | // Verify all CU relative references are valid CU offsets. |
| 285 | Optional<uint64_t> RefVal = AttrValue.Value.getAsReference(); |
| 286 | assert(RefVal); |
| 287 | if (RefVal) { |
| 288 | auto DieCU = Die.getDwarfUnit(); |
| 289 | auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset(); |
| 290 | auto CUOffset = AttrValue.Value.getRawUValue(); |
| 291 | if (CUOffset >= CUSize) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 292 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 293 | OS << "error: " << FormEncodingString(Form) << " CU offset " |
| 294 | << format("0x%08" PRIx32, CUOffset) |
| 295 | << " is invalid (must be less than CU size of " |
| 296 | << format("0x%08" PRIx32, CUSize) << "):\n"; |
| 297 | Die.dump(OS, 0); |
| 298 | OS << "\n"; |
| 299 | } else { |
| 300 | // Valid reference, but we will verify it points to an actual |
| 301 | // DIE later. |
| 302 | ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset()); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 305 | break; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 306 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 307 | case DW_FORM_ref_addr: { |
| 308 | // Verify all absolute DIE references have valid offsets in the |
| 309 | // .debug_info section. |
| 310 | Optional<uint64_t> RefVal = AttrValue.Value.getAsReference(); |
| 311 | assert(RefVal); |
| 312 | if (RefVal) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 313 | if (*RefVal >= DObj.getInfoSection().Data.size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 314 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 315 | OS << "error: DW_FORM_ref_addr offset beyond .debug_info " |
| 316 | "bounds:\n"; |
| 317 | Die.dump(OS, 0); |
| 318 | OS << "\n"; |
| 319 | } else { |
| 320 | // Valid reference, but we will verify it points to an actual |
| 321 | // DIE later. |
| 322 | ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset()); |
| 323 | } |
| 324 | } |
| 325 | break; |
| 326 | } |
| 327 | case DW_FORM_strp: { |
| 328 | auto SecOffset = AttrValue.Value.getAsSectionOffset(); |
| 329 | assert(SecOffset); // DW_FORM_strp is a section offset. |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 330 | if (SecOffset && *SecOffset >= DObj.getStringSection().size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 331 | ++NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 332 | OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n"; |
| 333 | Die.dump(OS, 0); |
| 334 | OS << "\n"; |
| 335 | } |
| 336 | break; |
| 337 | } |
| 338 | default: |
| 339 | break; |
| 340 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 341 | return NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 342 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 343 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 344 | unsigned DWARFVerifier::verifyDebugInfoReferences() { |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 345 | // Take all references and make sure they point to an actual DIE by |
| 346 | // getting the DIE by offset and emitting an error |
| 347 | OS << "Verifying .debug_info references...\n"; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 348 | unsigned NumErrors = 0; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 349 | for (auto Pair : ReferenceToDIEOffsets) { |
| 350 | auto Die = DCtx.getDIEForOffset(Pair.first); |
| 351 | if (Die) |
| 352 | continue; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 353 | ++NumErrors; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 354 | OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first) |
| 355 | << ". Offset is in between DIEs:\n"; |
| 356 | for (auto Offset : Pair.second) { |
| 357 | auto ReferencingDie = DCtx.getDIEForOffset(Offset); |
| 358 | ReferencingDie.dump(OS, 0); |
| 359 | OS << "\n"; |
| 360 | } |
| 361 | OS << "\n"; |
| 362 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 363 | return NumErrors; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 366 | void DWARFVerifier::verifyDebugLineStmtOffsets() { |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 367 | std::map<uint64_t, DWARFDie> StmtListToDie; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 368 | for (const auto &CU : DCtx.compile_units()) { |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 369 | auto Die = CU->getUnitDIE(); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 370 | // Get the attribute value as a section offset. No need to produce an |
| 371 | // error here if the encoding isn't correct because we validate this in |
| 372 | // the .debug_info verifier. |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 373 | auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list)); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 374 | if (!StmtSectionOffset) |
| 375 | continue; |
| 376 | const uint32_t LineTableOffset = *StmtSectionOffset; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 377 | auto LineTable = DCtx.getLineTableForUnit(CU.get()); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 378 | if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) { |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 379 | if (!LineTable) { |
| 380 | ++NumDebugLineErrors; |
| 381 | OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset) |
| 382 | << "] was not able to be parsed for CU:\n"; |
| 383 | Die.dump(OS, 0); |
| 384 | OS << '\n'; |
| 385 | continue; |
| 386 | } |
| 387 | } else { |
| 388 | // Make sure we don't get a valid line table back if the offset is wrong. |
| 389 | assert(LineTable == nullptr); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 390 | // Skip this line table as it isn't valid. No need to create an error |
| 391 | // here because we validate this in the .debug_info verifier. |
| 392 | continue; |
| 393 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 394 | auto Iter = StmtListToDie.find(LineTableOffset); |
| 395 | if (Iter != StmtListToDie.end()) { |
| 396 | ++NumDebugLineErrors; |
| 397 | OS << "error: two compile unit DIEs, " |
| 398 | << format("0x%08" PRIx32, Iter->second.getOffset()) << " and " |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 399 | << format("0x%08" PRIx32, Die.getOffset()) |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 400 | << ", have the same DW_AT_stmt_list section offset:\n"; |
| 401 | Iter->second.dump(OS, 0); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 402 | Die.dump(OS, 0); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 403 | OS << '\n'; |
| 404 | // Already verified this line table before, no need to do it again. |
| 405 | continue; |
| 406 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 407 | StmtListToDie[LineTableOffset] = Die; |
| 408 | } |
| 409 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 410 | |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 411 | void DWARFVerifier::verifyDebugLineRows() { |
| 412 | for (const auto &CU : DCtx.compile_units()) { |
| 413 | auto Die = CU->getUnitDIE(); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 414 | auto LineTable = DCtx.getLineTableForUnit(CU.get()); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 415 | // If there is no line table we will have created an error in the |
| 416 | // .debug_info verifier or in verifyDebugLineStmtOffsets(). |
| 417 | if (!LineTable) |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 418 | continue; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 419 | uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size(); |
| 420 | uint64_t PrevAddress = 0; |
| 421 | uint32_t RowIndex = 0; |
| 422 | for (const auto &Row : LineTable->Rows) { |
| 423 | if (Row.Address < PrevAddress) { |
| 424 | ++NumDebugLineErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 425 | OS << "error: .debug_line[" |
| 426 | << format("0x%08" PRIx32, |
| 427 | *toSectionOffset(Die.find(DW_AT_stmt_list))) |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 428 | << "] row[" << RowIndex |
| 429 | << "] decreases in address from previous row:\n"; |
| 430 | |
| 431 | DWARFDebugLine::Row::dumpTableHeader(OS); |
| 432 | if (RowIndex > 0) |
| 433 | LineTable->Rows[RowIndex - 1].dump(OS); |
| 434 | Row.dump(OS); |
| 435 | OS << '\n'; |
| 436 | } |
| 437 | |
| 438 | if (Row.File > MaxFileIndex) { |
| 439 | ++NumDebugLineErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 440 | OS << "error: .debug_line[" |
| 441 | << format("0x%08" PRIx32, |
| 442 | *toSectionOffset(Die.find(DW_AT_stmt_list))) |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 443 | << "][" << RowIndex << "] has invalid file index " << Row.File |
| 444 | << " (valid values are [1," << MaxFileIndex << "]):\n"; |
| 445 | DWARFDebugLine::Row::dumpTableHeader(OS); |
| 446 | Row.dump(OS); |
| 447 | OS << '\n'; |
| 448 | } |
| 449 | if (Row.EndSequence) |
| 450 | PrevAddress = 0; |
| 451 | else |
| 452 | PrevAddress = Row.Address; |
| 453 | ++RowIndex; |
| 454 | } |
| 455 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | bool DWARFVerifier::handleDebugLine() { |
| 459 | NumDebugLineErrors = 0; |
| 460 | OS << "Verifying .debug_line...\n"; |
| 461 | verifyDebugLineStmtOffsets(); |
| 462 | verifyDebugLineRows(); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 463 | return NumDebugLineErrors == 0; |
| 464 | } |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 465 | |
| 466 | bool DWARFVerifier::handleAppleNames() { |
| 467 | NumAppleNamesErrors = 0; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 468 | const DWARFObject &D = DCtx.getDWARFObj(); |
| 469 | DWARFDataExtractor AppleNamesSection(D, D.getAppleNamesSection(), |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 470 | DCtx.isLittleEndian(), 0); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 471 | DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 472 | DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData); |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 473 | |
| 474 | if (!AppleNames.extract()) { |
Spyridoula Gravani | 32614fc | 2017-06-16 22:03:21 +0000 | [diff] [blame] | 475 | return true; |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Spyridoula Gravani | 32614fc | 2017-06-16 22:03:21 +0000 | [diff] [blame] | 478 | OS << "Verifying .apple_names...\n"; |
| 479 | |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 480 | // Verify that all buckets have a valid hash index or are empty. |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 481 | uint32_t NumBuckets = AppleNames.getNumBuckets(); |
| 482 | uint32_t NumHashes = AppleNames.getNumHashes(); |
| 483 | |
| 484 | uint32_t BucketsOffset = |
| 485 | AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength(); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 486 | uint32_t HashesBase = BucketsOffset + NumBuckets * 4; |
| 487 | uint32_t OffsetsBase = HashesBase + NumHashes * 4; |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 488 | |
| 489 | for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) { |
| 490 | uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset); |
| 491 | if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 492 | OS << format("error: Bucket[%d] has invalid hash index: %u\n", BucketIdx, |
| 493 | HashIdx); |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 494 | ++NumAppleNamesErrors; |
| 495 | } |
| 496 | } |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 497 | |
| 498 | uint32_t NumAtoms = AppleNames.getAtomsDesc().size(); |
| 499 | if (NumAtoms == 0) { |
| 500 | OS << "error: no atoms; failed to read HashData\n"; |
| 501 | ++NumAppleNamesErrors; |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | if (!AppleNames.validateForms()) { |
| 506 | OS << "error: unsupported form; failed to read HashData\n"; |
| 507 | ++NumAppleNamesErrors; |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) { |
| 512 | uint32_t HashOffset = HashesBase + 4 * HashIdx; |
| 513 | uint32_t DataOffset = OffsetsBase + 4 * HashIdx; |
| 514 | uint32_t Hash = AppleNamesSection.getU32(&HashOffset); |
| 515 | uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset); |
| 516 | if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset, |
| 517 | sizeof(uint64_t))) { |
| 518 | OS << format("error: Hash[%d] has invalid HashData offset: 0x%08x\n", |
| 519 | HashIdx, HashDataOffset); |
| 520 | ++NumAppleNamesErrors; |
| 521 | } |
| 522 | |
| 523 | uint32_t StrpOffset; |
| 524 | uint32_t StringOffset; |
| 525 | uint32_t StringCount = 0; |
| 526 | uint32_t DieOffset = dwarf::DW_INVALID_OFFSET; |
| 527 | |
| 528 | while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) { |
| 529 | const uint32_t NumHashDataObjects = |
| 530 | AppleNamesSection.getU32(&HashDataOffset); |
| 531 | for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects; |
| 532 | ++HashDataIdx) { |
| 533 | DieOffset = AppleNames.readAtoms(HashDataOffset); |
| 534 | if (!DCtx.getDIEForOffset(DieOffset)) { |
| 535 | const uint32_t BucketIdx = |
| 536 | NumBuckets ? (Hash % NumBuckets) : UINT32_MAX; |
| 537 | StringOffset = StrpOffset; |
| 538 | const char *Name = StrData.getCStr(&StringOffset); |
| 539 | if (!Name) |
| 540 | Name = "<NULL>"; |
| 541 | |
| 542 | OS << format( |
| 543 | "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x " |
| 544 | "Str[%u] = 0x%08x " |
| 545 | "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n", |
| 546 | BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx, |
| 547 | DieOffset, Name); |
| 548 | |
| 549 | ++NumAppleNamesErrors; |
| 550 | } |
| 551 | } |
| 552 | ++StringCount; |
| 553 | } |
| 554 | } |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 555 | return NumAppleNamesErrors == 0; |
| 556 | } |