Fix ThreadCachedInt race condition
[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   // For some strange reason, it's allowed to
68   // lock a nullptr as long as length is zero.
69   // VirtualLock doesn't allow it, so handle
70   // it specially.
71   if (addr == nullptr && len == 0) {
72     return 0;
73   }
74   if (!VirtualLock((void*)addr, len)) {
75     return -1;
76   }
77   return 0;
78 }
79
80 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
81   // Make sure it's something we support first.
82
83   // No Anon shared.
84   if ((flags & (MAP_ANONYMOUS | MAP_SHARED)) == (MAP_ANONYMOUS | MAP_SHARED)) {
85     return MAP_FAILED;
86   }
87   // No private copy on write.
88   if ((flags & MAP_PRIVATE) == MAP_PRIVATE && fd != -1) {
89     return MAP_FAILED;
90   }
91   // Map isn't anon, must be file backed.
92   if (!(flags & MAP_ANONYMOUS) && fd == -1) {
93     return MAP_FAILED;
94   }
95
96   DWORD newProt;
97   DWORD accessFlags;
98   if (!mmap_to_page_protection(prot, newProt, accessFlags)) {
99     return MAP_FAILED;
100   }
101
102   void* ret;
103   if (!(flags & MAP_ANONYMOUS) || (flags & MAP_SHARED)) {
104     HANDLE h = INVALID_HANDLE_VALUE;
105     if (!(flags & MAP_ANONYMOUS)) {
106       h = (HANDLE)_get_osfhandle(fd);
107     }
108
109     HANDLE fmh = CreateFileMapping(
110         h,
111         nullptr,
112         newProt,
113         (DWORD)((length >> 32) & 0xFFFFFFFF),
114         (DWORD)(length & 0xFFFFFFFF),
115         nullptr);
116     if (fmh == nullptr) {
117       return MAP_FAILED;
118     }
119     ret = MapViewOfFileEx(
120         fmh,
121         accessFlags,
122         (DWORD)((off >> 32) & 0xFFFFFFFF),
123         (DWORD)(off & 0xFFFFFFFF),
124         0,
125         addr);
126     if (ret == nullptr) {
127       ret = MAP_FAILED;
128     }
129     CloseHandle(fmh);
130   } else {
131     // VirtualAlloc rounds size down to a multiple
132     // of the system allocation granularity :(
133     length = alignToAllocationGranularity(length);
134     ret = VirtualAlloc(addr, length, MEM_COMMIT | MEM_RESERVE, newProt);
135     if (ret == nullptr) {
136       return MAP_FAILED;
137     }
138   }
139
140   // TODO: Could technically implement MAP_POPULATE via PrefetchVirtualMemory
141   //       Should also see about implementing MAP_NORESERVE
142   return ret;
143 }
144
145 int mprotect(void* addr, size_t size, int prot) {
146   DWORD newProt;
147   DWORD access;
148   if (!mmap_to_page_protection(prot, newProt, access)) {
149     return -1;
150   }
151
152   DWORD oldProt;
153   BOOL res = VirtualProtect(addr, size, newProt, &oldProt);
154   if (!res) {
155     return -1;
156   }
157   return 0;
158 }
159
160 int munlock(const void* addr, size_t length) {
161   // See comment in mlock
162   if (addr == nullptr && length == 0) {
163     return 0;
164   }
165   if (!VirtualUnlock((void*)addr, length)) {
166     return -1;
167   }
168   return 0;
169 }
170
171 int munmap(void* addr, size_t length) {
172   // Try to unmap it as a file, otherwise VirtualFree.
173   if (!UnmapViewOfFile(addr)) {
174     if (folly::kIsDebug) {
175       // We can't do partial unmapping with Windows, so
176       // assert that we aren't trying to do that if we're
177       // in debug mode.
178       MEMORY_BASIC_INFORMATION inf;
179       VirtualQuery(addr, &inf, sizeof(inf));
180       assert(inf.BaseAddress == addr);
181       assert(inf.RegionSize == alignToAllocationGranularity(length));
182     }
183     if (!VirtualFree(addr, 0, MEM_RELEASE)) {
184       return -1;
185     }
186     return 0;
187   }
188   return 0;
189 }
190 }
191 #endif