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