From 5e4e7a6927a6a18c95c83250fbfc5c23e74c1129 Mon Sep 17 00:00:00 2001 From: xujie Date: Fri, 12 May 2023 10:37:10 +0800 Subject: [PATCH] fix musl code Signed-off-by: xujie --- musl_template.gni | 1 - .../linux/user/src/hook/musl_socket_preinit.c | 63 +++++++++++++- .../linux/user/src/hook/musl_socket_preinit.h | 28 +++++++ .../src/hook/musl_socket_preinit_common.c | 16 ---- .../src/hook/musl_socket_preinit_common.h | 83 ------------------- porting/linux/user/src/hook/socket_common.c | 4 +- 6 files changed, 92 insertions(+), 103 deletions(-) create mode 100644 porting/linux/user/src/hook/musl_socket_preinit.h delete mode 100644 porting/linux/user/src/hook/musl_socket_preinit_common.c delete mode 100644 porting/linux/user/src/hook/musl_socket_preinit_common.h diff --git a/musl_template.gni b/musl_template.gni index d46f605e2..ca7f25b00 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -720,7 +720,6 @@ template("musl_libs") { "./porting/linux/user/src/hook/musl_preinit.c", "./porting/linux/user/src/hook/musl_preinit_common.c", "./porting/linux/user/src/hook/musl_socket_preinit.c", - "./porting/linux/user/src/hook/musl_socket_preinit_common.c", "./porting/linux/user/src/hook/socket_common.c", ] diff --git a/porting/linux/user/src/hook/musl_socket_preinit.c b/porting/linux/user/src/hook/musl_socket_preinit.c index 74f55ee64..eb6c58789 100644 --- a/porting/linux/user/src/hook/musl_socket_preinit.c +++ b/porting/linux/user/src/hook/musl_socket_preinit.c @@ -9,13 +9,24 @@ #include #include #include -#include "musl_socket_preinit_common.h" +#include #include "musl_log.h" +#include "musl_socket.h" +#include "musl_socket_preinit.h" static char *__socket_hook_shared_lib = "libfwmark_client.z.so"; static char *__socket_hook_function_prefix = "ohos_socket_hook"; void* shared_lib_func[LAST_FUNC]; + +long long __current_dispatch; long long __ohos_socket_hook_shared_library; +bool __socket_hook_begin_flag; + +struct SocketDispatchType __musl_libc_socket_dispatch; +struct SocketDispatchType __libc_socket_default_dispatch = { + .socket = MuslSocket(socket), +}; + typedef bool (*init_func_type)(const struct SocketDispatchType*, bool*, const char*); typedef void (*finalize_func_type)(); #define MAX_SYMBOL_SIZE 1000 @@ -147,4 +158,54 @@ __attribute__((constructor())) static void __musl_socket_initialize() init_ohos_socket_hook(); } } + +__attribute__((always_inline)) +inline bool __get_socket_hook_begin_flag() +{ +#ifdef OHOS_SOCKET_HOOK_ENABLE + return __socket_hook_begin_flag; +#else + return false; +#endif +} + +__attribute__((always_inline)) +inline bool __get_socket_hook_flag() +{ +#ifdef OHOS_SOCKET_HOOK_ENABLE + void* handle = (void *)__ohos_socket_hook_shared_library; + if (handle == NULL) { + return false; + } else if (handle == (void *)-1) { + return true; + } else { + SocketGetHookFlagType get_hook_func_ptr = (SocketGetHookFlagType)(shared_lib_func[GET_HOOK_FLAG_FUNC]); + bool flag = get_hook_func_ptr(); + return flag; + } +#else + return false; +#endif +} + +__attribute__((always_inline)) +inline volatile const struct SocketDispatchType* get_socket_dispatch() +{ +#ifdef OHOS_SOCKET_HOOK_ENABLE + volatile const struct SocketDispatchType* ret = (struct SocketDispatchType *)__current_dispatch; + if (ret != NULL) { + if (!__get_socket_hook_begin_flag()) { + ret = NULL; + } else if (!__get_socket_hook_flag()) { + ret = NULL; + } else { + return ret; + } + } + return ret; +#else + return NULL; +#endif +} + #endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/musl_socket_preinit.h b/porting/linux/user/src/hook/musl_socket_preinit.h new file mode 100644 index 000000000..2a8178ecf --- /dev/null +++ b/porting/linux/user/src/hook/musl_socket_preinit.h @@ -0,0 +1,28 @@ +#ifndef _MUSL_SOCKET_PREINIT_COMMON_H +#define _MUSL_SOCKET_PREINIT_COMMON_H + +#include +#include "musl_socket_dispatch.h" +#include "common_def.h" + +enum SocketFuncEnum { + INITIALIZE_FUNC, + FINALIZE_FUNC, + GET_HOOK_FLAG_FUNC, + SET_HOOK_FLAG_FUNC, + LAST_FUNC, +}; + +#ifdef __cplusplus +extern "C" { +#endif + +bool __get_socket_hook_begin_flag(); +bool __get_socket_hook_flag(); +volatile const struct SocketDispatchType* get_socket_dispatch(); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/musl_socket_preinit_common.c b/porting/linux/user/src/hook/musl_socket_preinit_common.c deleted file mode 100644 index a586cf878..000000000 --- a/porting/linux/user/src/hook/musl_socket_preinit_common.c +++ /dev/null @@ -1,16 +0,0 @@ -#ifdef OHOS_SOCKET_HOOK_ENABLE -#include -#include "musl_socket.h" -#include "musl_socket_preinit_common.h" - -struct SocketDispatchType __musl_libc_socket_dispatch; - -long long __current_dispatch; - -struct SocketDispatchType __libc_socket_default_dispatch = { - .socket = MuslSocket(socket), -}; - -bool __socket_hook_begin_flag; - -#endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/musl_socket_preinit_common.h b/porting/linux/user/src/hook/musl_socket_preinit_common.h deleted file mode 100644 index c13b63206..000000000 --- a/porting/linux/user/src/hook/musl_socket_preinit_common.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef _MUSL_SOCKET_PREINIT_COMMON_H -#define _MUSL_SOCKET_PREINIT_COMMON_H - -#include -#include "musl_socket_dispatch.h" -#include "common_def.h" - -extern struct SocketDispatchType __musl_libc_socket_dispatch; -extern struct SocketDispatchType __libc_socket_default_dispatch; - -enum SocketFuncEnum { - INITIALIZE_FUNC, - FINALIZE_FUNC, - GET_HOOK_FLAG_FUNC, - SET_HOOK_FLAG_FUNC, - LAST_FUNC, -}; - -#ifdef OHOS_SOCKET_HOOK_ENABLE -extern long long __current_dispatch; -extern bool __socket_hook_begin_flag; -extern long long __ohos_socket_hook_shared_library; -extern void* shared_lib_func[LAST_FUNC]; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -__attribute__((always_inline)) -inline bool __get_socket_hook_begin_flag() -{ -#ifdef OHOS_SOCKET_HOOK_ENABLE - return __socket_hook_begin_flag; -#else - return false; -#endif -} - -__attribute__((always_inline)) -inline bool __get_socket_hook_flag() -{ -#ifdef OHOS_SOCKET_HOOK_ENABLE - void* handle = (void *)__ohos_socket_hook_shared_library; - if (handle == NULL) { - return false; - } else if (handle == (void *)-1) { - return true; - } else { - SocketGetHookFlagType get_hook_func_ptr = (SocketGetHookFlagType)(shared_lib_func[GET_HOOK_FLAG_FUNC]); - bool flag = get_hook_func_ptr(); - return flag; - } -#else - return false; -#endif -} - -__attribute__((always_inline)) -inline volatile const struct SocketDispatchType* get_socket_dispatch() -{ -#ifdef OHOS_SOCKET_HOOK_ENABLE - volatile const struct SocketDispatchType* ret = (struct SocketDispatchType *)__current_dispatch; - if (ret != NULL) { - if (!__get_socket_hook_begin_flag()) { - ret = NULL; - } else if (!__get_socket_hook_flag()) { - ret = NULL; - } else { - return ret; - } - } - return ret; -#else - return NULL; -#endif -} - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/socket_common.c b/porting/linux/user/src/hook/socket_common.c index 939b75c5a..f7bfd7a92 100644 --- a/porting/linux/user/src/hook/socket_common.c +++ b/porting/linux/user/src/hook/socket_common.c @@ -2,7 +2,7 @@ #include "musl_socket.h" #include #include "common_def.h" -#include "musl_socket_preinit_common.h" +#include "musl_socket_preinit.h" int socket(int domain, int type, int protocol) { @@ -15,4 +15,4 @@ int socket(int domain, int type, int protocol) return result; } -#endif \ No newline at end of file +#endif -- Gitee