diff options
author | Aleksandr Urakov <aleksandr.urakov@jetbrains.com> | 2018-11-01 08:54:38 +0000 |
---|---|---|
committer | Aleksandr Urakov <aleksandr.urakov@jetbrains.com> | 2018-11-01 08:54:38 +0000 |
commit | 9d5b2d4adc6d3ba29628d23173d4b078ba42d5fd (patch) | |
tree | d0f2f844a9c20a0118bda8974c7eb60fc754e73c /lldb/source/Plugins/Process/Windows | |
parent | 3d347bf545d51156b3c1bf6ed93100369568a550 (diff) | |
download | bcm5719-llvm-9d5b2d4adc6d3ba29628d23173d4b078ba42d5fd.tar.gz bcm5719-llvm-9d5b2d4adc6d3ba29628d23173d4b078ba42d5fd.zip |
[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
llvm-svn: 345815
Diffstat (limited to 'lldb/source/Plugins/Process/Windows')
-rw-r--r-- | lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp | 66 | ||||
-rw-r--r-- | lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h | 3 |
2 files changed, 69 insertions, 0 deletions
diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp index 5bc404aec00..33f4bc38e27 100644 --- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp +++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp @@ -72,6 +72,20 @@ std::string GetProcessExecutableName(DWORD pid) { 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(lldb::addr_t vm_addr, const void *buf, 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); diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h index ed3938beb34..27fd5823c18 100644 --- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h +++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h @@ -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; |