vsk created this revision.
vsk added reviewers: JDevlieghere, jingham.
vsk added a project: LLDB.

Some clients of lldb::FileSystem look like they are unintentionally
heap-allocating file contents.

This patch renames the CreateDataBuffer API to allow clients to express
intention more clearly. As a follow-up I propose adding a 'Readonly' API
that can use mmap().

rdar://53785446


https://reviews.llvm.org/D74659

Files:
  lldb/include/lldb/Host/FileSystem.h
  lldb/source/API/SBSection.cpp
  lldb/source/Commands/CommandObjectMemory.cpp
  lldb/source/Core/SourceManager.cpp
  lldb/source/Host/common/FileSystem.cpp
  lldb/source/Host/common/Host.cpp
  lldb/source/Host/linux/Host.cpp
  lldb/source/Host/netbsd/Host.cpp
  lldb/source/Interpreter/OptionValueFileSpec.cpp
  
lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
  lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
  lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
  lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
  lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
  lldb/source/Symbol/ObjectFile.cpp
  lldb/unittests/Process/minidump/MinidumpParserTest.cpp

Index: lldb/unittests/Process/minidump/MinidumpParserTest.cpp
===================================================================
--- lldb/unittests/Process/minidump/MinidumpParserTest.cpp
+++ lldb/unittests/Process/minidump/MinidumpParserTest.cpp
@@ -42,7 +42,8 @@
 
   void SetUpData(const char *minidump_filename) {
     std::string filename = GetInputFilePath(minidump_filename);
-    auto BufferPtr = FileSystem::Instance().CreateDataBuffer(filename, -1, 0);
+    auto BufferPtr =
+        FileSystem::Instance().CreateWritableDataBuffer(filename, -1, 0);
     ASSERT_NE(BufferPtr, nullptr);
     llvm::Expected<MinidumpParser> expected_parser =
         MinidumpParser::Create(BufferPtr);
Index: lldb/source/Symbol/ObjectFile.cpp
===================================================================
--- lldb/source/Symbol/ObjectFile.cpp
+++ lldb/source/Symbol/ObjectFile.cpp
@@ -75,8 +75,8 @@
         // container plug-ins can use these bytes to see if they can parse this
         // file.
         if (file_size > 0) {
-          data_sp = FileSystem::Instance().CreateDataBuffer(file->GetPath(),
-                                                            512, file_offset);
+          data_sp = FileSystem::Instance().CreateWritableDataBuffer(
+              file->GetPath(), 512, file_offset);
           data_offset = 0;
         }
       }
@@ -119,7 +119,7 @@
             }
             // We failed to find any cached object files in the container plug-
             // ins, so lets read the first 512 bytes and try again below...
-            data_sp = FileSystem::Instance().CreateDataBuffer(
+            data_sp = FileSystem::Instance().CreateWritableDataBuffer(
                 archive_file.GetPath(), 512, file_offset);
           }
         }
