9827f045dd6731d8999d87937880bce1e18678ad
[folly.git] / folly / portability / SysMman.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/portability/SysMman.h>
18
19 #ifdef _WIN32
20 #include <cassert>
21 #include <folly/Portability.h>
22 #include <folly/portability/Windows.h>
23
24 static bool mmap_to_page_protection(int prot, DWORD& ret, DWORD& acc) {
25   if (prot == PROT_NONE) {
26     ret = PAGE_NOACCESS;
27     acc = 0;
28   } else if (prot == PROT_READ) {
29     ret = PAGE_READONLY;
30     acc = FILE_MAP_READ;
31   } else if (prot == PROT_EXEC) {
32     ret = PAGE_EXECUTE;
33     acc = FILE_MAP_EXECUTE;
34   } else if (prot == (PROT_READ | PROT_EXEC)) {
35     ret = PAGE_EXECUTE_READ;
36     acc = FILE_MAP_READ | FILE_MAP_EXECUTE;
37   } else if (prot == (PROT_READ | PROT_WRITE)) {
38     ret = PAGE_READWRITE;
39     acc = FILE_MAP_READ | FILE_MAP_WRITE;
40   } else if (prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) {
41     ret = PAGE_EXECUTE_READWRITE;
42     acc = FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_EXECUTE;
43   } else {
44     return false;
45   }
46   return true;
47 }
48
49 static size_t alignToAllocationGranularity(size_t s) {
50   static size_t granularity = [] {
51     static SYSTEM_INFO inf;
52     GetSystemInfo(&inf);
53     return inf.dwAllocationGranularity;
54   }();
55   return (s + granularity - 1) / granularity * granularity;
56 }
57
58 extern "C" {
59 int madvise(const void* addr, size_t len, int advise) {
60   // We do nothing at all.
61   // Could probably implement dontneed via VirtualAlloc
62   // with the MEM_RESET and MEM_RESET_UNDO flags.
63   return 0;
64 }
65
66 int mlock(const void* addr, size_t len) {
67   if (!VirtualLock((void*)addr, len)) {
68     return -1;
69   }
70   return 0;
71 }
72
73 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
74   // Make sure it's something we support first.
75
76   // No Anon shared.
77   if ((flags & (MAP_ANONYMOUS | MAP_SHARED)) == (MAP_ANONYMOUS | MAP_SHARED)) {
78     return MAP_FAILED;
79   }
80   // No private copy on write.
81   if ((flags & MAP_PRIVATE) == MAP_PRIVATE && fd != -1) {
82     return MAP_FAILED;
83   }
84   // Map isn't anon, must be file backed.
85   if (!(flags & MAP_ANONYMOUS) && fd == -1) {
86     return MAP_FAILED;
87   }
88
89   DWORD newProt;
90   DWORD accessFlags;
91   if (!mmap_to_page_protection(prot, newProt, accessFlags)) {
92     return MAP_FAILED;
93   }
94
95   void* ret;
96   if (!(flags & MAP_ANONYMOUS) || (flags & MAP_SHARED)) {
97     HANDLE h = INVALID_HANDLE_VALUE;
98     if (!(flags & MAP_ANONYMOUS)) {
99       h = (HANDLE)_get_osfhandle(fd);
100     }
101
102     HANDLE fmh = CreateFileMapping(
103         h,
104         nullptr,
105         newProt,
106         (DWORD)((length >> 32) & 0xFFFFFFFF),
107         (DWORD)(length & 0xFFFFFFFF),
108         nullptr);
109     ret = MapViewOfFileEx(
110         fmh,
111         accessFlags,
112         (DWORD)((off >> 32) & 0xFFFFFFFF),
113         (DWORD)(off & 0xFFFFFFFF),
114         0,
115         addr);
116     if (ret == nullptr) {
117       ret = MAP_FAILED;
118     }
119     CloseHandle(fmh);
120   } else {
121     // VirtualAlloc rounds size down to a multiple
122     // of the system allocation granularity :(
123     length = alignToAllocationGranularity(length);
124     ret = VirtualAlloc(addr, length, MEM_COMMIT | MEM_RESERVE, newProt);
125     if (ret == nullptr) {
126       return MAP_FAILED;
127     }
128   }
129
130   // TODO: Could technically implement MAP_POPULATE via PrefetchVirtualMemory
131   //       Should also see about implementing MAP_NORESERVE
132   return ret;
133 }
134
135 int mprotect(void* addr, size_t size, int prot) {
136   DWORD newProt;
137   DWORD access;
138   if (!mmap_to_page_protection(prot, newProt, access)) {
139     return -1;
140   }
141
142   DWORD oldProt;
143   BOOL res = VirtualProtect(addr, size, newProt, &oldProt);
144   if (!res) {
145     return -1;
146   }
147   return 0;
148 }
149
150 int munlock(const void* addr, size_t length) {
151   if (!VirtualUnlock((void*)addr, length)) {
152     return -1;
153   }
154   return 0;
155 }
156
157 int munmap(void* addr, size_t length) {
158   // Try to unmap it as a file, otherwise VirtualFree.
159   if (!UnmapViewOfFile(addr)) {
160     if (folly::kIsDebug) {
161       // We can't do partial unmapping with Windows, so
162       // assert that we aren't trying to do that if we're
163       // in debug mode.
164       MEMORY_BASIC_INFORMATION inf;
165       VirtualQuery(addr, &inf, sizeof(inf));
166       assert(inf.BaseAddress == addr);
167       assert(inf.RegionSize == alignToAllocationGranularity(length));
168     }
169     if (!VirtualFree(addr, 0, MEM_RELEASE)) {
170       return -1;
171     }
172     return 0;
173   }
174   return 0;
175 }
176 }
177 #endif