代码拉取完成,页面将自动刷新
#include <malloc.h>
#include <stdbool.h>
#include <stdlib.h>
/*
1.
rk3326_m2g:/ # cat /proc/2914/maps
b1ed4000-b1ed5000 r-xp 00000000 b3:1b 11159 /data/malloc_hook
b1ed5000-b1ed6000 r--p 00000000 b3:1b 11159 /data/malloc_hook
139|console:/data # ./malloc_hook
malloc(10) call from 0xb1ed4a2c, return 0xec60d000 0xb1ed4a2c-b1ed4000=a2c addr2line addr
free call from 0xec79e88d, free p=0x0
realloc(2048) call from 0xb1ed4a3c, return 0xec60f000,old p=0xec60d000
free call from 0xec79e88d, free p=0x0
free call from 0xb1ed4a48, free p=0xec60f000
free call from 0xb1ed4a60, free p=0xec60d000
2.
bionic/libc/malloc_hooks/tests$ ls
malloc_hooks_tests.cpp
*/
static bool malloc_hook_called_;
static bool free_hook_called_;
static bool realloc_hook_called_;
static bool memalign_hook_called_;
static void *(*orig_malloc_hook_)(size_t, const void *);
static void (*orig_free_hook_)(void *, const void *);
static void *(*orig_realloc_hook_)(void *, size_t, const void *);
static void *(*orig_memalign_hook_)(size_t, size_t, const void *);
static void *test_malloc_hook(size_t, const void *);
static void *test_realloc_hook(void *ptr, size_t, const void *);
static void *test_memalign_hook(size_t, size_t, const void *);
static void test_free_hook(void *, const void *);
void *test_malloc_hook(size_t size, const void *caller)
{
malloc_hook_called_ = true;
__malloc_hook = orig_malloc_hook_;
/*call recursively*/
void *result = malloc(size);
/*printf might call malloc, so protect it too*/
printf("malloc(%u) call from %p, return %p\n", size, caller, result);
__malloc_hook = test_malloc_hook;
return result;
}
void *test_realloc_hook(void *ptr, size_t size, const void *caller)
{
realloc_hook_called_ = true;
__realloc_hook = orig_realloc_hook_;
void * result=realloc(ptr,size);
printf("realloc(%u) call from %p, return %p,old p=%p\n", size, caller, result,ptr);
__realloc_hook = test_realloc_hook;
return result;
}
void *test_memalign_hook(size_t alignment, size_t size, const void *arg)
{
memalign_hook_called_ = true;
return orig_memalign_hook_(alignment, size, arg);
}
void test_free_hook(void *ptr, const void *caller)
{
free_hook_called_ = true;
__free_hook = orig_free_hook_;
printf("free call from %p, free p=%p\n", caller, ptr);
__free_hook = test_free_hook;
return orig_free_hook_(ptr,caller);// it will free
}
void Init()
{
malloc_hook_called_ = false;
free_hook_called_ = false;
realloc_hook_called_ = false;
memalign_hook_called_ = false;
orig_malloc_hook_ = __malloc_hook;
orig_free_hook_ = __free_hook;
orig_realloc_hook_ = __realloc_hook;
orig_memalign_hook_ = __memalign_hook;
__malloc_hook = test_malloc_hook;
__free_hook = test_free_hook;
__realloc_hook = test_realloc_hook;
__memalign_hook = test_memalign_hook;
}
void Reset()
{
__malloc_hook = orig_malloc_hook_;
__free_hook = orig_free_hook_;
__realloc_hook = orig_realloc_hook_;
__memalign_hook = orig_memalign_hook_;
}
/*main*/
int main(void)
{
setenv("LIBC_HOOKS_ENABLE", "1", true);
char *p;
Init();
p = malloc(10);
//free(p);
p = realloc(p, 2048);
free(p);
p = memalign(32, 1024);
free(p);
Reset();
while(1);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。