Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 1 | //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of various Memory |
| 11 | // management utilities |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/DataTypes.h" |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Process.h" |
Rafael Espindola | 116f21c | 2014-06-12 01:25:33 +0000 | [diff] [blame] | 18 | #include "llvm/Support/WindowsError.h" |
Chandler Carruth | dd7ca93 | 2012-12-04 07:04:57 +0000 | [diff] [blame] | 19 | |
| 20 | // The Windows.h header must be the last one included. |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 21 | #include "WindowsSupport.h" |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | DWORD getWindowsProtectionFlags(unsigned Flags) { |
| 26 | switch (Flags) { |
| 27 | // Contrary to what you might expect, the Windows page protection flags |
| 28 | // are not a bitwise combination of RWX values |
| 29 | case llvm::sys::Memory::MF_READ: |
| 30 | return PAGE_READONLY; |
| 31 | case llvm::sys::Memory::MF_WRITE: |
| 32 | // Note: PAGE_WRITE is not supported by VirtualProtect |
| 33 | return PAGE_READWRITE; |
| 34 | case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE: |
| 35 | return PAGE_READWRITE; |
| 36 | case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC: |
| 37 | return PAGE_EXECUTE_READ; |
| 38 | case llvm::sys::Memory::MF_READ | |
| 39 | llvm::sys::Memory::MF_WRITE | |
| 40 | llvm::sys::Memory::MF_EXEC: |
| 41 | return PAGE_EXECUTE_READWRITE; |
| 42 | case llvm::sys::Memory::MF_EXEC: |
| 43 | return PAGE_EXECUTE; |
| 44 | default: |
| 45 | llvm_unreachable("Illegal memory protection flag specified!"); |
| 46 | } |
| 47 | // Provide a default return value as required by some compilers. |
| 48 | return PAGE_NOACCESS; |
| 49 | } |
| 50 | |
| 51 | size_t getAllocationGranularity() { |
| 52 | SYSTEM_INFO Info; |
| 53 | ::GetSystemInfo(&Info); |
| 54 | if (Info.dwPageSize > Info.dwAllocationGranularity) |
| 55 | return Info.dwPageSize; |
| 56 | else |
| 57 | return Info.dwAllocationGranularity; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 61 | |
| 62 | namespace llvm { |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 63 | namespace sys { |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 64 | |
| 65 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 66 | //=== WARNING: Implementation here must contain only Win32 specific code |
Reid Spencer | b88212e | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 67 | //=== and must not be UNIX code |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 68 | //===----------------------------------------------------------------------===// |
| 69 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 70 | MemoryBlock Memory::allocateMappedMemory(size_t NumBytes, |
| 71 | const MemoryBlock *const NearBlock, |
| 72 | unsigned Flags, |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 73 | std::error_code &EC) { |
| 74 | EC = std::error_code(); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 75 | if (NumBytes == 0) |
| 76 | return MemoryBlock(); |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 77 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 78 | // While we'd be happy to allocate single pages, the Windows allocation |
| 79 | // granularity may be larger than a single page (in practice, it is 64K) |
| 80 | // so mapping less than that will create an unreachable fragment of memory. |
Reid Kleckner | 6bb26da | 2015-06-11 22:22:45 +0000 | [diff] [blame^] | 81 | // Avoid using one-time initialization of static locals here, since they |
| 82 | // aren't thread safe with MSVC. |
| 83 | static volatile size_t GranularityCached; |
| 84 | size_t Granularity = GranularityCached; |
| 85 | if (Granularity == 0) { |
| 86 | Granularity = getAllocationGranularity(); |
| 87 | GranularityCached = Granularity; |
| 88 | } |
| 89 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 90 | const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity; |
Reid Spencer | b88212e | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 91 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 92 | uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) + |
| 93 | NearBlock->size() |
Alp Toker | 42235db | 2013-10-18 07:53:25 +0000 | [diff] [blame] | 94 | : 0; |
Andrew Lenharth | 0940218 | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 95 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 96 | // If the requested address is not aligned to the allocation granularity, |
| 97 | // round up to get beyond NearBlock. VirtualAlloc would have rounded down. |
| 98 | if (Start && Start % Granularity != 0) |
| 99 | Start += Granularity - Start % Granularity; |
| 100 | |
| 101 | DWORD Protect = getWindowsProtectionFlags(Flags); |
| 102 | |
| 103 | void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start), |
| 104 | NumBlocks*Granularity, |
| 105 | MEM_RESERVE | MEM_COMMIT, Protect); |
| 106 | if (PA == NULL) { |
NAKAMURA Takumi | 7bc976a | 2011-10-15 01:58:16 +0000 | [diff] [blame] | 107 | if (NearBlock) { |
| 108 | // Try again without the NearBlock hint |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 109 | return allocateMappedMemory(NumBytes, NULL, Flags, EC); |
NAKAMURA Takumi | 7bc976a | 2011-10-15 01:58:16 +0000 | [diff] [blame] | 110 | } |
Rafael Espindola | 116f21c | 2014-06-12 01:25:33 +0000 | [diff] [blame] | 111 | EC = mapWindowsError(::GetLastError()); |
Chris Lattner | 5a9d2e5 | 2006-07-07 17:32:37 +0000 | [diff] [blame] | 112 | return MemoryBlock(); |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 113 | } |
Reid Spencer | b88212e | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 114 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 115 | MemoryBlock Result; |
| 116 | Result.Address = PA; |
| 117 | Result.Size = NumBlocks*Granularity; |
Alp Toker | 42235db | 2013-10-18 07:53:25 +0000 | [diff] [blame] | 118 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 119 | if (Flags & MF_EXEC) |
| 120 | Memory::InvalidateInstructionCache(Result.Address, Result.Size); |
| 121 | |
| 122 | return Result; |
| 123 | } |
| 124 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 125 | std::error_code Memory::releaseMappedMemory(MemoryBlock &M) { |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 126 | if (M.Address == 0 || M.Size == 0) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 127 | return std::error_code(); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 128 | |
| 129 | if (!VirtualFree(M.Address, 0, MEM_RELEASE)) |
Rafael Espindola | 116f21c | 2014-06-12 01:25:33 +0000 | [diff] [blame] | 130 | return mapWindowsError(::GetLastError()); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 131 | |
| 132 | M.Address = 0; |
| 133 | M.Size = 0; |
| 134 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 135 | return std::error_code(); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 138 | std::error_code Memory::protectMappedMemory(const MemoryBlock &M, |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 139 | unsigned Flags) { |
| 140 | if (M.Address == 0 || M.Size == 0) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 141 | return std::error_code(); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 142 | |
| 143 | DWORD Protect = getWindowsProtectionFlags(Flags); |
| 144 | |
| 145 | DWORD OldFlags; |
| 146 | if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags)) |
Rafael Espindola | 116f21c | 2014-06-12 01:25:33 +0000 | [diff] [blame] | 147 | return mapWindowsError(::GetLastError()); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 148 | |
| 149 | if (Flags & MF_EXEC) |
| 150 | Memory::InvalidateInstructionCache(M.Address, M.Size); |
| 151 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 152 | return std::error_code(); |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /// InvalidateInstructionCache - Before the JIT can run a block of code |
| 156 | /// that has been emitted it must invalidate the instruction cache on some |
| 157 | /// platforms. |
| 158 | void Memory::InvalidateInstructionCache( |
| 159 | const void *Addr, size_t Len) { |
| 160 | FlushInstructionCache(GetCurrentProcess(), Addr, Len); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | MemoryBlock Memory::AllocateRWX(size_t NumBytes, |
| 165 | const MemoryBlock *NearBlock, |
| 166 | std::string *ErrMsg) { |
| 167 | MemoryBlock MB; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 168 | std::error_code EC; |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 169 | MB = allocateMappedMemory(NumBytes, NearBlock, |
| 170 | MF_READ|MF_WRITE|MF_EXEC, EC); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 171 | if (EC != std::error_code() && ErrMsg) { |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 172 | MakeErrMsg(ErrMsg, EC.message()); |
| 173 | } |
| 174 | return MB; |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | 5a9d2e5 | 2006-07-07 17:32:37 +0000 | [diff] [blame] | 177 | bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 178 | std::error_code EC = releaseMappedMemory(M); |
| 179 | if (EC == std::error_code()) |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 180 | return false; |
| 181 | MakeErrMsg(ErrMsg, EC.message()); |
| 182 | return true; |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 183 | } |
Reid Spencer | b88212e | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 184 | |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 185 | static DWORD getProtection(const void *addr) { |
| 186 | MEMORY_BASIC_INFORMATION info; |
| 187 | if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) { |
| 188 | return info.Protect; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 193 | bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) { |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 194 | if (!setRangeWritable(M.Address, M.Size)) { |
| 195 | return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: "); |
| 196 | } |
Argyrios Kyrtzidis | a6f9bb7 | 2008-10-04 08:15:32 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 200 | bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) { |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 201 | if (!setRangeExecutable(M.Address, M.Size)) { |
| 202 | return MakeErrMsg(ErrMsg, "Cannot set memory to executable: "); |
| 203 | } |
Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 207 | bool Memory::setRangeWritable(const void *Addr, size_t Size) { |
| 208 | DWORD prot = getProtection(Addr); |
| 209 | if (!prot) |
| 210 | return false; |
| 211 | |
| 212 | if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) { |
| 213 | prot = PAGE_EXECUTE_READWRITE; |
| 214 | } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) { |
| 215 | prot = PAGE_READWRITE; |
| 216 | } |
| 217 | |
| 218 | DWORD oldProt; |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 219 | Memory::InvalidateInstructionCache(Addr, Size); |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 220 | return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt) |
| 221 | == TRUE; |
| 222 | } |
| 223 | |
Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 224 | bool Memory::setRangeExecutable(const void *Addr, size_t Size) { |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 225 | DWORD prot = getProtection(Addr); |
| 226 | if (!prot) |
| 227 | return false; |
| 228 | |
| 229 | if (prot == PAGE_NOACCESS) { |
| 230 | prot = PAGE_EXECUTE; |
| 231 | } else if (prot == PAGE_READONLY) { |
| 232 | prot = PAGE_EXECUTE_READ; |
| 233 | } else if (prot == PAGE_READWRITE) { |
| 234 | prot = PAGE_EXECUTE_READWRITE; |
| 235 | } |
| 236 | |
| 237 | DWORD oldProt; |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 238 | Memory::InvalidateInstructionCache(Addr, Size); |
Michael J. Spencer | a7a90bf | 2011-10-13 23:16:01 +0000 | [diff] [blame] | 239 | return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt) |
| 240 | == TRUE; |
Argyrios Kyrtzidis | a6f9bb7 | 2008-10-04 08:15:32 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Andrew Kaylor | 1f66100 | 2012-09-19 20:46:12 +0000 | [diff] [blame] | 243 | } // namespace sys |
| 244 | } // namespace llvm |