blob: ae8371abf5b340689c5a0af526b2f1fed924ce0a [file] [log] [blame]
Reid Spencer566ac282004-09-11 04:59:30 +00001//===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer566ac282004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer566ac282004-09-11 04:59:30 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of various Memory
11// management utilities
12//
13//===----------------------------------------------------------------------===//
14
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/DataTypes.h"
Andrew Kaylor1f661002012-09-19 20:46:12 +000016#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000017#include "llvm/Support/Process.h"
Rafael Espindola116f21c2014-06-12 01:25:33 +000018#include "llvm/Support/WindowsError.h"
Chandler Carruthdd7ca932012-12-04 07:04:57 +000019
20// The Windows.h header must be the last one included.
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000021#include "WindowsSupport.h"
Andrew Kaylor1f661002012-09-19 20:46:12 +000022
23namespace {
24
25DWORD 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
51size_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 Spencer566ac282004-09-11 04:59:30 +000061
62namespace llvm {
Andrew Kaylor1f661002012-09-19 20:46:12 +000063namespace sys {
Reid Spencer566ac282004-09-11 04:59:30 +000064
65//===----------------------------------------------------------------------===//
Michael J. Spencer447762d2010-11-29 18:16:10 +000066//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencerb88212e2004-09-15 05:49:50 +000067//=== and must not be UNIX code
Reid Spencer566ac282004-09-11 04:59:30 +000068//===----------------------------------------------------------------------===//
69
Andrew Kaylor1f661002012-09-19 20:46:12 +000070MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
71 const MemoryBlock *const NearBlock,
72 unsigned Flags,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000073 std::error_code &EC) {
74 EC = std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +000075 if (NumBytes == 0)
76 return MemoryBlock();
Reid Spencer566ac282004-09-11 04:59:30 +000077
Andrew Kaylor1f661002012-09-19 20:46:12 +000078 // 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.
81 static const size_t Granularity = getAllocationGranularity();
82 const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
Reid Spencerb88212e2004-09-15 05:49:50 +000083
Andrew Kaylor1f661002012-09-19 20:46:12 +000084 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
85 NearBlock->size()
Alp Toker42235db2013-10-18 07:53:25 +000086 : 0;
Andrew Lenharth09402182005-07-29 23:40:16 +000087
Andrew Kaylor1f661002012-09-19 20:46:12 +000088 // If the requested address is not aligned to the allocation granularity,
89 // round up to get beyond NearBlock. VirtualAlloc would have rounded down.
90 if (Start && Start % Granularity != 0)
91 Start += Granularity - Start % Granularity;
92
93 DWORD Protect = getWindowsProtectionFlags(Flags);
94
95 void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start),
96 NumBlocks*Granularity,
97 MEM_RESERVE | MEM_COMMIT, Protect);
98 if (PA == NULL) {
NAKAMURA Takumi7bc976a2011-10-15 01:58:16 +000099 if (NearBlock) {
100 // Try again without the NearBlock hint
Andrew Kaylor1f661002012-09-19 20:46:12 +0000101 return allocateMappedMemory(NumBytes, NULL, Flags, EC);
NAKAMURA Takumi7bc976a2011-10-15 01:58:16 +0000102 }
Rafael Espindola116f21c2014-06-12 01:25:33 +0000103 EC = mapWindowsError(::GetLastError());
Chris Lattner5a9d2e52006-07-07 17:32:37 +0000104 return MemoryBlock();
Reid Spencer566ac282004-09-11 04:59:30 +0000105 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000106
Andrew Kaylor1f661002012-09-19 20:46:12 +0000107 MemoryBlock Result;
108 Result.Address = PA;
109 Result.Size = NumBlocks*Granularity;
Alp Toker42235db2013-10-18 07:53:25 +0000110
Andrew Kaylor1f661002012-09-19 20:46:12 +0000111 if (Flags & MF_EXEC)
112 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
113
114 return Result;
115}
116
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000117 std::error_code Memory::releaseMappedMemory(MemoryBlock &M) {
Andrew Kaylor1f661002012-09-19 20:46:12 +0000118 if (M.Address == 0 || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000119 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000120
121 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
Rafael Espindola116f21c2014-06-12 01:25:33 +0000122 return mapWindowsError(::GetLastError());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000123
124 M.Address = 0;
125 M.Size = 0;
126
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000127 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000128}
129
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000130 std::error_code Memory::protectMappedMemory(const MemoryBlock &M,
Andrew Kaylor1f661002012-09-19 20:46:12 +0000131 unsigned Flags) {
132 if (M.Address == 0 || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000133 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000134
135 DWORD Protect = getWindowsProtectionFlags(Flags);
136
137 DWORD OldFlags;
138 if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags))
Rafael Espindola116f21c2014-06-12 01:25:33 +0000139 return mapWindowsError(::GetLastError());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000140
141 if (Flags & MF_EXEC)
142 Memory::InvalidateInstructionCache(M.Address, M.Size);
143
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000144 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000145}
146
147/// InvalidateInstructionCache - Before the JIT can run a block of code
148/// that has been emitted it must invalidate the instruction cache on some
149/// platforms.
150void Memory::InvalidateInstructionCache(
151 const void *Addr, size_t Len) {
152 FlushInstructionCache(GetCurrentProcess(), Addr, Len);
153}
154
155
156MemoryBlock Memory::AllocateRWX(size_t NumBytes,
157 const MemoryBlock *NearBlock,
158 std::string *ErrMsg) {
159 MemoryBlock MB;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000160 std::error_code EC;
Andrew Kaylor1f661002012-09-19 20:46:12 +0000161 MB = allocateMappedMemory(NumBytes, NearBlock,
162 MF_READ|MF_WRITE|MF_EXEC, EC);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000163 if (EC != std::error_code() && ErrMsg) {
Andrew Kaylor1f661002012-09-19 20:46:12 +0000164 MakeErrMsg(ErrMsg, EC.message());
165 }
166 return MB;
Reid Spencer566ac282004-09-11 04:59:30 +0000167}
168
Chris Lattner5a9d2e52006-07-07 17:32:37 +0000169bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000170 std::error_code EC = releaseMappedMemory(M);
171 if (EC == std::error_code())
Andrew Kaylor1f661002012-09-19 20:46:12 +0000172 return false;
173 MakeErrMsg(ErrMsg, EC.message());
174 return true;
Reid Spencer566ac282004-09-11 04:59:30 +0000175}
Reid Spencerb88212e2004-09-15 05:49:50 +0000176
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000177static DWORD getProtection(const void *addr) {
178 MEMORY_BASIC_INFORMATION info;
179 if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
180 return info.Protect;
181 }
182 return 0;
183}
184
Jim Grosbach93960512008-10-20 21:39:23 +0000185bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000186 if (!setRangeWritable(M.Address, M.Size)) {
187 return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
188 }
Argyrios Kyrtzidisa6f9bb72008-10-04 08:15:32 +0000189 return true;
190}
191
Jim Grosbach93960512008-10-20 21:39:23 +0000192bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000193 if (!setRangeExecutable(M.Address, M.Size)) {
194 return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
195 }
Jim Grosbach93960512008-10-20 21:39:23 +0000196 return true;
197}
198
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000199bool Memory::setRangeWritable(const void *Addr, size_t Size) {
200 DWORD prot = getProtection(Addr);
201 if (!prot)
202 return false;
203
204 if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
205 prot = PAGE_EXECUTE_READWRITE;
206 } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
207 prot = PAGE_READWRITE;
208 }
209
210 DWORD oldProt;
Andrew Kaylor1f661002012-09-19 20:46:12 +0000211 Memory::InvalidateInstructionCache(Addr, Size);
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000212 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
213 == TRUE;
214}
215
Jim Grosbach93960512008-10-20 21:39:23 +0000216bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000217 DWORD prot = getProtection(Addr);
218 if (!prot)
219 return false;
220
221 if (prot == PAGE_NOACCESS) {
222 prot = PAGE_EXECUTE;
223 } else if (prot == PAGE_READONLY) {
224 prot = PAGE_EXECUTE_READ;
225 } else if (prot == PAGE_READWRITE) {
226 prot = PAGE_EXECUTE_READWRITE;
227 }
228
229 DWORD oldProt;
Andrew Kaylor1f661002012-09-19 20:46:12 +0000230 Memory::InvalidateInstructionCache(Addr, Size);
Michael J. Spencera7a90bf2011-10-13 23:16:01 +0000231 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
232 == TRUE;
Argyrios Kyrtzidisa6f9bb72008-10-04 08:15:32 +0000233}
234
Andrew Kaylor1f661002012-09-19 20:46:12 +0000235} // namespace sys
236} // namespace llvm