xref: /xnu-10063.121.3/tests/ipc_mach_port.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions #include <darwintest.h>
2*2c2f96dcSApple OSS Distributions #include <darwintest_multiprocess.h>
3*2c2f96dcSApple OSS Distributions #include <launch.h>
4*2c2f96dcSApple OSS Distributions #include <servers/bootstrap.h>
5*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
6*2c2f96dcSApple OSS Distributions #include "exc_helpers.h"
7*2c2f96dcSApple OSS Distributions 
8*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
9*2c2f96dcSApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
10*2c2f96dcSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
11*2c2f96dcSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
12*2c2f96dcSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
13*2c2f96dcSApple OSS Distributions 
14*2c2f96dcSApple OSS Distributions #pragma mark - helpers
15*2c2f96dcSApple OSS Distributions 
16*2c2f96dcSApple OSS Distributions #define SERVICE_NAME  "com.apple.xnu.test.mach_port"
17*2c2f96dcSApple OSS Distributions 
18*2c2f96dcSApple OSS Distributions struct one_port_msg {
19*2c2f96dcSApple OSS Distributions 	mach_msg_header_t          header;
20*2c2f96dcSApple OSS Distributions 	mach_msg_body_t            body;
21*2c2f96dcSApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
22*2c2f96dcSApple OSS Distributions 	mach_msg_trailer_t         trailer;            // subtract this when sending
23*2c2f96dcSApple OSS Distributions };
24*2c2f96dcSApple OSS Distributions 
25*2c2f96dcSApple OSS Distributions static mach_port_t
server_checkin(void)26*2c2f96dcSApple OSS Distributions server_checkin(void)
27*2c2f96dcSApple OSS Distributions {
28*2c2f96dcSApple OSS Distributions 	mach_port_t mp;
29*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions 	kr = bootstrap_check_in(bootstrap_port, SERVICE_NAME, &mp);
32*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "bootstrap_check_in");
33*2c2f96dcSApple OSS Distributions 	return mp;
34*2c2f96dcSApple OSS Distributions }
35*2c2f96dcSApple OSS Distributions 
36*2c2f96dcSApple OSS Distributions static mach_port_t
server_lookup(void)37*2c2f96dcSApple OSS Distributions server_lookup(void)
38*2c2f96dcSApple OSS Distributions {
39*2c2f96dcSApple OSS Distributions 	mach_port_t mp;
40*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
41*2c2f96dcSApple OSS Distributions 
42*2c2f96dcSApple OSS Distributions 	kr = bootstrap_look_up(bootstrap_port, SERVICE_NAME, &mp);
43*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "bootstrap_look_up");
44*2c2f96dcSApple OSS Distributions 	return mp;
45*2c2f96dcSApple OSS Distributions }
46*2c2f96dcSApple OSS Distributions 
47*2c2f96dcSApple OSS Distributions static mach_port_t
make_sr_port(void)48*2c2f96dcSApple OSS Distributions make_sr_port(void)
49*2c2f96dcSApple OSS Distributions {
50*2c2f96dcSApple OSS Distributions 	mach_port_options_t opts = {
51*2c2f96dcSApple OSS Distributions 		.flags = MPO_INSERT_SEND_RIGHT,
52*2c2f96dcSApple OSS Distributions 	};
53*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
54*2c2f96dcSApple OSS Distributions 	mach_port_t port;
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0ull, &port);
57*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
58*2c2f96dcSApple OSS Distributions 	return port;
59*2c2f96dcSApple OSS Distributions }
60*2c2f96dcSApple OSS Distributions 
61*2c2f96dcSApple OSS Distributions static void
destroy_port(mach_port_t port,bool receive,int srights)62*2c2f96dcSApple OSS Distributions destroy_port(mach_port_t port, bool receive, int srights)
63*2c2f96dcSApple OSS Distributions {
64*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
65*2c2f96dcSApple OSS Distributions 
66*2c2f96dcSApple OSS Distributions 	if (srights) {
67*2c2f96dcSApple OSS Distributions 		kr = mach_port_mod_refs(mach_task_self(), port,
68*2c2f96dcSApple OSS Distributions 		    MACH_PORT_RIGHT_SEND, -srights);
69*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "srights -= %d", srights);
70*2c2f96dcSApple OSS Distributions 	}
71*2c2f96dcSApple OSS Distributions 	if (receive) {
72*2c2f96dcSApple OSS Distributions 		kr = mach_port_mod_refs(mach_task_self(), port,
73*2c2f96dcSApple OSS Distributions 		    MACH_PORT_RIGHT_RECEIVE, -1);
74*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "receive -= 1");
75*2c2f96dcSApple OSS Distributions 	}
76*2c2f96dcSApple OSS Distributions }
77*2c2f96dcSApple OSS Distributions 
78*2c2f96dcSApple OSS Distributions static void
send_port(mach_msg_id_t id,mach_port_t dest,mach_port_t right,mach_msg_type_name_t disp)79*2c2f96dcSApple OSS Distributions send_port(
80*2c2f96dcSApple OSS Distributions 	mach_msg_id_t        id,
81*2c2f96dcSApple OSS Distributions 	mach_port_t          dest,
82*2c2f96dcSApple OSS Distributions 	mach_port_t          right,
83*2c2f96dcSApple OSS Distributions 	mach_msg_type_name_t disp)
84*2c2f96dcSApple OSS Distributions {
85*2c2f96dcSApple OSS Distributions 	struct one_port_msg msg = {
86*2c2f96dcSApple OSS Distributions 		.header = {
87*2c2f96dcSApple OSS Distributions 			.msgh_remote_port = dest,
88*2c2f96dcSApple OSS Distributions 			.msgh_bits        = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND,
89*2c2f96dcSApple OSS Distributions 	    0, MACH_MSG_TYPE_MOVE_SEND, MACH_MSGH_BITS_COMPLEX),
90*2c2f96dcSApple OSS Distributions 			.msgh_id          = id,
91*2c2f96dcSApple OSS Distributions 			.msgh_size        = offsetof(struct one_port_msg, trailer),
92*2c2f96dcSApple OSS Distributions 		},
93*2c2f96dcSApple OSS Distributions 		.body = {
94*2c2f96dcSApple OSS Distributions 			.msgh_descriptor_count = 1,
95*2c2f96dcSApple OSS Distributions 		},
96*2c2f96dcSApple OSS Distributions 		.port_descriptor = {
97*2c2f96dcSApple OSS Distributions 			.name        = right,
98*2c2f96dcSApple OSS Distributions 			.disposition = disp,
99*2c2f96dcSApple OSS Distributions 			.type        = MACH_MSG_PORT_DESCRIPTOR,
100*2c2f96dcSApple OSS Distributions 		},
101*2c2f96dcSApple OSS Distributions 	};
102*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions 	kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
105*2c2f96dcSApple OSS Distributions 	    msg.header.msgh_size, 0, MACH_PORT_NULL, 10000, 0);
106*2c2f96dcSApple OSS Distributions 
107*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "send(%d)", id);
108*2c2f96dcSApple OSS Distributions }
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions #pragma mark - basic test about right deduplication
111*2c2f96dcSApple OSS Distributions 
112*2c2f96dcSApple OSS Distributions static mach_port_t
receive_port(mach_msg_id_t expected_id,mach_port_t rcv_port,mach_msg_type_name_t expected_disp)113*2c2f96dcSApple OSS Distributions receive_port(
114*2c2f96dcSApple OSS Distributions 	mach_msg_id_t        expected_id,
115*2c2f96dcSApple OSS Distributions 	mach_port_t          rcv_port,
116*2c2f96dcSApple OSS Distributions 	mach_msg_type_name_t expected_disp)
117*2c2f96dcSApple OSS Distributions {
118*2c2f96dcSApple OSS Distributions 	struct one_port_msg msg = { };
119*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions 	T_LOG("waiting for message %d", expected_id);
122*2c2f96dcSApple OSS Distributions 	kr = mach_msg(&msg.header, MACH_RCV_MSG, 0,
123*2c2f96dcSApple OSS Distributions 	    sizeof(msg), rcv_port, 0, 0);
124*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "receive(%d)", expected_id);
125*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(msg.header.msgh_id, expected_id, "message id matches");
126*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_NE(msg.header.msgh_bits & MACH_MSGH_BITS_COMPLEX, 0,
127*2c2f96dcSApple OSS Distributions 	    "message is complex");
128*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(msg.body.msgh_descriptor_count, 1, "message has one right");
129*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ((mach_msg_type_name_t)msg.port_descriptor.disposition, expected_disp,
130*2c2f96dcSApple OSS Distributions 	    "port has right disposition");
131*2c2f96dcSApple OSS Distributions 	return msg.port_descriptor.name;
132*2c2f96dcSApple OSS Distributions }
133*2c2f96dcSApple OSS Distributions 
134*2c2f96dcSApple OSS Distributions T_HELPER_DECL(right_dedup_server, "right_dedup_server")
135*2c2f96dcSApple OSS Distributions {
136*2c2f96dcSApple OSS Distributions 	mach_port_t svc_port = server_checkin();
137*2c2f96dcSApple OSS Distributions 	mach_port_t ports[3];
138*2c2f96dcSApple OSS Distributions 
139*2c2f96dcSApple OSS Distributions 	ports[0] = receive_port(1, svc_port, MACH_MSG_TYPE_MOVE_RECEIVE);
140*2c2f96dcSApple OSS Distributions 	ports[1] = receive_port(2, svc_port, MACH_MSG_TYPE_MOVE_SEND);
141*2c2f96dcSApple OSS Distributions 	ports[2] = receive_port(3, svc_port, MACH_MSG_TYPE_MOVE_SEND);
142*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ports[0], ports[1], "receive, send, send");
143*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ports[0], ports[2], "receive, send, send");
144*2c2f96dcSApple OSS Distributions 	destroy_port(ports[0], true, 2);
145*2c2f96dcSApple OSS Distributions 
146*2c2f96dcSApple OSS Distributions 	ports[0] = receive_port(4, svc_port, MACH_MSG_TYPE_MOVE_SEND);
147*2c2f96dcSApple OSS Distributions 	ports[1] = receive_port(5, svc_port, MACH_MSG_TYPE_MOVE_RECEIVE);
148*2c2f96dcSApple OSS Distributions 	ports[2] = receive_port(6, svc_port, MACH_MSG_TYPE_MOVE_SEND);
149*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ports[0], ports[1], "send, receive, send");
150*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ports[0], ports[2], "send, receive, send");
151*2c2f96dcSApple OSS Distributions 	destroy_port(ports[0], true, 2);
152*2c2f96dcSApple OSS Distributions 
153*2c2f96dcSApple OSS Distributions 	ports[0] = receive_port(7, svc_port, MACH_MSG_TYPE_MOVE_SEND);
154*2c2f96dcSApple OSS Distributions 	ports[1] = receive_port(8, svc_port, MACH_MSG_TYPE_MOVE_SEND);
155*2c2f96dcSApple OSS Distributions 	ports[2] = receive_port(9, svc_port, MACH_MSG_TYPE_MOVE_RECEIVE);
156*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ports[0], ports[1], "send, send, receive");
157*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ports[0], ports[2], "send, send, receive");
158*2c2f96dcSApple OSS Distributions 	destroy_port(ports[0], true, 2);
159*2c2f96dcSApple OSS Distributions 
160*2c2f96dcSApple OSS Distributions 	T_END;
161*2c2f96dcSApple OSS Distributions }
162*2c2f96dcSApple OSS Distributions 
163*2c2f96dcSApple OSS Distributions T_HELPER_DECL(right_dedup_client, "right_dedup_client")
164*2c2f96dcSApple OSS Distributions {
165*2c2f96dcSApple OSS Distributions 	mach_port_t svc_port = server_lookup();
166*2c2f96dcSApple OSS Distributions 	mach_port_t port;
167*2c2f96dcSApple OSS Distributions 
168*2c2f96dcSApple OSS Distributions 	port = make_sr_port();
169*2c2f96dcSApple OSS Distributions 	send_port(1, svc_port, port, MACH_MSG_TYPE_MOVE_RECEIVE);
170*2c2f96dcSApple OSS Distributions 	send_port(2, svc_port, port, MACH_MSG_TYPE_COPY_SEND);
171*2c2f96dcSApple OSS Distributions 	send_port(3, svc_port, port, MACH_MSG_TYPE_MOVE_SEND);
172*2c2f96dcSApple OSS Distributions 
173*2c2f96dcSApple OSS Distributions 	port = make_sr_port();
174*2c2f96dcSApple OSS Distributions 	send_port(4, svc_port, port, MACH_MSG_TYPE_COPY_SEND);
175*2c2f96dcSApple OSS Distributions 	send_port(5, svc_port, port, MACH_MSG_TYPE_MOVE_RECEIVE);
176*2c2f96dcSApple OSS Distributions 	send_port(6, svc_port, port, MACH_MSG_TYPE_MOVE_SEND);
177*2c2f96dcSApple OSS Distributions 
178*2c2f96dcSApple OSS Distributions 	port = make_sr_port();
179*2c2f96dcSApple OSS Distributions 	send_port(7, svc_port, port, MACH_MSG_TYPE_COPY_SEND);
180*2c2f96dcSApple OSS Distributions 	send_port(8, svc_port, port, MACH_MSG_TYPE_MOVE_SEND);
181*2c2f96dcSApple OSS Distributions 	send_port(9, svc_port, port, MACH_MSG_TYPE_MOVE_RECEIVE);
182*2c2f96dcSApple OSS Distributions }
183*2c2f96dcSApple OSS Distributions 
184*2c2f96dcSApple OSS Distributions T_DECL(right_dedup, "make sure right deduplication works")
185*2c2f96dcSApple OSS Distributions {
186*2c2f96dcSApple OSS Distributions 	dt_helper_t helpers[] = {
187*2c2f96dcSApple OSS Distributions 		dt_launchd_helper_domain("com.apple.xnu.test.mach_port.plist",
188*2c2f96dcSApple OSS Distributions 	    "right_dedup_server", NULL, LAUNCH_SYSTEM_DOMAIN),
189*2c2f96dcSApple OSS Distributions 		dt_fork_helper("right_dedup_client"),
190*2c2f96dcSApple OSS Distributions 	};
191*2c2f96dcSApple OSS Distributions 	dt_run_helpers(helpers, 2, 600);
192*2c2f96dcSApple OSS Distributions }
193