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 #include <errno.h>
16 #include <poll.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/wait.h>
21 #include "fortify_test.h"
22 #include "test.h"
23
24 #define PPOLL_TIMESPEC_NSEC (100)
25
26 /**
27 * @tc.name : poll_0010
28 * @tc.desc : test poll normal condition
29 * @tc.level : Level 0
30 */
poll_0010(void)31 static void poll_0010(void)
32 {
33 errno = 0;
34 TEST(poll(NULL, 0, 1) == 0);
35 TEST(errno == 0);
36 return;
37 }
38
39 /**
40 * @tc.name : poll_0020
41 * @tc.desc : test poll suppress compiler optimizations
42 * @tc.level : Level 2
43 */
poll_0020(void)44 static void poll_0020(void)
45 {
46 struct sigaction sigabrt = {
47 .sa_handler = SignalHandler,
48 };
49 sigaction(SIGABRT, &sigabrt, NULL);
50
51 nfds_t fd_count = atoi("2");
52 struct pollfd buf[1] = {{0, POLLIN, 0}};
53
54 int status;
55 int pid = fork();
56 switch (pid) {
57 case -1:
58 t_error("fork failed: %s\n", strerror(errno));
59 break;
60 case 0:
61 // Set timeout to 0 to prevent waiting for polling if hardening tests fail.
62 poll(buf, fd_count, 0);
63 exit(0);
64 default:
65 waitpid(pid, &status, WUNTRACED);
66 TEST(WIFEXITED(status) == 0);
67 TEST(WIFSTOPPED(status) == 1);
68 TEST(WSTOPSIG(status) == SIGSTOP);
69 kill(pid, SIGCONT);
70 break;
71 }
72
73 return;
74 }
75
76 #ifdef _GNU_SOURCE
77 /**
78 * @tc.name : ppoll_0010
79 * @tc.desc : test ppoll normal condition
80 * @tc.level : Level 0
81 */
ppoll_0010(void)82 static void ppoll_0010(void)
83 {
84 errno = 0;
85 struct timespec ts = { .tv_nsec = PPOLL_TIMESPEC_NSEC };
86 TEST(ppoll(NULL, 0, &ts, NULL) == 0);
87 TEST(errno == 0);
88 return;
89 }
90
91 /**
92 * @tc.name : ppoll_0020
93 * @tc.desc : test ppoll suppress compiler optimizations
94 * @tc.level : Level 2
95 */
ppoll_0020(void)96 static void ppoll_0020(void)
97 {
98 struct sigaction sigabrt = {
99 .sa_handler = SignalHandler,
100 };
101 sigaction(SIGABRT, &sigabrt, NULL);
102
103 nfds_t fd_count = atoi("2");
104 struct pollfd buf[1] = {{0, POLLIN, 0}};
105 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
106 struct timespec timeout;
107 timeout.tv_sec = timeout.tv_nsec = 0;
108
109 int status;
110 int pid = fork();
111 switch (pid) {
112 case -1:
113 t_error("fork failed: %s\n", strerror(errno));
114 break;
115 case 0:
116 ppoll(buf, fd_count, &timeout, NULL);
117 exit(0);
118 default:
119 waitpid(pid, &status, WUNTRACED);
120 TEST(WIFEXITED(status) == 0);
121 TEST(WIFSTOPPED(status) == 1);
122 TEST(WSTOPSIG(status) == SIGSTOP);
123 kill(pid, SIGCONT);
124 break;
125 }
126
127 return;
128 }
129 #endif
130
main(int argc,char * argv[])131 int main(int argc, char *argv[]) {
132 poll_0010();
133 poll_0020();
134 #ifdef _GNU_SOURCE
135 ppoll_0010();
136 ppoll_0020();
137 #endif
138
139 return t_status;
140 }