I am trying to hack the malloc function to call my malloc function first.Once my malloc function is executed within that, I want to invoke the standard malloc. But, I am getting a recursion, since it is just loading my defined malloc. How can i fix the below code?
#include <dlfcn.h> #include "stdio.h" //#include "stdlib.h" void *handle; void *handle_malloc; int (*loadprg)(void); void * (*malloc_sysm)(size_t); void init() { handle = dlopen ("/export/home/joshis1/Foxtel/temp/libloadmap.so", RTLD_LAZY); if( handle == NULL) { puts(dlerror()); } handle_malloc = dlopen ("/lib/libc.so.6", RTLD_LAZY); if( handle_malloc == NULL) { puts("handle malloc error\r\n"); puts(dlerror()); } } #include "stdio.h" void *malloc(int size) { printf("called..my malloc\r\n"); malloc_sysm = dlsym(handle_malloc,"malloc"); if ( dlerror() != NULL) { puts("malloc symbol not found.."); exit(1); } printf("This should call actual malloc now..\r\n"); return malloc_sysm(size); } int main() { int *t; init(); printf("call load program now\r\n"); loadprg = dlsym(handle, "loadprg"); if( dlerror()!= NULL) { puts("Symbol load errror"); } (*loadprg)(); printf("Close the handle now..\r\n"); dlclose(handle); t = (int *) malloc (100); dlclose(handle_malloc); return 0; } The output is recursion to my defined malloc(). how to fix this?