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

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 12 13:02:25 PST 2024


https://github.com/aurelien35 updated https://github.com/llvm/llvm-project/pull/118951

>From 40c944b8166446682bd4f747408faeeda5e18bbf Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Fri, 6 Dec 2024 11:27:31 +0100
Subject: [PATCH 1/9] Update LVReaderHandler.cpp

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

Correction thanks to @CarlosAlbertoEnciso suggestion
---
 llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp b/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
index 71750f3d114c11..7d64100c60c21a 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();
 }
 

>From 4f2e0945f3ece9b7218aafb76eeb2c33fc270b0f Mon Sep 17 00:00:00 2001
From: Your Name <you at example.com>
Date: Mon, 9 Dec 2024 22:32:43 +0100
Subject: [PATCH 2/9] Add MSVC CodeView informations read from library test
 case

---
 .../LogicalView/CodeViewReaderTest.cpp        |  73 ++++++++++++++++++
 .../LogicalView/Inputs/test-codeview-msvc.lib | Bin 0 -> 3026 bytes
 2 files changed, 73 insertions(+)
 create mode 100644 llvm/unittests/DebugInfo/LogicalView/Inputs/test-codeview-msvc.lib

diff --git a/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp b/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
index 125bc17a006711..c05114541de03c 100644
--- a/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
+++ b/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
@@ -31,6 +31,8 @@ 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 +195,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 +438,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/test-codeview-msvc.lib b/llvm/unittests/DebugInfo/LogicalView/Inputs/test-codeview-msvc.lib
new file mode 100644
index 0000000000000000000000000000000000000000..f0f94238d4118e75883b6feaac8b21924e55624e
GIT binary patch
literal 3026
zcmcIm&2Jl35TA|nRkulPLqk+Pv<<F^QN7)bjnky1IEkG)s$*O6wuE-2*xB7UD{Qav
zuANXJ5vk&%aOnZ{QVE2_0r3YQq^bl7P>*onL!}oE9FPKsq)12zQD)Y=j!i=Vm5x07
z=KbEh`FJ1Wcj!dEDleaOA7$G~7!8F+ks_n*IB$1Hnt09)01W;-&n)SBH2T~`A`_oX
z%q*m$=ioov4TT(kIX4;w4_USx>}dTjVgF;)!nSo&HO!!_D{5U+SAr#@E^|7@>m+C^
zHY`S-5{@}D8?C_ekw}DPM<Qc(#5uR}Y7a<X)EC!#0S;iyM4VDpWwWj}dH_dJI6?`v
zlG}rN>SC?n&pL3f1K>RBU2`>%Zfv69ndD4$)%2Ijj@}3gzuGzu#zH2?ttxpl?;rpe
z5(GN8`y at qyk8+iEy?uxasQ0w3OQtQoMtma8BRYcP=kKzI at yW~28<B8B3BSjXUnA$O
zgeL}9 at n|eQJ#i)}CTAwchww8sD-N}T>0~T5F&obgvE*qJ`2*w;V7z2)bzGu7G$gHD
zshLtiEvwah(ZcT9*Ks)EWpV&t!UOOqtk<B~&T4X1H}oZw9i9uY>58e9v<q6fz)tC<
zidIys-~+#RVWBQB<V~}xE!NN}Jc!t`TCAwmR<s+ at yjcWl4O77wDdz8x18~ZU8AXd|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!-sTT3{_F0>1;XDkm&o6Em}Iz`V7MVCY(hNA+iYKk2k1kkQ0-o
w<2>3#8l!An3_Xk5sr5lbe*-Rto<!}G#O at ByTYs&LyRP_m8nT{kJB_&i1R^(VLI3~&

literal 0
HcmV?d00001


>From 3212dae9977dd3e24450d3d353be421851fcb26a Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Wed, 11 Dec 2024 07:56:47 +0100
Subject: [PATCH 3/9] Create README for LogicalView unit tests inputs
 generation

---
 llvm/unittests/DebugInfo/LogicalView/Inputs/README | 10 ++++++++++
 1 file changed, 10 insertions(+)
 create mode 100644 llvm/unittests/DebugInfo/LogicalView/Inputs/README

