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