2006-04-10 08:07:38 +00:00
|
|
|
/**
|
|
|
|
* @file leak_detective.c
|
|
|
|
*
|
2006-04-20 13:12:35 +00:00
|
|
|
* @brief Allocation hooks to find memory leaks.
|
2006-04-10 08:07:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <dlfcn.h>
|
2006-04-18 07:22:20 +00:00
|
|
|
#include <unistd.h>
|
2006-04-20 07:06:39 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <pthread.h>
|
2006-07-05 13:10:47 +00:00
|
|
|
#include <netdb.h>
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
#include "leak_detective.h"
|
|
|
|
|
|
|
|
#include <types.h>
|
|
|
|
|
|
|
|
#ifdef LEAK_DETECTIVE
|
|
|
|
|
|
|
|
/**
|
2006-07-12 11:15:31 +00:00
|
|
|
* Magic value which helps to detect memory corruption. Yummy!
|
2006-04-10 08:07:38 +00:00
|
|
|
*/
|
2006-07-12 11:15:31 +00:00
|
|
|
#define MEMORY_HEADER_MAGIC 0x7ac0be11
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pattern which is filled in memory before freeing it
|
|
|
|
*/
|
|
|
|
#define MEMORY_FREE_PATTERN 0xFF
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pattern which is filled in newly allocated memory
|
|
|
|
*/
|
|
|
|
#define MEMORY_ALLOC_PATTERN 0xEE
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
static void install_hooks(void);
|
|
|
|
static void uninstall_hooks(void);
|
|
|
|
static void *malloc_hook(size_t, const void *);
|
|
|
|
static void *realloc_hook(void *, size_t, const void *);
|
|
|
|
static void free_hook(void*, const void *);
|
2006-09-21 07:03:21 +00:00
|
|
|
|
|
|
|
static u_int count_malloc = 0;
|
|
|
|
static u_int count_free = 0;
|
|
|
|
static u_int count_realloc = 0;
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
typedef struct memory_header_t memory_header_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Header which is prepended to each allocated memory block
|
|
|
|
*/
|
|
|
|
struct memory_header_t {
|
|
|
|
/**
|
|
|
|
* Magci byte which must(!) hold MEMORY_HEADER_MAGIC
|
|
|
|
*/
|
|
|
|
u_int32_t magic;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of bytes following after the header
|
|
|
|
*/
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stack frames at the time of allocation
|
|
|
|
*/
|
|
|
|
void *stack_frames[STACK_FRAMES_COUNT];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of stacks frames obtained in stack_frames
|
|
|
|
*/
|
|
|
|
int stack_frame_count;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to previous entry in linked list
|
|
|
|
*/
|
|
|
|
memory_header_t *previous;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to next entry in linked list
|
|
|
|
*/
|
|
|
|
memory_header_t *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* first mem header is just a dummy to chain
|
|
|
|
* the others on it...
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
static memory_header_t first_header = {
|
2006-04-10 08:07:38 +00:00
|
|
|
magic: MEMORY_HEADER_MAGIC,
|
|
|
|
bytes: 0,
|
|
|
|
stack_frame_count: 0,
|
|
|
|
previous: NULL,
|
|
|
|
next: NULL
|
|
|
|
};
|
|
|
|
|
2006-04-20 11:48:57 +00:00
|
|
|
/**
|
|
|
|
* logger for the leak detective
|
|
|
|
*/
|
|
|
|
static logger_t *logger;
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
/**
|
|
|
|
* standard hooks, used to temparily remove hooking
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
static void *old_malloc_hook, *old_realloc_hook, *old_free_hook;
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2006-04-18 07:22:20 +00:00
|
|
|
/**
|
2006-04-20 11:48:57 +00:00
|
|
|
* are the hooks currently installed?
|
2006-04-18 07:22:20 +00:00
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
static bool installed = FALSE;
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2006-04-20 07:06:39 +00:00
|
|
|
/**
|
2006-04-20 11:48:57 +00:00
|
|
|
* Mutex to exclusivly uninstall hooks, access heap list
|
2006-04-20 07:06:39 +00:00
|
|
|
*/
|
2006-04-20 13:12:35 +00:00
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
2006-04-20 11:48:57 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* log stack frames queried by backtrace()
|
|
|
|
* TODO: Dump symbols of static functions!!!
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
static void log_stack_frames(void **stack_frames, int stack_frame_count)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
|
|
|
char **strings;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
strings = backtrace_symbols (stack_frames, stack_frame_count);
|
|
|
|
|
2006-07-04 13:26:20 +00:00
|
|
|
logger->log(logger, ERROR, " dumping %d stack frame addresses", stack_frame_count);
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
for (i = 0; i < stack_frame_count; i++)
|
|
|
|
{
|
2006-04-18 07:22:20 +00:00
|
|
|
logger->log(logger, ERROR, " %s", strings[i]);
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
free (strings);
|
|
|
|
}
|
|
|
|
|
2006-06-20 10:05:56 +00:00
|
|
|
/**
|
|
|
|
* Whitelist, which contains address ranges in stack frames ignored when leaking.
|
|
|
|
*
|
|
|
|
* This is necessary, as some function use allocation hacks (static buffers)
|
|
|
|
* and so on, which we want to suppress on leak reports.
|
|
|
|
*/
|
2006-06-20 11:03:47 +00:00
|
|
|
typedef struct whitelist_t whitelist_t;
|
|
|
|
|
|
|
|
struct whitelist_t {
|
2006-06-20 10:05:56 +00:00
|
|
|
void* range_start;
|
|
|
|
size_t range_size;
|
2006-06-20 11:03:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
whitelist_t whitelist[] = {
|
2006-07-04 13:26:20 +00:00
|
|
|
{pthread_create, 0x500},
|
2006-06-20 10:05:56 +00:00
|
|
|
{pthread_setspecific, 0xFF},
|
|
|
|
{mktime, 0xFF},
|
|
|
|
{inet_ntoa, 0xFF},
|
2006-07-04 13:26:20 +00:00
|
|
|
{strerror, 0xFF},
|
2006-07-05 13:10:47 +00:00
|
|
|
{getprotobynumber, 0xFF},
|
2006-07-18 12:53:54 +00:00
|
|
|
{getservbyport, 0xFF},
|
2006-06-20 10:05:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this stack frame is whitelisted.
|
|
|
|
*/
|
|
|
|
static bool is_whitelisted(void **stack_frames, int stack_frame_count)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i=0; i< stack_frame_count; i++)
|
|
|
|
{
|
2006-06-20 11:03:47 +00:00
|
|
|
for (j=0; j<sizeof(whitelist)/sizeof(whitelist_t); j++)
|
2006-06-20 10:05:56 +00:00
|
|
|
{
|
|
|
|
if (stack_frames[i] >= whitelist[j].range_start &&
|
|
|
|
stack_frames[i] <= (whitelist[j].range_start + whitelist[j].range_size))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-04-20 11:48:57 +00:00
|
|
|
/**
|
|
|
|
* Report leaks at library destruction
|
|
|
|
*/
|
|
|
|
void report_leaks()
|
|
|
|
{
|
|
|
|
memory_header_t *hdr;
|
|
|
|
int leaks = 0;
|
|
|
|
|
|
|
|
for (hdr = first_header.next; hdr != NULL; hdr = hdr->next)
|
|
|
|
{
|
2006-06-20 10:05:56 +00:00
|
|
|
if (!is_whitelisted(hdr->stack_frames, hdr->stack_frame_count))
|
|
|
|
{
|
2006-07-04 13:26:20 +00:00
|
|
|
logger->log(logger, ERROR, "Leak (%d bytes at %p):", hdr->bytes, hdr + 1);
|
2006-06-20 10:05:56 +00:00
|
|
|
log_stack_frames(hdr->stack_frames, hdr->stack_frame_count);
|
|
|
|
leaks++;
|
|
|
|
}
|
2006-04-20 11:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (leaks)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
logger->log(logger, CONTROL, "No leaks detected");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
logger->log(logger, ERROR, "One leak detected");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger->log(logger, ERROR, "%d leaks detected", leaks);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
/**
|
|
|
|
* Installs the malloc hooks, enables leak detection
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
static void install_hooks()
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
2006-04-20 07:06:39 +00:00
|
|
|
if (!installed)
|
|
|
|
{
|
|
|
|
old_malloc_hook = __malloc_hook;
|
|
|
|
old_realloc_hook = __realloc_hook;
|
|
|
|
old_free_hook = __free_hook;
|
|
|
|
__malloc_hook = malloc_hook;
|
|
|
|
__realloc_hook = realloc_hook;
|
|
|
|
__free_hook = free_hook;
|
|
|
|
installed = TRUE;
|
|
|
|
}
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstalls the malloc hooks, disables leak detection
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
static void uninstall_hooks()
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
2006-04-20 07:06:39 +00:00
|
|
|
if (installed)
|
|
|
|
{
|
|
|
|
__malloc_hook = old_malloc_hook;
|
|
|
|
__free_hook = old_free_hook;
|
|
|
|
__realloc_hook = old_realloc_hook;
|
|
|
|
installed = FALSE;
|
|
|
|
}
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook function for malloc()
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
void *malloc_hook(size_t bytes, const void *caller)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
|
|
|
memory_header_t *hdr;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
2006-09-21 07:03:21 +00:00
|
|
|
count_malloc++;
|
2006-04-10 08:07:38 +00:00
|
|
|
uninstall_hooks();
|
|
|
|
hdr = malloc(bytes + sizeof(memory_header_t));
|
2006-06-20 10:05:56 +00:00
|
|
|
/* set to something which causes crashes */
|
2006-07-12 11:15:31 +00:00
|
|
|
memset(hdr, MEMORY_ALLOC_PATTERN, bytes + sizeof(memory_header_t));
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
hdr->magic = MEMORY_HEADER_MAGIC;
|
|
|
|
hdr->bytes = bytes;
|
|
|
|
hdr->stack_frame_count = backtrace(hdr->stack_frames, STACK_FRAMES_COUNT);
|
2006-07-12 11:15:31 +00:00
|
|
|
install_hooks();
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
/* insert at the beginning of the list */
|
|
|
|
hdr->next = first_header.next;
|
|
|
|
if (hdr->next)
|
|
|
|
{
|
|
|
|
hdr->next->previous = hdr;
|
|
|
|
}
|
|
|
|
hdr->previous = &first_header;
|
|
|
|
first_header.next = hdr;
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
return hdr + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook function for free()
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
void free_hook(void *ptr, const void *caller)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
|
|
|
void *stack_frames[STACK_FRAMES_COUNT];
|
|
|
|
int stack_frame_count;
|
|
|
|
memory_header_t *hdr = ptr - sizeof(memory_header_t);
|
|
|
|
|
|
|
|
/* allow freeing of NULL */
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
2006-09-21 07:03:21 +00:00
|
|
|
count_free++;
|
2006-07-12 11:15:31 +00:00
|
|
|
uninstall_hooks();
|
2006-04-10 08:07:38 +00:00
|
|
|
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
|
|
|
{
|
2006-07-12 11:15:31 +00:00
|
|
|
logger->log(logger, ERROR, "freeing of invalid memory (%p, MAGIC 0x%x != 0x%x):",
|
|
|
|
ptr, hdr->magic, MEMORY_HEADER_MAGIC);
|
2006-04-10 08:07:38 +00:00
|
|
|
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
|
|
|
log_stack_frames(stack_frames, stack_frame_count);
|
2006-07-12 11:15:31 +00:00
|
|
|
install_hooks();
|
|
|
|
pthread_mutex_unlock(&mutex);
|
2006-04-10 08:07:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove item from list */
|
|
|
|
if (hdr->next)
|
|
|
|
{
|
|
|
|
hdr->next->previous = hdr->previous;
|
|
|
|
}
|
|
|
|
hdr->previous->next = hdr->next;
|
|
|
|
|
2006-07-12 11:15:31 +00:00
|
|
|
/* clear MAGIC, set mem to something remarkable */
|
|
|
|
memset(hdr, MEMORY_FREE_PATTERN, hdr->bytes + sizeof(memory_header_t));
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
free(hdr);
|
|
|
|
install_hooks();
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook function for realloc()
|
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
void *realloc_hook(void *old, size_t bytes, const void *caller)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
2006-06-07 13:22:38 +00:00
|
|
|
memory_header_t *hdr;
|
2006-04-10 08:07:38 +00:00
|
|
|
void *stack_frames[STACK_FRAMES_COUNT];
|
|
|
|
int stack_frame_count;
|
|
|
|
|
|
|
|
/* allow reallocation of NULL */
|
|
|
|
if (old == NULL)
|
|
|
|
{
|
|
|
|
return malloc_hook(bytes, caller);
|
|
|
|
}
|
2006-06-07 13:22:38 +00:00
|
|
|
|
|
|
|
hdr = old - sizeof(memory_header_t);
|
2006-07-12 11:15:31 +00:00
|
|
|
|
2006-06-07 13:22:38 +00:00
|
|
|
pthread_mutex_lock(&mutex);
|
2006-09-21 07:03:21 +00:00
|
|
|
count_realloc++;
|
2006-06-07 13:22:38 +00:00
|
|
|
uninstall_hooks();
|
2006-04-10 08:07:38 +00:00
|
|
|
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
|
|
|
{
|
2006-07-04 13:26:20 +00:00
|
|
|
logger->log(logger, ERROR, "reallocation of invalid memory (%p):", old);
|
2006-04-10 08:07:38 +00:00
|
|
|
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
|
|
|
log_stack_frames(stack_frames, stack_frame_count);
|
2006-07-12 11:15:31 +00:00
|
|
|
install_hooks();
|
|
|
|
pthread_mutex_unlock(&mutex);
|
2006-06-20 10:05:56 +00:00
|
|
|
raise(SIGKILL);
|
2006-04-10 08:07:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-06-07 13:22:38 +00:00
|
|
|
hdr = realloc(hdr, bytes + sizeof(memory_header_t));
|
|
|
|
|
|
|
|
/* update statistics */
|
|
|
|
hdr->bytes = bytes;
|
|
|
|
hdr->stack_frame_count = backtrace(hdr->stack_frames, STACK_FRAMES_COUNT);
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2006-06-07 13:22:38 +00:00
|
|
|
/* update header of linked list neighbours */
|
|
|
|
if (hdr->next)
|
|
|
|
{
|
|
|
|
hdr->next->previous = hdr;
|
|
|
|
}
|
|
|
|
hdr->previous->next = hdr;
|
|
|
|
install_hooks();
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
return hdr + 1;
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-04-20 11:48:57 +00:00
|
|
|
* Setup leak detective
|
2006-04-10 08:07:38 +00:00
|
|
|
*/
|
2006-04-20 11:48:57 +00:00
|
|
|
void leak_detective_init()
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
2006-04-20 07:06:39 +00:00
|
|
|
logger = logger_manager->get_logger(logger_manager, LEAK_DETECT);
|
2006-04-20 11:48:57 +00:00
|
|
|
install_hooks();
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
|
2006-04-20 11:48:57 +00:00
|
|
|
/**
|
|
|
|
* Clean up leak detective
|
|
|
|
*/
|
|
|
|
void leak_detective_cleanup()
|
|
|
|
{
|
|
|
|
uninstall_hooks();
|
2006-04-27 07:24:04 +00:00
|
|
|
report_leaks();
|
2006-04-20 11:48:57 +00:00
|
|
|
}
|
|
|
|
|
2006-09-21 07:03:21 +00:00
|
|
|
/**
|
|
|
|
* Log memory allocation statistics
|
|
|
|
*/
|
|
|
|
void leak_detective_status(logger_t *logger)
|
|
|
|
{
|
|
|
|
u_int blocks = 0;
|
|
|
|
size_t bytes = 0;
|
|
|
|
memory_header_t *hdr = &first_header;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
while ((hdr = hdr->next))
|
|
|
|
{
|
|
|
|
blocks++;
|
|
|
|
bytes += hdr->bytes;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
logger->log(logger, CONTROL|LEVEL1, "allocation statistics:");
|
|
|
|
logger->log(logger, CONTROL|LEVEL1, " call stats: malloc: %d, free: %d, realloc: %d",
|
|
|
|
count_malloc, count_free, count_realloc);
|
|
|
|
logger->log(logger, CONTROL|LEVEL1, " allocated %d blocks, total size %d bytes (avg. %d bytes)",
|
|
|
|
blocks, bytes, bytes/blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !LEAK_DETECTION */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy when !using LEAK_DETECTIVE
|
|
|
|
*/
|
|
|
|
void leak_detective_status(logger_t *logger)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
#endif /* LEAK_DETECTION */
|