-
Notifications
You must be signed in to change notification settings - Fork 3
/
alloc.c
316 lines (275 loc) · 9.71 KB
/
alloc.c
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
/*
Copyright (c) 2015 Forkscan authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "alloc.h"
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "util.h"
/****************************************************************************/
/* Defines, typedefs, etc. */
/****************************************************************************/
// Block size for allocating internal data structures.
#define ALLOC_BLOCKSIZE PAGESIZE
typedef struct memory_metadata_t memory_metadata_t;
/****************************************************************************/
/* Internal memory tracking. */
/****************************************************************************/
struct memory_metadata_t
{
void *addr;
size_t length;
const char *reason;
memory_metadata_t *next, *prev;
};
static size_t LOW_ADDR (memory_metadata_t *m) {
return (size_t)m->addr;
}
static size_t HIGH_ADDR (memory_metadata_t *m) {
return LOW_ADDR(m) + m->length;
}
static memory_metadata_t *free_list = NULL;
static memory_metadata_t *alloc_list = NULL; // circular linked list.
static pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER;
/**
* Wrap mmap(), since we only really use it as a great big malloc(). This
* function will terminate the program if it is unable to allocate memory.
* @return A pointer to the newly allocated memory. Failure is not an option!
*/
static void *mmap_wrap (size_t size, int shared)
{
void *ptr = mmap(NULL, size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | (shared ? MAP_SHARED : MAP_PRIVATE),
-1, 0);
if (MAP_FAILED == ptr) {
forkscan_fatal("failed mmap().\n");
}
assert(ptr);
return ptr;
}
/**
* Wrapper for munmap to be symetrical with mmap/mmap_wrap.
*/
int munmap_wrap (void *addr, size_t length)
{
return munmap(addr, length);
}
/**
* Perform the insertion of the metadata into the list. Why not embed this
* in metadata_insert()? Sometimes we want to insert while already holding
* the lock. Note: Ensure the lock is held when calling this function.
*/
static void metadata_do_insert (memory_metadata_t *meta)
{
if (NULL == alloc_list) {
alloc_list = meta;
meta->next = meta->prev = meta;
} else if (HIGH_ADDR(alloc_list->prev) <= LOW_ADDR(meta)) {
// What's all this? This is the common case, if mmap() is allocating
// blocks at increasing addresses. This optimizes when the block
// needs to go at the end of the list.
meta->prev = alloc_list->prev;
meta->next = alloc_list;
alloc_list->prev->next = meta;
alloc_list->prev = meta;
} else {
memory_metadata_t *curr = alloc_list;
while (LOW_ADDR(curr) < HIGH_ADDR(meta)) curr = curr->next;
meta->next = curr;
meta->prev = curr->prev;
curr->prev->next = meta;
curr->prev = meta;
if (curr == alloc_list) alloc_list = meta;
}
}
/**
* Insert the metadata node into the allocated list.
*/
static void metadata_insert (memory_metadata_t *meta)
{
/* The list is sorted. There should not be many entries, so operations
should be cheap. */
pthread_mutex_lock(&list_lock);
metadata_do_insert(meta);
pthread_mutex_unlock(&list_lock);
}
/**
* Remove the memory_metadata_t object containing *addr from the allocated
* list and return it.
*/
static memory_metadata_t *metadata_remove (void *addr)
{
memory_metadata_t *curr = NULL;
assert(alloc_list); // Without the lock?! It _should_ point to something.
pthread_mutex_lock(&list_lock);
for (curr = alloc_list; curr->addr != addr; curr = curr->next);
if (NULL == curr) {
forkscan_fatal("internal error at %s:%d\n",
__FILE__, __LINE__);
}
curr->next->prev = curr->prev;
curr->prev->next = curr->next;
if (curr == alloc_list) alloc_list = curr->next;
pthread_mutex_unlock(&list_lock);
// For sanity's sake:
curr->next = curr->prev = NULL;
return curr;
}
/**
* Return a fresh memory_metadata_t object.
*/
static memory_metadata_t *metadata_new ()
{
pthread_mutex_lock(&list_lock);
if (NULL == free_list) {
// No free nodes. Make a few.
size_t offset;
char *p = (char*)mmap_wrap(ALLOC_BLOCKSIZE, 0);
// The first entry is this memory block's metadata.
memory_metadata_t *node = (memory_metadata_t*)p;
node->addr = (void*)node;
node->length = ALLOC_BLOCKSIZE;
metadata_do_insert(node);
// Turn the rest of the memory into a list of nodes.
for (offset = sizeof(memory_metadata_t);
offset < ALLOC_BLOCKSIZE - sizeof(memory_metadata_t);
offset += sizeof(memory_metadata_t)) {
node = (memory_metadata_t*)(p + offset);
node->next = free_list;
free_list = node;
}
}
// Grab the first "free" entry and return it.
memory_metadata_t *ret = free_list;
assert(ret);
free_list = free_list->next;
pthread_mutex_unlock(&list_lock);
return ret;
}
/**
* Return a memory_metadata_t object to the pool of memory whence it came.
*/
static void metadata_free (memory_metadata_t *meta)
{
pthread_mutex_lock(&list_lock);
meta->next = free_list;
free_list = meta;
pthread_mutex_unlock(&list_lock);
}
/**
* See forkscan_alloc_next_subrange(). This is not thread-safe.
*/
static mem_range_t metadata_break_range (mem_range_t *big_range)
{
mem_range_t ret = *big_range;
memory_metadata_t *curr;
assert(alloc_list);
// Advance to where big_range begins.
curr = alloc_list;
while (HIGH_ADDR(curr) <= ret.low) {
curr = curr->next;
if (curr == alloc_list) break;
}
if (HIGH_ADDR(curr) > ret.low && LOW_ADDR(curr) < ret.high) {
// Advance the beginning of the range until it reaches something we
// haven't allocated.
while (LOW_ADDR(curr) <= ret.low) {
if (HIGH_ADDR(curr) >= ret.low) {
ret.low = MIN_OF(HIGH_ADDR(curr), ret.high);
}
curr = curr->next;
if (curr == alloc_list) break;
}
if (LOW_ADDR(curr) > ret.low) {
ret.high = MIN_OF(LOW_ADDR(curr), ret.high);
}
} else {
// The big_range falls entirely outside the range of our allocations.
}
big_range->low = ret.high;
return ret;
}
static void *alloc_mmap (size_t size, const char *reason, int shared)
{
memory_metadata_t *meta = metadata_new();
assert(size % PAGESIZE == 0);
assert(meta);
meta->length = size;
meta->addr = mmap_wrap(size, shared);
meta->reason = reason;
assert(meta->addr && meta->addr != MAP_FAILED);
metadata_insert(meta);
if (0 != mprotect(meta->addr, size, PROT_READ | PROT_WRITE)) {
forkscan_diagnostic("mprotect failed %s:%d\n",
__FILE__, __LINE__);
}
return meta->addr;
}
/****************************************************************************/
/* Interface */
/****************************************************************************/
/**
* mmap() for the Forkscan system. This call never fails. But you should
* only ever ask for big chunks in multiples of the page size.
* @return The allocated memory.
*/
void *forkscan_alloc_mmap (size_t size, const char *reason)
{
return alloc_mmap(size, reason, /*shared=*/0);
}
/**
* mmap() for the Forkscan system. This call never fails. But you should
* only ever ask for big chunks in multiples of the page size. The mmapped
* memory is marked as shared among processes.
* @return The allocated memory.
*/
void *forkscan_alloc_mmap_shared (size_t size, const char *reason)
{
return alloc_mmap(size, reason, /*shared=*/1);
}
/**
* munmap() for the Forkscan system.
*/
void forkscan_alloc_munmap (void *ptr)
{
assert(ptr);
memory_metadata_t *meta = metadata_remove(ptr);
if (NULL == meta) {
forkscan_fatal("lost track of memory.\n");
}
if (0 != munmap_wrap(meta->addr, meta->length)) {
forkscan_fatal("failed munmap().\n");
}
metadata_free(meta);
}
/**
* Given a *big_range, return the first chunk of it that doesn't contain
* memory that belongs to Forkscan. *big_range is modified to show the
* remaining portion of the range. This is not thread-safe.
*
* @return A chunk of memory that does not overlap with memory owned by
* Forkscan. This chunk may have zero length if no such chunk could be
* found in *big_range.
*/
mem_range_t forkscan_alloc_next_subrange (mem_range_t *big_range)
{
return metadata_break_range(big_range);
}