未验证 提交 b74a631e 编写于 作者: O openharmony_ci 提交者: Gitee

!670 支持calloc和realloc hook

Merge pull request !670 from 卢韬/master
...@@ -51,6 +51,26 @@ int munmap(void* addr, size_t length) ...@@ -51,6 +51,26 @@ int munmap(void* addr, size_t 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);
}
}
size_t malloc_usable_size(void* addr) size_t malloc_usable_size(void* addr)
{ {
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table(); volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
......
...@@ -43,6 +43,8 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { ...@@ -43,6 +43,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
...@@ -199,6 +201,28 @@ static bool init_memorytag_function(void* malloc_shared_library_handler, const c ...@@ -199,6 +201,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_malloc_usable_size_function(void* malloc_shared_library_handler, MallocMallocUsableSizeType* func, const char* prefix) static bool init_malloc_usable_size_function(void* malloc_shared_library_handler, MallocMallocUsableSizeType* func, const char* prefix)
{ {
char symbol[MAX_SYM_NAME_SIZE]; char symbol[MAX_SYM_NAME_SIZE];
...@@ -224,6 +248,12 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa ...@@ -224,6 +248,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),
.malloc_usable_size = MuslMalloc(malloc_usable_size), .malloc_usable_size = MuslMalloc(malloc_usable_size),
}; };
......
...@@ -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.
先完成此消息的编辑!
想要评论请 注册