• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 #include <malloc.h>
17 #include <pthread.h>
18 #include <sys/wait.h>
19 #include "gwp_asan_test.h"
20 #include "test.h"
21 
22 #define REPS 50
23 static int a = 0;
24 
fork_task(void * arg)25 void* fork_task(void *arg)
26 {
27     pid_t pid = fork();
28     if (pid < 0) {
29         t_error("FAIL fork failed.");
30     } else if (pid == 0) { // child process
31         a++;
32     } else { // parent process
33         a++;
34         return NULL;
35     }
36     return NULL;
37 }
38 
thread_fork_test()39 void thread_fork_test()
40 {
41     config_gwp_asan_environment(false);
42     for (int i = 0; i < REPS; i++) {
43         pthread_t tid;
44         pthread_create(&tid, NULL, fork_task, NULL);
45         pthread_join(tid, NULL);
46     }
47 }
48 
49 // Test whether it's ok to open gwp_asan in the sub-thread fork scenario.
main()50 int main()
51 {
52     pid_t pid = fork();
53     if (pid < 0) {
54         t_error("FAIL fork failed.");
55     } else if (pid == 0) { // child process
56         thread_fork_test();
57     } else { // parent process
58         int status;
59         if (waitpid(pid, &status, 0) != pid) {
60             t_error("gwp_asan_thread_fork_test waitpid failed.");
61         }
62 
63         if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
64             t_error("gwp_asan_thread_fork_test failed.");
65         }
66 
67         cancel_gwp_asan_environment(false);
68     }
69 
70     return t_status;
71 }