xref: /linux/tools/testing/selftests/futex/functional/futex_wait_uninitialized_heap.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /******************************************************************************
2  *
3  * Copyright FUJITSU LIMITED 2010
4  * Copyright KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
5  *
6  *   This program is free software;  you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  * DESCRIPTION
12  *      Wait on uninitialized heap. It shold be zero and FUTEX_WAIT should
13  *      return immediately. This test is intent to test zero page handling in
14  *      futex.
15  *
16  * AUTHOR
17  *      KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
18  *
19  * HISTORY
20  *      2010-Jan-6: Initial version by KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
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 WAIT_US 5000000
40 
41 static int child_blocked = 1;
42 static int child_ret;
43 void *buf;
44 
45 void usage(char *prog)
46 {
47 	printf("Usage: %s\n", prog);
48 	printf("  -c	Use color\n");
49 	printf("  -h	Display this help message\n");
50 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
51 	       VQUIET, VCRITICAL, VINFO);
52 }
53 
54 void *wait_thread(void *arg)
55 {
56 	int res;
57 
58 	child_ret = RET_PASS;
59 	res = futex_wait(buf, 1, NULL, 0);
60 	child_blocked = 0;
61 
62 	if (res != 0 && errno != EWOULDBLOCK) {
63 		error("futex failure\n", errno);
64 		child_ret = RET_ERROR;
65 	}
66 	pthread_exit(NULL);
67 }
68 
69 int main(int argc, char **argv)
70 {
71 	int c, ret = RET_PASS;
72 	long page_size;
73 	pthread_t thr;
74 
75 	while ((c = getopt(argc, argv, "chv:")) != -1) {
76 		switch (c) {
77 		case 'c':
78 			log_color(1);
79 			break;
80 		case 'h':
81 			usage(basename(argv[0]));
82 			exit(0);
83 		case 'v':
84 			log_verbosity(atoi(optarg));
85 			break;
86 		default:
87 			usage(basename(argv[0]));
88 			exit(1);
89 		}
90 	}
91 
92 	page_size = sysconf(_SC_PAGESIZE);
93 
94 	buf = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
95 		   MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
96 	if (buf == (void *)-1) {
97 		error("mmap\n", errno);
98 		exit(1);
99 	}
100 
101 	printf("%s: Test the uninitialized futex value in FUTEX_WAIT\n",
102 	       basename(argv[0]));
103 
104 
105 	ret = pthread_create(&thr, NULL, wait_thread, NULL);
106 	if (ret) {
107 		error("pthread_create\n", errno);
108 		ret = RET_ERROR;
109 		goto out;
110 	}
111 
112 	info("waiting %dus for child to return\n", WAIT_US);
113 	usleep(WAIT_US);
114 
115 	ret = child_ret;
116 	if (child_blocked) {
117 		fail("child blocked in kernel\n");
118 		ret = RET_FAIL;
119 	}
120 
121  out:
122 	print_result(ret);
123 	return ret;
124 }
125