提交 2517297c 编写于 作者: S shuxinyia

增加mmap hook的能力

Signed-off-by: NshuxinyiA <zhangcui11@huawei.com>
Signed-off-by: Nshuxinyia <shuxinyi4@h-partners.com>
上级 1cbbf7c5
...@@ -1936,6 +1936,7 @@ musl_src_porting_file = [ ...@@ -1936,6 +1936,7 @@ musl_src_porting_file = [
] ]
musl_inc_hook_files = [ musl_inc_hook_files = [
"porting/linux/user/src/hook/memory_tag.h",
"porting/linux/user/src/hook/musl_malloc_dispatch_table.h", "porting/linux/user/src/hook/musl_malloc_dispatch_table.h",
"porting/linux/user/src/hook/musl_malloc_dispatch.h", "porting/linux/user/src/hook/musl_malloc_dispatch.h",
"porting/linux/user/src/hook/musl_preinit_common.h", "porting/linux/user/src/hook/musl_preinit_common.h",
......
...@@ -30,4 +30,24 @@ void free(void* mem) ...@@ -30,4 +30,24 @@ void free(void* mem)
MuslMalloc(free)(mem); MuslMalloc(free)(mem);
} }
} }
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
{
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
return dispatch_table->mmap(addr, length, prot, flags, fd, offset);
} else {
return MuslMalloc(mmap)(addr, length, prot, flags, fd, offset);
}
}
int munmap(void* addr, size_t length)
{
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
return dispatch_table->munmap(addr, length);
} else {
return MuslMalloc(munmap)(addr, length);
}
}
#endif #endif
#include "memory_tag.h"
mtypeset __mem_typeset = NULL;
\ No newline at end of file
#ifndef _MEMORY_TAG_H
#define _MEMORY_TAG_H
#include <unistd.h>
typedef int (*mtypeset)(const void* addr, size_t addrlen, const char* tag, size_t tagLen);
extern mtypeset __mem_typeset;
#define MEM_TYPESET(addr, addrlen, tag, tagLen) \
do { \
if (__mem_typeset != NULL) { \
__mem_typeset(addr, addrlen, tag, tagLen); \
} \
} while (0)
#endif
\ No newline at end of file
...@@ -11,6 +11,8 @@ extern "C" { ...@@ -11,6 +11,8 @@ extern "C" {
#define MuslMalloc(func) func #define MuslMalloc(func) func
#endif #endif
void *__libc_mmap(void*, size_t, int, int, int, off_t);
int __libc_munmap(void*, size_t);
void *__libc_malloc(size_t); void *__libc_malloc(size_t);
void *__libc_calloc(size_t, size_t); void *__libc_calloc(size_t, size_t);
void *__libc_realloc(void *, size_t); void *__libc_realloc(void *, size_t);
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef void* (*MallocMmapType) (void*, size_t, int, int, int, off_t);
typedef int (*MallocMunmapType) (void*, size_t);
typedef void* (*MallocMallocType)(size_t); typedef void* (*MallocMallocType)(size_t);
typedef void* (*MallocReallocType)(void*, size_t); typedef void* (*MallocReallocType)(void*, size_t);
typedef void* (*MallocCallocType)(size_t, size_t); typedef void* (*MallocCallocType)(size_t, size_t);
...@@ -20,6 +22,8 @@ typedef bool (*GetHookFlagType)(); ...@@ -20,6 +22,8 @@ typedef bool (*GetHookFlagType)();
typedef bool (*SetHookFlagType)(bool); typedef bool (*SetHookFlagType)(bool);
struct MallocDispatchType { struct MallocDispatchType {
MallocMmapType mmap;
MallocMunmapType munmap;
MallocMallocType malloc; MallocMallocType malloc;
MallocCallocType calloc; MallocCallocType calloc;
MallocReallocType realloc; MallocReallocType realloc;
......
...@@ -19,6 +19,7 @@ which need be escaped. ...@@ -19,6 +19,7 @@ which need be escaped.
#include <signal.h> #include <signal.h>
#include "musl_malloc_dispatch_table.h" #include "musl_malloc_dispatch_table.h"
#include "musl_malloc.h" #include "musl_malloc.h"
#include "memory_tag.h"
#include "musl_preinit_common.h" #include "musl_preinit_common.h"
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -35,6 +36,8 @@ void* ohos_malloc_hook_init_function(size_t bytes); ...@@ -35,6 +36,8 @@ void* ohos_malloc_hook_init_function(size_t bytes);
static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.malloc = ohos_malloc_hook_init_function, .malloc = ohos_malloc_hook_init_function,
.free = MuslMalloc(free), .free = MuslMalloc(free),
.mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap),
}; };
#define MAX_SYM_NAME_SIZE 1000 #define MAX_SYM_NAME_SIZE 1000
static char *__malloc_hook_shared_lib = "libnative_hook.z.so"; static char *__malloc_hook_shared_lib = "libnative_hook.z.so";
...@@ -152,6 +155,40 @@ static bool init_free_function(void* malloc_shared_library_handler, MallocFreeTy ...@@ -152,6 +155,40 @@ static bool init_free_function(void* malloc_shared_library_handler, MallocFreeTy
return true; return true;
} }
static bool init_mmap_function(void* malloc_shared_library_handler, MallocMmapType* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "mmap");
*func = (MallocMmapType)(dlsym(malloc_shared_library_handler, symbol));
if (*func == NULL) {
return false;
}
return true;
}
static bool init_munmap_function(void* malloc_shared_library_handler, MallocMunmapType* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "munmap");
*func = (MallocMunmapType)(dlsym(malloc_shared_library_handler, symbol));
if (*func == NULL) {
return false;
}
return true;
}
static bool init_memorytag_function(void* malloc_shared_library_handler, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "memtag");
__mem_typeset = (mtypeset)(dlsym(malloc_shared_library_handler, symbol));
if (__mem_typeset == 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)) {
...@@ -160,6 +197,15 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa ...@@ -160,6 +197,15 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa
if (!init_free_function(shared_library_handler, &table->free, prefix)) { if (!init_free_function(shared_library_handler, &table->free, prefix)) {
return false; return false;
} }
if (!init_mmap_function(shared_library_handler, &table->mmap, prefix)) {
return false;
}
if (!init_munmap_function(shared_library_handler, &table->munmap, prefix)) {
return false;
}
if (!init_memorytag_function(shared_library_handler, prefix)) {
return false;
}
return true; return true;
} }
......
...@@ -10,6 +10,8 @@ struct musl_libc_globals __musl_libc_globals; ...@@ -10,6 +10,8 @@ struct musl_libc_globals __musl_libc_globals;
struct MallocDispatchType __libc_malloc_default_dispatch = { struct MallocDispatchType __libc_malloc_default_dispatch = {
.malloc = MuslMalloc(malloc), .malloc = MuslMalloc(malloc),
.free = MuslMalloc(free), .free = MuslMalloc(free),
.mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap),
}; };
volatile atomic_bool __hook_enable_hook_flag; volatile atomic_bool __hook_enable_hook_flag;
......
...@@ -40,6 +40,13 @@ void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off) ...@@ -40,6 +40,13 @@ void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
return (void *)__syscall_ret(ret); return (void *)__syscall_ret(ret);
} }
weak_alias(__mmap, mmap);
#ifdef HOOK_ENABLE
void* __libc_mmap(void*, size_t, int, int, int, off_t);
weak_alias(__mmap, __libc_mmap);
weak_alias(__libc_mmap, mmap64);
#else
weak_alias(__mmap, mmap);
weak_alias(mmap, mmap64); weak_alias(mmap, mmap64);
#endif // HOOK_ENABLE
...@@ -10,4 +10,9 @@ int __munmap(void *start, size_t len) ...@@ -10,4 +10,9 @@ int __munmap(void *start, size_t len)
return syscall(SYS_munmap, start, len); return syscall(SYS_munmap, start, len);
} }
#ifdef HOOK_ENABLE
int __libc_munmap(void*, size_t);
weak_alias(__munmap, __libc_munmap);
#else
weak_alias(__munmap, munmap); weak_alias(__munmap, munmap);
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册