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 | |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 10 | #include "SyntaxHighlighting.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/DWARF/DWARFVerifier.h" |
| 12 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
| 13 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 14 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
| 15 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFSection.h" |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include <map> |
| 21 | #include <set> |
| 22 | #include <vector> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace dwarf; |
| 26 | using namespace object; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 27 | using namespace syntax; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 28 | |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 29 | DWARFVerifier::DieRangeInfo::address_range_iterator |
| 30 | DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) { |
| 31 | auto Begin = Ranges.begin(); |
| 32 | auto End = Ranges.end(); |
| 33 | auto Pos = std::lower_bound(Begin, End, R); |
| 34 | |
| 35 | if (Pos != End) { |
| 36 | if (Pos->intersects(R)) |
| 37 | return Pos; |
| 38 | if (Pos != Begin) { |
| 39 | auto Iter = Pos - 1; |
| 40 | if (Iter->intersects(R)) |
| 41 | return Iter; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | Ranges.insert(Pos, R); |
| 46 | return Ranges.end(); |
| 47 | } |
| 48 | |
| 49 | DWARFVerifier::DieRangeInfo::die_range_info_iterator |
| 50 | DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) { |
| 51 | auto End = Children.end(); |
| 52 | auto Iter = Children.begin(); |
| 53 | while (Iter != End) { |
| 54 | if (Iter->intersects(RI)) |
| 55 | return Iter; |
| 56 | ++Iter; |
| 57 | } |
| 58 | Children.insert(RI); |
| 59 | return Children.end(); |
| 60 | } |
| 61 | |
| 62 | bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const { |
| 63 | // Both list of ranges are sorted so we can make this fast. |
| 64 | |
| 65 | if (Ranges.empty() || RHS.Ranges.empty()) |
| 66 | return false; |
| 67 | |
| 68 | // Since the ranges are sorted we can advance where we start searching with |
| 69 | // this object's ranges as we traverse RHS.Ranges. |
| 70 | auto End = Ranges.end(); |
| 71 | auto Iter = findRange(RHS.Ranges.front()); |
| 72 | |
| 73 | // Now linearly walk the ranges in this object and see if they contain each |
| 74 | // ranges from RHS.Ranges. |
| 75 | for (const auto &R : RHS.Ranges) { |
| 76 | while (Iter != End) { |
| 77 | if (Iter->contains(R)) |
| 78 | break; |
| 79 | ++Iter; |
| 80 | } |
| 81 | if (Iter == End) |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const { |
| 88 | if (Ranges.empty() || RHS.Ranges.empty()) |
| 89 | return false; |
| 90 | |
| 91 | auto End = Ranges.end(); |
| 92 | auto Iter = findRange(RHS.Ranges.front()); |
| 93 | for (const auto &R : RHS.Ranges) { |
Jonas Devlieghere | d585a20 | 2017-09-14 17:46:23 +0000 | [diff] [blame] | 94 | if(Iter == End) |
| 95 | return false; |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 96 | if (R.HighPC <= Iter->LowPC) |
| 97 | continue; |
| 98 | while (Iter != End) { |
| 99 | if (Iter->intersects(R)) |
| 100 | return true; |
| 101 | ++Iter; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 108 | bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData, |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 109 | uint32_t *Offset, unsigned UnitIndex, |
| 110 | uint8_t &UnitType, bool &isUnitDWARF64) { |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 111 | uint32_t AbbrOffset, Length; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 112 | uint8_t AddrSize = 0; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 113 | uint16_t Version; |
| 114 | bool Success = true; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 115 | |
| 116 | bool ValidLength = false; |
| 117 | bool ValidVersion = false; |
| 118 | bool ValidAddrSize = false; |
| 119 | bool ValidType = true; |
| 120 | bool ValidAbbrevOffset = true; |
| 121 | |
| 122 | uint32_t OffsetStart = *Offset; |
| 123 | Length = DebugInfoData.getU32(Offset); |
| 124 | if (Length == UINT32_MAX) { |
| 125 | isUnitDWARF64 = true; |
| 126 | OS << format( |
| 127 | "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n", |
| 128 | UnitIndex); |
| 129 | return false; |
| 130 | } |
| 131 | Version = DebugInfoData.getU16(Offset); |
| 132 | |
| 133 | if (Version >= 5) { |
| 134 | UnitType = DebugInfoData.getU8(Offset); |
| 135 | AddrSize = DebugInfoData.getU8(Offset); |
| 136 | AbbrOffset = DebugInfoData.getU32(Offset); |
| 137 | ValidType = DWARFUnit::isValidUnitType(UnitType); |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 138 | } else { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 139 | UnitType = 0; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 140 | AbbrOffset = DebugInfoData.getU32(Offset); |
| 141 | AddrSize = DebugInfoData.getU8(Offset); |
| 142 | } |
| 143 | |
| 144 | if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset)) |
| 145 | ValidAbbrevOffset = false; |
| 146 | |
| 147 | ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3); |
| 148 | ValidVersion = DWARFContext::isSupportedVersion(Version); |
| 149 | ValidAddrSize = AddrSize == 4 || AddrSize == 8; |
| 150 | if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset || |
| 151 | !ValidType) { |
| 152 | Success = false; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 153 | error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, |
| 154 | OffsetStart); |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 155 | if (!ValidLength) |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 156 | note() << "The length for this unit is too " |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 157 | "large for the .debug_info provided.\n"; |
| 158 | if (!ValidVersion) |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 159 | note() << "The 16 bit unit header version is not valid.\n"; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 160 | if (!ValidType) |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 161 | note() << "The unit type encoding is not valid.\n"; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 162 | if (!ValidAbbrevOffset) |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 163 | note() << "The offset into the .debug_abbrev section is " |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 164 | "not valid.\n"; |
| 165 | if (!ValidAddrSize) |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 166 | note() << "The address size is unsupported.\n"; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 167 | } |
| 168 | *Offset = OffsetStart + Length + 4; |
| 169 | return Success; |
| 170 | } |
| 171 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 172 | bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) { |
| 173 | uint32_t NumUnitErrors = 0; |
| 174 | unsigned NumDies = Unit.getNumDIEs(); |
| 175 | for (unsigned I = 0; I < NumDies; ++I) { |
| 176 | auto Die = Unit.getDIEAtIndex(I); |
| 177 | if (Die.getTag() == DW_TAG_null) |
| 178 | continue; |
| 179 | for (auto AttrValue : Die.attributes()) { |
| 180 | NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue); |
| 181 | NumUnitErrors += verifyDebugInfoForm(Die, AttrValue); |
| 182 | } |
| 183 | } |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 184 | |
Jonas Devlieghere | 35fdaa9 | 2017-09-28 15:57:50 +0000 | [diff] [blame] | 185 | if (DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false)) { |
| 186 | DieRangeInfo RI; |
| 187 | NumUnitErrors += verifyDieRanges(Die, RI); |
| 188 | } else { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 189 | error() << "Compilation unit without unit DIE.\n"; |
Jonas Devlieghere | 35fdaa9 | 2017-09-28 15:57:50 +0000 | [diff] [blame] | 190 | NumUnitErrors++; |
| 191 | } |
| 192 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 193 | return NumUnitErrors == 0; |
| 194 | } |
| 195 | |
Spyridoula Gravani | c6ef987 | 2017-07-21 00:51:32 +0000 | [diff] [blame] | 196 | unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) { |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 197 | unsigned NumErrors = 0; |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 198 | if (Abbrev) { |
| 199 | const DWARFAbbreviationDeclarationSet *AbbrDecls = |
| 200 | Abbrev->getAbbreviationDeclarationSet(0); |
| 201 | for (auto AbbrDecl : *AbbrDecls) { |
| 202 | SmallDenseSet<uint16_t> AttributeSet; |
| 203 | for (auto Attribute : AbbrDecl.attributes()) { |
| 204 | auto Result = AttributeSet.insert(Attribute.Attr); |
| 205 | if (!Result.second) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 206 | error() << "Abbreviation declaration contains multiple " |
| 207 | << AttributeString(Attribute.Attr) << " attributes.\n"; |
Spyridoula Gravani | c6ef987 | 2017-07-21 00:51:32 +0000 | [diff] [blame] | 208 | AbbrDecl.dump(OS); |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 209 | ++NumErrors; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
Spyridoula Gravani | c6ef987 | 2017-07-21 00:51:32 +0000 | [diff] [blame] | 214 | return NumErrors; |
| 215 | } |
| 216 | |
| 217 | bool DWARFVerifier::handleDebugAbbrev() { |
| 218 | OS << "Verifying .debug_abbrev...\n"; |
| 219 | |
| 220 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
| 221 | bool noDebugAbbrev = DObj.getAbbrevSection().empty(); |
| 222 | bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty(); |
| 223 | |
| 224 | if (noDebugAbbrev && noDebugAbbrevDWO) { |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | unsigned NumErrors = 0; |
| 229 | if (!noDebugAbbrev) |
| 230 | NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev()); |
| 231 | |
| 232 | if (!noDebugAbbrevDWO) |
| 233 | NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO()); |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 234 | return NumErrors == 0; |
| 235 | } |
| 236 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 237 | bool DWARFVerifier::handleDebugInfo() { |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 238 | OS << "Verifying .debug_info Unit Header Chain...\n"; |
| 239 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 240 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
| 241 | DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(), |
| 242 | DCtx.isLittleEndian(), 0); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 243 | uint32_t NumDebugInfoErrors = 0; |
| 244 | uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0; |
| 245 | uint8_t UnitType = 0; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 246 | bool isUnitDWARF64 = false; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 247 | bool isHeaderChainValid = true; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 248 | bool hasDIE = DebugInfoData.isValidOffset(Offset); |
| 249 | while (hasDIE) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 250 | OffsetStart = Offset; |
| 251 | if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType, |
| 252 | isUnitDWARF64)) { |
| 253 | isHeaderChainValid = false; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 254 | if (isUnitDWARF64) |
| 255 | break; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 256 | } else { |
| 257 | std::unique_ptr<DWARFUnit> Unit; |
| 258 | switch (UnitType) { |
| 259 | case dwarf::DW_UT_type: |
| 260 | case dwarf::DW_UT_split_type: { |
| 261 | DWARFUnitSection<DWARFTypeUnit> TUSection{}; |
| 262 | Unit.reset(new DWARFTypeUnit( |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 263 | DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(), |
| 264 | &DObj.getRangeSection(), DObj.getStringSection(), |
| 265 | DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(), |
| 266 | DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection, |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 267 | nullptr)); |
| 268 | break; |
| 269 | } |
| 270 | case dwarf::DW_UT_skeleton: |
| 271 | case dwarf::DW_UT_split_compile: |
| 272 | case dwarf::DW_UT_compile: |
| 273 | case dwarf::DW_UT_partial: |
| 274 | // UnitType = 0 means that we are |
| 275 | // verifying a compile unit in DWARF v4. |
| 276 | case 0: { |
| 277 | DWARFUnitSection<DWARFCompileUnit> CUSection{}; |
| 278 | Unit.reset(new DWARFCompileUnit( |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 279 | DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(), |
| 280 | &DObj.getRangeSection(), DObj.getStringSection(), |
| 281 | DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(), |
| 282 | DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection, |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 283 | nullptr)); |
| 284 | break; |
| 285 | } |
| 286 | default: { llvm_unreachable("Invalid UnitType."); } |
| 287 | } |
| 288 | Unit->extract(DebugInfoData, &OffsetStart); |
| 289 | if (!verifyUnitContents(*Unit)) |
| 290 | ++NumDebugInfoErrors; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 291 | } |
| 292 | hasDIE = DebugInfoData.isValidOffset(Offset); |
| 293 | ++UnitIdx; |
| 294 | } |
| 295 | if (UnitIdx == 0 && !hasDIE) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 296 | warn() << ".debug_info is empty.\n"; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 297 | isHeaderChainValid = true; |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 298 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 299 | NumDebugInfoErrors += verifyDebugInfoReferences(); |
| 300 | return (isHeaderChainValid && NumDebugInfoErrors == 0); |
Spyridoula Gravani | 890eedc | 2017-07-13 23:25:24 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 303 | unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die, |
| 304 | DieRangeInfo &ParentRI) { |
Spyridoula Gravani | e0ba415 | 2017-07-24 21:04:11 +0000 | [diff] [blame] | 305 | unsigned NumErrors = 0; |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 306 | |
| 307 | if (!Die.isValid()) |
| 308 | return NumErrors; |
| 309 | |
| 310 | DWARFAddressRangesVector Ranges = Die.getAddressRanges(); |
| 311 | |
| 312 | // Build RI for this DIE and check that ranges within this DIE do not |
| 313 | // overlap. |
| 314 | DieRangeInfo RI(Die); |
| 315 | for (auto Range : Ranges) { |
| 316 | if (!Range.valid()) { |
Spyridoula Gravani | e0ba415 | 2017-07-24 21:04:11 +0000 | [diff] [blame] | 317 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 318 | error() << format("Invalid address range [0x%08" PRIx64 " - 0x%08" PRIx64 |
| 319 | "].\n", |
| 320 | Range.LowPC, Range.HighPC); |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 321 | continue; |
| 322 | } |
| 323 | |
| 324 | // Verify that ranges don't intersect. |
| 325 | const auto IntersectingRange = RI.insert(Range); |
| 326 | if (IntersectingRange != RI.Ranges.end()) { |
| 327 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 328 | error() << format("DIE has overlapping address ranges: [0x%08" PRIx64 |
| 329 | " - 0x%08" PRIx64 "] and [0x%08" PRIx64 |
| 330 | " - 0x%08" PRIx64 "].\n", |
| 331 | Range.LowPC, Range.HighPC, IntersectingRange->LowPC, |
| 332 | IntersectingRange->HighPC); |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 333 | break; |
Spyridoula Gravani | e0ba415 | 2017-07-24 21:04:11 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 336 | |
| 337 | // Verify that children don't intersect. |
| 338 | const auto IntersectingChild = ParentRI.insert(RI); |
| 339 | if (IntersectingChild != ParentRI.Children.end()) { |
| 340 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 341 | error() << "DIEs have overlapping address ranges:"; |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 342 | Die.dump(OS, 0); |
| 343 | IntersectingChild->Die.dump(OS, 0); |
| 344 | OS << "\n"; |
| 345 | } |
| 346 | |
| 347 | // Verify that ranges are contained within their parent. |
| 348 | bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() && |
| 349 | !(Die.getTag() == DW_TAG_subprogram && |
| 350 | ParentRI.Die.getTag() == DW_TAG_subprogram); |
| 351 | if (ShouldBeContained && !ParentRI.contains(RI)) { |
| 352 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 353 | error() << "DIE address ranges are not " |
| 354 | "contained in its parent's ranges:"; |
Jonas Devlieghere | 5891060 | 2017-09-14 11:33:42 +0000 | [diff] [blame] | 355 | Die.dump(OS, 0); |
| 356 | ParentRI.Die.dump(OS, 0); |
| 357 | OS << "\n"; |
| 358 | } |
| 359 | |
| 360 | // Recursively check children. |
| 361 | for (DWARFDie Child : Die) |
| 362 | NumErrors += verifyDieRanges(Child, RI); |
| 363 | |
Spyridoula Gravani | e0ba415 | 2017-07-24 21:04:11 +0000 | [diff] [blame] | 364 | return NumErrors; |
| 365 | } |
| 366 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 367 | unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die, |
| 368 | DWARFAttribute &AttrValue) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 369 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 370 | unsigned NumErrors = 0; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 371 | const auto Attr = AttrValue.Attr; |
| 372 | switch (Attr) { |
| 373 | case DW_AT_ranges: |
| 374 | // Make sure the offset in the DW_AT_ranges attribute is valid. |
| 375 | if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 376 | if (*SectionOffset >= DObj.getRangeSection().Data.size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 377 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 378 | error() << "DW_AT_ranges offset is beyond .debug_ranges " |
| 379 | "bounds:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 380 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 381 | OS << "\n"; |
| 382 | } |
| 383 | } else { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 384 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 385 | error() << "DIE has invalid DW_AT_ranges encoding:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 386 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 387 | OS << "\n"; |
| 388 | } |
| 389 | break; |
| 390 | case DW_AT_stmt_list: |
| 391 | // Make sure the offset in the DW_AT_stmt_list attribute is valid. |
| 392 | if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 393 | if (*SectionOffset >= DObj.getLineSection().Data.size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 394 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 395 | error() << "DW_AT_stmt_list offset is beyond .debug_line " |
| 396 | "bounds: " |
| 397 | << format("0x%08" PRIx64, *SectionOffset) << "\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 398 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 399 | OS << "\n"; |
| 400 | } |
| 401 | } else { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 402 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 403 | error() << "DIE has invalid DW_AT_stmt_list encoding:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 404 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 405 | OS << "\n"; |
| 406 | } |
| 407 | break; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 408 | |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 409 | default: |
| 410 | break; |
| 411 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 412 | return NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 413 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 414 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 415 | unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die, |
| 416 | DWARFAttribute &AttrValue) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 417 | const DWARFObject &DObj = DCtx.getDWARFObj(); |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 418 | unsigned NumErrors = 0; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 419 | const auto Form = AttrValue.Value.getForm(); |
| 420 | switch (Form) { |
| 421 | case DW_FORM_ref1: |
| 422 | case DW_FORM_ref2: |
| 423 | case DW_FORM_ref4: |
| 424 | case DW_FORM_ref8: |
| 425 | case DW_FORM_ref_udata: { |
| 426 | // Verify all CU relative references are valid CU offsets. |
| 427 | Optional<uint64_t> RefVal = AttrValue.Value.getAsReference(); |
| 428 | assert(RefVal); |
| 429 | if (RefVal) { |
| 430 | auto DieCU = Die.getDwarfUnit(); |
| 431 | auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset(); |
| 432 | auto CUOffset = AttrValue.Value.getRawUValue(); |
| 433 | if (CUOffset >= CUSize) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 434 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 435 | error() << FormEncodingString(Form) << " CU offset " |
| 436 | << format("0x%08" PRIx64, CUOffset) |
| 437 | << " is invalid (must be less than CU size of " |
| 438 | << format("0x%08" PRIx32, CUSize) << "):\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 439 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 440 | OS << "\n"; |
| 441 | } else { |
| 442 | // Valid reference, but we will verify it points to an actual |
| 443 | // DIE later. |
| 444 | ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset()); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 447 | break; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 448 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 449 | case DW_FORM_ref_addr: { |
| 450 | // Verify all absolute DIE references have valid offsets in the |
| 451 | // .debug_info section. |
| 452 | Optional<uint64_t> RefVal = AttrValue.Value.getAsReference(); |
| 453 | assert(RefVal); |
| 454 | if (RefVal) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 455 | if (*RefVal >= DObj.getInfoSection().Data.size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 456 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 457 | error() << "DW_FORM_ref_addr offset beyond .debug_info " |
| 458 | "bounds:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 459 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 460 | OS << "\n"; |
| 461 | } else { |
| 462 | // Valid reference, but we will verify it points to an actual |
| 463 | // DIE later. |
| 464 | ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset()); |
| 465 | } |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | case DW_FORM_strp: { |
| 470 | auto SecOffset = AttrValue.Value.getAsSectionOffset(); |
| 471 | assert(SecOffset); // DW_FORM_strp is a section offset. |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 472 | if (SecOffset && *SecOffset >= DObj.getStringSection().size()) { |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 473 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 474 | error() << "DW_FORM_strp offset beyond .debug_str bounds:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 475 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 476 | OS << "\n"; |
| 477 | } |
| 478 | break; |
| 479 | } |
| 480 | default: |
| 481 | break; |
| 482 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 483 | return NumErrors; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 484 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 485 | |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 486 | unsigned DWARFVerifier::verifyDebugInfoReferences() { |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 487 | // Take all references and make sure they point to an actual DIE by |
| 488 | // getting the DIE by offset and emitting an error |
| 489 | OS << "Verifying .debug_info references...\n"; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 490 | unsigned NumErrors = 0; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 491 | for (auto Pair : ReferenceToDIEOffsets) { |
| 492 | auto Die = DCtx.getDIEForOffset(Pair.first); |
| 493 | if (Die) |
| 494 | continue; |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 495 | ++NumErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 496 | error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first) |
| 497 | << ". Offset is in between DIEs:\n"; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 498 | for (auto Offset : Pair.second) { |
| 499 | auto ReferencingDie = DCtx.getDIEForOffset(Offset); |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 500 | ReferencingDie.dump(OS, 0, DumpOpts); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 501 | OS << "\n"; |
| 502 | } |
| 503 | OS << "\n"; |
| 504 | } |
Spyridoula Gravani | f6bd788d | 2017-07-18 01:00:26 +0000 | [diff] [blame] | 505 | return NumErrors; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 508 | void DWARFVerifier::verifyDebugLineStmtOffsets() { |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 509 | std::map<uint64_t, DWARFDie> StmtListToDie; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 510 | for (const auto &CU : DCtx.compile_units()) { |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 511 | auto Die = CU->getUnitDIE(); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 512 | // Get the attribute value as a section offset. No need to produce an |
| 513 | // error here if the encoding isn't correct because we validate this in |
| 514 | // the .debug_info verifier. |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 515 | auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list)); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 516 | if (!StmtSectionOffset) |
| 517 | continue; |
| 518 | const uint32_t LineTableOffset = *StmtSectionOffset; |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 519 | auto LineTable = DCtx.getLineTableForUnit(CU.get()); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 520 | if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) { |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 521 | if (!LineTable) { |
| 522 | ++NumDebugLineErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 523 | error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset) |
| 524 | << "] was not able to be parsed for CU:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 525 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 526 | OS << '\n'; |
| 527 | continue; |
| 528 | } |
| 529 | } else { |
| 530 | // Make sure we don't get a valid line table back if the offset is wrong. |
| 531 | assert(LineTable == nullptr); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 532 | // Skip this line table as it isn't valid. No need to create an error |
| 533 | // here because we validate this in the .debug_info verifier. |
| 534 | continue; |
| 535 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 536 | auto Iter = StmtListToDie.find(LineTableOffset); |
| 537 | if (Iter != StmtListToDie.end()) { |
| 538 | ++NumDebugLineErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 539 | error() << "two compile unit DIEs, " |
| 540 | << format("0x%08" PRIx32, Iter->second.getOffset()) << " and " |
| 541 | << format("0x%08" PRIx32, Die.getOffset()) |
| 542 | << ", have the same DW_AT_stmt_list section offset:\n"; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 543 | Iter->second.dump(OS, 0, DumpOpts); |
| 544 | Die.dump(OS, 0, DumpOpts); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 545 | OS << '\n'; |
| 546 | // Already verified this line table before, no need to do it again. |
| 547 | continue; |
| 548 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 549 | StmtListToDie[LineTableOffset] = Die; |
| 550 | } |
| 551 | } |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 552 | |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 553 | void DWARFVerifier::verifyDebugLineRows() { |
| 554 | for (const auto &CU : DCtx.compile_units()) { |
| 555 | auto Die = CU->getUnitDIE(); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 556 | auto LineTable = DCtx.getLineTableForUnit(CU.get()); |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 557 | // If there is no line table we will have created an error in the |
| 558 | // .debug_info verifier or in verifyDebugLineStmtOffsets(). |
| 559 | if (!LineTable) |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 560 | continue; |
Jonas Devlieghere | f4ed65d | 2017-09-08 09:48:51 +0000 | [diff] [blame] | 561 | |
| 562 | // Verify prologue. |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 563 | uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size(); |
Jonas Devlieghere | f4ed65d | 2017-09-08 09:48:51 +0000 | [diff] [blame] | 564 | uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size(); |
| 565 | uint32_t FileIndex = 1; |
| 566 | StringMap<uint16_t> FullPathMap; |
| 567 | for (const auto &FileName : LineTable->Prologue.FileNames) { |
| 568 | // Verify directory index. |
| 569 | if (FileName.DirIdx > MaxDirIndex) { |
| 570 | ++NumDebugLineErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 571 | error() << ".debug_line[" |
| 572 | << format("0x%08" PRIx64, |
| 573 | *toSectionOffset(Die.find(DW_AT_stmt_list))) |
| 574 | << "].prologue.file_names[" << FileIndex |
| 575 | << "].dir_idx contains an invalid index: " << FileName.DirIdx |
| 576 | << "\n"; |
Jonas Devlieghere | f4ed65d | 2017-09-08 09:48:51 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // Check file paths for duplicates. |
| 580 | std::string FullPath; |
| 581 | const bool HasFullPath = LineTable->getFileNameByIndex( |
| 582 | FileIndex, CU->getCompilationDir(), |
| 583 | DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath); |
| 584 | assert(HasFullPath && "Invalid index?"); |
| 585 | (void)HasFullPath; |
| 586 | auto It = FullPathMap.find(FullPath); |
| 587 | if (It == FullPathMap.end()) |
| 588 | FullPathMap[FullPath] = FileIndex; |
| 589 | else if (It->second != FileIndex) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 590 | warn() << ".debug_line[" |
| 591 | << format("0x%08" PRIx64, |
| 592 | *toSectionOffset(Die.find(DW_AT_stmt_list))) |
| 593 | << "].prologue.file_names[" << FileIndex |
| 594 | << "] is a duplicate of file_names[" << It->second << "]\n"; |
Jonas Devlieghere | f4ed65d | 2017-09-08 09:48:51 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | FileIndex++; |
| 598 | } |
| 599 | |
| 600 | // Verify rows. |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 601 | uint64_t PrevAddress = 0; |
| 602 | uint32_t RowIndex = 0; |
| 603 | for (const auto &Row : LineTable->Rows) { |
Jonas Devlieghere | f4ed65d | 2017-09-08 09:48:51 +0000 | [diff] [blame] | 604 | // Verify row address. |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 605 | if (Row.Address < PrevAddress) { |
| 606 | ++NumDebugLineErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 607 | error() << ".debug_line[" |
| 608 | << format("0x%08" PRIx64, |
| 609 | *toSectionOffset(Die.find(DW_AT_stmt_list))) |
| 610 | << "] row[" << RowIndex |
| 611 | << "] decreases in address from previous row:\n"; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 612 | |
| 613 | DWARFDebugLine::Row::dumpTableHeader(OS); |
| 614 | if (RowIndex > 0) |
| 615 | LineTable->Rows[RowIndex - 1].dump(OS); |
| 616 | Row.dump(OS); |
| 617 | OS << '\n'; |
| 618 | } |
| 619 | |
Jonas Devlieghere | f4ed65d | 2017-09-08 09:48:51 +0000 | [diff] [blame] | 620 | // Verify file index. |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 621 | if (Row.File > MaxFileIndex) { |
| 622 | ++NumDebugLineErrors; |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 623 | error() << ".debug_line[" |
| 624 | << format("0x%08" PRIx64, |
| 625 | *toSectionOffset(Die.find(DW_AT_stmt_list))) |
| 626 | << "][" << RowIndex << "] has invalid file index " << Row.File |
| 627 | << " (valid values are [1," << MaxFileIndex << "]):\n"; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 628 | DWARFDebugLine::Row::dumpTableHeader(OS); |
| 629 | Row.dump(OS); |
| 630 | OS << '\n'; |
| 631 | } |
| 632 | if (Row.EndSequence) |
| 633 | PrevAddress = 0; |
| 634 | else |
| 635 | PrevAddress = Row.Address; |
| 636 | ++RowIndex; |
| 637 | } |
| 638 | } |
Greg Clayton | c5b2d56 | 2017-05-03 18:25:46 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | bool DWARFVerifier::handleDebugLine() { |
| 642 | NumDebugLineErrors = 0; |
| 643 | OS << "Verifying .debug_line...\n"; |
| 644 | verifyDebugLineStmtOffsets(); |
| 645 | verifyDebugLineRows(); |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 646 | return NumDebugLineErrors == 0; |
| 647 | } |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 648 | |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 649 | unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection, |
| 650 | DataExtractor *StrData, |
| 651 | const char *SectionName) { |
| 652 | unsigned NumErrors = 0; |
| 653 | DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection, |
| 654 | DCtx.isLittleEndian(), 0); |
| 655 | DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData); |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 656 | |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 657 | OS << "Verifying " << SectionName << "...\n"; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 658 | |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 659 | // Verify that the fixed part of the header is not too short. |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 660 | if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 661 | error() << "Section is too small to fit a section header.\n"; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 662 | return 1; |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 663 | } |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 664 | |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 665 | // Verify that the section is not too short. |
| 666 | if (!AccelTable.extract()) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 667 | error() << "Section is smaller than size described in section header.\n"; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 668 | return 1; |
| 669 | } |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 670 | |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 671 | // Verify that all buckets have a valid hash index or are empty. |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 672 | uint32_t NumBuckets = AccelTable.getNumBuckets(); |
| 673 | uint32_t NumHashes = AccelTable.getNumHashes(); |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 674 | |
| 675 | uint32_t BucketsOffset = |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 676 | AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength(); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 677 | uint32_t HashesBase = BucketsOffset + NumBuckets * 4; |
| 678 | uint32_t OffsetsBase = HashesBase + NumHashes * 4; |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 679 | for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) { |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 680 | uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset); |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 681 | if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 682 | error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx, |
| 683 | HashIdx); |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 684 | ++NumErrors; |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 687 | uint32_t NumAtoms = AccelTable.getAtomsDesc().size(); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 688 | if (NumAtoms == 0) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 689 | error() << "No atoms: failed to read HashData.\n"; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 690 | return 1; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 691 | } |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 692 | if (!AccelTable.validateForms()) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 693 | error() << "Unsupported form: failed to read HashData.\n"; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 694 | return 1; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) { |
| 698 | uint32_t HashOffset = HashesBase + 4 * HashIdx; |
| 699 | uint32_t DataOffset = OffsetsBase + 4 * HashIdx; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 700 | uint32_t Hash = AccelSectionData.getU32(&HashOffset); |
| 701 | uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset); |
| 702 | if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset, |
| 703 | sizeof(uint64_t))) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 704 | error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n", |
| 705 | HashIdx, HashDataOffset); |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 706 | ++NumErrors; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | uint32_t StrpOffset; |
| 710 | uint32_t StringOffset; |
| 711 | uint32_t StringCount = 0; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 712 | unsigned Offset; |
| 713 | unsigned Tag; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 714 | while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 715 | const uint32_t NumHashDataObjects = |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 716 | AccelSectionData.getU32(&HashDataOffset); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 717 | for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects; |
| 718 | ++HashDataIdx) { |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 719 | std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset); |
| 720 | auto Die = DCtx.getDIEForOffset(Offset); |
| 721 | if (!Die) { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 722 | const uint32_t BucketIdx = |
| 723 | NumBuckets ? (Hash % NumBuckets) : UINT32_MAX; |
| 724 | StringOffset = StrpOffset; |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 725 | const char *Name = StrData->getCStr(&StringOffset); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 726 | if (!Name) |
| 727 | Name = "<NULL>"; |
| 728 | |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 729 | error() << format( |
| 730 | "%s Bucket[%d] Hash[%d] = 0x%08x " |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 731 | "Str[%u] = 0x%08x " |
| 732 | "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n", |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 733 | SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset, |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 734 | HashDataIdx, Offset, Name); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 735 | |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 736 | ++NumErrors; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 737 | continue; |
| 738 | } |
| 739 | if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) { |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 740 | error() << "Tag " << dwarf::TagString(Tag) |
| 741 | << " in accelerator table does not match Tag " |
| 742 | << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx |
| 743 | << "].\n"; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 744 | ++NumErrors; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | ++StringCount; |
| 748 | } |
| 749 | } |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 750 | return NumErrors; |
| 751 | } |
| 752 | |
| 753 | bool DWARFVerifier::handleAccelTables() { |
| 754 | const DWARFObject &D = DCtx.getDWARFObj(); |
| 755 | DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0); |
| 756 | unsigned NumErrors = 0; |
| 757 | if (!D.getAppleNamesSection().Data.empty()) |
| 758 | NumErrors += |
| 759 | verifyAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names"); |
| 760 | if (!D.getAppleTypesSection().Data.empty()) |
| 761 | NumErrors += |
| 762 | verifyAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types"); |
| 763 | if (!D.getAppleNamespacesSection().Data.empty()) |
| 764 | NumErrors += verifyAccelTable(&D.getAppleNamespacesSection(), &StrData, |
| 765 | ".apple_namespaces"); |
| 766 | if (!D.getAppleObjCSection().Data.empty()) |
| 767 | NumErrors += |
| 768 | verifyAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc"); |
| 769 | return NumErrors == 0; |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 770 | } |
Jonas Devlieghere | 19fc4d9 | 2017-09-29 09:33:31 +0000 | [diff] [blame^] | 771 | |
| 772 | raw_ostream &DWARFVerifier::error() const { |
| 773 | return WithColor(OS, syntax::Error).get() << "error: "; |
| 774 | } |
| 775 | |
| 776 | raw_ostream &DWARFVerifier::warn() const { |
| 777 | return WithColor(OS, syntax::Warning).get() << "warning: "; |
| 778 | } |
| 779 | |
| 780 | raw_ostream &DWARFVerifier::note() const { |
| 781 | return WithColor(OS, syntax::Note).get() << "note: "; |
| 782 | } |