diff --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/README b/llvm/unittests/DebugInfo/LogicalView/Inputs/README
new file mode 100644
index 00000000000000..5874ffafe02313
--- /dev/null
+++ b/llvm/unittests/DebugInfo/LogicalView/Inputs/README
@@ -0,0 +1,10 @@
+LogicalView unit tests input files generation
+=============================================
+
+***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.*```
+

>From a64d550263db420bbd84ae76832dc95a4026c638 Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Wed, 11 Dec 2024 07:57:46 +0100
Subject: [PATCH 4/9] Update README

---
 llvm/unittests/DebugInfo/LogicalView/Inputs/README | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/README b/llvm/unittests/DebugInfo/LogicalView/Inputs/README
index 5874ffafe02313..545e81da157219 100644
--- a/llvm/unittests/DebugInfo/LogicalView/Inputs/README
+++ b/llvm/unittests/DebugInfo/LogicalView/Inputs/README
@@ -6,5 +6,5 @@ LogicalView unit tests input files generation
 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.*```
+```lib.exe /OUT:test-codeview-msvc.lib test-codeview-msvc.o```
 

>From d86c8ddf837fe693dc9c4aa3c96e6d8c93a20d4b Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Wed, 11 Dec 2024 07:59:57 +0100
Subject: [PATCH 5/9] Use markdown extension for README file

---
 llvm/unittests/DebugInfo/LogicalView/Inputs/README.md | 9 +++++++++
 1 file changed, 9 insertions(+)
 create mode 100644 llvm/unittests/DebugInfo/LogicalView/Inputs/README.md

diff --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md b/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
new file mode 100644
index 00000000000000..62875a1961d37b
--- /dev/null
+++ b/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
@@ -0,0 +1,9 @@
+LogicalView unit tests input files generation
+=============================================
+
+# 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```

>From 81ba620841c718faaf74c671eba4edbdb17e1dc6 Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Wed, 11 Dec 2024 08:01:23 +0100
Subject: [PATCH 6/9] Delete llvm/unittests/DebugInfo/LogicalView/Inputs/README

---
 llvm/unittests/DebugInfo/LogicalView/Inputs/README | 10 ----------
 1 file changed, 10 deletions(-)
 delete mode 100644 llvm/unittests/DebugInfo/LogicalView/Inputs/README

diff --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/README b/llvm/unittests/DebugInfo/LogicalView/Inputs/README
deleted file mode 100644
index 545e81da157219..00000000000000
--- a/llvm/unittests/DebugInfo/LogicalView/Inputs/README
+++ /dev/null
@@ -1,10 +0,0 @@
-LogicalView unit tests input files generation
-=============================================
-
-***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```
-

>From ae340e399f9cbb733a6ce26c06cb496997b572cc Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Wed, 11 Dec 2024 08:05:42 +0100
Subject: [PATCH 7/9] Fix CodeViewReaderTest.cpp code formatting

---
 llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp b/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
index c05114541de03c..7639f001e30dd7 100644
--- a/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
+++ b/llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp
@@ -32,7 +32,8 @@ 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 *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.

>From f164a866b55e695e4055960d35b36456d4279292 Mon Sep 17 00:00:00 2001
From: aurelien35 <aurelien35 at users.noreply.github.com>
Date: Wed, 11 Dec 2024 08:07:24 +0100
Subject: [PATCH 8/9] Fix LVReaderHandler.cpp code formatting

---
 llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp b/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
index 7d64100c60c21a..69513f2b9882cb 100644
--- a/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
+++ b/llvm/lib/DebugInfo/LogicalView/LVReaderHandler.cpp
@@ -90,7 +90,7 @@ Error LVReaderHandler::handleArchive(LVReaders &Readers, StringRef Filename,
 
   if (Err)
     return createStringError(errorToErrorCode(std::move(Err)), "%s",
-                               Filename.str().c_str());
+                             Filename.str().c_str());
   return Error::success();
 }
 

>From 1944610fedfc0d24340267600e36963293866497 Mon Sep 17 00:00:00 2001
From: Your Name <you at example.com>
Date: Thu, 12 Dec 2024 22:01:56 +0100
Subject: [PATCH 9/9] Update LogicalView unit tests inputs README file

---
 .../DebugInfo/LogicalView/Inputs/README.md    | 42 ++++++++++++++++++-
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md b/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
index 62875a1961d37b..ab194ffc1010c4 100644
--- a/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
+++ b/llvm/unittests/DebugInfo/LogicalView/Inputs/README.md
@@ -1,9 +1,47 @@
 LogicalView unit tests input files generation
 =============================================
 
-# test-codeview-msvc.lib
+# 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:
+It is generated by the "lib.exe" tool shipped with MSVC compiler.
 
 ```lib.exe /OUT:test-codeview-msvc.lib test-codeview-msvc.o```



More information about the llvm-commits mailing list