1*2c2f96dcSApple OSS Distributions#include <darwintest.h> 2*2c2f96dcSApple OSS Distributions#include <darwintest_utils.h> 3*2c2f96dcSApple OSS Distributions#include <sys/kern_memorystatus.h> 4*2c2f96dcSApple OSS Distributions#include <kern/debug.h> 5*2c2f96dcSApple OSS Distributions#include <mach-o/dyld.h> 6*2c2f96dcSApple OSS Distributions#include <sys/stackshot.h> 7*2c2f96dcSApple OSS Distributions#include <kdd.h> 8*2c2f96dcSApple OSS Distributions#include <signal.h> 9*2c2f96dcSApple OSS Distributions 10*2c2f96dcSApple OSS Distributions#define RECURSIONS 25 11*2c2f96dcSApple OSS Distributions#define FIRST_RECURSIVE_FRAME 3 12*2c2f96dcSApple OSS Distributions 13*2c2f96dcSApple OSS DistributionsT_GLOBAL_META( 14*2c2f96dcSApple OSS Distributions T_META_NAMESPACE("xnu.stackshot.accuracy"), 15*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 16*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("stackshot"), 17*2c2f96dcSApple OSS Distributions T_META_OWNER("jonathan_w_adams"), 18*2c2f96dcSApple OSS Distributions T_META_CHECK_LEAKS(false), 19*2c2f96dcSApple OSS Distributions T_META_ASROOT(true) 20*2c2f96dcSApple OSS Distributions ); 21*2c2f96dcSApple OSS Distributions 22*2c2f96dcSApple OSS Distributions 23*2c2f96dcSApple OSS Distributionsvoid child_init(void); 24*2c2f96dcSApple OSS Distributionsvoid parent_helper_singleproc(int); 25*2c2f96dcSApple OSS Distributions 26*2c2f96dcSApple OSS Distributions#define CHECK_FOR_FAULT_STATS (1 << 0) 27*2c2f96dcSApple OSS Distributions#define WRITE_STACKSHOT_BUFFER_TO_TMP (1 << 1) 28*2c2f96dcSApple OSS Distributions#define CHECK_FOR_KERNEL_THREADS (1 << 2) 29*2c2f96dcSApple OSS Distributionsint check_stackshot(void *, int); 30*2c2f96dcSApple OSS Distributions 31*2c2f96dcSApple OSS Distributions/* used for WRITE_STACKSHOT_BUFFER_TO_TMP */ 32*2c2f96dcSApple OSS Distributionsstatic char const *current_scenario_name; 33*2c2f96dcSApple OSS Distributionsstatic pid_t child_pid; 34*2c2f96dcSApple OSS Distributions 35*2c2f96dcSApple OSS Distributions/* helpers */ 36*2c2f96dcSApple OSS Distributions 37*2c2f96dcSApple OSS Distributionsstatic void __attribute__((noinline)) 38*2c2f96dcSApple OSS Distributionschild_recurse(int r, int spin, void (^cb)(void)) 39*2c2f96dcSApple OSS Distributions{ 40*2c2f96dcSApple OSS Distributions if (r > 0) { 41*2c2f96dcSApple OSS Distributions child_recurse(r - 1, spin, cb); 42*2c2f96dcSApple OSS Distributions } 43*2c2f96dcSApple OSS Distributions 44*2c2f96dcSApple OSS Distributions cb(); 45*2c2f96dcSApple OSS Distributions 46*2c2f96dcSApple OSS Distributions /* wait forever */ 47*2c2f96dcSApple OSS Distributions if (spin == 0) { 48*2c2f96dcSApple OSS Distributions sleep(100000); 49*2c2f96dcSApple OSS Distributions } else if (spin == 2) { 50*2c2f96dcSApple OSS Distributions int v = 1; 51*2c2f96dcSApple OSS Distributions /* ssh won't let the session die if we still have file handles open to its output. */ 52*2c2f96dcSApple OSS Distributions close(STDERR_FILENO); 53*2c2f96dcSApple OSS Distributions close(STDOUT_FILENO); 54*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.wedge_thread", NULL, NULL, &v, sizeof(v)), 55*2c2f96dcSApple OSS Distributions "wedged thread in the kernel"); 56*2c2f96dcSApple OSS Distributions } else { 57*2c2f96dcSApple OSS Distributions while (1) { 58*2c2f96dcSApple OSS Distributions __asm__ volatile("" : : : "memory"); 59*2c2f96dcSApple OSS Distributions } 60*2c2f96dcSApple OSS Distributions } 61*2c2f96dcSApple OSS Distributions} 62*2c2f96dcSApple OSS Distributions 63*2c2f96dcSApple OSS DistributionsT_HELPER_DECL(simple_child_process, "child process that will be frozen and others") 64*2c2f96dcSApple OSS Distributions{ 65*2c2f96dcSApple OSS Distributions child_init(); 66*2c2f96dcSApple OSS Distributions} 67*2c2f96dcSApple OSS Distributions 68*2c2f96dcSApple OSS DistributionsT_HELPER_DECL(sid_child_process, "child process that setsid()s") 69*2c2f96dcSApple OSS Distributions{ 70*2c2f96dcSApple OSS Distributions pid_t ppid = getppid(); 71*2c2f96dcSApple OSS Distributions 72*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsid(), "session id set"); 73*2c2f96dcSApple OSS Distributions 74*2c2f96dcSApple OSS Distributions child_recurse(RECURSIONS, 2, ^{ 75*2c2f96dcSApple OSS Distributions kill(ppid, SIGUSR1); 76*2c2f96dcSApple OSS Distributions }); 77*2c2f96dcSApple OSS Distributions 78*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("child_init returned!"); 79*2c2f96dcSApple OSS Distributions} 80*2c2f96dcSApple OSS Distributions 81*2c2f96dcSApple OSS Distributionsstatic void 82*2c2f96dcSApple OSS Distributionskill_children(void) 83*2c2f96dcSApple OSS Distributions{ 84*2c2f96dcSApple OSS Distributions kill(child_pid, SIGKILL); 85*2c2f96dcSApple OSS Distributions} 86*2c2f96dcSApple OSS Distributions 87*2c2f96dcSApple OSS Distributionsstatic void * 88*2c2f96dcSApple OSS Distributionstake_stackshot(pid_t target_pid, uint64_t extra_flags, uint64_t since_timestamp) 89*2c2f96dcSApple OSS Distributions{ 90*2c2f96dcSApple OSS Distributions void *stackshot_config; 91*2c2f96dcSApple OSS Distributions int err, retries = 5; 92*2c2f96dcSApple OSS Distributions uint64_t stackshot_flags = STACKSHOT_KCDATA_FORMAT | 93*2c2f96dcSApple OSS Distributions STACKSHOT_THREAD_WAITINFO | 94*2c2f96dcSApple OSS Distributions STACKSHOT_GET_DQ; 95*2c2f96dcSApple OSS Distributions 96*2c2f96dcSApple OSS Distributions /* we should be able to verify delta stackshots */ 97*2c2f96dcSApple OSS Distributions if (since_timestamp != 0) { 98*2c2f96dcSApple OSS Distributions stackshot_flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT; 99*2c2f96dcSApple OSS Distributions } 100*2c2f96dcSApple OSS Distributions 101*2c2f96dcSApple OSS Distributions stackshot_flags |= extra_flags; 102*2c2f96dcSApple OSS Distributions 103*2c2f96dcSApple OSS Distributions stackshot_config = stackshot_config_create(); 104*2c2f96dcSApple OSS Distributions T_ASSERT_NOTNULL(stackshot_config, "allocate stackshot config"); 105*2c2f96dcSApple OSS Distributions 106*2c2f96dcSApple OSS Distributions err = stackshot_config_set_flags(stackshot_config, stackshot_flags); 107*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(err, 0, "set flags on stackshot config"); 108*2c2f96dcSApple OSS Distributions 109*2c2f96dcSApple OSS Distributions err = stackshot_config_set_pid(stackshot_config, target_pid); 110*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(err, 0, "set target pid on stackshot config"); 111*2c2f96dcSApple OSS Distributions 112*2c2f96dcSApple OSS Distributions if (since_timestamp != 0) { 113*2c2f96dcSApple OSS Distributions err = stackshot_config_set_delta_timestamp(stackshot_config, since_timestamp); 114*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(err, 0, "set prev snapshot time on stackshot config"); 115*2c2f96dcSApple OSS Distributions } 116*2c2f96dcSApple OSS Distributions 117*2c2f96dcSApple OSS Distributions while (retries > 0) { 118*2c2f96dcSApple OSS Distributions err = stackshot_capture_with_config(stackshot_config); 119*2c2f96dcSApple OSS Distributions if (err == 0) { 120*2c2f96dcSApple OSS Distributions break; 121*2c2f96dcSApple OSS Distributions } else if (err == EBUSY || err == ETIMEDOUT) { 122*2c2f96dcSApple OSS Distributions T_LOG("stackshot capture returned %d (%s)\n", err, strerror(err)); 123*2c2f96dcSApple OSS Distributions if (retries == 0) { 124*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("failed to take stackshot with error after retries: %d: %s\n", err, strerror(err)); 125*2c2f96dcSApple OSS Distributions } 126*2c2f96dcSApple OSS Distributions 127*2c2f96dcSApple OSS Distributions retries--; 128*2c2f96dcSApple OSS Distributions continue; 129*2c2f96dcSApple OSS Distributions } else { 130*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("failed to take stackshot with error: %d: %s\n", err, strerror(err)); 131*2c2f96dcSApple OSS Distributions } 132*2c2f96dcSApple OSS Distributions } 133*2c2f96dcSApple OSS Distributions 134*2c2f96dcSApple OSS Distributions return stackshot_config; 135*2c2f96dcSApple OSS Distributions} 136*2c2f96dcSApple OSS Distributions 137*2c2f96dcSApple OSS Distributionsint 138*2c2f96dcSApple OSS Distributionscheck_stackshot(void *stackshot_config, int flags) 139*2c2f96dcSApple OSS Distributions{ 140*2c2f96dcSApple OSS Distributions void *buf; 141*2c2f96dcSApple OSS Distributions uint32_t buflen, kcdata_type; 142*2c2f96dcSApple OSS Distributions kcdata_iter_t iter; 143*2c2f96dcSApple OSS Distributions NSError *nserror = nil; 144*2c2f96dcSApple OSS Distributions pid_t target_pid; 145*2c2f96dcSApple OSS Distributions int ret = 0; 146*2c2f96dcSApple OSS Distributions uint64_t expected_return_addr = 0; 147*2c2f96dcSApple OSS Distributions bool found_fault_stats = false; 148*2c2f96dcSApple OSS Distributions struct stackshot_fault_stats fault_stats = {0}; 149*2c2f96dcSApple OSS Distributions 150*2c2f96dcSApple OSS Distributions buf = stackshot_config_get_stackshot_buffer(stackshot_config); 151*2c2f96dcSApple OSS Distributions T_ASSERT_NOTNULL(buf, "stackshot buffer is not null"); 152*2c2f96dcSApple OSS Distributions buflen = stackshot_config_get_stackshot_size(stackshot_config); 153*2c2f96dcSApple OSS Distributions T_ASSERT_GT(buflen, 0, "valid stackshot buffer length"); 154*2c2f96dcSApple OSS Distributions target_pid = ((struct stackshot_config*)stackshot_config)->sc_pid; 155*2c2f96dcSApple OSS Distributions T_ASSERT_GT(target_pid, 0, "valid target_pid"); 156*2c2f96dcSApple OSS Distributions 157*2c2f96dcSApple OSS Distributions /* if need to write it to fs, do it now */ 158*2c2f96dcSApple OSS Distributions if (flags & WRITE_STACKSHOT_BUFFER_TO_TMP) { 159*2c2f96dcSApple OSS Distributions char sspath[MAXPATHLEN]; 160*2c2f96dcSApple OSS Distributions strlcpy(sspath, current_scenario_name, sizeof(sspath)); 161*2c2f96dcSApple OSS Distributions strlcat(sspath, ".kcdata", sizeof(sspath)); 162*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(dt_resultfile(sspath, sizeof(sspath)), 163*2c2f96dcSApple OSS Distributions "create result file path"); 164*2c2f96dcSApple OSS Distributions 165*2c2f96dcSApple OSS Distributions FILE *f = fopen(sspath, "w"); 166*2c2f96dcSApple OSS Distributions T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(f, 167*2c2f96dcSApple OSS Distributions "open stackshot output file"); 168*2c2f96dcSApple OSS Distributions 169*2c2f96dcSApple OSS Distributions size_t written = fwrite(buf, buflen, 1, f); 170*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(written, "wrote stackshot to file"); 171*2c2f96dcSApple OSS Distributions 172*2c2f96dcSApple OSS Distributions fclose(f); 173*2c2f96dcSApple OSS Distributions } 174*2c2f96dcSApple OSS Distributions 175*2c2f96dcSApple OSS Distributions /* begin iterating */ 176*2c2f96dcSApple OSS Distributions iter = kcdata_iter(buf, buflen); 177*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "buffer is a stackshot"); 178*2c2f96dcSApple OSS Distributions 179*2c2f96dcSApple OSS Distributions /* time to iterate */ 180*2c2f96dcSApple OSS Distributions iter = kcdata_iter_next(iter); 181*2c2f96dcSApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 182*2c2f96dcSApple OSS Distributions kcdata_type = kcdata_iter_type(iter); 183*2c2f96dcSApple OSS Distributions NSNumber *parsedPid; 184*2c2f96dcSApple OSS Distributions NSMutableDictionary *parsedContainer, *parsedThreads; 185*2c2f96dcSApple OSS Distributions 186*2c2f96dcSApple OSS Distributions if ((flags & CHECK_FOR_FAULT_STATS) != 0 && 187*2c2f96dcSApple OSS Distributions kcdata_type == STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS) { 188*2c2f96dcSApple OSS Distributions memcpy(&fault_stats, kcdata_iter_payload(iter), sizeof(fault_stats)); 189*2c2f96dcSApple OSS Distributions found_fault_stats = true; 190*2c2f96dcSApple OSS Distributions } 191*2c2f96dcSApple OSS Distributions 192*2c2f96dcSApple OSS Distributions if (kcdata_type != KCDATA_TYPE_CONTAINER_BEGIN) { 193*2c2f96dcSApple OSS Distributions continue; 194*2c2f96dcSApple OSS Distributions } 195*2c2f96dcSApple OSS Distributions 196*2c2f96dcSApple OSS Distributions if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) { 197*2c2f96dcSApple OSS Distributions continue; 198*2c2f96dcSApple OSS Distributions } 199*2c2f96dcSApple OSS Distributions 200*2c2f96dcSApple OSS Distributions parsedContainer = parseKCDataContainer(&iter, &nserror); 201*2c2f96dcSApple OSS Distributions T_ASSERT_NOTNULL(parsedContainer, "parsedContainer is not null"); 202*2c2f96dcSApple OSS Distributions T_ASSERT_NULL(nserror, "no NSError occured while parsing the kcdata container"); 203*2c2f96dcSApple OSS Distributions 204*2c2f96dcSApple OSS Distributions /* 205*2c2f96dcSApple OSS Distributions * given that we've targetted the pid, we can be sure that this 206*2c2f96dcSApple OSS Distributions * ts_pid will be the pid we expect 207*2c2f96dcSApple OSS Distributions */ 208*2c2f96dcSApple OSS Distributions parsedPid = parsedContainer[@"task_snapshots"][@"task_snapshot"][@"ts_pid"]; 209*2c2f96dcSApple OSS Distributions T_ASSERT_EQ([parsedPid intValue], target_pid, "found correct pid"); 210*2c2f96dcSApple OSS Distributions 211*2c2f96dcSApple OSS Distributions /* start parsing the threads */ 212*2c2f96dcSApple OSS Distributions parsedThreads = parsedContainer[@"task_snapshots"][@"thread_snapshots"]; 213*2c2f96dcSApple OSS Distributions for (id th_key in parsedThreads) { 214*2c2f96dcSApple OSS Distributions uint32_t frame_index = 0; 215*2c2f96dcSApple OSS Distributions 216*2c2f96dcSApple OSS Distributions if ((flags & CHECK_FOR_KERNEL_THREADS) == 0) { 217*2c2f96dcSApple OSS Distributions /* skip threads that don't have enough frames */ 218*2c2f96dcSApple OSS Distributions if ([parsedThreads[th_key][@"user_stack_frames"] count] < RECURSIONS) { 219*2c2f96dcSApple OSS Distributions continue; 220*2c2f96dcSApple OSS Distributions } 221*2c2f96dcSApple OSS Distributions 222*2c2f96dcSApple OSS Distributions for (id frame in parsedThreads[th_key][@"user_stack_frames"]) { 223*2c2f96dcSApple OSS Distributions if ((frame_index >= FIRST_RECURSIVE_FRAME) && (frame_index < (RECURSIONS - FIRST_RECURSIVE_FRAME))) { 224*2c2f96dcSApple OSS Distributions if (expected_return_addr == 0ull) { 225*2c2f96dcSApple OSS Distributions expected_return_addr = [frame[@"lr"] unsignedLongLongValue]; 226*2c2f96dcSApple OSS Distributions } else { 227*2c2f96dcSApple OSS Distributions T_QUIET; 228*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(expected_return_addr, [frame[@"lr"] unsignedLongLongValue], "expected return address found"); 229*2c2f96dcSApple OSS Distributions } 230*2c2f96dcSApple OSS Distributions } 231*2c2f96dcSApple OSS Distributions frame_index ++; 232*2c2f96dcSApple OSS Distributions } 233*2c2f96dcSApple OSS Distributions } else { 234*2c2f96dcSApple OSS Distributions T_ASSERT_NOTNULL(parsedThreads[th_key][@"kernel_stack_frames"], 235*2c2f96dcSApple OSS Distributions "found kernel stack frames"); 236*2c2f96dcSApple OSS Distributions } 237*2c2f96dcSApple OSS Distributions 238*2c2f96dcSApple OSS Distributions } 239*2c2f96dcSApple OSS Distributions } 240*2c2f96dcSApple OSS Distributions 241*2c2f96dcSApple OSS Distributions if (found_fault_stats) { 242*2c2f96dcSApple OSS Distributions T_LOG("number of pages faulted in: %d", fault_stats.sfs_pages_faulted_in); 243*2c2f96dcSApple OSS Distributions T_LOG("MATUs spent faulting: %lld", fault_stats.sfs_time_spent_faulting); 244*2c2f96dcSApple OSS Distributions T_LOG("MATUS fault time limit: %lld", fault_stats.sfs_system_max_fault_time); 245*2c2f96dcSApple OSS Distributions T_LOG("did we stop because of the limit?: %s", fault_stats.sfs_stopped_faulting ? "yes" : "no"); 246*2c2f96dcSApple OSS Distributions if (expected_return_addr != 0ull) { 247*2c2f96dcSApple OSS Distributions T_ASSERT_GT(fault_stats.sfs_pages_faulted_in, 0, "faulted at least one page in"); 248*2c2f96dcSApple OSS Distributions T_LOG("NOTE: successfully faulted in the pages"); 249*2c2f96dcSApple OSS Distributions } else { 250*2c2f96dcSApple OSS Distributions T_LOG("NOTE: We were not able to fault the stack's pages back in"); 251*2c2f96dcSApple OSS Distributions 252*2c2f96dcSApple OSS Distributions /* if we couldn't fault the pages back in, then at least verify that we tried */ 253*2c2f96dcSApple OSS Distributions T_ASSERT_GT(fault_stats.sfs_time_spent_faulting, 0ull, "spent time trying to fault"); 254*2c2f96dcSApple OSS Distributions } 255*2c2f96dcSApple OSS Distributions } else if ((flags & CHECK_FOR_KERNEL_THREADS) == 0) { 256*2c2f96dcSApple OSS Distributions T_ASSERT_NE(expected_return_addr, 0ull, "found child thread with recursions"); 257*2c2f96dcSApple OSS Distributions } 258*2c2f96dcSApple OSS Distributions 259*2c2f96dcSApple OSS Distributions if (flags & CHECK_FOR_FAULT_STATS) { 260*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(found_fault_stats, true, "found fault stats"); 261*2c2f96dcSApple OSS Distributions } 262*2c2f96dcSApple OSS Distributions 263*2c2f96dcSApple OSS Distributions return ret; 264*2c2f96dcSApple OSS Distributions} 265*2c2f96dcSApple OSS Distributions 266*2c2f96dcSApple OSS Distributionsvoid 267*2c2f96dcSApple OSS Distributionschild_init(void) 268*2c2f96dcSApple OSS Distributions{ 269*2c2f96dcSApple OSS Distributions#if !TARGET_OS_OSX 270*2c2f96dcSApple OSS Distributions int freeze_state; 271*2c2f96dcSApple OSS Distributions#endif /* !TARGET_OS_OSX */ 272*2c2f96dcSApple OSS Distributions pid_t pid = getpid(); 273*2c2f96dcSApple OSS Distributions char padding[16 * 1024]; 274*2c2f96dcSApple OSS Distributions __asm__ volatile(""::"r"(padding)); 275*2c2f96dcSApple OSS Distributions 276*2c2f96dcSApple OSS Distributions T_LOG("child pid: %d\n", pid); 277*2c2f96dcSApple OSS Distributions 278*2c2f96dcSApple OSS Distributions#if !TARGET_OS_OSX 279*2c2f96dcSApple OSS Distributions /* allow us to be frozen */ 280*2c2f96dcSApple OSS Distributions freeze_state = memorystatus_control(MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE, pid, 0, NULL, 0); 281*2c2f96dcSApple OSS Distributions if (freeze_state == 0) { 282*2c2f96dcSApple OSS Distributions T_LOG("CHILD was found to be UNFREEZABLE, enabling freezing."); 283*2c2f96dcSApple OSS Distributions memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE, pid, 1, NULL, 0); 284*2c2f96dcSApple OSS Distributions freeze_state = memorystatus_control(MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE, pid, 0, NULL, 0); 285*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(freeze_state, 1, "successfully set freezeability"); 286*2c2f96dcSApple OSS Distributions } 287*2c2f96dcSApple OSS Distributions#else 288*2c2f96dcSApple OSS Distributions T_LOG("Cannot change freezeability as freezing is only available on embedded devices"); 289*2c2f96dcSApple OSS Distributions#endif /* !TARGET_OS_OSX */ 290*2c2f96dcSApple OSS Distributions 291*2c2f96dcSApple OSS Distributions /* 292*2c2f96dcSApple OSS Distributions * recurse a bunch of times to generate predictable data in the stackshot, 293*2c2f96dcSApple OSS Distributions * then send SIGUSR1 to the parent to let it know that we are done. 294*2c2f96dcSApple OSS Distributions */ 295*2c2f96dcSApple OSS Distributions child_recurse(RECURSIONS, 0, ^{ 296*2c2f96dcSApple OSS Distributions kill(getppid(), SIGUSR1); 297*2c2f96dcSApple OSS Distributions }); 298*2c2f96dcSApple OSS Distributions 299*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("child_recurse returned, but it must not?"); 300*2c2f96dcSApple OSS Distributions} 301*2c2f96dcSApple OSS Distributions 302*2c2f96dcSApple OSS Distributionsvoid 303*2c2f96dcSApple OSS Distributionsparent_helper_singleproc(int spin) 304*2c2f96dcSApple OSS Distributions{ 305*2c2f96dcSApple OSS Distributions dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0); 306*2c2f96dcSApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.basic_sp", NULL); 307*2c2f96dcSApple OSS Distributions void *stackshot_config; 308*2c2f96dcSApple OSS Distributions 309*2c2f96dcSApple OSS Distributions dispatch_async(dq, ^{ 310*2c2f96dcSApple OSS Distributions char padding[16 * 1024]; 311*2c2f96dcSApple OSS Distributions __asm__ volatile(""::"r"(padding)); 312*2c2f96dcSApple OSS Distributions 313*2c2f96dcSApple OSS Distributions child_recurse(RECURSIONS, spin, ^{ 314*2c2f96dcSApple OSS Distributions dispatch_semaphore_signal(child_done_sema); 315*2c2f96dcSApple OSS Distributions }); 316*2c2f96dcSApple OSS Distributions }); 317*2c2f96dcSApple OSS Distributions 318*2c2f96dcSApple OSS Distributions dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER); 319*2c2f96dcSApple OSS Distributions T_LOG("done waiting for child"); 320*2c2f96dcSApple OSS Distributions 321*2c2f96dcSApple OSS Distributions /* take the stackshot and parse it */ 322*2c2f96dcSApple OSS Distributions stackshot_config = take_stackshot(getpid(), 0, 0); 323*2c2f96dcSApple OSS Distributions 324*2c2f96dcSApple OSS Distributions /* check that the stackshot has the stack frames */ 325*2c2f96dcSApple OSS Distributions check_stackshot(stackshot_config, 0); 326*2c2f96dcSApple OSS Distributions 327*2c2f96dcSApple OSS Distributions T_LOG("done!"); 328*2c2f96dcSApple OSS Distributions} 329*2c2f96dcSApple OSS Distributions 330*2c2f96dcSApple OSS DistributionsT_DECL(basic, "test that no-fault stackshot works correctly") 331*2c2f96dcSApple OSS Distributions{ 332*2c2f96dcSApple OSS Distributions char path[PATH_MAX]; 333*2c2f96dcSApple OSS Distributions uint32_t path_size = sizeof(path); 334*2c2f96dcSApple OSS Distributions char *args[] = { path, "-n", "simple_child_process", NULL }; 335*2c2f96dcSApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.basic", NULL); 336*2c2f96dcSApple OSS Distributions dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0); 337*2c2f96dcSApple OSS Distributions dispatch_source_t child_sig_src; 338*2c2f96dcSApple OSS Distributions void *stackshot_config; 339*2c2f96dcSApple OSS Distributions 340*2c2f96dcSApple OSS Distributions current_scenario_name = __func__; 341*2c2f96dcSApple OSS Distributions 342*2c2f96dcSApple OSS Distributions T_LOG("parent pid: %d\n", getpid()); 343*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 344*2c2f96dcSApple OSS Distributions 345*2c2f96dcSApple OSS Distributions /* check if we can run the child successfully */ 346*2c2f96dcSApple OSS Distributions#if !TARGET_OS_OSX 347*2c2f96dcSApple OSS Distributions int freeze_state = memorystatus_control(MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE, getpid(), 0, NULL, 0); 348*2c2f96dcSApple OSS Distributions if (freeze_state == -1) { 349*2c2f96dcSApple OSS Distributions T_SKIP("This device doesn't have CONFIG_FREEZE enabled."); 350*2c2f96dcSApple OSS Distributions } 351*2c2f96dcSApple OSS Distributions#endif 352*2c2f96dcSApple OSS Distributions 353*2c2f96dcSApple OSS Distributions /* setup signal handling */ 354*2c2f96dcSApple OSS Distributions signal(SIGUSR1, SIG_IGN); 355*2c2f96dcSApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 356*2c2f96dcSApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ 357*2c2f96dcSApple OSS Distributions dispatch_semaphore_signal(child_done_sema); 358*2c2f96dcSApple OSS Distributions }); 359*2c2f96dcSApple OSS Distributions dispatch_activate(child_sig_src); 360*2c2f96dcSApple OSS Distributions 361*2c2f96dcSApple OSS Distributions /* create the child process */ 362*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&child_pid, args, false, NULL, NULL), "child launched"); 363*2c2f96dcSApple OSS Distributions T_ATEND(kill_children); 364*2c2f96dcSApple OSS Distributions 365*2c2f96dcSApple OSS Distributions /* wait until the child has recursed enough */ 366*2c2f96dcSApple OSS Distributions dispatch_semaphore_wait(child_done_sema, dispatch_time(DISPATCH_TIME_NOW, 10 /*seconds*/ * 1000000000ULL)); 367*2c2f96dcSApple OSS Distributions 368*2c2f96dcSApple OSS Distributions T_LOG("child finished, parent executing"); 369*2c2f96dcSApple OSS Distributions 370*2c2f96dcSApple OSS Distributions /* take the stackshot and parse it */ 371*2c2f96dcSApple OSS Distributions stackshot_config = take_stackshot(child_pid, 0, 0); 372*2c2f96dcSApple OSS Distributions 373*2c2f96dcSApple OSS Distributions /* check that the stackshot has the stack frames */ 374*2c2f96dcSApple OSS Distributions check_stackshot(stackshot_config, 0); 375*2c2f96dcSApple OSS Distributions 376*2c2f96dcSApple OSS Distributions T_LOG("all done, killing child"); 377*2c2f96dcSApple OSS Distributions 378*2c2f96dcSApple OSS Distributions /* tell the child to quit */ 379*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(child_pid, SIGTERM), "killed child"); 380*2c2f96dcSApple OSS Distributions} 381*2c2f96dcSApple OSS Distributions 382*2c2f96dcSApple OSS DistributionsT_DECL(basic_singleproc, "test that no-fault stackshot works correctly in single process setting") 383*2c2f96dcSApple OSS Distributions{ 384*2c2f96dcSApple OSS Distributions current_scenario_name = __func__; 385*2c2f96dcSApple OSS Distributions parent_helper_singleproc(0); 386*2c2f96dcSApple OSS Distributions} 387*2c2f96dcSApple OSS Distributions 388*2c2f96dcSApple OSS DistributionsT_DECL(basic_singleproc_spin, "test that no-fault stackshot works correctly in single process setting with spinning") 389*2c2f96dcSApple OSS Distributions{ 390*2c2f96dcSApple OSS Distributions current_scenario_name = __func__; 391*2c2f96dcSApple OSS Distributions parent_helper_singleproc(1); 392*2c2f96dcSApple OSS Distributions} 393*2c2f96dcSApple OSS Distributions 394*2c2f96dcSApple OSS DistributionsT_DECL(fault, "test that faulting stackshots work correctly") 395*2c2f96dcSApple OSS Distributions{ 396*2c2f96dcSApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_fault_accuracy", NULL); 397*2c2f96dcSApple OSS Distributions dispatch_source_t child_sig_src; 398*2c2f96dcSApple OSS Distributions dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0); 399*2c2f96dcSApple OSS Distributions void *stackshot_config; 400*2c2f96dcSApple OSS Distributions int oldftm, newval = 1, freeze_enabled, oldratio, newratio = 0; 401*2c2f96dcSApple OSS Distributions size_t oldlen = sizeof(oldftm), fe_len = sizeof(freeze_enabled), ratiolen = sizeof(oldratio); 402*2c2f96dcSApple OSS Distributions char path[PATH_MAX]; 403*2c2f96dcSApple OSS Distributions uint32_t path_size = sizeof(path); 404*2c2f96dcSApple OSS Distributions char *args[] = { path, "-n", "simple_child_process", NULL }; 405*2c2f96dcSApple OSS Distributions 406*2c2f96dcSApple OSS Distributions current_scenario_name = __func__; 407*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 408*2c2f96dcSApple OSS Distributions 409*2c2f96dcSApple OSS Distributions#if TARGET_OS_OSX 410*2c2f96dcSApple OSS Distributions T_SKIP("freezing is not available on macOS"); 411*2c2f96dcSApple OSS Distributions#endif /* TARGET_OS_OSX */ 412*2c2f96dcSApple OSS Distributions 413*2c2f96dcSApple OSS Distributions /* Try checking if freezing is enabled at all */ 414*2c2f96dcSApple OSS Distributions if (sysctlbyname("vm.freeze_enabled", &freeze_enabled, &fe_len, NULL, 0) == -1) { 415*2c2f96dcSApple OSS Distributions if (errno == ENOENT) { 416*2c2f96dcSApple OSS Distributions T_SKIP("This device doesn't have CONFIG_FREEZE enabled."); 417*2c2f96dcSApple OSS Distributions } else { 418*2c2f96dcSApple OSS Distributions T_FAIL("failed to query vm.freeze_enabled, errno: %d", errno); 419*2c2f96dcSApple OSS Distributions } 420*2c2f96dcSApple OSS Distributions } 421*2c2f96dcSApple OSS Distributions 422*2c2f96dcSApple OSS Distributions if (!freeze_enabled) { 423*2c2f96dcSApple OSS Distributions T_SKIP("Freeze is not enabled, skipping test."); 424*2c2f96dcSApple OSS Distributions } 425*2c2f96dcSApple OSS Distributions 426*2c2f96dcSApple OSS Distributions /* signal handling */ 427*2c2f96dcSApple OSS Distributions signal(SIGUSR1, SIG_IGN); 428*2c2f96dcSApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 429*2c2f96dcSApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ 430*2c2f96dcSApple OSS Distributions dispatch_semaphore_signal(child_done_sema); 431*2c2f96dcSApple OSS Distributions }); 432*2c2f96dcSApple OSS Distributions dispatch_activate(child_sig_src); 433*2c2f96dcSApple OSS Distributions 434*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&child_pid, args, false, NULL, NULL), "child launched"); 435*2c2f96dcSApple OSS Distributions T_ATEND(kill_children); 436*2c2f96dcSApple OSS Distributions 437*2c2f96dcSApple OSS Distributions dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER); 438*2c2f96dcSApple OSS Distributions 439*2c2f96dcSApple OSS Distributions /* keep processes in memory */ 440*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_to_memory", &oldftm, &oldlen, &newval, sizeof(newval)), 441*2c2f96dcSApple OSS Distributions "disabled freezing to disk"); 442*2c2f96dcSApple OSS Distributions 443*2c2f96dcSApple OSS Distributions /* set the ratio to zero */ 444*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_private_shared_pages_ratio", &oldratio, &ratiolen, &newratio, sizeof(newratio)), "disabled private:shared ratio checking"); 445*2c2f96dcSApple OSS Distributions 446*2c2f96dcSApple OSS Distributions /* freeze the child */ 447*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze", NULL, 0, &child_pid, sizeof(child_pid)), 448*2c2f96dcSApple OSS Distributions "froze child"); 449*2c2f96dcSApple OSS Distributions 450*2c2f96dcSApple OSS Distributions /* Sleep to allow the compressor to finish compressing the child */ 451*2c2f96dcSApple OSS Distributions sleep(5); 452*2c2f96dcSApple OSS Distributions 453*2c2f96dcSApple OSS Distributions /* take the stackshot and parse it */ 454*2c2f96dcSApple OSS Distributions stackshot_config = take_stackshot(child_pid, STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING, 0); 455*2c2f96dcSApple OSS Distributions 456*2c2f96dcSApple OSS Distributions /* check that the stackshot has the stack frames */ 457*2c2f96dcSApple OSS Distributions check_stackshot(stackshot_config, CHECK_FOR_FAULT_STATS); 458*2c2f96dcSApple OSS Distributions 459*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_to_memory", NULL, 0, &oldftm, sizeof(oldftm)), 460*2c2f96dcSApple OSS Distributions "reset freezing to disk"); 461*2c2f96dcSApple OSS Distributions 462*2c2f96dcSApple OSS Distributions /* reset the private:shared ratio */ 463*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.memorystatus_freeze_private_shared_pages_ratio", NULL, 0, &oldratio, sizeof(oldratio)), "reset private:shared ratio"); 464*2c2f96dcSApple OSS Distributions 465*2c2f96dcSApple OSS Distributions T_LOG("all done, killing child"); 466*2c2f96dcSApple OSS Distributions 467*2c2f96dcSApple OSS Distributions /* tell the child to quit */ 468*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(child_pid, SIGTERM), "killed child"); 469*2c2f96dcSApple OSS Distributions} 470*2c2f96dcSApple OSS Distributions 471*2c2f96dcSApple OSS DistributionsT_DECL(fault_singleproc, "test that faulting stackshots work correctly in a single process setting") 472*2c2f96dcSApple OSS Distributions{ 473*2c2f96dcSApple OSS Distributions dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0); 474*2c2f96dcSApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.fault_sp", NULL); 475*2c2f96dcSApple OSS Distributions void *stackshot_config; 476*2c2f96dcSApple OSS Distributions __block pthread_t child_thread; 477*2c2f96dcSApple OSS Distributions char *child_stack; 478*2c2f96dcSApple OSS Distributions size_t child_stacklen; 479*2c2f96dcSApple OSS Distributions 480*2c2f96dcSApple OSS Distributions#if !TARGET_OS_OSX 481*2c2f96dcSApple OSS Distributions T_SKIP("madvise(..., ..., MADV_PAGEOUT) is not available on embedded platforms"); 482*2c2f96dcSApple OSS Distributions#endif /* !TARGET_OS_OSX */ 483*2c2f96dcSApple OSS Distributions 484*2c2f96dcSApple OSS Distributions dispatch_async(dq, ^{ 485*2c2f96dcSApple OSS Distributions char padding[16 * 1024]; 486*2c2f96dcSApple OSS Distributions __asm__ volatile(""::"r"(padding)); 487*2c2f96dcSApple OSS Distributions 488*2c2f96dcSApple OSS Distributions child_recurse(RECURSIONS, 0, ^{ 489*2c2f96dcSApple OSS Distributions child_thread = pthread_self(); 490*2c2f96dcSApple OSS Distributions dispatch_semaphore_signal(child_done_sema); 491*2c2f96dcSApple OSS Distributions }); 492*2c2f96dcSApple OSS Distributions }); 493*2c2f96dcSApple OSS Distributions 494*2c2f96dcSApple OSS Distributions dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER); 495*2c2f96dcSApple OSS Distributions T_LOG("done waiting for child"); 496*2c2f96dcSApple OSS Distributions 497*2c2f96dcSApple OSS Distributions child_stack = pthread_get_stackaddr_np(child_thread); 498*2c2f96dcSApple OSS Distributions child_stacklen = pthread_get_stacksize_np(child_thread); 499*2c2f96dcSApple OSS Distributions child_stack -= child_stacklen; 500*2c2f96dcSApple OSS Distributions T_LOG("child stack: [0x%p - 0x%p]: 0x%zu bytes", (void *)child_stack, 501*2c2f96dcSApple OSS Distributions (void *)(child_stack + child_stacklen), child_stacklen); 502*2c2f96dcSApple OSS Distributions 503*2c2f96dcSApple OSS Distributions /* paging out the child */ 504*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(madvise(child_stack, child_stacklen, MADV_PAGEOUT), "paged out via madvise(2) the child stack"); 505*2c2f96dcSApple OSS Distributions 506*2c2f96dcSApple OSS Distributions /* take the stackshot and parse it */ 507*2c2f96dcSApple OSS Distributions stackshot_config = take_stackshot(getpid(), STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING, 0); 508*2c2f96dcSApple OSS Distributions 509*2c2f96dcSApple OSS Distributions /* check that the stackshot has the stack frames */ 510*2c2f96dcSApple OSS Distributions check_stackshot(stackshot_config, CHECK_FOR_FAULT_STATS); 511*2c2f96dcSApple OSS Distributions 512*2c2f96dcSApple OSS Distributions T_LOG("done!"); 513*2c2f96dcSApple OSS Distributions} 514*2c2f96dcSApple OSS Distributions 515*2c2f96dcSApple OSS DistributionsT_DECL(zombie, "test that threads wedged in the kernel can be stackshot'd") 516*2c2f96dcSApple OSS Distributions{ 517*2c2f96dcSApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot_accuracy.zombie", NULL); 518*2c2f96dcSApple OSS Distributions dispatch_semaphore_t child_done_sema = dispatch_semaphore_create(0); 519*2c2f96dcSApple OSS Distributions dispatch_source_t child_sig_src; 520*2c2f96dcSApple OSS Distributions void *stackshot_config; 521*2c2f96dcSApple OSS Distributions char path[PATH_MAX]; 522*2c2f96dcSApple OSS Distributions uint32_t path_size = sizeof(path); 523*2c2f96dcSApple OSS Distributions char *args[] = { path, "-n", "sid_child_process", NULL }; 524*2c2f96dcSApple OSS Distributions 525*2c2f96dcSApple OSS Distributions current_scenario_name = __func__; 526*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 527*2c2f96dcSApple OSS Distributions 528*2c2f96dcSApple OSS Distributions T_LOG("parent pid: %d\n", getpid()); 529*2c2f96dcSApple OSS Distributions 530*2c2f96dcSApple OSS Distributions /* setup signal handling */ 531*2c2f96dcSApple OSS Distributions signal(SIGUSR1, SIG_IGN); 532*2c2f96dcSApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 533*2c2f96dcSApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ 534*2c2f96dcSApple OSS Distributions dispatch_semaphore_signal(child_done_sema); 535*2c2f96dcSApple OSS Distributions }); 536*2c2f96dcSApple OSS Distributions dispatch_activate(child_sig_src); 537*2c2f96dcSApple OSS Distributions 538*2c2f96dcSApple OSS Distributions /* create the child process */ 539*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&child_pid, args, false, NULL, NULL), "child launched"); 540*2c2f96dcSApple OSS Distributions T_ATEND(kill_children); 541*2c2f96dcSApple OSS Distributions 542*2c2f96dcSApple OSS Distributions /* wait until the child has recursed enough */ 543*2c2f96dcSApple OSS Distributions dispatch_semaphore_wait(child_done_sema, DISPATCH_TIME_FOREVER); 544*2c2f96dcSApple OSS Distributions 545*2c2f96dcSApple OSS Distributions T_LOG("child finished, parent executing. invoking jetsam"); 546*2c2f96dcSApple OSS Distributions 547*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(memorystatus_control(MEMORYSTATUS_CMD_TEST_JETSAM, child_pid, 0, 0, 0), 548*2c2f96dcSApple OSS Distributions "jetsam'd the child"); 549*2c2f96dcSApple OSS Distributions 550*2c2f96dcSApple OSS Distributions /* Sleep to allow the target process to become zombified */ 551*2c2f96dcSApple OSS Distributions sleep(1); 552*2c2f96dcSApple OSS Distributions 553*2c2f96dcSApple OSS Distributions /* take the stackshot and parse it */ 554*2c2f96dcSApple OSS Distributions stackshot_config = take_stackshot(child_pid, 0, 0); 555*2c2f96dcSApple OSS Distributions 556*2c2f96dcSApple OSS Distributions /* check that the stackshot has the stack frames */ 557*2c2f96dcSApple OSS Distributions check_stackshot(stackshot_config, CHECK_FOR_KERNEL_THREADS); 558*2c2f96dcSApple OSS Distributions 559*2c2f96dcSApple OSS Distributions T_LOG("all done, unwedging and killing child"); 560*2c2f96dcSApple OSS Distributions 561*2c2f96dcSApple OSS Distributions int v = 1; 562*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.unwedge_thread", NULL, NULL, &v, sizeof(v)), 563*2c2f96dcSApple OSS Distributions "unwedged child"); 564*2c2f96dcSApple OSS Distributions 565*2c2f96dcSApple OSS Distributions /* tell the child to quit */ 566*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(child_pid, SIGTERM), "killed child"); 567*2c2f96dcSApple OSS Distributions} 568