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