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