• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "multithread_constructor.h"
17 
18 #include <pthread.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include "stdio.h"
23 #include "stdlib.h"
24 
25 #define NOINLINE __attribute__((noinline))
26 
27 const static unsigned int SLEEP_TIMEOUT = 360000;
28 
CreateThread(int * argv)29 static void CreateThread(int *argv)
30 {
31     int threadID = *argv;
32     printf("create MultiThread %d\n", threadID);
33     sleep(3); // 3 : sleep 3 seconds // 3 : three seconds
34     return;
35 }
36 
CreateThreadForCrash(const int * argv)37 static void CreateThreadForCrash(const int *argv)
38 {
39     int threadID = *argv;
40     printf("create ThreadForCrash %d\n", threadID);
41     int ret = raise(SIGSEGV);
42     if (ret != 0) {
43         printf("remove failed!");
44     }
45     return;
46 }
47 
CreateMultiThreadProcess(int threadNum)48 pid_t CreateMultiThreadProcess(int threadNum)
49 {
50     pid_t pid = fork();
51     if (pid < 0) {
52         printf("Failed to fork new test process.");
53     } else if (pid == 0) {
54         (void)MultiThreadConstructor(threadNum);
55         sleep(3); // 3 : sleep 3 seconds
56         _exit(0);
57     }
58     return pid;
59 }
60 
CreateMultiThreadForThreadCrash(int threadNum)61 pid_t CreateMultiThreadForThreadCrash(int threadNum)
62 {
63     pid_t pid = fork();
64     if (pid < 0) {
65         printf("Failed to fork new test process.");
66     } else if (pid == 0) {
67         (void)MultiThreadConstructorForThreadCrash(threadNum);
68     }
69     return pid;
70 }
71 
CreateMultiThreadForThreadCrashWithOpen(int threadNum,int openNum)72 pid_t CreateMultiThreadForThreadCrashWithOpen(int threadNum, int openNum)
73 {
74     pid_t pid = fork();
75     if (pid < 0) {
76         printf("Failed to fork new test process.");
77     } else if (pid == 0) {
78         for (int i = 0; i < openNum; ++i) {
79             fopen("/dev/null", "r");
80         }
81         (void)MultiThreadConstructorForThreadCrash(threadNum);
82     }
83     return pid;
84 }
85 
MultiThreadConstructor(const int threadNum)86 NOINLINE int MultiThreadConstructor(const int threadNum)
87 {
88     pthread_t t[threadNum];
89     int threadID[threadNum];
90 
91     for (int i = 0; i < threadNum; ++i) {
92         threadID[i] = i;
93         pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]);
94         pthread_detach(t[i]);
95     }
96     return 0;
97 }
98 
MultiThreadConstructorForThreadCrash(const int threadNum)99 NOINLINE int MultiThreadConstructorForThreadCrash(const int threadNum)
100 {
101     pthread_t t[threadNum];
102     int threadID[threadNum];
103     pthread_t threadCrash;
104     int threadIDCrash = threadNum;
105 
106     for (int i = 0; i < threadNum; ++i) {
107         threadID[i] = i;
108         pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]);
109         pthread_detach(t[i]);
110     }
111     pthread_create(&threadCrash, NULL, (void *(*)(void *))CreateThreadForCrash, &threadIDCrash);
112     pthread_detach(threadCrash);
113 
114     sleep(5); // 5 : wait 5s, then exit process
115 
116     return 0;
117 }
118 
TestFunc70(void)119 NOINLINE int TestFunc70(void)
120 {
121     sleep(SLEEP_TIMEOUT);
122     return 0;
123 }
124 
125 #ifndef UNITTEST
main(int argc,char * argv[])126 int main(int argc, char* argv[])
127 {
128     const int argumentLimit = 2;
129     if (argc != argumentLimit) {
130         printf("invalid input argument.\n");
131         return 0;
132     }
133     MultiThreadConstructor(atoi(argv[1]));
134     return 0;
135 }
136 #endif
137