From 9f99486c9856a6e5f165f489bd0d99f6ed8bb93f Mon Sep 17 00:00:00 2001 From: jwz Date: Fri, 26 Aug 2022 17:01:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=93=E5=8F=96native=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=98=B6=E6=AE=B5=E7=9A=84=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jwz --- musl_template.gni | 7 ++ porting/linux/user/ldso/dynlink.c | 7 +- porting/linux/user/src/hook/musl_preinit.c | 78 +++++++++++++------ .../linux/user/src/hook/musl_preinit_common.h | 2 +- 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/musl_template.gni b/musl_template.gni index 0a0a5e52..1138774b 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -187,6 +187,7 @@ template("musl_libs") { defines += [ "OHOS_DNS_PROXY_BY_NETSYS=1", "OHOS_PERMISSION_INTERNET=1", + "OHOS_ENABLE_PARAMETER", ] } @@ -470,6 +471,9 @@ template("musl_libs") { } deps = porting_deps + if (is_standard_system) { + deps += [ "//base/startup/init/services/param/base:parameterbase" ] + } } source_set("${abi_prefix}_musl_hook") { @@ -490,6 +494,9 @@ template("musl_libs") { "//third_party/musl:musl_copy_inc_root", "//third_party/musl:musl_copy_inc_sys", ] + if (is_standard_system) { + deps += [ "//base/startup/init/services/param/base:parameterbase" ] + } configs -= musl_inherited_configs diff --git a/porting/linux/user/ldso/dynlink.c b/porting/linux/user/ldso/dynlink.c index 7a38fc19..a92c96cb 100644 --- a/porting/linux/user/ldso/dynlink.c +++ b/porting/linux/user/ldso/dynlink.c @@ -34,6 +34,9 @@ #include "ns_config.h" #include "pthread_impl.h" #include "strops.h" +#ifdef OHOS_ENABLE_PARAMETER +#include "sys_param.h" +#endif static void error(const char *, ...); @@ -2280,7 +2283,9 @@ void __dls3(size_t *sp, size_t *auxv) env_path = getenv("LD_LIBRARY_PATH"); env_preload = getenv("LD_PRELOAD"); } - +#ifdef OHOS_ENABLE_PARAMETER + InitParameterClient(); +#endif /* If the main program was already loaded by the kernel, * AT_PHDR will point to some location other than the dynamic * linker's program headers. */ diff --git a/porting/linux/user/src/hook/musl_preinit.c b/porting/linux/user/src/hook/musl_preinit.c index 0eebbccd..8e0ff753 100644 --- a/porting/linux/user/src/hook/musl_preinit.c +++ b/porting/linux/user/src/hook/musl_preinit.c @@ -21,10 +21,14 @@ which need be escaped. #include "musl_malloc.h" #include "memory_tag.h" #include "musl_preinit_common.h" +#ifdef OHOS_ENABLE_PARAMETER +#include "sys_param.h" +#endif #include #include #include #include +#include #include #include #include @@ -42,39 +46,26 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { #define MAX_SYM_NAME_SIZE 1000 static char *__malloc_hook_shared_lib = "libnative_hook.z.so"; static char *__malloc_hook_function_prefix = "ohos_malloc_hook"; -static char *__get_param_shared_Lib = "libparam_client.z.so"; volatile atomic_llong ohos_malloc_hook_shared_library; void* function_of_shared_lib[LAST_FUNCTION]; static enum EnumHookMode __hook_mode = STEP_HOOK_MODE; -static char __hook_process_path[PATH_MAX+ 1] = {0}; +static char __hook_process_path[PATH_MAX + 1] = {0}; static char __progname[PATH_MAX + 1] = {0}; - static char* get_native_hook_param() { - int (*getFunction)(const char *name, char *value, unsigned int *len); - - void* shared_library_handle = dlopen(__get_param_shared_Lib, RTLD_NOW | RTLD_LOCAL); - if (!shared_library_handle) { - return NULL; - } - - getFunction = (int (*)(const char *name, char *value, unsigned int *len))dlsym((void*)shared_library_handle, "SystemGetParameter"); - if (getFunction == NULL) { - dlclose(shared_library_handle); - return NULL; - } +#ifdef OHOS_ENABLE_PARAMETER const char *key = MUSL_HOOK_PARAM_NAME; - char *value = (char *)malloc(OHOS_PARAM_MAX_SIZE); + char *value = (char *)calloc(OHOS_PARAM_MAX_SIZE, sizeof(char)); if (value == NULL) { - dlclose(shared_library_handle); return NULL; } - memset(value, 0, OHOS_PARAM_MAX_SIZE); unsigned int len = OHOS_PARAM_MAX_SIZE; - getFunction(key, value, &len); - dlclose(shared_library_handle); + (void)SystemReadParam(key, value, &len); return value; +#else + return NULL; +#endif } static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) @@ -102,7 +93,7 @@ static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) } if (strcmp(mode_str, "startup") == 0) { - *mode = STATRUP_HOOK_MODE; + *mode = STARTUP_HOOK_MODE; } else if (strcmp(mode_str, "direct") == 0) { *mode = DIRECT_HOOK_MODE; } else if (strcmp(mode_str, "step") == 0) { @@ -110,7 +101,7 @@ static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) } else { *mode = STEP_HOOK_MODE; } - if (*mode == STATRUP_HOOK_MODE) { + if (*mode == STARTUP_HOOK_MODE) { if (*ptr == '\"') { ++ptr; int idx = 0; @@ -133,6 +124,26 @@ static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size) return 0; } +static bool get_proc_name(pid_t pid, char *buf, unsigned int buf_len) +{ + if (pid <= 0) { + return false; + } + const int file_name_max_size = 40; + char target_file[file_name_max_size] = {0}; + (void)sprintf(target_file, "/proc/%d/cmdline", pid); + FILE *f = fopen(target_file, "r"); + if (f == NULL) { + return false; + } + if (fgets(buf, buf_len, f) == NULL) { + (void)fclose(f); + return false; + } + (void)fclose(f); + return true; +} + static bool init_malloc_function(void* malloc_shared_library_handler, MallocMallocType* func, const char* prefix) { char symbol[MAX_SYM_NAME_SIZE]; @@ -418,12 +429,31 @@ static void __initialize_malloc() __install_malloc_hook_signal_handler(); } - __attribute__((constructor(1))) static void __musl_initialize() { atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst); __set_default_malloc(); - //__init_musl_log(); + parse_hook_variable(&__hook_mode, __hook_process_path, sizeof(__hook_process_path)); + if (__hook_mode == STARTUP_HOOK_MODE) { + if (get_proc_name(getpid(), __progname, sizeof(__progname) - 1)) { + const char *pos = strrchr(__progname, '/'); + const char* file_name; + if (pos != NULL) { + file_name = pos + 1; + } else { + file_name = __progname; + } + if (strncmp(file_name, __hook_process_path, strlen(__hook_process_path)) == 0) { + atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)true, memory_order_seq_cst); + init_ohos_malloc_hook(); + } else { + __hook_mode = STEP_HOOK_MODE; + } + } else { + __hook_mode = STEP_HOOK_MODE; + } + } __initialize_malloc(); + errno = 0; } #endif diff --git a/porting/linux/user/src/hook/musl_preinit_common.h b/porting/linux/user/src/hook/musl_preinit_common.h index b6deb70c..a6386b75 100644 --- a/porting/linux/user/src/hook/musl_preinit_common.h +++ b/porting/linux/user/src/hook/musl_preinit_common.h @@ -22,7 +22,7 @@ enum EnumFunc { }; enum EnumHookMode { - STATRUP_HOOK_MODE, + STARTUP_HOOK_MODE, DIRECT_HOOK_MODE, STEP_HOOK_MODE, }; -- GitLab