Create a wrapper function for malloc and free in C

2019-01-02 22:58发布

I am trying to create wrapper functions for free and malloc in C to help notify me of memory leaks. Does anyone know how to declare these functions so when I call malloc() and free() it will call my custom functions and not the standards lib functions?

9条回答
三岁会撩人
2楼-- · 2019-01-02 23:04

If you are only talk about memory that you have under control, i.e. that you malloc and free on your own, you can take a look on rmdebug. Probably it is what you are going to write anyway, so you can save sometime. It has a very liberal licence, if that should be important for you.

I personally use it in a project, to look for memory leaks, the nice things is that it is much faster then valgrind, however it isn't that powerful so you don't get the full calling stack.

查看更多
我只想做你的唯一
3楼-- · 2019-01-02 23:13

Sorry for reopening a 7 years old post.

In my case I needed to wrap memalign/aligned_malloc under malloc. After trying other solutions I ended up implementing the one listed below. It seems to be working fine.

mymalloc.c.

/* 
 * Link-time interposition of malloc and free using the static
 * linker's (ld) "--wrap symbol" flag.
 * 
 * Compile the executable using "-Wl,--wrap,malloc -Wl,--wrap,free".
 * This tells the linker to resolve references to malloc as
 * __wrap_malloc, free as __wrap_free, __real_malloc as malloc, and
 * __real_free as free.
 */
#include <stdio.h>

void *__real_malloc(size_t size);
void __real_free(void *ptr);


/* 
 * __wrap_malloc - malloc wrapper function 
 */
void *__wrap_malloc(size_t size)
{
    void *ptr = __real_malloc(size);
    printf("malloc(%d) = %p\n", size, ptr);
    return ptr;
}

/* 
 * __wrap_free - free wrapper function 
 */
void __wrap_free(void *ptr)
{
    __real_free(ptr);
    printf("free(%p)\n", ptr);
}
查看更多
做个烂人
4楼-- · 2019-01-02 23:15

If your goal is to eliminate memory leaks, an easier, less intrusive way is to use a tool like Valgrind (free) or Purify (costly).

查看更多
祖国的老花朵
5楼-- · 2019-01-02 23:16

You can do wrapper and "overwrite" function with LD_PRELOAD - similarly to example shown earlier.

LD_PRELOAD=/path.../lib_fake_malloc.so ./app

But I recommend to do this "slightly" smarter, I mean calling dlsym once.

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <dlfcn.h>

void* malloc(size_t size)
{
    static void* (*real_malloc)(size_t) = NULL;
    if (!real_malloc)
        real_malloc = dlsym(RTLD_NEXT, "malloc");

    void *p = real_malloc(size);
    fprintf(stderr, "malloc(%d) = %p\n", size, p);
    return p;
}

example I've found here: http://www.jayconrod.com/cgi/view_post.py?23 post by Jay Conrod.

But what I've found really cool at this page is that: GNU linker provides a useful option, --wrap . When I check "man ld" there is following example:

void *
__wrap_malloc (size_t c)
{
    printf ("malloc called with %zu\n", c);
    return __real_malloc (c);
}

I agree with them that's "trivial example" :). Even dlsym is not needed.

Let, me cite one more part of my "man ld" page:

--wrap=symbol
       Use a wrapper function for symbol.
       Any undefined reference to symbol will be resolved to "__wrap_symbol".
       Any undefined reference to "__real_symbol" will be resolved to symbol.

I hope, description is complete and shows how to use those things.

查看更多
看我几分像从前
6楼-- · 2019-01-02 23:16

In C, the method I used was similar to:

#define malloc(x) _my_malloc(x, __FILE__, __LINE__)
#define free(x) _my_free(x)

This allowed me to detect the line and file of where the memory was allocated without too much difficulty. It should be cross-platform, but will encounter problems if the macro is already defined (which should only be the case if you are using another memory leak detector.)

If you want to implement the same in C++, the procedure is a bit more complex but uses the same trick.

查看更多
forever°为你锁心
7楼-- · 2019-01-02 23:16

If you define your own functions for malloc() and free() and explicitly link that with your applications, your functions should be used in preference to those in the library.

However, your function called 'malloc' cannot then call the library malloc function, because in 'c' there's no concept of separate namespaces. In other words, you'd have to implement the internals of malloc and free yourself.

Another approach would be to write functions my_malloc() and my_free(), which call the standard library ones. This would mean that any code calling malloc would have to be changed to call your my_xxx functions.

查看更多
登录 后发表回答