some code cleanups
This commit is contained in:
@@ -426,42 +426,31 @@ void free_hook(void *ptr, const void *caller)
|
|||||||
|
|
||||||
count_free++;
|
count_free++;
|
||||||
uninstall_hooks();
|
uninstall_hooks();
|
||||||
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
if (hdr->magic != MEMORY_HEADER_MAGIC ||
|
||||||
|
tail->magic != MEMORY_TAIL_MAGIC)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "freeing memory with corrupted header "
|
fprintf(stderr, "freeing invalid memory (%p): "
|
||||||
"(%p, MAGIC 0x%x != 0x%x):\n",
|
"header magic 0x%x, tail magic 0x%x:\n",
|
||||||
ptr, hdr->magic, MEMORY_HEADER_MAGIC);
|
ptr, hdr->magic, tail->magic);
|
||||||
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();
|
|
||||||
pthread_setschedparam(thread_id, oldpolicy, ¶ms);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (tail->magic != MEMORY_TAIL_MAGIC)
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "freeing memory with corrupted tail "
|
/* remove item from list */
|
||||||
"(%p, MAGIC 0x%x != 0x%x):\n",
|
if (hdr->next)
|
||||||
ptr, tail->magic, MEMORY_TAIL_MAGIC);
|
{
|
||||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
hdr->next->previous = hdr->previous;
|
||||||
log_stack_frames(stack_frames, stack_frame_count);
|
}
|
||||||
install_hooks();
|
hdr->previous->next = hdr->next;
|
||||||
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
|
||||||
return;
|
/* clear MAGIC, set mem to something remarkable */
|
||||||
|
memset(hdr, MEMORY_FREE_PATTERN, hdr->bytes + sizeof(memory_header_t));
|
||||||
|
|
||||||
|
free(hdr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* remove item from list */
|
|
||||||
if (hdr->next)
|
|
||||||
{
|
|
||||||
hdr->next->previous = hdr->previous;
|
|
||||||
}
|
|
||||||
hdr->previous->next = hdr->next;
|
|
||||||
|
|
||||||
/* clear MAGIC, set mem to something remarkable */
|
|
||||||
memset(hdr, MEMORY_FREE_PATTERN, hdr->bytes + sizeof(memory_header_t));
|
|
||||||
|
|
||||||
free(hdr);
|
|
||||||
install_hooks();
|
install_hooks();
|
||||||
|
|
||||||
pthread_setschedparam(thread_id, oldpolicy, ¶ms);
|
pthread_setschedparam(thread_id, oldpolicy, ¶ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,40 +483,25 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
|
|||||||
|
|
||||||
count_realloc++;
|
count_realloc++;
|
||||||
uninstall_hooks();
|
uninstall_hooks();
|
||||||
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
if (hdr->magic != MEMORY_HEADER_MAGIC ||
|
||||||
|
tail->magic != MEMORY_TAIL_MAGIC)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "reallocating memory with corrupted header "
|
fprintf(stderr, "reallocating invalid memory (%p): "
|
||||||
"(%p, MAGIC 0x%x != 0x%x):\n",
|
"header magic 0x%x, tail magic 0x%x:\n",
|
||||||
old, hdr->magic, MEMORY_HEADER_MAGIC);
|
old, hdr->magic, tail->magic);
|
||||||
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();
|
|
||||||
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
|
||||||
raise(SIGKILL);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (tail->magic != MEMORY_TAIL_MAGIC)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "reallocating memory with corrupted tail "
|
|
||||||
"(%p, MAGIC 0x%x != 0x%x):\n",
|
|
||||||
old, tail->magic, MEMORY_TAIL_MAGIC);
|
|
||||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
|
||||||
log_stack_frames(stack_frames, stack_frame_count);
|
|
||||||
install_hooks();
|
|
||||||
pthread_setschedparam(thread_id, oldpolicy, &oldparams);
|
|
||||||
raise(SIGKILL);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
/* clear tail magic, allocate, set tail magic */
|
/* clear tail magic, allocate, set tail magic */
|
||||||
memset(&tail->magic, MEMORY_ALLOC_PATTERN, sizeof(tail->magic));
|
memset(&tail->magic, MEMORY_ALLOC_PATTERN, sizeof(tail->magic));
|
||||||
hdr = realloc(hdr, sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
|
hdr = realloc(hdr, sizeof(memory_header_t) + bytes + sizeof(memory_tail_t));
|
||||||
tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
|
tail = ((void*)hdr) + bytes + sizeof(memory_header_t);
|
||||||
tail->magic = MEMORY_TAIL_MAGIC;
|
tail->magic = MEMORY_TAIL_MAGIC;
|
||||||
|
|
||||||
/* update statistics */
|
/* update statistics */
|
||||||
hdr->bytes = bytes;
|
hdr->bytes = bytes;
|
||||||
hdr->stack_frame_count = backtrace(hdr->stack_frames, STACK_FRAMES_COUNT);
|
hdr->stack_frame_count = backtrace(hdr->stack_frames, STACK_FRAMES_COUNT);
|
||||||
|
|
||||||
/* update header of linked list neighbours */
|
/* update header of linked list neighbours */
|
||||||
if (hdr->next)
|
if (hdr->next)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user