forked from efficios/memleak-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memleak-finder.c
406 lines (316 loc) · 8.51 KB
/
memleak-finder.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* memleak-finder.c
*
* Memory leak finder
*
* Copyright (c) 2013 Mathieu Desnoyers <[email protected]>
*
* This library 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 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include "hlist.h"
#include "jhash.h"
static volatile int print_to_console;
static pthread_mutex_t mh_mutex = PTHREAD_MUTEX_INITIALIZER;
static void *(*callocp)(size_t, size_t);
static void *(*mallocp)(size_t);
static void *(*reallocp)(void *, size_t);
static void *(*memalignp)(size_t, size_t);
static int (*posix_memalignp)(void **, size_t, size_t);
static void (*freep)(void *);
static volatile int initialized;
static __thread int thread_in_hook;
#define STATIC_CALLOC_LEN 4096
static char static_calloc_buf[STATIC_CALLOC_LEN];
static size_t static_calloc_len;
static pthread_mutex_t static_calloc_mutex = PTHREAD_MUTEX_INITIALIZER;
#define MH_HASH_BITS 20 /* 1 M entries, hardcoded for now */
#define MH_TABLE_SIZE (1 << MH_HASH_BITS)
static struct cds_hlist_head mh_table[MH_TABLE_SIZE];
struct mh_entry {
struct cds_hlist_node hlist;
void *ptr;
const void *alloc_caller;
char *caller_symbol;
size_t alloc_size;
};
static struct mh_entry *
get_mh(const void *ptr)
{
struct cds_hlist_head *head;
struct cds_hlist_node *node;
struct mh_entry *e;
uint32_t hash;
hash = jhash(&ptr, sizeof(ptr), 0);
head = &mh_table[hash & (MH_TABLE_SIZE - 1)];
cds_hlist_for_each_entry(e, node, head, hlist) {
if (ptr == e->ptr)
return e;
}
return NULL;
}
static void
add_mh(void *ptr, size_t alloc_size, const void *caller)
{
struct cds_hlist_head *head;
struct cds_hlist_node *node;
struct mh_entry *e;
uint32_t hash;
Dl_info info;
if (!ptr)
return;
hash = jhash(&ptr, sizeof(ptr), 0);
head = &mh_table[hash & (MH_TABLE_SIZE - 1)];
cds_hlist_for_each_entry(e, node, head, hlist) {
if (ptr == e->ptr) {
fprintf(stderr, "[warning] add_mh pointer %p is already there\n",
ptr);
//assert(0); /* already there */
}
}
e = malloc(sizeof(*e));
e->ptr = ptr;
e->alloc_caller = caller;
e->alloc_size = alloc_size;
if (dladdr(caller, &info) && info.dli_sname) {
e->caller_symbol = strdup(info.dli_sname);
} else {
e->caller_symbol = NULL;
}
cds_hlist_add_head(&e->hlist, head);
}
static void
del_mh(void *ptr, const void *caller)
{
struct mh_entry *e;
if (!ptr)
return;
e = get_mh(ptr);
if (!e) {
fprintf(stderr,
"[warning] trying to free unallocated ptr %p caller %p\n",
ptr, caller);
return;
}
cds_hlist_del(&e->hlist);
free(e->caller_symbol);
free(e);
}
static void __attribute__((constructor))
do_init(void)
{
char *env;
if (initialized)
return;
callocp = (void *(*) (size_t, size_t)) dlsym (RTLD_NEXT, "calloc");
mallocp = (void *(*) (size_t)) dlsym (RTLD_NEXT, "malloc");
reallocp = (void *(*) (void *, size_t)) dlsym (RTLD_NEXT, "realloc");
memalignp = (void *(*)(size_t, size_t)) dlsym (RTLD_NEXT, "memalign");
posix_memalignp = (int (*)(void **, size_t, size_t)) dlsym (RTLD_NEXT, "posix_memalign");
freep = (void (*) (void *)) dlsym (RTLD_NEXT, "free");
env = getenv("MEMLEAK_FINDER_PRINT");
if (env && strcmp(env, "1") == 0)
print_to_console = 1;
initialized = 1;
}
static
void *static_calloc(size_t nmemb, size_t size)
{
size_t prev_len;
pthread_mutex_lock(&static_calloc_mutex);
if (nmemb * size > sizeof(static_calloc_buf) - static_calloc_len) {
pthread_mutex_unlock(&static_calloc_mutex);
return NULL;
}
prev_len = static_calloc_len;
static_calloc_len += nmemb * size;
pthread_mutex_unlock(&static_calloc_mutex);
return &static_calloc_buf[prev_len];
}
void *
calloc(size_t nmemb, size_t size)
{
void *result;
const void *caller = __builtin_return_address(0);
if (callocp == NULL) {
return static_calloc(nmemb, size);
}
do_init();
if (thread_in_hook) {
return callocp(nmemb, size);
}
thread_in_hook = 1;
pthread_mutex_lock(&mh_mutex);
/* Call resursively */
result = callocp(nmemb, size);
add_mh(result, nmemb * size, caller);
/* printf might call malloc, so protect it too. */
if (print_to_console)
fprintf(stderr, "calloc(%zu,%zu) returns %p\n", nmemb, size, result);
pthread_mutex_unlock(&mh_mutex);
thread_in_hook = 0;
return result;
}
void *
malloc(size_t size)
{
void *result;
const void *caller = __builtin_return_address(0);
do_init();
if (thread_in_hook) {
return mallocp(size);
}
thread_in_hook = 1;
pthread_mutex_lock(&mh_mutex);
/* Call resursively */
result = mallocp(size);
add_mh(result, size, caller);
/* printf might call malloc, so protect it too. */
if (print_to_console)
fprintf(stderr, "malloc(%zu) returns %p\n", size, result);
pthread_mutex_unlock(&mh_mutex);
thread_in_hook = 0;
return result;
}
void *
realloc(void *ptr, size_t size)
{
void *result;
const void *caller = __builtin_return_address(0);
/*
* Return NULL if called on an address returned by
* static_calloc(). TODO: mimick realloc behavior instead.
*/
if ((char *) ptr >= static_calloc_buf &&
(char *) ptr < static_calloc_buf + STATIC_CALLOC_LEN) {
return NULL;
}
do_init();
if (thread_in_hook) {
return reallocp(ptr, size);
}
thread_in_hook = 1;
pthread_mutex_lock(&mh_mutex);
/* Call resursively */
result = reallocp(ptr, size);
if (size == 0 && ptr) {
/* equivalent to free() */
del_mh(ptr, caller);
} else if (result) {
del_mh(ptr, caller);
add_mh(result, size, caller);
}
/* printf might call malloc, so protect it too. */
if (print_to_console)
fprintf(stderr, "realloc(%p,%zu) returns %p\n", ptr, size, result);
pthread_mutex_unlock(&mh_mutex);
thread_in_hook = 0;
return result;
}
void *
memalign(size_t alignment, size_t size)
{
void *result;
const void *caller = __builtin_return_address(0);
do_init();
if (thread_in_hook) {
return memalignp(alignment, size);
}
thread_in_hook = 1;
pthread_mutex_lock(&mh_mutex);
/* Call resursively */
result = memalignp(alignment, size);
add_mh(result, size, caller);
/* printf might call malloc, so protect it too. */
if (print_to_console)
fprintf(stderr, "memalign(%zu,%zu) returns %p\n",
alignment, size, result);
pthread_mutex_unlock(&mh_mutex);
thread_in_hook = 0;
return result;
}
int
posix_memalign(void **memptr, size_t alignment, size_t size)
{
int result;
const void *caller = __builtin_return_address(0);
do_init();
if (thread_in_hook) {
return posix_memalignp(memptr, alignment, size);
}
thread_in_hook = 1;
pthread_mutex_lock(&mh_mutex);
/* Call resursively */
result = posix_memalignp(memptr, alignment, size);
add_mh(*memptr, size, caller);
/* printf might call malloc, so protect it too. */
if (print_to_console)
fprintf(stderr, "posix_memalign(%p,%zu,%zu) returns %d\n",
memptr, alignment, size, result);
pthread_mutex_unlock(&mh_mutex);
thread_in_hook = 0;
return result;
}
void
free(void *ptr)
{
const void *caller = __builtin_return_address(0);
/*
* Ensure that we skip memory allocated by static_calloc().
*/
if ((char *) ptr >= static_calloc_buf &&
(char *) ptr < static_calloc_buf + STATIC_CALLOC_LEN) {
return;
}
do_init();
if (thread_in_hook) {
freep(ptr);
return;
}
thread_in_hook = 1;
pthread_mutex_lock(&mh_mutex);
/* Call resursively */
freep(ptr);
del_mh(ptr, caller);
/* printf might call free, so protect it too. */
if (print_to_console)
fprintf(stderr, "freed pointer %p\n", ptr);
pthread_mutex_unlock(&mh_mutex);
thread_in_hook = 0;
}
static __attribute__((destructor))
void print_leaks(void)
{
unsigned long i;
for (i = 0; i < MH_TABLE_SIZE; i++) {
struct cds_hlist_head *head;
struct cds_hlist_node *node;
struct mh_entry *e;
head = &mh_table[i];
cds_hlist_for_each_entry(e, node, head, hlist) {
fprintf(stderr, "[leak] ptr: %p size: %zu caller: %p <%s>\n",
e->ptr, e->alloc_size, e->alloc_caller,
e->caller_symbol);
}
}
}