forked from dzeban/restrict-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memrestrict.c
321 lines (243 loc) · 6.35 KB
/
memrestrict.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
317
318
319
320
#define _GNU_SOURCE // Needed for RTLD_NEXT
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <uthash.h>
static void* (*libc_malloc) (size_t) = NULL;
static void* (*libc_calloc) (size_t, size_t) = NULL;
static void* (*libc_realloc)(void *, size_t) = NULL;
static void (*libc_free) (void *) = NULL;
static void* (*libc_mmap)(void *, size_t, int, int, int, off_t) = NULL;
static void* (*libc_mmap2)(void *, size_t, int, int, int, off_t) = NULL;
static void* (*libc_mremap)(void *, size_t, size_t, int, ...) = NULL;
static int (*libc_brk)(void *) = NULL;
static void* (*libc_sbrk)(intptr_t) = NULL;
struct malloc_item
{
void *p;
size_t size;
UT_hash_handle hh; // makes this structure hashable
};
// Hash table for accounting malloc/free
struct malloc_item *HT = NULL;
// Total memory allocated
static unsigned long mem_allocated = 0;
static unsigned long mem_threshold = 0;
#define DEFAULT_MEM_THRESHOLD 2 * 1048576 // Default threshold is 2 MiB;
// Thread-local var to prevent malloc/free recursion
static __thread int no_hook;
static int DEBUG = -1;
#define log(M, ...) do {\
if (DEBUG) { fprintf(stderr, "MEMRESTRICT:%d " M "\n", __LINE__, ##__VA_ARGS__); }\
} while(0)
// Wrapper to save original libc functions to global var
#define SAVE_LIBC_FUNC(var, f) do { \
var = dlsym(RTLD_NEXT, f);\
if (NULL == var) \
perror("dlsym");\
} while(0)
static void account_alloc(void *ptr, size_t size)
{
// Do not account itself
no_hook = 1;
// Allocating memory
if (size != 0) {
struct malloc_item *item, *out;
item = malloc(sizeof(*item));
item->p = ptr;
item->size = size;
HASH_ADD_PTR(HT, p, item);
mem_allocated += size;
log("Alloc: %p -> %zu\n", ptr, size);
}
// Freeing memory
else {
struct malloc_item *found;
HASH_FIND_PTR(HT, &ptr, found);
if (found) {
mem_allocated -= found->size;
log("Free: %p -> %zu\n", found->p, found->size);
HASH_DEL(HT, found);
free(found);
} else {
log("Freeing unaccounted allocation %p\n", ptr);
}
}
log(" [[[::: %d (%u) :::]]] \n", mem_allocated, HASH_COUNT(HT));
no_hook = 0;
}
static void account_realloc(void *p, void *ptr, size_t size)
{
// Do not account itself
no_hook = 1;
// ptr == NULL is equivalent to malloc(size)
if (ptr == NULL) {
account_alloc(p, size);
}
// size == 0 is equivalent to free(ptr),
// and p will be NULL
else if (size == 0) {
account_alloc(ptr, size);
}
// Now the real realloc
else {
log("Realloc: %p -> %d\n", ptr, size);
// if ptr was moved previous area will be freed
if (p != ptr) {
log("Realloc: Replacing pointer %p to %p\n", ptr, p);
account_alloc(ptr, 0);
account_alloc(p, size);
} else {
struct malloc_item *found;
int alloc_diff;
HASH_FIND_PTR(HT, &ptr, found);
if (found) {
// alloc_diff may be negative when shrinking memory
alloc_diff = size - found->size;
mem_allocated += alloc_diff;
found->size += alloc_diff;
log("Realloc: diff %p -> %d\n", ptr, alloc_diff);
} else {
log("Reallocating unaccounted pointer %p\n", ptr);
}
}
}
log(" [[[::: %d (%u) :::]]] \n", mem_allocated, HASH_COUNT(HT));
no_hook = 0;
}
// Initialize library parameters from environment.
// We don't have main(), so we have to call it in every callback.
static inline void init_env()
{
char *t;
if (mem_threshold == 0) {
t = secure_getenv("MR_THRESHOLD");
if (t)
mem_threshold = strtol(t, NULL, 0);
else
mem_threshold = DEFAULT_MEM_THRESHOLD;
}
if (DEBUG == -1) {
t = secure_getenv("MR_DEBUG");
if (t)
DEBUG = strtol(t, NULL, 0);
else
DEBUG = 0;
}
}
void *malloc(size_t size)
{
void *p = NULL;
if (libc_malloc == NULL)
SAVE_LIBC_FUNC(libc_malloc, "malloc");
init_env();
if (mem_allocated + size <= mem_threshold) {
p = libc_malloc(size);
} else {
log("Restricting malloc of %zu bytes\n", size);
errno = ENOMEM;
return NULL;
}
if (!no_hook)
account_alloc(p, size);
return p;
}
void *calloc(size_t nmemb, size_t size)
{
void *p = NULL;
if (libc_calloc == NULL)
SAVE_LIBC_FUNC(libc_calloc, "calloc");
init_env();
if (mem_allocated + nmemb * size <= mem_threshold) {
p = libc_calloc(nmemb, size);
} else {
log("Restricting calloc of %zu elements per %zu size\n", nmemb, size);
errno = ENOMEM;
return NULL;
}
if (!no_hook)
account_alloc(p, nmemb * size);
return p;
}
void *realloc(void *ptr, size_t size)
{
void *p = NULL;
if (libc_realloc == NULL)
SAVE_LIBC_FUNC(libc_realloc, "realloc");
init_env();
// TODO: CHECK new heap size *before* allowing realloc
if (mem_allocated <= mem_threshold) {
p = libc_realloc(ptr, size);
} else {
errno = ENOMEM;
return NULL;
}
if (!no_hook)
account_realloc(p, ptr, size);
return p;
}
void free(void *ptr)
{
if (libc_free == NULL)
SAVE_LIBC_FUNC(libc_free, "free");
init_env();
libc_free(ptr);
if (!no_hook)
account_alloc(ptr, 0);
}
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
void *p;
log("mmap for %p of length %zu prot %d flags %d fd %d offset %lld\n",
addr, length, prot, flags, fd, offset);
if (libc_mmap == NULL)
SAVE_LIBC_FUNC(libc_mmap, "mmap");
p = libc_mmap(addr, length, prot, flags, fd, offset);
return p;
}
void *mmap2(void *addr, size_t length, int prot, int flags, int fd, off_t pgoffset)
{
void *p;
log("mmap2 for %p of length %zu prot %d flags %d fd %d pgoffset %lld\n",
addr, length, prot, flags, fd, pgoffset);
if (libc_mmap2 == NULL)
SAVE_LIBC_FUNC(libc_mmap2, "mmap2");
p = libc_mmap2(addr, length, prot, flags, fd, pgoffset);
return p;
}
void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ...)
{
va_list ap;
void *new_address;
void *p;
log("mremap for %p of size %zu, new_size %zu, flags %d\n",
old_address, old_size, new_size, flags);
if (libc_mremap == NULL)
SAVE_LIBC_FUNC(libc_mremap, "mremap");
va_start(ap, flags);
new_address = va_arg(ap, void *);
va_end(ap);
p = libc_mremap(old_address, old_size, new_size, flags, new_address);
return p;
}
int brk(void *addr)
{
int ret;
log("brk set to %p\n", addr);
if (libc_brk == NULL)
SAVE_LIBC_FUNC(libc_brk, "brk");
ret = libc_brk(addr);
return ret;
}
void *sbrk(intptr_t increment)
{
void *p;
log("sbrk increment %d\n", increment);
if (libc_sbrk == NULL)
SAVE_LIBC_FUNC(libc_sbrk, "sbrk");
p = sbrk(increment);
return p;
}