[PATCH] D101892: [DebugInfo] UnwindTable::create() should not add empty rows to CFI unwind table

Venkata Ramanaiah Nalamothu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 5 04:06:41 PDT 2021


RamNalamothu created this revision.
RamNalamothu added a reviewer: clayborg.
Herald added a subscriber: hiraditya.
RamNalamothu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

UnwindTable::parseRows() may return successfully if the CFIProgram has no
CFI instructions and the UnwindRow return argument will be empty but currently
the callers are not checking for this case which is leading to incorrect
dumps in the unwind tables i.e.

  CFA=unspecified


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101892

Files:
  llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp
  llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp


Index: llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp
===================================================================
--- llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp
+++ llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp
@@ -447,6 +447,45 @@
   expectDumpResult(Locs, "");
 }
 
+// Here we test that empty rows are not added to UnwindTable when
+// dwarf::CIE::CFIs or dwarf::FDE::CFIs is empty.
+TEST(DWARFDebugFrame, UnwindTableEmptyRows) {
+  dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
+                                 /*Offset=*/0x0,
+                                 /*Length=*/0xff);
+
+  // Having an empty instructions list is fine.
+  EXPECT_THAT_ERROR(parseCFI(TestCIE, {}), Succeeded());
+  EXPECT_TRUE(TestCIE.cfis().empty());
+
+  // Verify dwarf::UnwindTable::create() won't result in errors and
+  // and empty rows are not added to CIE UnwindTable.
+  Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestCIE);
+  EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
+  const size_t ExpectedNumOfRows = 0;
+  EXPECT_EQ(RowsOrErr->size(), ExpectedNumOfRows);
+
+  dwarf::FDE TestFDE(/*IsDWARF64=*/true,
+                     /*Offset=*/0x3333abcdabcd,
+                     /*Length=*/0x4444abcdabcd,
+                     /*CIEPointer=*/0x1111abcdabcd,
+                     /*InitialLocation=*/0x1000,
+                     /*AddressRange=*/0x1000,
+                     /*Cie=*/&TestCIE,
+                     /*LSDAAddress=*/None,
+                     /*Arch=*/Triple::x86_64);
+
+  // Having an empty instructions list is fine.
+  EXPECT_THAT_ERROR(parseCFI(TestFDE, {}), Succeeded());
+  EXPECT_TRUE(TestFDE.cfis().empty());
+
+  // Verify dwarf::UnwindTable::create() won't result in errors and
+  // and empty rows are not added to FDE UnwindTable.
+  RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
+  EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
+  EXPECT_EQ(RowsOrErr->size(), ExpectedNumOfRows);
+}
+
 TEST(DWARFDebugFrame, UnwindTableErrorNonAscendingFDERows) {
   dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
                                  /*Offset=*/0x0,
Index: llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp
===================================================================
--- llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp
+++ llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp
@@ -214,7 +214,9 @@
   const RegisterLocations InitialLocs = Row.getRegisterLocations();
   if (Error FdeError = UT.parseRows(Fde->cfis(), Row, &InitialLocs))
     return std::move(FdeError);
-  UT.Rows.push_back(Row);
+  // Row will be empty if there are no CFI instructions.
+  if (!Cie->cfis().empty() || !Fde->cfis().empty())
+    UT.Rows.push_back(Row);
   return UT;
 }
 
@@ -223,7 +225,9 @@
   UnwindRow Row;
   if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr))
     return std::move(CieError);
-  UT.Rows.push_back(Row);
+  // Row will be empty if there are no CFI instructions.
+  if (!Cie->cfis().empty())
+    UT.Rows.push_back(Row);
   return UT;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101892.342989.patch
Type: text/x-patch
Size: 3061 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210505/3ce708ad/attachment.bin>


More information about the llvm-commits mailing list