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