• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _MUSL_PREINIT_COMMON_H
2 #define _MUSL_PREINIT_COMMON_H
3 
4 #include <stdatomic.h>
5 #include "musl_malloc_dispatch_table.h"
6 #include "musl_malloc_dispatch.h"
7 
8 extern struct musl_libc_globals __musl_libc_globals;
9 
10 extern struct MallocDispatchType __libc_malloc_default_dispatch;
11 
12 extern volatile atomic_bool __hook_enable_hook_flag;
13 
14 extern volatile atomic_bool __memleak_hook_flag;
15 extern volatile atomic_bool __custom_hook_flag;
16 
17 extern bool checkLoadMallocMemTrack;
18 
19 enum EnumFunc {
20 	INITIALIZE_FUNCTION,
21 	FINALIZE_FUNCTION,
22 	GET_HOOK_FLAG_FUNCTION,
23 	SET_HOOK_FLAG_FUNCTION,
24 	ON_START_FUNCTION,
25 	ON_END_FUNCTION,
26 	SEND_HOOK_MISC_DATA,
27 	GET_HOOK_CONFIG,
28 	LAST_FUNCTION,
29 };
30 
31 enum EnumHookMode {
32 	STARTUP_HOOK_MODE,
33 	DIRECT_HOOK_MODE,
34 	STEP_HOOK_MODE,
35 };
36 
37 #ifdef HOOK_ENABLE
38 extern void* function_of_shared_lib[];
39 extern volatile atomic_llong ohos_malloc_hook_shared_library;
40 #endif // HOOK_ENABLE
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 __attribute__((always_inline))
__get_global_hook_flag()47 inline bool __get_global_hook_flag()
48 {
49 #ifdef HOOK_ENABLE
50 	volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire);
51 	return g_flag;
52 #else
53 	return false;
54 #endif // HOOK_ENABLE
55 }
56 
57 __attribute__((always_inline))
__get_memleak_hook_flag()58 inline bool __get_memleak_hook_flag()
59 {
60 #ifdef HOOK_ENABLE
61 	volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire);
62 	return memleak_flag;
63 #else
64 	return false;
65 #endif // HOOK_ENABLE
66 }
67 
68 __attribute__((always_inline))
__get_custom_hook_flag()69 inline bool __get_custom_hook_flag()
70 {
71 #ifdef HOOK_ENABLE
72 	volatile bool custom_flag = atomic_load_explicit(&__custom_hook_flag, memory_order_acquire);
73 	return custom_flag;
74 #else
75 	return false;
76 #endif // HOOK_ENABLE
77 }
78 
79 __attribute__((always_inline))
__get_hook_flag()80 inline bool __get_hook_flag()
81 {
82 #ifdef HOOK_ENABLE
83 	volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
84 	if (impl_handle == NULL) {
85 		return false;
86 	}
87 	else if (impl_handle == (void *)-1) {
88 		return true;
89 	}
90 	else {
91 		GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]);
92 		bool flag = get_hook_func_ptr();
93 		return flag;
94 	}
95 #else
96 	return false;
97 #endif // HOOK_ENABLE
98 }
99 
100 __attribute__((always_inline))
__set_hook_flag(bool flag)101 inline bool __set_hook_flag(bool flag)
102 {
103 #ifdef HOOK_ENABLE
104 	volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
105 	if (impl_handle == NULL) {
106 		return false;
107 	}
108 	else if (impl_handle == (void *)-1) {
109 		return true;
110 	}
111 	else {
112 		SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]);
113 		return set_hook_func_ptr(flag);
114 	}
115 #else
116 	return false;
117 #endif // HOOK_ENABLE
118 }
119 
120 __attribute__((always_inline))
get_current_dispatch_table()121 inline volatile const struct MallocDispatchType* get_current_dispatch_table()
122 {
123 #ifdef HOOK_ENABLE
124 	volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
125 	if (ret != NULL) {
126 		if (__get_custom_hook_flag()) {
127 			return ret;
128 		}
129 		if (!__get_global_hook_flag()) {
130 			ret = NULL;
131 		}
132 		else if (!__get_hook_flag()) {
133 			ret = NULL;
134 		}
135 	}
136 	return ret;
137 #else
138 	return NULL;
139 #endif // HOOK_ENABLE
140 }
141 
142 __attribute__((always_inline))
__send_hook_misc_data(uint64_t id,const char * stackPtr,size_t stackSize,uint32_t type)143 inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, size_t stackSize, uint32_t type)
144 {
145 #ifdef HOOK_ENABLE
146 	volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
147 	if (impl_handle == NULL) {
148 		return false;
149 	}
150 	else if (impl_handle == (void*)-1) {
151 		return false;
152 	}
153 	else {
154 		SendHookMiscData send_hook_func_ptr = (SendHookMiscData)(function_of_shared_lib[SEND_HOOK_MISC_DATA]);
155 		return send_hook_func_ptr(id, stackPtr, stackSize, type);
156 	}
157 #else
158 	return false;
159 #endif // HOOK_ENABLE
160 }
161 
162 __attribute__((always_inline))
__get_hook_config()163 inline void* __get_hook_config()
164 {
165 #ifdef HOOK_ENABLE
166 	volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
167 	if (impl_handle == NULL) {
168 		return NULL;
169 	}
170 	else if (impl_handle == (void*)-1) {
171 		return NULL;
172 	}
173 	else {
174 		GetHookConfig get_hook_func_ptr = (GetHookConfig)(function_of_shared_lib[GET_HOOK_CONFIG]);
175 		return get_hook_func_ptr();
176 	}
177 #else
178 	return NULL;
179 #endif // HOOK_ENABLE
180 }
181 
182 #define MUSL_HOOK_PARAM_NAME "libc.hook_mode"
183 #define OHOS_PARAM_MAX_SIZE 96
184 #define FILE_NAME_MAX_SIZE 40
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif
191