[Lldb-commits] [PATCH] D72917: [lldb/DWARF] Change how we construct a llvm::DWARFContext

Pavel Labath via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Jan 17 05:32:23 PST 2020


labath created this revision.
labath added reviewers: JDevlieghere, aprantl.
Herald added a project: LLDB.

The goal of this patch is two-fold. First, it fixes a use-after-free in
the construction of the llvm DWARFContext. This happened because the
construction code was throwing away the lldb DataExtractors it got while
reading the sections (unlike their llvm counterparts, these are also
responsible for memory ownership). In most cases this did not matter,
because the sections are just slices of the mmapped file data. But this
isn't the case for compressed elf sections, in which case the section is
decompressed into a heap buffer. A similar thing also happen with object
files which are loaded from process memory.

The second goal is to make it explicit which sections go into the llvm
DWARFContext -- any access to the sections through both DWARF parsers
carries a risk of parsing things twice, so it's better if this is a
conscious decision. Also, this avoids loading completely irrelevant
sections (e.g. .text). At present, the only section that needs to be
present in the llvm DWARFContext is the debug_line_str. Using it through
both APIs is not a problem, as there is no parsing involved.

The first goal is achieved by loading the sections through the existing
lldb DWARFContext APIs, which already do the caching. The second by
explicitly enumerating the sections we wish to load.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72917

Files:
  lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
  lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp


Index: lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp
===================================================================
--- lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp
+++ lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp
@@ -3,12 +3,15 @@
 
 // REQUIRES: lld, zlib
 
-// RUN: %clang -g -c -o %t.o --target=x86_64-pc-linux -mllvm -accel-tables=Dwarf %s
+// RUN: %clang -c -o %t.o --target=x86_64-pc-linux -gdwarf-5 -gpubnames %s
 // RUN: ld.lld %t.o -o %t --compress-debug-sections=zlib
+// RUN: llvm-readobj --sections %t | FileCheck %s --check-prefix NAMES
 // RUN: lldb-test symbols --find=variable --name=foo %t | FileCheck %s
 
+// NAMES: Name: .debug_names
+
 // CHECK: Found 1 variables:
 int foo;
-// ONE-DAG: name = "foo", type = {{.*}} (int), {{.*}} decl = debug-names-compressed.cpp:[[@LINE-1]]
+// CHECK-DAG: name = "foo", type = {{.*}} (int), {{.*}} decl = debug-names-compressed.cpp:[[@LINE-1]]
 
 extern "C" void _start() {}
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
@@ -117,32 +117,17 @@
   if (!m_llvm_context) {
     llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map;
     uint8_t addr_size = 0;
-
-    auto AddSection = [&](Section &section) {
-      DataExtractor section_data;
-      section.GetSectionData(section_data);
-
+    auto AddSection = [&](llvm::StringRef name, DWARFDataExtractor data) {
       // Set the address size the first time we see it.
       if (addr_size == 0)
-        addr_size = section_data.GetByteSize();
+        addr_size = data.GetAddressByteSize();
 
-      llvm::StringRef data = llvm::toStringRef(section_data.GetData());
-      llvm::StringRef name = section.GetName().GetStringRef();
-      if (name.startswith("."))
-        name = name.drop_front();
       section_map.try_emplace(
-          name, llvm::MemoryBuffer::getMemBuffer(data, name, false));
+          name, llvm::MemoryBuffer::getMemBuffer(toStringRef(data.GetData()),
+                                                 name, false));
     };
 
-    if (m_main_section_list) {
-      for (auto &section : *m_main_section_list)
-        AddSection(*section);
-    }
-
-    if (m_dwo_section_list) {
-      for (auto &section : *m_dwo_section_list)
-        AddSection(*section);
-    }
+    AddSection("debug_line_str", getOrLoadLineStrData());
 
     m_llvm_context = llvm::DWARFContext::create(section_map, addr_size);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72917.238757.patch
Type: text/x-patch
Size: 2605 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200117/3da9785e/attachment-0001.bin>


More information about the lldb-commits mailing list