Hack a standard function in a library and subsequently call a library function

I am trying to hack the malloc function to call the malloc function first.Once my malloc function is executed inside this, I want to call the standard malloc. But I get recursion, since it is just loading my specific malloc. How can I fix the code below?

#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 for my specific malloc (). How to fix it?

0
source share
3 answers

To override common functions, you need to compile your own shared library and preload it through the LD_PRELOAD environment variable.

 #define _GNU_SOURCE #include <stdlib.h> #include <stdio.h> #include <dlfcn.h> void *malloc(size_t size) { printf("called..my malloc\r\n"); void *(*original_malloc)(size_t size); // Find original malloc function original_malloc = dlsym(RTLD_NEXT, "malloc"); if ( dlerror() != NULL) { puts("malloc symbol not found.."); exit(1); } printf("This should call actual malloc now..\r\n"); return (*original_malloc)(size); } $ gcc -Wall -fPIC -shared -o mymalloc.so mymalloc.c -ldl $ LD_PRELOAD=./mymalloc.so ./prog 

Your program will now use malloc from the preloaded library.

+3
source

The standard way that I always use is to create a macro called MALLOC (or MYMALLOC or something else) that does what you want. All occurrences of MALLOC I have to replace with a macro, of course, and I can understand when this is not what you want.

You can also achieve what you want by specifying a macro named MALLOC (i.e. written as the original MALLOC ) only when compiling the source in which you want to use your function. This MALLOC macro MALLOC then call a function called, say, wrappingMalloc , which must be declared in a file that is compiled without defining the MALLOC macro and which, in turn, can call the original MALLOC function. If this make-script is too much for you, you can also call the original function by calling (malloc) (this avoids running the macro again):

  #include <stdlib.h> #include <stdio.h> #define malloc(size) myMalloc(size) void *myMalloc(size_t size) { void *result; printf("mallocing %ld bytes", size); result = (malloc)(size); printf(" at %p\n", result); return result; } int main(int argc, char *argv[]) { char *buffer; buffer = malloc(10); return 0; } 

In C ++, you can get by overloading the new operator for your classes.

+1
source

I do not see a problem in your code. But why not move malloc_sysm = dlsym(handle_malloc,"malloc"); into your init() function?

0
source

All Articles