未验证 提交 d8169193 编写于 作者: O openharmony_ci 提交者: Gitee

!531 优化native hook 中的全局变量

Merge pull request !531 from zhangcui/master
......@@ -44,24 +44,21 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.munmap = MuslMalloc(munmap),
};
#define MAX_SYM_NAME_SIZE 1000
#define MAX_PROC_NAME_SIZE 256
static char *__malloc_hook_shared_lib = "libnative_hook.z.so";
static char *__malloc_hook_function_prefix = "ohos_malloc_hook";
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 __progname[PATH_MAX + 1] = {0};
static char __param_value[OHOS_PARAM_MAX_SIZE + 1] = {0};
static char* get_native_hook_param()
static void get_native_hook_param(char *buf, unsigned int buf_len)
{
#ifdef OHOS_ENABLE_PARAMETER
const char *key = MUSL_HOOK_PARAM_NAME;
unsigned int len = OHOS_PARAM_MAX_SIZE;
(void)SystemReadParam(key, __param_value, &len);
return __param_value;
unsigned int len = buf_len;
(void)SystemReadParam(key, buf, &len);
#else
return NULL;
return;
#endif
}
......@@ -70,16 +67,15 @@ static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size)
if (!mode || !path || size <= 0) {
return -1;
}
bool flag = __set_hook_flag(false);
char* hook_param_value = get_native_hook_param();
if (hook_param_value == NULL || hook_param_value[0] == '\0') {
char hook_param_value[OHOS_PARAM_MAX_SIZE + 1] = {0};
unsigned int len = OHOS_PARAM_MAX_SIZE;
get_native_hook_param(hook_param_value, len);
if (hook_param_value[0] == '\0') {
*mode = STEP_HOOK_MODE;
path[0] = '\0';
} else {
char* ptr = hook_param_value;
char* mode_str = hook_param_value;
while (*ptr && *ptr != ':') {
++ptr;
}
......@@ -89,11 +85,11 @@ static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size)
++ptr;
}
if (strcmp(mode_str, "startup") == 0) {
if (strcmp(hook_param_value, "startup") == 0) {
*mode = STARTUP_HOOK_MODE;
} else if (strcmp(mode_str, "direct") == 0) {
} else if (strcmp(hook_param_value, "direct") == 0) {
*mode = DIRECT_HOOK_MODE;
} else if (strcmp(mode_str, "step") == 0) {
} else if (strcmp(hook_param_value, "step") == 0) {
*mode = STEP_HOOK_MODE;
} else {
*mode = STEP_HOOK_MODE;
......@@ -427,17 +423,19 @@ __attribute__((constructor(1))) static void __musl_initialize()
{
atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
__set_default_malloc();
parse_hook_variable(&__hook_mode, __hook_process_path, sizeof(__hook_process_path));
char hook_process_path[MAX_PROC_NAME_SIZE + 1] = {0};
parse_hook_variable(&__hook_mode, hook_process_path, sizeof(hook_process_path) - 1);
if (__hook_mode == STARTUP_HOOK_MODE) {
if (get_proc_name(getpid(), __progname, sizeof(__progname) - 1)) {
const char *pos = strrchr(__progname, '/');
char proc_name[MAX_PROC_NAME_SIZE + 1] = {0};
if (get_proc_name(getpid(), proc_name, sizeof(proc_name) - 1)) {
const char *pos = strrchr(proc_name, '/');
const char* file_name;
if (pos != NULL) {
file_name = pos + 1;
} else {
file_name = __progname;
file_name = proc_name;
}
if (strncmp(file_name, __hook_process_path, strlen(__hook_process_path)) == 0) {
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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册