c
/*
* leak_detector.c - A simple LD_PRELOAD-based memory leak detector for Linux user-space C applications.
*
* Features:
* - Intercepts malloc(), free(), calloc(), realloc().
* - Tracks allocations with backtraces for leak reporting.
* - Thread-safe using pthread mutex.
* - Reports leaks automatically at program exit via destructor.
* - Handles realloc (removes old tracking, adds new).
* - Detects double-frees (ignores second free silently).
*
* Usage:
* gcc -shared -fPIC -Wall -O2 -rdynamic -o leak_detector.so leak_detector.c -ldl -pthread
* LD_PRELOAD=./leak_detector.so ./your_program
*
* Example test program:
* #include <stdlib.h>
* int main() {
* malloc(1024); // leak
* char *p = malloc(100);
* free(p);
* return 0;
* }
*
* Limitations:
* - Does not track mmap(), new/delete (C++), or aligned_alloc().
* - Backtraces may not resolve symbols without -rdynamic in the target binary.
* - Tracking allocations use real_malloc() to avoid recursion.
* - Rare malloc() failures for tracking structs result in untracked leaks.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <unistd.h>
#define MAX_BT 32
struct alloc_info {
void *ptr;
size_t size;
void *bt[MAX_BT];
int bt_size;
struct alloc_info *next;
};
static struct alloc_info *alloc_list = NULL;
static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
static void *(*real_malloc)(size_t) = NULL;
static void *(*real_calloc)(size_t, size_t) = NULL;
static void *(*real_realloc)(void *, size_t) = NULL;
static void (*real_free)(void *) = NULL;
static void ensure_real_funcs(void) {
if (real_malloc == NULL) {
real_malloc = dlsym(RTLD_NEXT, "malloc");
real_calloc = dlsym(RTLD_NEXT, "calloc");
real_realloc = dlsym(RTLD_NEXT, "realloc");
real_free = dlsym(RTLD_NEXT, "free");
// Ignore dlerror() for simplicity; failures will cause segfaults or silent fails.
}
}
static void track_alloc(void *ptr, size_t size) {
struct alloc_info *info = (struct alloc_info *)real_malloc(sizeof(struct alloc_info));
if (info) {
info->ptr = ptr;
info->size = size;
info->bt_size = backtrace(info->bt, MAX_BT);
pthread_mutex_lock(&list_mutex);
info->next = alloc_list;
alloc_list = info;
pthread_mutex_unlock(&list_mutex);
}
}
static void untrack_alloc(void *ptr) {
pthread_mutex_lock(&list_mutex);
struct alloc_info **p = &alloc_list;
while (*p) {
if ((*p)->ptr == ptr) {
struct alloc_info *info = *p;
*p = info->next;
pthread_mutex_unlock(&list_mutex);
real_free(info);
return;
}
p = &(*p)->next;
}
pthread_mutex_unlock(&list_mutex);
}
// malloc interceptor
void *malloc(size_t size) {
ensure_real_funcs();
void *ptr = real_malloc(size);
if (ptr) {
track_alloc(ptr, size);
}
return ptr;
}
// calloc interceptor
void *calloc(size_t nmemb, size_t size) {
ensure_real_funcs();
size_t total_size = nmemb * size; // Potential overflow, but matches libc behavior.
void *ptr = real_calloc(nmemb, size);
if (ptr) {
track_alloc(ptr, total_size);
}
return ptr;
}
// realloc interceptor
void *realloc(void *ptr, size_t size) {
ensure_real_funcs();
if (ptr) {
untrack_alloc(ptr);
}
void *new_ptr = real_realloc(ptr, size);
if (new_ptr) {
track_alloc(new_ptr, size);
}
return new_ptr;
}
// free interceptor
void free(void *ptr) {
if (ptr == NULL) {
return;
}
ensure_real_funcs();
untrack_alloc(ptr);
real_free(ptr);
}
static void report_leaks(void) {
pthread_mutex_lock(&list_mutex);
struct alloc_info *p = alloc_list;
if (p == NULL) {
pthread_mutex_unlock(&list_mutex);
fprintf(stderr, "No memory leaks detected.\n");
return;
}
size_t total_bytes = 0;
int leak_count = 0;
pid_t pid = getpid();
fprintf(stderr, "=== MEMORY LEAK SUMMARY (PID %d) ===\n", pid);
while (p) {
char **symbols = backtrace_symbols(p->bt, p->bt_size);
if (symbols) {
fprintf(stderr, "Leak #%d @ %p: %zu bytes\n", ++leak_count, p->ptr, p->size);
for (int j = 1; j < p->bt_size; j++) { // Skip frame 0 (backtrace itself)
fprintf(stderr, " %s\n", symbols[j]);
}
real_free(symbols);
} else {
fprintf(stderr, "Leak #%d @ %p: %zu bytes (no symbols)\n", ++leak_count, p->ptr, p->size);
}
total_bytes += p->size;
p = p->next;
}
fprintf(stderr, "SUMMARY: %d leak(s), %zu bytes total.\n", leak_count, total_bytes);
fprintf(stderr, "====================================\n");
pthread_mutex_unlock(&list_mutex);
}
// Destructor: run on library unload / program exit
void __attribute__((destructor)) leak_detector_fini(void) {
report_leaks();
}