0

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?

dexterous
  • 6,422
  • 12
  • 51
  • 99
  • Wow, that's a [classic](http://www.iecc.com/linker/linker09.html) hack you're trying to use. – Fred Foo Sep 16 '13 at 11:58
  • 2
    If you're using GNU libc, you might also look at [memory allocation hooks](http://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html). – Brett Hale Sep 16 '13 at 12:30

3 Answers3

3

To override shared functions you need to compile your own shared library and preload it via 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

Now your program will use malloc from preloaded library.

Kristaps Taube
  • 2,363
  • 1
  • 17
  • 17
1

The standard way I'm always using is creating a macro called MALLOC (or MYMALLOC or whatever) which does what you want. All occurrences of malloc I have to replace by the use of the macro, of course, and I can understand when this is not what you want.

You also can achieve what you want by defining a macro called malloc (i. e. spelled like the original malloc) only when compiling the source you want to have your feature in. This malloc macro then would call a function called, say, wrappingMalloc which should be declared in a file which is compiled without defining the macro malloc and which then in turn can call the original function malloc. If this makefile fiddling is too much for you, you could also call the original function by calling (malloc) (this avoids running into 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 might get along by overloading the new operator for your classes.

Alfe
  • 56,346
  • 20
  • 107
  • 159
0

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

Naruil
  • 2,300
  • 11
  • 15