t_compat_cancel.c revision 1.2 1 1.2 riastrad /* $NetBSD: t_compat_cancel.c,v 1.2 2025/04/11 02:07:17 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #define __LIBC12_SOURCE__ /* expose compat declarations */
30 1.1 riastrad
31 1.1 riastrad #include <sys/cdefs.h>
32 1.2 riastrad __RCSID("$NetBSD: t_compat_cancel.c,v 1.2 2025/04/11 02:07:17 riastradh Exp $");
33 1.1 riastrad
34 1.1 riastrad #include <sys/event.h>
35 1.1 riastrad #include <sys/mman.h>
36 1.1 riastrad
37 1.1 riastrad #include <aio.h>
38 1.1 riastrad #include <atf-c.h>
39 1.1 riastrad #include <mqueue.h>
40 1.1 riastrad #include <pthread.h>
41 1.1 riastrad #include <signal.h>
42 1.1 riastrad
43 1.1 riastrad #include <compat/sys/event.h>
44 1.1 riastrad #include <compat/sys/mman.h>
45 1.1 riastrad #include <compat/sys/poll.h>
46 1.1 riastrad #include <compat/sys/select.h>
47 1.1 riastrad
48 1.1 riastrad #include <compat/include/aio.h>
49 1.1 riastrad #include <compat/include/mqueue.h>
50 1.1 riastrad #include <compat/include/signal.h>
51 1.1 riastrad #include <compat/include/time.h>
52 1.1 riastrad
53 1.1 riastrad #include "cancelpoint.h"
54 1.1 riastrad #include "h_macros.h"
55 1.1 riastrad
56 1.1 riastrad pthread_barrier_t bar;
57 1.1 riastrad bool cleanup_done;
58 1.1 riastrad
59 1.1 riastrad static void
60 1.1 riastrad cancelpoint_compat100_kevent(void)
61 1.1 riastrad {
62 1.1 riastrad int kq;
63 1.1 riastrad struct kevent100 ev;
64 1.1 riastrad
65 1.1 riastrad memset(&ev, 0, sizeof(ev));
66 1.1 riastrad ev.ident = SIGUSR1;
67 1.1 riastrad ev.filter = EVFILT_SIGNAL;
68 1.1 riastrad ev.flags = EV_ADD|EV_ENABLE;
69 1.1 riastrad ev.fflags = 0;
70 1.1 riastrad ev.data = 0;
71 1.1 riastrad ev.udata = 0;
72 1.1 riastrad
73 1.1 riastrad RL(kq = kqueue());
74 1.1 riastrad RL(__kevent50(kq, &ev, 1, NULL, 1, &(const struct timespec){0,0}));
75 1.1 riastrad cancelpointready();
76 1.1 riastrad RL(__kevent50(kq, NULL, 0, &ev, 1, NULL));
77 1.1 riastrad }
78 1.1 riastrad
79 1.1 riastrad static void
80 1.2 riastrad cancelpoint_compat12_msync(void)
81 1.1 riastrad {
82 1.1 riastrad const unsigned long pagesize = sysconf(_SC_PAGESIZE);
83 1.1 riastrad int fd;
84 1.1 riastrad void *map;
85 1.1 riastrad
86 1.1 riastrad RL(fd = open("file", O_RDWR|O_CREAT, 0666));
87 1.1 riastrad RL(ftruncate(fd, pagesize));
88 1.1 riastrad REQUIRE_LIBC(map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
89 1.1 riastrad MAP_SHARED, fd, 0),
90 1.1 riastrad MAP_FAILED);
91 1.1 riastrad cancelpointready();
92 1.1 riastrad RL(msync(map, pagesize));
93 1.1 riastrad }
94 1.1 riastrad
95 1.1 riastrad static void
96 1.1 riastrad cancelpoint_compat50___sigtimedwait(void)
97 1.1 riastrad {
98 1.1 riastrad sigset_t mask, omask;
99 1.1 riastrad siginfo_t info;
100 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
101 1.1 riastrad
102 1.1 riastrad RL(__sigfillset14(&mask));
103 1.1 riastrad RL(__sigprocmask14(SIG_BLOCK, &mask, &omask));
104 1.1 riastrad cancelpointready();
105 1.1 riastrad RL(__sigtimedwait(&omask, &info, &t));
106 1.1 riastrad }
107 1.1 riastrad
108 1.1 riastrad static void
109 1.1 riastrad cancelpoint_compat50_aio_suspend(void)
110 1.1 riastrad {
111 1.1 riastrad int fd[2];
112 1.1 riastrad char buf[32];
113 1.1 riastrad struct aiocb aio = {
114 1.1 riastrad .aio_offset = 0,
115 1.1 riastrad .aio_buf = buf,
116 1.1 riastrad .aio_nbytes = sizeof(buf),
117 1.1 riastrad .aio_fildes = -1,
118 1.1 riastrad };
119 1.1 riastrad const struct aiocb *const aiolist[] = { &aio };
120 1.1 riastrad
121 1.1 riastrad RL(pipe(fd));
122 1.1 riastrad aio.aio_fildes = fd[0];
123 1.1 riastrad RL(aio_read(&aio));
124 1.1 riastrad cancelpointready();
125 1.1 riastrad RL(aio_suspend(aiolist, __arraycount(aiolist), NULL));
126 1.1 riastrad }
127 1.1 riastrad
128 1.1 riastrad static void
129 1.1 riastrad cancelpoint_compat50_kevent(void)
130 1.1 riastrad {
131 1.1 riastrad int kq;
132 1.1 riastrad struct kevent100 ev;
133 1.1 riastrad
134 1.1 riastrad memset(&ev, 0, sizeof(ev));
135 1.1 riastrad ev.ident = SIGUSR1;
136 1.1 riastrad ev.filter = EVFILT_SIGNAL;
137 1.1 riastrad ev.flags = EV_ADD|EV_ENABLE;
138 1.1 riastrad ev.fflags = 0;
139 1.1 riastrad ev.data = 0;
140 1.1 riastrad ev.udata = 0;
141 1.1 riastrad
142 1.1 riastrad RL(kq = kqueue());
143 1.1 riastrad RL(kevent(kq, &ev, 1, NULL, 1, &(const struct timespec50){0,0}));
144 1.1 riastrad cancelpointready();
145 1.1 riastrad RL(kevent(kq, NULL, 0, &ev, 1, NULL));
146 1.1 riastrad }
147 1.1 riastrad
148 1.1 riastrad static void
149 1.1 riastrad cancelpoint_compat50_mq_timedreceive(void)
150 1.1 riastrad {
151 1.1 riastrad mqd_t mq;
152 1.1 riastrad char buf[32];
153 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
154 1.1 riastrad
155 1.1 riastrad RL(mq = mq_open("mq", O_RDWR|O_CREAT, 0666, NULL));
156 1.1 riastrad cancelpointready();
157 1.1 riastrad RL(mq_timedreceive(mq, buf, sizeof(buf), NULL, &t));
158 1.1 riastrad }
159 1.1 riastrad
160 1.1 riastrad static void
161 1.1 riastrad cancelpoint_compat50_mq_timedsend(void)
162 1.1 riastrad {
163 1.1 riastrad mqd_t mq;
164 1.1 riastrad char buf[32] = {0};
165 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
166 1.1 riastrad
167 1.1 riastrad RL(mq = mq_open("mq", O_RDWR|O_CREAT, 0666, NULL));
168 1.1 riastrad cancelpointready();
169 1.1 riastrad RL(mq_timedsend(mq, buf, sizeof(buf), 0, &t));
170 1.1 riastrad }
171 1.1 riastrad
172 1.1 riastrad static void
173 1.1 riastrad cancelpoint_compat50_nanosleep(void)
174 1.1 riastrad {
175 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
176 1.1 riastrad
177 1.1 riastrad cancelpointready();
178 1.1 riastrad RL(nanosleep(&t, NULL));
179 1.1 riastrad }
180 1.1 riastrad
181 1.1 riastrad static void
182 1.1 riastrad cancelpoint_compat50_pollts(void)
183 1.1 riastrad {
184 1.1 riastrad int fd[2];
185 1.1 riastrad struct pollfd pfd;
186 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
187 1.1 riastrad
188 1.1 riastrad RL(pipe(fd));
189 1.1 riastrad pfd.fd = fd[0];
190 1.1 riastrad pfd.events = POLLIN;
191 1.1 riastrad cancelpointready();
192 1.1 riastrad RL(pollts(&pfd, 1, &t, NULL));
193 1.1 riastrad }
194 1.1 riastrad
195 1.1 riastrad static void
196 1.1 riastrad cancelpoint_compat50_pselect(void)
197 1.1 riastrad {
198 1.1 riastrad int fd[2];
199 1.1 riastrad fd_set readfd;
200 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
201 1.1 riastrad
202 1.1 riastrad FD_ZERO(&readfd);
203 1.1 riastrad
204 1.1 riastrad RL(pipe(fd));
205 1.1 riastrad FD_SET(fd[0], &readfd);
206 1.1 riastrad cancelpointready();
207 1.1 riastrad RL(pselect(fd[0] + 1, &readfd, NULL, NULL, &t, NULL));
208 1.1 riastrad }
209 1.1 riastrad
210 1.1 riastrad static void
211 1.1 riastrad cancelpoint_compat50_select(void)
212 1.1 riastrad {
213 1.1 riastrad int fd[2];
214 1.1 riastrad fd_set readfd;
215 1.1 riastrad struct timeval50 t = {.tv_sec = 1, .tv_usec = 0};
216 1.1 riastrad
217 1.1 riastrad FD_ZERO(&readfd);
218 1.1 riastrad
219 1.1 riastrad RL(pipe(fd));
220 1.1 riastrad FD_SET(fd[0], &readfd);
221 1.1 riastrad cancelpointready();
222 1.1 riastrad RL(select(fd[0] + 1, &readfd, NULL, NULL, &t));
223 1.1 riastrad }
224 1.1 riastrad
225 1.1 riastrad static void
226 1.2 riastrad cancelpoint_compat13_sigsuspend(void)
227 1.1 riastrad {
228 1.1 riastrad sigset13_t mask, omask;
229 1.1 riastrad
230 1.1 riastrad RL(sigfillset(&mask));
231 1.1 riastrad RL(sigprocmask(SIG_BLOCK, &mask, &omask));
232 1.1 riastrad cancelpointready();
233 1.1 riastrad RL(sigsuspend(&omask));
234 1.1 riastrad }
235 1.1 riastrad
236 1.1 riastrad static void
237 1.1 riastrad cancelpoint_compat50_sigtimedwait(void)
238 1.1 riastrad {
239 1.1 riastrad sigset_t mask, omask;
240 1.1 riastrad siginfo_t info;
241 1.1 riastrad struct timespec50 t = {.tv_sec = 2, .tv_nsec = 0};
242 1.1 riastrad
243 1.1 riastrad RL(__sigfillset14(&mask));
244 1.1 riastrad RL(__sigprocmask14(SIG_BLOCK, &mask, &omask));
245 1.1 riastrad cancelpointready();
246 1.1 riastrad RL(sigtimedwait(&omask, &info, &t));
247 1.1 riastrad }
248 1.1 riastrad
249 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat100_kevent, __nothing)
250 1.2 riastrad TEST_CANCELPOINT(cancelpoint_compat12_msync, __nothing)
251 1.2 riastrad TEST_CANCELPOINT(cancelpoint_compat13_sigsuspend,
252 1.1 riastrad atf_tc_expect_signal(SIGALRM, "PR lib/59240: POSIX.1-2024:"
253 1.1 riastrad " cancellation point audit"))
254 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50___sigtimedwait,
255 1.1 riastrad atf_tc_expect_signal(SIGALRM, "PR lib/59240: POSIX.1-2024:"
256 1.1 riastrad " cancellation point audit"))
257 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_aio_suspend, __nothing)
258 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_kevent, __nothing)
259 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_mq_timedreceive, __nothing)
260 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_mq_timedsend, __nothing)
261 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_nanosleep, __nothing)
262 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_pollts, __nothing)
263 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_pselect, __nothing)
264 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_select, __nothing)
265 1.1 riastrad TEST_CANCELPOINT(cancelpoint_compat50_sigtimedwait,
266 1.1 riastrad atf_tc_expect_signal(SIGALRM, "PR lib/59240: POSIX.1-2024:"
267 1.1 riastrad " cancellation point audit"))
268 1.1 riastrad
269 1.1 riastrad ATF_TP_ADD_TCS(tp)
270 1.1 riastrad {
271 1.1 riastrad
272 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat100_kevent);
273 1.2 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat12_msync);
274 1.2 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat13_sigsuspend);
275 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50___sigtimedwait);
276 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_aio_suspend);
277 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_kevent);
278 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_mq_timedreceive);
279 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_mq_timedsend);
280 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_nanosleep);
281 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_pollts);
282 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_pselect);
283 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_select);
284 1.1 riastrad ADD_TEST_CANCELPOINT(cancelpoint_compat50_sigtimedwait);
285 1.1 riastrad
286 1.1 riastrad return atf_no_error();
287 1.1 riastrad }
288