forked from johnsonjh/duma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paging.h
378 lines (327 loc) · 10.4 KB
/
paging.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* DUMA - Red-Zone memory allocator.
* Copyright (C) 2002-2009 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
* Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
* Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
* License: GNU GPL (GNU General Public License, see COPYING-GPL)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* FILE CONTENTS:
* internal implementation file
* contains system/platform dependent paging functions
*/
#ifndef DUMA_PAGING_H
#define DUMA_PAGING_H
/*
* Lots of systems are missing the definition of PROT_NONE.
*/
#ifndef PROT_NONE
#define PROT_NONE 0
#endif
/*
* 386 BSD has MAP_ANON instead of MAP_ANONYMOUS.
*/
#if (!defined(MAP_ANONYMOUS) && defined(MAP_ANON))
#define MAP_ANONYMOUS MAP_ANON
#endif
/* declarations */
#include "print.h"
/*
* For some reason, I can't find mprotect() in any of the headers on
* IRIX or SunOS 4.1.2
*/
/* extern C_LINKAGE int mprotect(void * addr, size_t len, int prot); */
#if !defined(WIN32)
static void *startAddr = (void *)0;
#endif
/* Function: stringErrorReport
*
* Get formatted error string and return. For WIN32
* FormatMessage is used, strerror all else.
*/
static const char *stringErrorReport(void) {
#if defined(WIN32)
DWORD LastError;
LPVOID lpMsgBuf;
LastError = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, LastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) /* Default language */
,
(LPTSTR)&lpMsgBuf, 0, NULL);
return (char *)lpMsgBuf; /* "Unknown error.\n"; */
#else
#ifndef DUMA_NO_STRERROR
static int failing;
if (!failing) {
const char *str;
failing++;
str = strerror(errno);
failing--;
if (str != NULL)
return str;
}
#endif
return DUMA_strerror(errno);
#endif
}
/* Function: mprotectFailed
*
* Report that VirtualProtect or mprotect failed and abort
* program execution.
*/
static void mprotectFailed(void) {
#if defined(WIN32)
DUMA_Abort("VirtualProtect() failed: %s", stringErrorReport());
#else
DUMA_Abort("mprotect() failed: %s.\nCheck README section 'MEMORY USAGE AND "
"EXECUTION SPEED'\n your (Linux) system may limit the number of "
"different page mappings per process",
stringErrorReport());
#endif
}
/* Function: Page_Create
*
* Create memory. Allocates actual memory. Uses
* VirtualAlloc on windows and mmap on unix.
*
* See Also:
* <Page_Delete>
*/
static void *Page_Create(size_t size, int exitonfail, int printerror) {
void *allocation;
#if defined(WIN32)
allocation =
VirtualAlloc(NULL /* address of region to reserve or commit */
,
(DWORD)size /* size of region */
,
(DWORD)MEM_COMMIT /* type of allocation */
,
(DWORD)PAGE_READWRITE /* type of access protection */
);
if ((void *)0 == allocation) {
if (exitonfail)
DUMA_Abort("VirtualAlloc(%d) failed: %s", (DUMA_SIZE)size,
stringErrorReport());
else if (printerror)
DUMA_Print("\nDUMA warning: VirtualAlloc(%d) failed: %s", (DUMA_SIZE)size,
stringErrorReport());
}
#elif defined(MAP_ANONYMOUS)
/*
* In this version, "startAddr" is a _hint_, not a demand.
* When the memory I map here is contiguous with other
* mappings, the allocator can coalesce the memory from two
* or more mappings into one large contiguous chunk, and thus
* might be able to find a fit that would not otherwise have
* been possible. I could _force_ it to be contiguous by using
* the MMAP_FIXED flag, but I don't want to stomp on memory mappings
* generated by other software, etc.
*/
allocation = mmap(startAddr, (int)size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#ifndef __hpux
/*
* Set the "address hint" for the next mmap() so that it will abut
* the mapping we just created.
*
* HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes
* when given a non-zero address hint, so we'll leave the hint set
* to zero on that system. HP recently told me this is now fixed.
* Someone please tell me when it is probable to assume that most
* of those systems that were running 9.01 have been upgraded.
*/
startAddr = allocation + size;
#endif
if (allocation == (void *)-1) {
allocation = (void *)0;
if (exitonfail)
DUMA_Abort("mmap(%d) failed: %s", (DUMA_SIZE)size, stringErrorReport());
else if (printerror)
DUMA_Print("\nDUMA warning: mmap(%d) failed: %s", (DUMA_SIZE)size,
stringErrorReport());
}
#else
static int devZeroFd = -1;
if (devZeroFd == -1) {
devZeroFd = open("/dev/zero", O_RDWR);
if (devZeroFd < 0)
DUMA_Abort("open() on /dev/zero failed: %s", stringErrorReport());
}
/*
* In this version, "startAddr" is a _hint_, not a demand.
* When the memory I map here is contiguous with other
* mappings, the allocator can coalesce the memory from two
* or more mappings into one large contiguous chunk, and thus
* might be able to find a fit that would not otherwise have
* been possible. I could _force_ it to be contiguous by using
* the MMAP_FIXED flag, but I don't want to stomp on memory mappings
* generated by other software, etc.
*/
allocation = mmap(startAddr, (int)size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
devZeroFd, 0);
startAddr = allocation + size;
if (allocation == (void *)-1) {
allocation = (void *)0;
if (exitonfail)
DUMA_Abort("mmap(%d) failed: %s", (DUMA_SIZE)size, stringErrorReport());
else if (printerror)
DUMA_Print("\nDUMA warning: mmap(%d) failed: %s", (DUMA_SIZE)size,
stringErrorReport());
}
#endif
// TESTING
// memset((void*)allocation, 0, startAddr);
return (void *)allocation;
}
/* Function: Page_AllowAccess
*
* Allow memory access to allocated memory.
*
* See Also:
* <Page_DenyAccess>
*/
void Page_AllowAccess(void *address, size_t size) {
#if defined(WIN32)
SIZE_T OldProtect, retQuery;
MEMORY_BASIC_INFORMATION MemInfo;
size_t tail_size;
BOOL ret;
while (size > 0) {
retQuery = VirtualQuery(address, &MemInfo, sizeof(MemInfo));
if (retQuery < sizeof(MemInfo))
DUMA_Abort("VirtualQuery() failed\n");
tail_size = (size > MemInfo.RegionSize) ? MemInfo.RegionSize : size;
ret = VirtualProtect(
(LPVOID)address /* address of region of committed pages */
,
(DWORD)tail_size /* size of the region */
,
(DWORD)PAGE_READWRITE /* desired access protection */
,
(PDWORD)&OldProtect /* address of variable to get old protection */
);
if (0 == ret)
mprotectFailed();
address = ((char *)address) + tail_size;
size -= tail_size;
}
#else
if (mprotect(address, size, PROT_READ | PROT_WRITE) < 0)
mprotectFailed();
#endif
}
/* Function: Page_DenyAccess
*
* Deny access to allocated memory region.
*
* See Also:
* <Page_AllowAccess>
*/
static void Page_DenyAccess(void *address, size_t size) {
#if defined(WIN32)
SIZE_T OldProtect, retQuery;
MEMORY_BASIC_INFORMATION MemInfo;
size_t tail_size;
BOOL ret;
while (size > 0) {
retQuery = VirtualQuery(address, &MemInfo, sizeof(MemInfo));
if (retQuery < sizeof(MemInfo))
DUMA_Abort("VirtualQuery() failed\n");
tail_size = (size > MemInfo.RegionSize) ? MemInfo.RegionSize : size;
ret = VirtualProtect(
(LPVOID)address /* address of region of committed pages */
,
(DWORD)tail_size /* size of the region */
,
(DWORD)PAGE_NOACCESS /* desired access protection */
,
(PDWORD)&OldProtect /* address of variable to get old protection */
);
if (0 == ret)
mprotectFailed();
address = ((char *)address) + tail_size;
size -= tail_size;
}
#else
if (mprotect(address, size, PROT_NONE) < 0)
mprotectFailed();
#endif
}
/* Function: Page_Delete
*
* Free's DUMA allocated memory. This is the real deal, make sure
* the page is no longer in our slot list first!
*
* See Also:
* <Page_Create>
*/
static void Page_Delete(void *address, size_t size) {
#if defined(WIN32)
void *alloc_address = address;
SIZE_T retQuery;
MEMORY_BASIC_INFORMATION MemInfo;
BOOL ret;
/* release physical memory commited to virtual address space */
while (size > 0) {
retQuery = VirtualQuery(address, &MemInfo, sizeof(MemInfo));
if (retQuery < sizeof(MemInfo))
DUMA_Abort("VirtualQuery() failed\n");
if (MemInfo.State == MEM_COMMIT) {
ret =
VirtualFree((LPVOID)MemInfo.BaseAddress /* base of committed pages */
,
(DWORD)MemInfo.RegionSize /* size of the region */
,
(DWORD)MEM_DECOMMIT /* type of free operation */
);
if (0 == ret)
DUMA_Abort("VirtualFree(,,MEM_DECOMMIT) failed: %s",
stringErrorReport());
}
address = ((char *)address) + MemInfo.RegionSize;
size -= MemInfo.RegionSize;
}
/* release virtual address space */
ret = VirtualFree((LPVOID)alloc_address, (DWORD)0, (DWORD)MEM_RELEASE);
if (0 == ret)
DUMA_Abort("VirtualFree(,,MEM_RELEASE) failed: %s", stringErrorReport());
#else
if (munmap(address, size) < 0)
Page_DenyAccess(address, size);
#endif
}
/* Function: Page_Size
*
* Retrieve page size.
*/
static size_t Page_Size(void) {
#if defined(WIN32)
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
return (size_t)SystemInfo.dwPageSize;
#elif defined(_SC_PAGESIZE)
return (size_t)sysconf(_SC_PAGESIZE);
#elif defined(_SC_PAGE_SIZE)
return (size_t)sysconf(_SC_PAGE_SIZE);
#else
return getpagesize();
#endif
}
#endif /* DUMA_PAGING_H */