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