仅适用于malloc的函数插入不是免费的 [英] Function interposition only working for malloc not free

查看:112
本文介绍了仅适用于malloc的函数插入不是免费的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在监视malloc和自由通过函数插入的使用时遇到了一个小问题.

I've come across a small problem while monitoring malloc and free trough the use of function interposition.

仅对malloc执行函数插入时,其工作方式为exepcted.但是,当尝试自由插入时,它也会最终陷入循环.我似乎递归地调用了free,但是我不知道为什么.

When performing the function interposition for just malloc, it works as exepcted. However, when trying to interpose free as well it ends up in a loop; i seems like free is recursivly invoked but i just dont know why.

这是malloc和free函数的代码. (mod_malloc_free.c)

This is the code for the malloc and free functions. (mod_malloc_free.c)

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

void* malloc(size_t size) {

   static void* (*real_malloc)(size_t) = NULL;

   printf("%s\n", "inside shared malloc");

   if(!real_malloc)
        real_malloc = dlsym(RTLD_NEXT, "malloc");

   void * p = real_malloc(size);

   printf("malloc(%d) = %p\n",size, p );

   printf("%s\n", "returning from shared malloc");

   return p;

   }

void free(void* ap ) {

    static void (*real_free)(void*) = NULL;

  printf("inside shared free...\n");

  if(!real_free)
       real_free = dlsym(RTLD_NEXT, "free");

   printf("free = %p\n", ap);

   real_free(ap);

}

主要仅由以下部分组成:

The main simply consists of:

#include <stdio.h>
#include <malloc.h>

int main(void) {

    void * p = malloc(123);

    printf("p = %p\n",p );

    free(p);

    return 0;

}

编译为:

gcc -shared -ldl -fPIC mod_malloc_free.c -o libcustom.so

gcc -shared -ldl -fPIC mod_malloc_free.c -o libcustom.so

gcc -o smallMain-墙smallMain.c

gcc -o smallMain -Wall smallMain.c

LD_PRELOAD =./libcustom.so ./smallMain

LD_PRELOAD=./libcustom.so ./smallMain

最诚挚的问候

未婚妻

推荐答案

printf可能正在调用free.当然,这意味着它还执行内存分配,因此提出了一个问题,为什么您看不到malloc中的递归调用. printf可能正在调用诸如callocrealloc之类的替代项.

It is likely printf is calling free. Of course, that implies it also performs memory allocation, so it raises the question why do you not see recursive calls in malloc. Likely printf is calling an alternative such as calloc or realloc.

要仅插入自己的代码,请在与外部库(例如-unexported_symbol开关)链接之前,使用宏替换调用或单独链接代码,并使用链接器功能删除mallocfree.苹果版本的ld).

To interpose in just your own code, use macros to replace the calls or link your code separately and use linker features to remove your malloc and free before linking with external libraries (such as the -unexported_symbol switch for the Apple version of ld).

要插入所有代码,请从例程中删除printf.调用更简单的例程,例如fputs.或者,使用静态标志来抑制递归:

To interpose in all code, remove printf from your routines. Call simpler routines, such as fputs instead. Alternatively, use a static flag to suppress the recursion:

void free(void *ap)
{
    static void (*RealFree)(void *) = 0;
    If (!RealFree)
        RealFree = dlsym(RTLD_NEXT, "free");

    static int InsideCall = 0;
    if (!InsideCall)
    {
        InsideCall = 1;
        … Do stuff…
        InsideCall = 0;
    }
}

(如果您有多个执行内存分配的线程或异常处理程序,则必须采取其他步骤.)

(If you have multiple threads or exception handlers that perform memory allocation, additional steps must be taken.)

这篇关于仅适用于malloc的函数插入不是免费的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