Skip to content

Fix crash due to un-checked error in LVReaderHandler::handleArchive method #118951

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 15 commits into from
Dec 17, 2024
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
3 changes: 3 additions & 0 deletions llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Error LVReaderHandler::handleArchive(LVReaders &Readers, StringRef Filename,
Filename.str().c_str());
}

if (Err)
return createStringError(errorToErrorCode(std::move(Err)), "%s",
Filename.str().c_str());
return Error::success();
}

Expand Down
74 changes: 74 additions & 0 deletions llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace {

const char *CodeViewClang = "test-codeview-clang.o";
const char *CodeViewMsvc = "test-codeview-msvc.o";
const char *CodeViewMsvcLib = "test-codeview-msvc.lib";
const char *CodeViewMsvcLibContentName =
"test-codeview-msvc.lib(test-codeview-msvc.o)";
const char *CodeViewPdbMsvc = "test-codeview-pdb-msvc.o";

// Helper function to get the first scope child from the given parent.
Expand Down Expand Up @@ -193,6 +196,72 @@ void checkElementPropertiesMsvcCodeview(LVReader *Reader) {
EXPECT_EQ(Lines->size(), 0x0eu);
}

// Check the logical elements basic properties (MSVC library - Codeview).
void checkElementPropertiesMsvcLibraryCodeview(LVReader *Reader) {
LVScopeRoot *Root = Reader->getScopesRoot();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that Root is non-null
ASSERT_NE(Root, nullptr);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this check in a separated patch.

LVScopeCompileUnit *CompileUnit =
static_cast<LVScopeCompileUnit *>(getFirstScopeChild(Root));
LVScopeFunction *Function =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same check here for CompileUnit and Function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this check in a separated patch.

static_cast<LVScopeFunction *>(getFirstScopeChild(CompileUnit));

EXPECT_EQ(Root->getFileFormatName(), "COFF-x86-64");
EXPECT_EQ(Root->getName(), CodeViewMsvcLibContentName);

EXPECT_EQ(CompileUnit->getBaseAddress(), 0u);
EXPECT_TRUE(CompileUnit->getProducer().starts_with("Microsoft"));
EXPECT_EQ(CompileUnit->getName(), "test.cpp");

EXPECT_EQ(Function->lineCount(), 14u);
EXPECT_EQ(Function->scopeCount(), 1u);
EXPECT_EQ(Function->symbolCount(), 3u);
EXPECT_EQ(Function->typeCount(), 0u);
EXPECT_EQ(Function->rangeCount(), 1u);

const LVLocations *Ranges = Function->getRanges();
ASSERT_NE(Ranges, nullptr);
ASSERT_EQ(Ranges->size(), 1u);
LVLocations::const_iterator IterLocation = Ranges->begin();
Copy link
Member

@CarlosAlbertoEnciso CarlosAlbertoEnciso Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the iterators for end
ASSERT_NE(IterLocation, Ranges->end());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this check in a separated patch.

LVLocation *Location = (*IterLocation);
EXPECT_STREQ(Location->getIntervalInfo().c_str(),
"{Range} Lines 2:9 [0x0000000000:0x0000000031]");

LVRange RangeList;
Function->getRanges(RangeList);

const LVRangeEntries &RangeEntries = RangeList.getEntries();
ASSERT_EQ(RangeEntries.size(), 2u);
LVRangeEntries::const_iterator IterRanges = RangeEntries.cbegin();
LVRangeEntry RangeEntry = *IterRanges;
EXPECT_EQ(RangeEntry.lower(), 0u);
EXPECT_EQ(RangeEntry.upper(), 0x31u);
EXPECT_EQ(RangeEntry.scope()->getLineNumber(), 0u);
EXPECT_EQ(RangeEntry.scope()->getName(), "foo");
EXPECT_EQ(RangeEntry.scope()->getOffset(), 0u);

++IterRanges;
RangeEntry = *IterRanges;
EXPECT_EQ(RangeEntry.lower(), 0x1bu);
EXPECT_EQ(RangeEntry.upper(), 0x28u);
EXPECT_EQ(RangeEntry.scope()->getLineNumber(), 0u);
EXPECT_EQ(RangeEntry.scope()->getName(), "foo::?");
EXPECT_EQ(RangeEntry.scope()->getOffset(), 0u);

const LVPublicNames &PublicNames = CompileUnit->getPublicNames();
ASSERT_EQ(PublicNames.size(), 1u);
LVPublicNames::const_iterator IterNames = PublicNames.cbegin();
LVScope *Foo = (*IterNames).first;
EXPECT_EQ(Foo->getName(), "foo");
EXPECT_EQ(Foo->getLineNumber(), 0u);
LVNameInfo NameInfo = (*IterNames).second;
EXPECT_EQ(NameInfo.first, 0u);
EXPECT_EQ(NameInfo.second, 0x31u);

// Lines (debug and assembler) for 'foo'.
const LVLines *Lines = Foo->getLines();
ASSERT_NE(Lines, nullptr);
EXPECT_EQ(Lines->size(), 0x0eu);
}

// Check the logical elements basic properties (MSVC - PDB).
void checkElementPropertiesMsvcCodeviewPdb(LVReader *Reader) {
LVScopeRoot *Root = Reader->getScopesRoot();
Expand Down Expand Up @@ -370,6 +439,11 @@ void elementProperties(SmallString<128> &InputsDir) {
createReader(ReaderHandler, InputsDir, CodeViewMsvc);
checkElementPropertiesMsvcCodeview(Reader.get());
}
{
std::unique_ptr<LVReader> Reader =
createReader(ReaderHandler, InputsDir, CodeViewMsvcLib);
checkElementPropertiesMsvcLibraryCodeview(Reader.get());
}
{
std::unique_ptr<LVReader> Reader =
createReader(ReaderHandler, InputsDir, CodeViewPdbMsvc);
Expand Down
47 changes: 47 additions & 0 deletions llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
LogicalView unit tests input files generation
=============================================

# Source file: test.cpp

```
using INTPTR = const int *;
int foo(INTPTR ParamPtr, unsigned ParamUnsigned, bool ParamBool) {
if (ParamBool) {
typedef int INTEGER;
const INTEGER CONSTANT = 7;
return CONSTANT;
}
return ParamUnsigned;
}
```

# Linux binary files:

## test-dwarf-clang.o

```clang -c -w -g -gdwarf-4 -O0 test.cpp -o test-dwarf-clang.o```

## test-dwarf-gcc.o

```g++ -c -w -g -O0 test.cpp -o test-dwarf-gcc.o```

# Windows binary files:

## test-codeview-clang.o

```clang++.exe --target=x86_64-windows -c -w -g -gcodeview test.cpp -o test-codeview-clang.o```

## test-codeview-msvc.o

```cl.exe /nologo /EHsc /Od /Z7 /Fotest-codeview-msvc.o /c test.cpp```

## test-codeview-pdb-msvc.o and test-codeview-pdb-msvc-.pdb

```cl.exe /nologo /EHsc /Od /Zi /Fotest-codeview-pdb-msvc.o /Fdtest-codeview-pdb-msvc-.pdb /c test.cpp```

## test-codeview-msvc.lib

This file is a static library embedding the **test-codeview-msvc.o** object file.
It is generated by the "lib.exe" tool shipped with MSVC compiler.

```lib.exe /OUT:test-codeview-msvc.lib test-codeview-msvc.o```
Binary file not shown.
Loading