xref: /xnu-10002.61.3/tests/poll.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions #ifdef T_NAMESPACE
2*0f4c859eSApple OSS Distributions #undef T_NAMESPACE
3*0f4c859eSApple OSS Distributions #endif
4*0f4c859eSApple OSS Distributions #include <darwintest.h>
5*0f4c859eSApple OSS Distributions 
6*0f4c859eSApple OSS Distributions #include <dispatch/dispatch.h>
7*0f4c859eSApple OSS Distributions #include <fcntl.h>
8*0f4c859eSApple OSS Distributions #include <mach/mach.h>
9*0f4c859eSApple OSS Distributions #include <poll.h>
10*0f4c859eSApple OSS Distributions #include <stdint.h>
11*0f4c859eSApple OSS Distributions #include <unistd.h>
12*0f4c859eSApple OSS Distributions 
13*0f4c859eSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.poll"),
14*0f4c859eSApple OSS Distributions     T_META_RUN_CONCURRENTLY(true));
15*0f4c859eSApple OSS Distributions 
16*0f4c859eSApple OSS Distributions #define SLEEP_TIME_SECS 1
17*0f4c859eSApple OSS Distributions #define POLL_TIMEOUT_MS 1800
18*0f4c859eSApple OSS Distributions static_assert(POLL_TIMEOUT_MS > (SLEEP_TIME_SECS * 1000),
19*0f4c859eSApple OSS Distributions     "poll timeout should be longer than sleep time");
20*0f4c859eSApple OSS Distributions 
21*0f4c859eSApple OSS Distributions /*
22*0f4c859eSApple OSS Distributions  * This matches the behavior of other UNIXes, but is under-specified in POSIX.
23*0f4c859eSApple OSS Distributions  *
24*0f4c859eSApple OSS Distributions  * See <rdar://problem/28372390>.
25*0f4c859eSApple OSS Distributions  */
26*0f4c859eSApple OSS Distributions T_DECL(sleep_with_no_fds,
27*0f4c859eSApple OSS Distributions     "poll() called with no fds provided should act like sleep")
28*0f4c859eSApple OSS Distributions {
29*0f4c859eSApple OSS Distributions 	uint64_t begin_time, sleep_time, poll_time;
30*0f4c859eSApple OSS Distributions 	struct pollfd pfd = { .fd = 0, .events = 0, .revents = 0 };
31*0f4c859eSApple OSS Distributions 
32*0f4c859eSApple OSS Distributions 	begin_time = mach_absolute_time();
33*0f4c859eSApple OSS Distributions 	sleep(SLEEP_TIME_SECS);
34*0f4c859eSApple OSS Distributions 	sleep_time = mach_absolute_time() - begin_time;
35*0f4c859eSApple OSS Distributions 	T_LOG("sleep(%d) ~= %llu mach absolute time units", SLEEP_TIME_SECS, sleep_time);
36*0f4c859eSApple OSS Distributions 
37*0f4c859eSApple OSS Distributions 	begin_time = mach_absolute_time();
38*0f4c859eSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(poll(&pfd, 0, POLL_TIMEOUT_MS),
39*0f4c859eSApple OSS Distributions 	    "poll() with 0 events and timeout %d ms", POLL_TIMEOUT_MS);
40*0f4c859eSApple OSS Distributions 	poll_time = mach_absolute_time() - begin_time;
41*0f4c859eSApple OSS Distributions 
42*0f4c859eSApple OSS Distributions 	T_EXPECT_GT(poll_time, sleep_time,
43*0f4c859eSApple OSS Distributions 	    "poll(... %d) should wait longer than sleep(1)", POLL_TIMEOUT_MS);
44*0f4c859eSApple OSS Distributions }
45*0f4c859eSApple OSS Distributions 
46*0f4c859eSApple OSS Distributions #define LAUNCHD_PATH "/sbin/launchd"
47*0f4c859eSApple OSS Distributions #define PIPE_DIR_TIMEOUT_SECS 1
48*0f4c859eSApple OSS Distributions 
49*0f4c859eSApple OSS Distributions /*
50*0f4c859eSApple OSS Distributions  * See <rdar://problem/28539155>.
51*0f4c859eSApple OSS Distributions  */
52*0f4c859eSApple OSS Distributions T_DECL(directories,
53*0f4c859eSApple OSS Distributions     "poll() with directories should return an error")
54*0f4c859eSApple OSS Distributions {
55*0f4c859eSApple OSS Distributions 	int file, dir, pipes[2];
56*0f4c859eSApple OSS Distributions 	struct pollfd pfd[] = {
57*0f4c859eSApple OSS Distributions 		{ .events = POLLIN },
58*0f4c859eSApple OSS Distributions 		{ .events = POLLIN },
59*0f4c859eSApple OSS Distributions 		{ .events = POLLIN },
60*0f4c859eSApple OSS Distributions 	};
61*0f4c859eSApple OSS Distributions 
62*0f4c859eSApple OSS Distributions 	file = open(LAUNCHD_PATH, O_RDONLY | O_NONBLOCK);
63*0f4c859eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(file, "open(%s)", LAUNCHD_PATH);
64*0f4c859eSApple OSS Distributions 	dir = open(".", O_RDONLY | O_NONBLOCK);
65*0f4c859eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(dir, "open(\".\")");
66*0f4c859eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pipe(pipes), NULL);
67*0f4c859eSApple OSS Distributions 
68*0f4c859eSApple OSS Distributions 	/* just directory */
69*0f4c859eSApple OSS Distributions 	pfd[0].fd = dir;
70*0f4c859eSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(poll(pfd, 1, -1), "poll() with a directory");
71*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[0].revents & POLLNVAL,
72*0f4c859eSApple OSS Distributions 	    "directory should be an invalid event");
73*0f4c859eSApple OSS Distributions 
74*0f4c859eSApple OSS Distributions 	/* file and directory */
75*0f4c859eSApple OSS Distributions 	pfd[0].fd = file; pfd[0].revents = 0;
76*0f4c859eSApple OSS Distributions 	pfd[1].fd = dir; pfd[1].revents = 0;
77*0f4c859eSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(poll(pfd, 2, -1),
78*0f4c859eSApple OSS Distributions 	    "poll() with a file and directory");
79*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[0].revents & POLLIN, "file should be readable");
80*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[1].revents & POLLNVAL,
81*0f4c859eSApple OSS Distributions 	    "directory should be an invalid event");
82*0f4c859eSApple OSS Distributions 
83*0f4c859eSApple OSS Distributions 	/* directory and file */
84*0f4c859eSApple OSS Distributions 	pfd[0].fd = dir; pfd[0].revents = 0;
85*0f4c859eSApple OSS Distributions 	pfd[1].fd = file; pfd[1].revents = 0;
86*0f4c859eSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(poll(pfd, 2, -1),
87*0f4c859eSApple OSS Distributions 	    "poll() with a directory and a file");
88*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[0].revents & POLLNVAL,
89*0f4c859eSApple OSS Distributions 	    "directory should be an invalid event");
90*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[1].revents & POLLIN, "file should be readable");
91*0f4c859eSApple OSS Distributions 
92*0f4c859eSApple OSS Distributions 	/* file and pipe */
93*0f4c859eSApple OSS Distributions 	pfd[0].fd = file; pfd[0].revents = 0;
94*0f4c859eSApple OSS Distributions 	pfd[1].fd = pipes[0]; pfd[0].revents = 0;
95*0f4c859eSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(poll(pfd, 2, -1),
96*0f4c859eSApple OSS Distributions 	    "poll() with a file and pipe");
97*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[0].revents & POLLIN, "file should be readable");
98*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_FALSE(pfd[1].revents & POLLIN,
99*0f4c859eSApple OSS Distributions 	    "pipe should not be readable");
100*0f4c859eSApple OSS Distributions 
101*0f4c859eSApple OSS Distributions 	/* file, directory, and pipe */
102*0f4c859eSApple OSS Distributions 	pfd[0].fd = file; pfd[0].revents = 0;
103*0f4c859eSApple OSS Distributions 	pfd[1].fd = dir; pfd[1].revents = 0;
104*0f4c859eSApple OSS Distributions 	pfd[2].fd = pipes[0]; pfd[2].revents = 0;
105*0f4c859eSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(poll(pfd, 3, -1),
106*0f4c859eSApple OSS Distributions 	    "poll() with a file, directory, and pipe");
107*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[0].revents & POLLIN, "file should be readable");
108*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[1].revents & POLLNVAL,
109*0f4c859eSApple OSS Distributions 	    "directory should be an invalid event");
110*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_FALSE(pfd[2].revents & POLLIN, "pipe should not be readable");
111*0f4c859eSApple OSS Distributions 
112*0f4c859eSApple OSS Distributions 	/* directory and pipe */
113*0f4c859eSApple OSS Distributions 	__block bool timed_out = true;
114*0f4c859eSApple OSS Distributions 	pfd[0].fd = dir; pfd[0].revents = 0;
115*0f4c859eSApple OSS Distributions 	pfd[1].fd = pipes[0]; pfd[1].revents = 0;
116*0f4c859eSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
117*0f4c859eSApple OSS Distributions 	    PIPE_DIR_TIMEOUT_SECS * NSEC_PER_SEC),
118*0f4c859eSApple OSS Distributions 	    dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
119*0f4c859eSApple OSS Distributions 		T_ASSERT_FALSE(timed_out, "poll timed out after %d seconds",
120*0f4c859eSApple OSS Distributions 		PIPE_DIR_TIMEOUT_SECS);
121*0f4c859eSApple OSS Distributions 	});
122*0f4c859eSApple OSS Distributions 
123*0f4c859eSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(poll(pfd, 3, -1),
124*0f4c859eSApple OSS Distributions 	    "poll() with a directory and pipe");
125*0f4c859eSApple OSS Distributions 	timed_out = false;
126*0f4c859eSApple OSS Distributions 
127*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(pfd[0].revents & POLLNVAL,
128*0f4c859eSApple OSS Distributions 	    "directory should be an invalid event");
129*0f4c859eSApple OSS Distributions 	T_QUIET; T_EXPECT_FALSE(pfd[1].revents & POLLIN, "pipe should not be readable");
130*0f4c859eSApple OSS Distributions }
131*0f4c859eSApple OSS Distributions 
132*0f4c859eSApple OSS Distributions #define PRIVATE
133*0f4c859eSApple OSS Distributions #include <libproc.h>
134*0f4c859eSApple OSS Distributions 
135*0f4c859eSApple OSS Distributions static void *
leak_thread(void * ptr)136*0f4c859eSApple OSS Distributions leak_thread(void *ptr)
137*0f4c859eSApple OSS Distributions {
138*0f4c859eSApple OSS Distributions 	T_LOG("Trying to find kevent kernel pointer...\n");
139*0f4c859eSApple OSS Distributions 
140*0f4c859eSApple OSS Distributions 	unsigned char *buffer = (unsigned char*) malloc(16392 * 8);
141*0f4c859eSApple OSS Distributions 
142*0f4c859eSApple OSS Distributions 	while (1) {
143*0f4c859eSApple OSS Distributions 		memset(buffer, 0, 16392 * 8);
144*0f4c859eSApple OSS Distributions 
145*0f4c859eSApple OSS Distributions 		// Dump the kevent udatas for self
146*0f4c859eSApple OSS Distributions 		int ret = proc_list_uptrs(getpid(), buffer, 16392 * 8);
147*0f4c859eSApple OSS Distributions 
148*0f4c859eSApple OSS Distributions 		if (ret > 0) {
149*0f4c859eSApple OSS Distributions 			T_LOG("udata pointers returned: %d\n", ret);
150*0f4c859eSApple OSS Distributions 			uint64_t *ptrs = (uint64_t*) buffer;
151*0f4c859eSApple OSS Distributions 			for (int i = 0; i < ret; i++) {
152*0f4c859eSApple OSS Distributions 				T_EXPECT_EQ(ptrs[i] & 0xffffff0000000000, 0, "kptr? -> 0x%llx\n", ptrs[i]);
153*0f4c859eSApple OSS Distributions 			}
154*0f4c859eSApple OSS Distributions 			break;
155*0f4c859eSApple OSS Distributions 		}
156*0f4c859eSApple OSS Distributions 	}
157*0f4c859eSApple OSS Distributions 
158*0f4c859eSApple OSS Distributions 	free(buffer);
159*0f4c859eSApple OSS Distributions 	return NULL;
160*0f4c859eSApple OSS Distributions }
161*0f4c859eSApple OSS Distributions 
162*0f4c859eSApple OSS Distributions T_DECL(poll_dont_leak_kernel_pointers, "poll and proc_pidinfo should not leak kernel pointers")
163*0f4c859eSApple OSS Distributions {
164*0f4c859eSApple OSS Distributions 	pthread_t thr;
165*0f4c859eSApple OSS Distributions 	pthread_create(&thr, NULL, *leak_thread, (void *) NULL);
166*0f4c859eSApple OSS Distributions 
167*0f4c859eSApple OSS Distributions 	struct pollfd fds[2] = {};
168*0f4c859eSApple OSS Distributions 	fds[0].fd = STDERR_FILENO;
169*0f4c859eSApple OSS Distributions 	fds[0].events = POLLERR | POLLHUP;
170*0f4c859eSApple OSS Distributions 	fds[0].revents = POLLERR;
171*0f4c859eSApple OSS Distributions 	fds[1].fd = STDOUT_FILENO;
172*0f4c859eSApple OSS Distributions 	fds[1].events = POLLERR | POLLHUP;
173*0f4c859eSApple OSS Distributions 	fds[1].revents = POLLERR;
174*0f4c859eSApple OSS Distributions 
175*0f4c859eSApple OSS Distributions 	//int poll_nocancel(struct pollfd *fds, u_int nfds, int timeout)
176*0f4c859eSApple OSS Distributions 	poll(fds, 2, 5000);
177*0f4c859eSApple OSS Distributions 
178*0f4c859eSApple OSS Distributions 	pthread_join(thr, NULL);
179*0f4c859eSApple OSS Distributions }
180