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