Index: include/llvm/DebugInfo/DWARF/DWARFDie.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFDie.h +++ include/llvm/DebugInfo/DWARF/DWARFDie.h @@ -42,15 +42,17 @@ /// DWARFUnit for each call. class DWARFDie { DWARFUnit *U = nullptr; - const DWARFDebugInfoEntry *Die = nullptr; + ArrayRef Die; public: DWARFDie() = default; - DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry * D) : U(Unit), Die(D) {} + DWARFDie(DWARFUnit *Unit, ArrayRef D) + : U(Unit), Die(D) {} + DWARFDie(DWARFUnit *Unit, DWARFDebugInfoEntry *D) = delete; - bool isValid() const { return U && Die; } + bool isValid() const { return U && !Die.empty(); } explicit operator bool() const { return isValid(); } - const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die; } + const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die.data(); } DWARFUnit *getDwarfUnit() const { return U; } /// Get the abbreviation declaration for this DIE. @@ -58,7 +60,7 @@ /// \returns the abbreviation declaration or NULL for null tags. const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const { assert(isValid() && "must check validity prior to calling"); - return Die->getAbbreviationDeclarationPtr(); + return Die[0].getAbbreviationDeclarationPtr(); } /// Get the absolute offset into the debug info or types section. @@ -66,7 +68,7 @@ /// \returns the DIE offset or -1U if invalid. uint32_t getOffset() const { assert(isValid() && "must check validity prior to calling"); - return Die->getOffset(); + return Die[0].getOffset(); } dwarf::Tag getTag() const { @@ -78,7 +80,7 @@ bool hasChildren() const { assert(isValid() && "must check validity prior to calling"); - return Die->hasChildren(); + return Die[0].hasChildren(); } /// Returns true for a valid DIE that terminates a sibling chain. @@ -109,8 +111,8 @@ /// \returns a valid DWARFDie instance if this object has children or an /// invalid DWARFDie instance if it doesn't. DWARFDie getFirstChild() const { - if (isValid() && Die->hasChildren()) - return DWARFDie(U, Die + 1); + if (isValid() && Die[0].hasChildren()) + return DWARFDie(U, Die.drop_front(1)); return DWARFDie(); } Index: include/llvm/DebugInfo/DWARF/DWARFUnit.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFUnit.h +++ include/llvm/DebugInfo/DWARF/DWARFUnit.h @@ -330,7 +330,7 @@ extractDIEsIfNeeded(ExtractUnitDIEOnly); if (DieArray.empty()) return DWARFDie(); - return DWARFDie(this, &DieArray[0]); + return DWARFDie(this, DieArray); } const char *getCompilationDir(); @@ -367,7 +367,7 @@ /// \brief Return the DIE object at the given index. DWARFDie getDIEAtIndex(unsigned Index) { assert(Index < DieArray.size()); - return DWARFDie(this, &DieArray[Index]); + return DWARFDie(this, {DieArray.data() + Index, DieArray.size() - Index}); } DWARFDie getParent(const DWARFDebugInfoEntry *Die); @@ -385,9 +385,10 @@ [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) { return LHS.getOffset() < Offset; }); - if (it != DieArray.end() && it->getOffset() == Offset) - return DWARFDie(this, &*it); - return DWARFDie(); + if (it == DieArray.end() || it->getOffset() != Offset) + return DWARFDie(); + size_t Ndx = it - DieArray.begin(); + return DWARFDie(this, {DieArray.data() + Ndx, DieArray.size() - Ndx}); } uint32_t getLineTableOffset() const { Index: lib/DebugInfo/DWARF/DWARFDie.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDie.cpp +++ lib/DebugInfo/DWARF/DWARFDie.cpp @@ -501,13 +501,13 @@ DWARFDie DWARFDie::getParent() const { if (isValid()) - return U->getParent(Die); + return U->getParent(&Die.front()); return DWARFDie(); } DWARFDie DWARFDie::getSibling() const { if (isValid()) - return U->getSibling(Die); + return U->getSibling(&Die.front()); return DWARFDie(); } Index: lib/DebugInfo/DWARF/DWARFUnit.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFUnit.cpp +++ lib/DebugInfo/DWARF/DWARFUnit.cpp @@ -425,7 +425,7 @@ const uint32_t ParentDepth = Depth - 1; for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) { if (DieArray[I].getDepth() == ParentDepth) - return DWARFDie(this, &DieArray[I]); + return DWARFDie(this, {DieArray.data() + I, DieArray.size() - I}); } return DWARFDie(); } @@ -445,7 +445,7 @@ for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx; ++I) { if (DieArray[I].getDepth() == Depth) - return DWARFDie(this, &DieArray[I]); + return DWARFDie(this, {DieArray.data() + I, DieArray.size() - I}); } return DWARFDie(); } Index: test/tools/llvm-dwarfdump/X86/verify_debug_info2.s =================================================================== --- test/tools/llvm-dwarfdump/X86/verify_debug_info2.s +++ test/tools/llvm-dwarfdump/X86/verify_debug_info2.s @@ -0,0 +1,42 @@ +# RUN: llvm-mc %s -filetype obj -triple=i686-pc-linux -o %t +# RUN: not llvm-dwarfdump -v -verify %t 2>&1 | FileCheck %s +# CHECK: The length for this unit is too large for the .debug_info provided. + +## Check we do not crash when trying to parse truncated .debug_info. +.section .debug_info,"",@progbits + .long 0x1c + .value 0x4 + .long .Ldebug_abbrev0 + .byte 0x4 + + .uleb128 0x1 # DW_TAG_compile_unit [1] * + .long 0 # DW_AT_producer [DW_FORM_strp] ( .debug_str[0x00000000] = "test") + .byte 0x4 # DW_AT_language [DW_FORM_data1] (DW_LANG_C_plus_plus) + .long 0 # DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000000] = "test") + .long 0 # DW_AT_comp_dir [DW_FORM_strp] ( .debug_str[0x00000000] = "test") + .long 0 # DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000) + .long 0 # DW_AT_high_pc [DW_FORM_data4] (0x00000000) + +.section .debug_abbrev,"",@progbits +.Ldebug_abbrev0: + .uleb128 0x1 + .uleb128 0x11 # DW_TAG_compile_unit, DW_CHILDREN_yes + .byte 0x1 + .uleb128 0x25 # DW_AT_producer, DW_FORM_strp + .uleb128 0xe + .uleb128 0x13 # DW_AT_language, DW_FORM_data1 + .uleb128 0xb + .uleb128 0x3 # DW_AT_name, DW_FORM_strp + .uleb128 0xe + .uleb128 0x1b # DW_AT_comp_dir, DW_FORM_strp + .uleb128 0xe + .uleb128 0x11 # DW_AT_low_pc, DW_FORM_addr + .uleb128 0x1 + .uleb128 0x12 # DW_AT_high_pc, DW_FORM_data4 + .uleb128 0x6 + .byte 0 + .byte 0 + .byte 0 + +.section .debug_str,"MS",@progbits,1 +.string "test" Index: tools/llvm-dwarfdump/llvm-dwarfdump.cpp =================================================================== --- tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -280,9 +280,9 @@ /// Print only DIEs that have a certain name. static void filterByName(const StringSet<> &Names, DWARFContext::cu_iterator_range CUs, raw_ostream &OS) { - for (const auto &CU : CUs) - for (const auto &Entry : CU->dies()) { - DWARFDie Die = {CU.get(), &Entry}; + for (const auto &CU : CUs) { + for (unsigned I = 0, E = CU->getNumDIEs(); I != E; ++I) { + DWARFDie Die = CU->getDIEAtIndex(I); if (const char *NamePtr = Die.getName(DINameKind::ShortName)) { std::string Name = (IgnoreCase && !UseRegex) ? StringRef(NamePtr).lower() : NamePtr; @@ -303,6 +303,7 @@ Die.dump(OS, 0, getDumpOpts()); } } + } } bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,