From 76474bca262647afa3901480a3327ad3b45fe5f0 Mon Sep 17 00:00:00 2001 From: zyxzyx Date: Wed, 10 Sep 2025 15:10:02 +0800 Subject: [PATCH] add restrace Signed-off-by: zyxzyx --- libc.map.txt | 1 + porting/linux/user/src/hook/memory_trace.c | 18 ++++++++ porting/linux/user/src/hook/memory_trace.h | 46 +++++++++++++++++++ .../user/src/hook/musl_malloc_dispatch.h | 3 ++ porting/linux/user/src/hook/musl_preinit.c | 18 ++++++++ src/hook/linux/memory_trace.c | 18 ++++++++ src/hook/linux/memory_trace.h | 46 +++++++++++++++++++ src/hook/linux/musl_malloc_dispatch.h | 2 + src/hook/linux/musl_preinit.c | 19 ++++++++ 9 files changed, 171 insertions(+) diff --git a/libc.map.txt b/libc.map.txt index 3b0e22b08..39601aa99 100644 --- a/libc.map.txt +++ b/libc.map.txt @@ -1149,6 +1149,7 @@ memrchr; memset; memtrace; + restrace; mincore; mkdir; mkdirat; diff --git a/porting/linux/user/src/hook/memory_trace.c b/porting/linux/user/src/hook/memory_trace.c index e72858ba7..34693310f 100644 --- a/porting/linux/user/src/hook/memory_trace.c +++ b/porting/linux/user/src/hook/memory_trace.c @@ -24,3 +24,21 @@ void memtrace(void* addr, size_t size, const char* tag, bool is_using) #endif return; } + +void restrace(unsigned long long mask, void* addr, size_t size, const char* tag, bool is_using) +{ +#ifdef HOOK_ENABLE + volatile const struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit( + &__musl_libc_globals.current_dispatch_table, memory_order_acquire); + if (__predict_false(dispatch_table != NULL)) { + if (!__get_global_hook_flag()) { + return; + } + else if (!__get_hook_flag()) { + return; + } + return dispatch_table->restrace(mask, addr, size, tag, is_using); + } + return; +#endif +} diff --git a/porting/linux/user/src/hook/memory_trace.h b/porting/linux/user/src/hook/memory_trace.h index 152d2c0f5..83f3a68b4 100644 --- a/porting/linux/user/src/hook/memory_trace.h +++ b/porting/linux/user/src/hook/memory_trace.h @@ -7,7 +7,53 @@ #ifdef __cplusplus extern "C" { #endif + +#define TAG_RES_FD_OPEN "RES_FD_OPEN" +#define TAG_RES_FD_EPOLL "RES_FD_EPOLL" +#define TAG_RES_FD_EVENTFD "RES_FD_EVENTFD" +#define TAG_RES_FD_SOCKET "RES_FD_SOCKET" +#define TAG_RES_FD_PIPE "RES_FD_PIPE" +#define TAG_RES_FD_DUP "RES_FD_DUP" +#define TAG_RES_FD_ALL "RES_FD_ALL" +#define TAG_RES_THREAD_PTHREAD "RES_THREAD_PTHREAD" +#define TAG_RES_GPU_VK "RES_GPU_VK" +#define TAG_RES_GPU_GLES_IMAGE "RES_GPU_GLES_IMAGE" +#define TAG_RES_GPU_GLES_BUFFER "RES_GPU_GLES_BUFFER" +#define TAG_RES_GPU_CL_IMAGE "RES_GPU_CL_IMAGE" +#define TAG_RES_GPU_CL_BUFFER "RES_GPU_CL_BUFFER" +#define TAG_RES_GPU_ALL "RES_GPU_ALL" +#define TAG_RES_DMABUF "RES_DMABUF" + +/* each bit represents resource hook point. + * |63 ... 32|31 ... 22|21 ... 12|11 - 10|9 ... 0| + * |RESERVED | DMABUF | GPU |THREAD | FD | + */ + +//FD +#define RES_FD_OPEN (1 << 0) +#define RES_FD_EPOLL (1 << 1) +#define RES_FD_EVENTFD (1 << 2) +#define RES_FD_SOCKET (1 << 3) +#define RES_FD_PIPE (1 << 4) +#define RES_FD_DUP (1 << 5) +#define RES_FD_MASK (0x3F) +//Thread +#define RES_THREAD_PTHREAD (1 << 10) +#define RES_THREAD_MASK (0x3 << 10) +//GPU Memory +#define RES_GPU_VK (1 << 12) +#define RES_GPU_GLES_IMAGE (1 << 13) +#define RES_GPU_GLES_BUFFER (1 << 14) +#define RES_GPU_CL_IMAGE (1 << 15) +#define RES_GPU_CL_BUFFER (1 << 16) +#define RES_GPU_MASK (0x1F << 12) +//ION Memory +#define RES_ION_MASK (0x1F << 22) +//RESERVED +#define RES_RESERVED_MASK (0xFFFFFF << 32) + void memtrace(void* addr, size_t size, const char* tag, bool is_using); +void restrace(unsigned long long mask, void* addr, size_t size, const char* tag, bool is_using); #ifdef __cplusplus } #endif diff --git a/porting/linux/user/src/hook/musl_malloc_dispatch.h b/porting/linux/user/src/hook/musl_malloc_dispatch.h index 2e7aa2046..d51ebc597 100644 --- a/porting/linux/user/src/hook/musl_malloc_dispatch.h +++ b/porting/linux/user/src/hook/musl_malloc_dispatch.h @@ -30,6 +30,7 @@ typedef void (*MallocStatsPrintType)(void (*) (void *, const char *), void *, co typedef int (*MalloptType)(int, int); typedef ssize_t (*MallocBacktraceType)(void*, uintptr_t*, size_t); typedef void (*MemTrace)(void*, size_t, const char*, bool); +typedef void (*ResTrace)(unsigned long long, void*, size_t, const char*, bool); typedef bool (*GetHookFlagType)(); typedef bool (*SetHookFlagType)(bool); @@ -58,6 +59,7 @@ struct MallocDispatchType { GetHookFlagType get_hook_flag; SetHookFlagType set_hook_flag; MemTrace memtrace; + ResTrace restrace; MallocPrctlType prctl; }; #ifdef __cplusplus @@ -65,3 +67,4 @@ struct MallocDispatchType { #endif #endif + diff --git a/porting/linux/user/src/hook/musl_preinit.c b/porting/linux/user/src/hook/musl_preinit.c index 97d04678f..aaa482c3a 100644 --- a/porting/linux/user/src/hook/musl_preinit.c +++ b/porting/linux/user/src/hook/musl_preinit.c @@ -39,6 +39,7 @@ void* ohos_malloc_hook_init_function(size_t bytes); void* ohos_aligned_alloc_hook_init_function(size_t alignment, size_t bytes); void* ohos_mmap_hook_init_function(void* addr, size_t length, int prot, int flags, int fd, off_t offset); void default_memtrace(void* addr, size_t size, const char* tag, bool is_using) {} +void default_restrace(unsigned long long mask, void* addr, size_t size, const char* tag, bool is_using) {} static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { .malloc = ohos_malloc_hook_init_function, @@ -51,6 +52,7 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { .malloc_usable_size = MuslMalloc(malloc_usable_size), .memtrace = default_memtrace, .aligned_alloc = ohos_aligned_alloc_hook_init_function, + .restrace = default_restrace, }; #define MAX_SYM_NAME_SIZE 1000 #define MAX_PROC_NAME_SIZE 256 @@ -231,6 +233,19 @@ static bool init_memtrace_function(void* malloc_shared_library_handler, MemTrace return true; } +static bool init_restrace_function(void* malloc_shared_library_handler, ResTrace* func, const char* prefix) +{ + char symbol[MAX_SYM_NAME_SIZE]; + if (snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "restrace") < 0) { + return false; + } + *func = (ResTrace)(dlsym(malloc_shared_library_handler, symbol)); + if (*func == NULL) { + return false; + } + return true; +} + static bool init_calloc_function(void* malloc_shared_library_handler, MallocCallocType* func, const char* prefix) { char symbol[MAX_SYM_NAME_SIZE]; @@ -332,6 +347,9 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa if (!init_memtrace_function(shared_library_handler, &table->memtrace, prefix)) { return false; } + if (!init_restrace_function(shared_library_handler, &table->restrace, prefix)) { + return false; + } if (!init_malloc_usable_size_function(shared_library_handler, &table->malloc_usable_size, prefix)) { return false; } diff --git a/src/hook/linux/memory_trace.c b/src/hook/linux/memory_trace.c index 060e13262..d29196e97 100644 --- a/src/hook/linux/memory_trace.c +++ b/src/hook/linux/memory_trace.c @@ -39,3 +39,21 @@ void memtrace(void* addr, size_t size, const char* tag, bool is_using) #endif return; } + +void restrace(unsigned long long mask, void* addr, size_t size, const char* tag, bool is_using) +{ +#ifdef HOOK_ENABLE + volatile const struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit( + &__musl_libc_globals.current_dispatch_table, memory_order_acquire); + if (__predict_false(dispatch_table != NULL)) { + if (!__get_global_hook_flag()) { + return; + } + else if (!__get_hook_flag()) { + return; + } + return dispatch_table->restrace(mask, addr, size, tag, is_using); + } + return; +#endif +} diff --git a/src/hook/linux/memory_trace.h b/src/hook/linux/memory_trace.h index 6edc4e572..cea2fb670 100644 --- a/src/hook/linux/memory_trace.h +++ b/src/hook/linux/memory_trace.h @@ -29,7 +29,53 @@ #ifdef __cplusplus extern "C" { #endif + +#define TAG_RES_FD_OPEN "RES_FD_OPEN" +#define TAG_RES_FD_EPOLL "RES_FD_EPOLL" +#define TAG_RES_FD_EVENTFD "RES_FD_EVENTFD" +#define TAG_RES_FD_SOCKET "RES_FD_SOCKET" +#define TAG_RES_FD_PIPE "RES_FD_PIPE" +#define TAG_RES_FD_DUP "RES_FD_DUP" +#define TAG_RES_FD_ALL "RES_FD_ALL" +#define TAG_RES_THREAD_PTHREAD "RES_THREAD_PTHREAD" +#define TAG_RES_GPU_VK "RES_GPU_VK" +#define TAG_RES_GPU_GLES_IMAGE "RES_GPU_GLES_IMAGE" +#define TAG_RES_GPU_GLES_BUFFER "RES_GPU_GLES_BUFFER" +#define TAG_RES_GPU_CL_IMAGE "RES_GPU_CL_IMAGE" +#define TAG_RES_GPU_CL_BUFFER "RES_GPU_CL_BUFFER" +#define TAG_RES_GPU_ALL "RES_GPU_ALL" +#define TAG_RES_DMABUF "RES_DMABUF" + +/* each bit represents resource hook point. + * |63 ... 32|31 ... 22|21 ... 12|11 - 10|9 ... 0| + * |RESERVED | DMABUF | GPU |THREAD | FD | + */ + +//FD +#define RES_FD_OPEN (1 << 0) +#define RES_FD_EPOLL (1 << 1) +#define RES_FD_EVENTFD (1 << 2) +#define RES_FD_SOCKET (1 << 3) +#define RES_FD_PIPE (1 << 4) +#define RES_FD_DUP (1 << 5) +#define RES_FD_MASK (0x3F) +//Thread +#define RES_THREAD_PTHREAD (1 << 10) +#define RES_THREAD_MASK (0x3 << 10) +//GPU Memory +#define RES_GPU_VK (1 << 12) +#define RES_GPU_GLES_IMAGE (1 << 13) +#define RES_GPU_GLES_BUFFER (1 << 14) +#define RES_GPU_CL_IMAGE (1 << 15) +#define RES_GPU_CL_BUFFER (1 << 16) +#define RES_GPU_MASK (0x1F << 12) +//ION Memory +#define RES_ION_MASK (0x1F << 22) +//RESERVED +#define RES_RESERVED_MASK (0xFFFFFF << 32) + void memtrace(void* addr, size_t size, const char* tag, bool is_using); +void restrace(unsigned long long mask, void* addr, size_t size, const char* tag, bool is_using); #ifdef __cplusplus } #endif diff --git a/src/hook/linux/musl_malloc_dispatch.h b/src/hook/linux/musl_malloc_dispatch.h index f0ea0eb57..ab93cf308 100644 --- a/src/hook/linux/musl_malloc_dispatch.h +++ b/src/hook/linux/musl_malloc_dispatch.h @@ -52,6 +52,7 @@ typedef void (*MallocStatsPrintType)(void (*) (void *, const char *), void *, co typedef int (*MalloptType)(int, int); typedef ssize_t (*MallocBacktraceType)(void*, uintptr_t*, size_t); typedef void (*MemTrace)(void*, size_t, const char*, bool); +typedef void (*ResTrace)(unsigned long long, void*, size_t, const char*, bool); typedef bool (*GetHookFlagType)(); typedef bool (*SetHookFlagType)(bool); @@ -80,6 +81,7 @@ struct MallocDispatchType { GetHookFlagType get_hook_flag; SetHookFlagType set_hook_flag; MemTrace memtrace; + ResTrace restrace; MallocPrctlType prctl; }; #ifdef __cplusplus diff --git a/src/hook/linux/musl_preinit.c b/src/hook/linux/musl_preinit.c index b72f35d98..51818c808 100644 --- a/src/hook/linux/musl_preinit.c +++ b/src/hook/linux/musl_preinit.c @@ -54,6 +54,7 @@ void* ohos_malloc_hook_init_function(size_t bytes); void* ohos_aligned_alloc_hook_init_function(size_t alignment, size_t bytes); void* ohos_mmap_hook_init_function(void* addr, size_t length, int prot, int flags, int fd, off_t offset); void default_memtrace(void* addr, size_t size, const char* tag, bool is_using) {} +void default_restrace(unsigned long long mask, void* addr, size_t size, const char* tag, bool is_using) {} #ifdef USE_GWP_ASAN extern void* libc_gwp_asan_malloc(size_t bytes); @@ -73,6 +74,7 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { .malloc_usable_size = libc_gwp_asan_malloc_usable_size, .memtrace = default_memtrace, .aligned_alloc = ohos_aligned_alloc_hook_init_function, + .restrace = default_restrace, }; #else static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { @@ -86,6 +88,7 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { .malloc_usable_size = MuslMalloc(malloc_usable_size), .memtrace = default_memtrace, .aligned_alloc = ohos_aligned_alloc_hook_init_function, + .restrace = default_restrace, }; #endif #define MAX_SYM_NAME_SIZE 1000 @@ -267,6 +270,19 @@ static bool init_memtrace_function(void* malloc_shared_library_handler, MemTrace return true; } +static bool init_restrace_function(void* malloc_shared_library_handler, ResTrace* func, const char* prefix) +{ + char symbol[MAX_SYM_NAME_SIZE]; + if (snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "restrace") < 0) { + return false; + } + *func = (ResTrace)(dlsym(malloc_shared_library_handler, symbol)); + if (*func == NULL) { + return false; + } + return true; +} + static bool init_calloc_function(void* malloc_shared_library_handler, MallocCallocType* func, const char* prefix) { char symbol[MAX_SYM_NAME_SIZE]; @@ -367,6 +383,9 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa if (!init_memtrace_function(shared_library_handler, &table->memtrace, prefix)) { return false; } + if (!init_restrace_function(shared_library_handler, &table->restrace, prefix)) { + return false; + } if (!init_malloc_usable_size_function(shared_library_handler, &table->malloc_usable_size, prefix)) { return false; } -- Gitee