@@ -208,8 +208,8 @@
                                            lldb::offset_t file_offset,
                                            lldb::offset_t file_size,
                                            ModuleSpecList &specs) {
-  DataBufferSP data_sp =
-      FileSystem::Instance().CreateDataBuffer(file.GetPath(), 512, file_offset);
+  DataBufferSP data_sp = FileSystem::Instance().CreateWritableDataBuffer(
+      file.GetPath(), 512, file_offset);
   if (data_sp) {
     if (file_size == 0) {
       const lldb::offset_t actual_file_size =
@@ -684,7 +684,8 @@
 
 DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size,
                                      uint64_t Offset) {
-  return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
+  return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
+                                                         Offset);
 }
 
 void llvm::format_provider<ObjectFile::Type>::format(
Index: lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
===================================================================
--- lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
+++ lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
@@ -141,8 +141,8 @@
   lldb::ProcessSP process_sp;
   // Read enough data for the Minidump header
   constexpr size_t header_size = sizeof(Header);
-  auto DataPtr = FileSystem::Instance().CreateDataBuffer(crash_file->GetPath(),
-                                                         header_size, 0);
+  auto DataPtr = FileSystem::Instance().CreateWritableDataBuffer(
+      crash_file->GetPath(), header_size, 0);
   if (!DataPtr)
     return nullptr;
 
@@ -150,8 +150,8 @@
   if (identify_magic(toStringRef(DataPtr->GetData())) != llvm::file_magic::minidump)
     return nullptr;
 
-  auto AllData =
-      FileSystem::Instance().CreateDataBuffer(crash_file->GetPath(), -1, 0);
+  auto AllData = FileSystem::Instance().CreateWritableDataBuffer(
+      crash_file->GetPath(), -1, 0);
   if (!AllData)
     return nullptr;
 
Index: lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
===================================================================
--- lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
+++ lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
@@ -65,7 +65,7 @@
   lldb::ProcessSP process_sp;
   if (crash_file) {
     const size_t header_size = sizeof(llvm::MachO::mach_header);
-    auto data_sp = FileSystem::Instance().CreateDataBuffer(
+    auto data_sp = FileSystem::Instance().CreateWritableDataBuffer(
         crash_file->GetPath(), header_size, 0);
     if (data_sp && data_sp->GetByteSize() == header_size) {
       DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
Index: lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
===================================================================
--- lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
+++ lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
@@ -60,7 +60,7 @@
     // the header extension.
     const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr);
 
-    auto data_sp = FileSystem::Instance().CreateDataBuffer(
+    auto data_sp = FileSystem::Instance().CreateWritableDataBuffer(
         crash_file->GetPath(), header_size, 0);
     if (data_sp && data_sp->GetByteSize() == header_size &&
         elf::ELFHeader::MagicBytesMatch(data_sp->GetBytes())) {
Index: lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
===================================================================
--- lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1165,8 +1165,8 @@
         xcode_dir_path.append(xcode_select_prefix_dir);
       xcode_dir_path.append("/usr/share/xcode-select/xcode_dir_path");
       temp_file_spec.SetFile(xcode_dir_path, FileSpec::Style::native);
-      auto dir_buffer =
-          FileSystem::Instance().CreateDataBuffer(temp_file_spec.GetPath());
+      auto dir_buffer = FileSystem::Instance().CreateWritableDataBuffer(
+          temp_file_spec.GetPath());
       if (dir_buffer && dir_buffer->GetByteSize() > 0) {
         llvm::StringRef path_ref(dir_buffer->GetChars());
         // Trim tailing newlines and make sure there is enough room for a null
Index: lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
===================================================================
--- lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
+++ lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
@@ -313,7 +313,8 @@
       // file gets updated by a new build while this .a file is being used for
       // debugging
       DataBufferSP archive_data_sp =
-          FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
+          FileSystem::Instance().CreateWritableDataBuffer(*file, length,
+                                                          file_offset);
       if (!archive_data_sp)
         return nullptr;
 
@@ -464,8 +465,8 @@
   bool set_archive_arch = false;
   if (!archive_sp) {
     set_archive_arch = true;
-    data_sp =
-        FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
+    data_sp = FileSystem::Instance().CreateWritableDataBuffer(file, file_size,
+                                                              file_offset);
     if (data_sp) {
       data.SetData(data_sp, 0, data_sp->GetByteSize());
       archive_sp = Archive::ParseAndCacheArchiveForFile(
Index: lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
===================================================================
--- lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
+++ lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
@@ -2443,7 +2443,8 @@
   }
 
   // Read file into data buffer
-  auto data_sp = FileSystem::Instance().CreateDataBuffer(file.GetPath());
+  auto data_sp =
+      FileSystem::Instance().CreateWritableDataBuffer(file.GetPath());
 
   // Cast start of buffer to FileHeader and use pointer to read metadata
   void *file_buf = data_sp->GetBytes();
@@ -2977,7 +2978,7 @@
   const FileSpec fs = m_module->GetFileSpec();
 
   auto buffer =
-      FileSystem::Instance().CreateDataBuffer(fs.GetPath(), size, addr);
+      FileSystem::Instance().CreateWritableDataBuffer(fs.GetPath(), size, addr);
   if (!buffer)
     return false;
 
Index: lldb/source/Interpreter/OptionValueFileSpec.cpp
===================================================================
--- lldb/source/Interpreter/OptionValueFileSpec.cpp
+++ lldb/source/Interpreter/OptionValueFileSpec.cpp
@@ -110,8 +110,8 @@
     const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
     if (m_data_sp && m_data_mod_time == file_mod_time)
       return m_data_sp;
-    m_data_sp =
-        FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
+    m_data_sp = FileSystem::Instance().CreateWritableDataBuffer(
+        m_current_value.GetPath());
     m_data_mod_time = file_mod_time;
   }
   return m_data_sp;
Index: lldb/source/Host/netbsd/Host.cpp
===================================================================
--- lldb/source/Host/netbsd/Host.cpp
+++ lldb/source/Host/netbsd/Host.cpp
@@ -104,7 +104,7 @@
   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
 
   if (process_info.ProcessIDIsValid()) {
-    auto buffer_sp = FileSystem::Instance().CreateDataBuffer(
+    auto buffer_sp = FileSystem::Instance().CreateWritableDataBuffer(
         process_info.GetExecutableFile(), 0x20, 0);
     if (buffer_sp) {
       uint8_t exe_class =
Index: lldb/source/Host/linux/Host.cpp
===================================================================
--- lldb/source/Host/linux/Host.cpp
+++ lldb/source/Host/linux/Host.cpp
@@ -124,7 +124,8 @@
 static ArchSpec GetELFProcessCPUType(llvm::StringRef exe_path) {
   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
 
-  auto buffer_sp = FileSystem::Instance().CreateDataBuffer(exe_path, 0x20, 0);
+  auto buffer_sp =
+      FileSystem::Instance().CreateWritableDataBuffer(exe_path, 0x20, 0);
   if (!buffer_sp)
     return ArchSpec();
 
Index: lldb/source/Host/common/Host.cpp
===================================================================
--- lldb/source/Host/common/Host.cpp
+++ lldb/source/Host/common/Host.cpp
@@ -572,8 +572,8 @@
             error.SetErrorStringWithFormat(
                 "shell command output is too large to fit into a std::string");
           } else {
-            auto Buffer =
-                FileSystem::Instance().CreateDataBuffer(output_file_spec);
+            auto Buffer = FileSystem::Instance().CreateWritableDataBuffer(
+                output_file_spec);
             if (error.Success())
               command_output_ptr->assign(Buffer->GetChars(),
                                          Buffer->GetByteSize());
Index: lldb/source/Host/common/FileSystem.cpp
===================================================================
--- lldb/source/Host/common/FileSystem.cpp
+++ lldb/source/Host/common/FileSystem.cpp
@@ -277,8 +277,8 @@
 }
 
 std::shared_ptr<DataBufferLLVM>
-FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
-                             uint64_t offset) {
+FileSystem::CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size,
+                                     uint64_t offset) {
   AddFile(path);
 
   const bool is_volatile = !IsLocal(path);
@@ -305,9 +305,9 @@
 }
 
 std::shared_ptr<DataBufferLLVM>
-FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
-                             uint64_t offset) {
-  return CreateDataBuffer(file_spec.GetPath(), size, offset);
+FileSystem::CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size,
+                                     uint64_t offset) {
+  return CreateWritableDataBuffer(file_spec.GetPath(), size, offset);
 }
 
 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
Index: lldb/source/Core/SourceManager.cpp
===================================================================
--- lldb/source/Core/SourceManager.cpp
+++ lldb/source/Core/SourceManager.cpp
@@ -439,7 +439,7 @@
   }
 
   if (m_mod_time != llvm::sys::TimePoint<>())
-    m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
+    m_data_sp = FileSystem::Instance().CreateWritableDataBuffer(m_file_spec);
 }
 
 uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
@@ -517,7 +517,7 @@
   if (curr_mod_time != llvm::sys::TimePoint<>() &&
       m_mod_time != curr_mod_time) {
     m_mod_time = curr_mod_time;
-    m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
+    m_data_sp = FileSystem::Instance().CreateWritableDataBuffer(m_file_spec);
     m_offsets.clear();
   }
 }
Index: lldb/source/Commands/CommandObjectMemory.cpp
===================================================================
--- lldb/source/Commands/CommandObjectMemory.cpp
+++ lldb/source/Commands/CommandObjectMemory.cpp
@@ -1351,7 +1351,7 @@
       size_t length = SIZE_MAX;
       if (item_byte_size > 1)
         length = item_byte_size;
-      auto data_sp = FileSystem::Instance().CreateDataBuffer(
+      auto data_sp = FileSystem::Instance().CreateWritableDataBuffer(
           m_memory_options.m_infile.GetPath(), length,
           m_memory_options.m_infile_offset);
       if (data_sp) {
Index: lldb/source/API/SBSection.cpp
===================================================================
--- lldb/source/API/SBSection.cpp
+++ lldb/source/API/SBSection.cpp
@@ -207,7 +207,7 @@
             else
               file_size = 0;
           }
-          auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
+          auto data_buffer_sp = FileSystem::Instance().CreateWritableDataBuffer(
               objfile->GetFileSpec().GetPath(), file_size, file_offset);
           if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
             DataExtractorSP data_extractor_sp(
Index: lldb/include/lldb/Host/FileSystem.h
===================================================================
--- lldb/include/lldb/Host/FileSystem.h
+++ lldb/include/lldb/Host/FileSystem.h
@@ -141,14 +141,14 @@
   void Resolve(FileSpec &file_spec);
   /// \}
 
-  //// Create memory buffer from path.
+  //// Read a file into a heap-allocated, writable memory buffer.
   /// \{
-  std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path,
-                                                   uint64_t size = 0,
-                                                   uint64_t offset = 0);
-  std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec,
-                                                   uint64_t size = 0,
-                                                   uint64_t offset = 0);
+  std::shared_ptr<DataBufferLLVM>
+  CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size = 0,
+                           uint64_t offset = 0);
+  std::shared_ptr<DataBufferLLVM>
+  CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size = 0,
+                           uint64_t offset = 0);
   /// \}
 
   /// Call into the Host to see if it can help find the file.
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH] ... Vedant Kumar via Phabricator via lldb-commits

Reply via email to