Have next memory overrides:
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
static FILE * FBacktrLog, *FAll, *FDeall;
static size_t all_allocated;
/* sample addresswise statistics:*/
static void ReportAlloc(FILE *f, const void *p)
{
fprintf(f, "%p\n", p);
}
static void LogBackTrace(size_t n, void *p)
{
static void *bt[256];
int bt_c;
//
fprintf(FBacktrLog, "%ld (in %p) allocated under context:\n", n, p);
fflush(FBacktrLog);
bt_c=backtrace(bt, 100); // up to 100 bt
backtrace_symbols_fd(bt, bt_c, fileno(FBacktrLog));
}
static void * my_alloc(size_t n)
{
void *new_obj;
//
all_allocated+=n;
new_obj=malloc(n);
if (!new_obj)
return NULL;
LogBackTrace(n, new_obj);
ReportAlloc(FAll, new_obj);
return new_obj;
}
static void my_free(void *p)
{
if (!p)
return;
ReportAlloc(FDeall, p);
free(p);
}
static void *my_realloc(void *ptr, size_t size)
{
void *new_obj;
//
if (ptr==NULL)
return my_alloc(size);
else if (size==0)
{
my_free(ptr);
return NULL;
}
ReportAlloc(FDeall, ptr);
new_obj=realloc(ptr, size);
if (!new_obj)
return NULL;
ReportAlloc(FAll, new_obj);
return new_obj;
}
int main(int argc, const char **argv)
{
FBacktrLog=fopen("./mem_log", "w");
if (!FBacktrLog)
goto l_err;
FAll=fopen("./alloc", "w");
if (!FAll)
goto l_err;
FDeall=fopen("./dealloc", "w");
if (!FDeall)
goto l_err;
CRYPTO_set_mem_functions(&my_alloc, &my_realloc, &my_free);
ERR_load_BIO_strings();
l_err:
if (FAll)
fclose(FAll);
if (FDeall)
fclose(FDeall);
if (FBacktrLog)
fclose(FBacktrLog);
system("sleep 1; sort alloc > alloc_s; sort dealloc > dealloc_s; rm alloc
dealloc");
return 0;
}
After runnig just compare alloc_s and dealloc_s.
They should be identical. But they aren't. Also in backtrace there is a
stack reported. For example i see that memory allocated in context
of lh_new is freed.
While memory under lh_insert isn't. Valgrind+memcheck shows only "still
reachable" so i think free is called but not under overriden stuff.
--
kind regards,
Serhiy Ivanov|