[llvm] r326995 - [DWARF] Don't attempt to parse line tables at invalid offsets

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 8 02:53:34 PST 2018


Author: jhenderson
Date: Thu Mar  8 02:53:34 2018
New Revision: 326995

URL: http://llvm.org/viewvc/llvm-project?rev=326995&view=rev
Log:
[DWARF] Don't attempt to parse line tables at invalid offsets

Whilst working on improvements to the error handling of the debug line
parsing code, I noticed that if an invalid offset were to be specified
in a call to getOrParseLineTable(), an entry in the LineTableMap would
still be created, even if the offset was not within the section range.
The immediate parsing attempt afterwards would fail (it would end up
getting a version of 0), and thereafter, any subsequent calls to
getOrParseLineTable or getLineTable would return the default-
constructed, invalid line table. In reality, we shouldn't even attempt
to parse this table, and we should always return a nullptr from these
two functions for this situation.

I have tested this via a unit test, which required some new framework
for unit testing debug line. My plan is to add quite a few more unit
tests for the new error reporting mechanism that will follow shortly,
hence the reason why the supporting code for the tests are written the
way they are - I intend to extend the DwarfGenerator class to support
generating debug line. At that point, I'll make sure that there are a
few positive test cases for this and the parsing code too.

Differential Revision: https://reviews.llvm.org/D44200

Reviewers: JDevlieghere, aprantl

Added:
    llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp
    llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.cpp
    llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.h
Modified:
    llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
    llvm/trunk/unittests/DebugInfo/DWARF/CMakeLists.txt
    llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp?rev=326995&r1=326994&r2=326995&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp Thu Mar  8 02:53:34 2018
@@ -449,6 +449,9 @@ const DWARFDebugLine::LineTable *
 DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
                                     uint32_t Offset, const DWARFContext &Ctx,
                                     const DWARFUnit *U) {
+  if (!DebugLineData.isValidOffset(Offset))
+    return nullptr;
+
   std::pair<LineTableIter, bool> Pos =
       LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
   LineTable *LT = &Pos.first->second;

Modified: llvm/trunk/unittests/DebugInfo/DWARF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/DWARF/CMakeLists.txt?rev=326995&r1=326994&r2=326995&view=diff
==============================================================================
--- llvm/trunk/unittests/DebugInfo/DWARF/CMakeLists.txt (original)
+++ llvm/trunk/unittests/DebugInfo/DWARF/CMakeLists.txt Thu Mar  8 02:53:34 2018
@@ -10,7 +10,9 @@ set(LLVM_LINK_COMPONENTS
 
 set(DebugInfoSources
   DwarfGenerator.cpp
+  DwarfUtils.cpp
   DWARFDebugInfoTest.cpp
+  DWARFDebugLineTest.cpp
   DWARFFormValueTest.cpp
   )
 

Modified: llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp?rev=326995&r1=326994&r2=326995&view=diff
==============================================================================
--- llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp (original)
+++ llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp Thu Mar  8 02:53:34 2018
@@ -1,4 +1,4 @@
-//===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
+//===- llvm/unittest/DebugInfo/DWARFDebugInfoTest.cpp ---------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "DwarfGenerator.h"
+#include "DwarfUtils.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallString.h"
@@ -36,36 +37,10 @@
 
 using namespace llvm;
 using namespace dwarf;
+using namespace utils;
 
 namespace {
 
-void initLLVMIfNeeded() {
-  static bool gInitialized = false;
-  if (!gInitialized) {
-    gInitialized = true;
-    InitializeAllTargets();
-    InitializeAllTargetMCs();
-    InitializeAllAsmPrinters();
-    InitializeAllAsmParsers();
-  }
-}
-
-Triple getHostTripleForAddrSize(uint8_t AddrSize) {
-  Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
-
-  if (AddrSize == 8 && PT.isArch32Bit())
-    return PT.get64BitArchVariant();
-  if (AddrSize == 4 && PT.isArch64Bit())
-    return PT.get32BitArchVariant();
-  return PT;
-}
-
-static bool isConfigurationSupported(Triple &T) {
-  initLLVMIfNeeded();
-  std::string Err;
-  return TargetRegistry::lookupTarget(T.getTriple(), Err);
-}
-
 template <uint16_t Version, class AddrType, class RefAddrType>
 void TestAllForms() {
   Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));

Added: llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp?rev=326995&view=auto
==============================================================================
--- llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp (added)
+++ llvm/trunk/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp Thu Mar  8 02:53:34 2018
@@ -0,0 +1,64 @@
+//===- DWARFDebugLineTest.cpp ---------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DwarfGenerator.h"
+#include "DwarfUtils.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace dwarf;
+using namespace object;
+using namespace utils;
+
+namespace {
+
+struct DebugLineGenerator {
+  bool init() {
+    Triple T = getHostTripleForAddrSize(8);
+    if (!isConfigurationSupported(T))
+      return false;
+    auto ExpectedGenerator = dwarfgen::Generator::create(T, 4);
+    if (ExpectedGenerator)
+      Generator.reset(ExpectedGenerator->release());
+    return true;
+  }
+
+  std::unique_ptr<DWARFContext> createContext() {
+    if (!Generator)
+      return nullptr;
+    StringRef FileBytes = Generator->generate();
+    MemoryBufferRef FileBuffer(FileBytes, "dwarf");
+    auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
+    if (Obj)
+      return DWARFContext::create(**Obj);
+    return nullptr;
+  }
+
+  std::unique_ptr<dwarfgen::Generator> Generator;
+};
+
+TEST(DWARFDebugLine, GetLineTableAtInvalidOffset) {
+  DebugLineGenerator LineGen;
+  if (!LineGen.init())
+    return;
+
+  DWARFDebugLine Line;
+  std::unique_ptr<DWARFContext> Context = LineGen.createContext();
+  ASSERT_TRUE(Context != nullptr);
+  const DWARFObject &Obj = Context->getDWARFObj();
+  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), true, 8);
+
+  EXPECT_EQ(Line.getOrParseLineTable(LineData, 0, *Context, nullptr), nullptr);
+}
+
+} // end anonymous namespace

