Skip to content

Commit c1b84d9

Browse files
committed
[DebugInfo] Add error handling to DWARFDebugAbbrev::getAbbreviationDeclarationSet
This gives us more meaningful information when `getAbbreviationDeclarationSet` fails. Right now only `verifyAbbrevSection` actually uses the error that it returns, but the other call sites could be rewritten to take advantage of the returned error. Differential Revision: https://reviews.llvm.org/D153459
1 parent 3e12b2e commit c1b84d9

File tree

5 files changed

+44
-29
lines changed

5 files changed

+44
-29
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DWARFDebugAbbrev {
6666
public:
6767
DWARFDebugAbbrev(DataExtractor Data);
6868

69-
const DWARFAbbreviationDeclarationSet *
69+
Expected<const DWARFAbbreviationDeclarationSet *>
7070
getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const;
7171

7272
void dump(raw_ostream &OS) const;

llvm/lib/DebugInfo/DWARF/DWARFDebugAbbrev.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,32 +139,30 @@ void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
139139
}
140140
}
141141

142-
const DWARFAbbreviationDeclarationSet*
142+
Expected<const DWARFAbbreviationDeclarationSet *>
143143
DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const {
144144
const auto End = AbbrDeclSets.end();
145145
if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
146-
return &(PrevAbbrOffsetPos->second);
146+
return &PrevAbbrOffsetPos->second;
147147
}
148148

149149
const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
150150
if (Pos != End) {
151151
PrevAbbrOffsetPos = Pos;
152-
return &(Pos->second);
152+
return &Pos->second;
153153
}
154154

155-
if (Data && CUAbbrOffset < Data->getData().size()) {
156-
uint64_t Offset = CUAbbrOffset;
157-
DWARFAbbreviationDeclarationSet AbbrDecls;
158-
if (Error Err = AbbrDecls.extract(*Data, &Offset)) {
159-
// FIXME: We should propagate the error upwards.
160-
consumeError(std::move(Err));
161-
return nullptr;
162-
}
163-
PrevAbbrOffsetPos =
164-
AbbrDeclSets.insert(std::make_pair(CUAbbrOffset, std::move(AbbrDecls)))
165-
.first;
166-
return &PrevAbbrOffsetPos->second;
167-
}
155+
if (!Data || CUAbbrOffset >= Data->getData().size())
156+
return make_error<llvm::object::GenericBinaryError>(
157+
"the abbreviation offset into the .debug_abbrev section is not valid");
158+
159+
uint64_t Offset = CUAbbrOffset;
160+
DWARFAbbreviationDeclarationSet AbbrDecls;
161+
if (Error Err = AbbrDecls.extract(*Data, &Offset))
162+
return std::move(Err);
168163

169-
return nullptr;
164+
PrevAbbrOffsetPos =
165+
AbbrDeclSets.insert(std::make_pair(CUAbbrOffset, std::move(AbbrDecls)))
166+
.first;
167+
return &PrevAbbrOffsetPos->second;
170168
}

llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,16 @@ DWARFUnit::getLastChildEntry(const DWARFDebugInfoEntry *Die) const {
10401040
}
10411041

10421042
const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
1043-
if (!Abbrevs)
1044-
Abbrevs = Abbrev->getAbbreviationDeclarationSet(getAbbreviationsOffset());
1043+
if (!Abbrevs) {
1044+
Expected<const DWARFAbbreviationDeclarationSet *> AbbrevsOrError =
1045+
Abbrev->getAbbreviationDeclarationSet(getAbbreviationsOffset());
1046+
if (!AbbrevsOrError) {
1047+
// FIXME: We should propagate this error upwards.
1048+
consumeError(AbbrevsOrError.takeError());
1049+
return nullptr;
1050+
}
1051+
Abbrevs = *AbbrevsOrError;
1052+
}
10451053
return Abbrevs;
10461054
}
10471055

llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,15 @@ bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
150150
AddrSize = DebugInfoData.getU8(Offset);
151151
}
152152

153-
if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
153+
Expected<const DWARFAbbreviationDeclarationSet *> AbbrevSetOrErr =
154+
DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset);
155+
if (!AbbrevSetOrErr) {
154156
ValidAbbrevOffset = false;
157+
// FIXME: A problematic debug_abbrev section is reported below in the form
158+
// of a `note:`. We should propagate this error there (or elsewhere) to
159+
// avoid losing the specific problem with the debug_abbrev section.
160+
consumeError(AbbrevSetOrErr.takeError());
161+
}
155162

156163
ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
157164
ValidVersion = DWARFContext::isSupportedVersion(Version);
@@ -302,14 +309,14 @@ unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
302309
if (!Abbrev)
303310
return 0;
304311

305-
const DWARFAbbreviationDeclarationSet *AbbrDecls =
312+
Expected<const DWARFAbbreviationDeclarationSet *> AbbrDeclsOrErr =
306313
Abbrev->getAbbreviationDeclarationSet(0);
307-
// FIXME: If we failed to get a DWARFAbbreviationDeclarationSet, it's possible
308-
// that there are errors. We need to propagate the error from
309-
// getAbbreviationDeclarationSet.
310-
if (!AbbrDecls)
311-
return 0;
314+
if (!AbbrDeclsOrErr) {
315+
error() << toString(AbbrDeclsOrErr.takeError()) << "\n";
316+
return 1;
317+
}
312318

319+
const auto *AbbrDecls = *AbbrDeclsOrErr;
313320
unsigned NumErrors = 0;
314321
for (auto AbbrDecl : *AbbrDecls) {
315322
SmallDenseSet<uint16_t> AttributeSet;

llvm/test/tools/llvm-dwarfdump/X86/empty-CU.s

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# RUN: llvm-mc %s -filetype obj -triple x86_64-apple-darwin -o - \
22
# RUN: | not llvm-dwarfdump --verify --debug-info - \
33
# RUN: | FileCheck %s
4+
# CHECK: error: unable to decode LEB128 at offset 0x00000005: malformed uleb128, extends past end
45
# CHECK: error: Compilation unit without DIE.
56

67
.section __DWARF,__debug_info,regular,debug
@@ -17,5 +18,6 @@
1718
.byte 1 # Abbrev code
1819
.byte 0x11 # TAG_compile_unit
1920
.byte 0 # no children
20-
.byte 0 # no attributes
21-
.byte 0
21+
.byte 0 # EOM(1)
22+
.byte 0 # EOM(2)
23+
# Intentionally missing EOM(3)

0 commit comments

Comments
 (0)