[llvm-commits] [llvm] r141910 - /llvm/trunk/lib/Support/Windows/Memory.inc

Michael J. Spencer bigcheesegs at gmail.com
Thu Oct 13 16:16:01 PDT 2011


Author: mspencer
Date: Thu Oct 13 18:16:01 2011
New Revision: 141910

URL: http://llvm.org/viewvc/llvm-project?rev=141910&view=rev
Log:
Support/Windows: Add support modifying memory permissions on Windows. Patch by Aaron Ballman!

Modified:
    llvm/trunk/lib/Support/Windows/Memory.inc

Modified: llvm/trunk/lib/Support/Windows/Memory.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Memory.inc?rev=141910&r1=141909&r2=141910&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Memory.inc (original)
+++ llvm/trunk/lib/Support/Windows/Memory.inc Thu Oct 13 18:16:01 2011
@@ -54,20 +54,62 @@
   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;
 }
 
 }





More information about the llvm-commits mailing list