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

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Jan 20 02:48:46 PST 2020


Author: Pavel Labath
Date: 2020-01-20T11:45:53+01:00
New Revision: 06e73f071ae12dc83c102ddecdb939dea880e588

URL: https://github.com/llvm/llvm-project/commit/06e73f071ae12dc83c102ddecdb939dea880e588
DIFF: https://github.com/llvm/llvm-project/commit/06e73f071ae12dc83c102ddecdb939dea880e588.diff

LOG: [lldb/DWARF] Change how we construct a llvm::DWARFContext

Summary:
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.

Reviewers: JDevlieghere, aprantl

Subscribers: lldb-commits

Tags: #lldb

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
index 5052b825fea6..1f7608ae26d1 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
@@ -117,32 +117,17 @@ llvm::DWARFContext &DWARFContext::GetAsLLVM() {
   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);
   }

diff  --git a/lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp b/lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp
index aeb0ff1d01b1..4dcbb4715220 100644
--- a/lldb/test/Shell/SymbolFile/DWARF/debug-names-compressed.cpp
+++ b/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() {}


        


More information about the lldb-commits mailing list