[llvm] 37e48e4 - Fix crash due to un-checked error in LVReaderHandler::handleArchive method (#118951)

via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 17 03:19:51 PST 2024


Author: aurelien35
Date: 2024-12-17T11:19:48Z
New Revision: 37e48e4a7360a6faf1b157e843160d9e65223890

URL: https://github.com/llvm/llvm-project/commit/37e48e4a7360a6faf1b157e843160d9e65223890
DIFF: https://github.com/llvm/llvm-project/commit/37e48e4a7360a6faf1b157e843160d9e65223890.diff

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

[llvm-debuginfo-analyzer] Fix crash due to un-checked error in LVReaderHandler::handleArchive
method.

- Added README describing how to generated the binary files used for the test.
- A follow up patch to add extra ASSERT_NE

Committed on behalf of @aurelien35

Added: 
    llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
    llvm/unittests/DebugInfo/LogicalView/Inputs/test-codeview-msvc.lib

Modified: 
    llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
    llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp b/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
index 71750f3d114c11..69513f2b9882cb 100644
--- a/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
+++ b/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
@@ -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();
 }
 

diff  --git a/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp b/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
index 125bc17a006711..7639f001e30dd7 100644
--- a/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
+++ b/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
@@ -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.
@@ -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();
+  LVScopeCompileUnit *CompileUnit =
+      static_cast<LVScopeCompileUnit *>(getFirstScopeChild(Root));
+  LVScopeFunction *Function =
+      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();
+  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();
@@ -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);

diff  --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md b/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
new file mode 100644
index 00000000000000..ab194ffc1010c4
--- /dev/null
+++ b/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
@@ -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```

diff  --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/test-codeview-msvc.lib b/llvm/unittests/DebugInfo/LogicalView/Inputs/test-codeview-msvc.lib
new file mode 100644
index 00000000000000..f0f94238d4118e
Binary files /dev/null and b/llvm/unittests/DebugInfo/LogicalView/Inputs/test-codeview-msvc.lib 
diff er


        


More information about the llvm-commits mailing list