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