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