简体   繁体   English

有没有办法覆盖 C 中的 malloc/free 函数?

[英]Is there a way to overwrite the malloc/free function in C?

有没有办法从自己的 C 应用程序中钩住 malloc/free 函数调用?

malloc() and free() are defined in the standard library; malloc()free()在标准库中定义; when linking code, the linker will search the library only for symbols that are not already resolved by eailier encountered object code, and object files generated from compilation are always linked before any libraries.链接代码时,链接器将仅在库中搜索尚未被 eailier 遇到的目标代码解析的符号,并且编译生成的目标文件始终任何库之前链接。

So you can override any library function simply by defining it in your own code, ensuring that it has the correct signature (same name, same number and types of parameters and same return type).因此,您可以通过在您自己的代码中简单地定义任何库函数来覆盖它,确保它具有正确的签名(相同的名称、相同的参数数量和类型以及相同的返回类型)。

Yes you can.是的你可以。 Here's an example program.这是一个示例程序。 It compiles and builds with gcc 4.8.2 but does not do anything useful since the implementations are not functional.它使用 gcc 4.8.2 编译和构建,但没有做任何有用的事情,因为实现不起作用。

#include <stdlib.h>

int main()
{
   int* ip = malloc(sizeof(int));
   double* dp = malloc(sizeof(double));

   free(ip);
   free(dp);
}

void* malloc(size_t s)
{
   return NULL;
}

void free(void* p)
{
}

Not sure if this counts as "overwriting', but you can effectively change the behavior of code that calls malloc and free by using a macro:不确定这是否算作“覆盖”,但您可以使用宏有效地更改调用mallocfree的代码的行为:

#define malloc(x) my_malloc(x)
#define free(x) my_free(x)

void * my_malloc(size_t nbytes)
{
    /* Do your magic here! */
}

void my_free(void *p)
{
    /* Do your magic here! */
}

int main(void)
{
   int *p = malloc(sizeof(int) * 4); /* calls my_malloc */
   free(p);                          /* calls my_free   */
}

您可能需要LD_PRELOAD机制来替换mallocfree

As many mentioned already, this is very platform specific.正如许多人已经提到的,这是非常特定于平台的。 Most "portable" way is described in an accepted answer to this question .这个问题的公认答案中描述了最“便携”的方式。 A port to non-posix platforms requires finding an appropriate replacement to dlsym .非 posix 平台的端口需要找到合适的替代dlsym

Since you mention Linux/gcc, hooks for malloc would probably serve you the best.既然你提到了 Linux/gcc,那么malloc 的钩子可能会为你提供最好的服务。

C does not provide function overloading. C 不提供函数重载。 So you cannot override.所以你不能覆盖。

Depending on the platform you are using, you may be able to remove the default malloc/free from the library and add your own using the linker or librarian tools.根据您使用的平台,您可以从库中删除默认的 malloc/free,并使用链接器或库管理器工具添加您自己的。 I'd suggest you only do this in a private area and make sure you can't corrupt the original library.我建议您只在私人区域执行此操作,并确保您不会损坏原始库。

On the Windows platform there is a Detour library.在 Windows 平台上有一个 Detour 库。 It basically patches any given function on the assembler level.它基本上修补了汇编程序级别的任何给定函数。 This allows interception of any C library or OS call, like CreateThread , HeapAlloc , etc. I used this library for overriding memory allocation functions in a working application.这允许拦截任何 C 库或操作系统调用,如CreateThreadHeapAlloc等。我使用这个库来覆盖工作应用程序中的内存分配函数。

This library is Windows specific.这个库是 Windows 特定的。 On other platforms most likely there are similar libraries.在其他平台上很可能有类似的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM