1 #include <darwintest.h>
2 #include <mach/mach.h>
3 #include <stdlib.h>
4 #include <sys/sysctl.h>
5 #include <unistd.h>
6 #include <darwintest_multiprocess.h>
7 #include <spawn.h>
8 #include <spawn_private.h>
9 #include <libproc_internal.h>
10 #include <signal.h>
11 #include <string.h>
12
13 #include <err.h>
14 #include <stdio.h>
15 #include <sysexits.h>
16 #include <stdbool.h>
17
18 #include "rnServer.h" // generated by MIG from rnserver.defs
19
20 #include <servers/bootstrap.h>
21 #include <libproc_internal.h> // proc*cpumon*()
22
23 T_GLOBAL_META(
24 T_META_NAMESPACE("xnu.fd"),
25 T_META_RUN_CONCURRENTLY(TRUE),
26 T_META_RADAR_COMPONENT_NAME("xnu"),
27 T_META_RADAR_COMPONENT_VERSION("file descriptors"));
28
29 #define MAX_ARGV 5
30
31 extern char **environ;
32 static mach_port_t resource_notify_port = MACH_PORT_NULL;
33
34 T_DECL(test_fd_table_set_soft_limit, "Allocate fds upto soft limit", T_META_IGNORECRASHES(".*fd_table_limits_client.*"), T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
35 {
36 char *test_prog_name = "./fd_table_limits_client";
37 char *child_args[MAX_ARGV];
38 int child_pid;
39 posix_spawnattr_t attrs;
40 int err;
41
42 #if TARGET_OS_BRIDGE
43 T_SKIP("Not running on target platforms");
44 #endif /* TARGET_OS_BRIDGE */
45
46 /* Initialize posix_spawn attributes */
47 posix_spawnattr_init(&attrs);
48
49 err = posix_spawnattr_set_filedesclimit_ext(&attrs, 200, 0);
50 T_EXPECT_POSIX_SUCCESS(err, "posix_spawnattr_set_filedesclimit_ext");
51
52 child_args[0] = test_prog_name;
53 child_args[1] = "200"; //soft limit
54 child_args[2] = "0"; //hard limit
55 child_args[3] = "1"; //test num
56 child_args[4] = NULL;
57
58 err = posix_spawn(&child_pid, child_args[0], NULL, &attrs, child_args, environ);
59 T_EXPECT_POSIX_SUCCESS(err, "posix_spawn fd_table_limits_client");
60
61 int child_status;
62 /* Wait for child and check for exception */
63
64 if (-1 == waitpid(child_pid, &child_status, 0)) {
65 T_FAIL("waitpid: child mia");
66 }
67
68 if (WIFSIGNALED(child_status)) {
69 T_FAIL("Child exited with signal = %d", WTERMSIG(child_status));
70 }
71
72 T_ASSERT_EQ(WIFEXITED(child_status), 1, "Child exited normally with exit value %d", WEXITSTATUS(child_status));
73 }
74
75 T_DECL(test_fd_table_set_hard_limit, "Allocate fds upto hard limit", T_META_IGNORECRASHES(".*fd_table_limits_client.*"), T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
76 {
77 char *test_prog_name = "./fd_table_limits_client";
78 char *child_args[MAX_ARGV];
79 int child_pid;
80 posix_spawnattr_t attrs;
81 int err;
82
83 #if TARGET_OS_BRIDGE
84 T_SKIP("Not running on target platforms");
85 #endif /* TARGET_OS_BRIDGE */
86
87 /* Initialize posix_spawn attributes */
88 posix_spawnattr_init(&attrs);
89
90 err = posix_spawnattr_set_filedesclimit_ext(&attrs, 0, 500);
91 T_EXPECT_POSIX_SUCCESS(err, "posix_spawnattr_set_filedesclimit_ext");
92
93 child_args[0] = test_prog_name;
94 child_args[1] = "0"; //soft limit
95 child_args[2] = "500"; //hard limit
96 child_args[3] = "1"; //test num
97 child_args[4] = NULL;
98
99 err = posix_spawn(&child_pid, child_args[0], NULL, &attrs, child_args, environ);
100 T_EXPECT_POSIX_SUCCESS(err, "posix_spawn fd_table_limits_client");
101
102 int child_status;
103 /* Wait for child and check for exception */
104
105 if (-1 == waitpid(child_pid, &child_status, 0)) {
106 T_FAIL("waitpid: child mia");
107 }
108
109 T_ASSERT_EQ(WIFEXITED(child_status), 0, "Child did not exit normally");
110
111 if (WIFSIGNALED(child_status)) {
112 T_ASSERT_EQ(child_status, 9, "Child exited with status = %x", child_status);
113 }
114 }
115
116 T_DECL(test_fd_table_setting_limits, "Allocate fds - both soft & hard limit", T_META_IGNORECRASHES(".*fd_table_limits_client.*"), T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
117 {
118 char *test_prog_name = "./fd_table_limits_client";
119 char *child_args[MAX_ARGV];
120 int child_pid;
121 posix_spawnattr_t attrs;
122 int err;
123
124 #if TARGET_OS_BRIDGE
125 T_SKIP("Not running on target platforms");
126 #endif /* TARGET_OS_BRIDGE */
127
128 /* Initialize posix_spawn attributes */
129 posix_spawnattr_init(&attrs);
130
131 err = posix_spawnattr_set_filedesclimit_ext(&attrs, 400, 800);
132 T_EXPECT_POSIX_SUCCESS(err, "posix_spawnattr_set_filedesclimit_ext");
133
134 child_args[0] = test_prog_name;
135 child_args[1] = "400"; //soft limit
136 child_args[2] = "800"; //hard limit
137 child_args[3] = "1"; //test num
138 child_args[4] = NULL;
139
140 err = posix_spawn(&child_pid, child_args[0], NULL, &attrs, child_args, environ);
141 T_EXPECT_POSIX_SUCCESS(err, "posix_spawn fd_table_limits_client");
142
143 int child_status;
144 /* Wait for child and check for exception */
145
146 if (-1 == waitpid(child_pid, &child_status, 0)) {
147 T_FAIL("waitpid: child mia");
148 }
149
150 T_ASSERT_EQ(WIFEXITED(child_status), 0, "Child did not exit normally");
151
152 if (WIFSIGNALED(child_status)) {
153 T_ASSERT_EQ(child_status, 9, "Child exited with status = %x", child_status);
154 }
155 }
156
157 typedef struct {
158 mach_msg_header_t header;
159 mach_msg_body_t body;
160 mach_msg_port_descriptor_t port_descriptor;
161 mach_msg_trailer_t trailer; // subtract this when sending
162 } ipc_complex_message;
163
164 struct args {
165 const char *progname;
166 int verbose;
167 int voucher;
168 int num_msgs;
169 const char *server_port_name;
170 mach_port_t server_port;
171 mach_port_t reply_port;
172 int request_msg_size;
173 void *request_msg;
174 int reply_msg_size;
175 void *reply_msg;
176 uint32_t persona_id;
177 long client_pid;
178 };
179
180 void parse_args(struct args *args);
181 void server_setup(struct args* args);
182 void* exception_server_thread(void *arg);
183 mach_port_t create_exception_port(void);
184 static mach_port_t create_resource_notify_port(void);
185 static ipc_complex_message icm_request = {};
186 static ipc_complex_message icm_reply = {};
187
188 #define TEST_TIMEOUT 10
189
190 void
parse_args(struct args * args)191 parse_args(struct args *args)
192 {
193 args->server_port_name = "TEST_FD_TABLE_LIMITS";
194 args->server_port = MACH_PORT_NULL;
195 args->reply_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
196 args->request_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
197 args->reply_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
198 args->request_msg = &icm_request;
199 args->reply_msg = &icm_reply;
200 }
201
202 /* Create a mach IPC listener which will respond to the client's message */
203 void
server_setup(struct args * args)204 server_setup(struct args *args)
205 {
206 kern_return_t ret;
207 mach_port_t bsport;
208
209 ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
210 &args->server_port);
211 T_ASSERT_MACH_SUCCESS(ret, "server: mach_port_allocate()");
212
213 ret = mach_port_insert_right(mach_task_self(), args->server_port, args->server_port,
214 MACH_MSG_TYPE_MAKE_SEND);
215 T_ASSERT_MACH_SUCCESS(ret, "server: mach_port_insert_right()");
216
217 ret = task_get_bootstrap_port(mach_task_self(), &bsport);
218 T_ASSERT_MACH_SUCCESS(ret, "server: task_get_bootstrap_port()");
219
220 ret = bootstrap_register(bsport, (const char *)args->server_port_name, args->server_port);
221 T_ASSERT_MACH_SUCCESS(ret, "server: bootstrap_register()");
222
223 T_LOG("server: waiting for IPC messages from client on port '%s'.\n",
224 args->server_port_name);
225
226 /* Make the server port as the resource notify port */
227 resource_notify_port = args->server_port;
228 }
229
230 T_DECL(test_fd_table_hard_limit_with_resource_notify_port, "Allocate ports upto hard limit and trigger notification", T_META_IGNORECRASHES(".*fd_table_limits_client.*"), T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
231 {
232 char *test_prog_name = "./fd_table_limits_client";
233 char *child_args[MAX_ARGV];
234 int child_pid;
235 posix_spawnattr_t attrs;
236 int err;
237 kern_return_t kr;
238 struct args* server_args = (struct args*)malloc(sizeof(struct args));
239
240 #if TARGET_OS_BRIDGE
241 T_SKIP("Not running on target platforms");
242 #endif /* TARGET_OS_BRIDGE */
243
244 /* Create the bootstrap port */
245 parse_args(server_args);
246 server_setup(server_args);
247
248 /*
249 * dt_helper_t helpers[] = {
250 * dt_launchd_helper_domain("com.apple.xnu.test.mach_port.plist",
251 * "right_dedup_server", NULL, LAUNCH_SYSTEM_DOMAIN),
252 * dt_fork_helper("right_dedup_client"),
253 * };
254 * dt_run_helpers(helpers, 2, 600);
255 */
256
257 /* Initialize posix_spawn attributes */
258 posix_spawnattr_init(&attrs);
259 err = posix_spawnattr_set_filedesclimit_ext(&attrs, 0, 500);
260 T_ASSERT_POSIX_SUCCESS(err, "posix_spawnattr_set_filedesclimit_ext");
261
262 child_args[0] = test_prog_name;
263 child_args[1] = "0"; // soft limit
264 child_args[2] = "500"; // hard limit
265 child_args[3] = "2"; // test num
266 child_args[4] = NULL;
267
268 err = posix_spawn(&child_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
269 T_ASSERT_POSIX_SUCCESS(err, "posix_spawn fd_table_limits_client");
270
271 server_args->client_pid = child_pid;
272
273 T_LOG("server: Let's see if we can catch some fd leak");
274 /*
275 * Recover the service port because the port must have been destroyed and sent the notification by now
276 */
277 kr = mach_msg_server_once(resource_notify_server, 4096, resource_notify_port, 0);
278 T_ASSERT_MACH_SUCCESS(kr, "mach_msg_server_once resource_notify_port");
279 }
280
281 // MIG's resource_notify_server() expects receive_cpu_usage_trigger()
282 // This must match the definition in xnu's resource_notify.defs
283 kern_return_t
receive_cpu_usage_violation(__unused mach_port_t receiver,__unused proc_name_t procname,__unused pid_t pid,__unused posix_path_t killed_proc_path,__unused mach_timespec_t timestamp,__unused int64_t observed_cpu_nsecs,__unused int64_t observation_nsecs,__unused int64_t cpu_nsecs_allowed,__unused int64_t limit_window_nsecs,__unused resource_notify_flags_t flags)284 receive_cpu_usage_violation(__unused mach_port_t receiver,
285 __unused proc_name_t procname,
286 __unused pid_t pid,
287 __unused posix_path_t killed_proc_path,
288 __unused mach_timespec_t timestamp,
289 __unused int64_t observed_cpu_nsecs,
290 __unused int64_t observation_nsecs,
291 __unused int64_t cpu_nsecs_allowed,
292 __unused int64_t limit_window_nsecs,
293 __unused resource_notify_flags_t flags)
294 {
295 return KERN_FAILURE;
296 }
297
298 kern_return_t
receive_cpu_wakes_violation(__unused mach_port_t receiver,__unused proc_name_t procname,__unused pid_t pid,__unused posix_path_t killed_proc_path,__unused mach_timespec_t timestamp,__unused int64_t observed_cpu_wakes,__unused int64_t observation_nsecs,__unused int64_t cpu_wakes_allowed,__unused int64_t limit_window_nsecs,__unused resource_notify_flags_t flags)299 receive_cpu_wakes_violation(__unused mach_port_t receiver,
300 __unused proc_name_t procname,
301 __unused pid_t pid,
302 __unused posix_path_t killed_proc_path,
303 __unused mach_timespec_t timestamp,
304 __unused int64_t observed_cpu_wakes,
305 __unused int64_t observation_nsecs,
306 __unused int64_t cpu_wakes_allowed,
307 __unused int64_t limit_window_nsecs,
308 __unused resource_notify_flags_t flags)
309 {
310 return KERN_FAILURE;
311 }
312
313 kern_return_t
receive_disk_writes_violation(__unused mach_port_t receiver,__unused proc_name_t procname,__unused pid_t pid,__unused posix_path_t killed_proc_path,__unused mach_timespec_t timestamp,__unused int64_t observed_bytes_dirtied,__unused int64_t observation_nsecs,__unused int64_t bytes_dirtied_allowed,__unused int64_t limit_window_nsecs,__unused resource_notify_flags_t flags)314 receive_disk_writes_violation(__unused mach_port_t receiver,
315 __unused proc_name_t procname,
316 __unused pid_t pid,
317 __unused posix_path_t killed_proc_path,
318 __unused mach_timespec_t timestamp,
319 __unused int64_t observed_bytes_dirtied,
320 __unused int64_t observation_nsecs,
321 __unused int64_t bytes_dirtied_allowed,
322 __unused int64_t limit_window_nsecs,
323 __unused resource_notify_flags_t flags)
324 {
325 return KERN_FAILURE;
326 }
327
328 kern_return_t
receive_port_space_violation(__unused mach_port_t receiver,__unused proc_name_t procname,__unused pid_t pid,__unused mach_timespec_t timestamp,__unused int64_t observed_ports,__unused int64_t ports_allowed,__unused mach_port_t fatal_port,__unused resource_notify_flags_t flags)329 receive_port_space_violation(__unused mach_port_t receiver,
330 __unused proc_name_t procname,
331 __unused pid_t pid,
332 __unused mach_timespec_t timestamp,
333 __unused int64_t observed_ports,
334 __unused int64_t ports_allowed,
335 __unused mach_port_t fatal_port,
336 __unused resource_notify_flags_t flags)
337 {
338 return KERN_FAILURE;
339 }
340
341 kern_return_t
receive_file_descriptors_violation(__unused mach_port_t receiver,__unused proc_name_t procname,__unused pid_t pid,__unused mach_timespec_t timestamp,__unused int64_t observed_filedesc,__unused int64_t filedesc_allowed,__unused mach_port_t fatal_port,__unused resource_notify_flags_t flags)342 receive_file_descriptors_violation(__unused mach_port_t receiver,
343 __unused proc_name_t procname,
344 __unused pid_t pid,
345 __unused mach_timespec_t timestamp,
346 __unused int64_t observed_filedesc,
347 __unused int64_t filedesc_allowed,
348 __unused mach_port_t fatal_port,
349 __unused resource_notify_flags_t flags)
350 {
351 T_LOG("Received a notification on the resource notify port");
352 T_LOG("filedesc_allowed = %lld, observed_filedesc = %lld", filedesc_allowed, observed_filedesc);
353 if (fatal_port) {
354 mach_port_deallocate(mach_task_self(), fatal_port);
355 }
356
357 return KERN_SUCCESS;
358 }
359