1*27b03b36SApple OSS Distributions#include <darwintest.h> 2*27b03b36SApple OSS Distributions#include <darwintest_utils.h> 3*27b03b36SApple OSS Distributions#include <darwintest_multiprocess.h> 4*27b03b36SApple OSS Distributions#include <kern/debug.h> 5*27b03b36SApple OSS Distributions#include <kern/kern_cdata.h> 6*27b03b36SApple OSS Distributions#include <kern/block_hint.h> 7*27b03b36SApple OSS Distributions#include <kdd.h> 8*27b03b36SApple OSS Distributions#include <libproc.h> 9*27b03b36SApple OSS Distributions#include <mach-o/dyld.h> 10*27b03b36SApple OSS Distributions#include <mach-o/dyld_images.h> 11*27b03b36SApple OSS Distributions#include <mach-o/dyld_priv.h> 12*27b03b36SApple OSS Distributions#include <sys/syscall.h> 13*27b03b36SApple OSS Distributions#include <sys/stackshot.h> 14*27b03b36SApple OSS Distributions#include <uuid/uuid.h> 15*27b03b36SApple OSS Distributions#include <servers/bootstrap.h> 16*27b03b36SApple OSS Distributions#include <pthread/workqueue_private.h> 17*27b03b36SApple OSS Distributions#include <dispatch/private.h> 18*27b03b36SApple OSS Distributions#include <stdalign.h> 19*27b03b36SApple OSS Distributions#import <zlib.h> 20*27b03b36SApple OSS Distributions 21*27b03b36SApple OSS DistributionsT_GLOBAL_META( 22*27b03b36SApple OSS Distributions T_META_NAMESPACE("xnu.stackshot"), 23*27b03b36SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 24*27b03b36SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("stackshot"), 25*27b03b36SApple OSS Distributions T_META_OWNER("jonathan_w_adams"), 26*27b03b36SApple OSS Distributions T_META_CHECK_LEAKS(false), 27*27b03b36SApple OSS Distributions T_META_ASROOT(true) 28*27b03b36SApple OSS Distributions ); 29*27b03b36SApple OSS Distributions 30*27b03b36SApple OSS Distributionsstatic const char *current_process_name(void); 31*27b03b36SApple OSS Distributionsstatic void verify_stackshot_sharedcache_layout(struct dyld_uuid_info_64 *uuids, uint32_t uuid_count); 32*27b03b36SApple OSS Distributionsstatic void parse_stackshot(uint64_t stackshot_parsing_flags, void *ssbuf, size_t sslen, NSDictionary *extra); 33*27b03b36SApple OSS Distributionsstatic void parse_thread_group_stackshot(void **sbuf, size_t sslen); 34*27b03b36SApple OSS Distributionsstatic uint64_t stackshot_timestamp(void *ssbuf, size_t sslen); 35*27b03b36SApple OSS Distributionsstatic void initialize_thread(void); 36*27b03b36SApple OSS Distributions 37*27b03b36SApple OSS Distributionsstatic uint64_t global_flags = 0; 38*27b03b36SApple OSS Distributions 39*27b03b36SApple OSS Distributions#define DEFAULT_STACKSHOT_BUFFER_SIZE (1024 * 1024) 40*27b03b36SApple OSS Distributions#define MAX_STACKSHOT_BUFFER_SIZE (6 * 1024 * 1024) 41*27b03b36SApple OSS Distributions 42*27b03b36SApple OSS Distributions#define SRP_SERVICE_NAME "com.apple.xnu.test.stackshot.special_reply_port" 43*27b03b36SApple OSS Distributions 44*27b03b36SApple OSS Distributions/* bit flags for parse_stackshot */ 45*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_DELTA 0x01 46*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_ZOMBIE 0x02 47*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_SHAREDCACHE_LAYOUT 0x04 48*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_DISPATCH_QUEUE_LABEL 0x08 49*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_TURNSTILEINFO 0x10 50*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_POSTEXEC 0x20 51*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_WAITINFO_CSEG 0x40 52*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_WAITINFO_SRP 0x80 53*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_TRANSLATED 0x100 54*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_SHAREDCACHE_FLAGS 0x200 55*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_EXEC_INPROGRESS 0x400 56*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_TRANSITIONING 0x800 57*27b03b36SApple OSS Distributions#define PARSE_STACKSHOT_ASYNCSTACK 0x1000 58*27b03b36SApple OSS Distributions 59*27b03b36SApple OSS Distributions/* keys for 'extra' dictionary for parse_stackshot */ 60*27b03b36SApple OSS Distributionsstatic const NSString* zombie_child_pid_key = @"zombie_child_pid"; // -> @(pid), required for PARSE_STACKSHOT_ZOMBIE 61*27b03b36SApple OSS Distributionsstatic const NSString* postexec_child_unique_pid_key = @"postexec_child_unique_pid"; // -> @(unique_pid), required for PARSE_STACKSHOT_POSTEXEC 62*27b03b36SApple OSS Distributionsstatic const NSString* cseg_expected_threadid_key = @"cseg_expected_threadid"; // -> @(tid), required for PARSE_STACKSHOT_WAITINFO_CSEG 63*27b03b36SApple OSS Distributionsstatic const NSString* srp_expected_threadid_key = @"srp_expected_threadid"; // -> @(tid), this or ..._pid required for PARSE_STACKSHOT_WAITINFO_SRP 64*27b03b36SApple OSS Distributionsstatic const NSString* srp_expected_pid_key = @"srp_expected_pid"; // -> @(pid), this or ..._threadid required for PARSE_STACKSHOT_WAITINFO_SRP 65*27b03b36SApple OSS Distributionsstatic const NSString* translated_child_pid_key = @"translated_child_pid"; // -> @(pid), required for PARSE_STACKSHOT_TRANSLATED 66*27b03b36SApple OSS Distributionsstatic const NSString* sharedcache_child_pid_key = @"sharedcache_child_pid"; // @(pid), required for PARSE_STACKSHOT_SHAREDCACHE_FLAGS 67*27b03b36SApple OSS Distributionsstatic const NSString* sharedcache_child_sameaddr_key = @"sharedcache_child_sameaddr"; // @(0 or 1), required for PARSE_STACKSHOT_SHAREDCACHE_FLAGS 68*27b03b36SApple OSS Distributionsstatic const NSString* exec_inprogress_pid_key = @"exec_inprogress_pid"; 69*27b03b36SApple OSS Distributionsstatic const NSString* exec_inprogress_found_key = @"exec_inprogress_found"; // callback when inprogress is found 70*27b03b36SApple OSS Distributionsstatic const NSString* transitioning_pid_key = @"transitioning_task_pid"; // -> @(pid), required for PARSE_STACKSHOT_TRANSITIONING 71*27b03b36SApple OSS Distributionsstatic const NSString* asyncstack_expected_threadid_key = @"asyncstack_expected_threadid"; // -> @(tid), required for PARSE_STACKSHOT_ASYNCSTACK 72*27b03b36SApple OSS Distributionsstatic const NSString* asyncstack_expected_stack_key = @"asyncstack_expected_stack"; // -> @[pc...]), expected PCs for asyncstack 73*27b03b36SApple OSS Distributions 74*27b03b36SApple OSS Distributions#define TEST_STACKSHOT_QUEUE_LABEL "houston.we.had.a.problem" 75*27b03b36SApple OSS Distributions#define TEST_STACKSHOT_QUEUE_LABEL_LENGTH sizeof(TEST_STACKSHOT_QUEUE_LABEL) 76*27b03b36SApple OSS Distributions 77*27b03b36SApple OSS DistributionsT_DECL(microstackshots, "test the microstackshot syscall") 78*27b03b36SApple OSS Distributions{ 79*27b03b36SApple OSS Distributions void *buf = NULL; 80*27b03b36SApple OSS Distributions unsigned int size = DEFAULT_STACKSHOT_BUFFER_SIZE; 81*27b03b36SApple OSS Distributions 82*27b03b36SApple OSS Distributions while (1) { 83*27b03b36SApple OSS Distributions buf = malloc(size); 84*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(buf, "allocated stackshot buffer"); 85*27b03b36SApple OSS Distributions 86*27b03b36SApple OSS Distributions#pragma clang diagnostic push 87*27b03b36SApple OSS Distributions#pragma clang diagnostic ignored "-Wdeprecated-declarations" 88*27b03b36SApple OSS Distributions int len = syscall(SYS_microstackshot, buf, size, 89*27b03b36SApple OSS Distributions (uint32_t) STACKSHOT_GET_MICROSTACKSHOT); 90*27b03b36SApple OSS Distributions#pragma clang diagnostic pop 91*27b03b36SApple OSS Distributions if (len == ENOSYS) { 92*27b03b36SApple OSS Distributions T_SKIP("microstackshot syscall failed, likely not compiled with CONFIG_TELEMETRY"); 93*27b03b36SApple OSS Distributions } 94*27b03b36SApple OSS Distributions if (len == -1 && errno == ENOSPC) { 95*27b03b36SApple OSS Distributions /* syscall failed because buffer wasn't large enough, try again */ 96*27b03b36SApple OSS Distributions free(buf); 97*27b03b36SApple OSS Distributions buf = NULL; 98*27b03b36SApple OSS Distributions size *= 2; 99*27b03b36SApple OSS Distributions T_ASSERT_LE(size, (unsigned int)MAX_STACKSHOT_BUFFER_SIZE, 100*27b03b36SApple OSS Distributions "growing stackshot buffer to sane size"); 101*27b03b36SApple OSS Distributions continue; 102*27b03b36SApple OSS Distributions } 103*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(len, "called microstackshot syscall"); 104*27b03b36SApple OSS Distributions break; 105*27b03b36SApple OSS Distributions } 106*27b03b36SApple OSS Distributions 107*27b03b36SApple OSS Distributions T_EXPECT_EQ(*(uint32_t *)buf, 108*27b03b36SApple OSS Distributions (uint32_t)STACKSHOT_MICRO_SNAPSHOT_MAGIC, 109*27b03b36SApple OSS Distributions "magic value for microstackshot matches"); 110*27b03b36SApple OSS Distributions 111*27b03b36SApple OSS Distributions free(buf); 112*27b03b36SApple OSS Distributions} 113*27b03b36SApple OSS Distributions 114*27b03b36SApple OSS Distributionsstruct scenario { 115*27b03b36SApple OSS Distributions const char *name; 116*27b03b36SApple OSS Distributions uint64_t flags; 117*27b03b36SApple OSS Distributions bool quiet; 118*27b03b36SApple OSS Distributions bool should_fail; 119*27b03b36SApple OSS Distributions bool maybe_unsupported; 120*27b03b36SApple OSS Distributions bool maybe_enomem; 121*27b03b36SApple OSS Distributions bool no_recordfile; 122*27b03b36SApple OSS Distributions pid_t target_pid; 123*27b03b36SApple OSS Distributions uint64_t since_timestamp; 124*27b03b36SApple OSS Distributions uint32_t size_hint; 125*27b03b36SApple OSS Distributions dt_stat_time_t timer; 126*27b03b36SApple OSS Distributions}; 127*27b03b36SApple OSS Distributions 128*27b03b36SApple OSS Distributionsstatic void 129*27b03b36SApple OSS Distributionsquiet(struct scenario *scenario) 130*27b03b36SApple OSS Distributions{ 131*27b03b36SApple OSS Distributions if (scenario->timer || scenario->quiet) { 132*27b03b36SApple OSS Distributions T_QUIET; 133*27b03b36SApple OSS Distributions } 134*27b03b36SApple OSS Distributions} 135*27b03b36SApple OSS Distributions 136*27b03b36SApple OSS Distributionsstatic void 137*27b03b36SApple OSS Distributionstake_stackshot(struct scenario *scenario, bool compress_ok, void (^cb)(void *buf, size_t size)) 138*27b03b36SApple OSS Distributions{ 139*27b03b36SApple OSS Distributionsstart: 140*27b03b36SApple OSS Distributions initialize_thread(); 141*27b03b36SApple OSS Distributions 142*27b03b36SApple OSS Distributions void *config = stackshot_config_create(); 143*27b03b36SApple OSS Distributions quiet(scenario); 144*27b03b36SApple OSS Distributions T_ASSERT_NOTNULL(config, "created stackshot config"); 145*27b03b36SApple OSS Distributions 146*27b03b36SApple OSS Distributions int ret = stackshot_config_set_flags(config, scenario->flags | global_flags); 147*27b03b36SApple OSS Distributions quiet(scenario); 148*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set flags %#llx on stackshot config", scenario->flags); 149*27b03b36SApple OSS Distributions 150*27b03b36SApple OSS Distributions if (scenario->size_hint > 0) { 151*27b03b36SApple OSS Distributions ret = stackshot_config_set_size_hint(config, scenario->size_hint); 152*27b03b36SApple OSS Distributions quiet(scenario); 153*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set size hint %" PRIu32 " on stackshot config", 154*27b03b36SApple OSS Distributions scenario->size_hint); 155*27b03b36SApple OSS Distributions } 156*27b03b36SApple OSS Distributions 157*27b03b36SApple OSS Distributions if (scenario->target_pid > 0) { 158*27b03b36SApple OSS Distributions ret = stackshot_config_set_pid(config, scenario->target_pid); 159*27b03b36SApple OSS Distributions quiet(scenario); 160*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set target pid %d on stackshot config", 161*27b03b36SApple OSS Distributions scenario->target_pid); 162*27b03b36SApple OSS Distributions } 163*27b03b36SApple OSS Distributions 164*27b03b36SApple OSS Distributions if (scenario->since_timestamp > 0) { 165*27b03b36SApple OSS Distributions ret = stackshot_config_set_delta_timestamp(config, scenario->since_timestamp); 166*27b03b36SApple OSS Distributions quiet(scenario); 167*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set since timestamp %" PRIu64 " on stackshot config", 168*27b03b36SApple OSS Distributions scenario->since_timestamp); 169*27b03b36SApple OSS Distributions } 170*27b03b36SApple OSS Distributions 171*27b03b36SApple OSS Distributions int retries_remaining = 5; 172*27b03b36SApple OSS Distributions 173*27b03b36SApple OSS Distributionsretry: ; 174*27b03b36SApple OSS Distributions uint64_t start_time = mach_absolute_time(); 175*27b03b36SApple OSS Distributions ret = stackshot_capture_with_config(config); 176*27b03b36SApple OSS Distributions uint64_t end_time = mach_absolute_time(); 177*27b03b36SApple OSS Distributions 178*27b03b36SApple OSS Distributions if (scenario->should_fail) { 179*27b03b36SApple OSS Distributions T_EXPECTFAIL; 180*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config"); 181*27b03b36SApple OSS Distributions return; 182*27b03b36SApple OSS Distributions } 183*27b03b36SApple OSS Distributions 184*27b03b36SApple OSS Distributions if (ret == EBUSY || ret == ETIMEDOUT) { 185*27b03b36SApple OSS Distributions if (retries_remaining > 0) { 186*27b03b36SApple OSS Distributions if (!scenario->timer) { 187*27b03b36SApple OSS Distributions T_LOG("stackshot_capture_with_config failed with %s (%d), retrying", 188*27b03b36SApple OSS Distributions strerror(ret), ret); 189*27b03b36SApple OSS Distributions } 190*27b03b36SApple OSS Distributions 191*27b03b36SApple OSS Distributions retries_remaining--; 192*27b03b36SApple OSS Distributions goto retry; 193*27b03b36SApple OSS Distributions } else { 194*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, 195*27b03b36SApple OSS Distributions "called stackshot_capture_with_config (no retries remaining)"); 196*27b03b36SApple OSS Distributions } 197*27b03b36SApple OSS Distributions } else if ((ret == ENOTSUP) && scenario->maybe_unsupported) { 198*27b03b36SApple OSS Distributions T_SKIP("kernel indicated this stackshot configuration is not supported"); 199*27b03b36SApple OSS Distributions } else if ((ret == ENOMEM) && scenario->maybe_enomem) { 200*27b03b36SApple OSS Distributions T_SKIP("insufficient available memory to run test"); 201*27b03b36SApple OSS Distributions } else { 202*27b03b36SApple OSS Distributions quiet(scenario); 203*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config"); 204*27b03b36SApple OSS Distributions } 205*27b03b36SApple OSS Distributions 206*27b03b36SApple OSS Distributions if (scenario->timer) { 207*27b03b36SApple OSS Distributions dt_stat_mach_time_add(scenario->timer, end_time - start_time); 208*27b03b36SApple OSS Distributions } 209*27b03b36SApple OSS Distributions void *buf = stackshot_config_get_stackshot_buffer(config); 210*27b03b36SApple OSS Distributions size_t size = stackshot_config_get_stackshot_size(config); 211*27b03b36SApple OSS Distributions if (scenario->name && !scenario->no_recordfile) { 212*27b03b36SApple OSS Distributions char sspath[MAXPATHLEN]; 213*27b03b36SApple OSS Distributions strlcpy(sspath, scenario->name, sizeof(sspath)); 214*27b03b36SApple OSS Distributions strlcat(sspath, ".kcdata", sizeof(sspath)); 215*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(dt_resultfile(sspath, sizeof(sspath)), 216*27b03b36SApple OSS Distributions "create result file path"); 217*27b03b36SApple OSS Distributions 218*27b03b36SApple OSS Distributions if (!scenario->quiet) { 219*27b03b36SApple OSS Distributions T_LOG("writing stackshot to %s", sspath); 220*27b03b36SApple OSS Distributions } 221*27b03b36SApple OSS Distributions 222*27b03b36SApple OSS Distributions FILE *f = fopen(sspath, "w"); 223*27b03b36SApple OSS Distributions T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(f, 224*27b03b36SApple OSS Distributions "open stackshot output file"); 225*27b03b36SApple OSS Distributions 226*27b03b36SApple OSS Distributions size_t written = fwrite(buf, size, 1, f); 227*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(written, "wrote stackshot to file"); 228*27b03b36SApple OSS Distributions 229*27b03b36SApple OSS Distributions fclose(f); 230*27b03b36SApple OSS Distributions } 231*27b03b36SApple OSS Distributions cb(buf, size); 232*27b03b36SApple OSS Distributions if (compress_ok) { 233*27b03b36SApple OSS Distributions if (global_flags == 0) { 234*27b03b36SApple OSS Distributions T_LOG("Restarting test with compression"); 235*27b03b36SApple OSS Distributions global_flags |= STACKSHOT_DO_COMPRESS; 236*27b03b36SApple OSS Distributions goto start; 237*27b03b36SApple OSS Distributions } else { 238*27b03b36SApple OSS Distributions global_flags = 0; 239*27b03b36SApple OSS Distributions } 240*27b03b36SApple OSS Distributions } 241*27b03b36SApple OSS Distributions 242*27b03b36SApple OSS Distributions ret = stackshot_config_dealloc(config); 243*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config"); 244*27b03b36SApple OSS Distributions} 245*27b03b36SApple OSS Distributions 246*27b03b36SApple OSS DistributionsT_DECL(simple_compressed, "take a simple compressed stackshot") 247*27b03b36SApple OSS Distributions{ 248*27b03b36SApple OSS Distributions struct scenario scenario = { 249*27b03b36SApple OSS Distributions .name = "kcdata_compressed", 250*27b03b36SApple OSS Distributions .flags = (STACKSHOT_DO_COMPRESS | STACKSHOT_SAVE_LOADINFO | STACKSHOT_THREAD_WAITINFO | STACKSHOT_GET_GLOBAL_MEM_STATS | 251*27b03b36SApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 252*27b03b36SApple OSS Distributions }; 253*27b03b36SApple OSS Distributions 254*27b03b36SApple OSS Distributions T_LOG("taking compressed kcdata stackshot"); 255*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 256*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 257*27b03b36SApple OSS Distributions }); 258*27b03b36SApple OSS Distributions} 259*27b03b36SApple OSS Distributions 260*27b03b36SApple OSS DistributionsT_DECL(panic_compressed, "take a compressed stackshot with the same flags as a panic stackshot") 261*27b03b36SApple OSS Distributions{ 262*27b03b36SApple OSS Distributions uint64_t stackshot_flags = (STACKSHOT_SAVE_KEXT_LOADINFO | 263*27b03b36SApple OSS Distributions STACKSHOT_SAVE_LOADINFO | 264*27b03b36SApple OSS Distributions STACKSHOT_KCDATA_FORMAT | 265*27b03b36SApple OSS Distributions STACKSHOT_ENABLE_BT_FAULTING | 266*27b03b36SApple OSS Distributions STACKSHOT_ENABLE_UUID_FAULTING | 267*27b03b36SApple OSS Distributions STACKSHOT_DO_COMPRESS | 268*27b03b36SApple OSS Distributions STACKSHOT_NO_IO_STATS | 269*27b03b36SApple OSS Distributions STACKSHOT_THREAD_WAITINFO | 270*27b03b36SApple OSS Distributions#if TARGET_OS_MAC 271*27b03b36SApple OSS Distributions STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT | 272*27b03b36SApple OSS Distributions#endif 273*27b03b36SApple OSS Distributions STACKSHOT_DISABLE_LATENCY_INFO); 274*27b03b36SApple OSS Distributions 275*27b03b36SApple OSS Distributions struct scenario scenario = { 276*27b03b36SApple OSS Distributions .name = "kcdata_panic_compressed", 277*27b03b36SApple OSS Distributions .flags = stackshot_flags, 278*27b03b36SApple OSS Distributions }; 279*27b03b36SApple OSS Distributions 280*27b03b36SApple OSS Distributions T_LOG("taking compressed kcdata stackshot with panic flags"); 281*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 282*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 283*27b03b36SApple OSS Distributions }); 284*27b03b36SApple OSS Distributions} 285*27b03b36SApple OSS Distributions 286*27b03b36SApple OSS DistributionsT_DECL(kcdata, "test that kcdata stackshots can be taken and parsed") 287*27b03b36SApple OSS Distributions{ 288*27b03b36SApple OSS Distributions struct scenario scenario = { 289*27b03b36SApple OSS Distributions .name = "kcdata", 290*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS | 291*27b03b36SApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 292*27b03b36SApple OSS Distributions }; 293*27b03b36SApple OSS Distributions 294*27b03b36SApple OSS Distributions T_LOG("taking kcdata stackshot"); 295*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 296*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 297*27b03b36SApple OSS Distributions }); 298*27b03b36SApple OSS Distributions} 299*27b03b36SApple OSS Distributions 300*27b03b36SApple OSS DistributionsT_DECL(kcdata_faulting, "test that kcdata stackshots while faulting can be taken and parsed") 301*27b03b36SApple OSS Distributions{ 302*27b03b36SApple OSS Distributions struct scenario scenario = { 303*27b03b36SApple OSS Distributions .name = "faulting", 304*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 305*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT 306*27b03b36SApple OSS Distributions | STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING), 307*27b03b36SApple OSS Distributions }; 308*27b03b36SApple OSS Distributions 309*27b03b36SApple OSS Distributions T_LOG("taking faulting stackshot"); 310*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 311*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 312*27b03b36SApple OSS Distributions }); 313*27b03b36SApple OSS Distributions} 314*27b03b36SApple OSS Distributions 315*27b03b36SApple OSS DistributionsT_DECL(bad_flags, "test a poorly-formed stackshot syscall") 316*27b03b36SApple OSS Distributions{ 317*27b03b36SApple OSS Distributions struct scenario scenario = { 318*27b03b36SApple OSS Distributions .flags = STACKSHOT_SAVE_IN_KERNEL_BUFFER /* not allowed from user space */, 319*27b03b36SApple OSS Distributions .should_fail = true, 320*27b03b36SApple OSS Distributions }; 321*27b03b36SApple OSS Distributions 322*27b03b36SApple OSS Distributions T_LOG("attempting to take stackshot with kernel-only flag"); 323*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(__unused void *ssbuf, __unused size_t sslen) { 324*27b03b36SApple OSS Distributions T_ASSERT_FAIL("stackshot data callback called"); 325*27b03b36SApple OSS Distributions }); 326*27b03b36SApple OSS Distributions} 327*27b03b36SApple OSS Distributions 328*27b03b36SApple OSS DistributionsT_DECL(delta, "test delta stackshots") 329*27b03b36SApple OSS Distributions{ 330*27b03b36SApple OSS Distributions struct scenario scenario = { 331*27b03b36SApple OSS Distributions .name = "delta", 332*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 333*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 334*27b03b36SApple OSS Distributions }; 335*27b03b36SApple OSS Distributions 336*27b03b36SApple OSS Distributions T_LOG("taking full stackshot"); 337*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 338*27b03b36SApple OSS Distributions uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen); 339*27b03b36SApple OSS Distributions 340*27b03b36SApple OSS Distributions T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time); 341*27b03b36SApple OSS Distributions 342*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 343*27b03b36SApple OSS Distributions 344*27b03b36SApple OSS Distributions struct scenario delta_scenario = { 345*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 346*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT 347*27b03b36SApple OSS Distributions | STACKSHOT_COLLECT_DELTA_SNAPSHOT), 348*27b03b36SApple OSS Distributions .since_timestamp = stackshot_time 349*27b03b36SApple OSS Distributions }; 350*27b03b36SApple OSS Distributions 351*27b03b36SApple OSS Distributions take_stackshot(&delta_scenario, false, ^(void *dssbuf, size_t dsslen) { 352*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DELTA, dssbuf, dsslen, nil); 353*27b03b36SApple OSS Distributions }); 354*27b03b36SApple OSS Distributions }); 355*27b03b36SApple OSS Distributions} 356*27b03b36SApple OSS Distributions 357*27b03b36SApple OSS DistributionsT_DECL(shared_cache_layout, "test stackshot inclusion of shared cache layout") 358*27b03b36SApple OSS Distributions{ 359*27b03b36SApple OSS Distributions struct scenario scenario = { 360*27b03b36SApple OSS Distributions .name = "shared_cache_layout", 361*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 362*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT | 363*27b03b36SApple OSS Distributions STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT), 364*27b03b36SApple OSS Distributions }; 365*27b03b36SApple OSS Distributions 366*27b03b36SApple OSS Distributions size_t shared_cache_length; 367*27b03b36SApple OSS Distributions const void *cache_header = _dyld_get_shared_cache_range(&shared_cache_length); 368*27b03b36SApple OSS Distributions if (cache_header == NULL) { 369*27b03b36SApple OSS Distributions T_SKIP("Device not running with shared cache, skipping test..."); 370*27b03b36SApple OSS Distributions } 371*27b03b36SApple OSS Distributions 372*27b03b36SApple OSS Distributions if (shared_cache_length == 0) { 373*27b03b36SApple OSS Distributions T_SKIP("dyld reports that currently running shared cache has zero length"); 374*27b03b36SApple OSS Distributions } 375*27b03b36SApple OSS Distributions 376*27b03b36SApple OSS Distributions T_LOG("taking stackshot with STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT set"); 377*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 378*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_SHAREDCACHE_LAYOUT, ssbuf, sslen, nil); 379*27b03b36SApple OSS Distributions }); 380*27b03b36SApple OSS Distributions} 381*27b03b36SApple OSS Distributions 382*27b03b36SApple OSS DistributionsT_DECL(stress, "test that taking stackshots for 60 seconds doesn't crash the system") 383*27b03b36SApple OSS Distributions{ 384*27b03b36SApple OSS Distributions uint64_t max_diff_time = 60ULL /* seconds */ * 1000000000ULL; 385*27b03b36SApple OSS Distributions uint64_t start_time; 386*27b03b36SApple OSS Distributions 387*27b03b36SApple OSS Distributions struct scenario scenario = { 388*27b03b36SApple OSS Distributions .name = "stress", 389*27b03b36SApple OSS Distributions .quiet = true, 390*27b03b36SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT | 391*27b03b36SApple OSS Distributions STACKSHOT_THREAD_WAITINFO | 392*27b03b36SApple OSS Distributions STACKSHOT_SAVE_LOADINFO | 393*27b03b36SApple OSS Distributions STACKSHOT_SAVE_KEXT_LOADINFO | 394*27b03b36SApple OSS Distributions STACKSHOT_GET_GLOBAL_MEM_STATS | 395*27b03b36SApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | 396*27b03b36SApple OSS Distributions STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT | 397*27b03b36SApple OSS Distributions STACKSHOT_THREAD_GROUP | 398*27b03b36SApple OSS Distributions STACKSHOT_SAVE_JETSAM_COALITIONS | 399*27b03b36SApple OSS Distributions STACKSHOT_ASID | 400*27b03b36SApple OSS Distributions 0), 401*27b03b36SApple OSS Distributions }; 402*27b03b36SApple OSS Distributions 403*27b03b36SApple OSS Distributions start_time = clock_gettime_nsec_np(CLOCK_MONOTONIC); 404*27b03b36SApple OSS Distributions while (clock_gettime_nsec_np(CLOCK_MONOTONIC) - start_time < max_diff_time) { 405*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void * __unused ssbuf, 406*27b03b36SApple OSS Distributions size_t __unused sslen) { 407*27b03b36SApple OSS Distributions printf("."); 408*27b03b36SApple OSS Distributions fflush(stdout); 409*27b03b36SApple OSS Distributions }); 410*27b03b36SApple OSS Distributions 411*27b03b36SApple OSS Distributions /* 412*27b03b36SApple OSS Distributions * After the first stackshot, there's no point in continuing to 413*27b03b36SApple OSS Distributions * write them to disk, and it wears down the SSDs. 414*27b03b36SApple OSS Distributions */ 415*27b03b36SApple OSS Distributions scenario.no_recordfile = true; 416*27b03b36SApple OSS Distributions 417*27b03b36SApple OSS Distributions /* Leave some time for the testing infrastructure to catch up */ 418*27b03b36SApple OSS Distributions usleep(10000); 419*27b03b36SApple OSS Distributions 420*27b03b36SApple OSS Distributions } 421*27b03b36SApple OSS Distributions printf("\n"); 422*27b03b36SApple OSS Distributions} 423*27b03b36SApple OSS Distributions 424*27b03b36SApple OSS DistributionsT_DECL(dispatch_queue_label, "test that kcdata stackshots contain libdispatch queue labels") 425*27b03b36SApple OSS Distributions{ 426*27b03b36SApple OSS Distributions struct scenario scenario = { 427*27b03b36SApple OSS Distributions .name = "kcdata", 428*27b03b36SApple OSS Distributions .flags = (STACKSHOT_GET_DQ | STACKSHOT_KCDATA_FORMAT), 429*27b03b36SApple OSS Distributions }; 430*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem, parent_done_sem; 431*27b03b36SApple OSS Distributions dispatch_queue_t dq; 432*27b03b36SApple OSS Distributions 433*27b03b36SApple OSS Distributions#if TARGET_OS_WATCH 434*27b03b36SApple OSS Distributions T_SKIP("This test is flaky on watches: 51663346"); 435*27b03b36SApple OSS Distributions#endif 436*27b03b36SApple OSS Distributions 437*27b03b36SApple OSS Distributions child_ready_sem = dispatch_semaphore_create(0); 438*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "dqlabel child semaphore"); 439*27b03b36SApple OSS Distributions 440*27b03b36SApple OSS Distributions parent_done_sem = dispatch_semaphore_create(0); 441*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(parent_done_sem, "dqlabel parent semaphore"); 442*27b03b36SApple OSS Distributions 443*27b03b36SApple OSS Distributions dq = dispatch_queue_create(TEST_STACKSHOT_QUEUE_LABEL, NULL); 444*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(dq, "dispatch queue"); 445*27b03b36SApple OSS Distributions 446*27b03b36SApple OSS Distributions /* start the helper thread */ 447*27b03b36SApple OSS Distributions dispatch_async(dq, ^{ 448*27b03b36SApple OSS Distributions dispatch_semaphore_signal(child_ready_sem); 449*27b03b36SApple OSS Distributions 450*27b03b36SApple OSS Distributions dispatch_semaphore_wait(parent_done_sem, DISPATCH_TIME_FOREVER); 451*27b03b36SApple OSS Distributions }); 452*27b03b36SApple OSS Distributions 453*27b03b36SApple OSS Distributions /* block behind the child starting up */ 454*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 455*27b03b36SApple OSS Distributions 456*27b03b36SApple OSS Distributions T_LOG("taking kcdata stackshot with libdispatch queue labels"); 457*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 458*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DISPATCH_QUEUE_LABEL, ssbuf, sslen, nil); 459*27b03b36SApple OSS Distributions }); 460*27b03b36SApple OSS Distributions 461*27b03b36SApple OSS Distributions dispatch_semaphore_signal(parent_done_sem); 462*27b03b36SApple OSS Distributions} 463*27b03b36SApple OSS Distributions 464*27b03b36SApple OSS Distributions#define CACHEADDR_ENV "STACKSHOT_TEST_DYLDADDR" 465*27b03b36SApple OSS DistributionsT_HELPER_DECL(spawn_reslide_child, "child process to spawn with alternate slide") 466*27b03b36SApple OSS Distributions{ 467*27b03b36SApple OSS Distributions size_t shared_cache_len; 468*27b03b36SApple OSS Distributions const void *addr, *prevaddr; 469*27b03b36SApple OSS Distributions uintmax_t v; 470*27b03b36SApple OSS Distributions char *endptr; 471*27b03b36SApple OSS Distributions 472*27b03b36SApple OSS Distributions const char *cacheaddr_env = getenv(CACHEADDR_ENV); 473*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cacheaddr_env, "getenv("CACHEADDR_ENV")"); 474*27b03b36SApple OSS Distributions errno = 0; 475*27b03b36SApple OSS Distributions endptr = NULL; 476*27b03b36SApple OSS Distributions v = strtoumax(cacheaddr_env, &endptr, 16); /* read hex value */ 477*27b03b36SApple OSS Distributions T_WITH_ERRNO; T_QUIET; T_ASSERT_NE(v, 0l, "getenv(%s) = \"%s\" should be a non-zero hex number", CACHEADDR_ENV, cacheaddr_env); 478*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(*endptr, 0, "getenv(%s) = \"%s\" endptr \"%s\" should be empty", CACHEADDR_ENV, cacheaddr_env, endptr); 479*27b03b36SApple OSS Distributions 480*27b03b36SApple OSS Distributions prevaddr = (const void *)v; 481*27b03b36SApple OSS Distributions addr = _dyld_get_shared_cache_range(&shared_cache_len); 482*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(addr, "shared cache address"); 483*27b03b36SApple OSS Distributions 484*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(getppid(), (addr == prevaddr) ? SIGUSR2 : SIGUSR1), "signaled parent to take stackshot"); 485*27b03b36SApple OSS Distributions for (;;) { 486*27b03b36SApple OSS Distributions (void) pause(); /* parent will kill -9 us */ 487*27b03b36SApple OSS Distributions } 488*27b03b36SApple OSS Distributions} 489*27b03b36SApple OSS Distributions 490*27b03b36SApple OSS DistributionsT_DECL(shared_cache_flags, "tests stackshot's task_ss_flags for the shared cache") 491*27b03b36SApple OSS Distributions{ 492*27b03b36SApple OSS Distributions posix_spawnattr_t attr; 493*27b03b36SApple OSS Distributions char *env_addr; 494*27b03b36SApple OSS Distributions char path[PATH_MAX]; 495*27b03b36SApple OSS Distributions __block bool child_same_addr = false; 496*27b03b36SApple OSS Distributions 497*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 498*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 499*27b03b36SApple OSS Distributions char *args[] = { path, "-n", "spawn_reslide_child", NULL }; 500*27b03b36SApple OSS Distributions pid_t pid; 501*27b03b36SApple OSS Distributions size_t shared_cache_len; 502*27b03b36SApple OSS Distributions const void *addr; 503*27b03b36SApple OSS Distributions 504*27b03b36SApple OSS Distributions dispatch_source_t child_diffsig_src, child_samesig_src; 505*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 506*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "shared_cache child semaphore"); 507*27b03b36SApple OSS Distributions 508*27b03b36SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 509*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 510*27b03b36SApple OSS Distributions 511*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 512*27b03b36SApple OSS Distributions signal(SIGUSR2, SIG_IGN); 513*27b03b36SApple OSS Distributions child_samesig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 514*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_samesig_src, "dispatch_source_create (child_samesig_src)"); 515*27b03b36SApple OSS Distributions child_diffsig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR2, 0, signal_processing_q); 516*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_diffsig_src, "dispatch_source_create (child_diffsig_src)"); 517*27b03b36SApple OSS Distributions 518*27b03b36SApple OSS Distributions /* child will signal us depending on if their addr is the same or different */ 519*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(child_samesig_src, ^{ child_same_addr = false; dispatch_semaphore_signal(child_ready_sem); }); 520*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(child_diffsig_src, ^{ child_same_addr = true; dispatch_semaphore_signal(child_ready_sem); }); 521*27b03b36SApple OSS Distributions dispatch_activate(child_samesig_src); 522*27b03b36SApple OSS Distributions dispatch_activate(child_diffsig_src); 523*27b03b36SApple OSS Distributions 524*27b03b36SApple OSS Distributions addr = _dyld_get_shared_cache_range(&shared_cache_len); 525*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(addr, "shared cache address"); 526*27b03b36SApple OSS Distributions 527*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(asprintf(&env_addr, "%p", addr), "asprintf of env_addr succeeded"); 528*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(setenv(CACHEADDR_ENV, env_addr, true), "setting "CACHEADDR_ENV" to %s", env_addr); 529*27b03b36SApple OSS Distributions 530*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(posix_spawnattr_init(&attr), "posix_spawnattr_init"); 531*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(posix_spawnattr_setflags(&attr, _POSIX_SPAWN_RESLIDE), "posix_spawnattr_setflags"); 532*27b03b36SApple OSS Distributions int sp_ret = posix_spawn(&pid, path, NULL, &attr, args, environ); 533*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 534*27b03b36SApple OSS Distributions 535*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 536*27b03b36SApple OSS Distributions T_LOG("received signal from child (%s), capturing stackshot", child_same_addr ? "same shared cache addr" : "different shared cache addr"); 537*27b03b36SApple OSS Distributions 538*27b03b36SApple OSS Distributions struct scenario scenario = { 539*27b03b36SApple OSS Distributions .name = "shared_cache_flags", 540*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 541*27b03b36SApple OSS Distributions | STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT 542*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 543*27b03b36SApple OSS Distributions }; 544*27b03b36SApple OSS Distributions 545*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 546*27b03b36SApple OSS Distributions int status; 547*27b03b36SApple OSS Distributions /* First kill the child so we can reap it */ 548*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "killing spawned process"); 549*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on spawned child"); 550*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(!!WIFSIGNALED(status), 1, "waitpid status should be signalled"); 551*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "waitpid status should be SIGKILLed"); 552*27b03b36SApple OSS Distributions 553*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_SHAREDCACHE_FLAGS, ssbuf, sslen, 554*27b03b36SApple OSS Distributions @{sharedcache_child_pid_key: @(pid), sharedcache_child_sameaddr_key: @(child_same_addr ? 1 : 0)}); 555*27b03b36SApple OSS Distributions }); 556*27b03b36SApple OSS Distributions} 557*27b03b36SApple OSS Distributions 558*27b03b36SApple OSS DistributionsT_DECL(transitioning_tasks, "test that stackshot contains transitioning task info", T_META_BOOTARGS_SET("enable_proc_exit_lpexit_spin=1")) 559*27b03b36SApple OSS Distributions{ 560*27b03b36SApple OSS Distributions int32_t sysctlValue = -1, numAttempts =0; 561*27b03b36SApple OSS Distributions char path[PATH_MAX]; 562*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 563*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 564*27b03b36SApple OSS Distributions char *args[] = { path, "-n", "exec_child_preexec", NULL }; 565*27b03b36SApple OSS Distributions 566*27b03b36SApple OSS Distributions dispatch_source_t child_sig_src; 567*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 568*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "exec child semaphore"); 569*27b03b36SApple OSS Distributions 570*27b03b36SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 571*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 572*27b03b36SApple OSS Distributions 573*27b03b36SApple OSS Distributions pid_t pid; 574*27b03b36SApple OSS Distributions 575*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 576*27b03b36SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 577*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 578*27b03b36SApple OSS Distributions 579*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 580*27b03b36SApple OSS Distributions dispatch_activate(child_sig_src); 581*27b03b36SApple OSS Distributions 582*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spin_pid", NULL, NULL, &sysctlValue, sizeof(sysctlValue)), "set debug.proc_exit_lpexit_spin_pid=-1"); 583*27b03b36SApple OSS Distributions 584*27b03b36SApple OSS Distributions int proc_exit_spin_pos = 0 ; 585*27b03b36SApple OSS Distributions 586*27b03b36SApple OSS Distributions while (0 == sysctlbyname("debug.proc_exit_lpexit_spin_pos", NULL, NULL, &proc_exit_spin_pos, sizeof(proc_exit_spin_pos))) { 587*27b03b36SApple OSS Distributions 588*27b03b36SApple OSS Distributions T_LOG(" ##### Testing while spinning in proc_exit at position %d ##### ", proc_exit_spin_pos); 589*27b03b36SApple OSS Distributions 590*27b03b36SApple OSS Distributions int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); 591*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 592*27b03b36SApple OSS Distributions 593*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 594*27b03b36SApple OSS Distributions 595*27b03b36SApple OSS Distributions struct proc_uniqidentifierinfo proc_info_data = { }; 596*27b03b36SApple OSS Distributions int retval = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &proc_info_data, sizeof(proc_info_data)); 597*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(retval, "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO"); 598*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ_INT(retval, (int) sizeof(proc_info_data), "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO returned data"); 599*27b03b36SApple OSS Distributions 600*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGUSR1), "signaled pre-exec child to exec"); 601*27b03b36SApple OSS Distributions 602*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 603*27b03b36SApple OSS Distributions 604*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spin_pid", NULL, NULL, &pid, sizeof(pid)), "set debug.proc_exit_lpexit_spin_pid = %d, ", pid); 605*27b03b36SApple OSS Distributions 606*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "kill post-exec child %d", pid); 607*27b03b36SApple OSS Distributions 608*27b03b36SApple OSS Distributions sysctlValue = 0; 609*27b03b36SApple OSS Distributions size_t len = sizeof(sysctlValue); 610*27b03b36SApple OSS Distributions while (numAttempts < 5) { 611*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spinning", &sysctlValue, &len, NULL, 0), "retrieve debug.proc_exit_lpexit_spinning"); 612*27b03b36SApple OSS Distributions if (sysctlValue != 1) numAttempts++; 613*27b03b36SApple OSS Distributions else break; 614*27b03b36SApple OSS Distributions sleep(1); 615*27b03b36SApple OSS Distributions } 616*27b03b36SApple OSS Distributions 617*27b03b36SApple OSS Distributions T_ASSERT_EQ_UINT(sysctlValue, 1, "find spinning task in proc_exit()"); 618*27b03b36SApple OSS Distributions 619*27b03b36SApple OSS Distributions struct scenario scenario = { 620*27b03b36SApple OSS Distributions .name = "transitioning_tasks", 621*27b03b36SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT) 622*27b03b36SApple OSS Distributions }; 623*27b03b36SApple OSS Distributions 624*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 625*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_TRANSITIONING, ssbuf, sslen, @{transitioning_pid_key: @(pid)}); 626*27b03b36SApple OSS Distributions 627*27b03b36SApple OSS Distributions // Kill the child 628*27b03b36SApple OSS Distributions int sysctlValueB = -1; 629*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spin_pid", NULL, NULL, &sysctlValueB, sizeof(sysctlValueB)), "set debug.proc_exit_lpexit_spin_pid=-1"); 630*27b03b36SApple OSS Distributions sleep(1); 631*27b03b36SApple OSS Distributions size_t blen = sizeof(sysctlValueB); 632*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spinning", &sysctlValueB, &blen, NULL, 0), "retrieve debug.proc_exit_lpexit_spinning"); 633*27b03b36SApple OSS Distributions T_ASSERT_EQ_UINT(sysctlValueB, 0, "make sure nothing is spining in proc_exit()"); 634*27b03b36SApple OSS Distributions int status; 635*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on post-exec child"); 636*27b03b36SApple OSS Distributions }); 637*27b03b36SApple OSS Distributions 638*27b03b36SApple OSS Distributions proc_exit_spin_pos++; 639*27b03b36SApple OSS Distributions } 640*27b03b36SApple OSS Distributions 641*27b03b36SApple OSS Distributions} 642*27b03b36SApple OSS Distributions 643*27b03b36SApple OSS Distributionsstatic void *stuck_sysctl_thread(void *arg) { 644*27b03b36SApple OSS Distributions int val = 1; 645*27b03b36SApple OSS Distributions dispatch_semaphore_t child_thread_started = *(dispatch_semaphore_t *)arg; 646*27b03b36SApple OSS Distributions 647*27b03b36SApple OSS Distributions dispatch_semaphore_signal(child_thread_started); 648*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.wedge_thread", NULL, NULL, &val, sizeof(val)), "wedge child thread"); 649*27b03b36SApple OSS Distributions 650*27b03b36SApple OSS Distributions return NULL; 651*27b03b36SApple OSS Distributions} 652*27b03b36SApple OSS Distributions 653*27b03b36SApple OSS DistributionsT_HELPER_DECL(zombie_child, "child process to sample as a zombie") 654*27b03b36SApple OSS Distributions{ 655*27b03b36SApple OSS Distributions pthread_t pthread; 656*27b03b36SApple OSS Distributions dispatch_semaphore_t child_thread_started = dispatch_semaphore_create(0); 657*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_thread_started, "zombie child thread semaphore"); 658*27b03b36SApple OSS Distributions 659*27b03b36SApple OSS Distributions /* spawn another thread to get stuck in the kernel, then call exit() to become a zombie */ 660*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&pthread, NULL, stuck_sysctl_thread, &child_thread_started), "pthread_create"); 661*27b03b36SApple OSS Distributions 662*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_thread_started, DISPATCH_TIME_FOREVER); 663*27b03b36SApple OSS Distributions 664*27b03b36SApple OSS Distributions /* sleep for a bit in the hope of ensuring that the other thread has called the sysctl before we signal the parent */ 665*27b03b36SApple OSS Distributions usleep(100); 666*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take stackshot"); 667*27b03b36SApple OSS Distributions 668*27b03b36SApple OSS Distributions exit(0); 669*27b03b36SApple OSS Distributions} 670*27b03b36SApple OSS Distributions 671*27b03b36SApple OSS DistributionsT_DECL(zombie, "tests a stackshot of a zombie task with a thread stuck in the kernel") 672*27b03b36SApple OSS Distributions{ 673*27b03b36SApple OSS Distributions char path[PATH_MAX]; 674*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 675*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 676*27b03b36SApple OSS Distributions char *args[] = { path, "-n", "zombie_child", NULL }; 677*27b03b36SApple OSS Distributions 678*27b03b36SApple OSS Distributions dispatch_source_t child_sig_src; 679*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 680*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "zombie child semaphore"); 681*27b03b36SApple OSS Distributions 682*27b03b36SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 683*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 684*27b03b36SApple OSS Distributions 685*27b03b36SApple OSS Distributions pid_t pid; 686*27b03b36SApple OSS Distributions 687*27b03b36SApple OSS Distributions T_LOG("spawning a child"); 688*27b03b36SApple OSS Distributions 689*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 690*27b03b36SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 691*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 692*27b03b36SApple OSS Distributions 693*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 694*27b03b36SApple OSS Distributions dispatch_activate(child_sig_src); 695*27b03b36SApple OSS Distributions 696*27b03b36SApple OSS Distributions int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); 697*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 698*27b03b36SApple OSS Distributions 699*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 700*27b03b36SApple OSS Distributions 701*27b03b36SApple OSS Distributions T_LOG("received signal from child, capturing stackshot"); 702*27b03b36SApple OSS Distributions 703*27b03b36SApple OSS Distributions struct proc_bsdshortinfo bsdshortinfo; 704*27b03b36SApple OSS Distributions int retval, iterations_to_wait = 10; 705*27b03b36SApple OSS Distributions 706*27b03b36SApple OSS Distributions while (iterations_to_wait > 0) { 707*27b03b36SApple OSS Distributions retval = proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 0, &bsdshortinfo, sizeof(bsdshortinfo)); 708*27b03b36SApple OSS Distributions if ((retval == 0) && errno == ESRCH) { 709*27b03b36SApple OSS Distributions T_LOG("unable to find child using proc_pidinfo, assuming zombie"); 710*27b03b36SApple OSS Distributions break; 711*27b03b36SApple OSS Distributions } 712*27b03b36SApple OSS Distributions 713*27b03b36SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_GT(retval, 0, "proc_pidinfo(PROC_PIDT_SHORTBSDINFO) returned a value > 0"); 714*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(retval, (int)sizeof(bsdshortinfo), "proc_pidinfo call for PROC_PIDT_SHORTBSDINFO returned expected size"); 715*27b03b36SApple OSS Distributions 716*27b03b36SApple OSS Distributions if (bsdshortinfo.pbsi_flags & PROC_FLAG_INEXIT) { 717*27b03b36SApple OSS Distributions T_LOG("child proc info marked as in exit"); 718*27b03b36SApple OSS Distributions break; 719*27b03b36SApple OSS Distributions } 720*27b03b36SApple OSS Distributions 721*27b03b36SApple OSS Distributions iterations_to_wait--; 722*27b03b36SApple OSS Distributions if (iterations_to_wait == 0) { 723*27b03b36SApple OSS Distributions /* 724*27b03b36SApple OSS Distributions * This will mark the test as failed but let it continue so we 725*27b03b36SApple OSS Distributions * don't leave a process stuck in the kernel. 726*27b03b36SApple OSS Distributions */ 727*27b03b36SApple OSS Distributions T_FAIL("unable to discover that child is marked as exiting"); 728*27b03b36SApple OSS Distributions } 729*27b03b36SApple OSS Distributions 730*27b03b36SApple OSS Distributions /* Give the child a few more seconds to make it to exit */ 731*27b03b36SApple OSS Distributions sleep(5); 732*27b03b36SApple OSS Distributions } 733*27b03b36SApple OSS Distributions 734*27b03b36SApple OSS Distributions /* Give the child some more time to make it through exit */ 735*27b03b36SApple OSS Distributions sleep(10); 736*27b03b36SApple OSS Distributions 737*27b03b36SApple OSS Distributions struct scenario scenario = { 738*27b03b36SApple OSS Distributions .name = "zombie", 739*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 740*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 741*27b03b36SApple OSS Distributions }; 742*27b03b36SApple OSS Distributions 743*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 744*27b03b36SApple OSS Distributions /* First unwedge the child so we can reap it */ 745*27b03b36SApple OSS Distributions int val = 1, status; 746*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.unwedge_thread", NULL, NULL, &val, sizeof(val)), "unwedge child"); 747*27b03b36SApple OSS Distributions 748*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on zombie child"); 749*27b03b36SApple OSS Distributions 750*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_ZOMBIE, ssbuf, sslen, @{zombie_child_pid_key: @(pid)}); 751*27b03b36SApple OSS Distributions }); 752*27b03b36SApple OSS Distributions} 753*27b03b36SApple OSS Distributions 754*27b03b36SApple OSS DistributionsT_HELPER_DECL(exec_child_preexec, "child process pre-exec") 755*27b03b36SApple OSS Distributions{ 756*27b03b36SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 757*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 758*27b03b36SApple OSS Distributions 759*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 760*27b03b36SApple OSS Distributions dispatch_source_t parent_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 761*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(parent_sig_src, "dispatch_source_create (child_sig_src)"); 762*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(parent_sig_src, ^{ 763*27b03b36SApple OSS Distributions 764*27b03b36SApple OSS Distributions // Parent took a timestamp then signaled us: exec into the next process 765*27b03b36SApple OSS Distributions 766*27b03b36SApple OSS Distributions char path[PATH_MAX]; 767*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 768*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 769*27b03b36SApple OSS Distributions char *args[] = { path, "-n", "exec_child_postexec", NULL }; 770*27b03b36SApple OSS Distributions 771*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(execve(args[0], args, NULL), "execing into exec_child_postexec"); 772*27b03b36SApple OSS Distributions }); 773*27b03b36SApple OSS Distributions dispatch_activate(parent_sig_src); 774*27b03b36SApple OSS Distributions 775*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take timestamp"); 776*27b03b36SApple OSS Distributions 777*27b03b36SApple OSS Distributions sleep(100); 778*27b03b36SApple OSS Distributions // Should never get here 779*27b03b36SApple OSS Distributions T_FAIL("Received signal to exec from parent"); 780*27b03b36SApple OSS Distributions} 781*27b03b36SApple OSS Distributions 782*27b03b36SApple OSS DistributionsT_HELPER_DECL(exec_child_postexec, "child process post-exec to sample") 783*27b03b36SApple OSS Distributions{ 784*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take stackshot"); 785*27b03b36SApple OSS Distributions sleep(100); 786*27b03b36SApple OSS Distributions // Should never get here 787*27b03b36SApple OSS Distributions T_FAIL("Killed by parent"); 788*27b03b36SApple OSS Distributions} 789*27b03b36SApple OSS Distributions 790*27b03b36SApple OSS DistributionsT_DECL(exec, "test getting full task snapshots for a task that execs") 791*27b03b36SApple OSS Distributions{ 792*27b03b36SApple OSS Distributions char path[PATH_MAX]; 793*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 794*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 795*27b03b36SApple OSS Distributions char *args[] = { path, "-n", "exec_child_preexec", NULL }; 796*27b03b36SApple OSS Distributions 797*27b03b36SApple OSS Distributions dispatch_source_t child_sig_src; 798*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 799*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "exec child semaphore"); 800*27b03b36SApple OSS Distributions 801*27b03b36SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 802*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 803*27b03b36SApple OSS Distributions 804*27b03b36SApple OSS Distributions pid_t pid; 805*27b03b36SApple OSS Distributions 806*27b03b36SApple OSS Distributions T_LOG("spawning a child"); 807*27b03b36SApple OSS Distributions 808*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 809*27b03b36SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 810*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 811*27b03b36SApple OSS Distributions 812*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 813*27b03b36SApple OSS Distributions dispatch_activate(child_sig_src); 814*27b03b36SApple OSS Distributions 815*27b03b36SApple OSS Distributions int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); 816*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 817*27b03b36SApple OSS Distributions 818*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 819*27b03b36SApple OSS Distributions uint64_t start_time = mach_absolute_time(); 820*27b03b36SApple OSS Distributions 821*27b03b36SApple OSS Distributions struct proc_uniqidentifierinfo proc_info_data = { }; 822*27b03b36SApple OSS Distributions int retval = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &proc_info_data, sizeof(proc_info_data)); 823*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(retval, "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO"); 824*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ_INT(retval, (int) sizeof(proc_info_data), "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO returned data"); 825*27b03b36SApple OSS Distributions uint64_t unique_pid = proc_info_data.p_uniqueid; 826*27b03b36SApple OSS Distributions 827*27b03b36SApple OSS Distributions T_LOG("received signal from pre-exec child, unique_pid is %llu, timestamp is %llu", unique_pid, start_time); 828*27b03b36SApple OSS Distributions 829*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGUSR1), "signaled pre-exec child to exec"); 830*27b03b36SApple OSS Distributions 831*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 832*27b03b36SApple OSS Distributions 833*27b03b36SApple OSS Distributions T_LOG("received signal from post-exec child, capturing stackshot"); 834*27b03b36SApple OSS Distributions 835*27b03b36SApple OSS Distributions struct scenario scenario = { 836*27b03b36SApple OSS Distributions .name = "exec", 837*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 838*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT 839*27b03b36SApple OSS Distributions | STACKSHOT_COLLECT_DELTA_SNAPSHOT), 840*27b03b36SApple OSS Distributions .since_timestamp = start_time 841*27b03b36SApple OSS Distributions }; 842*27b03b36SApple OSS Distributions 843*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 844*27b03b36SApple OSS Distributions // Kill the child 845*27b03b36SApple OSS Distributions int status; 846*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "kill post-exec child %d", pid); 847*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on post-exec child"); 848*27b03b36SApple OSS Distributions 849*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_POSTEXEC | PARSE_STACKSHOT_DELTA, ssbuf, sslen, @{postexec_child_unique_pid_key: @(unique_pid)}); 850*27b03b36SApple OSS Distributions }); 851*27b03b36SApple OSS Distributions} 852*27b03b36SApple OSS Distributions 853*27b03b36SApple OSS DistributionsT_DECL(exec_inprogress, "test stackshots of processes in the middle of exec") 854*27b03b36SApple OSS Distributions{ 855*27b03b36SApple OSS Distributions pid_t pid; 856*27b03b36SApple OSS Distributions /* a BASH quine which execs itself as long as the parent doesn't exit */ 857*27b03b36SApple OSS Distributions char *bash_prog = "[[ $PPID -ne 1 ]] && exec /bin/bash -c \"$0\" \"$0\""; 858*27b03b36SApple OSS Distributions char *args[] = { "/bin/bash", "-c", bash_prog, bash_prog, NULL }; 859*27b03b36SApple OSS Distributions 860*27b03b36SApple OSS Distributions posix_spawnattr_t sattr; 861*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawnattr_init(&sattr), "posix_spawnattr_init"); 862*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn(&pid, args[0], NULL, &sattr, args, NULL), "spawn exec_inprogress_child"); 863*27b03b36SApple OSS Distributions 864*27b03b36SApple OSS Distributions struct scenario scenario = { 865*27b03b36SApple OSS Distributions .name = "exec_inprogress", 866*27b03b36SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT), 867*27b03b36SApple OSS Distributions .target_pid = pid, 868*27b03b36SApple OSS Distributions }; 869*27b03b36SApple OSS Distributions 870*27b03b36SApple OSS Distributions int tries = 0; 871*27b03b36SApple OSS Distributions int tries_limit = 30; 872*27b03b36SApple OSS Distributions __block bool found = false; 873*27b03b36SApple OSS Distributions __block uint64_t cid1 = 0, cid2 = 0; 874*27b03b36SApple OSS Distributions 875*27b03b36SApple OSS Distributions for (tries = 0; !found && tries < tries_limit; tries++) { 876*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, 877*27b03b36SApple OSS Distributions ^( void *ssbuf, size_t sslen) { 878*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_EXEC_INPROGRESS, 879*27b03b36SApple OSS Distributions ssbuf, sslen, @{ 880*27b03b36SApple OSS Distributions exec_inprogress_pid_key: @(pid), 881*27b03b36SApple OSS Distributions exec_inprogress_found_key: ^(uint64_t id1, uint64_t id2) { found = true; cid1 = id1; cid2 = id2; }}); 882*27b03b36SApple OSS Distributions }); 883*27b03b36SApple OSS Distributions } 884*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "killing exec loop"); 885*27b03b36SApple OSS Distributions T_ASSERT_TRUE(found, "able to find our execing process mid-exec in %d tries", tries); 886*27b03b36SApple OSS Distributions T_ASSERT_NE(cid1, cid2, "container IDs for in-progress exec are unique"); 887*27b03b36SApple OSS Distributions T_PASS("found mid-exec process in %d tries", tries); 888*27b03b36SApple OSS Distributions} 889*27b03b36SApple OSS Distributions 890*27b03b36SApple OSS Distributions#ifdef _LP64 891*27b03b36SApple OSS Distributions#if __has_feature(ptrauth_calls) 892*27b03b36SApple OSS Distributions#define __ptrauth_swift_async_context_parent \ 893*27b03b36SApple OSS Distributions __ptrauth(ptrauth_key_process_independent_data, 1, 0xbda2) 894*27b03b36SApple OSS Distributions#define __ptrauth_swift_async_context_resume \ 895*27b03b36SApple OSS Distributions __ptrauth(ptrauth_key_function_pointer, 1, 0xd707) 896*27b03b36SApple OSS Distributions#else 897*27b03b36SApple OSS Distributions#define __ptrauth_swift_async_context_parent 898*27b03b36SApple OSS Distributions#define __ptrauth_swift_async_context_resume 899*27b03b36SApple OSS Distributions#endif 900*27b03b36SApple OSS Distributions// Add 1 to match the symbolication aid added by the stackshot backtracer. 901*27b03b36SApple OSS Distributions#define asyncstack_frame(x) ((uintptr_t)(void *)ptrauth_strip((void *)(x), ptrauth_key_function_pointer) + 1) 902*27b03b36SApple OSS Distributions 903*27b03b36SApple OSS Distributions// This struct fakes the Swift AsyncContext struct which is used by 904*27b03b36SApple OSS Distributions// the Swift concurrency runtime. We only care about the first 2 fields. 905*27b03b36SApple OSS Distributionsstruct fake_async_context { 906*27b03b36SApple OSS Distributions struct fake_async_context* __ptrauth_swift_async_context_parent next; 907*27b03b36SApple OSS Distributions void(*__ptrauth_swift_async_context_resume resume_pc)(void); 908*27b03b36SApple OSS Distributions}; 909*27b03b36SApple OSS Distributions 910*27b03b36SApple OSS Distributionsstatic void 911*27b03b36SApple OSS Distributionslevel1_func() 912*27b03b36SApple OSS Distributions{ 913*27b03b36SApple OSS Distributions} 914*27b03b36SApple OSS Distributionsstatic void 915*27b03b36SApple OSS Distributionslevel2_func() 916*27b03b36SApple OSS Distributions{ 917*27b03b36SApple OSS Distributions} 918*27b03b36SApple OSS Distributions 919*27b03b36SApple OSS Distributions// Create a chain of fake async contexts; sync with asyncstack_expected_stack below 920*27b03b36SApple OSS Distributionsstatic alignas(16) struct fake_async_context level1 = { 0, level1_func }; 921*27b03b36SApple OSS Distributionsstatic alignas(16) struct fake_async_context level2 = { &level1, level2_func }; 922*27b03b36SApple OSS Distributions 923*27b03b36SApple OSS Distributionsstruct async_test_semaphores { 924*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem; /* signal parent we're ready */ 925*27b03b36SApple OSS Distributions dispatch_semaphore_t child_exit_sem; /* parent tells us to go away */ 926*27b03b36SApple OSS Distributions}; 927*27b03b36SApple OSS Distributions 928*27b03b36SApple OSS Distributions#define ASYNCSTACK_THREAD_NAME "asyncstack_thread" 929*27b03b36SApple OSS Distributions 930*27b03b36SApple OSS Distributionsstatic void __attribute__((noinline, not_tail_called)) 931*27b03b36SApple OSS Distributionsexpect_asyncstack(void *arg) 932*27b03b36SApple OSS Distributions{ 933*27b03b36SApple OSS Distributions struct async_test_semaphores *async_ts = arg; 934*27b03b36SApple OSS Distributions 935*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_setname_np(ASYNCSTACK_THREAD_NAME), 936*27b03b36SApple OSS Distributions "set thread name to %s", ASYNCSTACK_THREAD_NAME); 937*27b03b36SApple OSS Distributions 938*27b03b36SApple OSS Distributions /* Tell the main thread we're all set up, then wait for permission to exit */ 939*27b03b36SApple OSS Distributions dispatch_semaphore_signal(async_ts->child_ready_sem); 940*27b03b36SApple OSS Distributions dispatch_semaphore_wait(async_ts->child_exit_sem, DISPATCH_TIME_FOREVER); 941*27b03b36SApple OSS Distributions usleep(1); /* make sure we don't tailcall semaphore_wait */ 942*27b03b36SApple OSS Distributions} 943*27b03b36SApple OSS Distributions 944*27b03b36SApple OSS Distributionsstatic void * 945*27b03b36SApple OSS Distributionsasyncstack_thread(void *arg) 946*27b03b36SApple OSS Distributions{ 947*27b03b36SApple OSS Distributions uint64_t *fp = __builtin_frame_address(0); 948*27b03b36SApple OSS Distributions // We cannot use a variable of pointer type, because this ABI is valid 949*27b03b36SApple OSS Distributions // on arm64_32 where pointers are 32bits, but the context pointer will 950*27b03b36SApple OSS Distributions // still be stored in a 64bits slot on the stack. 951*27b03b36SApple OSS Distributions#if __has_feature(ptrauth_calls) 952*27b03b36SApple OSS Distributions#define __stack_context_auth __ptrauth(ptrauth_key_process_dependent_data, 1, \ 953*27b03b36SApple OSS Distributions 0xc31a) 954*27b03b36SApple OSS Distributions struct fake_async_context * __stack_context_auth ctx = &level2; 955*27b03b36SApple OSS Distributions#else // __has_feature(ptrauth_calls) 956*27b03b36SApple OSS Distributions /* struct fake_async_context * */uint64_t ctx = (uintptr_t)&level2; 957*27b03b36SApple OSS Distributions#endif // !__has_feature(ptrauth_calls) 958*27b03b36SApple OSS Distributions 959*27b03b36SApple OSS Distributions // The signature of an async frame on the OS stack is: 960*27b03b36SApple OSS Distributions // [ <AsyncContext address>, <Saved FP | (1<<60)>, <return address> ] 961*27b03b36SApple OSS Distributions // The Async context must be right before the saved FP on the stack. This 962*27b03b36SApple OSS Distributions // should happen naturally in an optimized build as it is the only 963*27b03b36SApple OSS Distributions // variable on the stack. 964*27b03b36SApple OSS Distributions // This function cannot use T_ASSERT_* becuse it changes the stack 965*27b03b36SApple OSS Distributions // layout. 966*27b03b36SApple OSS Distributions assert((uintptr_t)fp - (uintptr_t)&ctx == 8); 967*27b03b36SApple OSS Distributions 968*27b03b36SApple OSS Distributions // Modify the saved FP on the stack to include the async frame marker 969*27b03b36SApple OSS Distributions *fp |= (0x1ULL << 60); 970*27b03b36SApple OSS Distributions expect_asyncstack(arg); 971*27b03b36SApple OSS Distributions return NULL; 972*27b03b36SApple OSS Distributions} 973*27b03b36SApple OSS Distributions 974*27b03b36SApple OSS DistributionsT_DECL(asyncstack, "test swift async stack entries") 975*27b03b36SApple OSS Distributions{ 976*27b03b36SApple OSS Distributions struct scenario scenario = { 977*27b03b36SApple OSS Distributions .name = "asyncstack", 978*27b03b36SApple OSS Distributions .flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_SAVE_LOADINFO, 979*27b03b36SApple OSS Distributions }; 980*27b03b36SApple OSS Distributions struct async_test_semaphores async_ts = { 981*27b03b36SApple OSS Distributions .child_ready_sem = dispatch_semaphore_create(0), 982*27b03b36SApple OSS Distributions .child_exit_sem = dispatch_semaphore_create(0), 983*27b03b36SApple OSS Distributions }; 984*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(async_ts.child_ready_sem, "child_ready_sem alloc"); 985*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(async_ts.child_exit_sem, "child_exit_sem alloc"); 986*27b03b36SApple OSS Distributions 987*27b03b36SApple OSS Distributions pthread_t pthread; 988*27b03b36SApple OSS Distributions __block uint64_t threadid = 0; 989*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&pthread, NULL, asyncstack_thread, &async_ts), "pthread_create"); 990*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_threadid_np(pthread, &threadid), "pthread_threadid_np"); 991*27b03b36SApple OSS Distributions 992*27b03b36SApple OSS Distributions dispatch_semaphore_wait(async_ts.child_ready_sem, DISPATCH_TIME_FOREVER); 993*27b03b36SApple OSS Distributions 994*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^( void *ssbuf, size_t sslen) { 995*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_ASYNCSTACK, ssbuf, sslen, @{ 996*27b03b36SApple OSS Distributions asyncstack_expected_threadid_key: @(threadid), 997*27b03b36SApple OSS Distributions asyncstack_expected_stack_key: @[ @(asyncstack_frame(level2_func)), @(asyncstack_frame(level1_func)) ], 998*27b03b36SApple OSS Distributions }); 999*27b03b36SApple OSS Distributions }); 1000*27b03b36SApple OSS Distributions 1001*27b03b36SApple OSS Distributions dispatch_semaphore_signal(async_ts.child_exit_sem); 1002*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_join(pthread, NULL), "wait for thread"); 1003*27b03b36SApple OSS Distributions 1004*27b03b36SApple OSS Distributions} 1005*27b03b36SApple OSS Distributions#endif 1006*27b03b36SApple OSS Distributions 1007*27b03b36SApple OSS Distributionsstatic uint32_t 1008*27b03b36SApple OSS Distributionsget_user_promotion_basepri(void) 1009*27b03b36SApple OSS Distributions{ 1010*27b03b36SApple OSS Distributions mach_msg_type_number_t count = THREAD_POLICY_STATE_COUNT; 1011*27b03b36SApple OSS Distributions struct thread_policy_state thread_policy; 1012*27b03b36SApple OSS Distributions boolean_t get_default = FALSE; 1013*27b03b36SApple OSS Distributions mach_port_t thread_port = pthread_mach_thread_np(pthread_self()); 1014*27b03b36SApple OSS Distributions 1015*27b03b36SApple OSS Distributions kern_return_t kr = thread_policy_get(thread_port, THREAD_POLICY_STATE, 1016*27b03b36SApple OSS Distributions (thread_policy_t)&thread_policy, &count, &get_default); 1017*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get"); 1018*27b03b36SApple OSS Distributions return thread_policy.thps_user_promotion_basepri; 1019*27b03b36SApple OSS Distributions} 1020*27b03b36SApple OSS Distributions 1021*27b03b36SApple OSS Distributionsstatic int 1022*27b03b36SApple OSS Distributionsget_pri(thread_t thread_port) 1023*27b03b36SApple OSS Distributions{ 1024*27b03b36SApple OSS Distributions kern_return_t kr; 1025*27b03b36SApple OSS Distributions 1026*27b03b36SApple OSS Distributions thread_extended_info_data_t extended_info; 1027*27b03b36SApple OSS Distributions mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT; 1028*27b03b36SApple OSS Distributions kr = thread_info(thread_port, THREAD_EXTENDED_INFO, 1029*27b03b36SApple OSS Distributions (thread_info_t)&extended_info, &count); 1030*27b03b36SApple OSS Distributions 1031*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info"); 1032*27b03b36SApple OSS Distributions 1033*27b03b36SApple OSS Distributions return extended_info.pth_curpri; 1034*27b03b36SApple OSS Distributions} 1035*27b03b36SApple OSS Distributions 1036*27b03b36SApple OSS Distributions 1037*27b03b36SApple OSS DistributionsT_DECL(turnstile_singlehop, "turnstile single hop test") 1038*27b03b36SApple OSS Distributions{ 1039*27b03b36SApple OSS Distributions dispatch_queue_t dq1, dq2; 1040*27b03b36SApple OSS Distributions dispatch_semaphore_t sema_x; 1041*27b03b36SApple OSS Distributions dispatch_queue_attr_t dq1_attr, dq2_attr; 1042*27b03b36SApple OSS Distributions __block qos_class_t main_qos = 0; 1043*27b03b36SApple OSS Distributions __block int main_relpri = 0, main_relpri2 = 0, main_afterpri = 0; 1044*27b03b36SApple OSS Distributions struct scenario scenario = { 1045*27b03b36SApple OSS Distributions .name = "turnstile_singlehop", 1046*27b03b36SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 1047*27b03b36SApple OSS Distributions }; 1048*27b03b36SApple OSS Distributions dq1_attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0); 1049*27b03b36SApple OSS Distributions dq2_attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0); 1050*27b03b36SApple OSS Distributions pthread_mutex_t lock_a = PTHREAD_MUTEX_INITIALIZER; 1051*27b03b36SApple OSS Distributions pthread_mutex_t lock_b = PTHREAD_MUTEX_INITIALIZER; 1052*27b03b36SApple OSS Distributions 1053*27b03b36SApple OSS Distributions pthread_mutex_t *lockap = &lock_a, *lockbp = &lock_b; 1054*27b03b36SApple OSS Distributions 1055*27b03b36SApple OSS Distributions dq1 = dispatch_queue_create("q1", dq1_attr); 1056*27b03b36SApple OSS Distributions dq2 = dispatch_queue_create("q2", dq2_attr); 1057*27b03b36SApple OSS Distributions sema_x = dispatch_semaphore_create(0); 1058*27b03b36SApple OSS Distributions 1059*27b03b36SApple OSS Distributions pthread_mutex_lock(lockap); 1060*27b03b36SApple OSS Distributions dispatch_async(dq1, ^{ 1061*27b03b36SApple OSS Distributions pthread_mutex_lock(lockbp); 1062*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pthread_get_qos_class_np(pthread_self(), &main_qos, &main_relpri), "get qos class"); 1063*27b03b36SApple OSS Distributions T_LOG("The priority of q1 is %d\n", get_pri(mach_thread_self())); 1064*27b03b36SApple OSS Distributions dispatch_semaphore_signal(sema_x); 1065*27b03b36SApple OSS Distributions pthread_mutex_lock(lockap); 1066*27b03b36SApple OSS Distributions }); 1067*27b03b36SApple OSS Distributions dispatch_semaphore_wait(sema_x, DISPATCH_TIME_FOREVER); 1068*27b03b36SApple OSS Distributions 1069*27b03b36SApple OSS Distributions T_LOG("Async1 completed"); 1070*27b03b36SApple OSS Distributions 1071*27b03b36SApple OSS Distributions pthread_set_qos_class_self_np(QOS_CLASS_UTILITY, 0); 1072*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pthread_get_qos_class_np(pthread_self(), &main_qos, &main_relpri), "get qos class"); 1073*27b03b36SApple OSS Distributions T_LOG("The priority of main is %d\n", get_pri(mach_thread_self())); 1074*27b03b36SApple OSS Distributions main_relpri = get_pri(mach_thread_self()); 1075*27b03b36SApple OSS Distributions 1076*27b03b36SApple OSS Distributions dispatch_async(dq2, ^{ 1077*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pthread_get_qos_class_np(pthread_self(), &main_qos, &main_relpri2), "get qos class"); 1078*27b03b36SApple OSS Distributions T_LOG("The priority of q2 is %d\n", get_pri(mach_thread_self())); 1079*27b03b36SApple OSS Distributions dispatch_semaphore_signal(sema_x); 1080*27b03b36SApple OSS Distributions pthread_mutex_lock(lockbp); 1081*27b03b36SApple OSS Distributions }); 1082*27b03b36SApple OSS Distributions dispatch_semaphore_wait(sema_x, DISPATCH_TIME_FOREVER); 1083*27b03b36SApple OSS Distributions 1084*27b03b36SApple OSS Distributions T_LOG("Async2 completed"); 1085*27b03b36SApple OSS Distributions 1086*27b03b36SApple OSS Distributions while (1) { 1087*27b03b36SApple OSS Distributions main_afterpri = (int) get_user_promotion_basepri(); 1088*27b03b36SApple OSS Distributions if (main_relpri != main_afterpri) { 1089*27b03b36SApple OSS Distributions T_LOG("Success with promotion pri is %d", main_afterpri); 1090*27b03b36SApple OSS Distributions break; 1091*27b03b36SApple OSS Distributions } 1092*27b03b36SApple OSS Distributions 1093*27b03b36SApple OSS Distributions usleep(100); 1094*27b03b36SApple OSS Distributions } 1095*27b03b36SApple OSS Distributions 1096*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^( void *ssbuf, size_t sslen) { 1097*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_TURNSTILEINFO, ssbuf, sslen, nil); 1098*27b03b36SApple OSS Distributions }); 1099*27b03b36SApple OSS Distributions} 1100*27b03b36SApple OSS Distributions 1101*27b03b36SApple OSS Distributions 1102*27b03b36SApple OSS Distributionsstatic void 1103*27b03b36SApple OSS Distributionsexpect_instrs_cycles_in_stackshot(void *ssbuf, size_t sslen) 1104*27b03b36SApple OSS Distributions{ 1105*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1106*27b03b36SApple OSS Distributions 1107*27b03b36SApple OSS Distributions bool in_task = false; 1108*27b03b36SApple OSS Distributions bool in_thread = false; 1109*27b03b36SApple OSS Distributions bool saw_instrs_cycles = false; 1110*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 1111*27b03b36SApple OSS Distributions 1112*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1113*27b03b36SApple OSS Distributions switch (kcdata_iter_type(iter)) { 1114*27b03b36SApple OSS Distributions case KCDATA_TYPE_CONTAINER_BEGIN: 1115*27b03b36SApple OSS Distributions switch (kcdata_iter_container_type(iter)) { 1116*27b03b36SApple OSS Distributions case STACKSHOT_KCCONTAINER_TASK: 1117*27b03b36SApple OSS Distributions in_task = true; 1118*27b03b36SApple OSS Distributions saw_instrs_cycles = false; 1119*27b03b36SApple OSS Distributions break; 1120*27b03b36SApple OSS Distributions 1121*27b03b36SApple OSS Distributions case STACKSHOT_KCCONTAINER_THREAD: 1122*27b03b36SApple OSS Distributions in_thread = true; 1123*27b03b36SApple OSS Distributions saw_instrs_cycles = false; 1124*27b03b36SApple OSS Distributions break; 1125*27b03b36SApple OSS Distributions 1126*27b03b36SApple OSS Distributions default: 1127*27b03b36SApple OSS Distributions break; 1128*27b03b36SApple OSS Distributions } 1129*27b03b36SApple OSS Distributions break; 1130*27b03b36SApple OSS Distributions 1131*27b03b36SApple OSS Distributions case STACKSHOT_KCTYPE_INSTRS_CYCLES: 1132*27b03b36SApple OSS Distributions saw_instrs_cycles = true; 1133*27b03b36SApple OSS Distributions break; 1134*27b03b36SApple OSS Distributions 1135*27b03b36SApple OSS Distributions case KCDATA_TYPE_CONTAINER_END: 1136*27b03b36SApple OSS Distributions if (in_thread) { 1137*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles, 1138*27b03b36SApple OSS Distributions "saw instructions and cycles in thread"); 1139*27b03b36SApple OSS Distributions in_thread = false; 1140*27b03b36SApple OSS Distributions } else if (in_task) { 1141*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles, 1142*27b03b36SApple OSS Distributions "saw instructions and cycles in task"); 1143*27b03b36SApple OSS Distributions in_task = false; 1144*27b03b36SApple OSS Distributions } 1145*27b03b36SApple OSS Distributions 1146*27b03b36SApple OSS Distributions default: 1147*27b03b36SApple OSS Distributions break; 1148*27b03b36SApple OSS Distributions } 1149*27b03b36SApple OSS Distributions } 1150*27b03b36SApple OSS Distributions} 1151*27b03b36SApple OSS Distributions 1152*27b03b36SApple OSS Distributionsstatic void 1153*27b03b36SApple OSS Distributionsskip_if_monotonic_unsupported(void) 1154*27b03b36SApple OSS Distributions{ 1155*27b03b36SApple OSS Distributions int supported = 0; 1156*27b03b36SApple OSS Distributions size_t supported_size = sizeof(supported); 1157*27b03b36SApple OSS Distributions int ret = sysctlbyname("kern.monotonic.supported", &supported, 1158*27b03b36SApple OSS Distributions &supported_size, 0, 0); 1159*27b03b36SApple OSS Distributions if (ret < 0 || !supported) { 1160*27b03b36SApple OSS Distributions T_SKIP("monotonic is unsupported"); 1161*27b03b36SApple OSS Distributions } 1162*27b03b36SApple OSS Distributions} 1163*27b03b36SApple OSS Distributions 1164*27b03b36SApple OSS DistributionsT_DECL(instrs_cycles, "test a getting instructions and cycles in stackshot") 1165*27b03b36SApple OSS Distributions{ 1166*27b03b36SApple OSS Distributions skip_if_monotonic_unsupported(); 1167*27b03b36SApple OSS Distributions 1168*27b03b36SApple OSS Distributions struct scenario scenario = { 1169*27b03b36SApple OSS Distributions .name = "instrs-cycles", 1170*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES 1171*27b03b36SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1172*27b03b36SApple OSS Distributions }; 1173*27b03b36SApple OSS Distributions 1174*27b03b36SApple OSS Distributions T_LOG("attempting to take stackshot with instructions and cycles"); 1175*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1176*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 1177*27b03b36SApple OSS Distributions expect_instrs_cycles_in_stackshot(ssbuf, sslen); 1178*27b03b36SApple OSS Distributions }); 1179*27b03b36SApple OSS Distributions} 1180*27b03b36SApple OSS Distributions 1181*27b03b36SApple OSS DistributionsT_DECL(delta_instrs_cycles, 1182*27b03b36SApple OSS Distributions "test delta stackshots with instructions and cycles") 1183*27b03b36SApple OSS Distributions{ 1184*27b03b36SApple OSS Distributions skip_if_monotonic_unsupported(); 1185*27b03b36SApple OSS Distributions 1186*27b03b36SApple OSS Distributions struct scenario scenario = { 1187*27b03b36SApple OSS Distributions .name = "delta-instrs-cycles", 1188*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES 1189*27b03b36SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1190*27b03b36SApple OSS Distributions }; 1191*27b03b36SApple OSS Distributions 1192*27b03b36SApple OSS Distributions T_LOG("taking full stackshot"); 1193*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1194*27b03b36SApple OSS Distributions uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen); 1195*27b03b36SApple OSS Distributions 1196*27b03b36SApple OSS Distributions T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time); 1197*27b03b36SApple OSS Distributions 1198*27b03b36SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 1199*27b03b36SApple OSS Distributions expect_instrs_cycles_in_stackshot(ssbuf, sslen); 1200*27b03b36SApple OSS Distributions 1201*27b03b36SApple OSS Distributions struct scenario delta_scenario = { 1202*27b03b36SApple OSS Distributions .name = "delta-instrs-cycles-next", 1203*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES 1204*27b03b36SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT 1205*27b03b36SApple OSS Distributions | STACKSHOT_COLLECT_DELTA_SNAPSHOT), 1206*27b03b36SApple OSS Distributions .since_timestamp = stackshot_time, 1207*27b03b36SApple OSS Distributions }; 1208*27b03b36SApple OSS Distributions 1209*27b03b36SApple OSS Distributions take_stackshot(&delta_scenario, false, ^(void *dssbuf, size_t dsslen) { 1210*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DELTA, dssbuf, dsslen, nil); 1211*27b03b36SApple OSS Distributions expect_instrs_cycles_in_stackshot(dssbuf, dsslen); 1212*27b03b36SApple OSS Distributions }); 1213*27b03b36SApple OSS Distributions }); 1214*27b03b36SApple OSS Distributions} 1215*27b03b36SApple OSS Distributions 1216*27b03b36SApple OSS Distributionsstatic void 1217*27b03b36SApple OSS Distributionscheck_thread_groups_supported() 1218*27b03b36SApple OSS Distributions{ 1219*27b03b36SApple OSS Distributions int err; 1220*27b03b36SApple OSS Distributions int supported = 0; 1221*27b03b36SApple OSS Distributions size_t supported_size = sizeof(supported); 1222*27b03b36SApple OSS Distributions err = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size, NULL, 0); 1223*27b03b36SApple OSS Distributions 1224*27b03b36SApple OSS Distributions if (err || !supported) 1225*27b03b36SApple OSS Distributions T_SKIP("thread groups not supported on this system"); 1226*27b03b36SApple OSS Distributions} 1227*27b03b36SApple OSS Distributions 1228*27b03b36SApple OSS DistributionsT_DECL(thread_groups, "test getting thread groups in stackshot") 1229*27b03b36SApple OSS Distributions{ 1230*27b03b36SApple OSS Distributions check_thread_groups_supported(); 1231*27b03b36SApple OSS Distributions 1232*27b03b36SApple OSS Distributions struct scenario scenario = { 1233*27b03b36SApple OSS Distributions .name = "thread-groups", 1234*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_THREAD_GROUP 1235*27b03b36SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1236*27b03b36SApple OSS Distributions }; 1237*27b03b36SApple OSS Distributions 1238*27b03b36SApple OSS Distributions T_LOG("attempting to take stackshot with thread group flag"); 1239*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1240*27b03b36SApple OSS Distributions parse_thread_group_stackshot(ssbuf, sslen); 1241*27b03b36SApple OSS Distributions }); 1242*27b03b36SApple OSS Distributions} 1243*27b03b36SApple OSS Distributions 1244*27b03b36SApple OSS Distributionsstatic void 1245*27b03b36SApple OSS Distributionsparse_page_table_asid_stackshot(void **ssbuf, size_t sslen) 1246*27b03b36SApple OSS Distributions{ 1247*27b03b36SApple OSS Distributions bool seen_asid = false; 1248*27b03b36SApple OSS Distributions bool seen_page_table_snapshot = false; 1249*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1250*27b03b36SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, 1251*27b03b36SApple OSS Distributions "buffer provided is a stackshot"); 1252*27b03b36SApple OSS Distributions 1253*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 1254*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1255*27b03b36SApple OSS Distributions switch (kcdata_iter_type(iter)) { 1256*27b03b36SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 1257*27b03b36SApple OSS Distributions T_QUIET; 1258*27b03b36SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_array_valid(iter), 1259*27b03b36SApple OSS Distributions "checked that array is valid"); 1260*27b03b36SApple OSS Distributions 1261*27b03b36SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_PAGE_TABLES) { 1262*27b03b36SApple OSS Distributions continue; 1263*27b03b36SApple OSS Distributions } 1264*27b03b36SApple OSS Distributions 1265*27b03b36SApple OSS Distributions T_ASSERT_FALSE(seen_page_table_snapshot, "check that we haven't yet seen a page table snapshot"); 1266*27b03b36SApple OSS Distributions seen_page_table_snapshot = true; 1267*27b03b36SApple OSS Distributions 1268*27b03b36SApple OSS Distributions T_ASSERT_EQ((size_t) kcdata_iter_array_elem_size(iter), sizeof(uint64_t), 1269*27b03b36SApple OSS Distributions "check that each element of the pagetable dump is the expected size"); 1270*27b03b36SApple OSS Distributions 1271*27b03b36SApple OSS Distributions uint64_t *pt_array = kcdata_iter_payload(iter); 1272*27b03b36SApple OSS Distributions uint32_t elem_count = kcdata_iter_array_elem_count(iter); 1273*27b03b36SApple OSS Distributions uint32_t j; 1274*27b03b36SApple OSS Distributions bool nonzero_tte = false; 1275*27b03b36SApple OSS Distributions for (j = 0; j < elem_count;) { 1276*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE(j + 4, elem_count, "check for valid page table segment header"); 1277*27b03b36SApple OSS Distributions uint64_t pa = pt_array[j]; 1278*27b03b36SApple OSS Distributions uint64_t num_entries = pt_array[j + 1]; 1279*27b03b36SApple OSS Distributions uint64_t start_va = pt_array[j + 2]; 1280*27b03b36SApple OSS Distributions uint64_t end_va = pt_array[j + 3]; 1281*27b03b36SApple OSS Distributions 1282*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(pa, (uint64_t) 0, "check that the pagetable physical address is non-zero"); 1283*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(pa % (num_entries * sizeof(uint64_t)), (uint64_t) 0, "check that the pagetable physical address is correctly aligned"); 1284*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(num_entries, (uint64_t) 0, "check that a pagetable region has more than 0 entries"); 1285*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE(j + 4 + num_entries, (uint64_t) elem_count, "check for sufficient space in page table array"); 1286*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(end_va, start_va, "check for valid VA bounds in page table segment header"); 1287*27b03b36SApple OSS Distributions 1288*27b03b36SApple OSS Distributions for (uint32_t k = j + 4; k < (j + 4 + num_entries); ++k) { 1289*27b03b36SApple OSS Distributions if (pt_array[k] != 0) { 1290*27b03b36SApple OSS Distributions nonzero_tte = true; 1291*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ((pt_array[k] >> 48) & 0xf, (uint64_t) 0, "check that bits[48:51] of arm64 TTE are clear"); 1292*27b03b36SApple OSS Distributions // L0-L2 table and non-compressed L3 block entries should always have bit 1 set; assumes L0-L2 blocks will not be used outside the kernel 1293*27b03b36SApple OSS Distributions bool table = ((pt_array[k] & 0x2) != 0); 1294*27b03b36SApple OSS Distributions if (table) { 1295*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(pt_array[k] & ((1ULL << 48) - 1) & ~((1ULL << 12) - 1), (uint64_t) 0, "check that arm64 TTE physical address is non-zero"); 1296*27b03b36SApple OSS Distributions } else { // should be a compressed PTE 1297*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(pt_array[k] & 0xC000000000000000ULL, (uint64_t) 0, "check that compressed PTE has at least one of bits [63:62] set"); 1298*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(pt_array[k] & ~0xC000000000000000ULL, (uint64_t) 0, "check that compressed PTE has no other bits besides [63:62] set"); 1299*27b03b36SApple OSS Distributions } 1300*27b03b36SApple OSS Distributions } 1301*27b03b36SApple OSS Distributions } 1302*27b03b36SApple OSS Distributions 1303*27b03b36SApple OSS Distributions j += (4 + num_entries); 1304*27b03b36SApple OSS Distributions } 1305*27b03b36SApple OSS Distributions T_ASSERT_TRUE(nonzero_tte, "check that we saw at least one non-empty TTE"); 1306*27b03b36SApple OSS Distributions T_ASSERT_EQ(j, elem_count, "check that page table dump size matches extent of last header"); 1307*27b03b36SApple OSS Distributions break; 1308*27b03b36SApple OSS Distributions } 1309*27b03b36SApple OSS Distributions case STACKSHOT_KCTYPE_ASID: { 1310*27b03b36SApple OSS Distributions T_ASSERT_FALSE(seen_asid, "check that we haven't yet seen an ASID"); 1311*27b03b36SApple OSS Distributions seen_asid = true; 1312*27b03b36SApple OSS Distributions } 1313*27b03b36SApple OSS Distributions } 1314*27b03b36SApple OSS Distributions } 1315*27b03b36SApple OSS Distributions T_ASSERT_TRUE(seen_page_table_snapshot, "check that we have seen a page table snapshot"); 1316*27b03b36SApple OSS Distributions T_ASSERT_TRUE(seen_asid, "check that we have seen an ASID"); 1317*27b03b36SApple OSS Distributions} 1318*27b03b36SApple OSS Distributions 1319*27b03b36SApple OSS DistributionsT_DECL(dump_page_tables, "test stackshot page table dumping support") 1320*27b03b36SApple OSS Distributions{ 1321*27b03b36SApple OSS Distributions struct scenario scenario = { 1322*27b03b36SApple OSS Distributions .name = "asid-page-tables", 1323*27b03b36SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT | STACKSHOT_ASID | STACKSHOT_PAGE_TABLES), 1324*27b03b36SApple OSS Distributions .size_hint = (9ull << 20), // 9 MB 1325*27b03b36SApple OSS Distributions .target_pid = getpid(), 1326*27b03b36SApple OSS Distributions .maybe_unsupported = true, 1327*27b03b36SApple OSS Distributions .maybe_enomem = true, 1328*27b03b36SApple OSS Distributions }; 1329*27b03b36SApple OSS Distributions 1330*27b03b36SApple OSS Distributions T_LOG("attempting to take stackshot with ASID and page table flags"); 1331*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1332*27b03b36SApple OSS Distributions parse_page_table_asid_stackshot(ssbuf, sslen); 1333*27b03b36SApple OSS Distributions }); 1334*27b03b36SApple OSS Distributions} 1335*27b03b36SApple OSS Distributions 1336*27b03b36SApple OSS Distributionsstatic void stackshot_verify_current_proc_uuid_info(void **ssbuf, size_t sslen, uint64_t expected_offset, const struct proc_uniqidentifierinfo *proc_info_data) 1337*27b03b36SApple OSS Distributions{ 1338*27b03b36SApple OSS Distributions const uuid_t *current_uuid = (const uuid_t *)(&proc_info_data->p_uuid); 1339*27b03b36SApple OSS Distributions 1340*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1341*27b03b36SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "buffer provided is a stackshot"); 1342*27b03b36SApple OSS Distributions 1343*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 1344*27b03b36SApple OSS Distributions 1345*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1346*27b03b36SApple OSS Distributions switch (kcdata_iter_type(iter)) { 1347*27b03b36SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 1348*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(kcdata_iter_array_valid(iter), "checked that array is valid"); 1349*27b03b36SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) == KCDATA_TYPE_LIBRARY_LOADINFO64) { 1350*27b03b36SApple OSS Distributions struct user64_dyld_uuid_info *info = (struct user64_dyld_uuid_info *) kcdata_iter_payload(iter); 1351*27b03b36SApple OSS Distributions if (uuid_compare(*current_uuid, info->imageUUID) == 0) { 1352*27b03b36SApple OSS Distributions T_ASSERT_EQ(expected_offset, info->imageLoadAddress, "found matching UUID with matching binary offset"); 1353*27b03b36SApple OSS Distributions return; 1354*27b03b36SApple OSS Distributions } 1355*27b03b36SApple OSS Distributions } else if (kcdata_iter_array_elem_type(iter) == KCDATA_TYPE_LIBRARY_LOADINFO) { 1356*27b03b36SApple OSS Distributions struct user32_dyld_uuid_info *info = (struct user32_dyld_uuid_info *) kcdata_iter_payload(iter); 1357*27b03b36SApple OSS Distributions if (uuid_compare(*current_uuid, info->imageUUID) == 0) { 1358*27b03b36SApple OSS Distributions T_ASSERT_EQ(expected_offset, ((uint64_t) info->imageLoadAddress), "found matching UUID with matching binary offset"); 1359*27b03b36SApple OSS Distributions return; 1360*27b03b36SApple OSS Distributions } 1361*27b03b36SApple OSS Distributions } 1362*27b03b36SApple OSS Distributions break; 1363*27b03b36SApple OSS Distributions } 1364*27b03b36SApple OSS Distributions default: 1365*27b03b36SApple OSS Distributions break; 1366*27b03b36SApple OSS Distributions } 1367*27b03b36SApple OSS Distributions } 1368*27b03b36SApple OSS Distributions 1369*27b03b36SApple OSS Distributions T_FAIL("failed to find matching UUID in stackshot data"); 1370*27b03b36SApple OSS Distributions} 1371*27b03b36SApple OSS Distributions 1372*27b03b36SApple OSS DistributionsT_DECL(translated, "tests translated bit is set correctly") 1373*27b03b36SApple OSS Distributions{ 1374*27b03b36SApple OSS Distributions#if !(TARGET_OS_OSX && TARGET_CPU_ARM64) 1375*27b03b36SApple OSS Distributions T_SKIP("Only valid on Apple silicon Macs") 1376*27b03b36SApple OSS Distributions#endif 1377*27b03b36SApple OSS Distributions // Get path of stackshot_translated_child helper binary 1378*27b03b36SApple OSS Distributions char path[PATH_MAX]; 1379*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 1380*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 1381*27b03b36SApple OSS Distributions char* binary_name = strrchr(path, '/'); 1382*27b03b36SApple OSS Distributions if (binary_name) binary_name++; 1383*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(binary_name, "Find basename in path '%s'", path); 1384*27b03b36SApple OSS Distributions strlcpy(binary_name, "stackshot_translated_child", path_size - (binary_name - path)); 1385*27b03b36SApple OSS Distributions char *args[] = { path, NULL }; 1386*27b03b36SApple OSS Distributions 1387*27b03b36SApple OSS Distributions dispatch_source_t child_sig_src; 1388*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 1389*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "exec child semaphore"); 1390*27b03b36SApple OSS Distributions 1391*27b03b36SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 1392*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 1393*27b03b36SApple OSS Distributions 1394*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 1395*27b03b36SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 1396*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 1397*27b03b36SApple OSS Distributions 1398*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 1399*27b03b36SApple OSS Distributions dispatch_activate(child_sig_src); 1400*27b03b36SApple OSS Distributions 1401*27b03b36SApple OSS Distributions // Spawn child 1402*27b03b36SApple OSS Distributions pid_t pid; 1403*27b03b36SApple OSS Distributions T_LOG("spawning translated child"); 1404*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(posix_spawn(&pid, args[0], NULL, NULL, args, NULL), "spawned process '%s' with PID %d", args[0], pid); 1405*27b03b36SApple OSS Distributions 1406*27b03b36SApple OSS Distributions // Wait for the the child to spawn up 1407*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 1408*27b03b36SApple OSS Distributions 1409*27b03b36SApple OSS Distributions // Make sure the child is running and is translated 1410*27b03b36SApple OSS Distributions int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid }; 1411*27b03b36SApple OSS Distributions struct kinfo_proc process_info; 1412*27b03b36SApple OSS Distributions size_t bufsize = sizeof(process_info); 1413*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctl(mib, (unsigned)(sizeof(mib)/sizeof(int)), &process_info, &bufsize, NULL, 0), "get translated child process info"); 1414*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(bufsize, (size_t)0, "process info is not empty"); 1415*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE((process_info.kp_proc.p_flag & P_TRANSLATED), "KERN_PROC_PID reports child is translated"); 1416*27b03b36SApple OSS Distributions 1417*27b03b36SApple OSS Distributions T_LOG("capturing stackshot"); 1418*27b03b36SApple OSS Distributions 1419*27b03b36SApple OSS Distributions struct scenario scenario = { 1420*27b03b36SApple OSS Distributions .name = "translated", 1421*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 1422*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 1423*27b03b36SApple OSS Distributions }; 1424*27b03b36SApple OSS Distributions 1425*27b03b36SApple OSS Distributions take_stackshot(&scenario, true, ^( void *ssbuf, size_t sslen) { 1426*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_TRANSLATED, ssbuf, sslen, @{translated_child_pid_key: @(pid)}); 1427*27b03b36SApple OSS Distributions }); 1428*27b03b36SApple OSS Distributions 1429*27b03b36SApple OSS Distributions // Kill the child 1430*27b03b36SApple OSS Distributions int status; 1431*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGTERM), "kill translated child"); 1432*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on translated child"); 1433*27b03b36SApple OSS Distributions 1434*27b03b36SApple OSS Distributions} 1435*27b03b36SApple OSS Distributions 1436*27b03b36SApple OSS DistributionsT_DECL(proc_uuid_info, "tests that the main binary UUID for a proc is always populated") 1437*27b03b36SApple OSS Distributions{ 1438*27b03b36SApple OSS Distributions struct proc_uniqidentifierinfo proc_info_data = { }; 1439*27b03b36SApple OSS Distributions mach_msg_type_number_t count; 1440*27b03b36SApple OSS Distributions kern_return_t kernel_status; 1441*27b03b36SApple OSS Distributions task_dyld_info_data_t task_dyld_info; 1442*27b03b36SApple OSS Distributions struct dyld_all_image_infos *target_infos; 1443*27b03b36SApple OSS Distributions int retval; 1444*27b03b36SApple OSS Distributions bool found_image_in_image_infos = false; 1445*27b03b36SApple OSS Distributions uint64_t expected_mach_header_offset = 0; 1446*27b03b36SApple OSS Distributions 1447*27b03b36SApple OSS Distributions /* Find the UUID of our main binary */ 1448*27b03b36SApple OSS Distributions retval = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &proc_info_data, sizeof(proc_info_data)); 1449*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(retval, "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO"); 1450*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ_INT(retval, (int) sizeof(proc_info_data), "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO returned data"); 1451*27b03b36SApple OSS Distributions 1452*27b03b36SApple OSS Distributions uuid_string_t str = {}; 1453*27b03b36SApple OSS Distributions uuid_unparse(*(uuid_t*)&proc_info_data.p_uuid, str); 1454*27b03b36SApple OSS Distributions T_LOG("Found current UUID is %s", str); 1455*27b03b36SApple OSS Distributions 1456*27b03b36SApple OSS Distributions /* Find the location of the dyld image info metadata */ 1457*27b03b36SApple OSS Distributions count = TASK_DYLD_INFO_COUNT; 1458*27b03b36SApple OSS Distributions kernel_status = task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&task_dyld_info, &count); 1459*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(kernel_status, KERN_SUCCESS, "retrieve task_info for TASK_DYLD_INFO"); 1460*27b03b36SApple OSS Distributions 1461*27b03b36SApple OSS Distributions target_infos = (struct dyld_all_image_infos *)task_dyld_info.all_image_info_addr; 1462*27b03b36SApple OSS Distributions 1463*27b03b36SApple OSS Distributions /* Find our binary in the dyld image info array */ 1464*27b03b36SApple OSS Distributions for (int i = 0; i < (int) target_infos->uuidArrayCount; i++) { 1465*27b03b36SApple OSS Distributions if (uuid_compare(target_infos->uuidArray[i].imageUUID, *(uuid_t*)&proc_info_data.p_uuid) == 0) { 1466*27b03b36SApple OSS Distributions expected_mach_header_offset = (uint64_t) target_infos->uuidArray[i].imageLoadAddress; 1467*27b03b36SApple OSS Distributions found_image_in_image_infos = true; 1468*27b03b36SApple OSS Distributions } 1469*27b03b36SApple OSS Distributions } 1470*27b03b36SApple OSS Distributions 1471*27b03b36SApple OSS Distributions T_ASSERT_TRUE(found_image_in_image_infos, "found binary image in dyld image info list"); 1472*27b03b36SApple OSS Distributions 1473*27b03b36SApple OSS Distributions /* Overwrite the dyld image info data so the kernel has to fallback to the UUID stored in the proc structure */ 1474*27b03b36SApple OSS Distributions target_infos->uuidArrayCount = 0; 1475*27b03b36SApple OSS Distributions 1476*27b03b36SApple OSS Distributions struct scenario scenario = { 1477*27b03b36SApple OSS Distributions .name = "proc_uuid_info", 1478*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT), 1479*27b03b36SApple OSS Distributions .target_pid = getpid(), 1480*27b03b36SApple OSS Distributions }; 1481*27b03b36SApple OSS Distributions 1482*27b03b36SApple OSS Distributions T_LOG("attempting to take stackshot for current PID"); 1483*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1484*27b03b36SApple OSS Distributions stackshot_verify_current_proc_uuid_info(ssbuf, sslen, expected_mach_header_offset, &proc_info_data); 1485*27b03b36SApple OSS Distributions }); 1486*27b03b36SApple OSS Distributions} 1487*27b03b36SApple OSS Distributions 1488*27b03b36SApple OSS DistributionsT_DECL(cseg_waitinfo, "test that threads stuck in the compressor report correct waitinfo") 1489*27b03b36SApple OSS Distributions{ 1490*27b03b36SApple OSS Distributions struct scenario scenario = { 1491*27b03b36SApple OSS Distributions .name = "cseg_waitinfo", 1492*27b03b36SApple OSS Distributions .quiet = false, 1493*27b03b36SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 1494*27b03b36SApple OSS Distributions }; 1495*27b03b36SApple OSS Distributions __block uint64_t thread_id = 0; 1496*27b03b36SApple OSS Distributions 1497*27b03b36SApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot.cseg_waitinfo", NULL); 1498*27b03b36SApple OSS Distributions dispatch_semaphore_t child_ok = dispatch_semaphore_create(0); 1499*27b03b36SApple OSS Distributions 1500*27b03b36SApple OSS Distributions dispatch_async(dq, ^{ 1501*27b03b36SApple OSS Distributions pthread_threadid_np(NULL, &thread_id); 1502*27b03b36SApple OSS Distributions dispatch_semaphore_signal(child_ok); 1503*27b03b36SApple OSS Distributions int val = 1; 1504*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.cseg_wedge_thread", NULL, NULL, &val, sizeof(val)), "wedge child thread"); 1505*27b03b36SApple OSS Distributions }); 1506*27b03b36SApple OSS Distributions 1507*27b03b36SApple OSS Distributions dispatch_semaphore_wait(child_ok, DISPATCH_TIME_FOREVER); 1508*27b03b36SApple OSS Distributions sleep(1); 1509*27b03b36SApple OSS Distributions 1510*27b03b36SApple OSS Distributions T_LOG("taking stackshot"); 1511*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1512*27b03b36SApple OSS Distributions int val = 1; 1513*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.cseg_unwedge_thread", NULL, NULL, &val, sizeof(val)), "unwedge child thread"); 1514*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_CSEG, ssbuf, sslen, @{cseg_expected_threadid_key: @(thread_id)}); 1515*27b03b36SApple OSS Distributions }); 1516*27b03b36SApple OSS Distributions} 1517*27b03b36SApple OSS Distributions 1518*27b03b36SApple OSS Distributionsstatic void 1519*27b03b36SApple OSS Distributionssrp_send( 1520*27b03b36SApple OSS Distributions mach_port_t send_port, 1521*27b03b36SApple OSS Distributions mach_port_t reply_port, 1522*27b03b36SApple OSS Distributions mach_port_t msg_port) 1523*27b03b36SApple OSS Distributions{ 1524*27b03b36SApple OSS Distributions kern_return_t ret = 0; 1525*27b03b36SApple OSS Distributions 1526*27b03b36SApple OSS Distributions struct test_msg { 1527*27b03b36SApple OSS Distributions mach_msg_header_t header; 1528*27b03b36SApple OSS Distributions mach_msg_body_t body; 1529*27b03b36SApple OSS Distributions mach_msg_port_descriptor_t port_descriptor; 1530*27b03b36SApple OSS Distributions }; 1531*27b03b36SApple OSS Distributions struct test_msg send_msg = { 1532*27b03b36SApple OSS Distributions .header = { 1533*27b03b36SApple OSS Distributions .msgh_remote_port = send_port, 1534*27b03b36SApple OSS Distributions .msgh_local_port = reply_port, 1535*27b03b36SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 1536*27b03b36SApple OSS Distributions reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0, 1537*27b03b36SApple OSS Distributions MACH_MSG_TYPE_MOVE_SEND, 1538*27b03b36SApple OSS Distributions MACH_MSGH_BITS_COMPLEX), 1539*27b03b36SApple OSS Distributions .msgh_id = 0x100, 1540*27b03b36SApple OSS Distributions .msgh_size = sizeof(send_msg), 1541*27b03b36SApple OSS Distributions }, 1542*27b03b36SApple OSS Distributions .body = { 1543*27b03b36SApple OSS Distributions .msgh_descriptor_count = 1, 1544*27b03b36SApple OSS Distributions }, 1545*27b03b36SApple OSS Distributions .port_descriptor = { 1546*27b03b36SApple OSS Distributions .name = msg_port, 1547*27b03b36SApple OSS Distributions .disposition = MACH_MSG_TYPE_MOVE_RECEIVE, 1548*27b03b36SApple OSS Distributions .type = MACH_MSG_PORT_DESCRIPTOR, 1549*27b03b36SApple OSS Distributions }, 1550*27b03b36SApple OSS Distributions }; 1551*27b03b36SApple OSS Distributions 1552*27b03b36SApple OSS Distributions if (msg_port == MACH_PORT_NULL) { 1553*27b03b36SApple OSS Distributions send_msg.body.msgh_descriptor_count = 0; 1554*27b03b36SApple OSS Distributions } 1555*27b03b36SApple OSS Distributions 1556*27b03b36SApple OSS Distributions ret = mach_msg(&(send_msg.header), 1557*27b03b36SApple OSS Distributions MACH_SEND_MSG | 1558*27b03b36SApple OSS Distributions MACH_SEND_TIMEOUT | 1559*27b03b36SApple OSS Distributions MACH_SEND_OVERRIDE, 1560*27b03b36SApple OSS Distributions send_msg.header.msgh_size, 1561*27b03b36SApple OSS Distributions 0, 1562*27b03b36SApple OSS Distributions MACH_PORT_NULL, 1563*27b03b36SApple OSS Distributions 10000, 1564*27b03b36SApple OSS Distributions 0); 1565*27b03b36SApple OSS Distributions 1566*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "client mach_msg"); 1567*27b03b36SApple OSS Distributions} 1568*27b03b36SApple OSS Distributions 1569*27b03b36SApple OSS DistributionsT_HELPER_DECL(srp_client, 1570*27b03b36SApple OSS Distributions "Client used for the special_reply_port test") 1571*27b03b36SApple OSS Distributions{ 1572*27b03b36SApple OSS Distributions pid_t ppid = getppid(); 1573*27b03b36SApple OSS Distributions dispatch_semaphore_t can_continue = dispatch_semaphore_create(0); 1574*27b03b36SApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("client_signalqueue", NULL); 1575*27b03b36SApple OSS Distributions dispatch_source_t sig_src; 1576*27b03b36SApple OSS Distributions 1577*27b03b36SApple OSS Distributions mach_msg_return_t mr; 1578*27b03b36SApple OSS Distributions mach_port_t service_port; 1579*27b03b36SApple OSS Distributions mach_port_t conn_port; 1580*27b03b36SApple OSS Distributions mach_port_t special_reply_port; 1581*27b03b36SApple OSS Distributions mach_port_options_t opts = { 1582*27b03b36SApple OSS Distributions .flags = MPO_INSERT_SEND_RIGHT, 1583*27b03b36SApple OSS Distributions }; 1584*27b03b36SApple OSS Distributions 1585*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 1586*27b03b36SApple OSS Distributions sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 1587*27b03b36SApple OSS Distributions 1588*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(sig_src, ^{ 1589*27b03b36SApple OSS Distributions dispatch_semaphore_signal(can_continue); 1590*27b03b36SApple OSS Distributions }); 1591*27b03b36SApple OSS Distributions dispatch_activate(sig_src); 1592*27b03b36SApple OSS Distributions 1593*27b03b36SApple OSS Distributions /* lookup the mach service port for the parent */ 1594*27b03b36SApple OSS Distributions kern_return_t kr = bootstrap_look_up(bootstrap_port, 1595*27b03b36SApple OSS Distributions SRP_SERVICE_NAME, &service_port); 1596*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up"); 1597*27b03b36SApple OSS Distributions 1598*27b03b36SApple OSS Distributions /* create the send-once right (special reply port) and message to send to the server */ 1599*27b03b36SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0ull, &conn_port); 1600*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct"); 1601*27b03b36SApple OSS Distributions 1602*27b03b36SApple OSS Distributions special_reply_port = thread_get_special_reply_port(); 1603*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port"); 1604*27b03b36SApple OSS Distributions 1605*27b03b36SApple OSS Distributions /* send the message with the special reply port */ 1606*27b03b36SApple OSS Distributions srp_send(service_port, special_reply_port, conn_port); 1607*27b03b36SApple OSS Distributions 1608*27b03b36SApple OSS Distributions /* signal the parent to continue */ 1609*27b03b36SApple OSS Distributions kill(ppid, SIGUSR1); 1610*27b03b36SApple OSS Distributions 1611*27b03b36SApple OSS Distributions struct { 1612*27b03b36SApple OSS Distributions mach_msg_header_t header; 1613*27b03b36SApple OSS Distributions mach_msg_body_t body; 1614*27b03b36SApple OSS Distributions mach_msg_port_descriptor_t port_descriptor; 1615*27b03b36SApple OSS Distributions } rcv_msg = { 1616*27b03b36SApple OSS Distributions .header = 1617*27b03b36SApple OSS Distributions { 1618*27b03b36SApple OSS Distributions .msgh_remote_port = MACH_PORT_NULL, 1619*27b03b36SApple OSS Distributions .msgh_local_port = special_reply_port, 1620*27b03b36SApple OSS Distributions .msgh_size = sizeof(rcv_msg), 1621*27b03b36SApple OSS Distributions }, 1622*27b03b36SApple OSS Distributions }; 1623*27b03b36SApple OSS Distributions 1624*27b03b36SApple OSS Distributions /* wait on the reply from the parent (that we will never receive) */ 1625*27b03b36SApple OSS Distributions mr = mach_msg(&(rcv_msg.header), 1626*27b03b36SApple OSS Distributions (MACH_RCV_MSG | MACH_RCV_SYNC_WAIT), 1627*27b03b36SApple OSS Distributions 0, 1628*27b03b36SApple OSS Distributions rcv_msg.header.msgh_size, 1629*27b03b36SApple OSS Distributions special_reply_port, 1630*27b03b36SApple OSS Distributions MACH_MSG_TIMEOUT_NONE, 1631*27b03b36SApple OSS Distributions service_port); 1632*27b03b36SApple OSS Distributions 1633*27b03b36SApple OSS Distributions /* not expected to execute as parent will SIGKILL client... */ 1634*27b03b36SApple OSS Distributions T_LOG("client process exiting after sending message to parent (server)"); 1635*27b03b36SApple OSS Distributions} 1636*27b03b36SApple OSS Distributions 1637*27b03b36SApple OSS Distributionsenum srp_test_type { 1638*27b03b36SApple OSS Distributions SRP_TEST_THREAD, /* expect waiter on current thread */ 1639*27b03b36SApple OSS Distributions SRP_TEST_PID, /* expect waiter on current PID */ 1640*27b03b36SApple OSS Distributions SRP_TEST_EITHER, /* waiter could be on either */ 1641*27b03b36SApple OSS Distributions}; 1642*27b03b36SApple OSS Distributions 1643*27b03b36SApple OSS Distributionsstatic void 1644*27b03b36SApple OSS Distributionscheck_srp_test(const char *name, enum srp_test_type ty) 1645*27b03b36SApple OSS Distributions{ 1646*27b03b36SApple OSS Distributions struct scenario scenario = { 1647*27b03b36SApple OSS Distributions .name = name, 1648*27b03b36SApple OSS Distributions .quiet = false, 1649*27b03b36SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 1650*27b03b36SApple OSS Distributions }; 1651*27b03b36SApple OSS Distributions uint64_t thread_id = 0; 1652*27b03b36SApple OSS Distributions pthread_threadid_np(NULL, &thread_id); 1653*27b03b36SApple OSS Distributions if (ty == SRP_TEST_THREAD) { 1654*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1655*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_SRP, ssbuf, sslen, 1656*27b03b36SApple OSS Distributions @{srp_expected_threadid_key: @(thread_id)}); 1657*27b03b36SApple OSS Distributions }); 1658*27b03b36SApple OSS Distributions } else if (ty == SRP_TEST_PID) { 1659*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1660*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_SRP, ssbuf, sslen, 1661*27b03b36SApple OSS Distributions @{srp_expected_pid_key: @(getpid())}); 1662*27b03b36SApple OSS Distributions }); 1663*27b03b36SApple OSS Distributions } else { 1664*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1665*27b03b36SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_SRP, ssbuf, sslen, 1666*27b03b36SApple OSS Distributions @{srp_expected_pid_key: @(getpid()), srp_expected_threadid_key: @(thread_id)}); 1667*27b03b36SApple OSS Distributions }); 1668*27b03b36SApple OSS Distributions } 1669*27b03b36SApple OSS Distributions 1670*27b03b36SApple OSS Distributions} 1671*27b03b36SApple OSS Distributions 1672*27b03b36SApple OSS Distributions 1673*27b03b36SApple OSS Distributions/* 1674*27b03b36SApple OSS Distributions * Tests the stackshot wait info plumbing for synchronous IPC that doesn't use kevent on the server. 1675*27b03b36SApple OSS Distributions * 1676*27b03b36SApple OSS Distributions * (part 1): tests the scenario where a client sends a request that includes a special reply port 1677*27b03b36SApple OSS Distributions * to a server that doesn't receive the message and doesn't copy the send-once right 1678*27b03b36SApple OSS Distributions * into its address space as a result. for this case the special reply port is enqueued 1679*27b03b36SApple OSS Distributions * in a port and we check which task has that receive right and use that info. (rdar://60440338) 1680*27b03b36SApple OSS Distributions * (part 2): tests the scenario where a client sends a request that includes a special reply port 1681*27b03b36SApple OSS Distributions * to a server that receives the message and copies in the send-once right, but doesn't 1682*27b03b36SApple OSS Distributions * reply to the client. for this case the special reply port is copied out and the kernel 1683*27b03b36SApple OSS Distributions * stashes the info about which task copied out the send once right. (rdar://60440592) 1684*27b03b36SApple OSS Distributions * (part 3): tests the same as part 2, but uses kevents, which allow for 1685*27b03b36SApple OSS Distributions * priority inheritance 1686*27b03b36SApple OSS Distributions */ 1687*27b03b36SApple OSS DistributionsT_DECL(special_reply_port, "test that tasks using special reply ports have correct waitinfo") 1688*27b03b36SApple OSS Distributions{ 1689*27b03b36SApple OSS Distributions dispatch_semaphore_t can_continue = dispatch_semaphore_create(0); 1690*27b03b36SApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("signalqueue", NULL); 1691*27b03b36SApple OSS Distributions dispatch_queue_t machdq = dispatch_queue_create("machqueue", NULL); 1692*27b03b36SApple OSS Distributions dispatch_source_t sig_src; 1693*27b03b36SApple OSS Distributions char path[PATH_MAX]; 1694*27b03b36SApple OSS Distributions uint32_t path_size = sizeof(path); 1695*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 1696*27b03b36SApple OSS Distributions char *client_args[] = { path, "-n", "srp_client", NULL }; 1697*27b03b36SApple OSS Distributions pid_t client_pid; 1698*27b03b36SApple OSS Distributions int sp_ret; 1699*27b03b36SApple OSS Distributions kern_return_t kr; 1700*27b03b36SApple OSS Distributions mach_port_t port; 1701*27b03b36SApple OSS Distributions 1702*27b03b36SApple OSS Distributions /* setup the signal handler in the parent (server) */ 1703*27b03b36SApple OSS Distributions T_LOG("setup sig handlers"); 1704*27b03b36SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 1705*27b03b36SApple OSS Distributions sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 1706*27b03b36SApple OSS Distributions 1707*27b03b36SApple OSS Distributions dispatch_source_set_event_handler(sig_src, ^{ 1708*27b03b36SApple OSS Distributions dispatch_semaphore_signal(can_continue); 1709*27b03b36SApple OSS Distributions }); 1710*27b03b36SApple OSS Distributions dispatch_activate(sig_src); 1711*27b03b36SApple OSS Distributions 1712*27b03b36SApple OSS Distributions /* register with the mach service name so the client can lookup and send a message to the parent (server) */ 1713*27b03b36SApple OSS Distributions T_LOG("Server about to check in"); 1714*27b03b36SApple OSS Distributions kr = bootstrap_check_in(bootstrap_port, SRP_SERVICE_NAME, &port); 1715*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "server bootstrap_check_in"); 1716*27b03b36SApple OSS Distributions 1717*27b03b36SApple OSS Distributions T_LOG("Launching client"); 1718*27b03b36SApple OSS Distributions sp_ret = posix_spawn(&client_pid, client_args[0], NULL, NULL, client_args, NULL); 1719*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", client_args[0], client_pid); 1720*27b03b36SApple OSS Distributions T_LOG("Spawned client as PID %d", client_pid); 1721*27b03b36SApple OSS Distributions 1722*27b03b36SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 1723*27b03b36SApple OSS Distributions T_LOG("Ready to take stackshot, but waiting 1s for the coast to clear"); 1724*27b03b36SApple OSS Distributions 1725*27b03b36SApple OSS Distributions /* 1726*27b03b36SApple OSS Distributions * can_continue indicates the client has signaled us, but we want to make 1727*27b03b36SApple OSS Distributions * sure they've actually blocked sending their mach message. It's cheesy, but 1728*27b03b36SApple OSS Distributions * sleep() works for this. 1729*27b03b36SApple OSS Distributions */ 1730*27b03b36SApple OSS Distributions sleep(1); 1731*27b03b36SApple OSS Distributions 1732*27b03b36SApple OSS Distributions /* 1733*27b03b36SApple OSS Distributions * take the stackshot without calling receive to verify that the stackshot wait 1734*27b03b36SApple OSS Distributions * info shows our (the server) thread for the scenario where the server has yet to 1735*27b03b36SApple OSS Distributions * receive the message. 1736*27b03b36SApple OSS Distributions */ 1737*27b03b36SApple OSS Distributions T_LOG("Taking stackshot for part 1 coverage"); 1738*27b03b36SApple OSS Distributions check_srp_test("srp", SRP_TEST_THREAD); 1739*27b03b36SApple OSS Distributions 1740*27b03b36SApple OSS Distributions /* 1741*27b03b36SApple OSS Distributions * receive the message from the client (which should copy the send once right into 1742*27b03b36SApple OSS Distributions * our address space). 1743*27b03b36SApple OSS Distributions */ 1744*27b03b36SApple OSS Distributions struct { 1745*27b03b36SApple OSS Distributions mach_msg_header_t header; 1746*27b03b36SApple OSS Distributions mach_msg_body_t body; 1747*27b03b36SApple OSS Distributions mach_msg_port_descriptor_t port_descriptor; 1748*27b03b36SApple OSS Distributions } rcv_msg = { 1749*27b03b36SApple OSS Distributions .header = 1750*27b03b36SApple OSS Distributions { 1751*27b03b36SApple OSS Distributions .msgh_remote_port = MACH_PORT_NULL, 1752*27b03b36SApple OSS Distributions .msgh_local_port = port, 1753*27b03b36SApple OSS Distributions .msgh_size = sizeof(rcv_msg), 1754*27b03b36SApple OSS Distributions }, 1755*27b03b36SApple OSS Distributions }; 1756*27b03b36SApple OSS Distributions 1757*27b03b36SApple OSS Distributions T_LOG("server: starting sync receive\n"); 1758*27b03b36SApple OSS Distributions 1759*27b03b36SApple OSS Distributions mach_msg_return_t mr; 1760*27b03b36SApple OSS Distributions mr = mach_msg(&(rcv_msg.header), 1761*27b03b36SApple OSS Distributions (MACH_RCV_MSG | MACH_RCV_TIMEOUT), 1762*27b03b36SApple OSS Distributions 0, 1763*27b03b36SApple OSS Distributions 4096, 1764*27b03b36SApple OSS Distributions port, 1765*27b03b36SApple OSS Distributions 10000, 1766*27b03b36SApple OSS Distributions MACH_PORT_NULL); 1767*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(mr, "mach_msg() recieve of message from client"); 1768*27b03b36SApple OSS Distributions 1769*27b03b36SApple OSS Distributions /* 1770*27b03b36SApple OSS Distributions * take the stackshot to verify that the stackshot wait info shows our (the server) PID 1771*27b03b36SApple OSS Distributions * for the scenario where the server has received the message and copied in the send-once right. 1772*27b03b36SApple OSS Distributions */ 1773*27b03b36SApple OSS Distributions T_LOG("Taking stackshot for part 2 coverage"); 1774*27b03b36SApple OSS Distributions check_srp_test("srp", SRP_TEST_PID); 1775*27b03b36SApple OSS Distributions 1776*27b03b36SApple OSS Distributions /* cleanup - kill the client */ 1777*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(client_pid, SIGKILL), "killing client"); 1778*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(client_pid, NULL, 0), "waiting for the client to exit"); 1779*27b03b36SApple OSS Distributions 1780*27b03b36SApple OSS Distributions // do it again, but using kevents 1781*27b03b36SApple OSS Distributions T_LOG("Launching client"); 1782*27b03b36SApple OSS Distributions sp_ret = posix_spawn(&client_pid, client_args[0], NULL, NULL, client_args, NULL); 1783*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", client_args[0], client_pid); 1784*27b03b36SApple OSS Distributions T_LOG("Spawned client as PID %d", client_pid); 1785*27b03b36SApple OSS Distributions 1786*27b03b36SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 1787*27b03b36SApple OSS Distributions T_LOG("Ready to take stackshot, but waiting 1s for the coast to clear"); 1788*27b03b36SApple OSS Distributions 1789*27b03b36SApple OSS Distributions /* 1790*27b03b36SApple OSS Distributions * can_continue indicates the client has signaled us, but we want to make 1791*27b03b36SApple OSS Distributions * sure they've actually blocked sending their mach message. It's cheesy, but 1792*27b03b36SApple OSS Distributions * sleep() works for this. 1793*27b03b36SApple OSS Distributions */ 1794*27b03b36SApple OSS Distributions sleep(1); 1795*27b03b36SApple OSS Distributions 1796*27b03b36SApple OSS Distributions dispatch_mach_t dispatch_mach = dispatch_mach_create(SRP_SERVICE_NAME, machdq, 1797*27b03b36SApple OSS Distributions ^(dispatch_mach_reason_t reason, 1798*27b03b36SApple OSS Distributions dispatch_mach_msg_t message, 1799*27b03b36SApple OSS Distributions mach_error_t error __unused) { 1800*27b03b36SApple OSS Distributions switch (reason) { 1801*27b03b36SApple OSS Distributions case DISPATCH_MACH_MESSAGE_RECEIVED: { 1802*27b03b36SApple OSS Distributions size_t size = 0; 1803*27b03b36SApple OSS Distributions mach_msg_header_t *msg __unused = dispatch_mach_msg_get_msg(message, &size); 1804*27b03b36SApple OSS Distributions T_LOG("server: recieved %ld byte message", size); 1805*27b03b36SApple OSS Distributions check_srp_test("turnstile_port_thread", SRP_TEST_THREAD); 1806*27b03b36SApple OSS Distributions T_LOG("server: letting client go"); 1807*27b03b36SApple OSS Distributions // drop the message on the ground, we'll kill the client later 1808*27b03b36SApple OSS Distributions dispatch_semaphore_signal(can_continue); 1809*27b03b36SApple OSS Distributions break; 1810*27b03b36SApple OSS Distributions } 1811*27b03b36SApple OSS Distributions default: 1812*27b03b36SApple OSS Distributions break; 1813*27b03b36SApple OSS Distributions } 1814*27b03b36SApple OSS Distributions }); 1815*27b03b36SApple OSS Distributions 1816*27b03b36SApple OSS Distributions dispatch_mach_connect(dispatch_mach, port, MACH_PORT_NULL, NULL); 1817*27b03b36SApple OSS Distributions 1818*27b03b36SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 1819*27b03b36SApple OSS Distributions 1820*27b03b36SApple OSS Distributions /* cleanup - kill the client */ 1821*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(client_pid, SIGKILL), "killing client"); 1822*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(client_pid, NULL, 0), "waiting for the client to exit"); 1823*27b03b36SApple OSS Distributions} 1824*27b03b36SApple OSS Distributions 1825*27b03b36SApple OSS Distributions#pragma mark performance tests 1826*27b03b36SApple OSS Distributions 1827*27b03b36SApple OSS Distributions#define SHOULD_REUSE_SIZE_HINT 0x01 1828*27b03b36SApple OSS Distributions#define SHOULD_USE_DELTA 0x02 1829*27b03b36SApple OSS Distributions#define SHOULD_TARGET_SELF 0x04 1830*27b03b36SApple OSS Distributions 1831*27b03b36SApple OSS Distributionsstatic void 1832*27b03b36SApple OSS Distributionsstackshot_perf(unsigned int options) 1833*27b03b36SApple OSS Distributions{ 1834*27b03b36SApple OSS Distributions struct scenario scenario = { 1835*27b03b36SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 1836*27b03b36SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 1837*27b03b36SApple OSS Distributions }; 1838*27b03b36SApple OSS Distributions 1839*27b03b36SApple OSS Distributions dt_stat_t size = dt_stat_create("bytes", "size"); 1840*27b03b36SApple OSS Distributions dt_stat_time_t duration = dt_stat_time_create("duration"); 1841*27b03b36SApple OSS Distributions scenario.timer = duration; 1842*27b03b36SApple OSS Distributions 1843*27b03b36SApple OSS Distributions if (options & SHOULD_TARGET_SELF) { 1844*27b03b36SApple OSS Distributions scenario.target_pid = getpid(); 1845*27b03b36SApple OSS Distributions } 1846*27b03b36SApple OSS Distributions 1847*27b03b36SApple OSS Distributions while (!dt_stat_stable(duration) || !dt_stat_stable(size)) { 1848*27b03b36SApple OSS Distributions __block uint64_t last_time = 0; 1849*27b03b36SApple OSS Distributions __block uint32_t size_hint = 0; 1850*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1851*27b03b36SApple OSS Distributions dt_stat_add(size, (double)sslen); 1852*27b03b36SApple OSS Distributions last_time = stackshot_timestamp(ssbuf, sslen); 1853*27b03b36SApple OSS Distributions size_hint = (uint32_t)sslen; 1854*27b03b36SApple OSS Distributions }); 1855*27b03b36SApple OSS Distributions if (options & SHOULD_USE_DELTA) { 1856*27b03b36SApple OSS Distributions scenario.since_timestamp = last_time; 1857*27b03b36SApple OSS Distributions scenario.flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT; 1858*27b03b36SApple OSS Distributions } 1859*27b03b36SApple OSS Distributions if (options & SHOULD_REUSE_SIZE_HINT) { 1860*27b03b36SApple OSS Distributions scenario.size_hint = size_hint; 1861*27b03b36SApple OSS Distributions } 1862*27b03b36SApple OSS Distributions } 1863*27b03b36SApple OSS Distributions 1864*27b03b36SApple OSS Distributions dt_stat_finalize(duration); 1865*27b03b36SApple OSS Distributions dt_stat_finalize(size); 1866*27b03b36SApple OSS Distributions} 1867*27b03b36SApple OSS Distributions 1868*27b03b36SApple OSS Distributionsstatic void 1869*27b03b36SApple OSS Distributionsstackshot_flag_perf_noclobber(uint64_t flag, char *flagname) 1870*27b03b36SApple OSS Distributions{ 1871*27b03b36SApple OSS Distributions struct scenario scenario = { 1872*27b03b36SApple OSS Distributions .quiet = true, 1873*27b03b36SApple OSS Distributions .flags = (flag | STACKSHOT_KCDATA_FORMAT), 1874*27b03b36SApple OSS Distributions }; 1875*27b03b36SApple OSS Distributions 1876*27b03b36SApple OSS Distributions dt_stat_t duration = dt_stat_create("nanoseconds per thread", "%s_duration", flagname); 1877*27b03b36SApple OSS Distributions dt_stat_t size = dt_stat_create("bytes per thread", "%s_size", flagname); 1878*27b03b36SApple OSS Distributions T_LOG("Testing \"%s\" = 0x%" PRIx64, flagname, flag); 1879*27b03b36SApple OSS Distributions 1880*27b03b36SApple OSS Distributions while (!dt_stat_stable(duration) || !dt_stat_stable(size)) { 1881*27b03b36SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1882*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1883*27b03b36SApple OSS Distributions unsigned long no_threads = 0; 1884*27b03b36SApple OSS Distributions mach_timebase_info_data_t timebase = {0, 0}; 1885*27b03b36SApple OSS Distributions uint64_t stackshot_duration = 0; 1886*27b03b36SApple OSS Distributions int found = 0; 1887*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "stackshot buffer"); 1888*27b03b36SApple OSS Distributions 1889*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1890*27b03b36SApple OSS Distributions switch(kcdata_iter_type(iter)) { 1891*27b03b36SApple OSS Distributions case STACKSHOT_KCTYPE_THREAD_SNAPSHOT: { 1892*27b03b36SApple OSS Distributions found |= 1; 1893*27b03b36SApple OSS Distributions no_threads ++; 1894*27b03b36SApple OSS Distributions break; 1895*27b03b36SApple OSS Distributions } 1896*27b03b36SApple OSS Distributions case STACKSHOT_KCTYPE_STACKSHOT_DURATION: { 1897*27b03b36SApple OSS Distributions struct stackshot_duration *ssd = kcdata_iter_payload(iter); 1898*27b03b36SApple OSS Distributions stackshot_duration = ssd->stackshot_duration; 1899*27b03b36SApple OSS Distributions found |= 2; 1900*27b03b36SApple OSS Distributions break; 1901*27b03b36SApple OSS Distributions } 1902*27b03b36SApple OSS Distributions case KCDATA_TYPE_TIMEBASE: { 1903*27b03b36SApple OSS Distributions found |= 4; 1904*27b03b36SApple OSS Distributions mach_timebase_info_data_t *tb = kcdata_iter_payload(iter); 1905*27b03b36SApple OSS Distributions memcpy(&timebase, tb, sizeof(timebase)); 1906*27b03b36SApple OSS Distributions break; 1907*27b03b36SApple OSS Distributions } 1908*27b03b36SApple OSS Distributions } 1909*27b03b36SApple OSS Distributions } 1910*27b03b36SApple OSS Distributions 1911*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(found, 0x7, "found everything needed"); 1912*27b03b36SApple OSS Distributions 1913*27b03b36SApple OSS Distributions uint64_t ns = (stackshot_duration * timebase.numer) / timebase.denom; 1914*27b03b36SApple OSS Distributions uint64_t per_thread_ns = ns / no_threads; 1915*27b03b36SApple OSS Distributions uint64_t per_thread_size = sslen / no_threads; 1916*27b03b36SApple OSS Distributions 1917*27b03b36SApple OSS Distributions dt_stat_add(duration, per_thread_ns); 1918*27b03b36SApple OSS Distributions dt_stat_add(size, per_thread_size); 1919*27b03b36SApple OSS Distributions }); 1920*27b03b36SApple OSS Distributions } 1921*27b03b36SApple OSS Distributions 1922*27b03b36SApple OSS Distributions dt_stat_finalize(duration); 1923*27b03b36SApple OSS Distributions dt_stat_finalize(size); 1924*27b03b36SApple OSS Distributions} 1925*27b03b36SApple OSS Distributions 1926*27b03b36SApple OSS Distributionsstatic void 1927*27b03b36SApple OSS Distributionsstackshot_flag_perf(uint64_t flag, char *flagname) 1928*27b03b36SApple OSS Distributions{ 1929*27b03b36SApple OSS Distributions /* 1930*27b03b36SApple OSS Distributions * STACKSHOT_NO_IO_STATS disables data collection, so set it for 1931*27b03b36SApple OSS Distributions * more accurate perfdata collection. 1932*27b03b36SApple OSS Distributions */ 1933*27b03b36SApple OSS Distributions flag |= STACKSHOT_NO_IO_STATS; 1934*27b03b36SApple OSS Distributions 1935*27b03b36SApple OSS Distributions stackshot_flag_perf_noclobber(flag, flagname); 1936*27b03b36SApple OSS Distributions} 1937*27b03b36SApple OSS Distributions 1938*27b03b36SApple OSS Distributions 1939*27b03b36SApple OSS DistributionsT_DECL(flag_perf, "test stackshot performance with different flags set", T_META_TAG_PERF) 1940*27b03b36SApple OSS Distributions{ 1941*27b03b36SApple OSS Distributions stackshot_flag_perf_noclobber(STACKSHOT_NO_IO_STATS, "baseline"); 1942*27b03b36SApple OSS Distributions stackshot_flag_perf_noclobber(0, "io_stats"); 1943*27b03b36SApple OSS Distributions 1944*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_THREAD_WAITINFO, "thread_waitinfo"); 1945*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_GET_DQ, "get_dq"); 1946*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_LOADINFO, "save_loadinfo"); 1947*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_GET_GLOBAL_MEM_STATS, "get_global_mem_stats"); 1948*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_KEXT_LOADINFO, "save_kext_loadinfo"); 1949*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_IMP_DONATION_PIDS, "save_imp_donation_pids"); 1950*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_ENABLE_BT_FAULTING, "enable_bt_faulting"); 1951*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT, "collect_sharedcache_layout"); 1952*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_ENABLE_UUID_FAULTING, "enable_uuid_faulting"); 1953*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_THREAD_GROUP, "thread_group"); 1954*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_JETSAM_COALITIONS, "save_jetsam_coalitions"); 1955*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_INSTRS_CYCLES, "instrs_cycles"); 1956*27b03b36SApple OSS Distributions stackshot_flag_perf(STACKSHOT_ASID, "asid"); 1957*27b03b36SApple OSS Distributions} 1958*27b03b36SApple OSS Distributions 1959*27b03b36SApple OSS DistributionsT_DECL(perf_no_size_hint, "test stackshot performance with no size hint", 1960*27b03b36SApple OSS Distributions T_META_TAG_PERF) 1961*27b03b36SApple OSS Distributions{ 1962*27b03b36SApple OSS Distributions stackshot_perf(0); 1963*27b03b36SApple OSS Distributions} 1964*27b03b36SApple OSS Distributions 1965*27b03b36SApple OSS DistributionsT_DECL(perf_size_hint, "test stackshot performance with size hint", 1966*27b03b36SApple OSS Distributions T_META_TAG_PERF) 1967*27b03b36SApple OSS Distributions{ 1968*27b03b36SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT); 1969*27b03b36SApple OSS Distributions} 1970*27b03b36SApple OSS Distributions 1971*27b03b36SApple OSS DistributionsT_DECL(perf_process, "test stackshot performance targeted at process", 1972*27b03b36SApple OSS Distributions T_META_TAG_PERF) 1973*27b03b36SApple OSS Distributions{ 1974*27b03b36SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_TARGET_SELF); 1975*27b03b36SApple OSS Distributions} 1976*27b03b36SApple OSS Distributions 1977*27b03b36SApple OSS DistributionsT_DECL(perf_delta, "test delta stackshot performance", 1978*27b03b36SApple OSS Distributions T_META_TAG_PERF) 1979*27b03b36SApple OSS Distributions{ 1980*27b03b36SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA); 1981*27b03b36SApple OSS Distributions} 1982*27b03b36SApple OSS Distributions 1983*27b03b36SApple OSS DistributionsT_DECL(perf_delta_process, "test delta stackshot performance targeted at a process", 1984*27b03b36SApple OSS Distributions T_META_TAG_PERF) 1985*27b03b36SApple OSS Distributions{ 1986*27b03b36SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA | SHOULD_TARGET_SELF); 1987*27b03b36SApple OSS Distributions} 1988*27b03b36SApple OSS Distributions 1989*27b03b36SApple OSS Distributionsstatic uint64_t 1990*27b03b36SApple OSS Distributionsstackshot_timestamp(void *ssbuf, size_t sslen) 1991*27b03b36SApple OSS Distributions{ 1992*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1993*27b03b36SApple OSS Distributions 1994*27b03b36SApple OSS Distributions uint32_t type = kcdata_iter_type(iter); 1995*27b03b36SApple OSS Distributions if (type != KCDATA_BUFFER_BEGIN_STACKSHOT && type != KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT) { 1996*27b03b36SApple OSS Distributions T_ASSERT_FAIL("invalid kcdata type %u", kcdata_iter_type(iter)); 1997*27b03b36SApple OSS Distributions } 1998*27b03b36SApple OSS Distributions 1999*27b03b36SApple OSS Distributions iter = kcdata_iter_find_type(iter, KCDATA_TYPE_MACH_ABSOLUTE_TIME); 2000*27b03b36SApple OSS Distributions T_QUIET; 2001*27b03b36SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_valid(iter), "timestamp found in stackshot"); 2002*27b03b36SApple OSS Distributions 2003*27b03b36SApple OSS Distributions return *(uint64_t *)kcdata_iter_payload(iter); 2004*27b03b36SApple OSS Distributions} 2005*27b03b36SApple OSS Distributions 2006*27b03b36SApple OSS Distributions#define TEST_THREAD_NAME "stackshot_test_thread" 2007*27b03b36SApple OSS Distributions 2008*27b03b36SApple OSS Distributionsstatic void 2009*27b03b36SApple OSS Distributionsparse_thread_group_stackshot(void **ssbuf, size_t sslen) 2010*27b03b36SApple OSS Distributions{ 2011*27b03b36SApple OSS Distributions bool seen_thread_group_snapshot = false; 2012*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2013*27b03b36SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, 2014*27b03b36SApple OSS Distributions "buffer provided is a stackshot"); 2015*27b03b36SApple OSS Distributions 2016*27b03b36SApple OSS Distributions NSMutableSet *thread_groups = [[NSMutableSet alloc] init]; 2017*27b03b36SApple OSS Distributions 2018*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 2019*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2020*27b03b36SApple OSS Distributions switch (kcdata_iter_type(iter)) { 2021*27b03b36SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 2022*27b03b36SApple OSS Distributions T_QUIET; 2023*27b03b36SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_array_valid(iter), 2024*27b03b36SApple OSS Distributions "checked that array is valid"); 2025*27b03b36SApple OSS Distributions 2026*27b03b36SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT) { 2027*27b03b36SApple OSS Distributions continue; 2028*27b03b36SApple OSS Distributions } 2029*27b03b36SApple OSS Distributions 2030*27b03b36SApple OSS Distributions seen_thread_group_snapshot = true; 2031*27b03b36SApple OSS Distributions 2032*27b03b36SApple OSS Distributions if (kcdata_iter_array_elem_size(iter) >= sizeof(struct thread_group_snapshot_v3)) { 2033*27b03b36SApple OSS Distributions struct thread_group_snapshot_v3 *tgs_array = kcdata_iter_payload(iter); 2034*27b03b36SApple OSS Distributions for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) { 2035*27b03b36SApple OSS Distributions struct thread_group_snapshot_v3 *tgs = tgs_array + j; 2036*27b03b36SApple OSS Distributions [thread_groups addObject:@(tgs->tgs_id)]; 2037*27b03b36SApple OSS Distributions } 2038*27b03b36SApple OSS Distributions } 2039*27b03b36SApple OSS Distributions else { 2040*27b03b36SApple OSS Distributions struct thread_group_snapshot *tgs_array = kcdata_iter_payload(iter); 2041*27b03b36SApple OSS Distributions for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) { 2042*27b03b36SApple OSS Distributions struct thread_group_snapshot *tgs = tgs_array + j; 2043*27b03b36SApple OSS Distributions [thread_groups addObject:@(tgs->tgs_id)]; 2044*27b03b36SApple OSS Distributions } 2045*27b03b36SApple OSS Distributions } 2046*27b03b36SApple OSS Distributions break; 2047*27b03b36SApple OSS Distributions } 2048*27b03b36SApple OSS Distributions } 2049*27b03b36SApple OSS Distributions } 2050*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2051*27b03b36SApple OSS Distributions NSError *error = nil; 2052*27b03b36SApple OSS Distributions 2053*27b03b36SApple OSS Distributions switch (kcdata_iter_type(iter)) { 2054*27b03b36SApple OSS Distributions 2055*27b03b36SApple OSS Distributions case KCDATA_TYPE_CONTAINER_BEGIN: { 2056*27b03b36SApple OSS Distributions T_QUIET; 2057*27b03b36SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_container_valid(iter), 2058*27b03b36SApple OSS Distributions "checked that container is valid"); 2059*27b03b36SApple OSS Distributions 2060*27b03b36SApple OSS Distributions if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_THREAD) { 2061*27b03b36SApple OSS Distributions break; 2062*27b03b36SApple OSS Distributions } 2063*27b03b36SApple OSS Distributions 2064*27b03b36SApple OSS Distributions NSDictionary *container = parseKCDataContainer(&iter, &error); 2065*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(container, "parsed thread container from stackshot"); 2066*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container"); 2067*27b03b36SApple OSS Distributions 2068*27b03b36SApple OSS Distributions int tg = [container[@"thread_snapshots"][@"thread_group"] intValue]; 2069*27b03b36SApple OSS Distributions 2070*27b03b36SApple OSS Distributions T_ASSERT_TRUE([thread_groups containsObject:@(tg)], "check that the thread group the thread is in exists"); 2071*27b03b36SApple OSS Distributions 2072*27b03b36SApple OSS Distributions break; 2073*27b03b36SApple OSS Distributions }; 2074*27b03b36SApple OSS Distributions 2075*27b03b36SApple OSS Distributions } 2076*27b03b36SApple OSS Distributions } 2077*27b03b36SApple OSS Distributions T_ASSERT_TRUE(seen_thread_group_snapshot, "check that we have seen a thread group snapshot"); 2078*27b03b36SApple OSS Distributions} 2079*27b03b36SApple OSS Distributions 2080*27b03b36SApple OSS Distributionsstatic void 2081*27b03b36SApple OSS Distributionsverify_stackshot_sharedcache_layout(struct dyld_uuid_info_64 *uuids, uint32_t uuid_count) 2082*27b03b36SApple OSS Distributions{ 2083*27b03b36SApple OSS Distributions uuid_t cur_shared_cache_uuid; 2084*27b03b36SApple OSS Distributions __block uint32_t lib_index = 0, libs_found = 0; 2085*27b03b36SApple OSS Distributions 2086*27b03b36SApple OSS Distributions _dyld_get_shared_cache_uuid(cur_shared_cache_uuid); 2087*27b03b36SApple OSS Distributions int result = dyld_shared_cache_iterate_text(cur_shared_cache_uuid, ^(const dyld_shared_cache_dylib_text_info* info) { 2088*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LT(lib_index, uuid_count, "dyld_shared_cache_iterate_text exceeded number of libraries returned by kernel"); 2089*27b03b36SApple OSS Distributions 2090*27b03b36SApple OSS Distributions libs_found++; 2091*27b03b36SApple OSS Distributions struct dyld_uuid_info_64 *cur_stackshot_uuid_entry = &uuids[lib_index]; 2092*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(memcmp(info->dylibUuid, cur_stackshot_uuid_entry->imageUUID, sizeof(info->dylibUuid)), 0, 2093*27b03b36SApple OSS Distributions "dyld returned UUID doesn't match kernel returned UUID"); 2094*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(info->loadAddressUnslid, cur_stackshot_uuid_entry->imageLoadAddress, 2095*27b03b36SApple OSS Distributions "dyld returned load address doesn't match kernel returned load address"); 2096*27b03b36SApple OSS Distributions lib_index++; 2097*27b03b36SApple OSS Distributions }); 2098*27b03b36SApple OSS Distributions 2099*27b03b36SApple OSS Distributions T_ASSERT_EQ(result, 0, "iterate shared cache layout"); 2100*27b03b36SApple OSS Distributions T_ASSERT_EQ(libs_found, uuid_count, "dyld iterator returned same number of libraries as kernel"); 2101*27b03b36SApple OSS Distributions 2102*27b03b36SApple OSS Distributions T_LOG("verified %d libraries from dyld shared cache", libs_found); 2103*27b03b36SApple OSS Distributions} 2104*27b03b36SApple OSS Distributions 2105*27b03b36SApple OSS Distributionsstatic void 2106*27b03b36SApple OSS Distributionscheck_shared_cache_uuid(uuid_t imageUUID) 2107*27b03b36SApple OSS Distributions{ 2108*27b03b36SApple OSS Distributions static uuid_t shared_cache_uuid; 2109*27b03b36SApple OSS Distributions static dispatch_once_t read_shared_cache_uuid; 2110*27b03b36SApple OSS Distributions 2111*27b03b36SApple OSS Distributions dispatch_once(&read_shared_cache_uuid, ^{ 2112*27b03b36SApple OSS Distributions T_QUIET; 2113*27b03b36SApple OSS Distributions T_ASSERT_TRUE(_dyld_get_shared_cache_uuid(shared_cache_uuid), "retrieve current shared cache UUID"); 2114*27b03b36SApple OSS Distributions }); 2115*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(uuid_compare(shared_cache_uuid, imageUUID), 0, 2116*27b03b36SApple OSS Distributions "dyld returned UUID doesn't match kernel returned UUID for system shared cache"); 2117*27b03b36SApple OSS Distributions} 2118*27b03b36SApple OSS Distributions 2119*27b03b36SApple OSS Distributions/* 2120*27b03b36SApple OSS Distributions * extra dictionary contains data relevant for the given flags: 2121*27b03b36SApple OSS Distributions * PARSE_STACKSHOT_ZOMBIE: zombie_child_pid_key -> @(pid) 2122*27b03b36SApple OSS Distributions * PARSE_STACKSHOT_POSTEXEC: postexec_child_unique_pid_key -> @(unique_pid) 2123*27b03b36SApple OSS Distributions */ 2124*27b03b36SApple OSS Distributionsstatic void 2125*27b03b36SApple OSS Distributionsparse_stackshot(uint64_t stackshot_parsing_flags, void *ssbuf, size_t sslen, NSDictionary *extra) 2126*27b03b36SApple OSS Distributions{ 2127*27b03b36SApple OSS Distributions bool delta = (stackshot_parsing_flags & PARSE_STACKSHOT_DELTA); 2128*27b03b36SApple OSS Distributions bool expect_sharedcache_child = (stackshot_parsing_flags & PARSE_STACKSHOT_SHAREDCACHE_FLAGS); 2129*27b03b36SApple OSS Distributions bool expect_zombie_child = (stackshot_parsing_flags & PARSE_STACKSHOT_ZOMBIE); 2130*27b03b36SApple OSS Distributions bool expect_postexec_child = (stackshot_parsing_flags & PARSE_STACKSHOT_POSTEXEC); 2131*27b03b36SApple OSS Distributions bool expect_cseg_waitinfo = (stackshot_parsing_flags & PARSE_STACKSHOT_WAITINFO_CSEG); 2132*27b03b36SApple OSS Distributions bool expect_translated_child = (stackshot_parsing_flags & PARSE_STACKSHOT_TRANSLATED); 2133*27b03b36SApple OSS Distributions bool expect_shared_cache_layout = false; 2134*27b03b36SApple OSS Distributions bool expect_shared_cache_uuid = !delta; 2135*27b03b36SApple OSS Distributions bool expect_dispatch_queue_label = (stackshot_parsing_flags & PARSE_STACKSHOT_DISPATCH_QUEUE_LABEL); 2136*27b03b36SApple OSS Distributions bool expect_turnstile_lock = (stackshot_parsing_flags & PARSE_STACKSHOT_TURNSTILEINFO); 2137*27b03b36SApple OSS Distributions bool expect_srp_waitinfo = (stackshot_parsing_flags & PARSE_STACKSHOT_WAITINFO_SRP); 2138*27b03b36SApple OSS Distributions bool expect_exec_inprogress = (stackshot_parsing_flags & PARSE_STACKSHOT_EXEC_INPROGRESS); 2139*27b03b36SApple OSS Distributions bool expect_transitioning_task = (stackshot_parsing_flags & PARSE_STACKSHOT_TRANSITIONING); 2140*27b03b36SApple OSS Distributions bool expect_asyncstack = (stackshot_parsing_flags & PARSE_STACKSHOT_ASYNCSTACK); 2141*27b03b36SApple OSS Distributions bool found_zombie_child = false, found_postexec_child = false, found_shared_cache_layout = false, found_shared_cache_uuid = false; 2142*27b03b36SApple OSS Distributions bool found_translated_child = false, found_transitioning_task = false; 2143*27b03b36SApple OSS Distributions bool found_dispatch_queue_label = false, found_turnstile_lock = false; 2144*27b03b36SApple OSS Distributions bool found_cseg_waitinfo = false, found_srp_waitinfo = false; 2145*27b03b36SApple OSS Distributions bool found_sharedcache_child = false, found_sharedcache_badflags = false, found_sharedcache_self = false; 2146*27b03b36SApple OSS Distributions bool found_asyncstack = false; 2147*27b03b36SApple OSS Distributions uint64_t srp_expected_threadid = 0; 2148*27b03b36SApple OSS Distributions pid_t zombie_child_pid = -1, srp_expected_pid = -1, sharedcache_child_pid = -1; 2149*27b03b36SApple OSS Distributions pid_t translated_child_pid = -1, transistioning_task_pid = -1; 2150*27b03b36SApple OSS Distributions bool sharedcache_child_sameaddr = false; 2151*27b03b36SApple OSS Distributions uint64_t postexec_child_unique_pid = 0, cseg_expected_threadid = 0; 2152*27b03b36SApple OSS Distributions uint64_t sharedcache_child_flags = 0, sharedcache_self_flags = 0; 2153*27b03b36SApple OSS Distributions uint64_t asyncstack_threadid = 0; 2154*27b03b36SApple OSS Distributions NSArray *asyncstack_stack = nil; 2155*27b03b36SApple OSS Distributions char *inflatedBufferBase = NULL; 2156*27b03b36SApple OSS Distributions pid_t exec_inprogress_pid = -1; 2157*27b03b36SApple OSS Distributions void (^exec_inprogress_cb)(uint64_t, uint64_t) = NULL; 2158*27b03b36SApple OSS Distributions int exec_inprogress_found = 0; 2159*27b03b36SApple OSS Distributions uint64_t exec_inprogress_containerid = 0; 2160*27b03b36SApple OSS Distributions 2161*27b03b36SApple OSS Distributions if (expect_shared_cache_uuid) { 2162*27b03b36SApple OSS Distributions uuid_t shared_cache_uuid; 2163*27b03b36SApple OSS Distributions if (!_dyld_get_shared_cache_uuid(shared_cache_uuid)) { 2164*27b03b36SApple OSS Distributions T_LOG("Skipping verifying shared cache UUID in stackshot data because not running with a shared cache"); 2165*27b03b36SApple OSS Distributions expect_shared_cache_uuid = false; 2166*27b03b36SApple OSS Distributions } 2167*27b03b36SApple OSS Distributions } 2168*27b03b36SApple OSS Distributions 2169*27b03b36SApple OSS Distributions if (stackshot_parsing_flags & PARSE_STACKSHOT_SHAREDCACHE_LAYOUT) { 2170*27b03b36SApple OSS Distributions size_t shared_cache_length = 0; 2171*27b03b36SApple OSS Distributions const void *cache_header = _dyld_get_shared_cache_range(&shared_cache_length); 2172*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cache_header, "current process running with shared cache"); 2173*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(shared_cache_length, sizeof(struct _dyld_cache_header), "valid shared cache length populated by _dyld_get_shared_cache_range"); 2174*27b03b36SApple OSS Distributions 2175*27b03b36SApple OSS Distributions if (_dyld_shared_cache_is_locally_built()) { 2176*27b03b36SApple OSS Distributions T_LOG("device running with locally built shared cache, expect shared cache layout"); 2177*27b03b36SApple OSS Distributions expect_shared_cache_layout = true; 2178*27b03b36SApple OSS Distributions } else { 2179*27b03b36SApple OSS Distributions T_LOG("device running with B&I built shared-cache, no shared cache layout expected"); 2180*27b03b36SApple OSS Distributions } 2181*27b03b36SApple OSS Distributions } 2182*27b03b36SApple OSS Distributions 2183*27b03b36SApple OSS Distributions if (expect_sharedcache_child) { 2184*27b03b36SApple OSS Distributions NSNumber* pid_num = extra[sharedcache_child_pid_key]; 2185*27b03b36SApple OSS Distributions NSNumber* sameaddr_num = extra[sharedcache_child_sameaddr_key]; 2186*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "sharedcache child pid provided"); 2187*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(sameaddr_num, "sharedcache child addrsame provided"); 2188*27b03b36SApple OSS Distributions sharedcache_child_pid = [pid_num intValue]; 2189*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(sharedcache_child_pid, 0, "sharedcache child pid greater than zero"); 2190*27b03b36SApple OSS Distributions sharedcache_child_sameaddr = [sameaddr_num intValue]; 2191*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GE([sameaddr_num intValue], 0, "sharedcache child sameaddr is boolean (0 or 1)"); 2192*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE([sameaddr_num intValue], 1, "sharedcache child sameaddr is boolean (0 or 1)"); 2193*27b03b36SApple OSS Distributions } 2194*27b03b36SApple OSS Distributions 2195*27b03b36SApple OSS Distributions if (expect_transitioning_task) { 2196*27b03b36SApple OSS Distributions NSNumber* pid_num = extra[transitioning_pid_key]; 2197*27b03b36SApple OSS Distributions T_ASSERT_NOTNULL(pid_num, "transitioning task pid provided"); 2198*27b03b36SApple OSS Distributions transistioning_task_pid = [pid_num intValue]; 2199*27b03b36SApple OSS Distributions } 2200*27b03b36SApple OSS Distributions 2201*27b03b36SApple OSS Distributions if (expect_zombie_child) { 2202*27b03b36SApple OSS Distributions NSNumber* pid_num = extra[zombie_child_pid_key]; 2203*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "zombie child pid provided"); 2204*27b03b36SApple OSS Distributions zombie_child_pid = [pid_num intValue]; 2205*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(zombie_child_pid, 0, "zombie child pid greater than zero"); 2206*27b03b36SApple OSS Distributions } 2207*27b03b36SApple OSS Distributions 2208*27b03b36SApple OSS Distributions if (expect_postexec_child) { 2209*27b03b36SApple OSS Distributions NSNumber* unique_pid_num = extra[postexec_child_unique_pid_key]; 2210*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(unique_pid_num, "postexec child unique pid provided"); 2211*27b03b36SApple OSS Distributions postexec_child_unique_pid = [unique_pid_num unsignedLongLongValue]; 2212*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(postexec_child_unique_pid, 0ull, "postexec child unique pid greater than zero"); 2213*27b03b36SApple OSS Distributions } 2214*27b03b36SApple OSS Distributions 2215*27b03b36SApple OSS Distributions if (expect_cseg_waitinfo) { 2216*27b03b36SApple OSS Distributions NSNumber* tid_num = extra[cseg_expected_threadid_key]; 2217*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(tid_num, "cseg's expected thread id provided"); 2218*27b03b36SApple OSS Distributions cseg_expected_threadid = tid_num.unsignedLongValue; 2219*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(cseg_expected_threadid, UINT64_C(0), "compressor segment thread is present"); 2220*27b03b36SApple OSS Distributions } 2221*27b03b36SApple OSS Distributions 2222*27b03b36SApple OSS Distributions if (expect_srp_waitinfo) { 2223*27b03b36SApple OSS Distributions NSNumber* threadid_num = extra[srp_expected_threadid_key]; 2224*27b03b36SApple OSS Distributions NSNumber* pid_num = extra[srp_expected_pid_key]; 2225*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(threadid_num != nil || pid_num != nil, "expected SRP threadid or pid"); 2226*27b03b36SApple OSS Distributions if (threadid_num != nil) { 2227*27b03b36SApple OSS Distributions srp_expected_threadid = [threadid_num unsignedLongLongValue]; 2228*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(srp_expected_threadid, 0ull, "srp_expected_threadid greater than zero"); 2229*27b03b36SApple OSS Distributions } 2230*27b03b36SApple OSS Distributions if (pid_num != nil) { 2231*27b03b36SApple OSS Distributions srp_expected_pid = [pid_num intValue]; 2232*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(srp_expected_pid, 0, "srp_expected_pid greater than zero"); 2233*27b03b36SApple OSS Distributions } 2234*27b03b36SApple OSS Distributions T_LOG("looking for SRP pid: %d threadid: %llu", srp_expected_pid, srp_expected_threadid); 2235*27b03b36SApple OSS Distributions } 2236*27b03b36SApple OSS Distributions 2237*27b03b36SApple OSS Distributions if (expect_translated_child) { 2238*27b03b36SApple OSS Distributions NSNumber* pid_num = extra[translated_child_pid_key]; 2239*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "translated child pid provided"); 2240*27b03b36SApple OSS Distributions translated_child_pid = [pid_num intValue]; 2241*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(translated_child_pid, 0, "translated child pid greater than zero"); 2242*27b03b36SApple OSS Distributions } 2243*27b03b36SApple OSS Distributions if (expect_exec_inprogress) { 2244*27b03b36SApple OSS Distributions NSNumber* pid_num = extra[exec_inprogress_pid_key]; 2245*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "exec inprogress pid provided"); 2246*27b03b36SApple OSS Distributions exec_inprogress_pid = [pid_num intValue]; 2247*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(exec_inprogress_pid, 0, "exec inprogress pid greater than zero"); 2248*27b03b36SApple OSS Distributions 2249*27b03b36SApple OSS Distributions exec_inprogress_cb = extra[exec_inprogress_found_key]; 2250*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(exec_inprogress_cb, "exec inprogress found callback provided"); 2251*27b03b36SApple OSS Distributions } 2252*27b03b36SApple OSS Distributions 2253*27b03b36SApple OSS Distributions if (expect_asyncstack) { 2254*27b03b36SApple OSS Distributions NSNumber* threadid_id = extra[asyncstack_expected_threadid_key]; 2255*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(threadid_id, "asyncstack threadid provided"); 2256*27b03b36SApple OSS Distributions asyncstack_threadid = [threadid_id unsignedLongLongValue]; 2257*27b03b36SApple OSS Distributions asyncstack_stack = extra[asyncstack_expected_stack_key]; 2258*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(asyncstack_stack, "asyncstack expected stack provided"); 2259*27b03b36SApple OSS Distributions } 2260*27b03b36SApple OSS Distributions 2261*27b03b36SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2262*27b03b36SApple OSS Distributions if (delta) { 2263*27b03b36SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT, 2264*27b03b36SApple OSS Distributions "buffer provided is a delta stackshot"); 2265*27b03b36SApple OSS Distributions 2266*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 2267*27b03b36SApple OSS Distributions } else { 2268*27b03b36SApple OSS Distributions if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_COMPRESSED) { 2269*27b03b36SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, 2270*27b03b36SApple OSS Distributions "buffer provided is a stackshot"); 2271*27b03b36SApple OSS Distributions 2272*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 2273*27b03b36SApple OSS Distributions } else { 2274*27b03b36SApple OSS Distributions /* we are dealing with a compressed buffer */ 2275*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 2276*27b03b36SApple OSS Distributions uint64_t compression_type = 0, totalout = 0, totalin = 0; 2277*27b03b36SApple OSS Distributions 2278*27b03b36SApple OSS Distributions uint64_t *data; 2279*27b03b36SApple OSS Distributions char *desc; 2280*27b03b36SApple OSS Distributions for (int i = 0; i < 3; i ++) { 2281*27b03b36SApple OSS Distributions kcdata_iter_get_data_with_desc(iter, &desc, (void **)&data, NULL); 2282*27b03b36SApple OSS Distributions if (strcmp(desc, "kcd_c_type") == 0) { 2283*27b03b36SApple OSS Distributions compression_type = *data; 2284*27b03b36SApple OSS Distributions } else if (strcmp(desc, "kcd_c_totalout") == 0){ 2285*27b03b36SApple OSS Distributions totalout = *data; 2286*27b03b36SApple OSS Distributions } else if (strcmp(desc, "kcd_c_totalin") == 0){ 2287*27b03b36SApple OSS Distributions totalin = *data; 2288*27b03b36SApple OSS Distributions } 2289*27b03b36SApple OSS Distributions 2290*27b03b36SApple OSS Distributions iter = kcdata_iter_next(iter); 2291*27b03b36SApple OSS Distributions } 2292*27b03b36SApple OSS Distributions 2293*27b03b36SApple OSS Distributions T_ASSERT_EQ(compression_type, UINT64_C(1), "zlib compression is used"); 2294*27b03b36SApple OSS Distributions T_ASSERT_GT(totalout, UINT64_C(0), "successfully gathered how long the compressed buffer is"); 2295*27b03b36SApple OSS Distributions T_ASSERT_GT(totalin, UINT64_C(0), "successfully gathered how long the uncompressed buffer will be at least"); 2296*27b03b36SApple OSS Distributions 2297*27b03b36SApple OSS Distributions /* progress to the next kcdata item */ 2298*27b03b36SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "compressed stackshot found"); 2299*27b03b36SApple OSS Distributions 2300*27b03b36SApple OSS Distributions char *bufferBase = kcdata_iter_payload(iter); 2301*27b03b36SApple OSS Distributions 2302*27b03b36SApple OSS Distributions /* 2303*27b03b36SApple OSS Distributions * zlib is used, allocate a buffer based on the metadata, plus 2304*27b03b36SApple OSS Distributions * extra scratch space (+12.5%) in case totalin was inconsistent 2305*27b03b36SApple OSS Distributions */ 2306*27b03b36SApple OSS Distributions size_t inflatedBufferSize = totalin + (totalin >> 3); 2307*27b03b36SApple OSS Distributions inflatedBufferBase = malloc(inflatedBufferSize); 2308*27b03b36SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(inflatedBufferBase, "allocated temporary output buffer"); 2309*27b03b36SApple OSS Distributions 2310*27b03b36SApple OSS Distributions z_stream zs; 2311*27b03b36SApple OSS Distributions memset(&zs, 0, sizeof(zs)); 2312*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(inflateInit(&zs), Z_OK, "inflateInit OK"); 2313*27b03b36SApple OSS Distributions zs.next_in = (unsigned char *)bufferBase; 2314*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE(totalout, (uint64_t)UINT_MAX, "stackshot is not too large"); 2315*27b03b36SApple OSS Distributions zs.avail_in = (uInt)totalout; 2316*27b03b36SApple OSS Distributions zs.next_out = (unsigned char *)inflatedBufferBase; 2317*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE(inflatedBufferSize, (size_t)UINT_MAX, "output region is not too large"); 2318*27b03b36SApple OSS Distributions zs.avail_out = (uInt)inflatedBufferSize; 2319*27b03b36SApple OSS Distributions T_ASSERT_EQ(inflate(&zs, Z_FINISH), Z_STREAM_END, "inflated buffer"); 2320*27b03b36SApple OSS Distributions inflateEnd(&zs); 2321*27b03b36SApple OSS Distributions 2322*27b03b36SApple OSS Distributions T_ASSERT_EQ((uint64_t)zs.total_out, totalin, "expected number of bytes inflated"); 2323*27b03b36SApple OSS Distributions 2324*27b03b36SApple OSS Distributions /* copy the data after the compressed area */ 2325*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GE((void *)bufferBase, ssbuf, 2326*27b03b36SApple OSS Distributions "base of compressed stackshot is after the returned stackshot buffer"); 2327*27b03b36SApple OSS Distributions size_t header_size = (size_t)(bufferBase - (char *)ssbuf); 2328*27b03b36SApple OSS Distributions size_t data_after_compressed_size = sslen - totalout - header_size; 2329*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE(data_after_compressed_size, 2330*27b03b36SApple OSS Distributions inflatedBufferSize - zs.total_out, 2331*27b03b36SApple OSS Distributions "footer fits in the buffer"); 2332*27b03b36SApple OSS Distributions memcpy(inflatedBufferBase + zs.total_out, 2333*27b03b36SApple OSS Distributions bufferBase + totalout, 2334*27b03b36SApple OSS Distributions data_after_compressed_size); 2335*27b03b36SApple OSS Distributions 2336*27b03b36SApple OSS Distributions iter = kcdata_iter(inflatedBufferBase, inflatedBufferSize); 2337*27b03b36SApple OSS Distributions } 2338*27b03b36SApple OSS Distributions } 2339*27b03b36SApple OSS Distributions 2340*27b03b36SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2341*27b03b36SApple OSS Distributions NSError *error = nil; 2342*27b03b36SApple OSS Distributions 2343*27b03b36SApple OSS Distributions switch (kcdata_iter_type(iter)) { 2344*27b03b36SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 2345*27b03b36SApple OSS Distributions T_QUIET; 2346*27b03b36SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_array_valid(iter), 2347*27b03b36SApple OSS Distributions "checked that array is valid"); 2348*27b03b36SApple OSS Distributions 2349*27b03b36SApple OSS Distributions NSMutableDictionary *array = parseKCDataArray(iter, &error); 2350*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(array, "parsed array from stackshot"); 2351*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing array"); 2352*27b03b36SApple OSS Distributions 2353*27b03b36SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) == STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT) { 2354*27b03b36SApple OSS Distributions struct dyld_uuid_info_64 *shared_cache_uuids = kcdata_iter_payload(iter); 2355*27b03b36SApple OSS Distributions uint32_t uuid_count = kcdata_iter_array_elem_count(iter); 2356*27b03b36SApple OSS Distributions T_ASSERT_NOTNULL(shared_cache_uuids, "parsed shared cache layout array"); 2357*27b03b36SApple OSS Distributions T_ASSERT_GT(uuid_count, 0, "returned valid number of UUIDs from shared cache"); 2358*27b03b36SApple OSS Distributions verify_stackshot_sharedcache_layout(shared_cache_uuids, uuid_count); 2359*27b03b36SApple OSS Distributions found_shared_cache_layout = true; 2360*27b03b36SApple OSS Distributions } 2361*27b03b36SApple OSS Distributions 2362*27b03b36SApple OSS Distributions break; 2363*27b03b36SApple OSS Distributions } 2364*27b03b36SApple OSS Distributions 2365*27b03b36SApple OSS Distributions case KCDATA_TYPE_CONTAINER_BEGIN: { 2366*27b03b36SApple OSS Distributions T_QUIET; 2367*27b03b36SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_container_valid(iter), 2368*27b03b36SApple OSS Distributions "checked that container is valid"); 2369*27b03b36SApple OSS Distributions 2370*27b03b36SApple OSS Distributions uint64_t containerid = kcdata_iter_container_id(iter); 2371*27b03b36SApple OSS Distributions uint32_t container_type = kcdata_iter_container_type(iter) ; 2372*27b03b36SApple OSS Distributions 2373*27b03b36SApple OSS Distributions /* 2374*27b03b36SApple OSS Distributions * treat containers other than tasks/transitioning_tasks 2375*27b03b36SApple OSS Distributions * as expanded in-line. 2376*27b03b36SApple OSS Distributions */ 2377*27b03b36SApple OSS Distributions if (container_type != STACKSHOT_KCCONTAINER_TASK && 2378*27b03b36SApple OSS Distributions container_type != STACKSHOT_KCCONTAINER_TRANSITIONING_TASK) { 2379*27b03b36SApple OSS Distributions break; 2380*27b03b36SApple OSS Distributions } 2381*27b03b36SApple OSS Distributions NSDictionary *container = parseKCDataContainer(&iter, &error); 2382*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(container, "parsed task/transitioning_task container from stackshot"); 2383*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container"); 2384*27b03b36SApple OSS Distributions 2385*27b03b36SApple OSS Distributions NSDictionary* task_snapshot = container[@"task_snapshots"][@"task_snapshot"]; 2386*27b03b36SApple OSS Distributions NSDictionary* task_delta_snapshot = container[@"task_snapshots"][@"task_delta_snapshot"]; 2387*27b03b36SApple OSS Distributions NSDictionary* transitioning_task_snapshot = container[@"transitioning_task_snapshots"][@"transitioning_task_snapshot"]; 2388*27b03b36SApple OSS Distributions 2389*27b03b36SApple OSS Distributions /* 2390*27b03b36SApple OSS Distributions * Having processed the container, we now only check it 2391*27b03b36SApple OSS Distributions * if it's the correct type. 2392*27b03b36SApple OSS Distributions */ 2393*27b03b36SApple OSS Distributions if ((!expect_transitioning_task && (container_type != STACKSHOT_KCCONTAINER_TASK)) || 2394*27b03b36SApple OSS Distributions (expect_transitioning_task && (container_type != STACKSHOT_KCCONTAINER_TRANSITIONING_TASK))) { 2395*27b03b36SApple OSS Distributions break; 2396*27b03b36SApple OSS Distributions } 2397*27b03b36SApple OSS Distributions if (!expect_transitioning_task) { 2398*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(!!task_snapshot != !!task_delta_snapshot, "Either task_snapshot xor task_delta_snapshot provided"); 2399*27b03b36SApple OSS Distributions } 2400*27b03b36SApple OSS Distributions 2401*27b03b36SApple OSS Distributions if (expect_dispatch_queue_label && !found_dispatch_queue_label) { 2402*27b03b36SApple OSS Distributions for (id thread_key in container[@"task_snapshots"][@"thread_snapshots"]) { 2403*27b03b36SApple OSS Distributions NSMutableDictionary *thread = container[@"task_snapshots"][@"thread_snapshots"][thread_key]; 2404*27b03b36SApple OSS Distributions NSString *dql = thread[@"dispatch_queue_label"]; 2405*27b03b36SApple OSS Distributions 2406*27b03b36SApple OSS Distributions if ([dql isEqualToString:@TEST_STACKSHOT_QUEUE_LABEL]) { 2407*27b03b36SApple OSS Distributions found_dispatch_queue_label = true; 2408*27b03b36SApple OSS Distributions break; 2409*27b03b36SApple OSS Distributions } 2410*27b03b36SApple OSS Distributions } 2411*27b03b36SApple OSS Distributions } 2412*27b03b36SApple OSS Distributions 2413*27b03b36SApple OSS Distributions if (expect_transitioning_task && !found_transitioning_task) { 2414*27b03b36SApple OSS Distributions if (transitioning_task_snapshot) { 2415*27b03b36SApple OSS Distributions uint64_t the_pid = [transitioning_task_snapshot[@"tts_pid"] unsignedLongLongValue]; 2416*27b03b36SApple OSS Distributions if (the_pid == (uint64_t)transistioning_task_pid) { 2417*27b03b36SApple OSS Distributions found_transitioning_task = true; 2418*27b03b36SApple OSS Distributions T_PASS("FOUND Transitioning task %llu has a transitioning task snapshot", (uint64_t) transistioning_task_pid); 2419*27b03b36SApple OSS Distributions break; 2420*27b03b36SApple OSS Distributions } 2421*27b03b36SApple OSS Distributions } 2422*27b03b36SApple OSS Distributions } 2423*27b03b36SApple OSS Distributions 2424*27b03b36SApple OSS Distributions if (expect_postexec_child && !found_postexec_child) { 2425*27b03b36SApple OSS Distributions if (task_snapshot) { 2426*27b03b36SApple OSS Distributions uint64_t unique_pid = [task_snapshot[@"ts_unique_pid"] unsignedLongLongValue]; 2427*27b03b36SApple OSS Distributions if (unique_pid == postexec_child_unique_pid) { 2428*27b03b36SApple OSS Distributions found_postexec_child = true; 2429*27b03b36SApple OSS Distributions 2430*27b03b36SApple OSS Distributions T_PASS("post-exec child %llu has a task snapshot", postexec_child_unique_pid); 2431*27b03b36SApple OSS Distributions 2432*27b03b36SApple OSS Distributions break; 2433*27b03b36SApple OSS Distributions } 2434*27b03b36SApple OSS Distributions } 2435*27b03b36SApple OSS Distributions 2436*27b03b36SApple OSS Distributions if (task_delta_snapshot) { 2437*27b03b36SApple OSS Distributions uint64_t unique_pid = [task_delta_snapshot[@"tds_unique_pid"] unsignedLongLongValue]; 2438*27b03b36SApple OSS Distributions if (unique_pid == postexec_child_unique_pid) { 2439*27b03b36SApple OSS Distributions found_postexec_child = true; 2440*27b03b36SApple OSS Distributions 2441*27b03b36SApple OSS Distributions T_FAIL("post-exec child %llu shouldn't have a delta task snapshot", postexec_child_unique_pid); 2442*27b03b36SApple OSS Distributions 2443*27b03b36SApple OSS Distributions break; 2444*27b03b36SApple OSS Distributions } 2445*27b03b36SApple OSS Distributions } 2446*27b03b36SApple OSS Distributions } 2447*27b03b36SApple OSS Distributions 2448*27b03b36SApple OSS Distributions if (!task_snapshot) { 2449*27b03b36SApple OSS Distributions break; 2450*27b03b36SApple OSS Distributions } 2451*27b03b36SApple OSS Distributions 2452*27b03b36SApple OSS Distributions int pid = [task_snapshot[@"ts_pid"] intValue]; 2453*27b03b36SApple OSS Distributions 2454*27b03b36SApple OSS Distributions if (pid && expect_shared_cache_uuid && !found_shared_cache_uuid) { 2455*27b03b36SApple OSS Distributions id ptr = container[@"task_snapshots"][@"shared_cache_dyld_load_info"]; 2456*27b03b36SApple OSS Distributions if (ptr) { 2457*27b03b36SApple OSS Distributions id uuid = ptr[@"imageUUID"]; 2458*27b03b36SApple OSS Distributions 2459*27b03b36SApple OSS Distributions uint8_t uuid_p[16]; 2460*27b03b36SApple OSS Distributions for (unsigned int i = 0; i < 16; i ++) { 2461*27b03b36SApple OSS Distributions NSNumber *uuidByte = uuid[i]; 2462*27b03b36SApple OSS Distributions uuid_p[i] = (uint8_t)uuidByte.charValue; 2463*27b03b36SApple OSS Distributions } 2464*27b03b36SApple OSS Distributions 2465*27b03b36SApple OSS Distributions check_shared_cache_uuid(uuid_p); 2466*27b03b36SApple OSS Distributions 2467*27b03b36SApple OSS Distributions uint64_t baseAddress = (uint64_t)((NSNumber *)ptr[@"imageSlidBaseAddress"]).longLongValue; 2468*27b03b36SApple OSS Distributions uint64_t firstMapping = (uint64_t)((NSNumber *)ptr[@"sharedCacheSlidFirstMapping"]).longLongValue; 2469*27b03b36SApple OSS Distributions 2470*27b03b36SApple OSS Distributions T_EXPECT_LE(baseAddress, firstMapping, 2471*27b03b36SApple OSS Distributions "in per-task shared_cache_dyld_load_info, " 2472*27b03b36SApple OSS Distributions "baseAddress <= firstMapping"); 2473*27b03b36SApple OSS Distributions T_EXPECT_GE(baseAddress + (7ull << 32) + (1ull << 29), 2474*27b03b36SApple OSS Distributions firstMapping, 2475*27b03b36SApple OSS Distributions "in per-task shared_cache_dyld_load_info, " 2476*27b03b36SApple OSS Distributions "baseAddress + 28.5gig >= firstMapping"); 2477*27b03b36SApple OSS Distributions 2478*27b03b36SApple OSS Distributions size_t shared_cache_len; 2479*27b03b36SApple OSS Distributions const void *addr = _dyld_get_shared_cache_range(&shared_cache_len); 2480*27b03b36SApple OSS Distributions T_EXPECT_EQ((uint64_t)addr, firstMapping, 2481*27b03b36SApple OSS Distributions "SlidFirstMapping should match shared_cache_range"); 2482*27b03b36SApple OSS Distributions 2483*27b03b36SApple OSS Distributions /* 2484*27b03b36SApple OSS Distributions * check_shared_cache_uuid() will assert on failure, so if 2485*27b03b36SApple OSS Distributions * we get here, then we have found the shared cache UUID 2486*27b03b36SApple OSS Distributions * and it's correct 2487*27b03b36SApple OSS Distributions */ 2488*27b03b36SApple OSS Distributions found_shared_cache_uuid = true; 2489*27b03b36SApple OSS Distributions } 2490*27b03b36SApple OSS Distributions } 2491*27b03b36SApple OSS Distributions 2492*27b03b36SApple OSS Distributions if (expect_sharedcache_child) { 2493*27b03b36SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 2494*27b03b36SApple OSS Distributions uint64_t sharedregion_flags = (task_flags & (kTaskSharedRegionNone | kTaskSharedRegionSystem | kTaskSharedRegionOther)); 2495*27b03b36SApple OSS Distributions id sharedregion_info = container[@"task_snapshots"][@"shared_cache_dyld_load_info"]; 2496*27b03b36SApple OSS Distributions if (!found_sharedcache_badflags) { 2497*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(sharedregion_flags, 0ll, "one of the kTaskSharedRegion flags should be set on all tasks"); 2498*27b03b36SApple OSS Distributions bool multiple = (sharedregion_flags & (sharedregion_flags - 1)) != 0; 2499*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_FALSE(multiple, "only one kTaskSharedRegion flag should be set on each task"); 2500*27b03b36SApple OSS Distributions found_sharedcache_badflags = (sharedregion_flags == 0 || multiple); 2501*27b03b36SApple OSS Distributions } 2502*27b03b36SApple OSS Distributions if (pid == 0) { 2503*27b03b36SApple OSS Distributions T_ASSERT_EQ(sharedregion_flags, (uint64_t)kTaskSharedRegionNone, "Kernel proc (pid 0) should have no shared region"); 2504*27b03b36SApple OSS Distributions } else if (pid == sharedcache_child_pid) { 2505*27b03b36SApple OSS Distributions found_sharedcache_child = true; 2506*27b03b36SApple OSS Distributions sharedcache_child_flags = sharedregion_flags; 2507*27b03b36SApple OSS Distributions } else if (pid == getpid()) { 2508*27b03b36SApple OSS Distributions found_sharedcache_self = true; 2509*27b03b36SApple OSS Distributions sharedcache_self_flags = sharedregion_flags; 2510*27b03b36SApple OSS Distributions } 2511*27b03b36SApple OSS Distributions if (sharedregion_flags == kTaskSharedRegionOther && !(task_flags & kTaskSharedRegionInfoUnavailable)) { 2512*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(sharedregion_info, "kTaskSharedRegionOther should have a shared_cache_dyld_load_info struct"); 2513*27b03b36SApple OSS Distributions } else { 2514*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NULL(sharedregion_info, "expect no shared_cache_dyld_load_info struct"); 2515*27b03b36SApple OSS Distributions } 2516*27b03b36SApple OSS Distributions } 2517*27b03b36SApple OSS Distributions 2518*27b03b36SApple OSS Distributions if (expect_zombie_child && (pid == zombie_child_pid)) { 2519*27b03b36SApple OSS Distributions found_zombie_child = true; 2520*27b03b36SApple OSS Distributions 2521*27b03b36SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 2522*27b03b36SApple OSS Distributions T_ASSERT_TRUE((task_flags & kTerminatedSnapshot) == kTerminatedSnapshot, "child zombie marked as terminated"); 2523*27b03b36SApple OSS Distributions 2524*27b03b36SApple OSS Distributions continue; 2525*27b03b36SApple OSS Distributions } 2526*27b03b36SApple OSS Distributions 2527*27b03b36SApple OSS Distributions if (expect_translated_child && (pid == translated_child_pid)) { 2528*27b03b36SApple OSS Distributions found_translated_child = true; 2529*27b03b36SApple OSS Distributions 2530*27b03b36SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 2531*27b03b36SApple OSS Distributions T_EXPECT_BITS_SET(task_flags, kTaskIsTranslated, "child marked as translated"); 2532*27b03b36SApple OSS Distributions 2533*27b03b36SApple OSS Distributions continue; 2534*27b03b36SApple OSS Distributions } 2535*27b03b36SApple OSS Distributions if (expect_exec_inprogress && (pid == exec_inprogress_pid || pid == -exec_inprogress_pid)) { 2536*27b03b36SApple OSS Distributions exec_inprogress_found++; 2537*27b03b36SApple OSS Distributions T_LOG("found exec task with pid %d, instance %d", pid, exec_inprogress_found); 2538*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_LE(exec_inprogress_found, 2, "no more than two with the expected pid"); 2539*27b03b36SApple OSS Distributions if (exec_inprogress_found == 2) { 2540*27b03b36SApple OSS Distributions T_LOG("found 2 tasks with pid %d", exec_inprogress_pid); 2541*27b03b36SApple OSS Distributions exec_inprogress_cb(containerid, exec_inprogress_containerid); 2542*27b03b36SApple OSS Distributions } else { 2543*27b03b36SApple OSS Distributions exec_inprogress_containerid = containerid; 2544*27b03b36SApple OSS Distributions } 2545*27b03b36SApple OSS Distributions } 2546*27b03b36SApple OSS Distributions if (expect_cseg_waitinfo) { 2547*27b03b36SApple OSS Distributions NSArray *winfos = container[@"task_snapshots"][@"thread_waitinfo"]; 2548*27b03b36SApple OSS Distributions 2549*27b03b36SApple OSS Distributions for (id i in winfos) { 2550*27b03b36SApple OSS Distributions NSNumber *waitType = i[@"wait_type"]; 2551*27b03b36SApple OSS Distributions NSNumber *owner = i[@"owner"]; 2552*27b03b36SApple OSS Distributions if (waitType.intValue == kThreadWaitCompressor && 2553*27b03b36SApple OSS Distributions owner.unsignedLongValue == cseg_expected_threadid) { 2554*27b03b36SApple OSS Distributions found_cseg_waitinfo = true; 2555*27b03b36SApple OSS Distributions break; 2556*27b03b36SApple OSS Distributions } 2557*27b03b36SApple OSS Distributions } 2558*27b03b36SApple OSS Distributions } 2559*27b03b36SApple OSS Distributions 2560*27b03b36SApple OSS Distributions if (expect_srp_waitinfo) { 2561*27b03b36SApple OSS Distributions NSArray *tinfos = container[@"task_snapshots"][@"thread_turnstileinfo"]; 2562*27b03b36SApple OSS Distributions NSArray *winfos = container[@"task_snapshots"][@"thread_waitinfo"]; 2563*27b03b36SApple OSS Distributions for (id i in tinfos) { 2564*27b03b36SApple OSS Distributions if (!found_srp_waitinfo) { 2565*27b03b36SApple OSS Distributions bool found_thread = false; 2566*27b03b36SApple OSS Distributions bool found_pid = false; 2567*27b03b36SApple OSS Distributions if (([i[@"turnstile_flags"] intValue] & STACKSHOT_TURNSTILE_STATUS_THREAD) && 2568*27b03b36SApple OSS Distributions [i[@"turnstile_context"] unsignedLongLongValue] == srp_expected_threadid && 2569*27b03b36SApple OSS Distributions srp_expected_threadid != 0) { 2570*27b03b36SApple OSS Distributions found_thread = true; 2571*27b03b36SApple OSS Distributions } 2572*27b03b36SApple OSS Distributions if (([i[@"turnstile_flags"] intValue] & STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK) && 2573*27b03b36SApple OSS Distributions [i[@"turnstile_context"] intValue] == srp_expected_pid && 2574*27b03b36SApple OSS Distributions srp_expected_pid != -1) { 2575*27b03b36SApple OSS Distributions found_pid = true; 2576*27b03b36SApple OSS Distributions } 2577*27b03b36SApple OSS Distributions if (found_pid || found_thread) { 2578*27b03b36SApple OSS Distributions T_LOG("found SRP %s %lld waiter: %d", (found_thread ? "thread" : "pid"), 2579*27b03b36SApple OSS Distributions [i[@"turnstile_context"] unsignedLongLongValue], [i[@"waiter"] intValue]); 2580*27b03b36SApple OSS Distributions /* we found something that is blocking the correct threadid */ 2581*27b03b36SApple OSS Distributions for (id j in winfos) { 2582*27b03b36SApple OSS Distributions if ([j[@"waiter"] intValue] == [i[@"waiter"] intValue] && 2583*27b03b36SApple OSS Distributions [j[@"wait_type"] intValue] == kThreadWaitPortReceive) { 2584*27b03b36SApple OSS Distributions found_srp_waitinfo = true; 2585*27b03b36SApple OSS Distributions T_EXPECT_EQ([j[@"wait_flags"] intValue], STACKSHOT_WAITINFO_FLAGS_SPECIALREPLY, 2586*27b03b36SApple OSS Distributions "SRP waitinfo should be marked as a special reply"); 2587*27b03b36SApple OSS Distributions break; 2588*27b03b36SApple OSS Distributions } 2589*27b03b36SApple OSS Distributions } 2590*27b03b36SApple OSS Distributions 2591*27b03b36SApple OSS Distributions if (found_srp_waitinfo) { 2592*27b03b36SApple OSS Distributions break; 2593*27b03b36SApple OSS Distributions } 2594*27b03b36SApple OSS Distributions } 2595*27b03b36SApple OSS Distributions } 2596*27b03b36SApple OSS Distributions } 2597*27b03b36SApple OSS Distributions } 2598*27b03b36SApple OSS Distributions 2599*27b03b36SApple OSS Distributions if (pid != getpid()) { 2600*27b03b36SApple OSS Distributions break; 2601*27b03b36SApple OSS Distributions } 2602*27b03b36SApple OSS Distributions 2603*27b03b36SApple OSS Distributions T_EXPECT_EQ_STR(current_process_name(), 2604*27b03b36SApple OSS Distributions [task_snapshot[@"ts_p_comm"] UTF8String], 2605*27b03b36SApple OSS Distributions "current process name matches in stackshot"); 2606*27b03b36SApple OSS Distributions 2607*27b03b36SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 2608*27b03b36SApple OSS Distributions T_ASSERT_BITS_NOTSET(task_flags, kTerminatedSnapshot, "current process not marked as terminated"); 2609*27b03b36SApple OSS Distributions T_ASSERT_BITS_NOTSET(task_flags, kTaskIsTranslated, "current process not marked as translated"); 2610*27b03b36SApple OSS Distributions 2611*27b03b36SApple OSS Distributions T_QUIET; 2612*27b03b36SApple OSS Distributions T_EXPECT_LE(pid, [task_snapshot[@"ts_unique_pid"] intValue], 2613*27b03b36SApple OSS Distributions "unique pid is greater than pid"); 2614*27b03b36SApple OSS Distributions 2615*27b03b36SApple OSS Distributions NSDictionary* task_cpu_architecture = container[@"task_snapshots"][@"task_cpu_architecture"]; 2616*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(task_cpu_architecture[@"cputype"], "have cputype"); 2617*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(task_cpu_architecture[@"cpusubtype"], "have cputype"); 2618*27b03b36SApple OSS Distributions int cputype = [task_cpu_architecture[@"cputype"] intValue]; 2619*27b03b36SApple OSS Distributions int cpusubtype = [task_cpu_architecture[@"cpusubtype"] intValue]; 2620*27b03b36SApple OSS Distributions 2621*27b03b36SApple OSS Distributions struct proc_archinfo archinfo; 2622*27b03b36SApple OSS Distributions int retval = proc_pidinfo(pid, PROC_PIDARCHINFO, 0, &archinfo, sizeof(archinfo)); 2623*27b03b36SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_GT(retval, 0, "proc_pidinfo(PROC_PIDARCHINFO) returned a value > 0"); 2624*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(retval, (int)sizeof(struct proc_archinfo), "proc_pidinfo call for PROC_PIDARCHINFO returned expected size"); 2625*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_EQ(cputype, archinfo.p_cputype, "cpu type is correct"); 2626*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_EQ(cpusubtype, archinfo.p_cpusubtype, "cpu subtype is correct"); 2627*27b03b36SApple OSS Distributions 2628*27b03b36SApple OSS Distributions bool found_main_thread = false; 2629*27b03b36SApple OSS Distributions uint64_t main_thread_id = -1ULL; 2630*27b03b36SApple OSS Distributions bool found_null_kernel_frame = false; 2631*27b03b36SApple OSS Distributions for (id thread_key in container[@"task_snapshots"][@"thread_snapshots"]) { 2632*27b03b36SApple OSS Distributions NSMutableDictionary *thread = container[@"task_snapshots"][@"thread_snapshots"][thread_key]; 2633*27b03b36SApple OSS Distributions NSDictionary *thread_snap = thread[@"thread_snapshot"]; 2634*27b03b36SApple OSS Distributions 2635*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_thread_id"] intValue], 0, 2636*27b03b36SApple OSS Distributions "thread ID of thread in current task is valid"); 2637*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_base_priority"] intValue], 0, 2638*27b03b36SApple OSS Distributions "base priority of thread in current task is valid"); 2639*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_sched_priority"] intValue], 0, 2640*27b03b36SApple OSS Distributions "scheduling priority of thread in current task is valid"); 2641*27b03b36SApple OSS Distributions 2642*27b03b36SApple OSS Distributions NSString *pth_name = thread[@"pth_name"]; 2643*27b03b36SApple OSS Distributions if (pth_name != nil && [pth_name isEqualToString:@TEST_THREAD_NAME]) { 2644*27b03b36SApple OSS Distributions found_main_thread = true; 2645*27b03b36SApple OSS Distributions main_thread_id = [thread_snap[@"ths_thread_id"] unsignedLongLongValue]; 2646*27b03b36SApple OSS Distributions 2647*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_total_syscalls"] intValue], 0, 2648*27b03b36SApple OSS Distributions "total syscalls of current thread is valid"); 2649*27b03b36SApple OSS Distributions 2650*27b03b36SApple OSS Distributions NSDictionary *cpu_times = thread[@"cpu_times"]; 2651*27b03b36SApple OSS Distributions T_EXPECT_GE([cpu_times[@"runnable_time"] intValue], 2652*27b03b36SApple OSS Distributions [cpu_times[@"system_time"] intValue] + 2653*27b03b36SApple OSS Distributions [cpu_times[@"user_time"] intValue], 2654*27b03b36SApple OSS Distributions "runnable time of current thread is valid"); 2655*27b03b36SApple OSS Distributions } 2656*27b03b36SApple OSS Distributions if (!found_null_kernel_frame) { 2657*27b03b36SApple OSS Distributions for (NSNumber *frame in thread[@"kernel_frames"]) { 2658*27b03b36SApple OSS Distributions if (frame.unsignedLongValue == 0) { 2659*27b03b36SApple OSS Distributions found_null_kernel_frame = true; 2660*27b03b36SApple OSS Distributions break; 2661*27b03b36SApple OSS Distributions } 2662*27b03b36SApple OSS Distributions } 2663*27b03b36SApple OSS Distributions } 2664*27b03b36SApple OSS Distributions if (expect_asyncstack && !found_asyncstack && 2665*27b03b36SApple OSS Distributions asyncstack_threadid == [thread_snap[@"ths_thread_id"] unsignedLongLongValue]) { 2666*27b03b36SApple OSS Distributions found_asyncstack = true; 2667*27b03b36SApple OSS Distributions NSArray* async_stack = thread[@"user_async_stack_frames"]; 2668*27b03b36SApple OSS Distributions NSNumber* start_idx = thread[@"user_async_start_index"]; 2669*27b03b36SApple OSS Distributions NSArray* user_stack = thread[@"user_stack_frames"]; 2670*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(async_stack, "async thread %#llx has user_async_stack_frames", asyncstack_threadid); 2671*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(start_idx, "async thread %#llx has user_async_start_index", asyncstack_threadid); 2672*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(user_stack, "async thread %#llx has user_stack_frames", asyncstack_threadid); 2673*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_EQ(async_stack.count, asyncstack_stack.count, 2674*27b03b36SApple OSS Distributions "actual async_stack count == expected async_stack count"); 2675*27b03b36SApple OSS Distributions for (size_t i = 0; i < async_stack.count; i++) { 2676*27b03b36SApple OSS Distributions T_EXPECT_EQ([async_stack[i][@"lr"] unsignedLongLongValue], 2677*27b03b36SApple OSS Distributions [asyncstack_stack[i] unsignedLongLongValue], "frame %zu matches", i); 2678*27b03b36SApple OSS Distributions } 2679*27b03b36SApple OSS Distributions } 2680*27b03b36SApple OSS Distributions } 2681*27b03b36SApple OSS Distributions T_EXPECT_TRUE(found_main_thread, "found main thread for current task in stackshot"); 2682*27b03b36SApple OSS Distributions T_EXPECT_FALSE(found_null_kernel_frame, "should not see any NULL kernel frames"); 2683*27b03b36SApple OSS Distributions 2684*27b03b36SApple OSS Distributions if (expect_turnstile_lock && !found_turnstile_lock) { 2685*27b03b36SApple OSS Distributions NSArray *tsinfos = container[@"task_snapshots"][@"thread_turnstileinfo"]; 2686*27b03b36SApple OSS Distributions 2687*27b03b36SApple OSS Distributions for (id i in tsinfos) { 2688*27b03b36SApple OSS Distributions if ([i[@"turnstile_context"] unsignedLongLongValue] == main_thread_id) { 2689*27b03b36SApple OSS Distributions found_turnstile_lock = true; 2690*27b03b36SApple OSS Distributions break; 2691*27b03b36SApple OSS Distributions } 2692*27b03b36SApple OSS Distributions } 2693*27b03b36SApple OSS Distributions } 2694*27b03b36SApple OSS Distributions break; 2695*27b03b36SApple OSS Distributions } 2696*27b03b36SApple OSS Distributions case STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO: { 2697*27b03b36SApple OSS Distributions struct dyld_shared_cache_loadinfo *payload = kcdata_iter_payload(iter); 2698*27b03b36SApple OSS Distributions T_ASSERT_EQ((size_t)kcdata_iter_size(iter), sizeof(*payload), "valid dyld_shared_cache_loadinfo struct"); 2699*27b03b36SApple OSS Distributions 2700*27b03b36SApple OSS Distributions check_shared_cache_uuid(payload->sharedCacheUUID); 2701*27b03b36SApple OSS Distributions 2702*27b03b36SApple OSS Distributions T_EXPECT_LE(payload->sharedCacheUnreliableSlidBaseAddress, 2703*27b03b36SApple OSS Distributions payload->sharedCacheSlidFirstMapping, 2704*27b03b36SApple OSS Distributions "SlidBaseAddress <= SlidFirstMapping"); 2705*27b03b36SApple OSS Distributions T_EXPECT_GE(payload->sharedCacheUnreliableSlidBaseAddress + (7ull << 32) + (1ull << 29), 2706*27b03b36SApple OSS Distributions payload->sharedCacheSlidFirstMapping, 2707*27b03b36SApple OSS Distributions "SlidFirstMapping should be within 28.5gigs of SlidBaseAddress"); 2708*27b03b36SApple OSS Distributions 2709*27b03b36SApple OSS Distributions size_t shared_cache_len; 2710*27b03b36SApple OSS Distributions const void *addr = _dyld_get_shared_cache_range(&shared_cache_len); 2711*27b03b36SApple OSS Distributions T_EXPECT_EQ((uint64_t)addr, payload->sharedCacheSlidFirstMapping, 2712*27b03b36SApple OSS Distributions "SlidFirstMapping should match shared_cache_range"); 2713*27b03b36SApple OSS Distributions 2714*27b03b36SApple OSS Distributions /* 2715*27b03b36SApple OSS Distributions * check_shared_cache_uuid() asserts on failure, so we must have 2716*27b03b36SApple OSS Distributions * found the shared cache UUID to be correct. 2717*27b03b36SApple OSS Distributions */ 2718*27b03b36SApple OSS Distributions found_shared_cache_uuid = true; 2719*27b03b36SApple OSS Distributions break; 2720*27b03b36SApple OSS Distributions } 2721*27b03b36SApple OSS Distributions } 2722*27b03b36SApple OSS Distributions } 2723*27b03b36SApple OSS Distributions 2724*27b03b36SApple OSS Distributions if (expect_sharedcache_child) { 2725*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_sharedcache_child, "found sharedcache child in kcdata"); 2726*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_sharedcache_self, "found self in kcdata"); 2727*27b03b36SApple OSS Distributions if (found_sharedcache_child && found_sharedcache_self) { 2728*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(sharedcache_child_flags, (uint64_t)kTaskSharedRegionNone, "sharedcache child should have shared region"); 2729*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NE(sharedcache_self_flags, (uint64_t)kTaskSharedRegionNone, "sharedcache: self should have shared region"); 2730*27b03b36SApple OSS Distributions if (sharedcache_self_flags == kTaskSharedRegionSystem && !sharedcache_child_sameaddr) { 2731*27b03b36SApple OSS Distributions /* If we're in the system shared region, and the child has a different address, child must have an Other shared region */ 2732*27b03b36SApple OSS Distributions T_ASSERT_EQ(sharedcache_child_flags, (uint64_t)kTaskSharedRegionOther, 2733*27b03b36SApple OSS Distributions "sharedcache child should have Other shared region"); 2734*27b03b36SApple OSS Distributions } 2735*27b03b36SApple OSS Distributions } 2736*27b03b36SApple OSS Distributions } 2737*27b03b36SApple OSS Distributions 2738*27b03b36SApple OSS Distributions if (expect_transitioning_task) { 2739*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_transitioning_task, "found transitioning_task child in kcdata"); 2740*27b03b36SApple OSS Distributions } 2741*27b03b36SApple OSS Distributions 2742*27b03b36SApple OSS Distributions if (expect_exec_inprogress) { 2743*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_GT(exec_inprogress_found, 0, "found at least 1 task for execing process"); 2744*27b03b36SApple OSS Distributions } 2745*27b03b36SApple OSS Distributions 2746*27b03b36SApple OSS Distributions if (expect_zombie_child) { 2747*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_zombie_child, "found zombie child in kcdata"); 2748*27b03b36SApple OSS Distributions } 2749*27b03b36SApple OSS Distributions 2750*27b03b36SApple OSS Distributions if (expect_postexec_child) { 2751*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_postexec_child, "found post-exec child in kcdata"); 2752*27b03b36SApple OSS Distributions } 2753*27b03b36SApple OSS Distributions 2754*27b03b36SApple OSS Distributions if (expect_translated_child) { 2755*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_translated_child, "found translated child in kcdata"); 2756*27b03b36SApple OSS Distributions } 2757*27b03b36SApple OSS Distributions 2758*27b03b36SApple OSS Distributions if (expect_shared_cache_layout) { 2759*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_shared_cache_layout, "shared cache layout found in kcdata"); 2760*27b03b36SApple OSS Distributions } 2761*27b03b36SApple OSS Distributions 2762*27b03b36SApple OSS Distributions if (expect_shared_cache_uuid) { 2763*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_shared_cache_uuid, "shared cache UUID found in kcdata"); 2764*27b03b36SApple OSS Distributions } 2765*27b03b36SApple OSS Distributions 2766*27b03b36SApple OSS Distributions if (expect_dispatch_queue_label) { 2767*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_dispatch_queue_label, "dispatch queue label found in kcdata"); 2768*27b03b36SApple OSS Distributions } 2769*27b03b36SApple OSS Distributions 2770*27b03b36SApple OSS Distributions if (expect_turnstile_lock) { 2771*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_turnstile_lock, "found expected deadlock"); 2772*27b03b36SApple OSS Distributions } 2773*27b03b36SApple OSS Distributions 2774*27b03b36SApple OSS Distributions if (expect_cseg_waitinfo) { 2775*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_cseg_waitinfo, "found c_seg waitinfo"); 2776*27b03b36SApple OSS Distributions } 2777*27b03b36SApple OSS Distributions 2778*27b03b36SApple OSS Distributions if (expect_srp_waitinfo) { 2779*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_srp_waitinfo, "found special reply port waitinfo"); 2780*27b03b36SApple OSS Distributions } 2781*27b03b36SApple OSS Distributions 2782*27b03b36SApple OSS Distributions if (expect_asyncstack) { 2783*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_asyncstack, "found async stack threadid"); 2784*27b03b36SApple OSS Distributions } 2785*27b03b36SApple OSS Distributions 2786*27b03b36SApple OSS Distributions T_ASSERT_FALSE(KCDATA_ITER_FOREACH_FAILED(iter), "successfully iterated kcdata"); 2787*27b03b36SApple OSS Distributions 2788*27b03b36SApple OSS Distributions free(inflatedBufferBase); 2789*27b03b36SApple OSS Distributions} 2790*27b03b36SApple OSS Distributions 2791*27b03b36SApple OSS Distributionsstatic const char * 2792*27b03b36SApple OSS Distributionscurrent_process_name(void) 2793*27b03b36SApple OSS Distributions{ 2794*27b03b36SApple OSS Distributions static char name[64]; 2795*27b03b36SApple OSS Distributions 2796*27b03b36SApple OSS Distributions if (!name[0]) { 2797*27b03b36SApple OSS Distributions int ret = proc_name(getpid(), name, sizeof(name)); 2798*27b03b36SApple OSS Distributions T_QUIET; 2799*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "proc_name failed for current process"); 2800*27b03b36SApple OSS Distributions } 2801*27b03b36SApple OSS Distributions 2802*27b03b36SApple OSS Distributions return name; 2803*27b03b36SApple OSS Distributions} 2804*27b03b36SApple OSS Distributions 2805*27b03b36SApple OSS Distributionsstatic void 2806*27b03b36SApple OSS Distributionsinitialize_thread(void) 2807*27b03b36SApple OSS Distributions{ 2808*27b03b36SApple OSS Distributions int ret = pthread_setname_np(TEST_THREAD_NAME); 2809*27b03b36SApple OSS Distributions T_QUIET; 2810*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set thread name to %s", TEST_THREAD_NAME); 2811*27b03b36SApple OSS Distributions} 2812