Skip to content

[llvm-debuginfo-analyzer] Fix a couple of unhandled DWARF situations leading to a crash #137221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion llvm/lib/DebugInfo/LogicalView/Core/LVScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,16 @@ void LVScope::addMissingElements(LVScope *Reference) {
Symbol->setIsOptimized();
Symbol->setReference(Reference);

// The symbol can be a constant, parameter or variable.
// The symbol can be a constant, parameter, variable or unspecified
// parameters (i.e. `...`).
if (Reference->getIsConstant())
Symbol->setIsConstant();
else if (Reference->getIsParameter())
Symbol->setIsParameter();
else if (Reference->getIsVariable())
Symbol->setIsVariable();
else if (Reference->getIsUnspecified())
Symbol->setIsUnspecified();
else
llvm_unreachable("Invalid symbol kind.");
}
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/DebugInfo/LogicalView/Readers/LVBinaryReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,15 @@ Error LVBinaryReader::createInstructions(LVScope *Scope,

ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(*SectionContentsOrErr);
uint64_t Offset = Address - SectionAddress;
if (Offset > Bytes.size()) {
LLVM_DEBUG({
dbgs() << "offset (" << hexValue(Offset) << ") is beyond section size ("
<< hexValue(Bytes.size()) << "); malformed input?\n";
});
return createStringError(
errc::bad_address,
"Failed to parse instructions; offset beyond section size");
}
uint8_t const *Begin = Bytes.data() + Offset;
uint8_t const *End = Bytes.data() + Offset + Size;

Expand Down
39 changes: 38 additions & 1 deletion llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ extern const char *TestMainArgv0;
namespace {

const char *DwarfClang = "test-dwarf-clang.o";
// Two compile units: one declares `extern int foo_printf(const char *, ...);`
// and another one that defines the function.
const char *DwarfClangUnspecParams = "test-dwarf-clang-unspec-params.elf";
const char *DwarfGcc = "test-dwarf-gcc.o";

// Helper function to get the first compile unit.
LVScopeCompileUnit *getFirstCompileUnit(LVScopeRoot *Root) {
EXPECT_NE(Root, nullptr);
const LVScopes *CompileUnits = Root->getScopes();
EXPECT_NE(CompileUnits, nullptr);
EXPECT_EQ(CompileUnits->size(), 1u);
EXPECT_GT(CompileUnits->size(), 0u);

LVScopes::const_iterator Iter = CompileUnits->begin();
EXPECT_NE(Iter, nullptr);
Expand Down Expand Up @@ -124,6 +127,36 @@ void checkElementProperties(LVReader *Reader) {
ASSERT_EQ(Lines->size(), 0x12u);
}

// Check proper handling of DW_AT_unspecified_parameters in
// LVScope::addMissingElements().
void checkUnspecifiedParameters(LVReader *Reader) {
LVScopeRoot *Root = Reader->getScopesRoot();
LVScopeCompileUnit *CompileUnit = getFirstCompileUnit(Root);

EXPECT_EQ(Root->getFileFormatName(), "elf64-x86-64");
EXPECT_EQ(Root->getName(), DwarfClangUnspecParams);

const LVPublicNames &PublicNames = CompileUnit->getPublicNames();
ASSERT_EQ(PublicNames.size(), 1u);

LVPublicNames::const_iterator IterNames = PublicNames.cbegin();
LVScope *Function = (*IterNames).first;
EXPECT_EQ(Function->getName(), "foo_printf");
const LVElements *Elements = Function->getChildren();
ASSERT_NE(Elements, nullptr);
// foo_printf is a variadic function whose prototype is
// `int foo_printf(const char *, ...)`, where the '...' is represented by a
// DW_TAG_unspecified_parameters, i.e. we expect to find at least one child
// for which getIsUnspecified() returns true.
EXPECT_EQ(std::any_of(
Elements->begin(), Elements->end(),
[](const LVElement *elt) {
return elt->getIsSymbol() &&
static_cast<const LVSymbol *>(elt)->getIsUnspecified();
}),
true);
}

// Check the logical elements selection.
void checkElementSelection(LVReader *Reader) {
LVScopeRoot *Root = Reader->getScopesRoot();
Expand Down Expand Up @@ -253,6 +286,7 @@ void elementProperties(SmallString<128> &InputsDir) {
ReaderOptions.setAttributePublics();
ReaderOptions.setAttributeRange();
ReaderOptions.setAttributeLocation();
ReaderOptions.setAttributeInserted();
ReaderOptions.setPrintAll();
ReaderOptions.resolveDependencies();

Expand All @@ -264,6 +298,9 @@ void elementProperties(SmallString<128> &InputsDir) {
std::unique_ptr<LVReader> Reader =
createReader(ReaderHandler, InputsDir, DwarfClang);
checkElementProperties(Reader.get());

Reader = createReader(ReaderHandler, InputsDir, DwarfClangUnspecParams);
checkUnspecifiedParameters(Reader.get());
}

// Logical elements selection.
Expand Down
Binary file not shown.