IsoSpec 2.2.1
Loading...
Searching...
No Matches
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
29static 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
37static 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
58static 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
75void* 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
146int 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#if 0
157// Unused by IsoSpec
158
159
160int _mprotect(void *addr, size_t len, int prot)
161{
162 DWORD newProtect = __map_mmap_prot_page(prot);
163 DWORD oldProtect = 0;
164
165 if (VirtualProtect(addr, len, newProtect, &oldProtect))
166 return 0;
167
168 errno = __map_mman_error(GetLastError(), EPERM);
169
170 return -1;
171}
172
173int msync(void *addr, size_t len, int /* flags */)
174{
175 if (FlushViewOfFile(addr, len))
176 return 0;
177
178 errno = __map_mman_error(GetLastError(), EPERM);
179
180 return -1;
181}
182
183int mlock(const void *addr, size_t len)
184{
185 if (VirtualLock((LPVOID)addr, len))
186 return 0;
187
188 errno = __map_mman_error(GetLastError(), EPERM);
189
190 return -1;
191}
192
193int munlock(const void *addr, size_t len)
194{
195 if (VirtualUnlock((LPVOID)addr, len))
196 return 0;
197
198 errno = __map_mman_error(GetLastError(), EPERM);
199
200 return -1;
201}
202#endif
203
204#endif