Przeglądaj źródła

LibDebug: Store LibDebug objects on the heap & make them non-copyable

This fixes an issue were some LibDebug objects (for example,
Dwarf::CompilationUnit) held a reference to their parent
Dwarf::DwarfInfo object, which was constructed on the stack and later
moved to the heap.
Itamar 4 lat temu
rodzic
commit
e9e4358a93

+ 1 - 1
Userland/Libraries/LibCoreDump/Backtrace.cpp

@@ -42,7 +42,7 @@ static const ELFObjectInfo* object_info_for_region(const ELF::Core::MemoryRegion
         return nullptr;
 
     auto image = make<ELF::Image>(file_or_error.value()->bytes());
-    auto info = make<ELFObjectInfo>(file_or_error.release_value(), Debug::DebugInfo { move(image) });
+    auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(move(image)));
     auto* info_ptr = info.ptr();
     s_debug_info_cache.set(path, move(info));
     return info_ptr;

+ 2 - 2
Userland/Libraries/LibCoreDump/Backtrace.h

@@ -14,14 +14,14 @@
 namespace CoreDump {
 
 struct ELFObjectInfo {
-    ELFObjectInfo(NonnullRefPtr<MappedFile> file, Debug::DebugInfo&& debug_info)
+    ELFObjectInfo(NonnullRefPtr<MappedFile> file, NonnullOwnPtr<Debug::DebugInfo>&& debug_info)
         : file(move(file))
         , debug_info(move(debug_info))
     {
     }
 
     NonnullRefPtr<MappedFile> file;
-    Debug::DebugInfo debug_info;
+    NonnullOwnPtr<Debug::DebugInfo> debug_info;
 };
 
 class Backtrace {

+ 3 - 0
Userland/Libraries/LibDebug/DebugInfo.h

@@ -20,6 +20,9 @@
 namespace Debug {
 
 class DebugInfo {
+    AK_MAKE_NONCOPYABLE(DebugInfo);
+    AK_MAKE_NONMOVABLE(DebugInfo);
+
 public:
     explicit DebugInfo(NonnullOwnPtr<const ELF::Image>, String source_root = {}, FlatPtr base_address = 0);
 

+ 4 - 0
Userland/Libraries/LibDebug/Dwarf/CompilationUnit.h

@@ -7,6 +7,7 @@
 #pragma once
 
 #include "AbbreviationsMap.h"
+#include <AK/Noncopyable.h>
 #include <AK/Types.h>
 
 namespace Debug::Dwarf {
@@ -15,6 +16,9 @@ class DwarfInfo;
 class DIE;
 
 class CompilationUnit {
+    AK_MAKE_NONCOPYABLE(CompilationUnit);
+    AK_MAKE_NONMOVABLE(CompilationUnit);
+
 public:
     CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader&);
 

+ 1 - 1
Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp

@@ -44,7 +44,7 @@ void DwarfInfo::populate_compilation_units()
         VERIFY(compilation_unit_header.address_size() == sizeof(u32));
 
         u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
-        m_compilation_units.empend(*this, unit_offset, compilation_unit_header);
+        m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header));
         stream.discard_or_error(length_after_header);
     }
 }

+ 4 - 1
Userland/Libraries/LibDebug/Dwarf/DwarfInfo.h

@@ -19,6 +19,9 @@
 namespace Debug::Dwarf {
 
 class DwarfInfo {
+    AK_MAKE_NONCOPYABLE(DwarfInfo);
+    AK_MAKE_NONMOVABLE(DwarfInfo);
+
 public:
     explicit DwarfInfo(const ELF::Image&);
 
@@ -44,7 +47,7 @@ private:
     ReadonlyBytes m_debug_strings_data;
     ReadonlyBytes m_debug_line_strings_data;
 
-    Vector<Dwarf::CompilationUnit> m_compilation_units;
+    NonnullOwnPtrVector<Dwarf::CompilationUnit> m_compilation_units;
 };
 
 template<typename Callback>

+ 4 - 5
Userland/Libraries/LibSymbolication/Symbolication.cpp

@@ -16,7 +16,7 @@ namespace Symbolication {
 
 struct CachedELF {
     NonnullRefPtr<MappedFile> mapped_file;
-    Debug::DebugInfo debug_info;
+    NonnullOwnPtr<Debug::DebugInfo> debug_info;
 };
 
 static HashMap<String, OwnPtr<CachedELF>> s_cache;
@@ -36,8 +36,7 @@ Optional<Symbol> symbolicate(String const& path, u32 address)
             s_cache.set(path, {});
             {};
         }
-        Debug::DebugInfo debug_info(move(elf));
-        auto cached_elf = make<CachedELF>(mapped_file.release_value(), move(debug_info));
+        auto cached_elf = make<CachedELF>(mapped_file.release_value(), make<Debug::DebugInfo>(move(elf)));
         s_cache.set(path, move(cached_elf));
     }
 
@@ -49,8 +48,8 @@ Optional<Symbol> symbolicate(String const& path, u32 address)
         return {};
 
     u32 offset = 0;
-    auto symbol = cached_elf->debug_info.elf().symbolicate(address, &offset);
-    auto source_position = cached_elf->debug_info.get_source_position(address);
+    auto symbol = cached_elf->debug_info->elf().symbolicate(address, &offset);
+    auto source_position = cached_elf->debug_info->get_source_position(address);
     String filename;
     u32 line_number = 0;
     if (source_position.has_value()) {