Added: llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.cpp?rev=326995&view=auto
==============================================================================
--- llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.cpp (added)
+++ llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.cpp Thu Mar  8 02:53:34 2018
@@ -0,0 +1,42 @@
+//===--- unittests/DebugInfo/DWARF/DwarfUtils.cpp ---------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DwarfUtils.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+
+using namespace llvm;
+
+static void initLLVMIfNeeded() {
+  static bool gInitialized = false;
+  if (!gInitialized) {
+    gInitialized = true;
+    InitializeAllTargets();
+    InitializeAllTargetMCs();
+    InitializeAllAsmPrinters();
+    InitializeAllAsmParsers();
+  }
+}
+
+Triple llvm::dwarf::utils::getHostTripleForAddrSize(uint8_t AddrSize) {
+  Triple T(Triple::normalize(LLVM_HOST_TRIPLE));
+
+  if (AddrSize == 8 && T.isArch32Bit())
+    return T.get64BitArchVariant();
+  if (AddrSize == 4 && T.isArch64Bit())
+    return T.get32BitArchVariant();
+  return T;
+}
+
+bool llvm::dwarf::utils::isConfigurationSupported(Triple &T) {
+  initLLVMIfNeeded();
+  std::string Err;
+  return TargetRegistry::lookupTarget(T.getTriple(), Err);
+}

Added: llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.h?rev=326995&view=auto
==============================================================================
--- llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.h (added)
+++ llvm/trunk/unittests/DebugInfo/DWARF/DwarfUtils.h Thu Mar  8 02:53:34 2018
@@ -0,0 +1,29 @@
+//===--- unittests/DebugInfo/DWARF/DwarfUtils.h -----------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFUTILS_H
+#define LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFUTILS_H
+
+#include <cstdint>
+
+namespace llvm {
+
+class Triple;
+
+namespace dwarf {
+namespace utils {
+
+Triple getHostTripleForAddrSize(uint8_t AddrSize);
+bool isConfigurationSupported(Triple &T);
+
+} // end namespace utils
+} // end namespace dwarf
+} // end namespace llvm
+
+#endif // LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFUTILS_H




More information about the llvm-commits mailing list