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