From 2ee256280603e58f1753024dc15ca7d1b739c125 Mon Sep 17 00:00:00 2001 From: ganchuantao Date: Fri, 8 Aug 2025 14:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=20=20=20=20sigchain=20=E4=B8=AD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0custom=5Fhook=5Fflag=20=E7=9A=84=E6=89=93=E5=8D=B0=20?= =?UTF-8?q?=20=20=20=20Signed-off-by:ganchuantao1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ganchuantao --- .../linux/user/src/hook/musl_preinit_common.c | 136 ++++++++++++++++ .../linux/user/src/hook/musl_preinit_common.h | 153 ++---------------- src/hook/linux/musl_preinit_common.c | 135 ++++++++++++++++ src/hook/linux/musl_preinit_common.h | 153 ++---------------- src/sigchain/linux/sigchain.c | 6 +- 5 files changed, 299 insertions(+), 284 deletions(-) diff --git a/porting/linux/user/src/hook/musl_preinit_common.c b/porting/linux/user/src/hook/musl_preinit_common.c index de2134f2a..fc1eb4b47 100644 --- a/porting/linux/user/src/hook/musl_preinit_common.c +++ b/porting/linux/user/src/hook/musl_preinit_common.c @@ -44,4 +44,140 @@ volatile atomic_bool __hook_enable_hook_flag; volatile atomic_bool __memleak_hook_flag; volatile atomic_bool __custom_hook_flag; +__attribute__((always_inline)) +inline bool __get_global_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire); + return g_flag; +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __get_memleak_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire); + return memleak_flag; +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __get_custom_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile bool custom_flag = atomic_load_explicit(&__custom_hook_flag, memory_order_acquire); + return custom_flag; +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __get_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return false; + } + else if (impl_handle == (void *)-1) { + return true; + } + else { + GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]); + bool flag = get_hook_func_ptr(); + return flag; + } +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __set_hook_flag(bool flag) +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return false; + } + else if (impl_handle == (void *)-1) { + return true; + } + else { + SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]); + return set_hook_func_ptr(flag); + } +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline volatile const struct MallocDispatchType* get_current_dispatch_table() +{ +#ifdef HOOK_ENABLE + volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire); + if (ret != NULL) { + if (__get_custom_hook_flag()) { + return ret; + } + if (!__get_global_hook_flag()) { + ret = NULL; + } + else if (!__get_hook_flag()) { + ret = NULL; + } + } + return ret; +#else + return NULL; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, size_t stackSize, uint32_t type) +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return false; + } + else if (impl_handle == (void*)-1) { + return false; + } + else { + SendHookMiscData send_hook_func_ptr = (SendHookMiscData)(function_of_shared_lib[SEND_HOOK_MISC_DATA]); + return send_hook_func_ptr(id, stackPtr, stackSize, type); + } +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline void* __get_hook_config() +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return NULL; + } + else if (impl_handle == (void*)-1) { + return NULL; + } + else { + GetHookConfig get_hook_func_ptr = (GetHookConfig)(function_of_shared_lib[GET_HOOK_CONFIG]); + return get_hook_func_ptr(); + } +#else + return NULL; +#endif // HOOK_ENABLE +} + #endif diff --git a/porting/linux/user/src/hook/musl_preinit_common.h b/porting/linux/user/src/hook/musl_preinit_common.h index 5d033ad17..2051664a9 100644 --- a/porting/linux/user/src/hook/musl_preinit_common.h +++ b/porting/linux/user/src/hook/musl_preinit_common.h @@ -37,152 +37,23 @@ enum EnumHookMode { #ifdef HOOK_ENABLE extern void* function_of_shared_lib[]; extern volatile atomic_llong ohos_malloc_hook_shared_library; +#define MUSL_HOOK_PARAM_NAME "libc.hook_mode" +#define OHOS_PARAM_MAX_SIZE 96 +#define FILE_NAME_MAX_SIZE 40 #endif // HOOK_ENABLE #ifdef __cplusplus extern "C" { #endif - -__attribute__((always_inline)) -inline bool __get_global_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire); - return g_flag; -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __get_memleak_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire); - return memleak_flag; -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __get_custom_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile bool custom_flag = atomic_load_explicit(&__custom_hook_flag, memory_order_acquire); - return custom_flag; -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __get_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return false; - } - else if (impl_handle == (void *)-1) { - return true; - } - else { - GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]); - bool flag = get_hook_func_ptr(); - return flag; - } -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __set_hook_flag(bool flag) -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return false; - } - else if (impl_handle == (void *)-1) { - return true; - } - else { - SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]); - return set_hook_func_ptr(flag); - } -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline volatile const struct MallocDispatchType* get_current_dispatch_table() -{ -#ifdef HOOK_ENABLE - volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire); - if (ret != NULL) { - if (__get_custom_hook_flag()) { - return ret; - } - if (!__get_global_hook_flag()) { - ret = NULL; - } - else if (!__get_hook_flag()) { - ret = NULL; - } - } - return ret; -#else - return NULL; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, size_t stackSize, uint32_t type) -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return false; - } - else if (impl_handle == (void*)-1) { - return false; - } - else { - SendHookMiscData send_hook_func_ptr = (SendHookMiscData)(function_of_shared_lib[SEND_HOOK_MISC_DATA]); - return send_hook_func_ptr(id, stackPtr, stackSize, type); - } -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline void* __get_hook_config() -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return NULL; - } - else if (impl_handle == (void*)-1) { - return NULL; - } - else { - GetHookConfig get_hook_func_ptr = (GetHookConfig)(function_of_shared_lib[GET_HOOK_CONFIG]); - return get_hook_func_ptr(); - } -#else - return NULL; -#endif // HOOK_ENABLE -} - -#define MUSL_HOOK_PARAM_NAME "libc.hook_mode" -#define OHOS_PARAM_MAX_SIZE 96 -#define FILE_NAME_MAX_SIZE 40 - +extern __attribute__((always_inline)) inline bool __get_global_hook_flag(); +extern __attribute__((always_inline)) inline bool __get_memleak_hook_flag(); +extern __attribute__((always_inline)) inline bool __get_custom_hook_flag(); +extern __attribute__((always_inline)) inline bool __get_hook_flag(); +extern __attribute__((always_inline)) inline bool __set_hook_flag(bool flag); +extern __attribute__((always_inline)) inline volatile const struct MallocDispatchType* get_current_dispatch_table(); +extern __attribute__((always_inline)) inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, + size_t stackSize, uint32_t type); +extern __attribute__((always_inline)) inline void* __get_hook_config(); #ifdef __cplusplus } #endif diff --git a/src/hook/linux/musl_preinit_common.c b/src/hook/linux/musl_preinit_common.c index 28aad04ce..4489474cc 100644 --- a/src/hook/linux/musl_preinit_common.c +++ b/src/hook/linux/musl_preinit_common.c @@ -58,5 +58,140 @@ struct MallocDispatchType __libc_malloc_default_dispatch = { volatile atomic_bool __hook_enable_hook_flag; volatile atomic_bool __memleak_hook_flag; volatile atomic_bool __custom_hook_flag; +__attribute__((always_inline)) +inline bool __get_global_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire); + return g_flag; +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __get_memleak_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire); + return memleak_flag; +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __get_custom_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile bool custom_flag = atomic_load_explicit(&__custom_hook_flag, memory_order_acquire); + return custom_flag; +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __get_hook_flag() +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return false; + } + else if (impl_handle == (void *)-1) { + return true; + } + else { + GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]); + bool flag = get_hook_func_ptr(); + return flag; + } +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __set_hook_flag(bool flag) +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return false; + } + else if (impl_handle == (void *)-1) { + return true; + } + else { + SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]); + return set_hook_func_ptr(flag); + } +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline volatile const struct MallocDispatchType* get_current_dispatch_table() +{ +#ifdef HOOK_ENABLE + volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire); + if (ret != NULL) { + if (__get_custom_hook_flag()) { + return ret; + } + if (!__get_global_hook_flag()) { + ret = NULL; + } + else if (!__get_hook_flag()) { + ret = NULL; + } + } + return ret; +#else + return NULL; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, size_t stackSize, uint32_t type) +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return false; + } + else if (impl_handle == (void*)-1) { + return false; + } + else { + SendHookMiscData send_hook_func_ptr = (SendHookMiscData)(function_of_shared_lib[SEND_HOOK_MISC_DATA]); + return send_hook_func_ptr(id, stackPtr, stackSize, type); + } +#else + return false; +#endif // HOOK_ENABLE +} + +__attribute__((always_inline)) +inline void* __get_hook_config() +{ +#ifdef HOOK_ENABLE + volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); + if (impl_handle == NULL) { + return NULL; + } + else if (impl_handle == (void*)-1) { + return NULL; + } + else { + GetHookConfig get_hook_func_ptr = (GetHookConfig)(function_of_shared_lib[GET_HOOK_CONFIG]); + return get_hook_func_ptr(); + } +#else + return NULL; +#endif // HOOK_ENABLE +} #endif diff --git a/src/hook/linux/musl_preinit_common.h b/src/hook/linux/musl_preinit_common.h index 8a6cc3e09..bf6b90918 100644 --- a/src/hook/linux/musl_preinit_common.h +++ b/src/hook/linux/musl_preinit_common.h @@ -58,152 +58,23 @@ enum EnumHookMode { #ifdef HOOK_ENABLE extern void* function_of_shared_lib[]; extern volatile atomic_llong ohos_malloc_hook_shared_library; +#define MUSL_HOOK_PARAM_NAME "libc.hook_mode" +#define OHOS_PARAM_MAX_SIZE 96 +#define FILE_NAME_MAX_SIZE 40 #endif // HOOK_ENABLE #ifdef __cplusplus extern "C" { #endif - -__attribute__((always_inline)) -inline bool __get_global_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire); - return g_flag; -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __get_memleak_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire); - return memleak_flag; -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __get_custom_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile bool custom_flag = atomic_load_explicit(&__custom_hook_flag, memory_order_acquire); - return custom_flag; -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __get_hook_flag() -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return false; - } - else if (impl_handle == (void *)-1) { - return true; - } - else { - GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]); - bool flag = get_hook_func_ptr(); - return flag; - } -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __set_hook_flag(bool flag) -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return false; - } - else if (impl_handle == (void *)-1) { - return true; - } - else { - SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]); - return set_hook_func_ptr(flag); - } -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline volatile const struct MallocDispatchType* get_current_dispatch_table() -{ -#ifdef HOOK_ENABLE - volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire); - if (ret != NULL) { - if (__get_custom_hook_flag()) { - return ret; - } - if (!__get_global_hook_flag()) { - ret = NULL; - } - else if (!__get_hook_flag()) { - ret = NULL; - } - } - return ret; -#else - return NULL; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, size_t stackSize, uint32_t type) -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return false; - } - else if (impl_handle == (void*)-1) { - return false; - } - else { - SendHookMiscData send_hook_func_ptr = (SendHookMiscData)(function_of_shared_lib[SEND_HOOK_MISC_DATA]); - return send_hook_func_ptr(id, stackPtr, stackSize, type); - } -#else - return false; -#endif // HOOK_ENABLE -} - -__attribute__((always_inline)) -inline void* __get_hook_config() -{ -#ifdef HOOK_ENABLE - volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire); - if (impl_handle == NULL) { - return NULL; - } - else if (impl_handle == (void*)-1) { - return NULL; - } - else { - GetHookConfig get_hook_func_ptr = (GetHookConfig)(function_of_shared_lib[GET_HOOK_CONFIG]); - return get_hook_func_ptr(); - } -#else - return NULL; -#endif // HOOK_ENABLE -} - -#define MUSL_HOOK_PARAM_NAME "libc.hook_mode" -#define OHOS_PARAM_MAX_SIZE 96 -#define FILE_NAME_MAX_SIZE 40 - +extern __attribute__((always_inline)) inline bool __get_global_hook_flag(); +extern __attribute__((always_inline)) inline bool __get_memleak_hook_flag(); +extern __attribute__((always_inline)) inline bool __get_custom_hook_flag(); +extern __attribute__((always_inline)) inline bool __get_hook_flag(); +extern __attribute__((always_inline)) inline bool __set_hook_flag(bool flag); +extern __attribute__((always_inline)) inline volatile const struct MallocDispatchType* get_current_dispatch_table(); +extern __attribute__((always_inline)) inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, + size_t stackSize, uint32_t type); +extern __attribute__((always_inline)) inline void* __get_hook_config(); #ifdef __cplusplus } #endif diff --git a/src/sigchain/linux/sigchain.c b/src/sigchain/linux/sigchain.c index 59b031f48..3b5dd94e2 100644 --- a/src/sigchain/linux/sigchain.c +++ b/src/sigchain/linux/sigchain.c @@ -27,6 +27,7 @@ extern int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old); +extern __attribute__((always_inline)) inline bool __get_custom_hook_flag(); #define SIG_CHAIN_KEY_VALUE_1 1 #define SIGNAL_CHAIN_SPECIAL_ACTION_MAX 3 @@ -238,7 +239,8 @@ static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_r "set_syscall_hooks_tl_lock=%{public}d " "set_syscall_hooks_linux_tl_lock=%{public}d " "fork_tl_lock=%{public}d " - "register_count=%{public}d ", + "register_count=%{public}d " + "__custom_hook_flag=%{public}d", __func__, idx, signo, (unsigned long long)sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction, noreturn, signo, thread_list_lock_status, get_tl_lock_count(), get_tl_lock_waiters(), get_tl_lock_tid_fail(), get_tl_lock_count_tid(), @@ -256,7 +258,7 @@ static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_r call_tl_lock_dump->set_syscall_hooks_tl_lock, call_tl_lock_dump->set_syscall_hooks_linux_tl_lock, call_tl_lock_dump->fork_tl_lock, - get_register_count()); + get_register_count(), __get_custom_hook_flag()); if (sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction(signo, siginfo, ucontext_raw)) { set_handling_signal(previous_value); -- Gitee