Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
WuBingzheng committed Jun 17, 2020
1 parent 0606fec commit ea2bb60
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions libleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ static long conf_start_ms = 0;


/* ### hooking symbols */
static void *(*leak_real_malloc)(size_t size);
static void (*leak_real_free)(void *ptr);
static void *(*leak_real_calloc)(size_t nmemb, size_t size);
static void *(*leak_real_realloc)(void *ptr, size_t size);
static pid_t (*leak_real_fork)(void); /* open new log file for new process */
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;
static __thread bool leak_in_process;
static bool leak_inited = false;
static __thread bool leak_in_process = false;
static FILE *leak_log_filp;


Expand Down Expand Up @@ -191,7 +191,7 @@ static const char *lib_maps_search(uintptr_t pc)

/* ### a simple memory allocation used before init() */

static uint8_t tmp_buffer[10 * 1024 * 1024]; /* increase this if need */
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)
{
Expand Down Expand Up @@ -311,6 +311,22 @@ static void __attribute__((constructor))init(void)
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,
Expand All @@ -328,22 +344,6 @@ static void __attribute__((constructor))init(void)
/* init backtrace state */
btstate = backtrace_create_state(NULL, 1, NULL, NULL);

/* 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);

/* log file */
char log_fname[100];
sprintf(log_fname, "%s.%d", conf_log_file, getpid());
Expand Down Expand Up @@ -474,13 +474,16 @@ static bool leak_enabled_check(void)
static int leak_backtrace_simple_cb(void *data, uintptr_t pc)
{
if (pc == (uintptr_t)-1) {
return 0;
return 1;
}
if (conf_lib_blacklist != NULL && lib_maps_search(pc) == NULL) {
return 1;
return -1;
}

struct leak_callstack *current = data;
if (current->ip_num > 100) {
return 1;
}
current->ips[current->ip_num++] = pc;
return 0;
}
Expand All @@ -491,7 +494,7 @@ static struct leak_callstack *leak_current(void)

struct leak_callstack current[20];
current->ip_num = 0;
if (backtrace_simple(btstate, 2, leak_backtrace_simple_cb, NULL, current) != 0) {
if (backtrace_simple(btstate, 2, leak_backtrace_simple_cb, NULL, current) < 0) {
return NULL;
}

Expand Down

0 comments on commit ea2bb60

Please sign in to comment.