• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * Currently,musl malloc/free hooks may introduce risks to GWP_ASan's memory management. This TEST aim to
18  * enhance libc-test by adding GWP_ASan test case for proactive monitoring.
19  */
20 
21 #include <signal.h>
22 #include <stdio.h>
23 #include <sys/wait.h>
24 #include "gwp_asan_test.h"
25 #include "test.h"
26 
handler(void)27 void handler(void)
28 {
29     t_error("Under the condition of memory hooks, GWP_ASan is encountering errors during usage.\n");
30     _exit(1);
31 }
32 
install_sigv_handler(void)33 void install_sigv_handler(void)
34 {
35     struct sigaction sigv = {
36         .sa_handler = handler,
37         .sa_flags = 0,
38     };
39     sigaction(SIGSEGV, &sigv, NULL);
40 }
41 
enable_hook_test(void)42 void enable_hook_test(void)
43 {
44     config_gwp_asan_environment(true);
45     clear_log(GWP_ASAN_LOG_DIR, GWP_ASAN_LOG_TAG);
46     install_sigv_handler();
47 
48     void *ptr = malloc(20);
49     if (!ptr) {
50         t_error("malloc failed.");
51         return;
52     }
53     if (!libc_gwp_asan_ptr_is_mine(ptr)) {
54         t_error("Memory is not allocated by gwp_asan.\n");
55         return;
56     }
57     // send signal 36 to self process and enable memory hook.
58     send_sig36();
59 
60     free(ptr);
61 }
62 
main(void)63 int main(void)
64 {
65     pid_t pid = fork();
66     if (pid < 0) {
67         t_error("FAIL fork failed.");
68     } else if (pid == 0) { // child process
69         enable_hook_test();
70     } else { // parent process
71         int status;
72         if (waitpid(pid, &status, 0) != pid) {
73             t_error("gwp_asan_enable_hook_test waitpid failed.\n");
74         }
75 
76         if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
77             t_error("gwp_asan_enable_hook_test failed.\n");
78         }
79 
80         cancel_gwp_asan_environment(true);
81     }
82 
83     return t_status;
84 }
85