1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3 *
4 * Copyright FUJITSU LIMITED 2010
5 * Copyright KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
6 *
7 * DESCRIPTION
8 * Wait on uninitialized heap. It shold be zero and FUTEX_WAIT should
9 * return immediately. This test is intent to test zero page handling in
10 * futex.
11 *
12 * AUTHOR
13 * KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
14 *
15 * HISTORY
16 * 2010-Jan-6: Initial version by KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
17 *
18 *****************************************************************************/
19
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/mman.h>
24 #include <syscall.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <linux/futex.h>
30 #include <libgen.h>
31
32 #include "futextest.h"
33 #include "../../kselftest_harness.h"
34
35 #define WAIT_US 5000000
36
37 static int child_blocked = 1;
38 static bool child_ret;
39 void *buf;
40
wait_thread(void * arg)41 void *wait_thread(void *arg)
42 {
43 int res;
44
45 child_ret = true;
46 res = futex_wait(buf, 1, NULL, 0);
47 child_blocked = 0;
48
49 if (res != 0 && errno != EWOULDBLOCK) {
50 ksft_exit_fail_msg("futex failure\n");
51 child_ret = false;
52 }
53 pthread_exit(NULL);
54 }
55
TEST(futex_wait_uninitialized_heap)56 TEST(futex_wait_uninitialized_heap)
57 {
58 long page_size;
59 pthread_t thr;
60 int ret;
61
62 page_size = sysconf(_SC_PAGESIZE);
63
64 buf = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
65 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
66 if (buf == (void *)-1)
67 ksft_exit_fail_msg("mmap\n");
68
69 ret = pthread_create(&thr, NULL, wait_thread, NULL);
70 if (ret)
71 ksft_exit_fail_msg("pthread_create\n");
72
73 ksft_print_dbg_msg("waiting %dus for child to return\n", WAIT_US);
74 usleep(WAIT_US);
75
76 if (child_blocked)
77 ksft_test_result_fail("child blocked in kernel\n");
78
79 if (!child_ret)
80 ksft_test_result_fail("child error\n");
81 }
82
83 TEST_HARNESS_MAIN
84