diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-10-13 23:16:01 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-10-13 23:16:01 +0000 |
commit | a7a90bfdab65e332363ff9a2acd2cdf854a78dc5 (patch) | |
tree | 3220c902a605c388bebeea54d5caeb792e956c10 /llvm/lib/Support/Windows/Memory.inc | |
parent | 92734d6f460a01145443e48418af3a8635ca1afa (diff) | |
download | bcm5719-llvm-a7a90bfdab65e332363ff9a2acd2cdf854a78dc5.tar.gz bcm5719-llvm-a7a90bfdab65e332363ff9a2acd2cdf854a78dc5.zip |
Support/Windows: Add support modifying memory permissions on Windows. Patch by Aaron Ballman!
llvm-svn: 141910
Diffstat (limited to 'llvm/lib/Support/Windows/Memory.inc')
-rw-r--r-- | llvm/lib/Support/Windows/Memory.inc | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/llvm/lib/Support/Windows/Memory.inc b/llvm/lib/Support/Windows/Memory.inc index 9f69e7367e6..8609d39dd6d 100644 --- a/llvm/lib/Support/Windows/Memory.inc +++ b/llvm/lib/Support/Windows/Memory.inc @@ -54,20 +54,62 @@ bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) { return false; } +static DWORD getProtection(const void *addr) { + MEMORY_BASIC_INFORMATION info; + if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) { + return info.Protect; + } + return 0; +} + bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) { + if (!setRangeWritable(M.Address, M.Size)) { + return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: "); + } return true; } bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) { - return false; + if (!setRangeExecutable(M.Address, M.Size)) { + return MakeErrMsg(ErrMsg, "Cannot set memory to executable: "); + } + return true; } bool Memory::setRangeWritable(const void *Addr, size_t Size) { - return true; + DWORD prot = getProtection(Addr); + if (!prot) + return false; + + if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) { + prot = PAGE_EXECUTE_READWRITE; + } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) { + prot = PAGE_READWRITE; + } + + DWORD oldProt; + sys::Memory::InvalidateInstructionCache(Addr, Size); + return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt) + == TRUE; } bool Memory::setRangeExecutable(const void *Addr, size_t Size) { - return false; + DWORD prot = getProtection(Addr); + if (!prot) + return false; + + if (prot == PAGE_NOACCESS) { + prot = PAGE_EXECUTE; + } else if (prot == PAGE_READONLY) { + prot = PAGE_EXECUTE_READ; + } else if (prot == PAGE_READWRITE) { + prot = PAGE_EXECUTE_READWRITE; + } + + DWORD oldProt; + sys::Memory::InvalidateInstructionCache(Addr, Size); + return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt) + == TRUE; } } |