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