This revision was automatically updated to reflect the committed changes.
Closed by commit rL345815: [Windows] A basic implementation of memory 
allocations in a debuggee process (authored by aleksandr.urakov, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D52618?vs=167338&id=172097#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D52618

Files:
  lldb/trunk/lit/Expr/TestIRMemoryMap.test
  lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test
  lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
  lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h

Index: lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h
===================================================================
--- lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h
+++ lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.h
@@ -84,6 +84,9 @@
                       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;
 
Index: lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
===================================================================
--- lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
+++ lldb/trunk/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
@@ -72,6 +72,20 @@
   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 @@
   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);
Index: lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test
===================================================================
--- lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test
+++ lldb/trunk/lit/Expr/TestIRMemoryMapWindows.test
@@ -0,0 +1,12 @@
+# REQUIRES: windows
+
+# 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
+
+# RUN: lldb-test ir-memory-map %t %S/Inputs/ir-memory-map-overlap1
+# RUN: lldb-test ir-memory-map -host-only %t %S/Inputs/ir-memory-map-overlap1
+
+# RUN: lldb-test ir-memory-map %t %S/Inputs/ir-memory-map-mix-malloc-free
+# RUN: lldb-test ir-memory-map -host-only %t %S/Inputs/ir-memory-map-mix-malloc-free
Index: lldb/trunk/lit/Expr/TestIRMemoryMap.test
===================================================================
--- lldb/trunk/lit/Expr/TestIRMemoryMap.test
+++ lldb/trunk/lit/Expr/TestIRMemoryMap.test
@@ -1,4 +1,4 @@
-# XFAIL: windows
+# UNSUPPORTED: windows
 
 # RUN: %cxx %p/Inputs/call-function.cpp -g -o %t
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to