1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_test_exc.h"
32 #include "pthread.h"
33 
34 #define TEST_THREAD_COUNT 5
35 static volatile int g_testCondFlag;
36 static int g_count[TEST_THREAD_COUNT];
37 
ThreadFunc2(void *arg)38 __attribute__((optnone)) static void *ThreadFunc2(void *arg)
39 {
40     int count = *(int *)arg;
41     int *test = nullptr;
42 
43     g_testCondFlag++;
44 
45     while (g_testCondFlag < 2) { /* 2, no special meaning  */
46     }
47 
48     *test = 0x1;
49 
50     while (1) {
51     }
52 
53     return nullptr;
54 }
55 
TestThread(void)56 static int TestThread(void)
57 {
58     pthread_t newThread;
59     int ret;
60     int currThreadPolicy = 3;
61     int currThreadPri;
62     pthread_attr_t a = { 0 };
63     struct sched_param param = { 0 };
64     int count = 0;
65 
66     ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, &param);
67     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
68     currThreadPri = param.sched_priority;
69 
70     ret = pthread_attr_init(&a);
71     param.sched_priority = currThreadPri + 1;
72     pthread_attr_setschedparam(&a, &param);
73 
74     while (count < TEST_THREAD_COUNT) {
75         g_count[count] = count;
76         ret = pthread_create(&newThread, &a, ThreadFunc2, &g_count[count]);
77         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78         count++;
79     }
80 
81     ret = pthread_join(newThread, nullptr);
82     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
83 
84     return 0;
85 }
86 
TestCase(void)87 __attribute__((optnone)) static int TestCase(void)
88 {
89     int *test = nullptr;
90     int count = 5;
91     int status = 0;
92     int ret;
93 
94     g_testCondFlag = 0;
95 
96     pid_t pid = fork();
97     ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, INVALID_PROCESS_ID, pid);
98     if (pid == 0) {
99         (void)TestThread();
100         while (1) {
101         }
102     }
103 
104     ret = waitpid(pid, &status, 0);
105     ICUNIT_ASSERT_EQUAL(ret, pid, ret);
106     ret = WIFEXITED(status);
107     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
108     ret = WIFSIGNALED(status);
109     ICUNIT_ASSERT_EQUAL(ret, 1, ret);
110     ret = WTERMSIG(status);
111     ICUNIT_ASSERT_EQUAL(ret, SIGUSR2, ret);
112     return 0;
113 }
114 
ItTestExc003(void)115 void ItTestExc003(void)
116 {
117     TEST_ADD_CASE("IT_TEST_EXC_003", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
118 }
119