1*2c2f96dcSApple OSS Distributions #include <unistd.h>
2*2c2f96dcSApple OSS Distributions #include <pthread.h>
3*2c2f96dcSApple OSS Distributions #include <errno.h>
4*2c2f96dcSApple OSS Distributions
5*2c2f96dcSApple OSS Distributions #include <sys/event.h>
6*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
7*2c2f96dcSApple OSS Distributions #include <mach/mach_port.h>
8*2c2f96dcSApple OSS Distributions
9*2c2f96dcSApple OSS Distributions #include <Block.h>
10*2c2f96dcSApple OSS Distributions #include <darwintest.h>
11*2c2f96dcSApple OSS Distributions
12*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
13*2c2f96dcSApple OSS Distributions T_META_NAMESPACE("xnu.kevent"),
14*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
15*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("kevent"),
16*2c2f96dcSApple OSS Distributions T_META_RUN_CONCURRENTLY(true)
17*2c2f96dcSApple OSS Distributions );
18*2c2f96dcSApple OSS Distributions
19*2c2f96dcSApple OSS Distributions static void
send(mach_port_t send_port)20*2c2f96dcSApple OSS Distributions send(mach_port_t send_port)
21*2c2f96dcSApple OSS Distributions {
22*2c2f96dcSApple OSS Distributions kern_return_t kr = 0;
23*2c2f96dcSApple OSS Distributions mach_msg_base_t msg = {
24*2c2f96dcSApple OSS Distributions .header = {
25*2c2f96dcSApple OSS Distributions .msgh_remote_port = send_port,
26*2c2f96dcSApple OSS Distributions .msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND,
27*2c2f96dcSApple OSS Distributions 0, MACH_MSG_TYPE_MOVE_SEND, 0),
28*2c2f96dcSApple OSS Distributions .msgh_id = 0x100,
29*2c2f96dcSApple OSS Distributions .msgh_size = sizeof(msg),
30*2c2f96dcSApple OSS Distributions },
31*2c2f96dcSApple OSS Distributions };
32*2c2f96dcSApple OSS Distributions
33*2c2f96dcSApple OSS Distributions kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
34*2c2f96dcSApple OSS Distributions msg.header.msgh_size, 0, MACH_PORT_NULL, 10000, 0);
35*2c2f96dcSApple OSS Distributions
36*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client mach_msg");
37*2c2f96dcSApple OSS Distributions }
38*2c2f96dcSApple OSS Distributions
39*2c2f96dcSApple OSS Distributions static kern_return_t
receive(mach_port_t rcv_port)40*2c2f96dcSApple OSS Distributions receive(mach_port_t rcv_port)
41*2c2f96dcSApple OSS Distributions {
42*2c2f96dcSApple OSS Distributions mach_msg_base_t msg = {
43*2c2f96dcSApple OSS Distributions .header = {
44*2c2f96dcSApple OSS Distributions .msgh_remote_port = MACH_PORT_NULL,
45*2c2f96dcSApple OSS Distributions .msgh_local_port = rcv_port,
46*2c2f96dcSApple OSS Distributions .msgh_size = sizeof(msg),
47*2c2f96dcSApple OSS Distributions },
48*2c2f96dcSApple OSS Distributions };
49*2c2f96dcSApple OSS Distributions
50*2c2f96dcSApple OSS Distributions return mach_msg(&msg.header, MACH_RCV_MSG | MACH_RCV_TIMEOUT,
51*2c2f96dcSApple OSS Distributions 0, msg.header.msgh_size, rcv_port, 5000, 0);
52*2c2f96dcSApple OSS Distributions }
53*2c2f96dcSApple OSS Distributions
54*2c2f96dcSApple OSS Distributions static void
fill_kevent(struct kevent * ke,uint16_t action,mach_port_t mp)55*2c2f96dcSApple OSS Distributions fill_kevent(struct kevent *ke, uint16_t action, mach_port_t mp)
56*2c2f96dcSApple OSS Distributions {
57*2c2f96dcSApple OSS Distributions *ke = (struct kevent){
58*2c2f96dcSApple OSS Distributions .filter = EVFILT_MACHPORT,
59*2c2f96dcSApple OSS Distributions .flags = action,
60*2c2f96dcSApple OSS Distributions .ident = mp,
61*2c2f96dcSApple OSS Distributions };
62*2c2f96dcSApple OSS Distributions }
63*2c2f96dcSApple OSS Distributions
64*2c2f96dcSApple OSS Distributions #define TS(s) (struct timespec){ .tv_sec = s }
65*2c2f96dcSApple OSS Distributions
66*2c2f96dcSApple OSS Distributions static void *
pthread_async_do(void * arg)67*2c2f96dcSApple OSS Distributions pthread_async_do(void *arg)
68*2c2f96dcSApple OSS Distributions {
69*2c2f96dcSApple OSS Distributions void (^block)(void) = arg;
70*2c2f96dcSApple OSS Distributions block();
71*2c2f96dcSApple OSS Distributions Block_release(block);
72*2c2f96dcSApple OSS Distributions pthread_detach(pthread_self());
73*2c2f96dcSApple OSS Distributions return NULL;
74*2c2f96dcSApple OSS Distributions }
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS Distributions static void
77*2c2f96dcSApple OSS Distributions pthread_async(void (^block)(void))
78*2c2f96dcSApple OSS Distributions {
79*2c2f96dcSApple OSS Distributions pthread_t th;
80*2c2f96dcSApple OSS Distributions int rc;
81*2c2f96dcSApple OSS Distributions
82*2c2f96dcSApple OSS Distributions rc = pthread_create(&th, NULL, pthread_async_do, Block_copy(block));
83*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
84*2c2f96dcSApple OSS Distributions }
85*2c2f96dcSApple OSS Distributions
86*2c2f96dcSApple OSS Distributions T_DECL(kqueue_machport, "basic EVFILT_MACHPORT tests")
87*2c2f96dcSApple OSS Distributions {
88*2c2f96dcSApple OSS Distributions mach_port_options_t opts = {
89*2c2f96dcSApple OSS Distributions .flags = MPO_INSERT_SEND_RIGHT,
90*2c2f96dcSApple OSS Distributions };
91*2c2f96dcSApple OSS Distributions mach_port_t mp, pset;
92*2c2f96dcSApple OSS Distributions kern_return_t kr;
93*2c2f96dcSApple OSS Distributions struct kevent ke[2];
94*2c2f96dcSApple OSS Distributions int kq, rc;
95*2c2f96dcSApple OSS Distributions
96*2c2f96dcSApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0, &mp);
97*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_port_construct()");
98*2c2f96dcSApple OSS Distributions
99*2c2f96dcSApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &pset);
100*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_port_allocate(PSET)");
101*2c2f96dcSApple OSS Distributions
102*2c2f96dcSApple OSS Distributions kr = mach_port_move_member(mach_task_self(), mp, pset);
103*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_port_move_member(PORT, PSET)");
104*2c2f96dcSApple OSS Distributions
105*2c2f96dcSApple OSS Distributions kq = kqueue();
106*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(kq, "kqueue()");
107*2c2f96dcSApple OSS Distributions
108*2c2f96dcSApple OSS Distributions /*
109*2c2f96dcSApple OSS Distributions * Fired when attached
110*2c2f96dcSApple OSS Distributions */
111*2c2f96dcSApple OSS Distributions send(mp);
112*2c2f96dcSApple OSS Distributions
113*2c2f96dcSApple OSS Distributions fill_kevent(&ke[0], EV_ADD, mp);
114*2c2f96dcSApple OSS Distributions fill_kevent(&ke[1], EV_ADD, pset);
115*2c2f96dcSApple OSS Distributions rc = kevent(kq, ke, 2, NULL, 0, &TS(5));
116*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(registration)");
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(5));
119*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 2, "kevent(fired at attach time)");
120*2c2f96dcSApple OSS Distributions
121*2c2f96dcSApple OSS Distributions receive(mp);
122*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(1));
123*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 0, "no event");
124*2c2f96dcSApple OSS Distributions
125*2c2f96dcSApple OSS Distributions /*
126*2c2f96dcSApple OSS Distributions * Fired after being attached, before wait
127*2c2f96dcSApple OSS Distributions */
128*2c2f96dcSApple OSS Distributions send(mp);
129*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(5));
130*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 2, "kevent(fired after attach time, before wait)");
131*2c2f96dcSApple OSS Distributions
132*2c2f96dcSApple OSS Distributions receive(mp);
133*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(1));
134*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 0, "no event");
135*2c2f96dcSApple OSS Distributions
136*2c2f96dcSApple OSS Distributions /*
137*2c2f96dcSApple OSS Distributions * Fired after being attached, after wait
138*2c2f96dcSApple OSS Distributions */
139*2c2f96dcSApple OSS Distributions pthread_async(^{
140*2c2f96dcSApple OSS Distributions sleep(1);
141*2c2f96dcSApple OSS Distributions send(mp);
142*2c2f96dcSApple OSS Distributions });
143*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(5));
144*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 2, "kevent(fired after attach time, after wait)");
145*2c2f96dcSApple OSS Distributions
146*2c2f96dcSApple OSS Distributions receive(mp);
147*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(1));
148*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 0, "no event");
149*2c2f96dcSApple OSS Distributions
150*2c2f96dcSApple OSS Distributions /* Make sure destroying ports wakes you up */
151*2c2f96dcSApple OSS Distributions pthread_async(^{
152*2c2f96dcSApple OSS Distributions sleep(1);
153*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(mach_port_destruct(mach_task_self(), mp, -1, 0),
154*2c2f96dcSApple OSS Distributions "mach_port_destruct");
155*2c2f96dcSApple OSS Distributions });
156*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(5));
157*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 1, "kevent(port-destroyed)");
158*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(ke[0].ident, (uintptr_t)mp, "event was for the port");
159*2c2f96dcSApple OSS Distributions
160*2c2f96dcSApple OSS Distributions pthread_async(^{
161*2c2f96dcSApple OSS Distributions sleep(1);
162*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(mach_port_mod_refs(mach_task_self(), pset,
163*2c2f96dcSApple OSS Distributions MACH_PORT_RIGHT_PORT_SET, -1), "destroy pset");
164*2c2f96dcSApple OSS Distributions });
165*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, ke, 2, &TS(5));
166*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 1, "kevent(port-destroyed)");
167*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(ke[0].ident, (uintptr_t)pset, "event was for the pset");
168*2c2f96dcSApple OSS Distributions }
169*2c2f96dcSApple OSS Distributions
170*2c2f96dcSApple OSS Distributions static int
kevent_attach_event(mach_port_t port,uint16_t flags,uint32_t fflags,int * error)171*2c2f96dcSApple OSS Distributions kevent_attach_event(mach_port_t port, uint16_t flags, uint32_t fflags, int *error)
172*2c2f96dcSApple OSS Distributions {
173*2c2f96dcSApple OSS Distributions int rc;
174*2c2f96dcSApple OSS Distributions
175*2c2f96dcSApple OSS Distributions struct kevent_qos_s kev = {
176*2c2f96dcSApple OSS Distributions .ident = port,
177*2c2f96dcSApple OSS Distributions .filter = EVFILT_MACHPORT,
178*2c2f96dcSApple OSS Distributions .flags = flags,
179*2c2f96dcSApple OSS Distributions .qos = 0xA00,
180*2c2f96dcSApple OSS Distributions .udata = 0x6666666666666666,
181*2c2f96dcSApple OSS Distributions .fflags = fflags,
182*2c2f96dcSApple OSS Distributions };
183*2c2f96dcSApple OSS Distributions
184*2c2f96dcSApple OSS Distributions struct kevent_qos_s kev_err = {};
185*2c2f96dcSApple OSS Distributions
186*2c2f96dcSApple OSS Distributions rc = kevent_id(0x88888887, &kev, 1, &kev_err, 1, NULL, NULL,
187*2c2f96dcSApple OSS Distributions KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
188*2c2f96dcSApple OSS Distributions
189*2c2f96dcSApple OSS Distributions *error = (int)kev_err.data;
190*2c2f96dcSApple OSS Distributions return rc;
191*2c2f96dcSApple OSS Distributions }
192*2c2f96dcSApple OSS Distributions
193*2c2f96dcSApple OSS Distributions /* rdar://95680295 (Turnstile Use-after-Free in XNU) */
194*2c2f96dcSApple OSS Distributions T_DECL(kqueue_machport_no_toggle_flags, "don't allow turnstile flags to be toggled for EVFILT_MACHPORT")
195*2c2f96dcSApple OSS Distributions {
196*2c2f96dcSApple OSS Distributions kern_return_t kr;
197*2c2f96dcSApple OSS Distributions int rc, error = 0;
198*2c2f96dcSApple OSS Distributions mach_port_t port = MACH_PORT_NULL;
199*2c2f96dcSApple OSS Distributions
200*2c2f96dcSApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
201*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_port_allocate()");
202*2c2f96dcSApple OSS Distributions
203*2c2f96dcSApple OSS Distributions rc = kevent_attach_event(port, EV_ADD | EV_ENABLE | EV_DISPATCH, 0, &error);
204*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(rc, 0, "kevent attach event");
205*2c2f96dcSApple OSS Distributions
206*2c2f96dcSApple OSS Distributions rc = kevent_attach_event(port, 0, MACH_RCV_MSG, &error);
207*2c2f96dcSApple OSS Distributions T_QUIET; T_EXPECT_EQ_INT(rc, 1, "registration failed");
208*2c2f96dcSApple OSS Distributions T_EXPECT_EQ_INT(error, EINVAL, "cannot modify filter flag MACH_RCV_MSG");
209*2c2f96dcSApple OSS Distributions
210*2c2f96dcSApple OSS Distributions rc = kevent_attach_event(port, 0, MACH_RCV_SYNC_PEEK, &error);
211*2c2f96dcSApple OSS Distributions T_QUIET; T_EXPECT_EQ_INT(rc, 1, "registration failed");
212*2c2f96dcSApple OSS Distributions T_EXPECT_EQ_INT(error, EINVAL, "cannot modify filter flag MACH_RCV_SYNC_PEEK");
213*2c2f96dcSApple OSS Distributions }
214