1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22
23 #ifndef _MUSL_PREINIT_COMMON_H
24 #define _MUSL_PREINIT_COMMON_H
25
26 #include <stdatomic.h>
27 #include "musl_malloc_dispatch_table.h"
28 #include "musl_malloc_dispatch.h"
29 extern struct musl_libc_globals __musl_libc_globals;
30
31 extern struct MallocDispatchType __libc_malloc_default_dispatch;
32
33 extern volatile atomic_bool __hook_enable_hook_flag;
34
35 extern volatile atomic_bool __memleak_hook_flag;
36 extern volatile atomic_bool __custom_hook_flag;
37
38 extern bool checkLoadMallocMemTrack;
39
40 enum EnumFunc {
41 INITIALIZE_FUNCTION,
42 FINALIZE_FUNCTION,
43 GET_HOOK_FLAG_FUNCTION,
44 SET_HOOK_FLAG_FUNCTION,
45 ON_START_FUNCTION,
46 ON_END_FUNCTION,
47 SEND_HOOK_MISC_DATA,
48 GET_HOOK_CONFIG,
49 LAST_FUNCTION,
50 };
51
52 enum EnumHookMode {
53 STARTUP_HOOK_MODE,
54 DIRECT_HOOK_MODE,
55 STEP_HOOK_MODE,
56 };
57
58 #ifdef HOOK_ENABLE
59 extern void* function_of_shared_lib[];
60 extern volatile atomic_llong ohos_malloc_hook_shared_library;
61 #endif // HOOK_ENABLE
62
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66
67 __attribute__((always_inline))
__get_global_hook_flag()68 inline bool __get_global_hook_flag()
69 {
70 #ifdef HOOK_ENABLE
71 volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire);
72 return g_flag;
73 #else
74 return false;
75 #endif // HOOK_ENABLE
76 }
77
78 __attribute__((always_inline))
__get_memleak_hook_flag()79 inline bool __get_memleak_hook_flag()
80 {
81 #ifdef HOOK_ENABLE
82 volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire);
83 return memleak_flag;
84 #else
85 return false;
86 #endif // HOOK_ENABLE
87 }
88
89 __attribute__((always_inline))
__get_custom_hook_flag()90 inline bool __get_custom_hook_flag()
91 {
92 #ifdef HOOK_ENABLE
93 volatile bool custom_flag = atomic_load_explicit(&__custom_hook_flag, memory_order_acquire);
94 return custom_flag;
95 #else
96 return false;
97 #endif // HOOK_ENABLE
98 }
99
100 __attribute__((always_inline))
__get_hook_flag()101 inline bool __get_hook_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 GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]);
113 bool flag = get_hook_func_ptr();
114 return flag;
115 }
116 #else
117 return false;
118 #endif // HOOK_ENABLE
119 }
120
121 __attribute__((always_inline))
__set_hook_flag(bool flag)122 inline bool __set_hook_flag(bool flag)
123 {
124 #ifdef HOOK_ENABLE
125 volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
126 if (impl_handle == NULL) {
127 return false;
128 }
129 else if (impl_handle == (void *)-1) {
130 return true;
131 }
132 else {
133 SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]);
134 return set_hook_func_ptr(flag);
135 }
136 #else
137 return false;
138 #endif // HOOK_ENABLE
139 }
140
141 __attribute__((always_inline))
get_current_dispatch_table()142 inline volatile const struct MallocDispatchType* get_current_dispatch_table()
143 {
144 #ifdef HOOK_ENABLE
145 volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
146 if (ret != NULL) {
147 if (__get_custom_hook_flag()) {
148 return ret;
149 }
150 if (!__get_global_hook_flag()) {
151 ret = NULL;
152 }
153 else if (!__get_hook_flag()) {
154 ret = NULL;
155 }
156 }
157 return ret;
158 #else
159 return NULL;
160 #endif // HOOK_ENABLE
161 }
162
163 __attribute__((always_inline))
__send_hook_misc_data(uint64_t id,const char * stackPtr,size_t stackSize,uint32_t type)164 inline bool __send_hook_misc_data(uint64_t id, const char* stackPtr, size_t stackSize, uint32_t type)
165 {
166 #ifdef HOOK_ENABLE
167 volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
168 if (impl_handle == NULL) {
169 return false;
170 }
171 else if (impl_handle == (void*)-1) {
172 return false;
173 }
174 else {
175 SendHookMiscData send_hook_func_ptr = (SendHookMiscData)(function_of_shared_lib[SEND_HOOK_MISC_DATA]);
176 return send_hook_func_ptr(id, stackPtr, stackSize, type);
177 }
178 #else
179 return false;
180 #endif // HOOK_ENABLE
181 }
182
183 __attribute__((always_inline))
__get_hook_config()184 inline void* __get_hook_config()
185 {
186 #ifdef HOOK_ENABLE
187 volatile void* impl_handle = (void*)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
188 if (impl_handle == NULL) {
189 return NULL;
190 }
191 else if (impl_handle == (void*)-1) {
192 return NULL;
193 }
194 else {
195 GetHookConfig get_hook_func_ptr = (GetHookConfig)(function_of_shared_lib[GET_HOOK_CONFIG]);
196 return get_hook_func_ptr();
197 }
198 #else
199 return NULL;
200 #endif // HOOK_ENABLE
201 }
202
203 #define MUSL_HOOK_PARAM_NAME "libc.hook_mode"
204 #define OHOS_PARAM_MAX_SIZE 96
205 #define FILE_NAME_MAX_SIZE 40
206
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif
212