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