t_futex_ops.c revision 1.3 1 1.3 thorpej /* $NetBSD: t_futex_ops.c,v 1.3 2020/04/30 04:18:07 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2019, 2020 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1 thorpej */
28 1.1 thorpej
29 1.1 thorpej #include <sys/cdefs.h>
30 1.1 thorpej __COPYRIGHT("@(#) Copyright (c) 2019, 2020\
31 1.1 thorpej The NetBSD Foundation, inc. All rights reserved.");
32 1.3 thorpej __RCSID("$NetBSD: t_futex_ops.c,v 1.3 2020/04/30 04:18:07 thorpej Exp $");
33 1.1 thorpej
34 1.1 thorpej #include <sys/fcntl.h>
35 1.1 thorpej #include <sys/mman.h>
36 1.1 thorpej #include <sys/wait.h>
37 1.1 thorpej #include <atomic.h>
38 1.1 thorpej #include <errno.h>
39 1.1 thorpej #include <lwp.h>
40 1.1 thorpej #include <stdlib.h>
41 1.1 thorpej #include <stdio.h>
42 1.3 thorpej #include <signal.h>
43 1.1 thorpej #include <time.h>
44 1.1 thorpej #include <limits.h>
45 1.1 thorpej #include <unistd.h>
46 1.1 thorpej
47 1.1 thorpej #include <atf-c.h>
48 1.1 thorpej
49 1.1 thorpej #include <libc/include/futex_private.h>
50 1.1 thorpej
51 1.1 thorpej #define LOAD(x) (*(volatile int *)(x))
52 1.1 thorpej #define STORE(x, y) *(volatile int *)(x) = (y)
53 1.1 thorpej
54 1.1 thorpej #if 0
55 1.1 thorpej #define DPRINTF(x) printf x
56 1.1 thorpej #else
57 1.1 thorpej #define DPRINTF(x) __nothing
58 1.1 thorpej #endif
59 1.1 thorpej
60 1.1 thorpej #define STACK_SIZE 65536
61 1.1 thorpej
62 1.1 thorpej static volatile int futex_word;
63 1.1 thorpej static volatile int futex_word1;
64 1.1 thorpej
65 1.1 thorpej static volatile unsigned int nlwps_running;
66 1.1 thorpej
67 1.1 thorpej struct lwp_data {
68 1.1 thorpej ucontext_t context;
69 1.1 thorpej void (*func)(void *);
70 1.1 thorpej void *stack_base;
71 1.1 thorpej lwpid_t lwpid;
72 1.1 thorpej pid_t child;
73 1.1 thorpej lwpid_t threadid;
74 1.1 thorpej int wait_op;
75 1.1 thorpej int op_flags;
76 1.1 thorpej int bitset;
77 1.1 thorpej volatile int *futex_ptr;
78 1.1 thorpej volatile int *error_ptr;
79 1.1 thorpej int block_val;
80 1.1 thorpej
81 1.1 thorpej void (*exit_func)(void);
82 1.1 thorpej
83 1.1 thorpej int futex_error;
84 1.1 thorpej };
85 1.1 thorpej
86 1.1 thorpej #define WAITER_LWP0 0
87 1.1 thorpej #define WAITER_LWP1 1
88 1.1 thorpej #define WAITER_LWP2 2
89 1.1 thorpej #define WAITER_LWP3 3
90 1.1 thorpej #define WAITER_LWP4 4
91 1.1 thorpej #define WAITER_LWP5 5
92 1.1 thorpej #define NLWPS 6
93 1.1 thorpej
94 1.1 thorpej struct lwp_data lwp_data[NLWPS];
95 1.1 thorpej
96 1.1 thorpej static const char *bs_path = "t_futex_ops_backing_store";
97 1.1 thorpej static int bs_fd = -1;
98 1.1 thorpej static int *bs_addr = MAP_FAILED;
99 1.1 thorpej static void *bs_source_buffer = NULL;
100 1.1 thorpej static void *bs_verify_buffer = NULL;
101 1.1 thorpej static long bs_pagesize;
102 1.1 thorpej
103 1.1 thorpej static void
104 1.1 thorpej create_lwp_waiter(struct lwp_data *d)
105 1.1 thorpej {
106 1.1 thorpej ATF_REQUIRE(_lwp_create(&d->context, 0, &d->lwpid) == 0);
107 1.1 thorpej }
108 1.1 thorpej
109 1.1 thorpej static void
110 1.1 thorpej exit_lwp_waiter(void)
111 1.1 thorpej {
112 1.1 thorpej _lwp_exit();
113 1.1 thorpej }
114 1.1 thorpej
115 1.1 thorpej static void
116 1.1 thorpej reap_lwp_waiter(struct lwp_data *d)
117 1.1 thorpej {
118 1.1 thorpej ATF_REQUIRE(_lwp_wait(d->lwpid, NULL) == 0);
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej static void
122 1.1 thorpej create_proc_waiter(struct lwp_data *d)
123 1.1 thorpej {
124 1.1 thorpej pid_t pid;
125 1.1 thorpej
126 1.1 thorpej ATF_REQUIRE((pid = fork()) != -1);
127 1.1 thorpej if (pid == 0) {
128 1.1 thorpej (*d->func)(d);
129 1.1 thorpej _exit(666); /* backstop */
130 1.1 thorpej } else
131 1.1 thorpej d->child = pid;
132 1.1 thorpej }
133 1.1 thorpej
134 1.1 thorpej static void
135 1.1 thorpej exit_proc_waiter(void)
136 1.1 thorpej {
137 1.1 thorpej _exit(0);
138 1.1 thorpej }
139 1.1 thorpej
140 1.1 thorpej static void
141 1.1 thorpej reap_proc_waiter(struct lwp_data *d)
142 1.1 thorpej {
143 1.1 thorpej int status;
144 1.1 thorpej
145 1.1 thorpej ATF_REQUIRE(waitpid(d->child, &status, 0) == d->child);
146 1.1 thorpej ATF_REQUIRE(WIFEXITED(status));
147 1.1 thorpej ATF_REQUIRE(WEXITSTATUS(status) == 0);
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej static void
151 1.1 thorpej setup_lwp_context(struct lwp_data *d, void (*func)(void *))
152 1.1 thorpej {
153 1.1 thorpej
154 1.1 thorpej memset(d, 0, sizeof(*d));
155 1.1 thorpej d->stack_base = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
156 1.1 thorpej MAP_ANON | MAP_STACK | MAP_PRIVATE, -1, 0);
157 1.1 thorpej ATF_REQUIRE(d->stack_base != MAP_FAILED);
158 1.1 thorpej _lwp_makecontext(&d->context, func, d, NULL, d->stack_base, STACK_SIZE);
159 1.1 thorpej d->threadid = 0;
160 1.1 thorpej d->func = func;
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej static void
164 1.1 thorpej simple_test_waiter_lwp(void *arg)
165 1.1 thorpej {
166 1.1 thorpej struct lwp_data *d = arg;
167 1.1 thorpej
168 1.1 thorpej d->threadid = _lwp_self();
169 1.1 thorpej
170 1.3 thorpej membar_producer();
171 1.1 thorpej atomic_inc_uint(&nlwps_running);
172 1.1 thorpej membar_sync();
173 1.1 thorpej
174 1.1 thorpej if (__futex(d->futex_ptr, d->wait_op | d->op_flags,
175 1.1 thorpej d->block_val, NULL, NULL, 0, d->bitset) == -1) {
176 1.1 thorpej d->futex_error = errno;
177 1.3 thorpej membar_sync();
178 1.3 thorpej atomic_dec_uint(&nlwps_running);
179 1.1 thorpej _lwp_exit();
180 1.1 thorpej } else {
181 1.1 thorpej d->futex_error = 0;
182 1.1 thorpej }
183 1.1 thorpej
184 1.1 thorpej membar_sync();
185 1.1 thorpej atomic_dec_uint(&nlwps_running);
186 1.1 thorpej
187 1.1 thorpej _lwp_exit();
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej static bool
191 1.1 thorpej verify_zero_bs(void)
192 1.1 thorpej {
193 1.1 thorpej
194 1.1 thorpej if (bs_verify_buffer == NULL) {
195 1.1 thorpej bs_verify_buffer = malloc(bs_pagesize);
196 1.1 thorpej ATF_REQUIRE(bs_verify_buffer != NULL);
197 1.1 thorpej }
198 1.1 thorpej
199 1.1 thorpej ATF_REQUIRE(pread(bs_fd, bs_verify_buffer,
200 1.1 thorpej bs_pagesize, 0) == bs_pagesize);
201 1.1 thorpej
202 1.1 thorpej return (memcmp(bs_verify_buffer, bs_source_buffer, bs_pagesize) == 0);
203 1.1 thorpej }
204 1.1 thorpej
205 1.1 thorpej static void
206 1.1 thorpej create_bs(int map_flags)
207 1.1 thorpej {
208 1.1 thorpej
209 1.1 thorpej bs_pagesize = sysconf(_SC_PAGESIZE);
210 1.1 thorpej ATF_REQUIRE(bs_pagesize > 0);
211 1.1 thorpej
212 1.1 thorpej if ((map_flags & (MAP_FILE | MAP_ANON)) == MAP_FILE) {
213 1.1 thorpej bs_source_buffer = calloc(1, bs_pagesize);
214 1.1 thorpej ATF_REQUIRE(bs_source_buffer != NULL);
215 1.1 thorpej
216 1.1 thorpej bs_fd = open(bs_path, O_RDWR | O_CREAT | O_EXCL, 0644);
217 1.1 thorpej ATF_REQUIRE(bs_fd != -1);
218 1.1 thorpej
219 1.1 thorpej ATF_REQUIRE(pwrite(bs_fd, bs_source_buffer,
220 1.1 thorpej bs_pagesize, 0) == bs_pagesize);
221 1.1 thorpej ATF_REQUIRE(verify_zero_bs());
222 1.1 thorpej }
223 1.1 thorpej
224 1.1 thorpej bs_addr = mmap(NULL, bs_pagesize, PROT_READ | PROT_WRITE,
225 1.1 thorpej map_flags | MAP_HASSEMAPHORE, bs_fd, 0);
226 1.1 thorpej ATF_REQUIRE(bs_addr != MAP_FAILED);
227 1.1 thorpej }
228 1.1 thorpej
229 1.1 thorpej static void
230 1.1 thorpej cleanup_bs(void)
231 1.1 thorpej {
232 1.1 thorpej
233 1.1 thorpej if (bs_fd != -1) {
234 1.1 thorpej (void) close(bs_fd);
235 1.1 thorpej bs_fd = -1;
236 1.1 thorpej (void) unlink(bs_path);
237 1.1 thorpej }
238 1.1 thorpej if (bs_source_buffer != NULL) {
239 1.1 thorpej free(bs_source_buffer);
240 1.1 thorpej bs_source_buffer = NULL;
241 1.1 thorpej }
242 1.1 thorpej if (bs_verify_buffer != NULL) {
243 1.1 thorpej free(bs_verify_buffer);
244 1.1 thorpej bs_verify_buffer = NULL;
245 1.1 thorpej }
246 1.1 thorpej if (bs_addr != MAP_FAILED) {
247 1.1 thorpej munmap(bs_addr, bs_pagesize);
248 1.1 thorpej bs_addr = MAP_FAILED;
249 1.1 thorpej }
250 1.1 thorpej }
251 1.1 thorpej
252 1.1 thorpej static void
253 1.1 thorpej do_cleanup(void)
254 1.1 thorpej {
255 1.1 thorpej int i;
256 1.1 thorpej
257 1.1 thorpej for (i = 0; i < NLWPS; i++) {
258 1.1 thorpej struct lwp_data *d = &lwp_data[i];
259 1.1 thorpej if (d->stack_base != NULL && d->stack_base != MAP_FAILED) {
260 1.1 thorpej (void) munmap(d->stack_base, STACK_SIZE);
261 1.1 thorpej }
262 1.1 thorpej }
263 1.1 thorpej memset(lwp_data, 0, sizeof(lwp_data));
264 1.1 thorpej STORE(&futex_word, 0);
265 1.1 thorpej STORE(&futex_word1, 0);
266 1.1 thorpej nlwps_running = 0;
267 1.1 thorpej
268 1.1 thorpej cleanup_bs();
269 1.1 thorpej }
270 1.1 thorpej
271 1.1 thorpej /*****************************************************************************/
272 1.1 thorpej
273 1.1 thorpej static void
274 1.1 thorpej wait_wake_test_waiter_lwp(void *arg)
275 1.1 thorpej {
276 1.1 thorpej struct lwp_data *d = arg;
277 1.1 thorpej
278 1.1 thorpej d->threadid = _lwp_self();
279 1.1 thorpej
280 1.1 thorpej STORE(d->futex_ptr, 1);
281 1.1 thorpej membar_sync();
282 1.1 thorpej
283 1.1 thorpej /* This will block because *futex_ptr == 1. */
284 1.1 thorpej if (__futex(d->futex_ptr, FUTEX_WAIT | d->op_flags,
285 1.1 thorpej 1, NULL, NULL, 0, 0) == -1) {
286 1.1 thorpej STORE(d->error_ptr, errno);
287 1.1 thorpej (*d->exit_func)();
288 1.1 thorpej } else {
289 1.1 thorpej STORE(d->error_ptr, 0);
290 1.1 thorpej }
291 1.1 thorpej
292 1.1 thorpej do {
293 1.1 thorpej membar_sync();
294 1.1 thorpej sleep(1);
295 1.1 thorpej } while (LOAD(d->futex_ptr) != 0);
296 1.1 thorpej
297 1.1 thorpej STORE(d->futex_ptr, 2);
298 1.1 thorpej membar_sync();
299 1.1 thorpej
300 1.1 thorpej do {
301 1.1 thorpej membar_sync();
302 1.1 thorpej sleep(1);
303 1.1 thorpej } while (LOAD(d->futex_ptr) != 3);
304 1.1 thorpej
305 1.1 thorpej /* This will not block because futex_word != 666. */
306 1.1 thorpej if (__futex(d->futex_ptr, FUTEX_WAIT | d->op_flags,
307 1.1 thorpej 666, NULL, NULL, 0, 0) == -1) {
308 1.1 thorpej /* This SHOULD be EAGAIN. */
309 1.1 thorpej STORE(d->error_ptr, errno);
310 1.1 thorpej }
311 1.1 thorpej
312 1.1 thorpej STORE(d->futex_ptr, 4);
313 1.1 thorpej membar_sync();
314 1.1 thorpej
315 1.1 thorpej (*d->exit_func)();
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej static void
319 1.1 thorpej do_futex_wait_wake_test(volatile int *futex_ptr, volatile int *error_ptr,
320 1.1 thorpej void (*create_func)(struct lwp_data *),
321 1.1 thorpej void (*exit_func)(void),
322 1.1 thorpej void (*reap_func)(struct lwp_data *),
323 1.1 thorpej int flags)
324 1.1 thorpej {
325 1.1 thorpej struct lwp_data *wlwp = &lwp_data[WAITER_LWP0];
326 1.1 thorpej int tries;
327 1.1 thorpej
328 1.1 thorpej if (error_ptr == NULL)
329 1.1 thorpej error_ptr = &wlwp->futex_error;
330 1.1 thorpej
331 1.1 thorpej if (create_func == NULL)
332 1.1 thorpej create_func = create_lwp_waiter;
333 1.1 thorpej if (exit_func == NULL)
334 1.1 thorpej exit_func = exit_lwp_waiter;
335 1.1 thorpej if (reap_func == NULL)
336 1.1 thorpej reap_func = reap_lwp_waiter;
337 1.1 thorpej
338 1.1 thorpej setup_lwp_context(wlwp, wait_wake_test_waiter_lwp);
339 1.1 thorpej
340 1.1 thorpej DPRINTF(("futex_basic_wait_wake: testing with flags 0x%x\n", flags));
341 1.1 thorpej wlwp->op_flags = flags;
342 1.1 thorpej wlwp->error_ptr = error_ptr;
343 1.1 thorpej STORE(error_ptr, -1);
344 1.1 thorpej wlwp->futex_ptr = futex_ptr;
345 1.1 thorpej STORE(futex_ptr, 0);
346 1.1 thorpej wlwp->exit_func = exit_func;
347 1.1 thorpej membar_sync();
348 1.1 thorpej
349 1.1 thorpej DPRINTF(("futex_basic_wait_wake: creating watier LWP\n"));
350 1.1 thorpej (*create_func)(wlwp);
351 1.1 thorpej
352 1.1 thorpej DPRINTF(("futex_basic_wait_wake: waiting for LWP %d to enter futex\n",
353 1.1 thorpej wlwp->lwpid));
354 1.1 thorpej for (tries = 0; tries < 5; tries++) {
355 1.1 thorpej membar_sync();
356 1.1 thorpej if (LOAD(futex_ptr) == 1)
357 1.1 thorpej break;
358 1.1 thorpej sleep(1);
359 1.1 thorpej }
360 1.1 thorpej membar_sync();
361 1.1 thorpej ATF_REQUIRE(LOAD(futex_ptr) == 1);
362 1.1 thorpej
363 1.1 thorpej /*
364 1.1 thorpej * If the LWP is blocked in the futex, it will not have yet
365 1.1 thorpej * modified *error_ptr.
366 1.1 thorpej */
367 1.1 thorpej DPRINTF(("futex_basic_wait_wake: checking for successful wait (%d)\n",
368 1.1 thorpej LOAD(error_ptr)));
369 1.1 thorpej for (tries = 0; tries < 5; tries++) {
370 1.1 thorpej membar_sync();
371 1.1 thorpej if (LOAD(error_ptr) == -1)
372 1.1 thorpej break;
373 1.1 thorpej sleep(1);
374 1.1 thorpej }
375 1.1 thorpej membar_sync();
376 1.1 thorpej ATF_REQUIRE(LOAD(error_ptr) == -1);
377 1.1 thorpej
378 1.1 thorpej /* Make sure invalid #wakes in rejected. */
379 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
380 1.1 thorpej __futex(futex_ptr, FUTEX_WAKE | flags,
381 1.1 thorpej -1, NULL, NULL, 0, 0) == -1);
382 1.1 thorpej
383 1.1 thorpej DPRINTF(("futex_basic_wait_wake: waking 1 waiter\n"));
384 1.1 thorpej ATF_REQUIRE(__futex(futex_ptr, FUTEX_WAKE | flags,
385 1.1 thorpej 1, NULL, NULL, 0, 0) == 1);
386 1.1 thorpej
387 1.1 thorpej DPRINTF(("futex_basic_wait_wake: checking for successful wake (%d)\n",
388 1.1 thorpej LOAD(error_ptr)));
389 1.1 thorpej for (tries = 0; tries < 5; tries++) {
390 1.1 thorpej membar_sync();
391 1.1 thorpej if (LOAD(error_ptr) == 0)
392 1.1 thorpej break;
393 1.1 thorpej sleep(1);
394 1.1 thorpej }
395 1.1 thorpej membar_sync();
396 1.1 thorpej ATF_REQUIRE(LOAD(error_ptr) == 0);
397 1.1 thorpej
398 1.1 thorpej STORE(futex_ptr, 0);
399 1.1 thorpej membar_sync();
400 1.1 thorpej
401 1.1 thorpej DPRINTF(("futex_basic_wait_wake: waiting for LWP to advance (2)\n"));
402 1.1 thorpej for (tries = 0; tries < 5; tries++) {
403 1.1 thorpej membar_sync();
404 1.1 thorpej if (LOAD(futex_ptr) == 2)
405 1.1 thorpej break;
406 1.1 thorpej sleep(1);
407 1.1 thorpej }
408 1.1 thorpej membar_sync();
409 1.1 thorpej ATF_REQUIRE(LOAD(futex_ptr) == 2);
410 1.1 thorpej
411 1.1 thorpej STORE(futex_ptr, 3);
412 1.1 thorpej membar_sync();
413 1.1 thorpej
414 1.1 thorpej DPRINTF(("futex_basic_wait_wake: waiting for LWP to advance (4)\n"));
415 1.1 thorpej for (tries = 0; tries < 5; tries++) {
416 1.1 thorpej membar_sync();
417 1.1 thorpej if (LOAD(futex_ptr) == 4)
418 1.1 thorpej break;
419 1.1 thorpej sleep(1);
420 1.1 thorpej }
421 1.1 thorpej membar_sync();
422 1.1 thorpej ATF_REQUIRE(LOAD(futex_ptr) == 4);
423 1.1 thorpej
424 1.1 thorpej DPRINTF(("futex_basic_wait_wake: checking for expected EGAIN\n"));
425 1.1 thorpej ATF_REQUIRE(LOAD(error_ptr) == EAGAIN);
426 1.1 thorpej
427 1.1 thorpej DPRINTF(("futex_basic_wait_wake: reaping LWP %d\n", wlwp->lwpid));
428 1.1 thorpej (*reap_func)(wlwp);
429 1.1 thorpej }
430 1.1 thorpej
431 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_basic_wait_wake_private);
432 1.1 thorpej ATF_TC_HEAD(futex_basic_wait_wake_private, tc)
433 1.1 thorpej {
434 1.1 thorpej atf_tc_set_md_var(tc, "descr",
435 1.1 thorpej "tests basic futex WAIT + WAKE operations (PRIVATE)");
436 1.1 thorpej }
437 1.1 thorpej ATF_TC_BODY(futex_basic_wait_wake_private, tc)
438 1.1 thorpej {
439 1.1 thorpej do_futex_wait_wake_test(&futex_word, NULL,
440 1.1 thorpej NULL, NULL, NULL,
441 1.1 thorpej FUTEX_PRIVATE_FLAG);
442 1.1 thorpej }
443 1.1 thorpej ATF_TC_CLEANUP(futex_basic_wait_wake_private, tc)
444 1.1 thorpej {
445 1.1 thorpej do_cleanup();
446 1.1 thorpej }
447 1.1 thorpej
448 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_basic_wait_wake_shared);
449 1.1 thorpej ATF_TC_HEAD(futex_basic_wait_wake_shared, tc)
450 1.1 thorpej {
451 1.1 thorpej atf_tc_set_md_var(tc, "descr",
452 1.1 thorpej "tests basic futex WAIT + WAKE operations (SHARED)");
453 1.1 thorpej }
454 1.1 thorpej ATF_TC_BODY(futex_basic_wait_wake_shared, tc)
455 1.1 thorpej {
456 1.1 thorpej do_futex_wait_wake_test(&futex_word, NULL,
457 1.1 thorpej NULL, NULL, NULL,
458 1.1 thorpej 0);
459 1.1 thorpej }
460 1.1 thorpej ATF_TC_CLEANUP(futex_basic_wait_wake_shared, tc)
461 1.1 thorpej {
462 1.1 thorpej do_cleanup();
463 1.1 thorpej }
464 1.1 thorpej
465 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_private);
466 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_private, tc)
467 1.1 thorpej {
468 1.1 thorpej atf_tc_set_md_var(tc, "descr",
469 1.1 thorpej "tests futex WAIT + WAKE operations (MAP_ANON + PRIVATE)");
470 1.1 thorpej }
471 1.1 thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_private, tc)
472 1.1 thorpej {
473 1.1 thorpej create_bs(MAP_ANON | MAP_PRIVATE);
474 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], NULL,
475 1.1 thorpej NULL, NULL, NULL,
476 1.1 thorpej FUTEX_PRIVATE_FLAG);
477 1.1 thorpej }
478 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_private, tc)
479 1.1 thorpej {
480 1.1 thorpej do_cleanup();
481 1.1 thorpej }
482 1.1 thorpej
483 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_shared);
484 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_shared, tc)
485 1.1 thorpej {
486 1.1 thorpej atf_tc_set_md_var(tc, "descr",
487 1.1 thorpej "tests futex WAIT + WAKE operations (MAP_ANON + SHARED)");
488 1.1 thorpej }
489 1.1 thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_shared, tc)
490 1.1 thorpej {
491 1.1 thorpej create_bs(MAP_ANON | MAP_PRIVATE);
492 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], NULL,
493 1.1 thorpej NULL, NULL, NULL,
494 1.1 thorpej 0);
495 1.1 thorpej }
496 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_shared, tc)
497 1.1 thorpej {
498 1.1 thorpej do_cleanup();
499 1.1 thorpej }
500 1.1 thorpej
501 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_private);
502 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_private, tc)
503 1.1 thorpej {
504 1.1 thorpej atf_tc_set_md_var(tc, "descr",
505 1.1 thorpej "tests futex WAIT + WAKE operations (MAP_FILE + PRIVATE)");
506 1.1 thorpej }
507 1.1 thorpej ATF_TC_BODY(futex_wait_wake_file_bs_private, tc)
508 1.1 thorpej {
509 1.1 thorpej /*
510 1.1 thorpej * This combination (non-COW mapped file + PRIVATE futex)
511 1.1 thorpej * doesn't really make sense, but we should make sure it
512 1.1 thorpej * works as expected.
513 1.1 thorpej */
514 1.1 thorpej create_bs(MAP_FILE | MAP_SHARED);
515 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], NULL,
516 1.1 thorpej NULL, NULL, NULL,
517 1.1 thorpej FUTEX_PRIVATE_FLAG);
518 1.1 thorpej ATF_REQUIRE(! verify_zero_bs());
519 1.1 thorpej }
520 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_private, tc)
521 1.1 thorpej {
522 1.1 thorpej do_cleanup();
523 1.1 thorpej }
524 1.1 thorpej
525 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_cow_private);
526 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_cow_private, tc)
527 1.1 thorpej {
528 1.1 thorpej atf_tc_set_md_var(tc, "descr",
529 1.1 thorpej "tests futex WAIT + WAKE operations (MAP_FILE COW + PRIVATE)");
530 1.1 thorpej }
531 1.1 thorpej ATF_TC_BODY(futex_wait_wake_file_bs_cow_private, tc)
532 1.1 thorpej {
533 1.1 thorpej create_bs(MAP_FILE | MAP_PRIVATE);
534 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], NULL,
535 1.1 thorpej NULL, NULL, NULL,
536 1.1 thorpej FUTEX_PRIVATE_FLAG);
537 1.1 thorpej ATF_REQUIRE(verify_zero_bs());
538 1.1 thorpej }
539 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_cow_private, tc)
540 1.1 thorpej {
541 1.1 thorpej do_cleanup();
542 1.1 thorpej }
543 1.1 thorpej
544 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_shared);
545 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_shared, tc)
546 1.1 thorpej {
547 1.1 thorpej atf_tc_set_md_var(tc, "descr",
548 1.1 thorpej "tests futex WAIT + WAKE operations (MAP_FILE + SHARED)");
549 1.1 thorpej }
550 1.1 thorpej ATF_TC_BODY(futex_wait_wake_file_bs_shared, tc)
551 1.1 thorpej {
552 1.1 thorpej create_bs(MAP_FILE | MAP_SHARED);
553 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], NULL,
554 1.1 thorpej NULL, NULL, NULL,
555 1.1 thorpej 0);
556 1.1 thorpej ATF_REQUIRE(! verify_zero_bs());
557 1.1 thorpej }
558 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_shared, tc)
559 1.1 thorpej {
560 1.1 thorpej do_cleanup();
561 1.1 thorpej }
562 1.1 thorpej
563 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_cow_shared);
564 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_cow_shared, tc)
565 1.1 thorpej {
566 1.1 thorpej atf_tc_set_md_var(tc, "descr",
567 1.1 thorpej "tests futex WAIT + WAKE operations (MAP_FILE COW + SHARED)");
568 1.1 thorpej }
569 1.1 thorpej ATF_TC_BODY(futex_wait_wake_file_bs_cow_shared, tc)
570 1.1 thorpej {
571 1.1 thorpej /*
572 1.1 thorpej * This combination (COW mapped file + SHARED futex)
573 1.1 thorpej * doesn't really make sense, but we should make sure it
574 1.1 thorpej * works as expected.
575 1.1 thorpej */
576 1.1 thorpej create_bs(MAP_FILE | MAP_PRIVATE);
577 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], NULL,
578 1.1 thorpej NULL, NULL, NULL,
579 1.1 thorpej 0);
580 1.1 thorpej ATF_REQUIRE(verify_zero_bs());
581 1.1 thorpej }
582 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_cow_shared, tc)
583 1.1 thorpej {
584 1.1 thorpej do_cleanup();
585 1.1 thorpej }
586 1.1 thorpej
587 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_shared_proc);
588 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_shared_proc, tc)
589 1.1 thorpej {
590 1.1 thorpej atf_tc_set_md_var(tc, "descr",
591 1.1 thorpej "tests multiproc futex WAIT + WAKE operations (MAP_ANON + SHARED)");
592 1.1 thorpej }
593 1.1 thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_shared_proc, tc)
594 1.1 thorpej {
595 1.1 thorpej create_bs(MAP_ANON | MAP_SHARED);
596 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], &bs_addr[1],
597 1.1 thorpej create_proc_waiter,
598 1.1 thorpej exit_proc_waiter,
599 1.1 thorpej reap_proc_waiter,
600 1.1 thorpej 0);
601 1.1 thorpej }
602 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_shared_proc, tc)
603 1.1 thorpej {
604 1.1 thorpej do_cleanup();
605 1.1 thorpej }
606 1.1 thorpej
607 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_shared_proc);
608 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_shared_proc, tc)
609 1.1 thorpej {
610 1.1 thorpej atf_tc_set_md_var(tc, "descr",
611 1.1 thorpej "tests multiproc futex WAIT + WAKE operations (MAP_ANON + SHARED)");
612 1.1 thorpej }
613 1.1 thorpej ATF_TC_BODY(futex_wait_wake_file_bs_shared_proc, tc)
614 1.1 thorpej {
615 1.1 thorpej create_bs(MAP_FILE | MAP_SHARED);
616 1.1 thorpej do_futex_wait_wake_test(&bs_addr[0], &bs_addr[1],
617 1.1 thorpej create_proc_waiter,
618 1.1 thorpej exit_proc_waiter,
619 1.1 thorpej reap_proc_waiter,
620 1.1 thorpej 0);
621 1.1 thorpej }
622 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_shared_proc, tc)
623 1.1 thorpej {
624 1.1 thorpej do_cleanup();
625 1.1 thorpej }
626 1.1 thorpej
627 1.1 thorpej /*****************************************************************************/
628 1.1 thorpej
629 1.1 thorpej ATF_TC(futex_wait_pointless_bitset);
630 1.1 thorpej ATF_TC_HEAD(futex_wait_pointless_bitset, tc)
631 1.1 thorpej {
632 1.1 thorpej atf_tc_set_md_var(tc, "descr",
633 1.1 thorpej "tests basic futex WAIT + WAKE operations (SHARED)");
634 1.1 thorpej }
635 1.1 thorpej ATF_TC_BODY(futex_wait_pointless_bitset, tc)
636 1.1 thorpej {
637 1.1 thorpej
638 1.1 thorpej futex_word = 1;
639 1.2 riastrad ATF_REQUIRE_ERRNO(EINVAL,
640 1.2 riastrad __futex(&futex_word, FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG,
641 1.2 riastrad 1, NULL, NULL, 0, 0) == -1);
642 1.1 thorpej }
643 1.1 thorpej
644 1.1 thorpej static void
645 1.1 thorpej do_futex_wait_wake_bitset_test(int flags)
646 1.1 thorpej {
647 1.1 thorpej struct lwp_data *wlwp0 = &lwp_data[WAITER_LWP0];
648 1.1 thorpej struct lwp_data *wlwp1 = &lwp_data[WAITER_LWP1];
649 1.1 thorpej int i, tries;
650 1.1 thorpej
651 1.1 thorpej for (i = WAITER_LWP0; i <= WAITER_LWP1; i++) {
652 1.1 thorpej setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
653 1.1 thorpej lwp_data[i].op_flags = flags;
654 1.1 thorpej lwp_data[i].futex_error = -1;
655 1.1 thorpej lwp_data[i].bitset = __BIT(i);
656 1.1 thorpej lwp_data[i].wait_op = FUTEX_WAIT_BITSET;
657 1.1 thorpej lwp_data[i].futex_ptr = &futex_word;
658 1.1 thorpej lwp_data[i].block_val = 1;
659 1.1 thorpej }
660 1.1 thorpej
661 1.1 thorpej STORE(&futex_word, 1);
662 1.1 thorpej membar_sync();
663 1.1 thorpej
664 1.1 thorpej ATF_REQUIRE(_lwp_create(&wlwp0->context, 0, &wlwp0->lwpid) == 0);
665 1.1 thorpej ATF_REQUIRE(_lwp_create(&wlwp1->context, 0, &wlwp1->lwpid) == 0);
666 1.1 thorpej
667 1.1 thorpej for (tries = 0; tries < 5; tries++) {
668 1.1 thorpej membar_sync();
669 1.1 thorpej if (nlwps_running == 2)
670 1.1 thorpej break;
671 1.1 thorpej sleep(1);
672 1.1 thorpej }
673 1.1 thorpej membar_sync();
674 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 2, "waiters failed to start");
675 1.1 thorpej
676 1.1 thorpej /* Ensure they're blocked. */
677 1.1 thorpej ATF_REQUIRE(wlwp0->futex_error == -1);
678 1.1 thorpej ATF_REQUIRE(wlwp1->futex_error == -1);
679 1.1 thorpej
680 1.1 thorpej /* Make sure invalid #wakes in rejected. */
681 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
682 1.1 thorpej __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
683 1.1 thorpej -1, NULL, NULL, 0, 0) == -1);
684 1.1 thorpej
685 1.1 thorpej /* This should result in no wakeups because no bits are set. */
686 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
687 1.1 thorpej INT_MAX, NULL, NULL, 0, 0) == 0);
688 1.1 thorpej
689 1.1 thorpej /* This should result in no wakeups because the wrongs bits are set. */
690 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
691 1.1 thorpej INT_MAX, NULL, NULL, 0,
692 1.1 thorpej ~(wlwp0->bitset | wlwp1->bitset)) == 0);
693 1.1 thorpej
694 1.1 thorpej /* Trust, but verify. */
695 1.1 thorpej sleep(1);
696 1.1 thorpej for (tries = 0; tries < 5; tries++) {
697 1.1 thorpej membar_sync();
698 1.1 thorpej if (nlwps_running == 2)
699 1.1 thorpej break;
700 1.1 thorpej sleep(1);
701 1.1 thorpej }
702 1.1 thorpej membar_sync();
703 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 2, "waiters exited unexpectedly");
704 1.1 thorpej
705 1.1 thorpej /* Wake up the first LWP. */
706 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
707 1.1 thorpej INT_MAX, NULL, NULL, 0,
708 1.1 thorpej wlwp0->bitset) == 1);
709 1.1 thorpej sleep(1);
710 1.1 thorpej for (tries = 0; tries < 5; tries++) {
711 1.1 thorpej membar_sync();
712 1.1 thorpej if (nlwps_running == 1)
713 1.1 thorpej break;
714 1.1 thorpej sleep(1);
715 1.1 thorpej }
716 1.1 thorpej membar_sync();
717 1.1 thorpej ATF_REQUIRE(nlwps_running == 1);
718 1.1 thorpej ATF_REQUIRE(wlwp0->futex_error == 0);
719 1.1 thorpej ATF_REQUIRE(_lwp_wait(wlwp0->lwpid, NULL) == 0);
720 1.1 thorpej
721 1.1 thorpej /* Wake up the second LWP. */
722 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
723 1.1 thorpej INT_MAX, NULL, NULL, 0,
724 1.1 thorpej wlwp1->bitset) == 1);
725 1.1 thorpej sleep(1);
726 1.1 thorpej for (tries = 0; tries < 5; tries++) {
727 1.1 thorpej membar_sync();
728 1.1 thorpej if (nlwps_running == 0)
729 1.1 thorpej break;
730 1.1 thorpej sleep(1);
731 1.1 thorpej }
732 1.1 thorpej membar_sync();
733 1.1 thorpej ATF_REQUIRE(nlwps_running == 0);
734 1.1 thorpej ATF_REQUIRE(wlwp1->futex_error == 0);
735 1.1 thorpej ATF_REQUIRE(_lwp_wait(wlwp1->lwpid, NULL) == 0);
736 1.1 thorpej }
737 1.1 thorpej
738 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_bitset);
739 1.1 thorpej ATF_TC_HEAD(futex_wait_wake_bitset, tc)
740 1.1 thorpej {
741 1.1 thorpej atf_tc_set_md_var(tc, "descr",
742 1.1 thorpej "tests futex WAIT_BITSET + WAKE_BITSET operations");
743 1.1 thorpej }
744 1.1 thorpej ATF_TC_BODY(futex_wait_wake_bitset, tc)
745 1.1 thorpej {
746 1.1 thorpej do_futex_wait_wake_bitset_test(FUTEX_PRIVATE_FLAG);
747 1.1 thorpej }
748 1.1 thorpej ATF_TC_CLEANUP(futex_wait_wake_bitset, tc)
749 1.1 thorpej {
750 1.1 thorpej do_cleanup();
751 1.1 thorpej }
752 1.1 thorpej
753 1.1 thorpej /*****************************************************************************/
754 1.1 thorpej
755 1.1 thorpej static void
756 1.1 thorpej do_futex_requeue_test(int flags, int op)
757 1.1 thorpej {
758 1.1 thorpej struct lwp_data *wlwp0 = &lwp_data[WAITER_LWP0];
759 1.1 thorpej struct lwp_data *wlwp1 = &lwp_data[WAITER_LWP1];
760 1.1 thorpej struct lwp_data *wlwp2 = &lwp_data[WAITER_LWP2];
761 1.1 thorpej struct lwp_data *wlwp3 = &lwp_data[WAITER_LWP3];
762 1.1 thorpej const int good_val3 = (op == FUTEX_CMP_REQUEUE) ? 1 : 0;
763 1.1 thorpej const int bad_val3 = (op == FUTEX_CMP_REQUEUE) ? 666 : 0;
764 1.1 thorpej int i, tries;
765 1.1 thorpej
766 1.1 thorpej for (i = WAITER_LWP0; i <= WAITER_LWP3; i++) {
767 1.1 thorpej setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
768 1.1 thorpej lwp_data[i].op_flags = flags;
769 1.1 thorpej lwp_data[i].futex_error = -1;
770 1.1 thorpej lwp_data[i].futex_ptr = &futex_word;
771 1.1 thorpej lwp_data[i].block_val = 1;
772 1.1 thorpej lwp_data[i].bitset = 0;
773 1.1 thorpej lwp_data[i].wait_op = FUTEX_WAIT;
774 1.1 thorpej }
775 1.1 thorpej
776 1.1 thorpej STORE(&futex_word, 1);
777 1.1 thorpej STORE(&futex_word1, 1);
778 1.1 thorpej membar_sync();
779 1.1 thorpej
780 1.1 thorpej ATF_REQUIRE(_lwp_create(&wlwp0->context, 0, &wlwp0->lwpid) == 0);
781 1.1 thorpej ATF_REQUIRE(_lwp_create(&wlwp1->context, 0, &wlwp1->lwpid) == 0);
782 1.1 thorpej ATF_REQUIRE(_lwp_create(&wlwp2->context, 0, &wlwp2->lwpid) == 0);
783 1.1 thorpej ATF_REQUIRE(_lwp_create(&wlwp3->context, 0, &wlwp3->lwpid) == 0);
784 1.1 thorpej
785 1.1 thorpej for (tries = 0; tries < 5; tries++) {
786 1.1 thorpej membar_sync();
787 1.1 thorpej if (nlwps_running == 4)
788 1.1 thorpej break;
789 1.1 thorpej sleep(1);
790 1.1 thorpej }
791 1.1 thorpej membar_sync();
792 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 4, "waiters failed to start");
793 1.1 thorpej
794 1.1 thorpej /* Ensure they're blocked. */
795 1.1 thorpej ATF_REQUIRE(wlwp0->futex_error == -1);
796 1.1 thorpej ATF_REQUIRE(wlwp1->futex_error == -1);
797 1.1 thorpej ATF_REQUIRE(wlwp2->futex_error == -1);
798 1.1 thorpej ATF_REQUIRE(wlwp3->futex_error == -1);
799 1.1 thorpej
800 1.1 thorpej /* Make sure invalid #wakes and #requeues are rejected. */
801 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
802 1.1 thorpej __futex(&futex_word, op | flags,
803 1.1 thorpej -1, NULL, &futex_word1, INT_MAX, bad_val3) == -1);
804 1.1 thorpej
805 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
806 1.1 thorpej __futex(&futex_word, op | flags,
807 1.1 thorpej 0, NULL, &futex_word1, -1, bad_val3) == -1);
808 1.1 thorpej
809 1.1 thorpej /*
810 1.1 thorpej * FUTEX 0: 4 LWPs
811 1.1 thorpej * FUTEX 1: 0 LWPs
812 1.1 thorpej */
813 1.1 thorpej
814 1.1 thorpej if (op == FUTEX_CMP_REQUEUE) {
815 1.1 thorpej /* This should fail because the futex_word value is 1. */
816 1.1 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
817 1.1 thorpej __futex(&futex_word, op | flags,
818 1.1 thorpej 0, NULL, &futex_word1, INT_MAX, bad_val3) == -1);
819 1.1 thorpej }
820 1.1 thorpej
821 1.1 thorpej /*
822 1.1 thorpej * FUTEX 0: 4 LWPs
823 1.1 thorpej * FUTEX 1: 0 LWPs
824 1.1 thorpej */
825 1.1 thorpej
826 1.1 thorpej /* Move all waiters from 0 to 1. */
827 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, op | flags,
828 1.1 thorpej 0, NULL, &futex_word1, INT_MAX, good_val3) == 0);
829 1.1 thorpej
830 1.1 thorpej /*
831 1.1 thorpej * FUTEX 0: 0 LWPs
832 1.1 thorpej * FUTEX 1: 4 LWPs
833 1.1 thorpej */
834 1.1 thorpej
835 1.1 thorpej if (op == FUTEX_CMP_REQUEUE) {
836 1.1 thorpej /* This should fail because the futex_word1 value is 1. */
837 1.1 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
838 1.1 thorpej __futex(&futex_word1, op | flags,
839 1.1 thorpej 1, NULL, &futex_word, 1, bad_val3) == -1);
840 1.1 thorpej }
841 1.1 thorpej
842 1.1 thorpej /*
843 1.1 thorpej * FUTEX 0: 0 LWPs
844 1.1 thorpej * FUTEX 1: 4 LWPs
845 1.1 thorpej */
846 1.1 thorpej
847 1.1 thorpej /* Wake one waiter on 1, move one waiter to 0. */
848 1.1 thorpej ATF_REQUIRE(__futex(&futex_word1, op | flags,
849 1.1 thorpej 1, NULL, &futex_word, 1, good_val3) == 1);
850 1.1 thorpej
851 1.1 thorpej /*
852 1.1 thorpej * FUTEX 0: 1 LWP
853 1.1 thorpej * FUTEX 1: 2 LWPs
854 1.1 thorpej */
855 1.1 thorpej
856 1.1 thorpej /* Wake all waiters on 0 (should be 1). */
857 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE | flags,
858 1.1 thorpej INT_MAX, NULL, NULL, 0, 0) == 1);
859 1.1 thorpej
860 1.1 thorpej /* Wake all waiters on 1 (should be 2). */
861 1.1 thorpej ATF_REQUIRE(__futex(&futex_word1, FUTEX_WAKE | flags,
862 1.1 thorpej INT_MAX, NULL, NULL, 0, 0) == 2);
863 1.1 thorpej
864 1.1 thorpej /* Trust, but verify. */
865 1.1 thorpej sleep(1);
866 1.1 thorpej for (tries = 0; tries < 5; tries++) {
867 1.1 thorpej membar_sync();
868 1.1 thorpej if (nlwps_running == 0)
869 1.1 thorpej break;
870 1.1 thorpej sleep(1);
871 1.1 thorpej }
872 1.1 thorpej membar_sync();
873 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "waiters failed to exit");
874 1.1 thorpej
875 1.1 thorpej ATF_REQUIRE(_lwp_wait(wlwp0->lwpid, NULL) == 0);
876 1.1 thorpej ATF_REQUIRE(_lwp_wait(wlwp1->lwpid, NULL) == 0);
877 1.1 thorpej ATF_REQUIRE(_lwp_wait(wlwp2->lwpid, NULL) == 0);
878 1.1 thorpej ATF_REQUIRE(_lwp_wait(wlwp3->lwpid, NULL) == 0);
879 1.1 thorpej }
880 1.1 thorpej
881 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_requeue);
882 1.1 thorpej ATF_TC_HEAD(futex_requeue, tc)
883 1.1 thorpej {
884 1.1 thorpej atf_tc_set_md_var(tc, "descr",
885 1.1 thorpej "tests futex REQUEUE operations");
886 1.1 thorpej }
887 1.1 thorpej ATF_TC_BODY(futex_requeue, tc)
888 1.1 thorpej {
889 1.1 thorpej do_futex_requeue_test(FUTEX_PRIVATE_FLAG, FUTEX_REQUEUE);
890 1.1 thorpej }
891 1.1 thorpej ATF_TC_CLEANUP(futex_requeue, tc)
892 1.1 thorpej {
893 1.1 thorpej do_cleanup();
894 1.1 thorpej }
895 1.1 thorpej
896 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_cmp_requeue);
897 1.1 thorpej ATF_TC_HEAD(futex_cmp_requeue, tc)
898 1.1 thorpej {
899 1.1 thorpej atf_tc_set_md_var(tc, "descr",
900 1.1 thorpej "tests futex CMP_REQUEUE operations");
901 1.1 thorpej }
902 1.1 thorpej ATF_TC_BODY(futex_cmp_requeue, tc)
903 1.1 thorpej {
904 1.1 thorpej do_futex_requeue_test(FUTEX_PRIVATE_FLAG, FUTEX_CMP_REQUEUE);
905 1.1 thorpej }
906 1.1 thorpej ATF_TC_CLEANUP(futex_cmp_requeue, tc)
907 1.1 thorpej {
908 1.1 thorpej do_cleanup();
909 1.1 thorpej }
910 1.1 thorpej
911 1.1 thorpej /*****************************************************************************/
912 1.1 thorpej
913 1.1 thorpej static void
914 1.1 thorpej do_futex_wake_op_op_test(int flags)
915 1.1 thorpej {
916 1.1 thorpej int op;
917 1.1 thorpej
918 1.1 thorpej futex_word = 0;
919 1.1 thorpej futex_word1 = 0;
920 1.1 thorpej
921 1.1 thorpej /*
922 1.1 thorpej * The op= operations should work even if there are no waiters.
923 1.1 thorpej */
924 1.1 thorpej
925 1.1 thorpej /*
926 1.1 thorpej * Because these operations use both futex addresses, exercise
927 1.1 thorpej * rejecting unaligned futex addresses here.
928 1.1 thorpej */
929 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
930 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
931 1.1 thorpej __futex((int *)1, FUTEX_WAKE_OP | flags,
932 1.1 thorpej 0, NULL, &futex_word1, 0, op) == -1);
933 1.1 thorpej ATF_REQUIRE(futex_word1 == 0);
934 1.1 thorpej
935 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
936 1.1 thorpej __futex(&futex_word, FUTEX_WAKE_OP | flags,
937 1.1 thorpej 0, NULL, (int *)1, 0, op) == -1);
938 1.1 thorpej ATF_REQUIRE(futex_word == 0);
939 1.1 thorpej
940 1.1 thorpej /* Check unmapped uaddr2 handling, too. */
941 1.1 thorpej ATF_REQUIRE_ERRNO(EFAULT,
942 1.1 thorpej __futex(&futex_word, FUTEX_WAKE_OP | flags,
943 1.1 thorpej 0, NULL, NULL, 0, op) == -1);
944 1.1 thorpej ATF_REQUIRE(futex_word == 0);
945 1.1 thorpej
946 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
947 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
948 1.1 thorpej 0, NULL, &futex_word1, 0, op) == 0);
949 1.1 thorpej ATF_REQUIRE(futex_word1 == 1);
950 1.1 thorpej
951 1.1 thorpej op = FUTEX_OP(FUTEX_OP_ADD, 1, FUTEX_OP_CMP_EQ, 0);
952 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
953 1.1 thorpej 0, NULL, &futex_word1, 0, op) == 0);
954 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
955 1.1 thorpej
956 1.1 thorpej op = FUTEX_OP(FUTEX_OP_OR, 2, FUTEX_OP_CMP_EQ, 0);
957 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
958 1.1 thorpej 0, NULL, &futex_word1, 0, op) == 0);
959 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
960 1.1 thorpej
961 1.1 thorpej /* This should fail because of invalid shift value 32. */
962 1.1 thorpej op = FUTEX_OP(FUTEX_OP_OR | FUTEX_OP_OPARG_SHIFT, 32,
963 1.1 thorpej FUTEX_OP_CMP_EQ, 0);
964 1.1 thorpej ATF_REQUIRE_ERRNO(EINVAL,
965 1.1 thorpej __futex(&futex_word, FUTEX_WAKE_OP | flags,
966 1.1 thorpej 0, NULL, &futex_word1, 0, op) == -1);
967 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
968 1.1 thorpej
969 1.1 thorpej op = FUTEX_OP(FUTEX_OP_OR | FUTEX_OP_OPARG_SHIFT, 31,
970 1.1 thorpej FUTEX_OP_CMP_EQ, 0);
971 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
972 1.1 thorpej 0, NULL, &futex_word1, 0, op) == 0);
973 1.1 thorpej ATF_REQUIRE(futex_word1 == (int)0x80000002);
974 1.1 thorpej
975 1.1 thorpej op = FUTEX_OP(FUTEX_OP_ANDN | FUTEX_OP_OPARG_SHIFT, 31,
976 1.1 thorpej FUTEX_OP_CMP_EQ, 0);
977 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
978 1.1 thorpej 0, NULL, &futex_word1, 0, op) == 0);
979 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
980 1.1 thorpej
981 1.1 thorpej op = FUTEX_OP(FUTEX_OP_XOR, 2, FUTEX_OP_CMP_EQ, 0);
982 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
983 1.1 thorpej 0, NULL, &futex_word1, 0, op) == 0);
984 1.1 thorpej ATF_REQUIRE(futex_word1 == 0);
985 1.1 thorpej }
986 1.1 thorpej
987 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wake_op_op);
988 1.1 thorpej ATF_TC_HEAD(futex_wake_op_op, tc)
989 1.1 thorpej {
990 1.1 thorpej atf_tc_set_md_var(tc, "descr",
991 1.1 thorpej "tests futex WAKE_OP OP operations");
992 1.1 thorpej }
993 1.1 thorpej ATF_TC_BODY(futex_wake_op_op, tc)
994 1.1 thorpej {
995 1.1 thorpej do_futex_wake_op_op_test(FUTEX_PRIVATE_FLAG);
996 1.1 thorpej }
997 1.1 thorpej ATF_TC_CLEANUP(futex_wake_op_op, tc)
998 1.1 thorpej {
999 1.1 thorpej do_cleanup();
1000 1.1 thorpej }
1001 1.1 thorpej
1002 1.1 thorpej static void
1003 1.1 thorpej create_wake_op_test_lwps(int flags)
1004 1.1 thorpej {
1005 1.1 thorpej int i;
1006 1.1 thorpej
1007 1.1 thorpej futex_word1 = 0;
1008 1.1 thorpej membar_sync();
1009 1.1 thorpej
1010 1.1 thorpej for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
1011 1.1 thorpej setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
1012 1.1 thorpej lwp_data[i].op_flags = flags;
1013 1.1 thorpej lwp_data[i].futex_error = -1;
1014 1.1 thorpej lwp_data[i].futex_ptr = &futex_word1;
1015 1.1 thorpej lwp_data[i].block_val = 0;
1016 1.1 thorpej lwp_data[i].bitset = 0;
1017 1.1 thorpej lwp_data[i].wait_op = FUTEX_WAIT;
1018 1.1 thorpej ATF_REQUIRE(_lwp_create(&lwp_data[i].context, 0,
1019 1.1 thorpej &lwp_data[i].lwpid) == 0);
1020 1.1 thorpej }
1021 1.1 thorpej
1022 1.1 thorpej for (i = 0; i < 5; i++) {
1023 1.1 thorpej membar_sync();
1024 1.1 thorpej if (nlwps_running == 6)
1025 1.1 thorpej break;
1026 1.1 thorpej sleep(1);
1027 1.1 thorpej }
1028 1.1 thorpej membar_sync();
1029 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 6, "waiters failed to start");
1030 1.1 thorpej
1031 1.1 thorpej /* Ensure they're blocked. */
1032 1.1 thorpej for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
1033 1.1 thorpej ATF_REQUIRE(lwp_data[i].futex_error == -1);
1034 1.1 thorpej }
1035 1.1 thorpej }
1036 1.1 thorpej
1037 1.1 thorpej static void
1038 1.1 thorpej reap_wake_op_test_lwps(void)
1039 1.1 thorpej {
1040 1.1 thorpej int i;
1041 1.1 thorpej
1042 1.1 thorpej for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
1043 1.1 thorpej ATF_REQUIRE(_lwp_wait(lwp_data[i].lwpid, NULL) == 0);
1044 1.1 thorpej }
1045 1.1 thorpej }
1046 1.1 thorpej
1047 1.1 thorpej static void
1048 1.1 thorpej do_futex_wake_op_cmp_test(int flags)
1049 1.1 thorpej {
1050 1.1 thorpej int tries, op;
1051 1.1 thorpej
1052 1.1 thorpej futex_word = 0;
1053 1.1 thorpej membar_sync();
1054 1.1 thorpej
1055 1.1 thorpej /*
1056 1.1 thorpej * Verify and negative and positive for each individual
1057 1.1 thorpej * compare.
1058 1.1 thorpej */
1059 1.1 thorpej
1060 1.1 thorpej create_wake_op_test_lwps(flags);
1061 1.1 thorpej
1062 1.1 thorpej /* #LWPs = 6 */
1063 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 1);
1064 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1065 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 0);
1066 1.1 thorpej ATF_REQUIRE(futex_word1 == 0);
1067 1.1 thorpej
1068 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
1069 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1070 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 1);
1071 1.1 thorpej ATF_REQUIRE(futex_word1 == 1);
1072 1.1 thorpej
1073 1.1 thorpej /* #LWPs = 5 */
1074 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_NE, 1);
1075 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1076 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 0);
1077 1.1 thorpej ATF_REQUIRE(futex_word1 == 1);
1078 1.1 thorpej
1079 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_NE, 2);
1080 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1081 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 1);
1082 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
1083 1.1 thorpej
1084 1.1 thorpej /* #LWPs = 4 */
1085 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_LT, 2);
1086 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1087 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 0);
1088 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
1089 1.1 thorpej
1090 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_LT, 3);
1091 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1092 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 1);
1093 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
1094 1.1 thorpej
1095 1.1 thorpej /* #LWPs = 3 */
1096 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_LE, 1);
1097 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1098 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 0);
1099 1.1 thorpej ATF_REQUIRE(futex_word1 == 1);
1100 1.1 thorpej
1101 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_LE, 1);
1102 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1103 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 1);
1104 1.1 thorpej ATF_REQUIRE(futex_word1 == 1);
1105 1.1 thorpej
1106 1.1 thorpej /* #LWPs = 2 */
1107 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 3, FUTEX_OP_CMP_GT, 3);
1108 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1109 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 0);
1110 1.1 thorpej ATF_REQUIRE(futex_word1 == 3);
1111 1.1 thorpej
1112 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_GT, 2);
1113 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1114 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 1);
1115 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
1116 1.1 thorpej
1117 1.1 thorpej /* #LWPs = 1 */
1118 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 3, FUTEX_OP_CMP_GE, 4);
1119 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1120 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 0);
1121 1.1 thorpej ATF_REQUIRE(futex_word1 == 3);
1122 1.1 thorpej
1123 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_GE, 3);
1124 1.1 thorpej ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
1125 1.1 thorpej 0, NULL, &futex_word1, 1, op) == 1);
1126 1.1 thorpej ATF_REQUIRE(futex_word1 == 2);
1127 1.1 thorpej
1128 1.1 thorpej /* #LWPs = 0 */
1129 1.1 thorpej
1130 1.1 thorpej /* Trust, but verify. */
1131 1.1 thorpej sleep(1);
1132 1.1 thorpej for (tries = 0; tries < 5; tries++) {
1133 1.1 thorpej membar_sync();
1134 1.1 thorpej if (nlwps_running == 0)
1135 1.1 thorpej break;
1136 1.1 thorpej sleep(1);
1137 1.1 thorpej }
1138 1.1 thorpej membar_sync();
1139 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "waiters failed to exit");
1140 1.1 thorpej
1141 1.1 thorpej reap_wake_op_test_lwps();
1142 1.1 thorpej
1143 1.1 thorpej /*
1144 1.1 thorpej * Verify wakes on uaddr work even if the uaddr2 comparison
1145 1.1 thorpej * fails.
1146 1.1 thorpej */
1147 1.1 thorpej
1148 1.1 thorpej create_wake_op_test_lwps(flags);
1149 1.1 thorpej
1150 1.1 thorpej /* #LWPs = 6 */
1151 1.1 thorpej ATF_REQUIRE(futex_word == 0);
1152 1.1 thorpej op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 666);
1153 1.1 thorpej ATF_REQUIRE(__futex(&futex_word1, FUTEX_WAKE_OP | flags,
1154 1.1 thorpej INT_MAX, NULL, &futex_word, 0, op) == 6);
1155 1.1 thorpej ATF_REQUIRE(futex_word == 0);
1156 1.1 thorpej
1157 1.1 thorpej /* #LWPs = 0 */
1158 1.1 thorpej
1159 1.1 thorpej /* Trust, but verify. */
1160 1.1 thorpej sleep(1);
1161 1.1 thorpej for (tries = 0; tries < 5; tries++) {
1162 1.1 thorpej membar_sync();
1163 1.1 thorpej if (nlwps_running == 0)
1164 1.1 thorpej break;
1165 1.1 thorpej sleep(1);
1166 1.1 thorpej }
1167 1.1 thorpej membar_sync();
1168 1.1 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "waiters failed to exit");
1169 1.1 thorpej
1170 1.1 thorpej reap_wake_op_test_lwps();
1171 1.1 thorpej }
1172 1.1 thorpej
1173 1.1 thorpej ATF_TC_WITH_CLEANUP(futex_wake_op_cmp);
1174 1.1 thorpej ATF_TC_HEAD(futex_wake_op_cmp, tc)
1175 1.1 thorpej {
1176 1.1 thorpej atf_tc_set_md_var(tc, "descr",
1177 1.1 thorpej "tests futex WAKE_OP CMP operations");
1178 1.1 thorpej }
1179 1.1 thorpej ATF_TC_BODY(futex_wake_op_cmp, tc)
1180 1.1 thorpej {
1181 1.1 thorpej do_futex_wake_op_cmp_test(FUTEX_PRIVATE_FLAG);
1182 1.1 thorpej }
1183 1.1 thorpej ATF_TC_CLEANUP(futex_wake_op_cmp, tc)
1184 1.1 thorpej {
1185 1.1 thorpej do_cleanup();
1186 1.1 thorpej }
1187 1.1 thorpej
1188 1.1 thorpej /*****************************************************************************/
1189 1.1 thorpej
1190 1.1 thorpej static void
1191 1.1 thorpej do_futex_wait_timeout(bool relative, clockid_t clock)
1192 1.1 thorpej {
1193 1.1 thorpej struct timespec ts;
1194 1.1 thorpej struct timespec deadline;
1195 1.1 thorpej int op = relative ? FUTEX_WAIT : FUTEX_WAIT_BITSET;
1196 1.1 thorpej
1197 1.1 thorpej if (clock == CLOCK_REALTIME)
1198 1.1 thorpej op |= FUTEX_CLOCK_REALTIME;
1199 1.1 thorpej
1200 1.1 thorpej ATF_REQUIRE(clock_gettime(clock, &deadline) == 0);
1201 1.1 thorpej deadline.tv_sec += 2;
1202 1.1 thorpej if (relative) {
1203 1.1 thorpej ts.tv_sec = 2;
1204 1.1 thorpej ts.tv_nsec = 0;
1205 1.1 thorpej } else {
1206 1.1 thorpej ts = deadline;
1207 1.1 thorpej }
1208 1.1 thorpej
1209 1.1 thorpej futex_word = 1;
1210 1.1 thorpej ATF_REQUIRE_ERRNO(ETIMEDOUT,
1211 1.1 thorpej __futex(&futex_word, op | FUTEX_PRIVATE_FLAG,
1212 1.1 thorpej 1, &ts, NULL, 0, FUTEX_BITSET_MATCH_ANY) == -1);
1213 1.1 thorpej
1214 1.1 thorpej /* Can't reliably check CLOCK_REALTIME in the presence of NTP. */
1215 1.1 thorpej if (clock != CLOCK_REALTIME) {
1216 1.1 thorpej ATF_REQUIRE(clock_gettime(clock, &ts) == 0);
1217 1.1 thorpej ATF_REQUIRE(ts.tv_sec >= deadline.tv_sec);
1218 1.1 thorpej ATF_REQUIRE(ts.tv_sec > deadline.tv_sec ||
1219 1.1 thorpej ts.tv_nsec >= deadline.tv_nsec);
1220 1.1 thorpej }
1221 1.1 thorpej }
1222 1.1 thorpej
1223 1.1 thorpej ATF_TC(futex_wait_timeout_relative);
1224 1.1 thorpej ATF_TC_HEAD(futex_wait_timeout_relative, tc)
1225 1.1 thorpej {
1226 1.1 thorpej atf_tc_set_md_var(tc, "descr",
1227 1.1 thorpej "tests futex WAIT with relative timeout");
1228 1.1 thorpej }
1229 1.1 thorpej ATF_TC_BODY(futex_wait_timeout_relative, tc)
1230 1.1 thorpej {
1231 1.1 thorpej do_futex_wait_timeout(true, CLOCK_MONOTONIC);
1232 1.1 thorpej }
1233 1.1 thorpej
1234 1.1 thorpej ATF_TC(futex_wait_timeout_relative_rt);
1235 1.1 thorpej ATF_TC_HEAD(futex_wait_timeout_relative_rt, tc)
1236 1.1 thorpej {
1237 1.1 thorpej atf_tc_set_md_var(tc, "descr",
1238 1.1 thorpej "tests futex WAIT with relative timeout (REALTIME)");
1239 1.1 thorpej }
1240 1.1 thorpej ATF_TC_BODY(futex_wait_timeout_relative_rt, tc)
1241 1.1 thorpej {
1242 1.1 thorpej do_futex_wait_timeout(true, CLOCK_REALTIME);
1243 1.1 thorpej }
1244 1.1 thorpej
1245 1.1 thorpej ATF_TC(futex_wait_timeout_deadline);
1246 1.1 thorpej ATF_TC_HEAD(futex_wait_timeout_deadline, tc)
1247 1.1 thorpej {
1248 1.1 thorpej atf_tc_set_md_var(tc, "descr",
1249 1.1 thorpej "tests futex WAIT with absolute deadline");
1250 1.1 thorpej }
1251 1.1 thorpej ATF_TC_BODY(futex_wait_timeout_deadline, tc)
1252 1.1 thorpej {
1253 1.1 thorpej do_futex_wait_timeout(false, CLOCK_MONOTONIC);
1254 1.1 thorpej }
1255 1.1 thorpej
1256 1.1 thorpej ATF_TC(futex_wait_timeout_deadline_rt);
1257 1.1 thorpej ATF_TC_HEAD(futex_wait_timeout_deadline_rt, tc)
1258 1.1 thorpej {
1259 1.1 thorpej atf_tc_set_md_var(tc, "descr",
1260 1.1 thorpej "tests futex WAIT with absolute deadline (REALTIME)");
1261 1.1 thorpej }
1262 1.1 thorpej ATF_TC_BODY(futex_wait_timeout_deadline_rt, tc)
1263 1.1 thorpej {
1264 1.1 thorpej do_futex_wait_timeout(false, CLOCK_REALTIME);
1265 1.1 thorpej }
1266 1.1 thorpej
1267 1.1 thorpej /*****************************************************************************/
1268 1.1 thorpej
1269 1.3 thorpej static void
1270 1.3 thorpej sig_noop(int sig __unused)
1271 1.3 thorpej {
1272 1.3 thorpej }
1273 1.3 thorpej
1274 1.3 thorpej static void (*old_act)(int) = SIG_DFL;
1275 1.3 thorpej
1276 1.3 thorpej static void
1277 1.3 thorpej do_futex_wait_evil_unmapped(int map_flags)
1278 1.3 thorpej {
1279 1.3 thorpej int i;
1280 1.3 thorpej
1281 1.3 thorpej create_bs(map_flags);
1282 1.3 thorpej
1283 1.3 thorpej old_act = signal(SIGUSR1, sig_noop);
1284 1.3 thorpej ATF_REQUIRE(old_act != SIG_ERR);
1285 1.3 thorpej
1286 1.3 thorpej setup_lwp_context(&lwp_data[0], simple_test_waiter_lwp);
1287 1.3 thorpej lwp_data[0].op_flags = 0;
1288 1.3 thorpej lwp_data[0].futex_error = -1;
1289 1.3 thorpej lwp_data[0].futex_ptr = &bs_addr[0];
1290 1.3 thorpej lwp_data[0].block_val = 0;
1291 1.3 thorpej lwp_data[0].bitset = 0;
1292 1.3 thorpej lwp_data[0].wait_op = FUTEX_WAIT;
1293 1.3 thorpej ATF_REQUIRE(_lwp_create(&lwp_data[0].context, 0,
1294 1.3 thorpej &lwp_data[0].lwpid) == 0);
1295 1.3 thorpej
1296 1.3 thorpej for (i = 0; i < 5; i++) {
1297 1.3 thorpej membar_sync();
1298 1.3 thorpej if (nlwps_running == 1)
1299 1.3 thorpej break;
1300 1.3 thorpej sleep(1);
1301 1.3 thorpej }
1302 1.3 thorpej membar_sync();
1303 1.3 thorpej ATF_REQUIRE_EQ_MSG(nlwps_running, 1, "waiters failed to start");
1304 1.3 thorpej
1305 1.3 thorpej /* Ensure it's blocked. */
1306 1.3 thorpej ATF_REQUIRE(lwp_data[0].futex_error == -1);
1307 1.3 thorpej
1308 1.3 thorpej /* Rudely unmap the backing store. */
1309 1.3 thorpej cleanup_bs();
1310 1.3 thorpej
1311 1.3 thorpej /* Signal the waiter so that it leaves the futex. */
1312 1.3 thorpej ATF_REQUIRE(_lwp_kill(lwp_data[0].threadid, SIGUSR1) == 0);
1313 1.3 thorpej
1314 1.3 thorpej /* Yay! No panic! */
1315 1.3 thorpej
1316 1.3 thorpej reap_lwp_waiter(&lwp_data[0]);
1317 1.3 thorpej }
1318 1.3 thorpej
1319 1.3 thorpej ATF_TC_WITH_CLEANUP(futex_wait_evil_unmapped_anon);
1320 1.3 thorpej ATF_TC_HEAD(futex_wait_evil_unmapped_anon, tc)
1321 1.3 thorpej {
1322 1.3 thorpej atf_tc_set_md_var(tc, "descr",
1323 1.3 thorpej "tests futex WAIT while futex is unmapped - anon memory");
1324 1.3 thorpej }
1325 1.3 thorpej ATF_TC_BODY(futex_wait_evil_unmapped_anon, tc)
1326 1.3 thorpej {
1327 1.3 thorpej do_futex_wait_evil_unmapped(MAP_ANON);
1328 1.3 thorpej }
1329 1.3 thorpej ATF_TC_CLEANUP(futex_wait_evil_unmapped_anon, tc)
1330 1.3 thorpej {
1331 1.3 thorpej signal(SIGUSR1, old_act);
1332 1.3 thorpej do_cleanup();
1333 1.3 thorpej }
1334 1.3 thorpej
1335 1.3 thorpej /*****************************************************************************/
1336 1.3 thorpej
1337 1.1 thorpej ATF_TP_ADD_TCS(tp)
1338 1.1 thorpej {
1339 1.1 thorpej ATF_TP_ADD_TC(tp, futex_basic_wait_wake_private);
1340 1.1 thorpej ATF_TP_ADD_TC(tp, futex_basic_wait_wake_shared);
1341 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_private);
1342 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_shared);
1343 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_private);
1344 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_shared);
1345 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_cow_private);
1346 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_cow_shared);
1347 1.1 thorpej
1348 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_shared_proc);
1349 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_shared_proc);
1350 1.1 thorpej
1351 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_pointless_bitset);
1352 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_wake_bitset);
1353 1.1 thorpej
1354 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_timeout_relative);
1355 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_timeout_relative_rt);
1356 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_timeout_deadline);
1357 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wait_timeout_deadline_rt);
1358 1.1 thorpej
1359 1.3 thorpej ATF_TP_ADD_TC(tp, futex_wait_evil_unmapped_anon);
1360 1.3 thorpej
1361 1.1 thorpej ATF_TP_ADD_TC(tp, futex_requeue);
1362 1.1 thorpej ATF_TP_ADD_TC(tp, futex_cmp_requeue);
1363 1.1 thorpej
1364 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wake_op_op);
1365 1.1 thorpej ATF_TP_ADD_TC(tp, futex_wake_op_cmp);
1366 1.1 thorpej
1367 1.1 thorpej return atf_no_error();
1368 1.1 thorpej }
1369