-
Notifications
You must be signed in to change notification settings - Fork 1
/
memcheck_rt.c
590 lines (498 loc) · 14.1 KB
/
memcheck_rt.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
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include "memcheck_rt.h"
#include "memcheck_hashtable.h"
#ifdef DEBUG_MEMCHECK_RT
#define RT_TRACE printf
#else
#define RT_TRACE(...)
#endif // DEBUG_MEMCHECK_RT
#define RT_ENV_SIGNO "RT_ENV_SIGNO"
#define RT_ENV_FILE "RT_ENV_FILE"
#define RT_DEFAULT_FILE "memcheck_rt.log"
///////////////////////////////////////////////////////////////////////////////
// predefines
static void rt_init_hook(void) ;
static void * rt_malloc_hook(size_t size, const void *caller) ;
static void * rt_realloc_hook(void *ptr, size_t size, const void *caller) ;
static void rt_free_hook(void *ptr, const void *caller) ;
static void *(*old_malloc_hook)(size_t, const void *) ;
static void *(*old_realloc_hook)(size_t, const void *) ;
static void (*old_free_hook) (void *, const void *) ;
static void *(*bak_malloc_hook)(size_t, const void *) ;
static void *(*bak_realloc_hook)(size_t, const void *) ;
static void (*bak_free_hook) (void *, const void *) ;
static void sig_record(int signo) ;
static FILE *record_fp = NULL ;
static pthread_mutex_t __lock = PTHREAD_MUTEX_INITIALIZER ;
static pthread_mutex_t lock_record = PTHREAD_MUTEX_INITIALIZER ;
/// there are some stack in this libary which is no use for user
/// please modify the value if necessary
static const int useful_stack_start = 2 ;
///////////////////////////////////////////////////////////////////////////////
// some predefined function
///////////////////////////////////////////////////////////////////////////////
// thread mutex
#if 0
int __attribute__((weak))
pthead_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
return 0;
}
int __attribute__((weak)) pthread_mutex_destroy(pthread_mutex_t *mutex)
{
return 0;
}
int __attribute__((weak)) pthread_mutex_lock(pthread_mutex_t *mutex)
{
return 0;
}
int __attribute__((weak)) pthread_mutex_unlock(pthread_mutex_t *mutex)
{
return 0;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// real function for `malloc', `realloc' and `free'
static __attribute__((always_inline)) void nohook_begin()
{
pthread_mutex_lock(&__lock) ;
bak_malloc_hook = __malloc_hook ;
bak_realloc_hook = __realloc_hook ;
bak_free_hook = __free_hook ;
__malloc_hook = old_malloc_hook ;
__realloc_hook = old_realloc_hook ;
__free_hook = old_free_hook ;
}
static __attribute__((always_inline)) void nohook_end()
{
old_malloc_hook = __malloc_hook ;
old_realloc_hook= __realloc_hook ;
old_free_hook = __free_hook ;
__malloc_hook = bak_malloc_hook ;
__realloc_hook = bak_realloc_hook ;
__free_hook = bak_free_hook ;
pthread_mutex_unlock(&__lock ) ;
}
static inline void *real_malloc(size_t size)
{
void *result ;
pthread_mutex_lock(&__lock) ;
bak_malloc_hook = __malloc_hook ;
__malloc_hook = old_malloc_hook ;
result = malloc(size) ;
old_malloc_hook = __malloc_hook ;
__malloc_hook = bak_malloc_hook ;
pthread_mutex_unlock(&__lock) ;
return result ;
}
static inline void *real_realloc(void *ptr, size_t size)
{
void *result ;
pthread_mutex_lock(&__lock) ;
bak_realloc_hook = __realloc_hook ;
__realloc_hook = old_realloc_hook ;
result = realloc(ptr, size) ;
old_realloc_hook = __realloc_hook ;
__realloc_hook = bak_realloc_hook ;
pthread_mutex_unlock(&__lock) ;
return result ;
}
static inline void real_free(void *ptr)
{
pthread_mutex_lock(&__lock) ;
bak_free_hook = __free_hook ;
__free_hook = old_free_hook ;
free(ptr) ;
old_free_hook = __free_hook ;
__free_hook = bak_free_hook ;
pthread_mutex_unlock(&__lock) ;
}
///////////////////////////////////////////////////////////////////////////////
// records
// there are two hash tables, one for malloc information with stack and symbols
// and the other for recording pointer and size corresponding
static DECLARE_HASHTABLE(malloc_info_hash_table, 10) ;
static DECLARE_HASHTABLE(pointer_info_hash_table, 10) ;
#ifdef __cpluscplus
extern "C" {
#endif
static void memcheck_dump(FILE *fp) ;
static void memcheck_dump_safe(FILE *fp) ;
static void memcheck_dump_console();
#ifdef __cpluscplus
}
#endif
static void __attribute__((constructor)) memcheck_record_init(void)
{
// pthread_mutex_init(&lock_record, NULL) ;
}
static void __attribute__((destructor)) memcheck_record_exit(void)
{
memcheck_dump_safe(record_fp) ;
}
struct memcheck_hnode
{
struct hlist_node hnode ;
struct malloc_record record ;
};
struct pointer_hnode
{
struct hlist_node hnode ;
size_t size ;
void * pointer ;
struct malloc_record * record ;
};
static inline void *malloc_info_key(void *stack_info[], int stack_size)
{
return stack_info[stack_size / 2] ;
}
static struct memcheck_hnode *memcheck_hnode_find(void *stack_info[], int stack_size)
{
void *key = malloc_info_key(stack_info, stack_size) ;
const int index = hash_min(key, HASH_BITS(malloc_info_hash_table)) ;
struct hlist_head *hhead = &malloc_info_hash_table[index] ;
struct memcheck_hnode *pos ;
hlist_for_each_entry(pos, hhead, hnode)
{
if (pos->record.stack_size == stack_size &&
0 == memcmp(pos->record.stack_info, stack_info,
stack_size * sizeof(void *) )
)
{
printf("%p %p\n", pthread_self(), pos->record.stack_info) ;
return pos ;
}
}
return NULL ;
}
static struct pointer_hnode *pointer_hnode_find(void *ptr)
{
const int index = hash_min(ptr, HASH_BITS(pointer_info_hash_table)) ;
struct hlist_head *hhead = &pointer_info_hash_table[index] ;
struct pointer_hnode *pos ;
hlist_for_each_entry(pos, hhead, hnode)
{
if (pos->pointer == ptr)
return pos ;
}
return NULL ;
}
/// package some routines with malloc and free
/// use `always_inline' can reduce the stack level
static __attribute__((always_inline)) int nohook_backtrace(void **buffer, int size)
{
nohook_begin() ;
size = backtrace(buffer, size) ;
nohook_end() ;
return size ;
}
static __attribute__((always_inline)) char **nohook_backtrace_symbols(void *const *buffer, int size)
{
char **ret ;
nohook_begin() ;
ret = backtrace_symbols(buffer, size) ;
nohook_end() ;
return ret ;
}
/// record `malloc', `realloc' and `free'
static void memcheck_record_malloc(void *ptr, size_t size)
{
struct memcheck_hnode *mc_hnode ;
struct pointer_hnode *ptr_hnode ;
void *stack_info[RT_MAX_STACK_SIZE] ;
int stack_size ;
stack_size = nohook_backtrace(stack_info, RT_STACK_SIZE) ;
if (stack_size <= 0)
return ;
mc_hnode = memcheck_hnode_find(stack_info, stack_size) ;
if (!mc_hnode )
{
mc_hnode = (struct memcheck_hnode *)
real_malloc(sizeof(struct memcheck_hnode) ) ;
#ifdef DEBUG_MEMCHECK_RT
{
const int index = hash_min(malloc_info_key(stack_info, stack_size),
HASH_BITS(malloc_info_hash_table)) ;
RT_TRACE("new malloc info hash node: %d\n", index) ;
}
#endif
mc_hnode->record.record_type = MT_MALLOC ;
mc_hnode->record.stack_size = stack_size ;
mc_hnode->record.mem_size = 0 ;
mc_hnode->record.stack_info = real_malloc(sizeof(void *) * stack_size) ;
if (!mc_hnode->record.stack_info)
{
real_free(mc_hnode) ;
return ;
}
memcpy(mc_hnode->record.stack_info, stack_info, sizeof(void *) * stack_size) ;
mc_hnode->record.symbols = NULL ;// nohook_backtrace_symbols(stack_info, stack_size) ;
hash_add(malloc_info_hash_table, &mc_hnode->hnode,
malloc_info_key(stack_info, stack_size)) ;
}
if (!mc_hnode)
return ;
mc_hnode->record.mem_size += size ;
ptr_hnode = pointer_hnode_find(ptr) ;
if (ptr_hnode)
{
RT_TRACE("pointer already exist! %p\n", ptr) ;
}
ptr_hnode = (struct pointer_hnode *)real_malloc(sizeof(struct pointer_hnode)) ;
if (!ptr_hnode)
return ;
#ifdef DEBUG_MEMCHECK_RT
{
const int index = hash_min(ptr, HASH_BITS(pointer_info_hash_table)) ;
RT_TRACE("new pointer info hash node: %d\n", index) ;
}
#endif
ptr_hnode->pointer = ptr ;
ptr_hnode->record = &mc_hnode->record ;
ptr_hnode->size = size ;
hash_add(pointer_info_hash_table, &ptr_hnode->hnode, ptr) ;
}
static void memcheck_record_realloc(void *ptr, size_t size)
{
struct malloc_record *record ;
struct pointer_hnode *ptr_hnode ;
ptr_hnode = pointer_hnode_find(ptr) ;
if (!ptr_hnode)
{
RT_TRACE("can not find pointer in `realloc'! %p\n", ptr) ;
return ;
}
/// NOTE: there is may have more than one `alloc stack' for the pointer
/// because of `realloc', but only one will be recorded which is
/// `malloc stack'
record = &ptr_hnode->record ;
record->mem_size -= (ptr_hnode->size - size) ;
ptr_hnode->size = size ;
}
static void memcheck_record_free(void *ptr)
{
struct pointer_hnode *ptr_hnode ;
struct malloc_record *record ;
ptr_hnode = pointer_hnode_find(ptr) ;
if (!ptr_hnode)
{
#if 0
// will never come here because double free will be catched by system
int i ;
void *stack_info[RT_MAX_STACK_SIZE] ;
int stack_size ;
char **symbols ;
RT_TRACE("double free\n") ;
stack_size = nohook_backtrace(stack_info, RT_STACK_SIZE) ;
if (stack_size <= 0)
return ;
symbols = nohook_backtrace_symbols(stack_info, stack_size) ;
for (i = 0 ; i < stack_size ; i++)
{
RT_TRACE("\t%s\n", symbols[i]) ;
}
RT_TRACE("\n") ;
real_free(symbols) ;
#endif
return ;
}
record = ptr_hnode->record ;
record->mem_size -= ptr_hnode->size ;
hash_del(&ptr_hnode->hnode) ;
real_free(ptr_hnode) ;
}
static __attribute__((always_inline)) void memcheck_record(enum malloc_type type, void *ptr, size_t size)
{
pthread_mutex_lock(&lock_record) ;
switch (type)
{
default:
RT_TRACE("unknown type to record: %p, %u\n", ptr, size) ;
break ;
case MT_MALLOC:
memcheck_record_malloc(ptr, size) ;
break ;
case MT_REALLOC:
memcheck_record_realloc(ptr, size) ;
break ;
case MT_FREE:
memcheck_record_free(ptr) ;
break ;
};
pthread_mutex_unlock(&lock_record) ;
}
static void sig_reocrd(int signo)
{
if (record_fp)
memcheck_dump_safe(record_fp) ;
}
/// be careful use it in `gdb' in case that some threads may call memcheck_*
/// routines which can cause thread unsafe events occurs
static void memcheck_dump(FILE *fp)
{
struct memcheck_hnode *node ;
struct malloc_record *record ;
int i, j ;
RT_TRACE("[memcheck_rt] memcheck_dump begin\n") ;
fprintf(fp, "-----------------------------------------------------------------"
"---------------\n") ;
hash_for_each(malloc_info_hash_table, i, node, hnode)
{
record = &node->record ;
if (0 == record->mem_size)
continue ;
fprintf(fp, "memory size %d\n", record->mem_size) ;
if (NULL == record->symbols)
{
record->symbols = nohook_backtrace_symbols(
record->stack_info, record->stack_size) ;
}
if (record->symbols)
{
for (j = useful_stack_start ; j < record->stack_size ; j++)
{
fprintf(fp, "\t%014p:\t%s\n", record->stack_info[j], record->symbols[j]) ;
}
fprintf(fp, "\n") ;
}
}
fprintf(fp, "\n") ;
RT_TRACE("[memcheck_rt] memcheck_dump end\n") ;
}
static inline void memcheck_dump_safe(FILE *fp)
{
pthread_mutex_lock(&lock_record) ;
memcheck_dump(fp) ;
pthread_mutex_unlock(&lock_record) ;
}
static void memcheck_dump_console()
{
memcheck_dump(stdout);
}
///////////////////////////////////////////////////////////////////////////////
// hooks
#if 0 // not use anymore
inline void write_record(enum malloc_type type, void *ptr, size_t size)
{
struct malloc_record record ;
void *stack_info[RT_MAX_STACK_SIZE] ;
struct iovec iov[2] ;
if (!record_fp)
return ;
record.record_type = type ;
record.stack_size = backtrace(stack_info, RT_STACK_SIZE) ;
record.pointer = ptr ;
record.mem_size = size ;
iov[0].iov_base = &record ;
iov[0].iov_len = sizeof(record) ;
iov[1].iov_base = stack_info ;
iov[1].iov_len = record.stack_size * sizeof(void *) ;
RT_TRACE("stack size: %d\n", record.stack_size) ;
pthread_mutex_lock(&lock_record) ;
if (-1 == writev(record_fd, iov, 2) )
{
if (record_fd >= 0)
{
void *i[RT_MAX_STACK_SIZE] ;
memset((void *)i, 0, sizeof(i) ) ;
write(record_fd, i, sizeof(i) ) ;
close(record_fd) ;
record_fd = -1 ;
}
}
pthread_mutex_unlock(&lock_record) ;
}
#endif
void (*volatile __malloc_initialize_hook) (void) = NULL ;
static __attribute__((constructor)) void memcheck_rt_init(void)
{
__malloc_initialize_hook = rt_init_hook ;
// pthread_mutex_init(&__lock, NULL) ;
}
static void rt_init_hook(void)
{
const char *file_rt = getenv(RT_ENV_FILE) ;
if (!file_rt)
file_rt = RT_DEFAULT_FILE ;
if (file_rt)
{
record_fp = fopen(file_rt, "ab+" ) ;
if (!record_fp)
{
fprintf(stderr, "[memecheck_rt]: can not open %s\n", file_rt) ;
exit(1) ;
}
fprintf(record_fp, "=============================================="
"==================================\n") ;
RT_TRACE("[memecheck_rt]: open file for record %s\n", file_rt) ;
}
assert(record_fp != NULL) ;
int signo = 0 ;
const char *sig = getenv(RT_ENV_SIGNO) ;
if (sig)
signo = atoi(sig) ;
if (signo > 0)
{
struct sigaction sigact ;
sigact.sa_handler = sig_reocrd ;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0 ;
sigaction(signo, &sigact, NULL) ;
}
old_malloc_hook = __malloc_hook ;
old_realloc_hook = __realloc_hook ;
old_free_hook = __free_hook ;
__malloc_hook = rt_malloc_hook ;
__realloc_hook = rt_realloc_hook ;
__free_hook = rt_free_hook ;
}
static void *rt_malloc_hook(size_t size, const void *caller)
{
void *result ;
pthread_mutex_lock(&__lock) ;
__malloc_hook = old_malloc_hook ;
result = malloc(size) ;
old_malloc_hook = __malloc_hook ;
// restore
__malloc_hook = rt_malloc_hook ;
pthread_mutex_unlock(&__lock) ;
// record
memcheck_record(MT_MALLOC, result, size) ;
return result ;
}
static void *rt_realloc_hook(void *ptr, size_t size, const void *caller)
{
void *result ;
pthread_mutex_lock(&__lock) ;
__realloc_hook = old_realloc_hook ;
result = realloc(ptr, size) ;
old_realloc_hook = __realloc_hook ;
// restore
__realloc_hook = rt_realloc_hook ;
pthread_mutex_unlock(&__lock) ;
// record
memcheck_record(MT_REALLOC, ptr, size) ;
return result ;
}
static void rt_free_hook(void *ptr, const void *caller)
{
pthread_mutex_lock(&__lock) ;
__free_hook = old_free_hook ;
free(ptr) ;
old_free_hook = __free_hook ;
// restore
__free_hook = rt_free_hook ;
pthread_mutex_unlock(&__lock) ;
// record
memcheck_record(MT_FREE, ptr, 0) ;
}