-
Notifications
You must be signed in to change notification settings - Fork 52
/
libleak.c
749 lines (608 loc) · 17.6 KB
/
libleak.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#define _GNU_SOURCE
#include <backtrace.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
/* libwuya */
#include "wuy_dict.h"
#include "wuy_list.h"
#include "wuy_pool.h"
/* ### configuration from environment variables */
/* LEAK_EXPIRE: log if a memory block is not freed after this time.
* You should change this according to your scenarios. */
static time_t conf_expire = 60;
/* LEAK_AUTO_EXPIRE: increase 'expire' if any block is freed
* after expired, if set. */
static bool conf_auto_expire = false;
/* LEAK_PID_CHECK: interval to check conf_pid_file to enable
* detectiong. Only useful for multi-process program.
* 0 means no checking and detect all processes. */
static time_t conf_pid_check = 0;
/* LEAK_PID_FILE: each line contains a pid to detect. */
static char *conf_pid_file = "/tmp/libleak.enabled";
/* LEAK_LOG_FILE: log file name. */
static char *conf_log_file = "/tmp/libleak";
/* LEAK_LIB_BLACKLIST: blacklist of shared libraries, seperated by `,`. */
static char *conf_lib_blacklist = NULL;
/* LEAK_AFTER: start detect after this time (in second). */
static long conf_start_ms = 0;
/* ### hooking symbols */
static void *(*leak_real_malloc)(size_t size) = NULL;
static void (*leak_real_free)(void *ptr) = NULL;
static void *(*leak_real_calloc)(size_t nmemb, size_t size) = NULL;
static void *(*leak_real_realloc)(void *ptr, size_t size) = NULL;
static pid_t (*leak_real_fork)(void) = NULL; /* open new log file for new process */
/* ### running flags */
static bool leak_inited = false;
static __thread bool leak_in_process = false;
static FILE *leak_log_filp;
/* ### structures and utils for call-stack and memory-block */
struct leak_callstack {
int id;
int alloc_count, free_count;
int expired_count, free_expired_count;
size_t alloc_size, free_size;
size_t expired_size, free_expired_size;
time_t free_min, free_max, free_total;
wuy_list_t expired_list;
wuy_dict_node_t dict_node;
pthread_mutex_t mutex;
int ip_num;
uintptr_t ips[0];
};
static wuy_dict_t *leak_callstack_dict;
static pthread_mutex_t leak_callstack_mutex = PTHREAD_MUTEX_INITIALIZER;
struct leak_memblock {
void *address;
size_t size;
time_t create;
wuy_dict_node_t dict_node;
wuy_list_node_t list_node;
bool expired;
struct leak_callstack *callstack;
};
static wuy_pool_t *leak_memblock_pool;
static wuy_dict_t *leak_memblock_dict;
static WUY_LIST(leak_memblock_list);
static pthread_mutex_t leak_memblock_mutex = PTHREAD_MUTEX_INITIALIZER;
struct backtrace_state *btstate;
static uint32_t leak_callstack_hash(const void *item)
{
const struct leak_callstack *cs = item;
uint32_t hash = cs->ip_num;
for (int i = 0; i < cs->ip_num; i++) {
hash ^= wuy_dict_hash_pointer((void *)cs->ips[i]);
}
return hash;
}
static bool leak_callstack_equal(const void *a, const void *b)
{
const struct leak_callstack *csa = a;
const struct leak_callstack *csb = b;
return (csa->ip_num == csb->ip_num) &&
(memcmp(csa->ips, csb->ips, sizeof(void *) * csa->ip_num) == 0);
}
static int leak_callstack_cmp(const void *a, const void *b)
{
const struct leak_callstack *csa = *(const struct leak_callstack **)a;
const struct leak_callstack *csb = *(const struct leak_callstack **)b;
return (csa->expired_count - csa->free_expired_count)
- (csb->expired_count - csb->free_expired_count);
}
static uint32_t leak_memblock_hash(const void *item)
{
const struct leak_memblock *mb = item;
return wuy_dict_hash_pointer(mb->address);
}
static bool leak_memblock_equal(const void *a, const void *b)
{
const struct leak_memblock *mba = a;
const struct leak_memblock *mbb = b;
return mba->address == mbb->address;
}
/* ### build library maps by reading /proc */
struct lib_map {
const char *name;
size_t start;
size_t end;
bool enabled;
};
static struct lib_map lib_maps[1000];
static int lib_map_num = 0;
static void lib_maps_build(void)
{
char fname[100];
sprintf(fname, "/proc/%d/maps", getpid());
FILE *filp = fopen(fname, "r");
int ia, ib, ic, id;
size_t start, end;
char line[1024], path[1024], perms[5], deleted[100];
while (fgets(line, sizeof(line), filp) != NULL) {
int ret = sscanf(line, "%zx-%zx %s %x %x:%x %d %s %s",
&start, &end, perms, &ia, &ib, &ic, &id, path, deleted);
if (ret != 8 || perms[2] != 'x' || path[0] != '/') {
continue;
}
struct lib_map *lib = &lib_maps[lib_map_num++];
lib->name = strdup(strrchr(path, '/') + 1);
lib->start = start;
lib->end = end;
if (conf_lib_blacklist != NULL) {
lib->enabled = strstr(conf_lib_blacklist, lib->name) == NULL;
} else {
lib->enabled = true;
}
}
fclose(filp);
}
static const char *lib_maps_search(uintptr_t pc)
{
for (int i = 0; i < lib_map_num; i++) {
struct lib_map *lib = &lib_maps[i];
if (pc >= lib->start && pc <= lib->end) {
return lib->enabled ? lib->name : NULL;
}
}
return "[unknown]";
}
/* ### a simple memory allocation used before init() */
static uint8_t tmp_buffer[1024 * 1024]; /* increase this if necessary */
static uint8_t *tmp_buf_pos = tmp_buffer;
static void *tmp_malloc(size_t size)
{
size = (size + 7) / 8 * 8;
if (size > sizeof(tmp_buffer) - (tmp_buf_pos - tmp_buffer)) {
abort();
}
void *p = tmp_buf_pos;
tmp_buf_pos += size;
return p;
}
static void *tmp_calloc(size_t n, size_t size)
{
void *p = tmp_malloc(n * size);
bzero(p, n * size);
return p;
}
static void *tmp_realloc(void *oldp, size_t size)
{
void *newp = tmp_malloc(size);
memcpy(newp, oldp, size);
return newp;
}
static bool tmp_free(void *p)
{
return (p >= (void *)tmp_buffer) && (p <= (void *)tmp_buffer + sizeof(tmp_buffer));
}
/* ### report statistics on exiting */
static void leak_report(void)
{
fprintf(leak_log_filp, "\n# callstack statistics: (in ascending order)\n\n");
struct leak_callstack *callstacks[wuy_dict_count(leak_callstack_dict)];
int count = 0;
wuy_dict_node_t *node;
wuy_dict_iter(leak_callstack_dict, node) {
struct leak_callstack *cs = wuy_containerof(node,
struct leak_callstack, dict_node);
if (cs->expired_count == cs->free_expired_count) {
continue;
}
callstacks[count++] = cs;
}
qsort(callstacks, count, sizeof(struct leak_callstack *), leak_callstack_cmp);
time_t now = time(NULL);
for (int i = 0; i < count; i++) {
struct leak_callstack *cs = callstacks[i];
time_t unfree_max = 0;
if (!wuy_list_empty(&cs->expired_list)) {
struct leak_memblock *mb = wuy_containerof(
wuy_list_first(&cs->expired_list),
struct leak_memblock, list_node);
unfree_max = now - mb->create;
}
fprintf(leak_log_filp, "callstack[%d]: may-leak=%d (%zu bytes)\n"
" expired=%d (%zu bytes), free_expired=%d (%zu bytes)\n"
" alloc=%d (%zu bytes), free=%d (%zu bytes)\n"
" freed memory live time: min=%ld max=%ld average=%ld\n"
" un-freed memory live time: max=%ld\n",
cs->id,
cs->expired_count - cs->free_expired_count,
cs->expired_size - cs->free_expired_size,
cs->expired_count, cs->expired_size,
cs->free_expired_count, cs->free_expired_size,
cs->alloc_count, cs->alloc_size,
cs->free_count, cs->free_size,
cs->free_min, cs->free_max,
cs->free_count ? cs->free_total / cs->free_count : 0,
unfree_max);
}
}
/* ### module init */
static long leak_now_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
static void __attribute__((constructor))init(void)
{
/* read configs from environment variables */
char *ev = getenv("LEAK_EXPIRE");
if (ev != NULL) {
conf_expire = atoi(ev);
}
ev = getenv("LEAK_AUTO_EXPIRE");
if (ev != NULL) {
conf_auto_expire = true;
}
ev = getenv("LEAK_LOG_FILE");
if (ev != NULL) {
conf_log_file = strdup(ev);
}
ev = getenv("LEAK_PID_CHECK");
if (ev != NULL) {
conf_pid_check = atoi(ev);
}
ev = getenv("LEAK_PID_FILE");
if (ev != NULL) {
conf_pid_file = strdup(ev);
}
ev = getenv("LEAK_LIB_BLACKLIST");
if (ev != NULL) {
conf_lib_blacklist = strdup(ev);
}
ev = getenv("LEAK_AFTER");
if (ev != NULL) {
conf_start_ms = leak_now_ms() + atoi(ev) * 1000;
}
/* hook symbols */
leak_real_malloc = dlsym(RTLD_NEXT, "malloc");
assert(leak_real_malloc != NULL);
leak_real_realloc = dlsym(RTLD_NEXT, "realloc");
assert(leak_real_realloc != NULL);
leak_real_calloc = dlsym(RTLD_NEXT, "calloc");
assert(leak_real_calloc != NULL);
leak_real_free = dlsym(RTLD_NEXT, "free");
assert(leak_real_free != NULL);
leak_real_fork = dlsym(RTLD_NEXT, "fork");
assert(leak_real_fork != NULL);
/* init dict and memory pool */
leak_callstack_dict = wuy_dict_new_func(leak_callstack_hash,
leak_callstack_equal,
offsetof(struct leak_callstack, dict_node));
leak_memblock_dict = wuy_dict_new_func(leak_memblock_hash,
leak_memblock_equal,
offsetof(struct leak_memblock, dict_node));
leak_memblock_pool = wuy_pool_new_type(struct leak_memblock);
/* build shared library maps */
lib_maps_build();
/* init backtrace state */
btstate = backtrace_create_state(NULL, 1, NULL, NULL);
/* log file */
char log_fname[100];
sprintf(log_fname, "%s.%d", conf_log_file, getpid());
leak_log_filp = fopen(log_fname, "w");
assert(leak_log_filp != NULL);
/* report at exit */
atexit(leak_report);
fprintf(leak_log_filp, "# start detect. expire=%lds\n", conf_expire);
fflush(leak_log_filp);
leak_inited = true;
}
/* ### check, expire and log memory blocks */
static int leak_backtrace_full_cb (void *data, uintptr_t pc,
const char *filename, int lineno,
const char *function)
{
fprintf(leak_log_filp, " 0x%016zx %s", pc, lib_maps_search(pc));
if (filename != NULL || function != NULL) {
fprintf(leak_log_filp, " %s:%d %s()\n", filename, lineno, function);
} else {
fprintf(leak_log_filp, "\n");
}
return 0;
}
static void leak_callstack_print(struct leak_callstack *cs)
{
for (int i = 0; i < cs->ip_num; i++) {
backtrace_pcinfo(btstate, cs->ips[i], leak_backtrace_full_cb, NULL, NULL);
}
}
static void leak_expire(void)
{
time_t now = time(NULL);
if (pthread_mutex_trylock(&leak_memblock_mutex) != 0) {
return;
}
wuy_list_node_t *node, *safe;
wuy_list_iter_safe(&leak_memblock_list, node, safe) {
struct leak_memblock *mb = wuy_containerof(node, struct leak_memblock, list_node);
if (now - mb->create < conf_expire) {
break;
}
mb->expired = true;
wuy_list_delete(&mb->list_node);
struct leak_callstack *cs = mb->callstack;
pthread_mutex_lock(&cs->mutex);
cs->expired_count++;
cs->expired_size += mb->size;
wuy_list_append(&cs->expired_list, &mb->list_node);
pthread_mutex_unlock(&cs->mutex);
fprintf(leak_log_filp, "callstack[%d] expires. count=%d size=%zu/%zu alloc=%d free=%d\n",
cs->id, cs->expired_count, mb->size, cs->expired_size,
cs->alloc_count, cs->free_count);
if ((cs->expired_count % 100) == 1) {
/* print callstack once every 100 expiration */
leak_callstack_print(cs);
}
}
pthread_mutex_unlock(&leak_memblock_mutex);
fflush(leak_log_filp);
}
/* ### check LEAK_PID_FILE to enable/disable detectiong current process */
static bool leak_enabled_check(void)
{
/* enable all processes if LEAK_PID_CHECK is not set */
if (conf_pid_check == 0) {
return true;
}
static bool leak_enabled = false;
static time_t last_check;
time_t now = time(NULL);
if (now - last_check < conf_pid_check) {
return leak_enabled;
}
last_check = now;
FILE *fp = fopen(conf_pid_file, "r");
if (fp == NULL) {
return leak_enabled;
}
bool old = leak_enabled;
leak_enabled = false; /* disable if the pid is not found in file later */
pid_t pid_enabled, pid_self = getpid();
while (fscanf(fp, "%d", &pid_enabled) == 1) {
if (pid_enabled == pid_self) {
leak_enabled = true; /* enable! */
break;
}
}
fclose(fp);
if (old ^ leak_enabled) {
if (!leak_enabled) {
leak_report();
}
fprintf(leak_log_filp, "# switch %s.\n", leak_enabled ? "enabled" : "disabled");
fflush(leak_log_filp);
}
return leak_enabled;
}
static int leak_backtrace_simple_cb(void *data, uintptr_t pc)
{
if (pc == (uintptr_t)-1) {
return 1;
}
if (conf_lib_blacklist != NULL && lib_maps_search(pc) == NULL) {
return -1;
}
struct leak_callstack *current = data;
if (current->ip_num > 100) {
return 1;
}
current->ips[current->ip_num++] = pc;
return 0;
}
static struct leak_callstack *leak_current(void)
{
static int leak_callstack_id = 1;
struct leak_callstack current[20];
current->ip_num = 0;
if (backtrace_simple(btstate, 2, leak_backtrace_simple_cb, NULL, current) < 0) {
return NULL;
}
pthread_mutex_lock(&leak_callstack_mutex);
struct leak_callstack *cs = wuy_dict_get(leak_callstack_dict, current);
if (cs != NULL) {
pthread_mutex_unlock(&leak_callstack_mutex);
return cs;
}
cs = leak_real_calloc(1, sizeof(struct leak_callstack) + sizeof(void *) * current->ip_num);
cs->id = leak_callstack_id++;
cs->ip_num = current->ip_num;
pthread_mutex_init(&cs->mutex, NULL);
memcpy(cs->ips, current->ips, sizeof(void *) * current->ip_num);
wuy_list_init(&cs->expired_list);
wuy_dict_add(leak_callstack_dict, cs);
pthread_mutex_unlock(&leak_callstack_mutex);
return cs;
}
static bool leak_do_skip(void)
{
if (!leak_inited) {
return true;
}
if (leak_in_process) {
return true;
}
if (conf_start_ms != 0 && leak_now_ms() < conf_start_ms) {
return true;
}
return false;
}
static void leak_process_alloc(void *p, size_t size)
{
if (leak_do_skip()) {
return;
}
leak_in_process = true;
if (!leak_enabled_check()) {
leak_in_process = false;
return;
}
struct leak_callstack *cs = leak_current();
if (cs == NULL) {
leak_in_process = false;
return;
}
pthread_mutex_lock(&cs->mutex);
cs->alloc_size += size;
cs->alloc_count++;
pthread_mutex_unlock(&cs->mutex);
pthread_mutex_lock(&leak_memblock_mutex);
struct leak_memblock *mb = wuy_pool_alloc(leak_memblock_pool);
mb->address = p;
mb->size = size;
mb->expired = false;
mb->create = time(NULL);
mb->callstack = cs;
wuy_dict_add(leak_memblock_dict, mb);
wuy_list_append(&leak_memblock_list, &mb->list_node);
pthread_mutex_unlock(&leak_memblock_mutex);
leak_expire();
leak_in_process = false;
}
static void leak_process_free(void *p)
{
if (leak_do_skip()) {
return;
}
leak_in_process = true;
if (!leak_enabled_check()) {
leak_in_process = false;
return;
}
pthread_mutex_lock(&leak_memblock_mutex);
struct leak_memblock key = { .address = p };
struct leak_memblock *mb = wuy_dict_get(leak_memblock_dict, &key);
if (mb == NULL) {
pthread_mutex_unlock(&leak_memblock_mutex);
leak_in_process = false;
return;
}
wuy_dict_delete(leak_memblock_dict, mb);
wuy_list_delete(&mb->list_node);
struct leak_memblock tmp = *mb;
wuy_pool_free(mb);
mb = &tmp;
pthread_mutex_unlock(&leak_memblock_mutex);
/* update callstack stats */
struct leak_callstack *cs = mb->callstack;
pthread_mutex_lock(&cs->mutex);
cs->free_count++;
cs->free_size += mb->size;
if (mb->expired) {
cs->free_expired_count++;
cs->free_expired_size += mb->size;
time_t live = time(NULL) - mb->create;
fprintf(leak_log_filp, "callstack[%d] frees after expired."
" live=%ld expired=%d free_expired=%d\n",
cs->id, live, cs->expired_count, cs->free_expired_count);
if (conf_auto_expire && live > conf_expire) {
fprintf(leak_log_filp, "# increase expire from %ld to %ld.\n",
conf_expire, live);
conf_expire = live;
}
fflush(leak_log_filp);
}
time_t t = time(NULL) - mb->create;
if (t > cs->free_max) {
cs->free_max = t;
}
if (cs->free_min == 0 || t < cs->free_min) {
cs->free_min = t;
}
cs->free_total += t;
pthread_mutex_unlock(&cs->mutex);
leak_expire();
leak_in_process = false;
}
static void leak_process_update(void *p, size_t size)
{
if (leak_do_skip()) {
return;
}
leak_in_process = true;
if (!leak_enabled_check()) {
leak_in_process = false;
return;
}
pthread_mutex_lock(&leak_memblock_mutex);
struct leak_memblock key = { .address = p };
struct leak_memblock *mb = wuy_dict_get(leak_memblock_dict, &key);
if (mb == NULL) {
pthread_mutex_unlock(&leak_memblock_mutex);
leak_in_process = false;
return;
}
mb->callstack->alloc_size += size - mb->size;
mb->size = size;
pthread_mutex_unlock(&leak_memblock_mutex);
leak_expire();
leak_in_process = false;
}
void *malloc(size_t size)
{
if (leak_real_malloc == NULL) {
return tmp_malloc(size);
}
void *p = leak_real_malloc(size);
leak_process_alloc(p, size);
return p;
}
void free(void *p)
{
if (p == NULL) {
return;
}
if (tmp_free(p)) {
return;
}
leak_real_free(p);
leak_process_free(p);
}
void *calloc(size_t nmemb, size_t size)
{
if (leak_real_calloc == NULL) {
return tmp_calloc(nmemb, size);
}
void *p = leak_real_calloc(nmemb, size);
leak_process_alloc(p, nmemb * size);
return p;
}
void *realloc(void *ptr, size_t size)
{
if (ptr == NULL) {
return malloc(size);
}
if (tmp_free(ptr)) {
return tmp_realloc(ptr, size);
}
void *newp = leak_real_realloc(ptr, size);
if (newp == ptr) {
leak_process_update(ptr, size);
} else {
leak_process_free(ptr);
leak_process_alloc(newp, size);
}
return newp;
}
pid_t fork(void)
{
pid_t pid = leak_real_fork();
if (pid == 0) {
fclose(leak_log_filp);
char log_fname[100];
sprintf(log_fname, "%s.%d", conf_log_file, getpid());
leak_log_filp = fopen(log_fname, "w");
assert(leak_log_filp != NULL);
fprintf(leak_log_filp, "# start detect. parent=%d expire=%lds\n",
getppid(), conf_expire);
fflush(leak_log_filp);
}
return pid;
}