2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/portability/SysMman.h>
23 #include <folly/Portability.h>
24 #include <folly/portability/Windows.h>
26 static bool mmap_to_page_protection(int prot, DWORD& ret, DWORD& acc) {
27 if (prot == PROT_NONE) {
30 } else if (prot == PROT_READ) {
33 } else if (prot == PROT_EXEC) {
35 acc = FILE_MAP_EXECUTE;
36 } else if (prot == (PROT_READ | PROT_EXEC)) {
37 ret = PAGE_EXECUTE_READ;
38 acc = FILE_MAP_READ | FILE_MAP_EXECUTE;
39 } else if (prot == (PROT_READ | PROT_WRITE)) {
41 acc = FILE_MAP_READ | FILE_MAP_WRITE;
42 } else if (prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) {
43 ret = PAGE_EXECUTE_READWRITE;
44 acc = FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_EXECUTE;
51 static size_t alignToAllocationGranularity(size_t s) {
52 static size_t granularity = [] {
53 static SYSTEM_INFO inf;
55 return inf.dwAllocationGranularity;
57 return (s + granularity - 1) / granularity * granularity;
61 int madvise(const void* /* addr */, size_t /* len */, int /* advise */) {
62 // We do nothing at all.
63 // Could probably implement dontneed via VirtualAlloc
64 // with the MEM_RESET and MEM_RESET_UNDO flags.
68 int mlock(const void* addr, size_t len) {
69 // For some strange reason, it's allowed to
70 // lock a nullptr as long as length is zero.
71 // VirtualLock doesn't allow it, so handle
73 if (addr == nullptr && len == 0) {
76 if (!VirtualLock((void*)addr, len)) {
83 constexpr uint32_t kMMapLengthMagic = 0xFACEB00C;
84 struct MemMapDebugTrailer {
90 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
91 // Make sure it's something we support first.
94 if ((flags & (MAP_ANONYMOUS | MAP_SHARED)) == (MAP_ANONYMOUS | MAP_SHARED)) {
97 // No private copy on write.
98 if ((flags & MAP_PRIVATE) == MAP_PRIVATE && fd != -1) {
101 // Map isn't anon, must be file backed.
102 if (!(flags & MAP_ANONYMOUS) && fd == -1) {
108 if (!mmap_to_page_protection(prot, newProt, accessFlags)) {
113 if (!(flags & MAP_ANONYMOUS) || (flags & MAP_SHARED)) {
114 HANDLE h = INVALID_HANDLE_VALUE;
115 if (!(flags & MAP_ANONYMOUS)) {
116 h = (HANDLE)_get_osfhandle(fd);
119 HANDLE fmh = CreateFileMapping(
123 (DWORD)((length >> 32) & 0xFFFFFFFF),
124 (DWORD)(length & 0xFFFFFFFF),
126 if (fmh == nullptr) {
129 ret = MapViewOfFileEx(
132 (DWORD)(0), // off_t is only 32-bit :(
133 (DWORD)(off & 0xFFFFFFFF),
136 if (ret == nullptr) {
141 auto baseLength = length;
142 if (folly::kIsDebug) {
143 // In debug mode we keep track of the length to make
144 // sure you're only munmapping the entire thing if
145 // we're using VirtualAlloc.
146 length += sizeof(MemMapDebugTrailer);
149 // VirtualAlloc rounds size down to a multiple
150 // of the system allocation granularity :(
151 length = alignToAllocationGranularity(length);
152 ret = VirtualAlloc(addr, length, MEM_COMMIT | MEM_RESERVE, newProt);
153 if (ret == nullptr) {
157 if (folly::kIsDebug) {
158 auto deb = (MemMapDebugTrailer*)((char*)ret + baseLength);
159 deb->length = baseLength;
160 deb->magic = kMMapLengthMagic;
164 // TODO: Could technically implement MAP_POPULATE via PrefetchVirtualMemory
165 // Should also see about implementing MAP_NORESERVE
169 int mprotect(void* addr, size_t size, int prot) {
172 if (!mmap_to_page_protection(prot, newProt, access)) {
177 BOOL res = VirtualProtect(addr, size, newProt, &oldProt);
184 int munlock(const void* addr, size_t length) {
185 // See comment in mlock
186 if (addr == nullptr && length == 0) {
189 if (!VirtualUnlock((void*)addr, length)) {
195 int munmap(void* addr, size_t length) {
196 // Try to unmap it as a file, otherwise VirtualFree.
197 if (!UnmapViewOfFile(addr)) {
198 if (folly::kIsDebug) {
199 // We can't do partial unmapping with Windows, so
200 // assert that we aren't trying to do that if we're
202 MEMORY_BASIC_INFORMATION inf;
203 VirtualQuery(addr, &inf, sizeof(inf));
204 assert(inf.AllocationBase == addr);
206 auto deb = (MemMapDebugTrailer*)((char*)addr + length);
207 assert(deb->length == length);
208 assert(deb->magic == kMMapLengthMagic);
210 if (!VirtualFree(addr, 0, MEM_RELEASE)) {