-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_malloc.c
368 lines (317 loc) · 8.96 KB
/
clean_malloc.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
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
/**
* Copyright (c) 2012 Jean-Christophe DUBOIS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1, 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* @file clear_malloc.c
* @author Jean-Christophe DUBOIS ([email protected])
* @brief memory allocation interposing library.
*
* The purpose of this library is to make sure that any deallocated
* memory (through free()) is set to 0 before being given back to the
* system.
* This allow to avoid that application data are still used (through
* invalid pointer) after they have been released.
* It also allow to prevent that deallocated data is found in newly
* allocated memory blocks.
*
* This library redefines the following functions:
* - malloc
* - calloc
* - realloc
* - valloc (deprecated)
* - memalign (deprecated)
* - posix_memalign
* - free
*
* In turn these functions will use the following functions from glibc.
* - malloc
* - free
* - posix_memalign
*
* Usage: LD_PRELOAD=./clean_malloc.so command args ...
*
* Changes:
*
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define __USE_GNU
#include <dlfcn.h>
static void *default_malloc(size_t size);
static void default_free(void *ptr);
static int default_posix_memalign(void **memptr, size_t alignment, size_t size);
static void *(*real_malloc) (size_t size) = default_malloc;
static void (*real_free) (void *ptr) = default_free;
static int (*real_posix_memalign) (void **memptr, size_t alignment,
size_t size) = default_posix_memalign;
#define MIN(a,b) ((a>b) ? b : a)
#ifdef DEBUG
#define debug(fmt, ...) fprintf(stderr, "%s " fmt, __func__, ## __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif
#ifdef CHECK_COOKIE
#define ALLOC_COOKIE 0x12345678
#endif
struct alloc_header {
#ifdef CHECK_COOKIE
unsigned int cookie;
unsigned int dummy;
#endif
void *ptr;
size_t requested_size;
};
#define EXTRA_STATIC_SPACE 128
static char extra_space[EXTRA_STATIC_SPACE];
static int extra_space_count = 0;
/**
* We use a constructor to lookup the malloc/free/posix_memalign addresses
* of the glibc functions.
* However it does happen that our functions are called before the
* constructor is called. In such instance we setup some default versions
* of these functions that will force the call to the constructor.
*/
__attribute__ ((constructor))
void init_malloc(void)
{
static int init_done;
void *ptr;
/*
* TBD: We should protect init_done access/modification in case of
* multithreaded application. But as it is mostly used by the
* dynamic linker it might not be necessary as we should not be
* in multithreaded mode a this time.
*/
if (init_done) {
return;
}
init_done = 1;
/* We resolve the various symbols we are going to overload and use */
ptr = dlsym(RTLD_NEXT, "malloc");
if (ptr) {
real_malloc = ptr;
} else {
debug("malloc %s\n", dlerror());
}
ptr = dlsym(RTLD_NEXT, "free");
if (ptr) {
real_free = ptr;
} else {
debug("free %s\n", dlerror());
}
ptr = dlsym(RTLD_NEXT, "posix_memalign");
if (ptr) {
real_posix_memalign = ptr;
} else {
debug("posix_memalign %s\n", dlerror());
}
}
/*
* There is a chicken and egg problem with calloc. dlsym
* is calling calloc. So the constructor (init_malloc) will call calloc
* to resolve the malloc/realloc/free/... funtions.
* Our calloc in turn will call real_malloc and we will end up here when
* the constructor is not yet done.
* Therefore we need to write a very crude malloc that will return a bit
* of space when dlsym needs it.
* In other cases it will force the call of the constructor.
*/
static void *default_malloc(size_t size)
{
/*
* TBD: We should protect extra_space_count access/modification
* Now at the time it is used there should not be that many
* threads. So is it really needed?
*/
char *ptr = &extra_space[extra_space_count];
if ((extra_space_count + size) > EXTRA_STATIC_SPACE) {
/*
* If too much data is requested, this is not dlsym.
* So we can force the call to init_malloc.
*/
init_malloc();
if (real_malloc == default_malloc) {
debug("Failed to resolve 'malloc', returning NULL\n");
return NULL;
}
return real_malloc(size);
} else {
/* it is assumed this space will never be released */
extra_space_count += size;
}
return ptr;
}
/**
* For this default free function, we force the constructor and call the
* real free if the function address resolution was successful.
*/
static void default_free(void *ptr)
{
/*
* if free is called before the constructor, we force the
* constructor.
*/
init_malloc();
/* if real_free was not found, we return NULL */
if (real_free == default_free) {
debug("Failed to resolve 'free'\n");
return;
}
/* We can now use the real_free */
real_free(ptr);
}
/**
* For this default posix_memalign function, we force the constructor and
* call the real posix_memalign if the function address resolution was
* successful.
*/
static int default_posix_memalign(void **memptr, size_t alignment, size_t size)
{
/*
* if posix_memalign is called before the constructor, we force the
* constructor.
*/
init_malloc();
/* if real_posix_memalign was not found, we return NULL */
if (real_posix_memalign == default_posix_memalign) {
debug
("Failed to resolve 'default_posix_memalign', returning NULL\n");
return -ENOMEM;
}
/* We can now use the real_posix_memalign */
return real_posix_memalign(memptr, alignment, size);
}
/**
* This is our malloc function. Basically we add a header to the requested
* memory where we can store the size of the allocated memory and the real
* pointer to the block.
* We need to support malloc(0) as the glibc malloc function returns a
* valid pointer in such case. Some RegExp functions (among others) do not
* expect malloc(0) to return NULL.
*/
void *malloc(size_t size)
{
void *ptr = NULL;
struct alloc_header alloc_header;
size_t allocated_size;
alloc_header.requested_size = size;
allocated_size = alloc_header.requested_size + sizeof(alloc_header);
#ifdef CHECK_COOKIE
alloc_header.cookie = ALLOC_COOKIE;
alloc_header.dummy = 0;
#endif
alloc_header.ptr = real_malloc(allocated_size);
if (alloc_header.ptr) {
*(struct alloc_header *)alloc_header.ptr = alloc_header;
ptr = alloc_header.ptr + sizeof(alloc_header);
}
return ptr;
}
/**
* For calloc, we just call malloc then memset the area to 0
*/
void *calloc(size_t nmemb, size_t size)
{
void *ptr = malloc(size * nmemb);
if (ptr && (size * nmemb)) {
memset(ptr, 0, size * nmemb);
}
return ptr;
}
/**
* for free, we memset the allocated memory to 0 (using the header
* information, before calling the real free function
*/
void free(void *ptr)
{
if (ptr) {
struct alloc_header *store_ptr = (struct alloc_header *)ptr;
store_ptr--;
#ifdef CHECK_COOKIE
if (store_ptr->cookie != ALLOC_COOKIE) {
fprintf(stderr, "%s: Invalid pointer\n", __func__);
return;
}
#endif
memset(store_ptr->ptr, 0,
(ptr - store_ptr->ptr) + store_ptr->requested_size);
real_free(store_ptr->ptr);
}
}
void *realloc(void *ptr, size_t size)
{
void *new_ptr = malloc(size);
if (ptr) {
if (new_ptr) {
struct alloc_header *store_ptr =
(struct alloc_header *)ptr;
store_ptr--;
memcpy(new_ptr, ptr,
MIN(size, store_ptr->requested_size));
}
free(ptr);
}
return new_ptr;
}
int posix_memalign(void **memptr, size_t alignment, size_t size)
{
int rc = 0;
if (!alignment || !memptr) {
rc = -EINVAL;
} else {
struct alloc_header alloc_header;
size_t allocated_size;
*memptr = NULL;
alloc_header.requested_size = size;
allocated_size =
(sizeof(alloc_header) / alignment) +
((sizeof(alloc_header) % alignment) ? 1 : 0);
allocated_size *= alignment;
allocated_size += alloc_header.requested_size;
#ifdef CHECK_COOKIE
alloc_header.cookie = ALLOC_COOKIE;
alloc_header.dummy = 0;
#endif
alloc_header.ptr = NULL;
rc = real_posix_memalign(&alloc_header.ptr, alignment,
allocated_size);
if (!rc && alloc_header.ptr) {
struct alloc_header *store_ptr;
*memptr =
alloc_header.ptr + allocated_size -
alloc_header.requested_size;
store_ptr = (struct alloc_header *)*memptr;
store_ptr--;
*store_ptr = alloc_header;
}
}
return rc;
}
void *memalign(size_t boundary, size_t size)
{
void *ptr = NULL;
posix_memalign(&ptr, boundary, size);
return ptr;
}
void *valloc(size_t size)
{
return memalign(getpagesize(), size);
}