replaced mutex in leak detective with thread scheduling
This commit is contained in:
+1
-1
@@ -233,7 +233,7 @@ static void drop_capabilities(private_daemon_t *this, bool full)
|
|||||||
struct __user_cap_data_struct data;
|
struct __user_cap_data_struct data;
|
||||||
|
|
||||||
/* CAP_NET_ADMIN is needed to use netlink */
|
/* CAP_NET_ADMIN is needed to use netlink */
|
||||||
u_int32_t keep = (1<<CAP_NET_ADMIN);
|
u_int32_t keep = (1<<CAP_NET_ADMIN) | (1<<CAP_SYS_NICE);
|
||||||
|
|
||||||
if (full)
|
if (full)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -162,12 +162,6 @@ static void *old_malloc_hook, *old_realloc_hook, *old_free_hook;
|
|||||||
*/
|
*/
|
||||||
static bool installed = FALSE;
|
static bool installed = FALSE;
|
||||||
|
|
||||||
/**
|
|
||||||
* Mutex to exclusivly uninstall hooks, access heap list
|
|
||||||
*/
|
|
||||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* log stack frames queried by backtrace()
|
* log stack frames queried by backtrace()
|
||||||
* TODO: Dump symbols of static functions. This could be done with
|
* TODO: Dump symbols of static functions. This could be done with
|
||||||
@@ -367,8 +361,15 @@ void *malloc_hook(size_t bytes, const void *caller)
|
|||||||
{
|
{
|
||||||
memory_header_t *hdr;
|
memory_header_t *hdr;
|
||||||
memory_tail_t *tail;
|
memory_tail_t *tail;
|
||||||
|
pthread_t thread_id = pthread_self();
|
||||||
|
int oldpolicy;
|
||||||
|
struct sched_param oldparams, params;
|
||||||
|
|
||||||
|
pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
|
||||||
|
|
||||||
|
params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
|
||||||
|
pthread_setschedparam(thread_id, SCHED_FIFO, ¶ms);
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
count_malloc++;
|
count_malloc++;
|
||||||
uninstall_hooks();
|
uninstall_hooks();
|
||||||
hdr = malloc(sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
|
hdr = malloc(sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
|
||||||
@@ -391,7 +392,9 @@ void *malloc_hook(size_t bytes, const void *caller)
|
|||||||
}
|
}
|
||||||
hdr->previous = &first_header;
|
hdr->previous = &first_header;
|
||||||
first_header.next = hdr;
|
first_header.next = hdr;
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
|
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
||||||
|
|
||||||
return hdr + 1;
|
return hdr + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,7 +407,10 @@ void free_hook(void *ptr, const void *caller)
|
|||||||
int stack_frame_count;
|
int stack_frame_count;
|
||||||
memory_header_t *hdr;
|
memory_header_t *hdr;
|
||||||
memory_tail_t *tail;
|
memory_tail_t *tail;
|
||||||
|
pthread_t thread_id = pthread_self();
|
||||||
|
int oldpolicy;
|
||||||
|
struct sched_param oldparams, params;
|
||||||
|
|
||||||
/* allow freeing of NULL */
|
/* allow freeing of NULL */
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
{
|
{
|
||||||
@@ -413,7 +419,11 @@ void free_hook(void *ptr, const void *caller)
|
|||||||
hdr = ptr - sizeof(memory_header_t);
|
hdr = ptr - sizeof(memory_header_t);
|
||||||
tail = ptr + hdr->bytes;
|
tail = ptr + hdr->bytes;
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
|
||||||
|
|
||||||
|
params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
|
||||||
|
pthread_setschedparam(thread_id, SCHED_FIFO, ¶ms);
|
||||||
|
|
||||||
count_free++;
|
count_free++;
|
||||||
uninstall_hooks();
|
uninstall_hooks();
|
||||||
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
||||||
@@ -424,7 +434,7 @@ void free_hook(void *ptr, const void *caller)
|
|||||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
||||||
log_stack_frames(stack_frames, stack_frame_count);
|
log_stack_frames(stack_frames, stack_frame_count);
|
||||||
install_hooks();
|
install_hooks();
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_setschedparam(thread_id, oldpolicy, ¶ms);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (tail->magic != MEMORY_TAIL_MAGIC)
|
if (tail->magic != MEMORY_TAIL_MAGIC)
|
||||||
@@ -435,7 +445,7 @@ void free_hook(void *ptr, const void *caller)
|
|||||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
||||||
log_stack_frames(stack_frames, stack_frame_count);
|
log_stack_frames(stack_frames, stack_frame_count);
|
||||||
install_hooks();
|
install_hooks();
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,7 +461,8 @@ void free_hook(void *ptr, const void *caller)
|
|||||||
|
|
||||||
free(hdr);
|
free(hdr);
|
||||||
install_hooks();
|
install_hooks();
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
|
pthread_setschedparam(thread_id, oldpolicy, ¶ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -463,7 +474,10 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
|
|||||||
void *stack_frames[STACK_FRAMES_COUNT];
|
void *stack_frames[STACK_FRAMES_COUNT];
|
||||||
int stack_frame_count;
|
int stack_frame_count;
|
||||||
memory_tail_t *tail;
|
memory_tail_t *tail;
|
||||||
|
pthread_t thread_id = pthread_self();
|
||||||
|
int oldpolicy;
|
||||||
|
struct sched_param oldparams, params;
|
||||||
|
|
||||||
/* allow reallocation of NULL */
|
/* allow reallocation of NULL */
|
||||||
if (old == NULL)
|
if (old == NULL)
|
||||||
{
|
{
|
||||||
@@ -473,7 +487,11 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
|
|||||||
hdr = old - sizeof(memory_header_t);
|
hdr = old - sizeof(memory_header_t);
|
||||||
tail = old + hdr->bytes;
|
tail = old + hdr->bytes;
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_getschedparam(thread_id, &oldpolicy, &oldparams);
|
||||||
|
|
||||||
|
params.__sched_priority = sched_get_priority_max(SCHED_FIFO);
|
||||||
|
pthread_setschedparam(thread_id, SCHED_FIFO, ¶ms);
|
||||||
|
|
||||||
count_realloc++;
|
count_realloc++;
|
||||||
uninstall_hooks();
|
uninstall_hooks();
|
||||||
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
||||||
@@ -484,7 +502,7 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
|
|||||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
||||||
log_stack_frames(stack_frames, stack_frame_count);
|
log_stack_frames(stack_frames, stack_frame_count);
|
||||||
install_hooks();
|
install_hooks();
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
||||||
raise(SIGKILL);
|
raise(SIGKILL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -496,7 +514,7 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
|
|||||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
||||||
log_stack_frames(stack_frames, stack_frame_count);
|
log_stack_frames(stack_frames, stack_frame_count);
|
||||||
install_hooks();
|
install_hooks();
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
||||||
raise(SIGKILL);
|
raise(SIGKILL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -517,7 +535,7 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
|
|||||||
}
|
}
|
||||||
hdr->previous->next = hdr;
|
hdr->previous->next = hdr;
|
||||||
install_hooks();
|
install_hooks();
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
||||||
return hdr + 1;
|
return hdr + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user