[Lldb-commits] [lldb] r345815 - [Windows] A basic implementation of memory allocations in a debuggee process

Aleksandr Urakov via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 1 01:54:38 PDT 2018


Author: aleksandr.urakov
Date: Thu Nov  1 01:54:38 2018
New Revision: 345815

URL: http://llvm.org/viewvc/llvm-project?rev=345815&view=rev
Log:
[Windows] A basic implementation of memory allocations in a debuggee process

Summary:
This patch adds a basic implementation of `DoAllocateMemory` and
`DoDeallocateMemory` for Windows processes. For now it considers only the
executable permission (and always allows reads and writes).

Reviewers: zturner, asmith, stella.stamenova, labath, clayborg

Reviewed By: zturner

Subscribers: Hui, vsk, jingham, aleksandr.urakov, clayborg, abidh, teemperor, lldb-commits

Tags: #lldb

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

Added:
    lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test
      - copied, changed from r345812, lldb/trunk/lit/Expr/TestIRMemoryMap.test
Modified:
    lldb/trunk/lit/Expr/TestIRMemoryMap.test
    lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
    lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h

Modified: lldb/trunk/lit/Expr/TestIRMemoryMap.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Expr/TestIRMemoryMap.test?rev=345815&r1=345814&r2=345815&view=diff
==============================================================================
--- lldb/trunk/lit/Expr/TestIRMemoryMap.test (original)
+++ lldb/trunk/lit/Expr/TestIRMemoryMap.test Thu Nov  1 01:54:38 2018
@@ -1,4 +1,4 @@
-# XFAIL: windows
+# UNSUPPORTED: windows
 
 # RUN: %cxx %p/Inputs/call-function.cpp -g -o %t
 

Copied: lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test (from r345812, lldb/trunk/lit/Expr/TestIRMemoryMap.test)
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test?p2=lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test&p1=lldb/trunk/lit/Expr/TestIRMemoryMap.test&r1=345812&r2=345815&rev=345815&view=diff
==============================================================================
--- lldb/trunk/lit/Expr/TestIRMemoryMap.test (original)
+++ lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test Thu Nov  1 01:54:38 2018
@@ -1,6 +1,6 @@
-# XFAIL: windows
+# REQUIRES: windows
 
-# RUN: %cxx %p/Inputs/call-function.cpp -g -o %t
+# RUN: clang-cl /Zi %p/Inputs/call-function.cpp -o %t
 
 # RUN: lldb-test ir-memory-map %t %S/Inputs/ir-memory-map-basic
 # RUN: lldb-test ir-memory-map -host-only %t %S/Inputs/ir-memory-map-basic

Modified: lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp?rev=345815&r1=345814&r2=345815&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp (original)
+++ lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp Thu Nov  1 01:54:38 2018
@@ -72,6 +72,20 @@ std::string GetProcessExecutableName(DWO
   return file_name;
 }
 
+DWORD ConvertLldbToWinApiProtect(uint32_t protect) {
+  // We also can process a read / write permissions here, but if the debugger
+  // will make later a write into the allocated memory, it will fail. To get
+  // around it is possible inside DoWriteMemory to remember memory permissions,
+  // allow write, write and restore permissions, but for now we process only
+  // the executable permission.
+  //
+  // TODO: Process permissions other than executable
+  if (protect & ePermissionsExecutable)
+    return PAGE_EXECUTE_READWRITE;
+
+  return PAGE_READWRITE;
+}
+
 } // anonymous namespace
 
 namespace lldb_private {
@@ -695,6 +709,58 @@ size_t ProcessWindows::DoWriteMemory(lld
   return bytes_written;
 }
 
+lldb::addr_t ProcessWindows::DoAllocateMemory(size_t size, uint32_t permissions,
+                                              Status &error) {
+  Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY);
+  llvm::sys::ScopedLock lock(m_mutex);
+  LLDB_LOG(log, "attempting to allocate {0} bytes with permissions {1}", size,
+           permissions);
+
+  if (!m_session_data) {
+    LLDB_LOG(log, "cannot allocate, there is no active debugger connection.");
+    error.SetErrorString(
+        "cannot allocate, there is no active debugger connection");
+    return 0;
+  }
+
+  HostProcess process = m_session_data->m_debugger->GetProcess();
+  lldb::process_t handle = process.GetNativeProcess().GetSystemHandle();
+  auto protect = ConvertLldbToWinApiProtect(permissions);
+  auto result = VirtualAllocEx(handle, nullptr, size, MEM_COMMIT, protect);
+  if (!result) {
+    error.SetError(GetLastError(), eErrorTypeWin32);
+    LLDB_LOG(log, "allocating failed with error: {0}", error);
+    return 0;
+  }
+
+  return reinterpret_cast<addr_t>(result);
+}
+
+Status ProcessWindows::DoDeallocateMemory(lldb::addr_t ptr) {
+  Status result;
+
+  Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY);
+  llvm::sys::ScopedLock lock(m_mutex);
+  LLDB_LOG(log, "attempting to deallocate bytes at address {0}", ptr);
+
+  if (!m_session_data) {
+    LLDB_LOG(log, "cannot deallocate, there is no active debugger connection.");
+    result.SetErrorString(
+        "cannot deallocate, there is no active debugger connection");
+    return result;
+  }
+
+  HostProcess process = m_session_data->m_debugger->GetProcess();
+  lldb::process_t handle = process.GetNativeProcess().GetSystemHandle();
+  if (!VirtualFreeEx(handle, reinterpret_cast<LPVOID>(ptr), 0, MEM_RELEASE)) {
+    result.SetError(GetLastError(), eErrorTypeWin32);
+    LLDB_LOG(log, "deallocating failed with error: {0}", result);
+    return result;
+  }
+
+  return result;
+}
+
 Status ProcessWindows::GetMemoryRegionInfo(lldb::addr_t vm_addr,
                                            MemoryRegionInfo &info) {
   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY);

Modified: lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h?rev=345815&r1=345814&r2=345815&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h (original)
+++ lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h Thu Nov  1 01:54:38 2018
@@ -84,6 +84,9 @@ public:
                       Status &error) override;
   size_t DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
                        Status &error) override;
+  lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
+                                Status &error) override;
+  Status DoDeallocateMemory(lldb::addr_t ptr) override;
   Status GetMemoryRegionInfo(lldb::addr_t vm_addr,
                              MemoryRegionInfo &info) override;
 




More information about the lldb-commits mailing list