musl_preinit_common.h 2.6 KB
Newer Older
chuxuezhe1111's avatar
chuxuezhe1111 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#ifndef _MUSL_PREINIT_COMMON_H
#define _MUSL_PREINIT_COMMON_H

#include <stdatomic.h>
#include "musl_malloc_dispatch_table.h"
#include "musl_malloc_dispatch.h"

extern struct musl_libc_globals __musl_libc_globals;

extern struct MallocDispatchType __libc_malloc_default_dispatch;

extern volatile atomic_bool __hook_enable_hook_flag;

enum EnumFunc {
	INITIALIZE_FUNCTION,
	FINALIZE_FUNCTION,
	GET_HOOK_FLAG_FUNCTION,
	SET_HOOK_FLAG_FUNCTION,
	ON_START_FUNCTION,
	ON_END_FUNCTION,
	LAST_FUNCTION,
};

enum EnumHookMode {
	STATRUP_HOOK_MODE,
	DIRECT_HOOK_MODE,
	STEP_HOOK_MODE,
};

S
stesen 已提交
30
#ifdef HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
31
extern void* function_of_shared_lib[];
S
stesen 已提交
32 33
extern volatile atomic_llong ohos_malloc_hook_shared_library;
#endif // HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
34 35 36 37 38 39 40 41

#ifdef __cplusplus
extern "C" {
#endif

__attribute__((always_inline))
inline bool __get_global_hook_flag()
{
S
stesen 已提交
42
#ifdef HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
43 44
	volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire);
	return g_flag;
S
stesen 已提交
45 46 47
#else
	return false;
#endif // HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
48 49 50 51 52
}

__attribute__((always_inline))
inline bool __get_hook_flag()
{
S
stesen 已提交
53 54
#ifdef HOOK_ENABLE
	volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
chuxuezhe1111's avatar
chuxuezhe1111 已提交
55 56 57 58 59 60 61 62 63 64 65
	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;
	}
S
stesen 已提交
66 67 68
#else
	return false;
#endif // HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
69 70 71 72 73
}

__attribute__((always_inline))
inline bool __set_hook_flag(bool flag)
{
S
stesen 已提交
74 75
#ifdef HOOK_ENABLE
	volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
chuxuezhe1111's avatar
chuxuezhe1111 已提交
76 77 78 79 80 81 82 83 84 85
	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);
	}
S
stesen 已提交
86 87 88
#else
	return false;
#endif // HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
89 90 91 92 93
}

__attribute__((always_inline))
inline volatile const struct MallocDispatchType* get_current_dispatch_table()
{
S
stesen 已提交
94
#ifdef HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
95 96 97 98 99 100 101 102 103 104
	volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
	if (ret != NULL) {
		if (!__get_global_hook_flag()) {
			ret = NULL;
		}
		else if (!__get_hook_flag()) {
			ret = NULL;
		}
	}
	return ret;
S
stesen 已提交
105 106 107
#else
	return NULL;
#endif // HOOK_ENABLE
chuxuezhe1111's avatar
chuxuezhe1111 已提交
108 109 110 111 112 113 114 115 116 117
}

#define MUSL_HOOK_PARAM_NAME "libc.hook_mode"
#define OHOS_PARAM_MAX_SIZE 96

#ifdef __cplusplus
}
#endif

#endif