1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdatomic.h>
4 #include <memory.h>
5 #include "test.h"
6 #define HOOK_ENABLE
7 #include "musl_preinit_common.h"
8
9 #define TEST(c, ...) \
10 ( (c) || (t_error(#c " failed: " __VA_ARGS__),0) )
11
12 static bool client_hook_flag;
13
get_hook_flag()14 bool get_hook_flag()
15 {
16 return client_hook_flag;
17 }
18
set_hook_flag(bool flag)19 bool set_hook_flag(bool flag)
20 {
21 bool before_flag = client_hook_flag;
22 client_hook_flag = flag;
23 return before_flag;
24 }
25
install_hook_function()26 void install_hook_function()
27 {
28 memset(function_of_shared_lib, 0, sizeof(function_of_shared_lib[0]) * LAST_FUNCTION);
29 function_of_shared_lib[GET_HOOK_FLAG_FUNCTION] = get_hook_flag;
30 function_of_shared_lib[SET_HOOK_FLAG_FUNCTION] = set_hook_flag;
31 }
32
main(int argc,char ** argv)33 int main(int argc, char **argv)
34 {
35 printf("Installing client's functions...");
36 install_hook_function();
37 printf("\nInstallation Completes\n");
38
39 printf("Start 7 scenes testing...\n");
40
41 printf("Case 1: client shared library has not been loaded.\n");
42 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)0, memory_order_seq_cst);
43 bool hook_flag = __get_hook_flag();
44 TEST(hook_flag == false, "hook_flag shoud be false\n");
45 volatile const struct MallocDispatchType* malloc_dispatch_table = get_current_dispatch_table();
46 TEST(malloc_dispatch_table == NULL, "malloc_dispatch_table shoud be NULL\n");
47
48 printf("Case 2: client shared library is loading, no uninstall signal raising, but the functions hasn't been loaded, using the temporary malloc table.\n");
49 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)-1, memory_order_seq_cst);
50 atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile const long long)0x95919591, memory_order_seq_cst);
51 atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)true, memory_order_seq_cst);
52 hook_flag = __get_hook_flag();
53 TEST(hook_flag == true, "hook_flag shoud be true\n");
54 malloc_dispatch_table = get_current_dispatch_table();
55 TEST(malloc_dispatch_table != NULL, "malloc_dispatch_table shoud be not NULL\n");
56
57 printf("Case 3: client shared library is loading, uninstall signal raising, but the functions hasn't been loaded, using the temporary malloc table.\n");
58 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)-1, memory_order_seq_cst);
59 atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile const long long)0x95919591, memory_order_seq_cst);
60 atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
61 hook_flag = __get_hook_flag();
62 TEST(hook_flag == true, "hook_flag shoud be true\n");
63 malloc_dispatch_table = get_current_dispatch_table();
64 TEST(malloc_dispatch_table == NULL, "malloc_dispatch_table shoud be NULL\n");
65
66 printf("Case 4: client shared library is loaded, no uninstall signal raising, outside client malloc.\n");
67 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)0x10319090, memory_order_seq_cst);
68 atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)true, memory_order_seq_cst);
69 set_hook_flag(true);
70 hook_flag = __get_hook_flag();
71 TEST(hook_flag == true, "hook_flag shoud be true\n");
72 malloc_dispatch_table = get_current_dispatch_table();
73 TEST(malloc_dispatch_table != NULL, "malloc_dispatch_table shoud be not NULL\n");
74
75 printf("Case 5: client shared library is loaded, no uninstall signal raising, inside client malloc.\n");
76 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)0x10319090, memory_order_seq_cst);
77 atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)true, memory_order_seq_cst);
78 set_hook_flag(false);
79 hook_flag = __get_hook_flag();
80 TEST(hook_flag == false, "hook_flag shoud be false\n");
81 malloc_dispatch_table = get_current_dispatch_table();
82 TEST(malloc_dispatch_table == NULL, "malloc_dispatch_table shoud be NULL\n");
83
84 printf("Case 6: client shared library is loaded, uninstall signal raising, outside client malloc.\n");
85 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)0x10319090, memory_order_seq_cst);
86 atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
87 set_hook_flag(true);
88 hook_flag = __get_hook_flag();
89 TEST(hook_flag == true, "hook_flag shoud be true\n");
90 malloc_dispatch_table = get_current_dispatch_table();
91 TEST(malloc_dispatch_table == NULL, "malloc_dispatch_table shoud be NULL\n");
92
93 printf("Case 7: client shared library is loaded, uninstall signal raising, inside client malloc.\n");
94 atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)0x10319090, memory_order_seq_cst);
95 atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
96 set_hook_flag(false);
97 hook_flag = __get_hook_flag();
98 TEST(hook_flag == false, "hook_flag shoud be false\n");
99 malloc_dispatch_table = get_current_dispatch_table();
100 TEST(malloc_dispatch_table == NULL, "malloc_dispatch_table shoud be NULL\n");
101 TEST(malloc_dispatch_table == NULL, "malloc_dispatch_table shoud be NULL\n");
102
103 if (t_status == 0) {
104 printf("All test cases passed!\n");
105 } else {
106 printf("***There are some test cases not passed.\n");
107 }
108 return t_status;
109 }
110