1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <pthread.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
28#include <syscall.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <unistd.h>
32#include <errno.h>
33#include <linux/futex.h>
34#include <libgen.h>
35
36#include "logging.h"
37#include "futextest.h"
38
39#define TEST_NAME "futex-wait-uninitialized-heap"
40#define WAIT_US 5000000
41
42static int child_blocked = 1;
43static int child_ret;
44void *buf;
45
46void usage(char *prog)
47{
48 printf("Usage: %s\n", prog);
49 printf(" -c Use color\n");
50 printf(" -h Display this help message\n");
51 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
52 VQUIET, VCRITICAL, VINFO);
53}
54
55void *wait_thread(void *arg)
56{
57 int res;
58
59 child_ret = RET_PASS;
60 res = futex_wait(buf, 1, NULL, 0);
61 child_blocked = 0;
62
63 if (res != 0 && errno != EWOULDBLOCK) {
64 error("futex failure\n", errno);
65 child_ret = RET_ERROR;
66 }
67 pthread_exit(NULL);
68}
69
70int main(int argc, char **argv)
71{
72 int c, ret = RET_PASS;
73 long page_size;
74 pthread_t thr;
75
76 while ((c = getopt(argc, argv, "chv:")) != -1) {
77 switch (c) {
78 case 'c':
79 log_color(1);
80 break;
81 case 'h':
82 usage(basename(argv[0]));
83 exit(0);
84 case 'v':
85 log_verbosity(atoi(optarg));
86 break;
87 default:
88 usage(basename(argv[0]));
89 exit(1);
90 }
91 }
92
93 page_size = sysconf(_SC_PAGESIZE);
94
95 buf = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
96 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
97 if (buf == (void *)-1) {
98 error("mmap\n", errno);
99 exit(1);
100 }
101
102 ksft_print_header();
103 ksft_print_msg("%s: Test the uninitialized futex value in FUTEX_WAIT\n",
104 basename(argv[0]));
105
106
107 ret = pthread_create(&thr, NULL, wait_thread, NULL);
108 if (ret) {
109 error("pthread_create\n", errno);
110 ret = RET_ERROR;
111 goto out;
112 }
113
114 info("waiting %dus for child to return\n", WAIT_US);
115 usleep(WAIT_US);
116
117 ret = child_ret;
118 if (child_blocked) {
119 fail("child blocked in kernel\n");
120 ret = RET_FAIL;
121 }
122
123 out:
124 print_result(TEST_NAME, ret);
125 return ret;
126}
127