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 LAST_FUNCTION,
25 };
26
27 enum EnumHookMode {
28 STARTUP_HOOK_MODE,
29 DIRECT_HOOK_MODE,
30 STEP_HOOK_MODE,
31 };
32
33 #ifdef HOOK_ENABLE
34 extern void* function_of_shared_lib[];
35 extern volatile atomic_llong ohos_malloc_hook_shared_library;
36 #endif // HOOK_ENABLE
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 __attribute__((always_inline))
__get_global_hook_flag()43 inline bool __get_global_hook_flag()
44 {
45 #ifdef HOOK_ENABLE
46 volatile bool g_flag = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire);
47 return g_flag;
48 #else
49 return false;
50 #endif // HOOK_ENABLE
51 }
52
53 __attribute__((always_inline))
__get_memleak_hook_flag()54 inline bool __get_memleak_hook_flag()
55 {
56 #ifdef HOOK_ENABLE
57 volatile bool memleak_flag = atomic_load_explicit(&__memleak_hook_flag, memory_order_acquire);
58 return memleak_flag;
59 #else
60 return false;
61 #endif // HOOK_ENABLE
62 }
63
64
65 __attribute__((always_inline))
__get_hook_flag()66 inline bool __get_hook_flag()
67 {
68 #ifdef HOOK_ENABLE
69 volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
70 if (impl_handle == NULL) {
71 return false;
72 }
73 else if (impl_handle == (void *)-1) {
74 return true;
75 }
76 else {
77 GetHookFlagType get_hook_func_ptr = (GetHookFlagType)(function_of_shared_lib[GET_HOOK_FLAG_FUNCTION]);
78 bool flag = get_hook_func_ptr();
79 return flag;
80 }
81 #else
82 return false;
83 #endif // HOOK_ENABLE
84 }
85
86 __attribute__((always_inline))
__set_hook_flag(bool flag)87 inline bool __set_hook_flag(bool flag)
88 {
89 #ifdef HOOK_ENABLE
90 volatile void* impl_handle = (void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
91 if (impl_handle == NULL) {
92 return false;
93 }
94 else if (impl_handle == (void *)-1) {
95 return true;
96 }
97 else {
98 SetHookFlagType set_hook_func_ptr = (SetHookFlagType)(function_of_shared_lib[SET_HOOK_FLAG_FUNCTION]);
99 return set_hook_func_ptr(flag);
100 }
101 #else
102 return false;
103 #endif // HOOK_ENABLE
104 }
105
106 __attribute__((always_inline))
get_current_dispatch_table()107 inline volatile const struct MallocDispatchType* get_current_dispatch_table()
108 {
109 #ifdef HOOK_ENABLE
110 volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
111 if (ret != NULL) {
112 if (__get_memleak_hook_flag()) {
113 return ret;
114 }
115 if (!__get_global_hook_flag()) {
116 ret = NULL;
117 }
118 else if (!__get_hook_flag()) {
119 ret = NULL;
120 }
121 }
122 return ret;
123 #else
124 return NULL;
125 #endif // HOOK_ENABLE
126 }
127
128 #define MUSL_HOOK_PARAM_NAME "libc.hook_mode"
129 #define OHOS_PARAM_MAX_SIZE 96
130 #define FILE_NAME_MAX_SIZE 40
131
132 #ifdef __cplusplus
133 }
134 #endif
135
136 #endif
137