diff --git a/libc.map.txt b/libc.map.txt index 3b0e22b08f7f79c8994905b6af1c80202b5857bd..39601aa99ca29cb79260b410c209a7ea464c68ca 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 e72858ba7bb4864a69bf5900fa1955053ce74879..34693310fd72b3285336f2fa5add9bb1fe368847 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 152d2c0f5ee298550f6c0ff0371e97dbcd1c6406..83f3a68b4bbc6ee5852f9aa1c59179a8af80a56c 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 2e7aa2046804437ec95b29b624ca15bbcb982d8b..d51ebc597d1f9f14deab08c703f48834bb426f12 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 97d04678fb46cc19b7ca6da1bd769fb95c52f713..aaa482c3a156da312e7fbb72928c0bf30f1e2fc4 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 060e132624c1fc3367177840187bef15fcefe151..d29196e97af3dddf26ba4fdc31d7e84d7aa10bdc 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 6edc4e57252fe661c14d412db29c7d434a4780cb..cea2fb6705f346c3bf6aeedb0bcf3cc282a452fd 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 f0ea0eb5796006dfc9060429d4016742d611bfbf..ab93cf30896868e84965510d4de185a578962809 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 b72f35d9853e0914126c21139f52e443be7cdd16..51818c808f80faffed01408722b9a6a4b752b2bf 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; }