1*e3723e1fSApple OSS Distributions #include <unistd.h>
2*e3723e1fSApple OSS Distributions #include <pthread.h>
3*e3723e1fSApple OSS Distributions #include <errno.h>
4*e3723e1fSApple OSS Distributions
5*e3723e1fSApple OSS Distributions #include <sys/event.h>
6*e3723e1fSApple OSS Distributions #include <mach/mach.h>
7*e3723e1fSApple OSS Distributions #include <mach/mach_port.h>
8*e3723e1fSApple OSS Distributions
9*e3723e1fSApple OSS Distributions #include <Block.h>
10*e3723e1fSApple OSS Distributions #include <darwintest.h>
11*e3723e1fSApple OSS Distributions
12*e3723e1fSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
13*e3723e1fSApple OSS Distributions
14*e3723e1fSApple OSS Distributions /*
15*e3723e1fSApple OSS Distributions * <rdar://problem/30231213> close() of kqueue FD races with kqueue_scan park
16*e3723e1fSApple OSS Distributions *
17*e3723e1fSApple OSS Distributions * When close concurrent with poll goes wrong, the close hangs
18*e3723e1fSApple OSS Distributions * and the kevent never gets any more events.
19*e3723e1fSApple OSS Distributions */
20*e3723e1fSApple OSS Distributions
21*e3723e1fSApple OSS Distributions /* Both events should fire at about the same time */
22*e3723e1fSApple OSS Distributions static uint32_t timeout_ms = 10;
23*e3723e1fSApple OSS Distributions
24*e3723e1fSApple OSS Distributions static void *
poll_kqueue(void * arg)25*e3723e1fSApple OSS Distributions poll_kqueue(void *arg)
26*e3723e1fSApple OSS Distributions {
27*e3723e1fSApple OSS Distributions int fd = (int)arg;
28*e3723e1fSApple OSS Distributions
29*e3723e1fSApple OSS Distributions struct kevent kev = {
30*e3723e1fSApple OSS Distributions .filter = EVFILT_TIMER,
31*e3723e1fSApple OSS Distributions .flags = EV_ADD,
32*e3723e1fSApple OSS Distributions .data = timeout_ms,
33*e3723e1fSApple OSS Distributions };
34*e3723e1fSApple OSS Distributions
35*e3723e1fSApple OSS Distributions int rv = kevent(fd, &kev, 1, NULL, 0, NULL);
36*e3723e1fSApple OSS Distributions
37*e3723e1fSApple OSS Distributions if (rv == -1 && errno == EBADF) {
38*e3723e1fSApple OSS Distributions /* The close may race with this thread spawning */
39*e3723e1fSApple OSS Distributions T_LOG("kqueue already closed?");
40*e3723e1fSApple OSS Distributions return NULL;
41*e3723e1fSApple OSS Distributions } else {
42*e3723e1fSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kevent");
43*e3723e1fSApple OSS Distributions }
44*e3723e1fSApple OSS Distributions
45*e3723e1fSApple OSS Distributions while ((rv = kevent(fd, NULL, 0, &kev, 1, NULL)) == 1) {
46*e3723e1fSApple OSS Distributions T_LOG("poll\n");
47*e3723e1fSApple OSS Distributions }
48*e3723e1fSApple OSS Distributions
49*e3723e1fSApple OSS Distributions if (rv != -1 || errno != EBADF) {
50*e3723e1fSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rv, "fd should be closed");
51*e3723e1fSApple OSS Distributions }
52*e3723e1fSApple OSS Distributions
53*e3723e1fSApple OSS Distributions return NULL;
54*e3723e1fSApple OSS Distributions }
55*e3723e1fSApple OSS Distributions
56*e3723e1fSApple OSS Distributions static void
run_test(void)57*e3723e1fSApple OSS Distributions run_test(void)
58*e3723e1fSApple OSS Distributions {
59*e3723e1fSApple OSS Distributions int fd = kqueue();
60*e3723e1fSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "kqueue");
61*e3723e1fSApple OSS Distributions
62*e3723e1fSApple OSS Distributions pthread_t thread;
63*e3723e1fSApple OSS Distributions int rv = pthread_create(&thread, NULL, poll_kqueue,
64*e3723e1fSApple OSS Distributions (void *)(uintptr_t)fd);
65*e3723e1fSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_create");
66*e3723e1fSApple OSS Distributions
67*e3723e1fSApple OSS Distributions usleep(timeout_ms * 1000);
68*e3723e1fSApple OSS Distributions
69*e3723e1fSApple OSS Distributions rv = close(fd);
70*e3723e1fSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rv, "close");
71*e3723e1fSApple OSS Distributions
72*e3723e1fSApple OSS Distributions rv = pthread_join(thread, NULL);
73*e3723e1fSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_join");
74*e3723e1fSApple OSS Distributions }
75*e3723e1fSApple OSS Distributions
76*e3723e1fSApple OSS Distributions T_DECL(kqueue_close_race, "Races kqueue close with kqueue process",
77*e3723e1fSApple OSS Distributions T_META_LTEPHASE(LTE_POSTINIT), T_META_TIMEOUT(5), T_META_TAG_VM_PREFERRED)
78*e3723e1fSApple OSS Distributions {
79*e3723e1fSApple OSS Distributions for (uint32_t i = 1; i < 100; i++) {
80*e3723e1fSApple OSS Distributions run_test();
81*e3723e1fSApple OSS Distributions }
82*e3723e1fSApple OSS Distributions }
83*e3723e1fSApple OSS Distributions
84*e3723e1fSApple OSS Distributions static void *
pthread_async_do(void * arg)85*e3723e1fSApple OSS Distributions pthread_async_do(void *arg)
86*e3723e1fSApple OSS Distributions {
87*e3723e1fSApple OSS Distributions void (^block)(void) = arg;
88*e3723e1fSApple OSS Distributions block();
89*e3723e1fSApple OSS Distributions Block_release(block);
90*e3723e1fSApple OSS Distributions pthread_detach(pthread_self());
91*e3723e1fSApple OSS Distributions return NULL;
92*e3723e1fSApple OSS Distributions }
93*e3723e1fSApple OSS Distributions
94*e3723e1fSApple OSS Distributions static void
95*e3723e1fSApple OSS Distributions pthread_async(void (^block)(void))
96*e3723e1fSApple OSS Distributions {
97*e3723e1fSApple OSS Distributions pthread_t th;
98*e3723e1fSApple OSS Distributions int rc;
99*e3723e1fSApple OSS Distributions
100*e3723e1fSApple OSS Distributions rc = pthread_create(&th, NULL, pthread_async_do, Block_copy(block));
101*e3723e1fSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
102*e3723e1fSApple OSS Distributions }
103*e3723e1fSApple OSS Distributions
104*e3723e1fSApple OSS Distributions T_DECL(kqueue_filter_close,
105*e3723e1fSApple OSS Distributions "Check closing a kqfile with various filters works, rdar://72542450", T_META_TAG_VM_PREFERRED)
106*e3723e1fSApple OSS Distributions {
107*e3723e1fSApple OSS Distributions mach_port_t mp, pset;
108*e3723e1fSApple OSS Distributions kern_return_t kr;
109*e3723e1fSApple OSS Distributions struct kevent ke;
110*e3723e1fSApple OSS Distributions int kq, rc;
111*e3723e1fSApple OSS Distributions int pfd[2];
112*e3723e1fSApple OSS Distributions
113*e3723e1fSApple OSS Distributions kq = kqueue();
114*e3723e1fSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kq, "kqueue()");
115*e3723e1fSApple OSS Distributions
116*e3723e1fSApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
117*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_allocate(RECEIVE)");
118*e3723e1fSApple OSS Distributions
119*e3723e1fSApple OSS Distributions ke = (struct kevent){
120*e3723e1fSApple OSS Distributions .filter = EVFILT_MACHPORT,
121*e3723e1fSApple OSS Distributions .flags = EV_ADD,
122*e3723e1fSApple OSS Distributions .ident = mp,
123*e3723e1fSApple OSS Distributions };
124*e3723e1fSApple OSS Distributions rc = kevent(kq, &ke, 1, NULL, 0, NULL);
125*e3723e1fSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(mp)");
126*e3723e1fSApple OSS Distributions
127*e3723e1fSApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &pset);
128*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_allocate(PSET)");
129*e3723e1fSApple OSS Distributions
130*e3723e1fSApple OSS Distributions ke = (struct kevent){
131*e3723e1fSApple OSS Distributions .filter = EVFILT_MACHPORT,
132*e3723e1fSApple OSS Distributions .flags = EV_ADD,
133*e3723e1fSApple OSS Distributions .ident = pset,
134*e3723e1fSApple OSS Distributions };
135*e3723e1fSApple OSS Distributions rc = kevent(kq, &ke, 1, NULL, 0, NULL);
136*e3723e1fSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(pset)");
137*e3723e1fSApple OSS Distributions
138*e3723e1fSApple OSS Distributions rc = pipe(pfd);
139*e3723e1fSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "pipe");
140*e3723e1fSApple OSS Distributions
141*e3723e1fSApple OSS Distributions ke = (struct kevent){
142*e3723e1fSApple OSS Distributions .filter = EVFILT_READ,
143*e3723e1fSApple OSS Distributions .flags = EV_ADD,
144*e3723e1fSApple OSS Distributions .ident = (unsigned long)pfd[0],
145*e3723e1fSApple OSS Distributions };
146*e3723e1fSApple OSS Distributions rc = kevent(kq, &ke, 1, NULL, 0, NULL);
147*e3723e1fSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(fd)");
148*e3723e1fSApple OSS Distributions
149*e3723e1fSApple OSS Distributions pthread_async(^{
150*e3723e1fSApple OSS Distributions sleep(1);
151*e3723e1fSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(close(kq), "close");
152*e3723e1fSApple OSS Distributions });
153*e3723e1fSApple OSS Distributions
154*e3723e1fSApple OSS Distributions rc = kevent(kq, NULL, 0, &ke, 1, NULL);
155*e3723e1fSApple OSS Distributions T_EXPECT_POSIX_FAILURE(rc, EBADF, "kevent(closed fd) returns EBADF");
156*e3723e1fSApple OSS Distributions }
157