Support/Windows: Add support modifying memory permissions on Windows. Patch by Aaron...
[oota-llvm.git] / lib / Support / Windows / Memory.inc
1 //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides the Win32 specific implementation of various Memory
11 // management utilities
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "Windows.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Process.h"
18
19 namespace llvm {
20 using namespace sys;
21
22 //===----------------------------------------------------------------------===//
23 //=== WARNING: Implementation here must contain only Win32 specific code
24 //===          and must not be UNIX code
25 //===----------------------------------------------------------------------===//
26
27 MemoryBlock Memory::AllocateRWX(size_t NumBytes,
28                                 const MemoryBlock *NearBlock,
29                                 std::string *ErrMsg) {
30   if (NumBytes == 0) return MemoryBlock();
31
32   static const size_t pageSize = Process::GetPageSize();
33   size_t NumPages = (NumBytes+pageSize-1)/pageSize;
34
35   //FIXME: support NearBlock if ever needed on Win64.
36
37   void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
38                   PAGE_EXECUTE_READWRITE);
39   if (pa == NULL) {
40     MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
41     return MemoryBlock();
42   }
43
44   MemoryBlock result;
45   result.Address = pa;
46   result.Size = NumPages*pageSize;
47   return result;
48 }
49
50 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
51   if (M.Address == 0 || M.Size == 0) return false;
52   if (!VirtualFree(M.Address, 0, MEM_RELEASE))
53     return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
54   return false;
55 }
56
57 static DWORD getProtection(const void *addr) {
58   MEMORY_BASIC_INFORMATION info;
59   if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
60     return info.Protect;
61   }
62   return 0;
63 }
64
65 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
66   if (!setRangeWritable(M.Address, M.Size)) {
67     return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
68   }
69   return true;
70 }
71
72 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
73   if (!setRangeExecutable(M.Address, M.Size)) {
74     return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
75   }
76   return true;
77 }
78
79 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
80   DWORD prot = getProtection(Addr);
81   if (!prot)
82     return false;
83
84   if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
85     prot = PAGE_EXECUTE_READWRITE;
86   } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
87     prot = PAGE_READWRITE;
88   }
89
90   DWORD oldProt;
91   sys::Memory::InvalidateInstructionCache(Addr, Size);
92   return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
93             == TRUE;
94 }
95
96 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
97   DWORD prot = getProtection(Addr);
98   if (!prot)
99     return false;
100
101   if (prot == PAGE_NOACCESS) {
102     prot = PAGE_EXECUTE;
103   } else if (prot == PAGE_READONLY) {
104     prot = PAGE_EXECUTE_READ;
105   } else if (prot == PAGE_READWRITE) {
106     prot = PAGE_EXECUTE_READWRITE;
107   }
108
109   DWORD oldProt;
110   sys::Memory::InvalidateInstructionCache(Addr, Size);
111   return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
112             == TRUE;
113 }
114
115 }