1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <sys/kern_memorystatus.h>
4 #include <kern/debug.h>
5 #include <mach-o/dyld.h>
6 #include <sys/stackshot.h>
11 #define FIRST_RECURSIVE_FRAME 3
14 T_META_NAMESPACE("xnu.stackshot.accuracy"),
15 T_META_CHECK_LEAKS(false),
20 void child_init(void);
21 void parent_helper_singleproc(int);
23 #define CHECK_FOR_FAULT_STATS (1 << 0)
24 #define WRITE_STACKSHOT_BUFFER_TO_TMP (1 << 1)
25 #define CHECK_FOR_KERNEL_THREADS (1 << 2)
26 int check_stackshot(void *, int);
28 /* used for WRITE_STACKSHOT_BUFFER_TO_TMP */
29 static char const *current_scenario_name;
30 static pid_t child_pid;
34 static void __attribute__((noinline))
35 child_recurse(int r, int spin, void (^cb)(void))
38 child_recurse(r - 1, spin, cb);
46 } else if (spin == 2) {
48 /* ssh won't let the session die if we still have file handles open to its output. */
51 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.wedge_thread", NULL, NULL, &v, sizeof(v)),
52 "wedged thread in the kernel");
55 __asm__ volatile("" : : : "memory");
60 T_HELPER_DECL(simple_child_process, "child process that will be frozen and others")
65 T_HELPER_DECL(sid_child_process, "child process that setsid()s")
67 pid_t ppid = getppid();
69 T_ASSERT_POSIX_SUCCESS(setsid(), "session id set");
71 child_recurse(RECURSIONS, 2, ^{
75 T_ASSERT_FAIL("child_init returned!");
81 kill(child_pid, SIGKILL);
85 take_stackshot(pid_t target_pid, uint32_t extra_flags, uint64_t since_timestamp)
87 void *stackshot_config;
89 uint32_t stackshot_flags = STACKSHOT_KCDATA_FORMAT |
90 STACKSHOT_THREAD_WAITINFO |
93 /* we should be able to verify delta stackshots */
94 if (since_timestamp != 0) {
95 stackshot_flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT;
98 stackshot_flags |= extra_flags;
100 stackshot_config = stackshot_config_create();
101 T_ASSERT_NOTNULL(stackshot_config, "allocate stackshot config");
103 err = stackshot_config_set_flags(stackshot_config, stackshot_flags);
104 T_ASSERT_EQ(err, 0, "set flags on stackshot config");
106 err = stackshot_config_set_pid(stackshot_config, target_pid);
107 T_ASSERT_EQ(err, 0, "set target pid on stackshot config");
109 if (since_timestamp != 0) {
110 err = stackshot_config_set_delta_timestamp(stackshot_config, since_timestamp);
111 T_ASSERT_EQ(err, 0, "set prev snapshot time on stackshot config");
114 while (retries > 0) {
115 err = stackshot_capture_with_config(stackshot_config);
118 } else if (err == EBUSY || err == ETIMEDOUT) {
119 T_LOG("stackshot capture returned %d (%s)\n", err, strerror(err));
121 T_ASSERT_FAIL("failed to take stackshot with error after retries: %d: %s\n", err, strerror(err));
127 T_ASSERT_FAIL("failed to take stackshot with error: %d: %s\n", err, strerror(err));
131 return stackshot_config;
135 check_stackshot(void *stackshot_config, int flags)
138 uint32_t buflen, kcdata_type;
140 NSError *nserror = nil;
143 uint64_t expected_return_addr = 0;
144 bool found_fault_stats = false;
145 struct stackshot_fault_stats fault_stats = {0};
147 buf = stackshot_config_get_stackshot_buffer(stackshot_config);
148 T_ASSERT_NOTNULL(buf, "stackshot buffer is not null");
149 buflen = stackshot_config_get_stackshot_size(stackshot_config);
150 T_ASSERT_GT(buflen, 0, "valid stackshot buffer length");
151 target_pid = ((struct stackshot_config*)stackshot_config)->sc_pid;
152 T_ASSERT_GT(target_pid, 0, "valid target_pid");
154 /* if need to write it to fs, do it now */
155 if (flags & WRITE_STACKSHOT_BUFFER_TO_TMP) {
156 char sspath[MAXPATHLEN];
157 strlcpy(sspath, current_scenario_name, sizeof(sspath));
158 strlcat(sspath, ".kcdata", sizeof(sspath));
159 T_QUIET; T_ASSERT_POSIX_ZERO(dt_resultfile(sspath, sizeof(sspath)),
160 "create result file path");
162 FILE *f = fopen(sspath, "w");
163 T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(f,
164 "open stackshot output file");
166 size_t written = fwrite(buf, buflen, 1, f);
167 T_QUIET; T_ASSERT_POSIX_SUCCESS(written, "wrote stackshot to file");
172 /* begin iterating */
173 iter = kcdata_iter(buf, buflen);
174 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "buffer is a stackshot");
176 /* time to iterate */
177 iter = kcdata_iter_next(iter);
178 KCDATA_ITER_FOREACH(iter) {
179 kcdata_type = kcdata_iter_type(iter);
181 NSMutableDictionary *parsedContainer, *parsedThreads;
183 if ((flags & CHECK_FOR_FAULT_STATS) != 0 &&
184 kcdata_type == STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS) {
185 memcpy(&fault_stats, kcdata_iter_payload(iter), sizeof(fault_stats));
186 found_fault_stats = true;
189 if (kcdata_type != KCDATA_TYPE_CONTAINER_BEGIN) {
193 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) {
197 parsedContainer = parseKCDataContainer(&iter, &nserror);
198 T_ASSERT_NOTNULL(parsedContainer, "parsedContainer is not null");
199 T_ASSERT_NULL(nserror, "no NSError occured while parsing the kcdata container");
202 * given that we've targetted the pid, we can be sure that this
203 * ts_pid will be the pid we expect
205 parsedPid = parsedContainer[@"task_snapshots"][@"task_snapshot"][@"ts_pid"];
206 T_ASSERT_EQ([parsedPid intValue], target_pid, "found correct pid");
208 /* start parsing the threads */
209 parsedThreads = parsedContainer[@"task_snapshots"][@"thread_snapshots"];
210 for (id th_key in parsedThreads) {
211 uint32_t frame_index = 0;
213 if ((flags & CHECK_FOR_KERNEL_THREADS) == 0) {
214 /* skip threads that don't have enough frames */
215 if ([parsedThreads[th_key][@"user_stack_frames"] count] < RECURSIONS) {
219 for (id frame in parsedThreads[th_key][@"user_stack_frames"]) {
220 if ((frame_index >= FIRST_RECURSIVE_FRAME) && (frame_index < (RECURSIONS - FIRST_RECURSIVE_FRAME))) {
221 if (expected_return_addr == 0ull) {
222 expected_return_addr = [frame[@"lr"] unsignedLongLongValue];
225 T_ASSERT_EQ(expected_return_addr, [frame[@"lr"] unsignedLongLongValue], "expected return address found");
231 T_ASSERT_NOTNULL(parsedThreads[th_key][@"kernel_stack_frames"],
232 "found kernel stack frames");
238 if (found_fault_stats) {
239 T_LOG("number of pages faulted in: %d", fault_stats.sfs_pages_faulted_in);
240 T_LOG("MATUs spent faulting: %lld", fault_stats.sfs_time_spent_faulting);
241 T_LOG("MATUS fault time limit: %lld", fault_stats.sfs_system_max_fault_time);
242 T_LOG("did we stop because of the limit?: %s", fault_stats.sfs_stopped_faulting ? "yes" : "no");
243 if (expected_return_addr != 0ull) {
244 T_ASSERT_GT(fault_stats.sfs_pages_faulted_in, 0, "faulted at least one page in");
245 T_LOG("NOTE: successfully faulted in the pages");
247 T_LOG("NOTE: We were not able to fault the stack's pages back in");
249 /* if we couldn't fault the pages back in, then at least verify that we tried */
250 T_ASSERT_GT(fault_stats.sfs_time_spent_faulting, 0ull, "spent time trying to fault");
252 } else if ((flags & CHECK_FOR_KERNEL_THREADS) == 0) {
253 T_ASSERT_NE(expected_return_addr, 0ull, "found child thread with recursions");
256 if (flags & CHECK_FOR_FAULT_STATS) {
257 T_ASSERT_EQ(found_fault_stats, true, "found fault stats");
268 #endif /* !TARGET_OS_OSX */
269 pid_t pid = getpid();
270 char padding[16 * 1024];
271 __asm__ volatile(""::"r"(padding));
273 T_LOG("child pid: %d\n", pid);
276 /* allow us to be frozen */
277 freeze_state = memorystatus_control(MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE, pid, 0, NULL, 0);
278 if (freeze_state == -1) {
279 T_SKIP("This device doesn't have CONFIG_FREEZE enabled.");
280 } else if (freeze_state == 0) {
281 T_LOG("CHILD was found to be UNFREEZABLE, enabling freezing.");
282 memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE, pid, 1, NULL, 0);
283 freeze_state = memorystatus_control(MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE, pid, 0, NULL, 0);
284 T_ASSERT_EQ(freeze_state, 1, "successfully set freezeability");
287 T_LOG("Cannot change freezeability as freezing is only available on embedded devices");
288 #endif /* !TARGET_OS_OSX */
291 * recurse a bunch of times to generate predictable data in the stackshot,
292 * then send SIGUSR1 to the parent to let it know that we are done.
294 child_recurse(RECURSIONS, 0, ^{
295 kill(getppid(), SIGUSR1);
298 T_ASSERT_FAIL("child_recurse returned, but it must not?");
302 parent_helper_singleproc(int spin)
304 dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0);
305 dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.basic_sp", NULL);
306 void *stackshot_config;
308 dispatch_async(dq, ^{
309 char padding[16 * 1024];
310 __asm__ volatile(""::"r"(padding));
312 child_recurse(RECURSIONS, spin, ^{
313 dispatch_semaphore_signal(child_done_sema);
317 dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER);
318 T_LOG("done waiting for child");
320 /* take the stackshot and parse it */
321 stackshot_config = take_stackshot(getpid(), 0, 0);
323 /* check that the stackshot has the stack frames */
324 check_stackshot(stackshot_config, 0);
329 T_DECL(basic, "test that no-fault stackshot works correctly")
332 uint32_t path_size = sizeof(path);
333 char *args[] = { path, "-n", "simple_child_process", NULL };
334 dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.basic", NULL);
335 dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0);
336 dispatch_source_t child_sig_src;
337 void *stackshot_config;
339 current_scenario_name = __func__;
341 T_LOG("parent pid: %d\n", getpid());
342 T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
344 /* setup signal handling */
345 signal(SIGUSR1, SIG_IGN);
346 child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq);
347 dispatch_source_set_event_handler(child_sig_src, ^{
348 dispatch_semaphore_signal(child_done_sema);
350 dispatch_activate(child_sig_src);
352 /* create the child process */
353 T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&child_pid, args, false, NULL, NULL), "child launched");
354 T_ATEND(kill_children);
356 /* wait until the child has recursed enough */
357 dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER);
359 T_LOG("child finished, parent executing");
361 /* take the stackshot and parse it */
362 stackshot_config = take_stackshot(child_pid, 0, 0);
364 /* check that the stackshot has the stack frames */
365 check_stackshot(stackshot_config, 0);
367 T_LOG("all done, killing child");
369 /* tell the child to quit */
370 T_ASSERT_POSIX_SUCCESS(kill(child_pid, SIGTERM), "killed child");
373 T_DECL(basic_singleproc, "test that no-fault stackshot works correctly in single process setting")
375 current_scenario_name = __func__;
376 parent_helper_singleproc(0);
379 T_DECL(basic_singleproc_spin, "test that no-fault stackshot works correctly in single process setting with spinning")
381 current_scenario_name = __func__;
382 parent_helper_singleproc(1);
385 T_DECL(fault, "test that faulting stackshots work correctly")
387 dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_fault_accuracy", NULL);
388 dispatch_source_t child_sig_src;
389 dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0);
390 void *stackshot_config;
391 int oldftm, newval = 1, freeze_enabled, oldratio, newratio = 0;
392 size_t oldlen = sizeof(oldftm), fe_len = sizeof(freeze_enabled), ratiolen = sizeof(oldratio);
394 uint32_t path_size = sizeof(path);
395 char *args[] = { path, "-n", "simple_child_process", NULL };
397 current_scenario_name = __func__;
398 T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
401 T_SKIP("freezing is not available on macOS");
402 #endif /* TARGET_OS_OSX */
404 /* Try checking if freezing is enabled at all */
405 if (sysctlbyname("vm.freeze_enabled", &freeze_enabled, &fe_len, NULL, 0) == -1) {
406 if (errno == ENOENT) {
407 T_SKIP("This device doesn't have CONFIG_FREEZE enabled.");
409 T_FAIL("failed to query vm.freeze_enabled, errno: %d", errno);
413 if (!freeze_enabled) {
414 T_SKIP("Freeze is not enabled, skipping test.");
417 /* signal handling */
418 signal(SIGUSR1, SIG_IGN);
419 child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq);
420 dispatch_source_set_event_handler(child_sig_src, ^{
421 dispatch_semaphore_signal(child_done_sema);
423 dispatch_activate(child_sig_src);
425 T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&child_pid, args, false, NULL, NULL), "child launched");
426 T_ATEND(kill_children);
428 dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER);
430 /* keep processes in memory */
431 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_to_memory", &oldftm, &oldlen, &newval, sizeof(newval)),
432 "disabled freezing to disk");
434 /* set the ratio to zero */
435 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_private_shared_pages_ratio", &oldratio, &ratiolen, &newratio, sizeof(newratio)), "disabled private:shared ratio checking");
437 /* freeze the child */
438 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze", NULL, 0, &child_pid, sizeof(child_pid)),
441 /* Sleep to allow the compressor to finish compressing the child */
444 /* take the stackshot and parse it */
445 stackshot_config = take_stackshot(child_pid, STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING, 0);
447 /* check that the stackshot has the stack frames */
448 check_stackshot(stackshot_config, CHECK_FOR_FAULT_STATS);
450 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_to_memory", NULL, 0, &oldftm, sizeof(oldftm)),
451 "reset freezing to disk");
453 /* reset the private:shared ratio */
454 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_private_shared_pages_ratio", NULL, 0, &oldratio, sizeof(oldratio)), "reset private:shared ratio");
456 T_LOG("all done, killing child");
458 /* tell the child to quit */
459 T_ASSERT_POSIX_SUCCESS(kill(child_pid, SIGTERM), "killed child");
462 T_DECL(fault_singleproc, "test that faulting stackshots work correctly in a single process setting")
464 dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0);
465 dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.fault_sp", NULL);
466 void *stackshot_config;
467 __block pthread_t child_thread;
469 size_t child_stacklen;
472 T_SKIP("madvise(..., ..., MADV_PAGEOUT) is not available on embedded platforms");
473 #endif /* !TARGET_OS_OSX */
475 dispatch_async(dq, ^{
476 char padding[16 * 1024];
477 __asm__ volatile(""::"r"(padding));
479 child_recurse(RECURSIONS, 0, ^{
480 child_thread = pthread_self();
481 dispatch_semaphore_signal(child_done_sema);
485 dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER);
486 T_LOG("done waiting for child");
488 child_stack = pthread_get_stackaddr_np(child_thread);
489 child_stacklen = pthread_get_stacksize_np(child_thread);
490 child_stack -= child_stacklen;
491 T_LOG("child stack: [0x%p - 0x%p]: 0x%zu bytes", (void *)child_stack,
492 (void *)(child_stack + child_stacklen), child_stacklen);
494 /* paging out the child */
495 T_ASSERT_POSIX_SUCCESS(madvise(child_stack, child_stacklen, MADV_PAGEOUT), "paged out via madvise(2) the child stack");
497 /* take the stackshot and parse it */
498 stackshot_config = take_stackshot(getpid(), STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING, 0);
500 /* check that the stackshot has the stack frames */
501 check_stackshot(stackshot_config, CHECK_FOR_FAULT_STATS);
506 T_DECL(zombie, "test that threads wedged in the kernel can be stackshot'd")
508 dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.zombie", NULL);
509 dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0);
510 dispatch_source_t child_sig_src;
511 void *stackshot_config;
513 uint32_t path_size = sizeof(path);
514 char *args[] = { path, "-n", "sid_child_process", NULL };
516 current_scenario_name = __func__;
517 T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
519 T_LOG("parent pid: %d\n", getpid());
521 /* setup signal handling */
522 signal(SIGUSR1, SIG_IGN);
523 child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq);
524 dispatch_source_set_event_handler(child_sig_src, ^{
525 dispatch_semaphore_signal(child_done_sema);
527 dispatch_activate(child_sig_src);
529 /* create the child process */
530 T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&child_pid, args, false, NULL, NULL), "child launched");
531 T_ATEND(kill_children);
533 /* wait until the child has recursed enough */
534 dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER);
536 T_LOG("child finished, parent executing. invoking jetsam");
538 T_ASSERT_POSIX_SUCCESS(memorystatus_control(MEMORYSTATUS_CMD_TEST_JETSAM, child_pid, 0, 0, 0),
539 "jetsam'd the child");
541 /* Sleep to allow the target process to become zombified */
544 /* take the stackshot and parse it */
545 stackshot_config = take_stackshot(child_pid, 0, 0);
547 /* check that the stackshot has the stack frames */
548 check_stackshot(stackshot_config, CHECK_FOR_KERNEL_THREADS);
550 T_LOG("all done, unwedging and killing child");
553 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.unwedge_thread", NULL, NULL, &v, sizeof(v)),
556 /* tell the child to quit */
557 T_ASSERT_POSIX_SUCCESS(kill(child_pid, SIGTERM), "killed child");