xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/futex/futex_wait04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Cyril Hrubis <[email protected]>
4  *
5  * Based on futextest (futext_wait_uninitialized_heap.c)
6  * written by KOSAKI Motohiro <[email protected]>
7  *
8  * Wait on uninitialized heap. It shold be zero and FUTEX_WAIT should return
9  * immediately. This test tests zero page handling in futex code.
10  */
11 
12 #include <errno.h>
13 
14 #include "futextest.h"
15 
16 static struct futex_test_variants variants[] = {
17 #if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
18 	{ .fntype = FUTEX_FN_FUTEX, .tstype = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
19 #endif
20 
21 #if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
22 	{ .fntype = FUTEX_FN_FUTEX64, .tstype = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
23 #endif
24 };
25 
run(void)26 static void run(void)
27 {
28 	struct futex_test_variants *tv = &variants[tst_variant];
29 	struct tst_ts to = tst_ts_from_ns(tv->tstype, 10000);
30 	size_t pagesize = getpagesize();
31 	void *buf;
32 	int res;
33 
34 	buf = SAFE_MMAP(NULL, pagesize, PROT_READ|PROT_WRITE,
35 			MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
36 
37 	res = futex_wait(tv->fntype, buf, 1, &to, 0);
38 	if (res == -1 && errno == EWOULDBLOCK)
39 		tst_res(TPASS | TERRNO, "futex_wait() returned %i", res);
40 	else
41 		tst_res(TFAIL | TERRNO, "futex_wait() returned %i", res);
42 
43 	SAFE_MUNMAP(buf, pagesize);
44 }
45 
setup(void)46 static void setup(void)
47 {
48 	struct futex_test_variants *tv = &variants[tst_variant];
49 
50 	tst_res(TINFO, "Testing variant: %s", tv->desc);
51 	futex_supported_by_kernel(tv->fntype);
52 }
53 
54 static struct tst_test test = {
55 	.setup = setup,
56 	.test_all = run,
57 	.test_variants = ARRAY_SIZE(variants),
58 };
59