Index: include/llvm/DebugInfo/DWARF/DWARFVerifier.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFVerifier.h +++ include/llvm/DebugInfo/DWARF/DWARFVerifier.h @@ -31,7 +31,6 @@ /// can verify each reference points to a valid DIE and not an offset that /// lies between to valid DIEs. std::map> ReferenceToDIEOffsets; - uint32_t NumDebugInfoErrors = 0; uint32_t NumDebugLineErrors = 0; uint32_t NumAppleNamesErrors = 0; @@ -50,14 +49,16 @@ /// \param Offset A reference to the offset start of the unit. The offset will /// be updated to point to the next unit in .debug_info /// \param UnitIndex The index of the unit to be verified + /// \param UnitType A reference to the type of the unit /// \param isUnitDWARF64 A reference to a flag that shows whether the unit is /// in 64-bit format. /// /// \returns true if the header is verified successfully, false otherwise. bool verifyUnitHeader(const DWARFDataExtractor DebugInfoData, - uint32_t *Offset, unsigned UnitIndex, + uint32_t *Offset, unsigned UnitIndex, uint8_t &UnitType, bool &isUnitDWARF64); + bool verifyUnitContents(DWARFUnit Unit); /// Verifies the attribute's DWARF attribute and its value. /// /// This function currently checks for: @@ -66,7 +67,10 @@ /// /// \param Die The DWARF DIE that owns the attribute value /// \param AttrValue The DWARF attribute value to check - void verifyDebugInfoAttribute(const DWARFDie &Die, DWARFAttribute &AttrValue); + /// \param NumUnitErrors Reference to the number of errors for the unit that + /// is currently being verified + void verifyDebugInfoAttribute(const DWARFDie &Die, DWARFAttribute &AttrValue, + uint32_t &NumUnitErrors); /// Verifies the attribute's DWARF form. /// @@ -77,7 +81,10 @@ /// /// \param Die The DWARF DIE that owns the attribute value /// \param AttrValue The DWARF attribute value to check - void verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue); + /// \param NumUnitErrors Reference to the number of errors for the unit that + /// is currently being verified + void verifyDebugInfoForm(const DWARFDie &Die, DWARFAttribute &AttrValue, + uint32_t &NumUnitErrors); /// Verifies the all valid references that were found when iterating through /// all of the DIE attributes. @@ -86,7 +93,10 @@ /// offset matches. This helps to ensure if a DWARF link phase moved things /// around, that it doesn't create invalid references by failing to relocate /// CU relative and absolute references. - void verifyDebugInfoReferences(); + /// + /// \param NumDebugInfoErrors Reference to the number of errors for the + /// .debug_info section + void verifyDebugInfoReferences(uint32_t &NumDebugInfoErrors); /// Verify the the DW_AT_stmt_list encoding and value and ensure that no /// compile units that have the same DW_AT_stmt_list value. @@ -102,14 +112,6 @@ public: DWARFVerifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {} - /// Verify the unit header chain in the .debug_info section. - /// - /// Any errors are reported to the stream that this object was - /// constructed with. - /// - /// \returns true if the unit header chain verifies successfully, false - /// otherwise. - bool handleDebugInfoUnitHeaderChain(); /// Verify the information in the .debug_info section. /// /// Any errors are reported to the stream that was this object was Index: lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -414,8 +414,6 @@ bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) { bool Success = true; DWARFVerifier verifier(OS, *this); - if (!verifier.handleDebugInfoUnitHeaderChain()) - Success = false; if (DumpType == DIDT_All || DumpType == DIDT_Info) { if (!verifier.handleDebugInfo()) Success = false; Index: lib/DebugInfo/DWARF/DWARFVerifier.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFVerifier.cpp +++ lib/DebugInfo/DWARF/DWARFVerifier.cpp @@ -25,10 +25,10 @@ using namespace object; bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData, - uint32_t *Offset, unsigned UnitIndex, - bool &isUnitDWARF64) { + uint32_t *Offset, unsigned UnitIndex, + uint8_t &UnitType, bool &isUnitDWARF64) { uint32_t AbbrOffset, Length; - uint8_t AddrSize = 0, UnitType = 0; + uint8_t AddrSize = 0; uint16_t Version; bool Success = true; @@ -55,6 +55,7 @@ AbbrOffset = DebugInfoData.getU32(Offset); ValidType = DWARFUnit::isValidUnitType(UnitType); } else { + UnitType = 0; AbbrOffset = DebugInfoData.getU32(Offset); AddrSize = DebugInfoData.getU8(Offset); } @@ -86,47 +87,103 @@ return Success; } -bool DWARFVerifier::handleDebugInfoUnitHeaderChain() { +bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) { + uint32_t NumUnitErrors = 0; + unsigned NumDies = Unit.getNumDIEs(); + for (unsigned I = 0; I < NumDies; ++I) { + auto Die = Unit.getDIEAtIndex(I); + if (Die.getTag() == DW_TAG_null) + continue; + for (auto AttrValue : Die.attributes()) { + verifyDebugInfoAttribute(Die, AttrValue, NumUnitErrors); + verifyDebugInfoForm(Die, AttrValue, NumUnitErrors); + } + } + return NumUnitErrors == 0; +} + +bool DWARFVerifier::handleDebugInfo() { OS << "Verifying .debug_info Unit Header Chain...\n"; DWARFDataExtractor DebugInfoData(DCtx.getInfoSection(), DCtx.isLittleEndian(), 0); - uint32_t Offset = 0, UnitIdx = 0; + uint32_t NumDebugInfoErrors = 0; + uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0; + uint8_t UnitType = 0; bool isUnitDWARF64 = false; - bool Success = true; + bool isHeaderChainValid = true; bool hasDIE = DebugInfoData.isValidOffset(Offset); while (hasDIE) { - if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, isUnitDWARF64)) { - Success = false; + OffsetStart = Offset; + if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType, + isUnitDWARF64)) { + isHeaderChainValid = false; if (isUnitDWARF64) break; + } else { + std::unique_ptr Unit; + switch (UnitType) { + case dwarf::DW_UT_type: + case dwarf::DW_UT_split_type: { + DWARFUnitSection TUSection{}; + Unit.reset(new DWARFTypeUnit( + DCtx, DCtx.getInfoSection(), DCtx.getDebugAbbrev(), + &DCtx.getRangeSection(), DCtx.getStringSection(), + DCtx.getStringOffsetSection(), &DCtx.getAppleObjCSection(), + DCtx.getLineSection(), DCtx.isLittleEndian(), false, TUSection, + nullptr)); + break; + } + case dwarf::DW_UT_skeleton: + case dwarf::DW_UT_split_compile: + case dwarf::DW_UT_compile: + case dwarf::DW_UT_partial: + // UnitType = 0 means that we are + // verifying a compile unit in dwarf4 version. + case 0: { + DWARFUnitSection CUSection{}; + Unit.reset(new DWARFCompileUnit( + DCtx, DCtx.getInfoSection(), DCtx.getDebugAbbrev(), + &DCtx.getRangeSection(), DCtx.getStringSection(), + DCtx.getStringOffsetSection(), &DCtx.getAppleObjCSection(), + DCtx.getLineSection(), DCtx.isLittleEndian(), false, CUSection, + nullptr)); + break; + } + default: { llvm_unreachable("Invalid UnitType."); } + } + Unit->extract(DebugInfoData, &OffsetStart); + if (!verifyUnitContents(*Unit)) + ++NumDebugInfoErrors; } hasDIE = DebugInfoData.isValidOffset(Offset); ++UnitIdx; } if (UnitIdx == 0 && !hasDIE) { OS << "Warning: .debug_info is empty.\n"; - Success = true; + isHeaderChainValid = true; } - return Success; + verifyDebugInfoReferences(NumDebugInfoErrors); + return (isHeaderChainValid && NumDebugInfoErrors == 0); } void DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die, - DWARFAttribute &AttrValue) { + DWARFAttribute &AttrValue, + uint32_t &NumUnitErrors) { const auto Attr = AttrValue.Attr; switch (Attr) { case DW_AT_ranges: // Make sure the offset in the DW_AT_ranges attribute is valid. if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) { if (*SectionOffset >= DCtx.getRangeSection().Data.size()) { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: DW_AT_ranges offset is beyond .debug_ranges " "bounds:\n"; Die.dump(OS, 0); OS << "\n"; } } else { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: DIE has invalid DW_AT_ranges encoding:\n"; Die.dump(OS, 0); OS << "\n"; @@ -136,7 +193,7 @@ // Make sure the offset in the DW_AT_stmt_list attribute is valid. if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) { if (*SectionOffset >= DCtx.getLineSection().Data.size()) { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: DW_AT_stmt_list offset is beyond .debug_line " "bounds: " << format("0x%08" PRIx32, *SectionOffset) << "\n"; @@ -144,7 +201,7 @@ OS << "\n"; } } else { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n"; Die.dump(OS, 0); OS << "\n"; @@ -157,7 +214,8 @@ } void DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die, - DWARFAttribute &AttrValue) { + DWARFAttribute &AttrValue, + uint32_t &NumUnitErrors) { const auto Form = AttrValue.Value.getForm(); switch (Form) { case DW_FORM_ref1: @@ -173,7 +231,7 @@ auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset(); auto CUOffset = AttrValue.Value.getRawUValue(); if (CUOffset >= CUSize) { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: " << FormEncodingString(Form) << " CU offset " << format("0x%08" PRIx32, CUOffset) << " is invalid (must be less than CU size of " @@ -195,7 +253,7 @@ assert(RefVal); if (RefVal) { if (*RefVal >= DCtx.getInfoSection().Data.size()) { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: DW_FORM_ref_addr offset beyond .debug_info " "bounds:\n"; Die.dump(OS, 0); @@ -212,7 +270,7 @@ auto SecOffset = AttrValue.Value.getAsSectionOffset(); assert(SecOffset); // DW_FORM_strp is a section offset. if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) { - ++NumDebugInfoErrors; + ++NumUnitErrors; OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n"; Die.dump(OS, 0); OS << "\n"; @@ -224,7 +282,7 @@ } } -void DWARFVerifier::verifyDebugInfoReferences() { +void DWARFVerifier::verifyDebugInfoReferences(uint32_t &NumDebugInfoErrors) { // Take all references and make sure they point to an actual DIE by // getting the DIE by offset and emitting an error OS << "Verifying .debug_info references...\n"; @@ -244,26 +302,6 @@ } } -bool DWARFVerifier::handleDebugInfo() { - NumDebugInfoErrors = 0; - OS << "Verifying .debug_info...\n"; - for (const auto &CU : DCtx.compile_units()) { - unsigned NumDies = CU->getNumDIEs(); - for (unsigned I = 0; I < NumDies; ++I) { - auto Die = CU->getDIEAtIndex(I); - const auto Tag = Die.getTag(); - if (Tag == DW_TAG_null) - continue; - for (auto AttrValue : Die.attributes()) { - verifyDebugInfoAttribute(Die, AttrValue); - verifyDebugInfoForm(Die, AttrValue); - } - } - } - verifyDebugInfoReferences(); - return NumDebugInfoErrors == 0; -} - void DWARFVerifier::verifyDebugLineStmtOffsets() { std::map StmtListToDie; for (const auto &CU : DCtx.compile_units()) {