IsoSpec  2.1.2
mman.cpp
1 /*
2  * NOLINT(legal/copyright) - the original authors did not slap a (C) notice in here,
3  * for whatever reason, and I'm in no position to do that for them.
4  *
5  * This file has been included as a part of IsoSpec project, under a MIT licence. It
6  * comes from the repository:
7  *
8  * https://github.com/witwall/mman-win32
9  *
10  * which itself is a mirror of:
11  *
12  * https://code.google.com/archive/p/mman-win32/
13  */
14 
15 #include "platform.h"
16 #if ISOSPEC_GOT_MMAN && !ISOSPEC_GOT_SYSTEM_MMAN
17 
18 #include <windows.h>
19 #include <errno.h>
20 #include <io.h>
21 
22 #include "mman.h"
23 
24 #ifndef FILE_MAP_EXECUTE
25 #define FILE_MAP_EXECUTE 0x0020
26 #endif /* FILE_MAP_EXECUTE */
27 
28 
29 static int __map_mman_error(const DWORD err, const int deferr)
30 {
31  if (err == 0)
32  return 0;
33  // TODO: implement NOLINT(readability/todo) - well, should be assigned to the original authors
34  return err;
35 }
36 
37 static DWORD __map_mmap_prot_page(const int prot)
38 {
39  DWORD protect = 0;
40 
41  if (prot == PROT_NONE)
42  return protect;
43 
44  if ((prot & PROT_EXEC) != 0)
45  {
46  protect = ((prot & PROT_WRITE) != 0) ?
47  PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
48  }
49  else
50  {
51  protect = ((prot & PROT_WRITE) != 0) ?
52  PAGE_READWRITE : PAGE_READONLY;
53  }
54 
55  return protect;
56 }
57 
58 static DWORD __map_mmap_prot_file(const int prot)
59 {
60  DWORD desiredAccess = 0;
61 
62  if (prot == PROT_NONE)
63  return desiredAccess;
64 
65  if ((prot & PROT_READ) != 0)
66  desiredAccess |= FILE_MAP_READ;
67  if ((prot & PROT_WRITE) != 0)
68  desiredAccess |= FILE_MAP_WRITE;
69  if ((prot & PROT_EXEC) != 0)
70  desiredAccess |= FILE_MAP_EXECUTE;
71 
72  return desiredAccess;
73 }
74 
75 void* mmap(void *addr, size_t len, int prot, int flags, int fildes, OffsetType off)
76 {
77  HANDLE fm, h;
78 
79  void * map = MAP_FAILED;
80 
81 #ifdef _MSC_VER
82 #pragma warning(push)
83 #pragma warning(disable: 4293)
84 #endif
85 
86  const DWORD dwFileOffsetLow = (sizeof(OffsetType) <= sizeof(DWORD)) ?
87  (DWORD)off : (DWORD)(off & 0xFFFFFFFFL);
88  const DWORD dwFileOffsetHigh = (sizeof(OffsetType) <= sizeof(DWORD)) ?
89  (DWORD)0 : (DWORD)((off >> 32) & 0xFFFFFFFFL);
90  const DWORD protect = __map_mmap_prot_page(prot);
91  const DWORD desiredAccess = __map_mmap_prot_file(prot);
92 
93  const OffsetType maxSize = off + (OffsetType)len;
94 
95  const DWORD dwMaxSizeLow = (sizeof(OffsetType) <= sizeof(DWORD)) ?
96  (DWORD)maxSize : (DWORD)(maxSize & 0xFFFFFFFFL);
97  const DWORD dwMaxSizeHigh = (sizeof(OffsetType) <= sizeof(DWORD)) ?
98  (DWORD)0 : (DWORD)((maxSize >> 32) & 0xFFFFFFFFL);
99 
100 #ifdef _MSC_VER
101 #pragma warning(pop)
102 #endif
103 
104  errno = 0;
105 
106  if (len == 0
107  /* Unsupported flag combinations */
108  || (flags & MAP_FIXED) != 0
109  /* Usupported protection combinations */
110  || prot == PROT_EXEC)
111  {
112  errno = EINVAL;
113  return MAP_FAILED;
114  }
115 
116  h = ((flags & MAP_ANONYMOUS) == 0) ?
117  (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE;
118 
119  if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE)
120  {
121  errno = EBADF;
122  return MAP_FAILED;
123  }
124 
125  fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL);
126 
127  if (fm == NULL)
128  {
129  errno = __map_mman_error(GetLastError(), EPERM);
130  return MAP_FAILED;
131  }
132 
133  map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len);
134 
135  CloseHandle(fm);
136 
137  if (map == NULL)
138  {
139  errno = __map_mman_error(GetLastError(), EPERM);
140  return MAP_FAILED;
141  }
142 
143  return map;
144 }
145 
146 int munmap(void *addr, size_t len)
147 {
148  if (UnmapViewOfFile(addr))
149  return 0;
150 
151  errno = __map_mman_error(GetLastError(), EPERM);
152 
153  return -1;
154 }
155 
156 int _mprotect(void *addr, size_t len, int prot)
157 {
158  DWORD newProtect = __map_mmap_prot_page(prot);
159  DWORD oldProtect = 0;
160 
161  if (VirtualProtect(addr, len, newProtect, &oldProtect))
162  return 0;
163 
164  errno = __map_mman_error(GetLastError(), EPERM);
165 
166  return -1;
167 }
168 
169 int msync(void *addr, size_t len, int flags)
170 {
171  if (FlushViewOfFile(addr, len))
172  return 0;
173 
174  errno = __map_mman_error(GetLastError(), EPERM);
175 
176  return -1;
177 }
178 
179 int mlock(const void *addr, size_t len)
180 {
181  if (VirtualLock((LPVOID)addr, len))
182  return 0;
183 
184  errno = __map_mman_error(GetLastError(), EPERM);
185 
186  return -1;
187 }
188 
189 int munlock(const void *addr, size_t len)
190 {
191  if (VirtualUnlock((LPVOID)addr, len))
192  return 0;
193 
194  errno = __map_mman_error(GetLastError(), EPERM);
195 
196  return -1;
197 }
198 
199 
200 #endif