提交 002602ef 编写于 作者: L lutao 提交者: 卢韬

fixed 6ff6b312 from https://gitee.com/lu-tao/third_party_musl/pulls/670

add calloc and realloc hook
Signed-off-by: Nlutao <lutao31@huawei.com>
上级 ac9b2040
...@@ -50,4 +50,24 @@ int munmap(void* addr, size_t length) ...@@ -50,4 +50,24 @@ int munmap(void* addr, size_t length)
return MuslMalloc(munmap)(addr, length); return MuslMalloc(munmap)(addr, length);
} }
} }
void* calloc(size_t m, size_t n)
{
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
return dispatch_table->calloc(m, n);
} else {
return MuslMalloc(calloc)(m, n);
}
}
void* realloc(void *p, size_t n)
{
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
return dispatch_table->realloc(p, n);
} else {
return MuslMalloc(realloc)(p, n);
}
}
#endif #endif
...@@ -42,6 +42,8 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { ...@@ -42,6 +42,8 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.free = MuslMalloc(free), .free = MuslMalloc(free),
.mmap = MuslMalloc(mmap), .mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap), .munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc),
.realloc = MuslMalloc(realloc),
}; };
#define MAX_SYM_NAME_SIZE 1000 #define MAX_SYM_NAME_SIZE 1000
#define MAX_PROC_NAME_SIZE 256 #define MAX_PROC_NAME_SIZE 256
...@@ -188,6 +190,28 @@ static bool init_memorytag_function(void* malloc_shared_library_handler, const c ...@@ -188,6 +190,28 @@ static bool init_memorytag_function(void* malloc_shared_library_handler, const c
return true; return true;
} }
static bool init_calloc_function(void* malloc_shared_library_handler, MallocCallocType* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "calloc");
*func = (MallocCallocType)(dlsym(malloc_shared_library_handler, symbol));
if (*func == NULL) {
return false;
}
return true;
}
static bool init_realloc_function(void* malloc_shared_library_handler, MallocReallocType* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "realloc");
*func = (MallocReallocType)(dlsym(malloc_shared_library_handler, symbol));
if (*func == NULL) {
return false;
}
return true;
}
static bool init_hook_functions(void* shared_library_handler, struct MallocDispatchType* table, const char* prefix) static bool init_hook_functions(void* shared_library_handler, struct MallocDispatchType* table, const char* prefix)
{ {
if (!init_malloc_function(shared_library_handler, &table->malloc, prefix)) { if (!init_malloc_function(shared_library_handler, &table->malloc, prefix)) {
...@@ -202,6 +226,12 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa ...@@ -202,6 +226,12 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa
if (!init_munmap_function(shared_library_handler, &table->munmap, prefix)) { if (!init_munmap_function(shared_library_handler, &table->munmap, prefix)) {
return false; return false;
} }
if (!init_calloc_function(shared_library_handler, &table->calloc, prefix)) {
return false;
}
if (!init_realloc_function(shared_library_handler, &table->realloc, prefix)) {
return false;
}
if (!init_memorytag_function(shared_library_handler, prefix)) { if (!init_memorytag_function(shared_library_handler, prefix)) {
return false; return false;
} }
......
...@@ -12,6 +12,8 @@ struct MallocDispatchType __libc_malloc_default_dispatch = { ...@@ -12,6 +12,8 @@ struct MallocDispatchType __libc_malloc_default_dispatch = {
.free = MuslMalloc(free), .free = MuslMalloc(free),
.mmap = MuslMalloc(mmap), .mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap), .munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc),
.realloc = MuslMalloc(realloc),
}; };
volatile atomic_bool __hook_enable_hook_flag; volatile atomic_bool __hook_enable_hook_flag;
......
...@@ -564,7 +564,11 @@ static size_t mal0_clear(char *p, size_t pagesz, size_t n) ...@@ -564,7 +564,11 @@ static size_t mal0_clear(char *p, size_t pagesz, size_t n)
} }
} }
#ifdef HOOK_ENABLE
void *__libc_calloc(size_t m, size_t n)
#else
void *calloc(size_t m, size_t n) void *calloc(size_t m, size_t n)
#endif
{ {
#ifdef USE_JEMALLOC #ifdef USE_JEMALLOC
return je_calloc(m, n); return je_calloc(m, n);
...@@ -603,7 +607,11 @@ void *internal_calloc(size_t m, size_t n) ...@@ -603,7 +607,11 @@ void *internal_calloc(size_t m, size_t n)
return memset(p, 0, n); return memset(p, 0, n);
} }
#ifdef HOOK_ENABLE
void *__libc_realloc(void *p, size_t n)
#else
void *realloc(void *p, size_t n) void *realloc(void *p, size_t n)
#endif
{ {
#ifdef USE_JEMALLOC #ifdef USE_JEMALLOC
return je_realloc(p, n); return je_realloc(p, n);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册