1 /*
2  * Copyright (c) 2022 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 const static unsigned int SLEEP_TIMEOUT = 360000;
26 
CreateThread(int * argv)27 static void CreateThread(int *argv)
28 {
29     int threadID = *argv;
30     printf("create MultiThread %d\n", threadID);
31     TestFunc1();
32     return;
33 }
34 
CreateThreadForCrash(const int * argv)35 static void CreateThreadForCrash(const int *argv)
36 {
37     int threadID = *argv;
38     printf("create ThreadForCrash %d\n", threadID);
39     int ret = raise(SIGSEGV);
40     if (ret != 0) {
41         printf("remove failed!");
42     }
43     return;
44 }
45 
MultiThreadConstructor(const int threadNum)46 NOINLINE int MultiThreadConstructor(const int threadNum)
47 {
48     pthread_t t[threadNum];
49     int threadID[threadNum];
50 
51     for (int i = 0; i < threadNum; ++i) {
52         threadID[i] = i;
53         pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]);
54         pthread_detach(t[i]);
55     }
56 
57     while (1) {
58         continue;
59     }
60 
61     return 0;
62 }
63 
MultiThreadConstructorForThreadCrash(const int threadNum)64 NOINLINE int MultiThreadConstructorForThreadCrash(const int threadNum)
65 {
66     pthread_t t[threadNum];
67     int threadID[threadNum];
68     pthread_t threadCrash;
69     int threadIDCrash = threadNum;
70 
71     for (int i = 0; i < threadNum; ++i) {
72         threadID[i] = i;
73         pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]);
74         pthread_detach(t[i]);
75     }
76     pthread_create(&threadCrash, NULL, (void *(*)(void *))CreateThreadForCrash, &threadIDCrash);
77     pthread_detach(threadCrash);
78 
79     sleep(5); // 5 : wait 5s, then exit process
80 
81     return 0;
82 }
83 
TestFunc70(void)84 NOINLINE int TestFunc70(void)
85 {
86     sleep(SLEEP_TIMEOUT);
87     return 0;
88 }
89 
90 #ifndef UNITTEST
main(int argc,char * argv[])91 int main(int argc, char* argv[])
92 {
93     const int argumentLimit = 2;
94     if (argc != argumentLimit) {
95         printf("invalid input argument.\n");
96         return 0;
97     }
98     MultiThreadConstructor(atoi(argv[1]));
99     return 0;
100 }
101 #endif
102 
103 // auto gen function
104 GEN_TEST_FUNCTION(0, 1)
105 GEN_TEST_FUNCTION(1, 2)
106 GEN_TEST_FUNCTION(2, 3)
107 GEN_TEST_FUNCTION(3, 4)
108 GEN_TEST_FUNCTION(4, 5)
109 GEN_TEST_FUNCTION(5, 6)
110 GEN_TEST_FUNCTION(6, 7)
111 GEN_TEST_FUNCTION(7, 8)
112 GEN_TEST_FUNCTION(8, 9)
113 GEN_TEST_FUNCTION(9, 10)
114 
115 GEN_TEST_FUNCTION(10, 11)
116 GEN_TEST_FUNCTION(11, 12)
117 GEN_TEST_FUNCTION(12, 13)
118 GEN_TEST_FUNCTION(13, 14)
119 GEN_TEST_FUNCTION(14, 15)
120 GEN_TEST_FUNCTION(15, 16)
121 GEN_TEST_FUNCTION(16, 17)
122 GEN_TEST_FUNCTION(17, 18)
123 GEN_TEST_FUNCTION(18, 19)
124 GEN_TEST_FUNCTION(19, 20)
125 
126 GEN_TEST_FUNCTION(20, 21)
127 GEN_TEST_FUNCTION(21, 22)
128 GEN_TEST_FUNCTION(22, 23)
129 GEN_TEST_FUNCTION(23, 24)
130 GEN_TEST_FUNCTION(24, 25)
131 GEN_TEST_FUNCTION(25, 26)
132 GEN_TEST_FUNCTION(26, 27)
133 GEN_TEST_FUNCTION(27, 28)
134 GEN_TEST_FUNCTION(28, 29)
135 GEN_TEST_FUNCTION(29, 30)
136 
137 GEN_TEST_FUNCTION(30, 31)
138 GEN_TEST_FUNCTION(31, 32)
139 GEN_TEST_FUNCTION(32, 33)
140 GEN_TEST_FUNCTION(33, 34)
141 GEN_TEST_FUNCTION(34, 35)
142 GEN_TEST_FUNCTION(35, 36)
143 GEN_TEST_FUNCTION(36, 37)
144 GEN_TEST_FUNCTION(37, 38)
145 GEN_TEST_FUNCTION(38, 39)
146 GEN_TEST_FUNCTION(39, 40)
147 
148 GEN_TEST_FUNCTION(40, 41)
149 GEN_TEST_FUNCTION(41, 42)
150 GEN_TEST_FUNCTION(42, 43)
151 GEN_TEST_FUNCTION(43, 44)
152 GEN_TEST_FUNCTION(44, 45)
153 GEN_TEST_FUNCTION(45, 46)
154 GEN_TEST_FUNCTION(46, 47)
155 GEN_TEST_FUNCTION(47, 48)
156 GEN_TEST_FUNCTION(48, 49)
157 GEN_TEST_FUNCTION(49, 50)
158 
159 GEN_TEST_FUNCTION(50, 51)
160 GEN_TEST_FUNCTION(51, 52)
161 GEN_TEST_FUNCTION(52, 53)
162 GEN_TEST_FUNCTION(53, 54)
163 GEN_TEST_FUNCTION(54, 55)
164 GEN_TEST_FUNCTION(55, 56)
165 GEN_TEST_FUNCTION(56, 57)
166 GEN_TEST_FUNCTION(57, 58)
167 GEN_TEST_FUNCTION(58, 59)
168 GEN_TEST_FUNCTION(59, 60)
169 
170 GEN_TEST_FUNCTION(60, 61)
171 GEN_TEST_FUNCTION(61, 62)
172 GEN_TEST_FUNCTION(62, 63)
173 GEN_TEST_FUNCTION(63, 64)
174 GEN_TEST_FUNCTION(64, 65)
175 GEN_TEST_FUNCTION(65, 66)
176 GEN_TEST_FUNCTION(66, 67)
177 GEN_TEST_FUNCTION(67, 68)
178 GEN_TEST_FUNCTION(68, 69)
179 GEN_TEST_FUNCTION(69, 70)
180