1*4f1223e8SApple OSS Distributions#include <darwintest.h> 2*4f1223e8SApple OSS Distributions#include <darwintest_utils.h> 3*4f1223e8SApple OSS Distributions#include <darwintest_multiprocess.h> 4*4f1223e8SApple OSS Distributions#include <kern/debug.h> 5*4f1223e8SApple OSS Distributions#include <kern/kern_cdata.h> 6*4f1223e8SApple OSS Distributions#include <kern/block_hint.h> 7*4f1223e8SApple OSS Distributions#include <kdd.h> 8*4f1223e8SApple OSS Distributions#include <libproc.h> 9*4f1223e8SApple OSS Distributions#include <os/atomic_private.h> 10*4f1223e8SApple OSS Distributions#include <mach-o/dyld.h> 11*4f1223e8SApple OSS Distributions#include <mach-o/dyld_images.h> 12*4f1223e8SApple OSS Distributions#include <mach-o/dyld_priv.h> 13*4f1223e8SApple OSS Distributions#include <sys/syscall.h> 14*4f1223e8SApple OSS Distributions#include <sys/stackshot.h> 15*4f1223e8SApple OSS Distributions#include <uuid/uuid.h> 16*4f1223e8SApple OSS Distributions#include <servers/bootstrap.h> 17*4f1223e8SApple OSS Distributions#include <pthread/workqueue_private.h> 18*4f1223e8SApple OSS Distributions#include <dispatch/private.h> 19*4f1223e8SApple OSS Distributions#include <stdalign.h> 20*4f1223e8SApple OSS Distributions#include <TargetConditionals.h> 21*4f1223e8SApple OSS Distributions 22*4f1223e8SApple OSS Distributions#import <zlib.h> 23*4f1223e8SApple OSS Distributions#import <IOKit/IOKitLib.h> 24*4f1223e8SApple OSS Distributions#import <IOKit/IOKitLibPrivate.h> 25*4f1223e8SApple OSS Distributions#import <IOKit/IOKitKeysPrivate.h> 26*4f1223e8SApple OSS Distributions#import "test_utils.h" 27*4f1223e8SApple OSS Distributions 28*4f1223e8SApple OSS Distributions 29*4f1223e8SApple OSS Distributions 30*4f1223e8SApple OSS DistributionsT_GLOBAL_META( 31*4f1223e8SApple OSS Distributions T_META_NAMESPACE("xnu.stackshot"), 32*4f1223e8SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 33*4f1223e8SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("stackshot"), 34*4f1223e8SApple OSS Distributions T_META_OWNER("jonathan_w_adams"), 35*4f1223e8SApple OSS Distributions T_META_CHECK_LEAKS(false), 36*4f1223e8SApple OSS Distributions T_META_ASROOT(true), 37*4f1223e8SApple OSS Distributions XNU_T_META_SOC_SPECIFIC 38*4f1223e8SApple OSS Distributions ); 39*4f1223e8SApple OSS Distributions 40*4f1223e8SApple OSS Distributionsstatic const char *current_process_name(void); 41*4f1223e8SApple OSS Distributionsstatic void verify_stackshot_sharedcache_layout(struct dyld_uuid_info_64 *uuids, uint32_t uuid_count); 42*4f1223e8SApple OSS Distributionsstatic void parse_stackshot(uint64_t stackshot_parsing_flags, void *ssbuf, size_t sslen, NSDictionary *extra); 43*4f1223e8SApple OSS Distributionsstatic void parse_thread_group_stackshot(void **sbuf, size_t sslen); 44*4f1223e8SApple OSS Distributionsstatic uint64_t stackshot_timestamp(void *ssbuf, size_t sslen); 45*4f1223e8SApple OSS Distributionsstatic void initialize_thread(void); 46*4f1223e8SApple OSS Distributions 47*4f1223e8SApple OSS Distributionsstatic uint64_t global_flags = 0; 48*4f1223e8SApple OSS Distributions 49*4f1223e8SApple OSS Distributions#define DEFAULT_STACKSHOT_BUFFER_SIZE (1024 * 1024) 50*4f1223e8SApple OSS Distributions#define MAX_STACKSHOT_BUFFER_SIZE (6 * 1024 * 1024) 51*4f1223e8SApple OSS Distributions 52*4f1223e8SApple OSS Distributions#define SRP_SERVICE_NAME "com.apple.xnu.test.stackshot.special_reply_port" 53*4f1223e8SApple OSS Distributions 54*4f1223e8SApple OSS Distributions/* bit flags for parse_stackshot */ 55*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_DELTA 0x01 56*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_ZOMBIE 0x02 57*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_SHAREDCACHE_LAYOUT 0x04 58*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_DISPATCH_QUEUE_LABEL 0x08 59*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_TURNSTILEINFO 0x10 60*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_POSTEXEC 0x20 61*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_WAITINFO_CSEG 0x40 62*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_WAITINFO_SRP 0x80 63*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_TRANSLATED 0x100 64*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_SHAREDCACHE_FLAGS 0x200 65*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_EXEC_INPROGRESS 0x400 66*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_TRANSITIONING 0x800 67*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_ASYNCSTACK 0x1000 68*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_COMPACTINFO 0x2000 /* TODO: rdar://88789261 */ 69*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_DRIVERKIT 0x4000 70*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_THROTTLED_SP 0x8000 71*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_SUSPENDINFO 0x10000 72*4f1223e8SApple OSS Distributions#define PARSE_STACKSHOT_TARGETPID 0x20000 73*4f1223e8SApple OSS Distributions 74*4f1223e8SApple OSS Distributions/* keys for 'extra' dictionary for parse_stackshot */ 75*4f1223e8SApple OSS Distributionsstatic const NSString* zombie_child_pid_key = @"zombie_child_pid"; // -> @(pid), required for PARSE_STACKSHOT_ZOMBIE 76*4f1223e8SApple OSS Distributionsstatic const NSString* postexec_child_unique_pid_key = @"postexec_child_unique_pid"; // -> @(unique_pid), required for PARSE_STACKSHOT_POSTEXEC 77*4f1223e8SApple OSS Distributionsstatic const NSString* cseg_expected_threadid_key = @"cseg_expected_threadid"; // -> @(tid), required for PARSE_STACKSHOT_WAITINFO_CSEG 78*4f1223e8SApple OSS Distributionsstatic const NSString* srp_expected_threadid_key = @"srp_expected_threadid"; // -> @(tid), this or ..._pid required for PARSE_STACKSHOT_WAITINFO_SRP 79*4f1223e8SApple OSS Distributionsstatic const NSString* srp_expected_pid_key = @"srp_expected_pid"; // -> @(pid), this or ..._threadid required for PARSE_STACKSHOT_WAITINFO_SRP 80*4f1223e8SApple OSS Distributionsstatic const NSString* translated_child_pid_key = @"translated_child_pid"; // -> @(pid), required for PARSE_STACKSHOT_TRANSLATED 81*4f1223e8SApple OSS Distributionsstatic const NSString* sharedcache_child_pid_key = @"sharedcache_child_pid"; // @(pid), required for PARSE_STACKSHOT_SHAREDCACHE_FLAGS 82*4f1223e8SApple OSS Distributionsstatic const NSString* sharedcache_child_sameaddr_key = @"sharedcache_child_sameaddr"; // @(0 or 1), required for PARSE_STACKSHOT_SHAREDCACHE_FLAGS 83*4f1223e8SApple OSS Distributionsstatic const NSString* exec_inprogress_pid_key = @"exec_inprogress_pid"; 84*4f1223e8SApple OSS Distributionsstatic const NSString* exec_inprogress_found_key = @"exec_inprogress_found"; // callback when inprogress is found 85*4f1223e8SApple OSS Distributionsstatic const NSString* transitioning_pid_key = @"transitioning_task_pid"; // -> @(pid), required for PARSE_STACKSHOT_TRANSITIONING 86*4f1223e8SApple OSS Distributionsstatic const NSString* asyncstack_expected_threadid_key = @"asyncstack_expected_threadid"; // -> @(tid), required for PARSE_STACKSHOT_ASYNCSTACK 87*4f1223e8SApple OSS Distributionsstatic const NSString* asyncstack_expected_stack_key = @"asyncstack_expected_stack"; // -> @[pc...]), expected PCs for asyncstack 88*4f1223e8SApple OSS Distributionsstatic const NSString* driverkit_found_key = @"driverkit_found_key"; // callback when driverkit process is found. argument is the process pid. 89*4f1223e8SApple OSS Distributionsstatic const NSString* sp_throttled_expected_ctxt_key = @"sp_throttled_expected_ctxt_key"; // -> @(ctxt), required for PARSE_STACKSHOT_THROTTLED_SP 90*4f1223e8SApple OSS Distributionsstatic const NSString* sp_throttled_expect_flag = @"sp_throttled_expect_flag"; // -> @(is_throttled), required for PARSE_STACKSHOT_THROTTLED_SP 91*4f1223e8SApple OSS Distributionsstatic const NSString* no_exclaves_key = @"no_exclaves"; 92*4f1223e8SApple OSS Distributions 93*4f1223e8SApple OSS Distributions#define TEST_STACKSHOT_QUEUE_LABEL "houston.we.had.a.problem" 94*4f1223e8SApple OSS Distributions#define TEST_STACKSHOT_QUEUE_LABEL_LENGTH sizeof(TEST_STACKSHOT_QUEUE_LABEL) 95*4f1223e8SApple OSS Distributions 96*4f1223e8SApple OSS Distributions#define THROTTLED_SERVICE_NAME "com.apple.xnu.test.stackshot.throttled_service" 97*4f1223e8SApple OSS Distributions 98*4f1223e8SApple OSS Distributionsstatic int64_t 99*4f1223e8SApple OSS Distributionsrun_sysctl_test(const char *t, int64_t value) 100*4f1223e8SApple OSS Distributions{ 101*4f1223e8SApple OSS Distributions char name[1024]; 102*4f1223e8SApple OSS Distributions int64_t result = 0; 103*4f1223e8SApple OSS Distributions size_t s = sizeof(value); 104*4f1223e8SApple OSS Distributions int rc; 105*4f1223e8SApple OSS Distributions 106*4f1223e8SApple OSS Distributions snprintf(name, sizeof(name), "debug.test.%s", t); 107*4f1223e8SApple OSS Distributions rc = sysctlbyname(name, &result, &s, &value, s); 108*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", name); 109*4f1223e8SApple OSS Distributions return result; 110*4f1223e8SApple OSS Distributions} 111*4f1223e8SApple OSS Distributions 112*4f1223e8SApple OSS DistributionsT_DECL(microstackshots, "test the microstackshot syscall", T_META_TAG_VM_PREFERRED) 113*4f1223e8SApple OSS Distributions{ 114*4f1223e8SApple OSS Distributions void *buf = NULL; 115*4f1223e8SApple OSS Distributions unsigned int size = DEFAULT_STACKSHOT_BUFFER_SIZE; 116*4f1223e8SApple OSS Distributions 117*4f1223e8SApple OSS Distributions while (1) { 118*4f1223e8SApple OSS Distributions buf = malloc(size); 119*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(buf, "allocated stackshot buffer"); 120*4f1223e8SApple OSS Distributions 121*4f1223e8SApple OSS Distributions#pragma clang diagnostic push 122*4f1223e8SApple OSS Distributions#pragma clang diagnostic ignored "-Wdeprecated-declarations" 123*4f1223e8SApple OSS Distributions int len = syscall(SYS_microstackshot, buf, size, 124*4f1223e8SApple OSS Distributions (uint32_t) STACKSHOT_GET_MICROSTACKSHOT); 125*4f1223e8SApple OSS Distributions#pragma clang diagnostic pop 126*4f1223e8SApple OSS Distributions if (len == ENOSYS) { 127*4f1223e8SApple OSS Distributions T_SKIP("microstackshot syscall failed, likely not compiled with CONFIG_TELEMETRY"); 128*4f1223e8SApple OSS Distributions } 129*4f1223e8SApple OSS Distributions if (len == -1 && errno == ENOSPC) { 130*4f1223e8SApple OSS Distributions /* syscall failed because buffer wasn't large enough, try again */ 131*4f1223e8SApple OSS Distributions free(buf); 132*4f1223e8SApple OSS Distributions buf = NULL; 133*4f1223e8SApple OSS Distributions size *= 2; 134*4f1223e8SApple OSS Distributions T_ASSERT_LE(size, (unsigned int)MAX_STACKSHOT_BUFFER_SIZE, 135*4f1223e8SApple OSS Distributions "growing stackshot buffer to sane size"); 136*4f1223e8SApple OSS Distributions continue; 137*4f1223e8SApple OSS Distributions } 138*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(len, "called microstackshot syscall"); 139*4f1223e8SApple OSS Distributions break; 140*4f1223e8SApple OSS Distributions } 141*4f1223e8SApple OSS Distributions 142*4f1223e8SApple OSS Distributions T_EXPECT_EQ(*(uint32_t *)buf, 143*4f1223e8SApple OSS Distributions (uint32_t)STACKSHOT_MICRO_SNAPSHOT_MAGIC, 144*4f1223e8SApple OSS Distributions "magic value for microstackshot matches"); 145*4f1223e8SApple OSS Distributions 146*4f1223e8SApple OSS Distributions free(buf); 147*4f1223e8SApple OSS Distributions} 148*4f1223e8SApple OSS Distributions 149*4f1223e8SApple OSS Distributionsstruct scenario { 150*4f1223e8SApple OSS Distributions const char *name; 151*4f1223e8SApple OSS Distributions uint64_t flags; 152*4f1223e8SApple OSS Distributions bool quiet; 153*4f1223e8SApple OSS Distributions bool should_fail; 154*4f1223e8SApple OSS Distributions bool maybe_unsupported; 155*4f1223e8SApple OSS Distributions bool maybe_enomem; 156*4f1223e8SApple OSS Distributions bool no_recordfile; 157*4f1223e8SApple OSS Distributions pid_t target_pid; 158*4f1223e8SApple OSS Distributions bool target_kernel; 159*4f1223e8SApple OSS Distributions uint64_t since_timestamp; 160*4f1223e8SApple OSS Distributions uint32_t size_hint; 161*4f1223e8SApple OSS Distributions dt_stat_time_t timer; 162*4f1223e8SApple OSS Distributions}; 163*4f1223e8SApple OSS Distributions 164*4f1223e8SApple OSS Distributionsstatic void 165*4f1223e8SApple OSS Distributionsquiet(struct scenario *scenario) 166*4f1223e8SApple OSS Distributions{ 167*4f1223e8SApple OSS Distributions if (scenario->timer || scenario->quiet) { 168*4f1223e8SApple OSS Distributions T_QUIET; 169*4f1223e8SApple OSS Distributions } 170*4f1223e8SApple OSS Distributions} 171*4f1223e8SApple OSS Distributions 172*4f1223e8SApple OSS Distributionsstatic void 173*4f1223e8SApple OSS Distributionstake_stackshot(struct scenario *scenario, bool compress_ok, void (^cb)(void *buf, size_t size)) 174*4f1223e8SApple OSS Distributions{ 175*4f1223e8SApple OSS Distributionsstart: 176*4f1223e8SApple OSS Distributions initialize_thread(); 177*4f1223e8SApple OSS Distributions 178*4f1223e8SApple OSS Distributions void *config = stackshot_config_create(); 179*4f1223e8SApple OSS Distributions quiet(scenario); 180*4f1223e8SApple OSS Distributions T_ASSERT_NOTNULL(config, "created stackshot config"); 181*4f1223e8SApple OSS Distributions 182*4f1223e8SApple OSS Distributions int ret = stackshot_config_set_flags(config, scenario->flags | global_flags); 183*4f1223e8SApple OSS Distributions quiet(scenario); 184*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set flags %#llx on stackshot config", scenario->flags); 185*4f1223e8SApple OSS Distributions 186*4f1223e8SApple OSS Distributions if (scenario->size_hint > 0) { 187*4f1223e8SApple OSS Distributions ret = stackshot_config_set_size_hint(config, scenario->size_hint); 188*4f1223e8SApple OSS Distributions quiet(scenario); 189*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set size hint %" PRIu32 " on stackshot config", 190*4f1223e8SApple OSS Distributions scenario->size_hint); 191*4f1223e8SApple OSS Distributions } 192*4f1223e8SApple OSS Distributions 193*4f1223e8SApple OSS Distributions if (scenario->target_pid > 0) { 194*4f1223e8SApple OSS Distributions ret = stackshot_config_set_pid(config, scenario->target_pid); 195*4f1223e8SApple OSS Distributions quiet(scenario); 196*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set target pid %d on stackshot config", 197*4f1223e8SApple OSS Distributions scenario->target_pid); 198*4f1223e8SApple OSS Distributions } else if (scenario->target_kernel) { 199*4f1223e8SApple OSS Distributions ret = stackshot_config_set_pid(config, 0); 200*4f1223e8SApple OSS Distributions quiet(scenario); 201*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set kernel target on stackshot config"); 202*4f1223e8SApple OSS Distributions } 203*4f1223e8SApple OSS Distributions 204*4f1223e8SApple OSS Distributions if (scenario->since_timestamp > 0) { 205*4f1223e8SApple OSS Distributions ret = stackshot_config_set_delta_timestamp(config, scenario->since_timestamp); 206*4f1223e8SApple OSS Distributions quiet(scenario); 207*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set since timestamp %" PRIu64 " on stackshot config", 208*4f1223e8SApple OSS Distributions scenario->since_timestamp); 209*4f1223e8SApple OSS Distributions } 210*4f1223e8SApple OSS Distributions 211*4f1223e8SApple OSS Distributions int retries_remaining = 5; 212*4f1223e8SApple OSS Distributions 213*4f1223e8SApple OSS Distributionsretry: ; 214*4f1223e8SApple OSS Distributions uint64_t start_time = mach_absolute_time(); 215*4f1223e8SApple OSS Distributions ret = stackshot_capture_with_config(config); 216*4f1223e8SApple OSS Distributions uint64_t end_time = mach_absolute_time(); 217*4f1223e8SApple OSS Distributions 218*4f1223e8SApple OSS Distributions if (scenario->should_fail) { 219*4f1223e8SApple OSS Distributions T_EXPECTFAIL; 220*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config"); 221*4f1223e8SApple OSS Distributions return; 222*4f1223e8SApple OSS Distributions } 223*4f1223e8SApple OSS Distributions 224*4f1223e8SApple OSS Distributions if (ret == EBUSY || ret == ETIMEDOUT) { 225*4f1223e8SApple OSS Distributions if (retries_remaining > 0) { 226*4f1223e8SApple OSS Distributions if (!scenario->timer) { 227*4f1223e8SApple OSS Distributions T_LOG("stackshot_capture_with_config failed with %s (%d), retrying", 228*4f1223e8SApple OSS Distributions strerror(ret), ret); 229*4f1223e8SApple OSS Distributions } 230*4f1223e8SApple OSS Distributions 231*4f1223e8SApple OSS Distributions retries_remaining--; 232*4f1223e8SApple OSS Distributions goto retry; 233*4f1223e8SApple OSS Distributions } else { 234*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, 235*4f1223e8SApple OSS Distributions "called stackshot_capture_with_config (no retries remaining)"); 236*4f1223e8SApple OSS Distributions } 237*4f1223e8SApple OSS Distributions } else if ((ret == ENOTSUP) && scenario->maybe_unsupported) { 238*4f1223e8SApple OSS Distributions T_SKIP("kernel indicated this stackshot configuration is not supported"); 239*4f1223e8SApple OSS Distributions } else if ((ret == ENOMEM) && scenario->maybe_enomem) { 240*4f1223e8SApple OSS Distributions T_SKIP("insufficient available memory to run test"); 241*4f1223e8SApple OSS Distributions } else { 242*4f1223e8SApple OSS Distributions quiet(scenario); 243*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config"); 244*4f1223e8SApple OSS Distributions } 245*4f1223e8SApple OSS Distributions 246*4f1223e8SApple OSS Distributions if (scenario->timer) { 247*4f1223e8SApple OSS Distributions dt_stat_mach_time_add(scenario->timer, end_time - start_time); 248*4f1223e8SApple OSS Distributions } 249*4f1223e8SApple OSS Distributions void *buf = stackshot_config_get_stackshot_buffer(config); 250*4f1223e8SApple OSS Distributions size_t size = stackshot_config_get_stackshot_size(config); 251*4f1223e8SApple OSS Distributions if (scenario->name && !scenario->no_recordfile) { 252*4f1223e8SApple OSS Distributions char sspath[MAXPATHLEN]; 253*4f1223e8SApple OSS Distributions strlcpy(sspath, scenario->name, sizeof(sspath)); 254*4f1223e8SApple OSS Distributions strlcat(sspath, ".kcdata", sizeof(sspath)); 255*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(dt_resultfile(sspath, sizeof(sspath)), 256*4f1223e8SApple OSS Distributions "create result file path"); 257*4f1223e8SApple OSS Distributions 258*4f1223e8SApple OSS Distributions if (!scenario->quiet) { 259*4f1223e8SApple OSS Distributions T_LOG("writing stackshot to %s", sspath); 260*4f1223e8SApple OSS Distributions } 261*4f1223e8SApple OSS Distributions 262*4f1223e8SApple OSS Distributions FILE *f = fopen(sspath, "w"); 263*4f1223e8SApple OSS Distributions T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(f, 264*4f1223e8SApple OSS Distributions "open stackshot output file"); 265*4f1223e8SApple OSS Distributions 266*4f1223e8SApple OSS Distributions size_t written = fwrite(buf, size, 1, f); 267*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(written, "wrote stackshot to file"); 268*4f1223e8SApple OSS Distributions 269*4f1223e8SApple OSS Distributions fclose(f); 270*4f1223e8SApple OSS Distributions } 271*4f1223e8SApple OSS Distributions cb(buf, size); 272*4f1223e8SApple OSS Distributions if (compress_ok) { 273*4f1223e8SApple OSS Distributions if (global_flags == 0) { 274*4f1223e8SApple OSS Distributions T_LOG("Restarting test with compression"); 275*4f1223e8SApple OSS Distributions global_flags |= STACKSHOT_DO_COMPRESS; 276*4f1223e8SApple OSS Distributions goto start; 277*4f1223e8SApple OSS Distributions } else { 278*4f1223e8SApple OSS Distributions global_flags = 0; 279*4f1223e8SApple OSS Distributions } 280*4f1223e8SApple OSS Distributions } 281*4f1223e8SApple OSS Distributions 282*4f1223e8SApple OSS Distributions ret = stackshot_config_dealloc(config); 283*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config"); 284*4f1223e8SApple OSS Distributions} 285*4f1223e8SApple OSS Distributions 286*4f1223e8SApple OSS DistributionsT_DECL(simple_compressed, "take a simple compressed stackshot", T_META_TAG_VM_PREFERRED) 287*4f1223e8SApple OSS Distributions{ 288*4f1223e8SApple OSS Distributions struct scenario scenario = { 289*4f1223e8SApple OSS Distributions .name = "kcdata_compressed", 290*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_DO_COMPRESS | STACKSHOT_SAVE_LOADINFO | STACKSHOT_THREAD_WAITINFO | STACKSHOT_GET_GLOBAL_MEM_STATS | 291*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 292*4f1223e8SApple OSS Distributions }; 293*4f1223e8SApple OSS Distributions 294*4f1223e8SApple OSS Distributions T_LOG("taking compressed kcdata stackshot"); 295*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 296*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 297*4f1223e8SApple OSS Distributions }); 298*4f1223e8SApple OSS Distributions} 299*4f1223e8SApple OSS Distributions 300*4f1223e8SApple OSS DistributionsT_DECL(panic_compressed, "take a compressed stackshot with the same flags as a panic stackshot", T_META_TAG_VM_PREFERRED) 301*4f1223e8SApple OSS Distributions{ 302*4f1223e8SApple OSS Distributions uint64_t stackshot_flags = (STACKSHOT_SAVE_KEXT_LOADINFO | 303*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_LOADINFO | 304*4f1223e8SApple OSS Distributions STACKSHOT_KCDATA_FORMAT | 305*4f1223e8SApple OSS Distributions STACKSHOT_ENABLE_BT_FAULTING | 306*4f1223e8SApple OSS Distributions STACKSHOT_ENABLE_UUID_FAULTING | 307*4f1223e8SApple OSS Distributions STACKSHOT_DO_COMPRESS | 308*4f1223e8SApple OSS Distributions STACKSHOT_NO_IO_STATS | 309*4f1223e8SApple OSS Distributions STACKSHOT_THREAD_WAITINFO | 310*4f1223e8SApple OSS Distributions#if TARGET_OS_MAC 311*4f1223e8SApple OSS Distributions STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT | 312*4f1223e8SApple OSS Distributions#endif 313*4f1223e8SApple OSS Distributions STACKSHOT_DISABLE_LATENCY_INFO); 314*4f1223e8SApple OSS Distributions 315*4f1223e8SApple OSS Distributions struct scenario scenario = { 316*4f1223e8SApple OSS Distributions .name = "kcdata_panic_compressed", 317*4f1223e8SApple OSS Distributions .flags = stackshot_flags, 318*4f1223e8SApple OSS Distributions }; 319*4f1223e8SApple OSS Distributions 320*4f1223e8SApple OSS Distributions T_LOG("taking compressed kcdata stackshot with panic flags"); 321*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 322*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 323*4f1223e8SApple OSS Distributions }); 324*4f1223e8SApple OSS Distributions} 325*4f1223e8SApple OSS Distributions 326*4f1223e8SApple OSS DistributionsT_DECL(kcdata, "test that kcdata stackshots can be taken and parsed", T_META_TAG_VM_PREFERRED) 327*4f1223e8SApple OSS Distributions{ 328*4f1223e8SApple OSS Distributions struct scenario scenario = { 329*4f1223e8SApple OSS Distributions .name = "kcdata", 330*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS | 331*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 332*4f1223e8SApple OSS Distributions }; 333*4f1223e8SApple OSS Distributions 334*4f1223e8SApple OSS Distributions T_LOG("taking kcdata stackshot"); 335*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 336*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 337*4f1223e8SApple OSS Distributions }); 338*4f1223e8SApple OSS Distributions} 339*4f1223e8SApple OSS Distributions 340*4f1223e8SApple OSS Distributionsstatic void 341*4f1223e8SApple OSS Distributionsget_stats(stackshot_stats_t *_Nonnull out) 342*4f1223e8SApple OSS Distributions{ 343*4f1223e8SApple OSS Distributions size_t oldlen = sizeof (*out); 344*4f1223e8SApple OSS Distributions bzero(out, oldlen); 345*4f1223e8SApple OSS Distributions int result = sysctlbyname("kern.stackshot_stats", out, &oldlen, NULL, 0); 346*4f1223e8SApple OSS Distributions T_WITH_ERRNO; T_ASSERT_POSIX_SUCCESS(result, "reading \"kern.stackshot_stats\" sysctl should succeed"); 347*4f1223e8SApple OSS Distributions T_EXPECT_EQ(oldlen, sizeof (*out), "kernel should update full stats structure"); 348*4f1223e8SApple OSS Distributions} 349*4f1223e8SApple OSS Distributions 350*4f1223e8SApple OSS Distributionsstatic void 351*4f1223e8SApple OSS Distributionslog_stats(mach_timebase_info_data_t timebase, uint64_t now, const char *name, stackshot_stats_t stat) 352*4f1223e8SApple OSS Distributions{ 353*4f1223e8SApple OSS Distributions uint64_t last_ago = (now - stat.ss_last_start) * timebase.numer / timebase.denom; 354*4f1223e8SApple OSS Distributions uint64_t last_duration = (stat.ss_last_end - stat.ss_last_start) * timebase.numer / timebase.denom; 355*4f1223e8SApple OSS Distributions uint64_t total_duration = (stat.ss_duration) * timebase.numer / timebase.denom; 356*4f1223e8SApple OSS Distributions 357*4f1223e8SApple OSS Distributions uint64_t nanosec = 1000000000llu; 358*4f1223e8SApple OSS Distributions T_LOG("%s: %8lld stackshots, %10lld.%09lld total nsecs, last %lld.%09lld secs ago, %lld.%09lld secs long", 359*4f1223e8SApple OSS Distributions name, stat.ss_count, 360*4f1223e8SApple OSS Distributions total_duration / nanosec, total_duration % nanosec, 361*4f1223e8SApple OSS Distributions last_ago / nanosec, last_ago % nanosec, 362*4f1223e8SApple OSS Distributions last_duration / nanosec, last_duration % nanosec); 363*4f1223e8SApple OSS Distributions} 364*4f1223e8SApple OSS Distributions 365*4f1223e8SApple OSS DistributionsT_DECL(stats, "test that stackshot stats can be read out and change when a stackshot occurs", T_META_TAG_VM_PREFERRED) 366*4f1223e8SApple OSS Distributions{ 367*4f1223e8SApple OSS Distributions mach_timebase_info_data_t timebase = {0, 0}; 368*4f1223e8SApple OSS Distributions mach_timebase_info(&timebase); 369*4f1223e8SApple OSS Distributions 370*4f1223e8SApple OSS Distributions struct scenario scenario = { 371*4f1223e8SApple OSS Distributions .name = "kcdata", 372*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT), 373*4f1223e8SApple OSS Distributions }; 374*4f1223e8SApple OSS Distributions 375*4f1223e8SApple OSS Distributions stackshot_stats_t pre, post; 376*4f1223e8SApple OSS Distributions 377*4f1223e8SApple OSS Distributions get_stats(&pre); 378*4f1223e8SApple OSS Distributions 379*4f1223e8SApple OSS Distributions T_LOG("taking kcdata stackshot"); 380*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(__unused void *ssbuf, __unused size_t sslen) { 381*4f1223e8SApple OSS Distributions (void)0; 382*4f1223e8SApple OSS Distributions }); 383*4f1223e8SApple OSS Distributions 384*4f1223e8SApple OSS Distributions get_stats(&post); 385*4f1223e8SApple OSS Distributions 386*4f1223e8SApple OSS Distributions uint64_t now = mach_absolute_time(); 387*4f1223e8SApple OSS Distributions 388*4f1223e8SApple OSS Distributions log_stats(timebase, now, " pre", pre); 389*4f1223e8SApple OSS Distributions log_stats(timebase, now, " post", post); 390*4f1223e8SApple OSS Distributions 391*4f1223e8SApple OSS Distributions int64_t delta_stackshots = (int64_t)(post.ss_count - pre.ss_count); 392*4f1223e8SApple OSS Distributions int64_t delta_duration = (int64_t)(post.ss_duration - pre.ss_duration) * (int64_t)timebase.numer / (int64_t)timebase.denom; 393*4f1223e8SApple OSS Distributions int64_t delta_nsec = delta_duration % 1000000000ll; 394*4f1223e8SApple OSS Distributions if (delta_nsec < 0) { 395*4f1223e8SApple OSS Distributions delta_nsec += 1000000000ll; 396*4f1223e8SApple OSS Distributions } 397*4f1223e8SApple OSS Distributions T_LOG("delta: %+8lld stackshots, %+10lld.%09lld total nsecs", delta_stackshots, delta_duration / 1000000000ll, delta_nsec); 398*4f1223e8SApple OSS Distributions 399*4f1223e8SApple OSS Distributions T_EXPECT_LT(pre.ss_last_start, pre.ss_last_end, "pre: stackshot should take time"); 400*4f1223e8SApple OSS Distributions T_EXPECT_LT(pre.ss_count, post.ss_count, "stackshot count should increase when a stackshot is taken"); 401*4f1223e8SApple OSS Distributions T_EXPECT_LT(pre.ss_duration, post.ss_duration, "stackshot duration should increase when a stackshot is taken"); 402*4f1223e8SApple OSS Distributions T_EXPECT_LT(pre.ss_last_end, post.ss_last_start, "previous end should be less than new start after a stackshot"); 403*4f1223e8SApple OSS Distributions T_EXPECT_LT(post.ss_last_start, post.ss_last_end, "post: stackshot should take time"); 404*4f1223e8SApple OSS Distributions} 405*4f1223e8SApple OSS Distributions 406*4f1223e8SApple OSS DistributionsT_DECL(kcdata_faulting, "test that kcdata stackshots while faulting can be taken and parsed", T_META_TAG_VM_PREFERRED) 407*4f1223e8SApple OSS Distributions{ 408*4f1223e8SApple OSS Distributions struct scenario scenario = { 409*4f1223e8SApple OSS Distributions .name = "faulting", 410*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 411*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT 412*4f1223e8SApple OSS Distributions | STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING), 413*4f1223e8SApple OSS Distributions }; 414*4f1223e8SApple OSS Distributions 415*4f1223e8SApple OSS Distributions T_LOG("taking faulting stackshot"); 416*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 417*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 418*4f1223e8SApple OSS Distributions }); 419*4f1223e8SApple OSS Distributions} 420*4f1223e8SApple OSS Distributions 421*4f1223e8SApple OSS DistributionsT_DECL(bad_flags, "test a poorly-formed stackshot syscall", T_META_TAG_VM_PREFERRED) 422*4f1223e8SApple OSS Distributions{ 423*4f1223e8SApple OSS Distributions struct scenario scenario = { 424*4f1223e8SApple OSS Distributions .flags = STACKSHOT_SAVE_IN_KERNEL_BUFFER /* not allowed from user space */, 425*4f1223e8SApple OSS Distributions .should_fail = true, 426*4f1223e8SApple OSS Distributions }; 427*4f1223e8SApple OSS Distributions 428*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with kernel-only flag"); 429*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(__unused void *ssbuf, __unused size_t sslen) { 430*4f1223e8SApple OSS Distributions T_ASSERT_FAIL("stackshot data callback called"); 431*4f1223e8SApple OSS Distributions }); 432*4f1223e8SApple OSS Distributions} 433*4f1223e8SApple OSS Distributions 434*4f1223e8SApple OSS DistributionsT_DECL(delta, "test delta stackshots", T_META_TAG_VM_PREFERRED) 435*4f1223e8SApple OSS Distributions{ 436*4f1223e8SApple OSS Distributions struct scenario scenario = { 437*4f1223e8SApple OSS Distributions .name = "delta", 438*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 439*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 440*4f1223e8SApple OSS Distributions }; 441*4f1223e8SApple OSS Distributions 442*4f1223e8SApple OSS Distributions T_LOG("taking full stackshot"); 443*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 444*4f1223e8SApple OSS Distributions uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen); 445*4f1223e8SApple OSS Distributions 446*4f1223e8SApple OSS Distributions T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time); 447*4f1223e8SApple OSS Distributions 448*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 449*4f1223e8SApple OSS Distributions 450*4f1223e8SApple OSS Distributions struct scenario delta_scenario = { 451*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 452*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT 453*4f1223e8SApple OSS Distributions | STACKSHOT_COLLECT_DELTA_SNAPSHOT), 454*4f1223e8SApple OSS Distributions .since_timestamp = stackshot_time 455*4f1223e8SApple OSS Distributions }; 456*4f1223e8SApple OSS Distributions 457*4f1223e8SApple OSS Distributions take_stackshot(&delta_scenario, false, ^(void *dssbuf, size_t dsslen) { 458*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DELTA, dssbuf, dsslen, nil); 459*4f1223e8SApple OSS Distributions }); 460*4f1223e8SApple OSS Distributions }); 461*4f1223e8SApple OSS Distributions} 462*4f1223e8SApple OSS Distributions 463*4f1223e8SApple OSS DistributionsT_DECL(shared_cache_layout, "test stackshot inclusion of shared cache layout", T_META_TAG_VM_PREFERRED) 464*4f1223e8SApple OSS Distributions{ 465*4f1223e8SApple OSS Distributions struct scenario scenario = { 466*4f1223e8SApple OSS Distributions .name = "shared_cache_layout", 467*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 468*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT | 469*4f1223e8SApple OSS Distributions STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT), 470*4f1223e8SApple OSS Distributions }; 471*4f1223e8SApple OSS Distributions 472*4f1223e8SApple OSS Distributions size_t shared_cache_length; 473*4f1223e8SApple OSS Distributions const void *cache_header = _dyld_get_shared_cache_range(&shared_cache_length); 474*4f1223e8SApple OSS Distributions if (cache_header == NULL) { 475*4f1223e8SApple OSS Distributions T_SKIP("Device not running with shared cache, skipping test..."); 476*4f1223e8SApple OSS Distributions } 477*4f1223e8SApple OSS Distributions 478*4f1223e8SApple OSS Distributions if (shared_cache_length == 0) { 479*4f1223e8SApple OSS Distributions T_SKIP("dyld reports that currently running shared cache has zero length"); 480*4f1223e8SApple OSS Distributions } 481*4f1223e8SApple OSS Distributions 482*4f1223e8SApple OSS Distributions T_LOG("taking stackshot with STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT set"); 483*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 484*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_SHAREDCACHE_LAYOUT, ssbuf, sslen, nil); 485*4f1223e8SApple OSS Distributions }); 486*4f1223e8SApple OSS Distributions} 487*4f1223e8SApple OSS Distributions 488*4f1223e8SApple OSS DistributionsT_DECL(stress, "test that taking stackshots for 60 seconds doesn't crash the system", T_META_TAG_VM_PREFERRED) 489*4f1223e8SApple OSS Distributions{ 490*4f1223e8SApple OSS Distributions uint64_t max_diff_time = 60ULL /* seconds */ * 1000000000ULL; 491*4f1223e8SApple OSS Distributions uint64_t start_time; 492*4f1223e8SApple OSS Distributions 493*4f1223e8SApple OSS Distributions struct scenario scenario = { 494*4f1223e8SApple OSS Distributions .name = "stress", 495*4f1223e8SApple OSS Distributions .quiet = true, 496*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT | 497*4f1223e8SApple OSS Distributions STACKSHOT_THREAD_WAITINFO | 498*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_LOADINFO | 499*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_KEXT_LOADINFO | 500*4f1223e8SApple OSS Distributions STACKSHOT_GET_GLOBAL_MEM_STATS | 501*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | 502*4f1223e8SApple OSS Distributions STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT | 503*4f1223e8SApple OSS Distributions STACKSHOT_THREAD_GROUP | 504*4f1223e8SApple OSS Distributions STACKSHOT_SAVE_JETSAM_COALITIONS | 505*4f1223e8SApple OSS Distributions STACKSHOT_ASID | 506*4f1223e8SApple OSS Distributions STACKSHOT_EXCLAVES | 507*4f1223e8SApple OSS Distributions 0), 508*4f1223e8SApple OSS Distributions }; 509*4f1223e8SApple OSS Distributions 510*4f1223e8SApple OSS Distributions start_time = clock_gettime_nsec_np(CLOCK_MONOTONIC); 511*4f1223e8SApple OSS Distributions while (clock_gettime_nsec_np(CLOCK_MONOTONIC) - start_time < max_diff_time) { 512*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void * __unused ssbuf, 513*4f1223e8SApple OSS Distributions size_t __unused sslen) { 514*4f1223e8SApple OSS Distributions printf("."); 515*4f1223e8SApple OSS Distributions fflush(stdout); 516*4f1223e8SApple OSS Distributions }); 517*4f1223e8SApple OSS Distributions 518*4f1223e8SApple OSS Distributions /* 519*4f1223e8SApple OSS Distributions * After the first stackshot, there's no point in continuing to 520*4f1223e8SApple OSS Distributions * write them to disk, and it wears down the SSDs. 521*4f1223e8SApple OSS Distributions */ 522*4f1223e8SApple OSS Distributions scenario.no_recordfile = true; 523*4f1223e8SApple OSS Distributions 524*4f1223e8SApple OSS Distributions /* Leave some time for the testing infrastructure to catch up */ 525*4f1223e8SApple OSS Distributions usleep(10000); 526*4f1223e8SApple OSS Distributions 527*4f1223e8SApple OSS Distributions } 528*4f1223e8SApple OSS Distributions printf("\n"); 529*4f1223e8SApple OSS Distributions} 530*4f1223e8SApple OSS Distributions 531*4f1223e8SApple OSS DistributionsT_DECL(dispatch_queue_label, "test that kcdata stackshots contain libdispatch queue labels", T_META_TAG_VM_PREFERRED) 532*4f1223e8SApple OSS Distributions{ 533*4f1223e8SApple OSS Distributions struct scenario scenario = { 534*4f1223e8SApple OSS Distributions .name = "kcdata", 535*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_GET_DQ | STACKSHOT_KCDATA_FORMAT), 536*4f1223e8SApple OSS Distributions }; 537*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem, parent_done_sem; 538*4f1223e8SApple OSS Distributions dispatch_queue_t dq; 539*4f1223e8SApple OSS Distributions 540*4f1223e8SApple OSS Distributions#if TARGET_OS_WATCH 541*4f1223e8SApple OSS Distributions T_SKIP("This test is flaky on watches: 51663346"); 542*4f1223e8SApple OSS Distributions#endif 543*4f1223e8SApple OSS Distributions 544*4f1223e8SApple OSS Distributions child_ready_sem = dispatch_semaphore_create(0); 545*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "dqlabel child semaphore"); 546*4f1223e8SApple OSS Distributions 547*4f1223e8SApple OSS Distributions parent_done_sem = dispatch_semaphore_create(0); 548*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(parent_done_sem, "dqlabel parent semaphore"); 549*4f1223e8SApple OSS Distributions 550*4f1223e8SApple OSS Distributions dq = dispatch_queue_create(TEST_STACKSHOT_QUEUE_LABEL, NULL); 551*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(dq, "dispatch queue"); 552*4f1223e8SApple OSS Distributions 553*4f1223e8SApple OSS Distributions /* start the helper thread */ 554*4f1223e8SApple OSS Distributions dispatch_async(dq, ^{ 555*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(child_ready_sem); 556*4f1223e8SApple OSS Distributions 557*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(parent_done_sem, DISPATCH_TIME_FOREVER); 558*4f1223e8SApple OSS Distributions }); 559*4f1223e8SApple OSS Distributions 560*4f1223e8SApple OSS Distributions /* block behind the child starting up */ 561*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 562*4f1223e8SApple OSS Distributions 563*4f1223e8SApple OSS Distributions T_LOG("taking kcdata stackshot with libdispatch queue labels"); 564*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^(void *ssbuf, size_t sslen) { 565*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DISPATCH_QUEUE_LABEL, ssbuf, sslen, nil); 566*4f1223e8SApple OSS Distributions }); 567*4f1223e8SApple OSS Distributions 568*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(parent_done_sem); 569*4f1223e8SApple OSS Distributions} 570*4f1223e8SApple OSS Distributions 571*4f1223e8SApple OSS Distributions#define CACHEADDR_ENV "STACKSHOT_TEST_DYLDADDR" 572*4f1223e8SApple OSS DistributionsT_HELPER_DECL(spawn_reslide_child, "child process to spawn with alternate slide") 573*4f1223e8SApple OSS Distributions{ 574*4f1223e8SApple OSS Distributions size_t shared_cache_len; 575*4f1223e8SApple OSS Distributions const void *addr, *prevaddr; 576*4f1223e8SApple OSS Distributions uintmax_t v; 577*4f1223e8SApple OSS Distributions char *endptr; 578*4f1223e8SApple OSS Distributions 579*4f1223e8SApple OSS Distributions const char *cacheaddr_env = getenv(CACHEADDR_ENV); 580*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cacheaddr_env, "getenv("CACHEADDR_ENV")"); 581*4f1223e8SApple OSS Distributions errno = 0; 582*4f1223e8SApple OSS Distributions endptr = NULL; 583*4f1223e8SApple OSS Distributions v = strtoumax(cacheaddr_env, &endptr, 16); /* read hex value */ 584*4f1223e8SApple 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); 585*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(*endptr, 0, "getenv(%s) = \"%s\" endptr \"%s\" should be empty", CACHEADDR_ENV, cacheaddr_env, endptr); 586*4f1223e8SApple OSS Distributions 587*4f1223e8SApple OSS Distributions prevaddr = (const void *)v; 588*4f1223e8SApple OSS Distributions addr = _dyld_get_shared_cache_range(&shared_cache_len); 589*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(addr, "shared cache address"); 590*4f1223e8SApple OSS Distributions 591*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(getppid(), (addr == prevaddr) ? SIGUSR2 : SIGUSR1), "signaled parent to take stackshot"); 592*4f1223e8SApple OSS Distributions for (;;) { 593*4f1223e8SApple OSS Distributions (void) pause(); /* parent will kill -9 us */ 594*4f1223e8SApple OSS Distributions } 595*4f1223e8SApple OSS Distributions} 596*4f1223e8SApple OSS Distributions 597*4f1223e8SApple OSS DistributionsT_DECL(shared_cache_flags, "tests stackshot's task_ss_flags for the shared cache", T_META_TAG_VM_PREFERRED) 598*4f1223e8SApple OSS Distributions{ 599*4f1223e8SApple OSS Distributions posix_spawnattr_t attr; 600*4f1223e8SApple OSS Distributions char *env_addr; 601*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 602*4f1223e8SApple OSS Distributions __block bool child_same_addr = false; 603*4f1223e8SApple OSS Distributions 604*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 605*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 606*4f1223e8SApple OSS Distributions char *args[] = { path, "-n", "spawn_reslide_child", NULL }; 607*4f1223e8SApple OSS Distributions pid_t pid; 608*4f1223e8SApple OSS Distributions size_t shared_cache_len; 609*4f1223e8SApple OSS Distributions const void *addr; 610*4f1223e8SApple OSS Distributions 611*4f1223e8SApple OSS Distributions dispatch_source_t child_diffsig_src, child_samesig_src; 612*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 613*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "shared_cache child semaphore"); 614*4f1223e8SApple OSS Distributions 615*4f1223e8SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 616*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 617*4f1223e8SApple OSS Distributions 618*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 619*4f1223e8SApple OSS Distributions signal(SIGUSR2, SIG_IGN); 620*4f1223e8SApple OSS Distributions child_samesig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 621*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_samesig_src, "dispatch_source_create (child_samesig_src)"); 622*4f1223e8SApple OSS Distributions child_diffsig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR2, 0, signal_processing_q); 623*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_diffsig_src, "dispatch_source_create (child_diffsig_src)"); 624*4f1223e8SApple OSS Distributions 625*4f1223e8SApple OSS Distributions /* child will signal us depending on if their addr is the same or different */ 626*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(child_samesig_src, ^{ child_same_addr = false; dispatch_semaphore_signal(child_ready_sem); }); 627*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(child_diffsig_src, ^{ child_same_addr = true; dispatch_semaphore_signal(child_ready_sem); }); 628*4f1223e8SApple OSS Distributions dispatch_activate(child_samesig_src); 629*4f1223e8SApple OSS Distributions dispatch_activate(child_diffsig_src); 630*4f1223e8SApple OSS Distributions 631*4f1223e8SApple OSS Distributions addr = _dyld_get_shared_cache_range(&shared_cache_len); 632*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(addr, "shared cache address"); 633*4f1223e8SApple OSS Distributions 634*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(asprintf(&env_addr, "%p", addr), "asprintf of env_addr succeeded"); 635*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(setenv(CACHEADDR_ENV, env_addr, true), "setting "CACHEADDR_ENV" to %s", env_addr); 636*4f1223e8SApple OSS Distributions 637*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(posix_spawnattr_init(&attr), "posix_spawnattr_init"); 638*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(posix_spawnattr_setflags(&attr, _POSIX_SPAWN_RESLIDE), "posix_spawnattr_setflags"); 639*4f1223e8SApple OSS Distributions int sp_ret = posix_spawn(&pid, path, NULL, &attr, args, environ); 640*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 641*4f1223e8SApple OSS Distributions 642*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 643*4f1223e8SApple OSS Distributions T_LOG("received signal from child (%s), capturing stackshot", child_same_addr ? "same shared cache addr" : "different shared cache addr"); 644*4f1223e8SApple OSS Distributions 645*4f1223e8SApple OSS Distributions struct scenario scenario = { 646*4f1223e8SApple OSS Distributions .name = "shared_cache_flags", 647*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 648*4f1223e8SApple OSS Distributions | STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT 649*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 650*4f1223e8SApple OSS Distributions }; 651*4f1223e8SApple OSS Distributions 652*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 653*4f1223e8SApple OSS Distributions int status; 654*4f1223e8SApple OSS Distributions /* First kill the child so we can reap it */ 655*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "killing spawned process"); 656*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on spawned child"); 657*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(!!WIFSIGNALED(status), 1, "waitpid status should be signalled"); 658*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "waitpid status should be SIGKILLed"); 659*4f1223e8SApple OSS Distributions 660*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_SHAREDCACHE_FLAGS, ssbuf, sslen, 661*4f1223e8SApple OSS Distributions @{sharedcache_child_pid_key: @(pid), sharedcache_child_sameaddr_key: @(child_same_addr ? 1 : 0)}); 662*4f1223e8SApple OSS Distributions }); 663*4f1223e8SApple OSS Distributions} 664*4f1223e8SApple OSS Distributions 665*4f1223e8SApple OSS DistributionsT_DECL(transitioning_tasks, "test that stackshot contains transitioning task info", T_META_BOOTARGS_SET("enable_proc_exit_lpexit_spin=1"), T_META_TAG_VM_PREFERRED) 666*4f1223e8SApple OSS Distributions{ 667*4f1223e8SApple OSS Distributions int32_t sysctlValue = -1, numAttempts =0; 668*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 669*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 670*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 671*4f1223e8SApple OSS Distributions char *args[] = { path, "-n", "exec_child_preexec", NULL }; 672*4f1223e8SApple OSS Distributions 673*4f1223e8SApple OSS Distributions dispatch_source_t child_sig_src; 674*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 675*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "exec child semaphore"); 676*4f1223e8SApple OSS Distributions 677*4f1223e8SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 678*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 679*4f1223e8SApple OSS Distributions 680*4f1223e8SApple OSS Distributions pid_t pid; 681*4f1223e8SApple OSS Distributions 682*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 683*4f1223e8SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 684*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 685*4f1223e8SApple OSS Distributions 686*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 687*4f1223e8SApple OSS Distributions dispatch_activate(child_sig_src); 688*4f1223e8SApple OSS Distributions 689*4f1223e8SApple 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"); 690*4f1223e8SApple OSS Distributions 691*4f1223e8SApple OSS Distributions int proc_exit_spin_pos = 0 ; 692*4f1223e8SApple OSS Distributions 693*4f1223e8SApple OSS Distributions while (0 == sysctlbyname("debug.proc_exit_lpexit_spin_pos", NULL, NULL, &proc_exit_spin_pos, sizeof(proc_exit_spin_pos))) { 694*4f1223e8SApple OSS Distributions 695*4f1223e8SApple OSS Distributions T_LOG(" ##### Testing while spinning in proc_exit at position %d ##### ", proc_exit_spin_pos); 696*4f1223e8SApple OSS Distributions 697*4f1223e8SApple OSS Distributions int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); 698*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 699*4f1223e8SApple OSS Distributions 700*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 701*4f1223e8SApple OSS Distributions 702*4f1223e8SApple OSS Distributions struct proc_uniqidentifierinfo proc_info_data = { }; 703*4f1223e8SApple OSS Distributions int retval = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &proc_info_data, sizeof(proc_info_data)); 704*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(retval, "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO"); 705*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ_INT(retval, (int) sizeof(proc_info_data), "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO returned data"); 706*4f1223e8SApple OSS Distributions 707*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGUSR1), "signaled pre-exec child to exec"); 708*4f1223e8SApple OSS Distributions 709*4f1223e8SApple OSS Distributions /* wait for a signal from post-exec child */ 710*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 711*4f1223e8SApple OSS Distributions 712*4f1223e8SApple 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); 713*4f1223e8SApple OSS Distributions 714*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "kill post-exec child %d", pid); 715*4f1223e8SApple OSS Distributions 716*4f1223e8SApple OSS Distributions sysctlValue = 0; 717*4f1223e8SApple OSS Distributions size_t len = sizeof(sysctlValue); 718*4f1223e8SApple OSS Distributions while (numAttempts < 5) { 719*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spinning", &sysctlValue, &len, NULL, 0), "retrieve debug.proc_exit_lpexit_spinning"); 720*4f1223e8SApple OSS Distributions if (sysctlValue != 1) numAttempts++; 721*4f1223e8SApple OSS Distributions else break; 722*4f1223e8SApple OSS Distributions sleep(1); 723*4f1223e8SApple OSS Distributions } 724*4f1223e8SApple OSS Distributions 725*4f1223e8SApple OSS Distributions T_ASSERT_EQ_UINT(sysctlValue, 1, "find spinning task in proc_exit()"); 726*4f1223e8SApple OSS Distributions 727*4f1223e8SApple OSS Distributions struct scenario scenario = { 728*4f1223e8SApple OSS Distributions .name = "transitioning_tasks", 729*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT) 730*4f1223e8SApple OSS Distributions }; 731*4f1223e8SApple OSS Distributions 732*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 733*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_TRANSITIONING, ssbuf, sslen, @{transitioning_pid_key: @(pid)}); 734*4f1223e8SApple OSS Distributions 735*4f1223e8SApple OSS Distributions // Kill the child 736*4f1223e8SApple OSS Distributions int sysctlValueB = -1; 737*4f1223e8SApple 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"); 738*4f1223e8SApple OSS Distributions sleep(1); 739*4f1223e8SApple OSS Distributions size_t blen = sizeof(sysctlValueB); 740*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("debug.proc_exit_lpexit_spinning", &sysctlValueB, &blen, NULL, 0), "retrieve debug.proc_exit_lpexit_spinning"); 741*4f1223e8SApple OSS Distributions T_ASSERT_EQ_UINT(sysctlValueB, 0, "make sure nothing is spining in proc_exit()"); 742*4f1223e8SApple OSS Distributions int status; 743*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on post-exec child"); 744*4f1223e8SApple OSS Distributions }); 745*4f1223e8SApple OSS Distributions 746*4f1223e8SApple OSS Distributions proc_exit_spin_pos++; 747*4f1223e8SApple OSS Distributions } 748*4f1223e8SApple OSS Distributions 749*4f1223e8SApple OSS Distributions} 750*4f1223e8SApple OSS Distributions 751*4f1223e8SApple OSS Distributionsstatic void *stuck_sysctl_thread(void *arg) { 752*4f1223e8SApple OSS Distributions int val = 1; 753*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_thread_started = *(dispatch_semaphore_t *)arg; 754*4f1223e8SApple OSS Distributions 755*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(child_thread_started); 756*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.wedge_thread", NULL, NULL, &val, sizeof(val)), "wedge child thread"); 757*4f1223e8SApple OSS Distributions 758*4f1223e8SApple OSS Distributions return NULL; 759*4f1223e8SApple OSS Distributions} 760*4f1223e8SApple OSS Distributions 761*4f1223e8SApple OSS DistributionsT_HELPER_DECL(zombie_child, "child process to sample as a zombie") 762*4f1223e8SApple OSS Distributions{ 763*4f1223e8SApple OSS Distributions pthread_t pthread; 764*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_thread_started = dispatch_semaphore_create(0); 765*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_thread_started, "zombie child thread semaphore"); 766*4f1223e8SApple OSS Distributions 767*4f1223e8SApple OSS Distributions /* spawn another thread to get stuck in the kernel, then call exit() to become a zombie */ 768*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&pthread, NULL, stuck_sysctl_thread, &child_thread_started), "pthread_create"); 769*4f1223e8SApple OSS Distributions 770*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_thread_started, DISPATCH_TIME_FOREVER); 771*4f1223e8SApple OSS Distributions 772*4f1223e8SApple OSS Distributions /* sleep for a bit in the hope of ensuring that the other thread has called the sysctl before we signal the parent */ 773*4f1223e8SApple OSS Distributions usleep(100); 774*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take stackshot"); 775*4f1223e8SApple OSS Distributions 776*4f1223e8SApple OSS Distributions exit(0); 777*4f1223e8SApple OSS Distributions} 778*4f1223e8SApple OSS Distributions 779*4f1223e8SApple OSS DistributionsT_DECL(zombie, "tests a stackshot of a zombie task with a thread stuck in the kernel", 780*4f1223e8SApple OSS Distributions T_META_ENABLED(false), /* test is too flaky to run by default, but transitioning_tasks covers this case as well */ 781*4f1223e8SApple OSS Distributions T_META_TAG_VM_PREFERRED) 782*4f1223e8SApple OSS Distributions{ 783*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 784*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 785*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 786*4f1223e8SApple OSS Distributions char *args[] = { path, "-n", "zombie_child", NULL }; 787*4f1223e8SApple OSS Distributions 788*4f1223e8SApple OSS Distributions dispatch_source_t child_sig_src; 789*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 790*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "zombie child semaphore"); 791*4f1223e8SApple OSS Distributions 792*4f1223e8SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 793*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 794*4f1223e8SApple OSS Distributions 795*4f1223e8SApple OSS Distributions pid_t pid; 796*4f1223e8SApple OSS Distributions 797*4f1223e8SApple OSS Distributions T_LOG("spawning a child"); 798*4f1223e8SApple OSS Distributions 799*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 800*4f1223e8SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 801*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 802*4f1223e8SApple OSS Distributions 803*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 804*4f1223e8SApple OSS Distributions dispatch_activate(child_sig_src); 805*4f1223e8SApple OSS Distributions 806*4f1223e8SApple OSS Distributions int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); 807*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 808*4f1223e8SApple OSS Distributions 809*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 810*4f1223e8SApple OSS Distributions 811*4f1223e8SApple OSS Distributions T_LOG("received signal from child, capturing stackshot"); 812*4f1223e8SApple OSS Distributions 813*4f1223e8SApple OSS Distributions struct proc_bsdshortinfo bsdshortinfo; 814*4f1223e8SApple OSS Distributions int retval, iterations_to_wait = 10; 815*4f1223e8SApple OSS Distributions 816*4f1223e8SApple OSS Distributions while (iterations_to_wait > 0) { 817*4f1223e8SApple OSS Distributions retval = proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 0, &bsdshortinfo, sizeof(bsdshortinfo)); 818*4f1223e8SApple OSS Distributions if ((retval == 0) && errno == ESRCH) { 819*4f1223e8SApple OSS Distributions T_LOG("unable to find child using proc_pidinfo, assuming zombie"); 820*4f1223e8SApple OSS Distributions break; 821*4f1223e8SApple OSS Distributions } 822*4f1223e8SApple OSS Distributions 823*4f1223e8SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_GT(retval, 0, "proc_pidinfo(PROC_PIDT_SHORTBSDINFO) returned a value > 0"); 824*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(retval, (int)sizeof(bsdshortinfo), "proc_pidinfo call for PROC_PIDT_SHORTBSDINFO returned expected size"); 825*4f1223e8SApple OSS Distributions 826*4f1223e8SApple OSS Distributions if (bsdshortinfo.pbsi_flags & PROC_FLAG_INEXIT) { 827*4f1223e8SApple OSS Distributions T_LOG("child proc info marked as in exit"); 828*4f1223e8SApple OSS Distributions break; 829*4f1223e8SApple OSS Distributions } 830*4f1223e8SApple OSS Distributions 831*4f1223e8SApple OSS Distributions iterations_to_wait--; 832*4f1223e8SApple OSS Distributions if (iterations_to_wait == 0) { 833*4f1223e8SApple OSS Distributions /* 834*4f1223e8SApple OSS Distributions * This will mark the test as failed but let it continue so we 835*4f1223e8SApple OSS Distributions * don't leave a process stuck in the kernel. 836*4f1223e8SApple OSS Distributions */ 837*4f1223e8SApple OSS Distributions T_FAIL("unable to discover that child is marked as exiting"); 838*4f1223e8SApple OSS Distributions } 839*4f1223e8SApple OSS Distributions 840*4f1223e8SApple OSS Distributions /* Give the child a few more seconds to make it to exit */ 841*4f1223e8SApple OSS Distributions sleep(5); 842*4f1223e8SApple OSS Distributions } 843*4f1223e8SApple OSS Distributions 844*4f1223e8SApple OSS Distributions /* Give the child some more time to make it through exit */ 845*4f1223e8SApple OSS Distributions sleep(10); 846*4f1223e8SApple OSS Distributions 847*4f1223e8SApple OSS Distributions struct scenario scenario = { 848*4f1223e8SApple OSS Distributions .name = "zombie", 849*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 850*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 851*4f1223e8SApple OSS Distributions }; 852*4f1223e8SApple OSS Distributions 853*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 854*4f1223e8SApple OSS Distributions /* First unwedge the child so we can reap it */ 855*4f1223e8SApple OSS Distributions int val = 1, status; 856*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.unwedge_thread", NULL, NULL, &val, sizeof(val)), "unwedge child"); 857*4f1223e8SApple OSS Distributions 858*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on zombie child"); 859*4f1223e8SApple OSS Distributions 860*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_ZOMBIE, ssbuf, sslen, @{zombie_child_pid_key: @(pid)}); 861*4f1223e8SApple OSS Distributions }); 862*4f1223e8SApple OSS Distributions} 863*4f1223e8SApple OSS Distributions 864*4f1223e8SApple OSS DistributionsT_HELPER_DECL(exec_child_preexec, "child process pre-exec") 865*4f1223e8SApple OSS Distributions{ 866*4f1223e8SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 867*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 868*4f1223e8SApple OSS Distributions 869*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 870*4f1223e8SApple OSS Distributions dispatch_source_t parent_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 871*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(parent_sig_src, "dispatch_source_create (child_sig_src)"); 872*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(parent_sig_src, ^{ 873*4f1223e8SApple OSS Distributions 874*4f1223e8SApple OSS Distributions // Parent took a timestamp then signaled us: exec into the next process 875*4f1223e8SApple OSS Distributions 876*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 877*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 878*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 879*4f1223e8SApple OSS Distributions char *args[] = { path, "-n", "exec_child_postexec", NULL }; 880*4f1223e8SApple OSS Distributions 881*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(execve(args[0], args, NULL), "execing into exec_child_postexec"); 882*4f1223e8SApple OSS Distributions }); 883*4f1223e8SApple OSS Distributions dispatch_activate(parent_sig_src); 884*4f1223e8SApple OSS Distributions 885*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take timestamp"); 886*4f1223e8SApple OSS Distributions 887*4f1223e8SApple OSS Distributions sleep(100); 888*4f1223e8SApple OSS Distributions // Should never get here 889*4f1223e8SApple OSS Distributions T_FAIL("Received signal to exec from parent"); 890*4f1223e8SApple OSS Distributions} 891*4f1223e8SApple OSS Distributions 892*4f1223e8SApple OSS DistributionsT_HELPER_DECL(exec_child_postexec, "child process post-exec to sample") 893*4f1223e8SApple OSS Distributions{ 894*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take stackshot"); 895*4f1223e8SApple OSS Distributions sleep(100); 896*4f1223e8SApple OSS Distributions // Should never get here 897*4f1223e8SApple OSS Distributions T_FAIL("Killed by parent"); 898*4f1223e8SApple OSS Distributions} 899*4f1223e8SApple OSS Distributions 900*4f1223e8SApple OSS DistributionsT_DECL(exec, "test getting full task snapshots for a task that execs", T_META_TAG_VM_PREFERRED) 901*4f1223e8SApple OSS Distributions{ 902*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 903*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 904*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 905*4f1223e8SApple OSS Distributions char *args[] = { path, "-n", "exec_child_preexec", NULL }; 906*4f1223e8SApple OSS Distributions 907*4f1223e8SApple OSS Distributions dispatch_source_t child_sig_src; 908*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 909*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "exec child semaphore"); 910*4f1223e8SApple OSS Distributions 911*4f1223e8SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 912*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 913*4f1223e8SApple OSS Distributions 914*4f1223e8SApple OSS Distributions pid_t pid; 915*4f1223e8SApple OSS Distributions 916*4f1223e8SApple OSS Distributions T_LOG("spawning a child"); 917*4f1223e8SApple OSS Distributions 918*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 919*4f1223e8SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 920*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 921*4f1223e8SApple OSS Distributions 922*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 923*4f1223e8SApple OSS Distributions dispatch_activate(child_sig_src); 924*4f1223e8SApple OSS Distributions 925*4f1223e8SApple OSS Distributions int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); 926*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid); 927*4f1223e8SApple OSS Distributions 928*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 929*4f1223e8SApple OSS Distributions uint64_t start_time = mach_absolute_time(); 930*4f1223e8SApple OSS Distributions 931*4f1223e8SApple OSS Distributions struct proc_uniqidentifierinfo proc_info_data = { }; 932*4f1223e8SApple OSS Distributions int retval = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &proc_info_data, sizeof(proc_info_data)); 933*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(retval, "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO"); 934*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ_INT(retval, (int) sizeof(proc_info_data), "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO returned data"); 935*4f1223e8SApple OSS Distributions uint64_t unique_pid = proc_info_data.p_uniqueid; 936*4f1223e8SApple OSS Distributions 937*4f1223e8SApple OSS Distributions T_LOG("received signal from pre-exec child, unique_pid is %llu, timestamp is %llu", unique_pid, start_time); 938*4f1223e8SApple OSS Distributions 939*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGUSR1), "signaled pre-exec child to exec"); 940*4f1223e8SApple OSS Distributions 941*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 942*4f1223e8SApple OSS Distributions 943*4f1223e8SApple OSS Distributions T_LOG("received signal from post-exec child, capturing stackshot"); 944*4f1223e8SApple OSS Distributions 945*4f1223e8SApple OSS Distributions struct scenario scenario = { 946*4f1223e8SApple OSS Distributions .name = "exec", 947*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 948*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT 949*4f1223e8SApple OSS Distributions | STACKSHOT_COLLECT_DELTA_SNAPSHOT), 950*4f1223e8SApple OSS Distributions .since_timestamp = start_time 951*4f1223e8SApple OSS Distributions }; 952*4f1223e8SApple OSS Distributions 953*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^( void *ssbuf, size_t sslen) { 954*4f1223e8SApple OSS Distributions // Kill the child 955*4f1223e8SApple OSS Distributions int status; 956*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "kill post-exec child %d", pid); 957*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on post-exec child"); 958*4f1223e8SApple OSS Distributions 959*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_POSTEXEC | PARSE_STACKSHOT_DELTA, ssbuf, sslen, @{postexec_child_unique_pid_key: @(unique_pid)}); 960*4f1223e8SApple OSS Distributions }); 961*4f1223e8SApple OSS Distributions} 962*4f1223e8SApple OSS Distributions 963*4f1223e8SApple OSS DistributionsT_DECL( 964*4f1223e8SApple OSS Distributions exec_inprogress, 965*4f1223e8SApple OSS Distributions "test stackshots of processes in the middle of exec", 966*4f1223e8SApple OSS Distributions T_META_ENABLED(false), /* rdar://111691318 */ 967*4f1223e8SApple OSS Distributions T_META_TAG_VM_PREFERRED) 968*4f1223e8SApple OSS Distributions{ 969*4f1223e8SApple OSS Distributions pid_t pid; 970*4f1223e8SApple OSS Distributions /* a BASH quine which execs itself as long as the parent doesn't exit */ 971*4f1223e8SApple OSS Distributions char *bash_prog = "[[ $PPID -ne 1 ]] && exec /bin/bash -c \"$0\" \"$0\""; 972*4f1223e8SApple OSS Distributions char *args[] = { "/bin/bash", "-c", bash_prog, bash_prog, NULL }; 973*4f1223e8SApple OSS Distributions 974*4f1223e8SApple OSS Distributions posix_spawnattr_t sattr; 975*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawnattr_init(&sattr), "posix_spawnattr_init"); 976*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn(&pid, args[0], NULL, &sattr, args, NULL), "spawn exec_inprogress_child"); 977*4f1223e8SApple OSS Distributions 978*4f1223e8SApple OSS Distributions struct scenario scenario = { 979*4f1223e8SApple OSS Distributions .name = "exec_inprogress", 980*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT), 981*4f1223e8SApple OSS Distributions .target_pid = pid, 982*4f1223e8SApple OSS Distributions }; 983*4f1223e8SApple OSS Distributions 984*4f1223e8SApple OSS Distributions int tries = 0; 985*4f1223e8SApple OSS Distributions int tries_limit = 30; 986*4f1223e8SApple OSS Distributions __block bool found = false; 987*4f1223e8SApple OSS Distributions __block uint64_t cid1 = 0, cid2 = 0; 988*4f1223e8SApple OSS Distributions 989*4f1223e8SApple OSS Distributions for (tries = 0; !found && tries < tries_limit; tries++) { 990*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, 991*4f1223e8SApple OSS Distributions ^( void *ssbuf, size_t sslen) { 992*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_EXEC_INPROGRESS | PARSE_STACKSHOT_TARGETPID, 993*4f1223e8SApple OSS Distributions ssbuf, sslen, @{ 994*4f1223e8SApple OSS Distributions exec_inprogress_pid_key: @(pid), 995*4f1223e8SApple OSS Distributions exec_inprogress_found_key: ^(uint64_t id1, uint64_t id2) { found = true; cid1 = id1; cid2 = id2; }}); 996*4f1223e8SApple OSS Distributions }); 997*4f1223e8SApple OSS Distributions } 998*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGKILL), "killing exec loop"); 999*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(found, "able to find our execing process mid-exec in %d tries", tries); 1000*4f1223e8SApple OSS Distributions T_ASSERT_NE(cid1, cid2, "container IDs for in-progress exec are unique"); 1001*4f1223e8SApple OSS Distributions T_PASS("found mid-exec process in %d tries", tries); 1002*4f1223e8SApple OSS Distributions} 1003*4f1223e8SApple OSS Distributions 1004*4f1223e8SApple OSS Distributions#ifdef _LP64 1005*4f1223e8SApple OSS Distributions#if __has_feature(ptrauth_calls) 1006*4f1223e8SApple OSS Distributions#define __ptrauth_swift_async_context_parent \ 1007*4f1223e8SApple OSS Distributions __ptrauth(ptrauth_key_process_independent_data, 1, 0xbda2) 1008*4f1223e8SApple OSS Distributions#define __ptrauth_swift_async_context_resume \ 1009*4f1223e8SApple OSS Distributions __ptrauth(ptrauth_key_function_pointer, 1, 0xd707) 1010*4f1223e8SApple OSS Distributions#else 1011*4f1223e8SApple OSS Distributions#define __ptrauth_swift_async_context_parent 1012*4f1223e8SApple OSS Distributions#define __ptrauth_swift_async_context_resume 1013*4f1223e8SApple OSS Distributions#endif 1014*4f1223e8SApple OSS Distributions// Add 1 to match the symbolication aid added by the stackshot backtracer. 1015*4f1223e8SApple OSS Distributions#define asyncstack_frame(x) ((uintptr_t)(void *)ptrauth_strip((void *)(x), ptrauth_key_function_pointer) + 1) 1016*4f1223e8SApple OSS Distributions 1017*4f1223e8SApple OSS Distributions// This struct fakes the Swift AsyncContext struct which is used by 1018*4f1223e8SApple OSS Distributions// the Swift concurrency runtime. We only care about the first 2 fields. 1019*4f1223e8SApple OSS Distributionsstruct fake_async_context { 1020*4f1223e8SApple OSS Distributions struct fake_async_context* __ptrauth_swift_async_context_parent next; 1021*4f1223e8SApple OSS Distributions void(*__ptrauth_swift_async_context_resume resume_pc)(void); 1022*4f1223e8SApple OSS Distributions}; 1023*4f1223e8SApple OSS Distributions 1024*4f1223e8SApple OSS Distributionsstatic void 1025*4f1223e8SApple OSS Distributionslevel1_func() 1026*4f1223e8SApple OSS Distributions{ 1027*4f1223e8SApple OSS Distributions} 1028*4f1223e8SApple OSS Distributionsstatic void 1029*4f1223e8SApple OSS Distributionslevel2_func() 1030*4f1223e8SApple OSS Distributions{ 1031*4f1223e8SApple OSS Distributions} 1032*4f1223e8SApple OSS Distributions 1033*4f1223e8SApple OSS Distributions// Create a chain of fake async contexts; sync with asyncstack_expected_stack below 1034*4f1223e8SApple OSS Distributionsstatic alignas(16) struct fake_async_context level1 = { 0, level1_func }; 1035*4f1223e8SApple OSS Distributionsstatic alignas(16) struct fake_async_context level2 = { &level1, level2_func }; 1036*4f1223e8SApple OSS Distributions 1037*4f1223e8SApple OSS Distributionsstruct async_test_semaphores { 1038*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem; /* signal parent we're ready */ 1039*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_exit_sem; /* parent tells us to go away */ 1040*4f1223e8SApple OSS Distributions}; 1041*4f1223e8SApple OSS Distributions 1042*4f1223e8SApple OSS Distributions#define ASYNCSTACK_THREAD_NAME "asyncstack_thread" 1043*4f1223e8SApple OSS Distributions 1044*4f1223e8SApple OSS Distributionsstatic void __attribute__((noinline, not_tail_called)) 1045*4f1223e8SApple OSS Distributionsexpect_asyncstack(void *arg) 1046*4f1223e8SApple OSS Distributions{ 1047*4f1223e8SApple OSS Distributions struct async_test_semaphores *async_ts = arg; 1048*4f1223e8SApple OSS Distributions 1049*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_setname_np(ASYNCSTACK_THREAD_NAME), 1050*4f1223e8SApple OSS Distributions "set thread name to %s", ASYNCSTACK_THREAD_NAME); 1051*4f1223e8SApple OSS Distributions 1052*4f1223e8SApple OSS Distributions /* Tell the main thread we're all set up, then wait for permission to exit */ 1053*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(async_ts->child_ready_sem); 1054*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(async_ts->child_exit_sem, DISPATCH_TIME_FOREVER); 1055*4f1223e8SApple OSS Distributions usleep(1); /* make sure we don't tailcall semaphore_wait */ 1056*4f1223e8SApple OSS Distributions} 1057*4f1223e8SApple OSS Distributions 1058*4f1223e8SApple OSS Distributionsstatic void * 1059*4f1223e8SApple OSS Distributionsasyncstack_thread(void *arg) 1060*4f1223e8SApple OSS Distributions{ 1061*4f1223e8SApple OSS Distributions uint64_t *fp = __builtin_frame_address(0); 1062*4f1223e8SApple OSS Distributions // We cannot use a variable of pointer type, because this ABI is valid 1063*4f1223e8SApple OSS Distributions // on arm64_32 where pointers are 32bits, but the context pointer will 1064*4f1223e8SApple OSS Distributions // still be stored in a 64bits slot on the stack. 1065*4f1223e8SApple OSS Distributions#if __has_feature(ptrauth_calls) 1066*4f1223e8SApple OSS Distributions#define __stack_context_auth __ptrauth(ptrauth_key_process_dependent_data, 1, \ 1067*4f1223e8SApple OSS Distributions 0xc31a) 1068*4f1223e8SApple OSS Distributions struct fake_async_context * __stack_context_auth ctx = &level2; 1069*4f1223e8SApple OSS Distributions#else // __has_feature(ptrauth_calls) 1070*4f1223e8SApple OSS Distributions /* struct fake_async_context * */uint64_t ctx = (uintptr_t)&level2; 1071*4f1223e8SApple OSS Distributions#endif // !__has_feature(ptrauth_calls) 1072*4f1223e8SApple OSS Distributions 1073*4f1223e8SApple OSS Distributions // The signature of an async frame on the OS stack is: 1074*4f1223e8SApple OSS Distributions // [ <AsyncContext address>, <Saved FP | (1<<60)>, <return address> ] 1075*4f1223e8SApple OSS Distributions // The Async context must be right before the saved FP on the stack. This 1076*4f1223e8SApple OSS Distributions // should happen naturally in an optimized build as it is the only 1077*4f1223e8SApple OSS Distributions // variable on the stack. 1078*4f1223e8SApple OSS Distributions // This function cannot use T_ASSERT_* becuse it changes the stack 1079*4f1223e8SApple OSS Distributions // layout. 1080*4f1223e8SApple OSS Distributions assert((uintptr_t)fp - (uintptr_t)&ctx == 8); 1081*4f1223e8SApple OSS Distributions 1082*4f1223e8SApple OSS Distributions // Modify the saved FP on the stack to include the async frame marker 1083*4f1223e8SApple OSS Distributions *fp |= (0x1ULL << 60); 1084*4f1223e8SApple OSS Distributions expect_asyncstack(arg); 1085*4f1223e8SApple OSS Distributions return NULL; 1086*4f1223e8SApple OSS Distributions} 1087*4f1223e8SApple OSS Distributions 1088*4f1223e8SApple OSS DistributionsT_DECL(asyncstack, "test swift async stack entries", T_META_TAG_VM_PREFERRED) 1089*4f1223e8SApple OSS Distributions{ 1090*4f1223e8SApple OSS Distributions struct scenario scenario = { 1091*4f1223e8SApple OSS Distributions .name = "asyncstack", 1092*4f1223e8SApple OSS Distributions .flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_SAVE_LOADINFO, 1093*4f1223e8SApple OSS Distributions }; 1094*4f1223e8SApple OSS Distributions struct async_test_semaphores async_ts = { 1095*4f1223e8SApple OSS Distributions .child_ready_sem = dispatch_semaphore_create(0), 1096*4f1223e8SApple OSS Distributions .child_exit_sem = dispatch_semaphore_create(0), 1097*4f1223e8SApple OSS Distributions }; 1098*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(async_ts.child_ready_sem, "child_ready_sem alloc"); 1099*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(async_ts.child_exit_sem, "child_exit_sem alloc"); 1100*4f1223e8SApple OSS Distributions 1101*4f1223e8SApple OSS Distributions pthread_t pthread; 1102*4f1223e8SApple OSS Distributions __block uint64_t threadid = 0; 1103*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&pthread, NULL, asyncstack_thread, &async_ts), "pthread_create"); 1104*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_threadid_np(pthread, &threadid), "pthread_threadid_np"); 1105*4f1223e8SApple OSS Distributions 1106*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(async_ts.child_ready_sem, DISPATCH_TIME_FOREVER); 1107*4f1223e8SApple OSS Distributions 1108*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^( void *ssbuf, size_t sslen) { 1109*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_ASYNCSTACK, ssbuf, sslen, @{ 1110*4f1223e8SApple OSS Distributions asyncstack_expected_threadid_key: @(threadid), 1111*4f1223e8SApple OSS Distributions asyncstack_expected_stack_key: @[ @(asyncstack_frame(level2_func)), @(asyncstack_frame(level1_func)) ], 1112*4f1223e8SApple OSS Distributions }); 1113*4f1223e8SApple OSS Distributions }); 1114*4f1223e8SApple OSS Distributions 1115*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(async_ts.child_exit_sem); 1116*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(pthread_join(pthread, NULL), "wait for thread"); 1117*4f1223e8SApple OSS Distributions 1118*4f1223e8SApple OSS Distributions} 1119*4f1223e8SApple OSS Distributions#endif /* #ifdef _LP64 */ 1120*4f1223e8SApple OSS Distributions 1121*4f1223e8SApple OSS Distributionsstatic uint32_t 1122*4f1223e8SApple OSS Distributionsget_user_promotion_basepri(void) 1123*4f1223e8SApple OSS Distributions{ 1124*4f1223e8SApple OSS Distributions mach_msg_type_number_t count = THREAD_POLICY_STATE_COUNT; 1125*4f1223e8SApple OSS Distributions struct thread_policy_state thread_policy; 1126*4f1223e8SApple OSS Distributions boolean_t get_default = FALSE; 1127*4f1223e8SApple OSS Distributions mach_port_t thread_port = pthread_mach_thread_np(pthread_self()); 1128*4f1223e8SApple OSS Distributions 1129*4f1223e8SApple OSS Distributions kern_return_t kr = thread_policy_get(thread_port, THREAD_POLICY_STATE, 1130*4f1223e8SApple OSS Distributions (thread_policy_t)&thread_policy, &count, &get_default); 1131*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get"); 1132*4f1223e8SApple OSS Distributions return thread_policy.thps_user_promotion_basepri; 1133*4f1223e8SApple OSS Distributions} 1134*4f1223e8SApple OSS Distributions 1135*4f1223e8SApple OSS Distributionsstatic int 1136*4f1223e8SApple OSS Distributionsget_pri(thread_t thread_port) 1137*4f1223e8SApple OSS Distributions{ 1138*4f1223e8SApple OSS Distributions kern_return_t kr; 1139*4f1223e8SApple OSS Distributions 1140*4f1223e8SApple OSS Distributions thread_extended_info_data_t extended_info; 1141*4f1223e8SApple OSS Distributions mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT; 1142*4f1223e8SApple OSS Distributions kr = thread_info(thread_port, THREAD_EXTENDED_INFO, 1143*4f1223e8SApple OSS Distributions (thread_info_t)&extended_info, &count); 1144*4f1223e8SApple OSS Distributions 1145*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info"); 1146*4f1223e8SApple OSS Distributions 1147*4f1223e8SApple OSS Distributions return extended_info.pth_curpri; 1148*4f1223e8SApple OSS Distributions} 1149*4f1223e8SApple OSS Distributions 1150*4f1223e8SApple OSS Distributions 1151*4f1223e8SApple OSS DistributionsT_DECL(turnstile_singlehop, "turnstile single hop test", T_META_TAG_VM_PREFERRED) 1152*4f1223e8SApple OSS Distributions{ 1153*4f1223e8SApple OSS Distributions dispatch_queue_t dq1, dq2; 1154*4f1223e8SApple OSS Distributions dispatch_semaphore_t sema_x; 1155*4f1223e8SApple OSS Distributions dispatch_queue_attr_t dq1_attr, dq2_attr; 1156*4f1223e8SApple OSS Distributions __block qos_class_t main_qos = 0; 1157*4f1223e8SApple OSS Distributions __block int main_relpri = 0, main_relpri2 = 0, main_afterpri = 0; 1158*4f1223e8SApple OSS Distributions struct scenario scenario = { 1159*4f1223e8SApple OSS Distributions .name = "turnstile_singlehop", 1160*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 1161*4f1223e8SApple OSS Distributions }; 1162*4f1223e8SApple OSS Distributions dq1_attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0); 1163*4f1223e8SApple OSS Distributions dq2_attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0); 1164*4f1223e8SApple OSS Distributions pthread_mutex_t lock_a = PTHREAD_MUTEX_INITIALIZER; 1165*4f1223e8SApple OSS Distributions pthread_mutex_t lock_b = PTHREAD_MUTEX_INITIALIZER; 1166*4f1223e8SApple OSS Distributions 1167*4f1223e8SApple OSS Distributions pthread_mutex_t *lockap = &lock_a, *lockbp = &lock_b; 1168*4f1223e8SApple OSS Distributions 1169*4f1223e8SApple OSS Distributions dq1 = dispatch_queue_create("q1", dq1_attr); 1170*4f1223e8SApple OSS Distributions dq2 = dispatch_queue_create("q2", dq2_attr); 1171*4f1223e8SApple OSS Distributions sema_x = dispatch_semaphore_create(0); 1172*4f1223e8SApple OSS Distributions 1173*4f1223e8SApple OSS Distributions pthread_mutex_lock(lockap); 1174*4f1223e8SApple OSS Distributions dispatch_async(dq1, ^{ 1175*4f1223e8SApple OSS Distributions pthread_mutex_lock(lockbp); 1176*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pthread_get_qos_class_np(pthread_self(), &main_qos, &main_relpri), "get qos class"); 1177*4f1223e8SApple OSS Distributions T_LOG("The priority of q1 is %d\n", get_pri(mach_thread_self())); 1178*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(sema_x); 1179*4f1223e8SApple OSS Distributions pthread_mutex_lock(lockap); 1180*4f1223e8SApple OSS Distributions }); 1181*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(sema_x, DISPATCH_TIME_FOREVER); 1182*4f1223e8SApple OSS Distributions 1183*4f1223e8SApple OSS Distributions T_LOG("Async1 completed"); 1184*4f1223e8SApple OSS Distributions 1185*4f1223e8SApple OSS Distributions pthread_set_qos_class_self_np(QOS_CLASS_UTILITY, 0); 1186*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pthread_get_qos_class_np(pthread_self(), &main_qos, &main_relpri), "get qos class"); 1187*4f1223e8SApple OSS Distributions T_LOG("The priority of main is %d\n", get_pri(mach_thread_self())); 1188*4f1223e8SApple OSS Distributions main_relpri = get_pri(mach_thread_self()); 1189*4f1223e8SApple OSS Distributions 1190*4f1223e8SApple OSS Distributions dispatch_async(dq2, ^{ 1191*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pthread_get_qos_class_np(pthread_self(), &main_qos, &main_relpri2), "get qos class"); 1192*4f1223e8SApple OSS Distributions T_LOG("The priority of q2 is %d\n", get_pri(mach_thread_self())); 1193*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(sema_x); 1194*4f1223e8SApple OSS Distributions pthread_mutex_lock(lockbp); 1195*4f1223e8SApple OSS Distributions }); 1196*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(sema_x, DISPATCH_TIME_FOREVER); 1197*4f1223e8SApple OSS Distributions 1198*4f1223e8SApple OSS Distributions T_LOG("Async2 completed"); 1199*4f1223e8SApple OSS Distributions 1200*4f1223e8SApple OSS Distributions while (1) { 1201*4f1223e8SApple OSS Distributions main_afterpri = (int) get_user_promotion_basepri(); 1202*4f1223e8SApple OSS Distributions if (main_relpri != main_afterpri) { 1203*4f1223e8SApple OSS Distributions T_LOG("Success with promotion pri is %d", main_afterpri); 1204*4f1223e8SApple OSS Distributions break; 1205*4f1223e8SApple OSS Distributions } 1206*4f1223e8SApple OSS Distributions 1207*4f1223e8SApple OSS Distributions usleep(100); 1208*4f1223e8SApple OSS Distributions } 1209*4f1223e8SApple OSS Distributions 1210*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^( void *ssbuf, size_t sslen) { 1211*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_TURNSTILEINFO, ssbuf, sslen, nil); 1212*4f1223e8SApple OSS Distributions }); 1213*4f1223e8SApple OSS Distributions} 1214*4f1223e8SApple OSS Distributions 1215*4f1223e8SApple OSS Distributions 1216*4f1223e8SApple OSS Distributionsstatic void 1217*4f1223e8SApple OSS Distributionsexpect_instrs_cycles_in_stackshot(void *ssbuf, size_t sslen) 1218*4f1223e8SApple OSS Distributions{ 1219*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1220*4f1223e8SApple OSS Distributions 1221*4f1223e8SApple OSS Distributions bool in_task = false; 1222*4f1223e8SApple OSS Distributions bool in_thread = false; 1223*4f1223e8SApple OSS Distributions bool saw_instrs_cycles = false; 1224*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 1225*4f1223e8SApple OSS Distributions 1226*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1227*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 1228*4f1223e8SApple OSS Distributions case KCDATA_TYPE_CONTAINER_BEGIN: 1229*4f1223e8SApple OSS Distributions switch (kcdata_iter_container_type(iter)) { 1230*4f1223e8SApple OSS Distributions case STACKSHOT_KCCONTAINER_TASK: 1231*4f1223e8SApple OSS Distributions in_task = true; 1232*4f1223e8SApple OSS Distributions saw_instrs_cycles = false; 1233*4f1223e8SApple OSS Distributions break; 1234*4f1223e8SApple OSS Distributions 1235*4f1223e8SApple OSS Distributions case STACKSHOT_KCCONTAINER_THREAD: 1236*4f1223e8SApple OSS Distributions in_thread = true; 1237*4f1223e8SApple OSS Distributions saw_instrs_cycles = false; 1238*4f1223e8SApple OSS Distributions break; 1239*4f1223e8SApple OSS Distributions 1240*4f1223e8SApple OSS Distributions default: 1241*4f1223e8SApple OSS Distributions break; 1242*4f1223e8SApple OSS Distributions } 1243*4f1223e8SApple OSS Distributions break; 1244*4f1223e8SApple OSS Distributions 1245*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_INSTRS_CYCLES: 1246*4f1223e8SApple OSS Distributions saw_instrs_cycles = true; 1247*4f1223e8SApple OSS Distributions break; 1248*4f1223e8SApple OSS Distributions 1249*4f1223e8SApple OSS Distributions case KCDATA_TYPE_CONTAINER_END: 1250*4f1223e8SApple OSS Distributions if (in_thread) { 1251*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles, 1252*4f1223e8SApple OSS Distributions "saw instructions and cycles in thread"); 1253*4f1223e8SApple OSS Distributions in_thread = false; 1254*4f1223e8SApple OSS Distributions } else if (in_task) { 1255*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles, 1256*4f1223e8SApple OSS Distributions "saw instructions and cycles in task"); 1257*4f1223e8SApple OSS Distributions in_task = false; 1258*4f1223e8SApple OSS Distributions } 1259*4f1223e8SApple OSS Distributions 1260*4f1223e8SApple OSS Distributions default: 1261*4f1223e8SApple OSS Distributions break; 1262*4f1223e8SApple OSS Distributions } 1263*4f1223e8SApple OSS Distributions } 1264*4f1223e8SApple OSS Distributions} 1265*4f1223e8SApple OSS Distributions 1266*4f1223e8SApple OSS Distributionsstatic void 1267*4f1223e8SApple OSS Distributionsskip_if_monotonic_unsupported(void) 1268*4f1223e8SApple OSS Distributions{ 1269*4f1223e8SApple OSS Distributions int supported = 0; 1270*4f1223e8SApple OSS Distributions size_t supported_size = sizeof(supported); 1271*4f1223e8SApple OSS Distributions int ret = sysctlbyname("kern.monotonic.supported", &supported, 1272*4f1223e8SApple OSS Distributions &supported_size, 0, 0); 1273*4f1223e8SApple OSS Distributions if (ret < 0 || !supported) { 1274*4f1223e8SApple OSS Distributions T_SKIP("monotonic is unsupported"); 1275*4f1223e8SApple OSS Distributions } 1276*4f1223e8SApple OSS Distributions} 1277*4f1223e8SApple OSS Distributions 1278*4f1223e8SApple OSS DistributionsT_DECL(instrs_cycles, "test a getting instructions and cycles in stackshot", T_META_TAG_VM_PREFERRED) 1279*4f1223e8SApple OSS Distributions{ 1280*4f1223e8SApple OSS Distributions skip_if_monotonic_unsupported(); 1281*4f1223e8SApple OSS Distributions 1282*4f1223e8SApple OSS Distributions struct scenario scenario = { 1283*4f1223e8SApple OSS Distributions .name = "instrs-cycles", 1284*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES 1285*4f1223e8SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1286*4f1223e8SApple OSS Distributions }; 1287*4f1223e8SApple OSS Distributions 1288*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with instructions and cycles"); 1289*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1290*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 1291*4f1223e8SApple OSS Distributions expect_instrs_cycles_in_stackshot(ssbuf, sslen); 1292*4f1223e8SApple OSS Distributions }); 1293*4f1223e8SApple OSS Distributions} 1294*4f1223e8SApple OSS Distributions 1295*4f1223e8SApple OSS DistributionsT_DECL(delta_instrs_cycles, 1296*4f1223e8SApple OSS Distributions "test delta stackshots with instructions and cycles", T_META_TAG_VM_PREFERRED) 1297*4f1223e8SApple OSS Distributions{ 1298*4f1223e8SApple OSS Distributions skip_if_monotonic_unsupported(); 1299*4f1223e8SApple OSS Distributions 1300*4f1223e8SApple OSS Distributions struct scenario scenario = { 1301*4f1223e8SApple OSS Distributions .name = "delta-instrs-cycles", 1302*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES 1303*4f1223e8SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1304*4f1223e8SApple OSS Distributions }; 1305*4f1223e8SApple OSS Distributions 1306*4f1223e8SApple OSS Distributions T_LOG("taking full stackshot"); 1307*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1308*4f1223e8SApple OSS Distributions uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen); 1309*4f1223e8SApple OSS Distributions 1310*4f1223e8SApple OSS Distributions T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time); 1311*4f1223e8SApple OSS Distributions 1312*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 1313*4f1223e8SApple OSS Distributions expect_instrs_cycles_in_stackshot(ssbuf, sslen); 1314*4f1223e8SApple OSS Distributions 1315*4f1223e8SApple OSS Distributions struct scenario delta_scenario = { 1316*4f1223e8SApple OSS Distributions .name = "delta-instrs-cycles-next", 1317*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES 1318*4f1223e8SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT 1319*4f1223e8SApple OSS Distributions | STACKSHOT_COLLECT_DELTA_SNAPSHOT), 1320*4f1223e8SApple OSS Distributions .since_timestamp = stackshot_time, 1321*4f1223e8SApple OSS Distributions }; 1322*4f1223e8SApple OSS Distributions 1323*4f1223e8SApple OSS Distributions take_stackshot(&delta_scenario, false, ^(void *dssbuf, size_t dsslen) { 1324*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DELTA, dssbuf, dsslen, nil); 1325*4f1223e8SApple OSS Distributions expect_instrs_cycles_in_stackshot(dssbuf, dsslen); 1326*4f1223e8SApple OSS Distributions }); 1327*4f1223e8SApple OSS Distributions }); 1328*4f1223e8SApple OSS Distributions} 1329*4f1223e8SApple OSS Distributions 1330*4f1223e8SApple OSS Distributionsstatic void 1331*4f1223e8SApple OSS Distributionscheck_thread_groups_supported() 1332*4f1223e8SApple OSS Distributions{ 1333*4f1223e8SApple OSS Distributions int err; 1334*4f1223e8SApple OSS Distributions int supported = 0; 1335*4f1223e8SApple OSS Distributions size_t supported_size = sizeof(supported); 1336*4f1223e8SApple OSS Distributions err = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size, NULL, 0); 1337*4f1223e8SApple OSS Distributions 1338*4f1223e8SApple OSS Distributions if (err || !supported) 1339*4f1223e8SApple OSS Distributions T_SKIP("thread groups not supported on this system"); 1340*4f1223e8SApple OSS Distributions} 1341*4f1223e8SApple OSS Distributions 1342*4f1223e8SApple OSS DistributionsT_DECL(thread_groups, "test getting thread groups in stackshot", T_META_TAG_VM_PREFERRED) 1343*4f1223e8SApple OSS Distributions{ 1344*4f1223e8SApple OSS Distributions check_thread_groups_supported(); 1345*4f1223e8SApple OSS Distributions 1346*4f1223e8SApple OSS Distributions struct scenario scenario = { 1347*4f1223e8SApple OSS Distributions .name = "thread-groups", 1348*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_THREAD_GROUP 1349*4f1223e8SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1350*4f1223e8SApple OSS Distributions }; 1351*4f1223e8SApple OSS Distributions 1352*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with thread group flag"); 1353*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1354*4f1223e8SApple OSS Distributions parse_thread_group_stackshot(ssbuf, sslen); 1355*4f1223e8SApple OSS Distributions }); 1356*4f1223e8SApple OSS Distributions} 1357*4f1223e8SApple OSS Distributions 1358*4f1223e8SApple OSS DistributionsT_DECL(compactinfo, "test compactinfo inclusion", T_META_TAG_VM_PREFERRED) 1359*4f1223e8SApple OSS Distributions{ 1360*4f1223e8SApple OSS Distributions struct scenario scenario = { 1361*4f1223e8SApple OSS Distributions .name = "compactinfo", 1362*4f1223e8SApple OSS Distributions .target_pid = getpid(), 1363*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_SAVE_DYLD_COMPACTINFO 1364*4f1223e8SApple OSS Distributions | STACKSHOT_KCDATA_FORMAT), 1365*4f1223e8SApple OSS Distributions }; 1366*4f1223e8SApple OSS Distributions 1367*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with compactinfo flag"); 1368*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1369*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_COMPACTINFO | PARSE_STACKSHOT_TARGETPID, ssbuf, sslen, nil); 1370*4f1223e8SApple OSS Distributions }); 1371*4f1223e8SApple OSS Distributions} 1372*4f1223e8SApple OSS Distributions 1373*4f1223e8SApple OSS DistributionsT_DECL(suspendinfo, "test task suspend info inclusion", T_META_TAG_VM_PREFERRED) 1374*4f1223e8SApple OSS Distributions{ 1375*4f1223e8SApple OSS Distributions struct scenario scenario = { 1376*4f1223e8SApple OSS Distributions .name = "suspendinfo", 1377*4f1223e8SApple OSS Distributions .target_pid = getpid(), 1378*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT), 1379*4f1223e8SApple OSS Distributions }; 1380*4f1223e8SApple OSS Distributions 1381*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with suspendinfo flag"); 1382*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1383*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_SUSPENDINFO | PARSE_STACKSHOT_TARGETPID, ssbuf, sslen, nil); 1384*4f1223e8SApple OSS Distributions }); 1385*4f1223e8SApple OSS Distributions} 1386*4f1223e8SApple OSS Distributions 1387*4f1223e8SApple OSS Distributionsstatic NSMutableSet * find_driverkit_pids(io_registry_entry_t root) { 1388*4f1223e8SApple OSS Distributions NSMutableSet * driverkit_pids = [NSMutableSet setWithCapacity:3]; 1389*4f1223e8SApple OSS Distributions io_registry_entry_t current = IO_OBJECT_NULL; 1390*4f1223e8SApple OSS Distributions io_iterator_t iter = IO_OBJECT_NULL; 1391*4f1223e8SApple OSS Distributions 1392*4f1223e8SApple OSS Distributions T_EXPECT_MACH_SUCCESS(IORegistryEntryGetChildIterator(root, kIOServicePlane, &iter), "get registry iterator"); 1393*4f1223e8SApple OSS Distributions 1394*4f1223e8SApple OSS Distributions while ((current = IOIteratorNext(iter)) != IO_OBJECT_NULL) { 1395*4f1223e8SApple OSS Distributions if (_IOObjectConformsTo(current, "IOUserServer", kIOClassNameOverrideNone)) { 1396*4f1223e8SApple OSS Distributions CFMutableDictionaryRef cfProperties = NULL; 1397*4f1223e8SApple OSS Distributions NSMutableDictionary * properties; 1398*4f1223e8SApple OSS Distributions NSString * client_creator_info; 1399*4f1223e8SApple OSS Distributions NSArray<NSString *> *creator_info_array; 1400*4f1223e8SApple OSS Distributions pid_t pid; 1401*4f1223e8SApple OSS Distributions 1402*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(IORegistryEntryCreateCFProperties(current, &cfProperties, kCFAllocatorDefault, kNilOptions), "get properties"); 1403*4f1223e8SApple OSS Distributions properties = CFBridgingRelease(cfProperties); 1404*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(properties, "properties is not null"); 1405*4f1223e8SApple OSS Distributions client_creator_info = properties[@kIOUserClientCreatorKey]; 1406*4f1223e8SApple OSS Distributions creator_info_array = [client_creator_info componentsSeparatedByString:@","]; 1407*4f1223e8SApple OSS Distributions if ([creator_info_array[0] hasPrefix:@"pid"]) { 1408*4f1223e8SApple OSS Distributions NSArray<NSString *> *pid_info = [creator_info_array[0] componentsSeparatedByString:@" "]; 1409*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(pid_info.count, 2UL, "Get pid info components from %s", creator_info_array[0].UTF8String); 1410*4f1223e8SApple OSS Distributions pid = pid_info[1].intValue; 1411*4f1223e8SApple OSS Distributions } else { 1412*4f1223e8SApple OSS Distributions T_ASSERT_FAIL("No pid info in client creator info: %s", client_creator_info.UTF8String); 1413*4f1223e8SApple OSS Distributions } 1414*4f1223e8SApple OSS Distributions T_LOG("Found driver pid %d", pid); 1415*4f1223e8SApple OSS Distributions [driverkit_pids addObject:[NSNumber numberWithInt:pid]]; 1416*4f1223e8SApple OSS Distributions } else { 1417*4f1223e8SApple OSS Distributions [driverkit_pids unionSet:find_driverkit_pids(current)]; 1418*4f1223e8SApple OSS Distributions } 1419*4f1223e8SApple OSS Distributions IOObjectRelease(current); 1420*4f1223e8SApple OSS Distributions } 1421*4f1223e8SApple OSS Distributions 1422*4f1223e8SApple OSS Distributions IOObjectRelease(iter); 1423*4f1223e8SApple OSS Distributions return driverkit_pids; 1424*4f1223e8SApple OSS Distributions} 1425*4f1223e8SApple OSS Distributions 1426*4f1223e8SApple OSS DistributionsT_DECL(driverkit, "test driverkit inclusion", T_META_TAG_VM_PREFERRED) 1427*4f1223e8SApple OSS Distributions{ 1428*4f1223e8SApple OSS Distributions struct scenario scenario = { 1429*4f1223e8SApple OSS Distributions .name = "driverkit", 1430*4f1223e8SApple OSS Distributions .target_kernel = true, 1431*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT 1432*4f1223e8SApple OSS Distributions | STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL), 1433*4f1223e8SApple OSS Distributions }; 1434*4f1223e8SApple OSS Distributions 1435*4f1223e8SApple OSS Distributions io_registry_entry_t root = IORegistryGetRootEntry(kIOMainPortDefault); 1436*4f1223e8SApple OSS Distributions NSMutableSet * driverkit_pids = find_driverkit_pids(root); 1437*4f1223e8SApple OSS Distributions IOObjectRelease(root); 1438*4f1223e8SApple OSS Distributions 1439*4f1223e8SApple OSS Distributions T_LOG("expecting to find %lu driverkit processes", [driverkit_pids count]); 1440*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL flag"); 1441*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1442*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_DRIVERKIT | PARSE_STACKSHOT_TARGETPID, ssbuf, sslen, @{ 1443*4f1223e8SApple OSS Distributions driverkit_found_key: ^(pid_t pid) { 1444*4f1223e8SApple OSS Distributions [driverkit_pids removeObject:[NSNumber numberWithInt:pid]]; 1445*4f1223e8SApple OSS Distributions }}); 1446*4f1223e8SApple OSS Distributions }); 1447*4f1223e8SApple OSS Distributions 1448*4f1223e8SApple OSS Distributions T_EXPECT_EQ([driverkit_pids count], (NSUInteger)0, "found expected number of driverkit processes"); 1449*4f1223e8SApple OSS Distributions} 1450*4f1223e8SApple OSS Distributions 1451*4f1223e8SApple OSS Distributionsstatic void 1452*4f1223e8SApple OSS Distributionsparse_page_table_asid_stackshot(void **ssbuf, size_t sslen) 1453*4f1223e8SApple OSS Distributions{ 1454*4f1223e8SApple OSS Distributions bool seen_asid = false; 1455*4f1223e8SApple OSS Distributions bool seen_page_table_snapshot = false; 1456*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1457*4f1223e8SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, 1458*4f1223e8SApple OSS Distributions "buffer provided is a stackshot"); 1459*4f1223e8SApple OSS Distributions 1460*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 1461*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1462*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 1463*4f1223e8SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 1464*4f1223e8SApple OSS Distributions T_QUIET; 1465*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_array_valid(iter), 1466*4f1223e8SApple OSS Distributions "checked that array is valid"); 1467*4f1223e8SApple OSS Distributions 1468*4f1223e8SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_PAGE_TABLES) { 1469*4f1223e8SApple OSS Distributions continue; 1470*4f1223e8SApple OSS Distributions } 1471*4f1223e8SApple OSS Distributions 1472*4f1223e8SApple OSS Distributions T_ASSERT_FALSE(seen_page_table_snapshot, "check that we haven't yet seen a page table snapshot"); 1473*4f1223e8SApple OSS Distributions seen_page_table_snapshot = true; 1474*4f1223e8SApple OSS Distributions 1475*4f1223e8SApple OSS Distributions T_ASSERT_EQ((size_t) kcdata_iter_array_elem_size(iter), sizeof(uint64_t), 1476*4f1223e8SApple OSS Distributions "check that each element of the pagetable dump is the expected size"); 1477*4f1223e8SApple OSS Distributions 1478*4f1223e8SApple OSS Distributions uint64_t *pt_array = kcdata_iter_payload(iter); 1479*4f1223e8SApple OSS Distributions uint32_t elem_count = kcdata_iter_array_elem_count(iter); 1480*4f1223e8SApple OSS Distributions uint32_t j; 1481*4f1223e8SApple OSS Distributions bool nonzero_tte = false; 1482*4f1223e8SApple OSS Distributions for (j = 0; j < elem_count;) { 1483*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE(j + 4, elem_count, "check for valid page table segment header"); 1484*4f1223e8SApple OSS Distributions uint64_t pa = pt_array[j]; 1485*4f1223e8SApple OSS Distributions uint64_t num_entries = pt_array[j + 1]; 1486*4f1223e8SApple OSS Distributions uint64_t start_va = pt_array[j + 2]; 1487*4f1223e8SApple OSS Distributions uint64_t end_va = pt_array[j + 3]; 1488*4f1223e8SApple OSS Distributions 1489*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NE(pa, (uint64_t) 0, "check that the pagetable physical address is non-zero"); 1490*4f1223e8SApple 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"); 1491*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NE(num_entries, (uint64_t) 0, "check that a pagetable region has more than 0 entries"); 1492*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE(j + 4 + num_entries, (uint64_t) elem_count, "check for sufficient space in page table array"); 1493*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(end_va, start_va, "check for valid VA bounds in page table segment header"); 1494*4f1223e8SApple OSS Distributions 1495*4f1223e8SApple OSS Distributions for (uint32_t k = j + 4; k < (j + 4 + num_entries); ++k) { 1496*4f1223e8SApple OSS Distributions if (pt_array[k] != 0) { 1497*4f1223e8SApple OSS Distributions nonzero_tte = true; 1498*4f1223e8SApple 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"); 1499*4f1223e8SApple 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 1500*4f1223e8SApple OSS Distributions bool table = ((pt_array[k] & 0x2) != 0); 1501*4f1223e8SApple OSS Distributions if (table) { 1502*4f1223e8SApple 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"); 1503*4f1223e8SApple OSS Distributions } else { // should be a compressed PTE 1504*4f1223e8SApple 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"); 1505*4f1223e8SApple 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"); 1506*4f1223e8SApple OSS Distributions } 1507*4f1223e8SApple OSS Distributions } 1508*4f1223e8SApple OSS Distributions } 1509*4f1223e8SApple OSS Distributions 1510*4f1223e8SApple OSS Distributions j += (4 + num_entries); 1511*4f1223e8SApple OSS Distributions } 1512*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(nonzero_tte, "check that we saw at least one non-empty TTE"); 1513*4f1223e8SApple OSS Distributions T_ASSERT_EQ(j, elem_count, "check that page table dump size matches extent of last header"); 1514*4f1223e8SApple OSS Distributions break; 1515*4f1223e8SApple OSS Distributions } 1516*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_ASID: { 1517*4f1223e8SApple OSS Distributions T_ASSERT_FALSE(seen_asid, "check that we haven't yet seen an ASID"); 1518*4f1223e8SApple OSS Distributions seen_asid = true; 1519*4f1223e8SApple OSS Distributions } 1520*4f1223e8SApple OSS Distributions } 1521*4f1223e8SApple OSS Distributions } 1522*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(seen_page_table_snapshot, "check that we have seen a page table snapshot"); 1523*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(seen_asid, "check that we have seen an ASID"); 1524*4f1223e8SApple OSS Distributions} 1525*4f1223e8SApple OSS Distributions 1526*4f1223e8SApple OSS DistributionsT_DECL(dump_page_tables, "test stackshot page table dumping support", T_META_TAG_VM_PREFERRED) 1527*4f1223e8SApple OSS Distributions{ 1528*4f1223e8SApple OSS Distributions struct scenario scenario = { 1529*4f1223e8SApple OSS Distributions .name = "asid-page-tables", 1530*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT | STACKSHOT_ASID | STACKSHOT_PAGE_TABLES), 1531*4f1223e8SApple OSS Distributions .size_hint = (9ull << 20), // 9 MB 1532*4f1223e8SApple OSS Distributions .target_pid = getpid(), 1533*4f1223e8SApple OSS Distributions .maybe_unsupported = true, 1534*4f1223e8SApple OSS Distributions .maybe_enomem = true, 1535*4f1223e8SApple OSS Distributions }; 1536*4f1223e8SApple OSS Distributions 1537*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with ASID and page table flags"); 1538*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1539*4f1223e8SApple OSS Distributions parse_page_table_asid_stackshot(ssbuf, sslen); 1540*4f1223e8SApple OSS Distributions }); 1541*4f1223e8SApple OSS Distributions} 1542*4f1223e8SApple OSS Distributions 1543*4f1223e8SApple 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) 1544*4f1223e8SApple OSS Distributions{ 1545*4f1223e8SApple OSS Distributions const uuid_t *current_uuid = (const uuid_t *)(&proc_info_data->p_uuid); 1546*4f1223e8SApple OSS Distributions 1547*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 1548*4f1223e8SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "buffer provided is a stackshot"); 1549*4f1223e8SApple OSS Distributions 1550*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 1551*4f1223e8SApple OSS Distributions 1552*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 1553*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 1554*4f1223e8SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 1555*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(kcdata_iter_array_valid(iter), "checked that array is valid"); 1556*4f1223e8SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) == KCDATA_TYPE_LIBRARY_LOADINFO64) { 1557*4f1223e8SApple OSS Distributions struct user64_dyld_uuid_info *info = (struct user64_dyld_uuid_info *) kcdata_iter_payload(iter); 1558*4f1223e8SApple OSS Distributions if (uuid_compare(*current_uuid, info->imageUUID) == 0) { 1559*4f1223e8SApple OSS Distributions T_ASSERT_EQ(expected_offset, info->imageLoadAddress, "found matching UUID with matching binary offset"); 1560*4f1223e8SApple OSS Distributions return; 1561*4f1223e8SApple OSS Distributions } 1562*4f1223e8SApple OSS Distributions } else if (kcdata_iter_array_elem_type(iter) == KCDATA_TYPE_LIBRARY_LOADINFO) { 1563*4f1223e8SApple OSS Distributions struct user32_dyld_uuid_info *info = (struct user32_dyld_uuid_info *) kcdata_iter_payload(iter); 1564*4f1223e8SApple OSS Distributions if (uuid_compare(*current_uuid, info->imageUUID) == 0) { 1565*4f1223e8SApple OSS Distributions T_ASSERT_EQ(expected_offset, ((uint64_t) info->imageLoadAddress), "found matching UUID with matching binary offset"); 1566*4f1223e8SApple OSS Distributions return; 1567*4f1223e8SApple OSS Distributions } 1568*4f1223e8SApple OSS Distributions } 1569*4f1223e8SApple OSS Distributions break; 1570*4f1223e8SApple OSS Distributions } 1571*4f1223e8SApple OSS Distributions default: 1572*4f1223e8SApple OSS Distributions break; 1573*4f1223e8SApple OSS Distributions } 1574*4f1223e8SApple OSS Distributions } 1575*4f1223e8SApple OSS Distributions 1576*4f1223e8SApple OSS Distributions T_FAIL("failed to find matching UUID in stackshot data"); 1577*4f1223e8SApple OSS Distributions} 1578*4f1223e8SApple OSS Distributions 1579*4f1223e8SApple OSS DistributionsT_DECL(translated, 1580*4f1223e8SApple OSS Distributions "tests translated bit is set correctly", 1581*4f1223e8SApple OSS Distributions T_META_TAG_VM_PREFERRED, 1582*4f1223e8SApple OSS Distributions T_META_ENABLED(false /* rdar://133956022 */)) 1583*4f1223e8SApple OSS Distributions{ 1584*4f1223e8SApple OSS Distributions#if !(TARGET_OS_OSX && TARGET_CPU_ARM64) 1585*4f1223e8SApple OSS Distributions T_SKIP("Only valid on Apple silicon Macs") 1586*4f1223e8SApple OSS Distributions#endif 1587*4f1223e8SApple OSS Distributions // Get path of stackshot_translated_child helper binary 1588*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 1589*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 1590*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 1591*4f1223e8SApple OSS Distributions char* binary_name = strrchr(path, '/'); 1592*4f1223e8SApple OSS Distributions if (binary_name) binary_name++; 1593*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(binary_name, "Find basename in path '%s'", path); 1594*4f1223e8SApple OSS Distributions strlcpy(binary_name, "stackshot_translated_child", path_size - (binary_name - path)); 1595*4f1223e8SApple OSS Distributions char *args[] = { path, NULL }; 1596*4f1223e8SApple OSS Distributions 1597*4f1223e8SApple OSS Distributions dispatch_source_t child_sig_src; 1598*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0); 1599*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "exec child semaphore"); 1600*4f1223e8SApple OSS Distributions 1601*4f1223e8SApple OSS Distributions dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL); 1602*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(signal_processing_q, "signal processing queue"); 1603*4f1223e8SApple OSS Distributions 1604*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 1605*4f1223e8SApple OSS Distributions child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q); 1606*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)"); 1607*4f1223e8SApple OSS Distributions 1608*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); }); 1609*4f1223e8SApple OSS Distributions dispatch_activate(child_sig_src); 1610*4f1223e8SApple OSS Distributions 1611*4f1223e8SApple OSS Distributions // Spawn child 1612*4f1223e8SApple OSS Distributions pid_t pid; 1613*4f1223e8SApple OSS Distributions T_LOG("spawning translated child"); 1614*4f1223e8SApple 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); 1615*4f1223e8SApple OSS Distributions 1616*4f1223e8SApple OSS Distributions // Wait for the the child to spawn up 1617*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER); 1618*4f1223e8SApple OSS Distributions 1619*4f1223e8SApple OSS Distributions // Make sure the child is running and is translated 1620*4f1223e8SApple OSS Distributions int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid }; 1621*4f1223e8SApple OSS Distributions struct kinfo_proc process_info; 1622*4f1223e8SApple OSS Distributions size_t bufsize = sizeof(process_info); 1623*4f1223e8SApple 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"); 1624*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(bufsize, (size_t)0, "process info is not empty"); 1625*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE((process_info.kp_proc.p_flag & P_TRANSLATED), "KERN_PROC_PID reports child is translated"); 1626*4f1223e8SApple OSS Distributions 1627*4f1223e8SApple OSS Distributions T_LOG("capturing stackshot"); 1628*4f1223e8SApple OSS Distributions 1629*4f1223e8SApple OSS Distributions struct scenario scenario = { 1630*4f1223e8SApple OSS Distributions .name = "translated", 1631*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 1632*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 1633*4f1223e8SApple OSS Distributions }; 1634*4f1223e8SApple OSS Distributions 1635*4f1223e8SApple OSS Distributions take_stackshot(&scenario, true, ^( void *ssbuf, size_t sslen) { 1636*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_TRANSLATED, ssbuf, sslen, @{translated_child_pid_key: @(pid)}); 1637*4f1223e8SApple OSS Distributions }); 1638*4f1223e8SApple OSS Distributions 1639*4f1223e8SApple OSS Distributions // Kill the child 1640*4f1223e8SApple OSS Distributions int status; 1641*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGTERM), "kill translated child"); 1642*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on translated child"); 1643*4f1223e8SApple OSS Distributions 1644*4f1223e8SApple OSS Distributions} 1645*4f1223e8SApple OSS Distributions 1646*4f1223e8SApple OSS DistributionsT_DECL(proc_uuid_info, "tests that the main binary UUID for a proc is always populated", T_META_TAG_VM_PREFERRED) 1647*4f1223e8SApple OSS Distributions{ 1648*4f1223e8SApple OSS Distributions struct proc_uniqidentifierinfo proc_info_data = { }; 1649*4f1223e8SApple OSS Distributions mach_msg_type_number_t count; 1650*4f1223e8SApple OSS Distributions kern_return_t kernel_status; 1651*4f1223e8SApple OSS Distributions task_dyld_info_data_t task_dyld_info; 1652*4f1223e8SApple OSS Distributions struct dyld_all_image_infos *target_infos; 1653*4f1223e8SApple OSS Distributions int retval; 1654*4f1223e8SApple OSS Distributions bool found_image_in_image_infos = false; 1655*4f1223e8SApple OSS Distributions uint64_t expected_mach_header_offset = 0; 1656*4f1223e8SApple OSS Distributions 1657*4f1223e8SApple OSS Distributions /* Find the UUID of our main binary */ 1658*4f1223e8SApple OSS Distributions retval = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &proc_info_data, sizeof(proc_info_data)); 1659*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(retval, "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO"); 1660*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ_INT(retval, (int) sizeof(proc_info_data), "proc_pidinfo PROC_PIDUNIQIDENTIFIERINFO returned data"); 1661*4f1223e8SApple OSS Distributions 1662*4f1223e8SApple OSS Distributions uuid_string_t str = {}; 1663*4f1223e8SApple OSS Distributions uuid_unparse(*(uuid_t*)&proc_info_data.p_uuid, str); 1664*4f1223e8SApple OSS Distributions T_LOG("Found current UUID is %s", str); 1665*4f1223e8SApple OSS Distributions 1666*4f1223e8SApple OSS Distributions /* Find the location of the dyld image info metadata */ 1667*4f1223e8SApple OSS Distributions count = TASK_DYLD_INFO_COUNT; 1668*4f1223e8SApple OSS Distributions kernel_status = task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&task_dyld_info, &count); 1669*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(kernel_status, KERN_SUCCESS, "retrieve task_info for TASK_DYLD_INFO"); 1670*4f1223e8SApple OSS Distributions 1671*4f1223e8SApple OSS Distributions target_infos = (struct dyld_all_image_infos *)task_dyld_info.all_image_info_addr; 1672*4f1223e8SApple OSS Distributions 1673*4f1223e8SApple OSS Distributions /* Find our binary in the dyld image info array */ 1674*4f1223e8SApple OSS Distributions for (int i = 0; i < (int) target_infos->uuidArrayCount; i++) { 1675*4f1223e8SApple OSS Distributions if (uuid_compare(target_infos->uuidArray[i].imageUUID, *(uuid_t*)&proc_info_data.p_uuid) == 0) { 1676*4f1223e8SApple OSS Distributions expected_mach_header_offset = (uint64_t) target_infos->uuidArray[i].imageLoadAddress; 1677*4f1223e8SApple OSS Distributions found_image_in_image_infos = true; 1678*4f1223e8SApple OSS Distributions } 1679*4f1223e8SApple OSS Distributions } 1680*4f1223e8SApple OSS Distributions 1681*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(found_image_in_image_infos, "found binary image in dyld image info list"); 1682*4f1223e8SApple OSS Distributions 1683*4f1223e8SApple OSS Distributions /* Overwrite the dyld image info data so the kernel has to fallback to the UUID stored in the proc structure */ 1684*4f1223e8SApple OSS Distributions target_infos->uuidArrayCount = 0; 1685*4f1223e8SApple OSS Distributions 1686*4f1223e8SApple OSS Distributions struct scenario scenario = { 1687*4f1223e8SApple OSS Distributions .name = "proc_uuid_info", 1688*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT), 1689*4f1223e8SApple OSS Distributions .target_pid = getpid(), 1690*4f1223e8SApple OSS Distributions }; 1691*4f1223e8SApple OSS Distributions 1692*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot for current PID"); 1693*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1694*4f1223e8SApple OSS Distributions stackshot_verify_current_proc_uuid_info(ssbuf, sslen, expected_mach_header_offset, &proc_info_data); 1695*4f1223e8SApple OSS Distributions }); 1696*4f1223e8SApple OSS Distributions} 1697*4f1223e8SApple OSS Distributions 1698*4f1223e8SApple OSS DistributionsT_DECL(cseg_waitinfo, "test that threads stuck in the compressor report correct waitinfo", T_META_TAG_VM_PREFERRED) 1699*4f1223e8SApple OSS Distributions{ 1700*4f1223e8SApple OSS Distributions struct scenario scenario = { 1701*4f1223e8SApple OSS Distributions .name = "cseg_waitinfo", 1702*4f1223e8SApple OSS Distributions .quiet = false, 1703*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 1704*4f1223e8SApple OSS Distributions }; 1705*4f1223e8SApple OSS Distributions __block uint64_t thread_id = 0; 1706*4f1223e8SApple OSS Distributions 1707*4f1223e8SApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("com.apple.stackshot.cseg_waitinfo", NULL); 1708*4f1223e8SApple OSS Distributions dispatch_semaphore_t child_ok = dispatch_semaphore_create(0); 1709*4f1223e8SApple OSS Distributions 1710*4f1223e8SApple OSS Distributions dispatch_async(dq, ^{ 1711*4f1223e8SApple OSS Distributions pthread_threadid_np(NULL, &thread_id); 1712*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(child_ok); 1713*4f1223e8SApple OSS Distributions int val = 1; 1714*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.cseg_wedge_thread", NULL, NULL, &val, sizeof(val)), "wedge child thread"); 1715*4f1223e8SApple OSS Distributions }); 1716*4f1223e8SApple OSS Distributions 1717*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(child_ok, DISPATCH_TIME_FOREVER); 1718*4f1223e8SApple OSS Distributions sleep(1); 1719*4f1223e8SApple OSS Distributions 1720*4f1223e8SApple OSS Distributions T_LOG("taking stackshot"); 1721*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1722*4f1223e8SApple OSS Distributions int val = 1; 1723*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.cseg_unwedge_thread", NULL, NULL, &val, sizeof(val)), "unwedge child thread"); 1724*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_CSEG, ssbuf, sslen, @{cseg_expected_threadid_key: @(thread_id)}); 1725*4f1223e8SApple OSS Distributions }); 1726*4f1223e8SApple OSS Distributions} 1727*4f1223e8SApple OSS Distributions 1728*4f1223e8SApple OSS Distributionsstatic void 1729*4f1223e8SApple OSS Distributionssrp_send( 1730*4f1223e8SApple OSS Distributions mach_port_t send_port, 1731*4f1223e8SApple OSS Distributions mach_port_t reply_port, 1732*4f1223e8SApple OSS Distributions mach_port_t msg_port) 1733*4f1223e8SApple OSS Distributions{ 1734*4f1223e8SApple OSS Distributions kern_return_t ret = 0; 1735*4f1223e8SApple OSS Distributions 1736*4f1223e8SApple OSS Distributions struct test_msg { 1737*4f1223e8SApple OSS Distributions mach_msg_header_t header; 1738*4f1223e8SApple OSS Distributions mach_msg_body_t body; 1739*4f1223e8SApple OSS Distributions mach_msg_port_descriptor_t port_descriptor; 1740*4f1223e8SApple OSS Distributions }; 1741*4f1223e8SApple OSS Distributions struct test_msg send_msg = { 1742*4f1223e8SApple OSS Distributions .header = { 1743*4f1223e8SApple OSS Distributions .msgh_remote_port = send_port, 1744*4f1223e8SApple OSS Distributions .msgh_local_port = reply_port, 1745*4f1223e8SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 1746*4f1223e8SApple OSS Distributions reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0, 1747*4f1223e8SApple OSS Distributions MACH_MSG_TYPE_MOVE_SEND, 1748*4f1223e8SApple OSS Distributions MACH_MSGH_BITS_COMPLEX), 1749*4f1223e8SApple OSS Distributions .msgh_id = 0x100, 1750*4f1223e8SApple OSS Distributions .msgh_size = sizeof(send_msg), 1751*4f1223e8SApple OSS Distributions }, 1752*4f1223e8SApple OSS Distributions .body = { 1753*4f1223e8SApple OSS Distributions .msgh_descriptor_count = 1, 1754*4f1223e8SApple OSS Distributions }, 1755*4f1223e8SApple OSS Distributions .port_descriptor = { 1756*4f1223e8SApple OSS Distributions .name = msg_port, 1757*4f1223e8SApple OSS Distributions .disposition = MACH_MSG_TYPE_MOVE_RECEIVE, 1758*4f1223e8SApple OSS Distributions .type = MACH_MSG_PORT_DESCRIPTOR, 1759*4f1223e8SApple OSS Distributions }, 1760*4f1223e8SApple OSS Distributions }; 1761*4f1223e8SApple OSS Distributions 1762*4f1223e8SApple OSS Distributions if (msg_port == MACH_PORT_NULL) { 1763*4f1223e8SApple OSS Distributions send_msg.body.msgh_descriptor_count = 0; 1764*4f1223e8SApple OSS Distributions } 1765*4f1223e8SApple OSS Distributions 1766*4f1223e8SApple OSS Distributions ret = mach_msg(&(send_msg.header), 1767*4f1223e8SApple OSS Distributions MACH_SEND_MSG | 1768*4f1223e8SApple OSS Distributions MACH_SEND_TIMEOUT | 1769*4f1223e8SApple OSS Distributions MACH_SEND_OVERRIDE, 1770*4f1223e8SApple OSS Distributions send_msg.header.msgh_size, 1771*4f1223e8SApple OSS Distributions 0, 1772*4f1223e8SApple OSS Distributions MACH_PORT_NULL, 1773*4f1223e8SApple OSS Distributions 10000, 1774*4f1223e8SApple OSS Distributions 0); 1775*4f1223e8SApple OSS Distributions 1776*4f1223e8SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "client mach_msg"); 1777*4f1223e8SApple OSS Distributions} 1778*4f1223e8SApple OSS Distributions 1779*4f1223e8SApple OSS DistributionsT_HELPER_DECL(srp_client, 1780*4f1223e8SApple OSS Distributions "Client used for the special_reply_port test") 1781*4f1223e8SApple OSS Distributions{ 1782*4f1223e8SApple OSS Distributions pid_t ppid = getppid(); 1783*4f1223e8SApple OSS Distributions dispatch_semaphore_t can_continue = dispatch_semaphore_create(0); 1784*4f1223e8SApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("client_signalqueue", NULL); 1785*4f1223e8SApple OSS Distributions dispatch_source_t sig_src; 1786*4f1223e8SApple OSS Distributions 1787*4f1223e8SApple OSS Distributions mach_msg_return_t mr; 1788*4f1223e8SApple OSS Distributions mach_port_t service_port; 1789*4f1223e8SApple OSS Distributions mach_port_t conn_port; 1790*4f1223e8SApple OSS Distributions mach_port_t special_reply_port; 1791*4f1223e8SApple OSS Distributions mach_port_options_t opts = { 1792*4f1223e8SApple OSS Distributions .flags = MPO_INSERT_SEND_RIGHT, 1793*4f1223e8SApple OSS Distributions }; 1794*4f1223e8SApple OSS Distributions 1795*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 1796*4f1223e8SApple OSS Distributions sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 1797*4f1223e8SApple OSS Distributions 1798*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(sig_src, ^{ 1799*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(can_continue); 1800*4f1223e8SApple OSS Distributions }); 1801*4f1223e8SApple OSS Distributions dispatch_activate(sig_src); 1802*4f1223e8SApple OSS Distributions 1803*4f1223e8SApple OSS Distributions /* lookup the mach service port for the parent */ 1804*4f1223e8SApple OSS Distributions kern_return_t kr = bootstrap_look_up(bootstrap_port, 1805*4f1223e8SApple OSS Distributions SRP_SERVICE_NAME, &service_port); 1806*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up"); 1807*4f1223e8SApple OSS Distributions 1808*4f1223e8SApple OSS Distributions /* create the send-once right (special reply port) and message to send to the server */ 1809*4f1223e8SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0ull, &conn_port); 1810*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct"); 1811*4f1223e8SApple OSS Distributions 1812*4f1223e8SApple OSS Distributions special_reply_port = thread_get_special_reply_port(); 1813*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port"); 1814*4f1223e8SApple OSS Distributions 1815*4f1223e8SApple OSS Distributions /* send the message with the special reply port */ 1816*4f1223e8SApple OSS Distributions srp_send(service_port, special_reply_port, conn_port); 1817*4f1223e8SApple OSS Distributions 1818*4f1223e8SApple OSS Distributions /* signal the parent to continue */ 1819*4f1223e8SApple OSS Distributions kill(ppid, SIGUSR1); 1820*4f1223e8SApple OSS Distributions 1821*4f1223e8SApple OSS Distributions struct { 1822*4f1223e8SApple OSS Distributions mach_msg_header_t header; 1823*4f1223e8SApple OSS Distributions mach_msg_body_t body; 1824*4f1223e8SApple OSS Distributions mach_msg_port_descriptor_t port_descriptor; 1825*4f1223e8SApple OSS Distributions } rcv_msg = { 1826*4f1223e8SApple OSS Distributions .header = 1827*4f1223e8SApple OSS Distributions { 1828*4f1223e8SApple OSS Distributions .msgh_remote_port = MACH_PORT_NULL, 1829*4f1223e8SApple OSS Distributions .msgh_local_port = special_reply_port, 1830*4f1223e8SApple OSS Distributions .msgh_size = sizeof(rcv_msg), 1831*4f1223e8SApple OSS Distributions }, 1832*4f1223e8SApple OSS Distributions }; 1833*4f1223e8SApple OSS Distributions 1834*4f1223e8SApple OSS Distributions /* wait on the reply from the parent (that we will never receive) */ 1835*4f1223e8SApple OSS Distributions mr = mach_msg(&(rcv_msg.header), 1836*4f1223e8SApple OSS Distributions (MACH_RCV_MSG | MACH_RCV_SYNC_WAIT), 1837*4f1223e8SApple OSS Distributions 0, 1838*4f1223e8SApple OSS Distributions rcv_msg.header.msgh_size, 1839*4f1223e8SApple OSS Distributions special_reply_port, 1840*4f1223e8SApple OSS Distributions MACH_MSG_TIMEOUT_NONE, 1841*4f1223e8SApple OSS Distributions service_port); 1842*4f1223e8SApple OSS Distributions 1843*4f1223e8SApple OSS Distributions /* not expected to execute as parent will SIGKILL client... */ 1844*4f1223e8SApple OSS Distributions T_LOG("client process exiting after sending message to parent (server)"); 1845*4f1223e8SApple OSS Distributions} 1846*4f1223e8SApple OSS Distributions 1847*4f1223e8SApple OSS Distributionsenum srp_test_type { 1848*4f1223e8SApple OSS Distributions SRP_TEST_THREAD, /* expect waiter on current thread */ 1849*4f1223e8SApple OSS Distributions SRP_TEST_PID, /* expect waiter on current PID */ 1850*4f1223e8SApple OSS Distributions SRP_TEST_EITHER, /* waiter could be on either */ 1851*4f1223e8SApple OSS Distributions}; 1852*4f1223e8SApple OSS Distributions 1853*4f1223e8SApple OSS Distributionsstatic void 1854*4f1223e8SApple OSS Distributionscheck_srp_test(const char *name, enum srp_test_type ty) 1855*4f1223e8SApple OSS Distributions{ 1856*4f1223e8SApple OSS Distributions struct scenario scenario = { 1857*4f1223e8SApple OSS Distributions .name = name, 1858*4f1223e8SApple OSS Distributions .quiet = false, 1859*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 1860*4f1223e8SApple OSS Distributions }; 1861*4f1223e8SApple OSS Distributions uint64_t thread_id = 0; 1862*4f1223e8SApple OSS Distributions pthread_threadid_np(NULL, &thread_id); 1863*4f1223e8SApple OSS Distributions if (ty == SRP_TEST_THREAD) { 1864*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1865*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_SRP, ssbuf, sslen, 1866*4f1223e8SApple OSS Distributions @{srp_expected_threadid_key: @(thread_id)}); 1867*4f1223e8SApple OSS Distributions }); 1868*4f1223e8SApple OSS Distributions } else if (ty == SRP_TEST_PID) { 1869*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1870*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_SRP, ssbuf, sslen, 1871*4f1223e8SApple OSS Distributions @{srp_expected_pid_key: @(getpid())}); 1872*4f1223e8SApple OSS Distributions }); 1873*4f1223e8SApple OSS Distributions } else { 1874*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 1875*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_WAITINFO_SRP, ssbuf, sslen, 1876*4f1223e8SApple OSS Distributions @{srp_expected_pid_key: @(getpid()), srp_expected_threadid_key: @(thread_id)}); 1877*4f1223e8SApple OSS Distributions }); 1878*4f1223e8SApple OSS Distributions } 1879*4f1223e8SApple OSS Distributions 1880*4f1223e8SApple OSS Distributions} 1881*4f1223e8SApple OSS Distributions 1882*4f1223e8SApple OSS Distributions 1883*4f1223e8SApple OSS Distributions/* 1884*4f1223e8SApple OSS Distributions * Tests the stackshot wait info plumbing for synchronous IPC that doesn't use kevent on the server. 1885*4f1223e8SApple OSS Distributions * 1886*4f1223e8SApple OSS Distributions * (part 1): tests the scenario where a client sends a request that includes a special reply port 1887*4f1223e8SApple OSS Distributions * to a server that doesn't receive the message and doesn't copy the send-once right 1888*4f1223e8SApple OSS Distributions * into its address space as a result. for this case the special reply port is enqueued 1889*4f1223e8SApple OSS Distributions * in a port and we check which task has that receive right and use that info. (rdar://60440338) 1890*4f1223e8SApple OSS Distributions * (part 2): tests the scenario where a client sends a request that includes a special reply port 1891*4f1223e8SApple OSS Distributions * to a server that receives the message and copies in the send-once right, but doesn't 1892*4f1223e8SApple OSS Distributions * reply to the client. for this case the special reply port is copied out and the kernel 1893*4f1223e8SApple OSS Distributions * stashes the info about which task copied out the send once right. (rdar://60440592) 1894*4f1223e8SApple OSS Distributions * (part 3): tests the same as part 2, but uses kevents, which allow for 1895*4f1223e8SApple OSS Distributions * priority inheritance 1896*4f1223e8SApple OSS Distributions */ 1897*4f1223e8SApple OSS DistributionsT_DECL(special_reply_port, "test that tasks using special reply ports have correct waitinfo", T_META_TAG_VM_PREFERRED) 1898*4f1223e8SApple OSS Distributions{ 1899*4f1223e8SApple OSS Distributions dispatch_semaphore_t can_continue = dispatch_semaphore_create(0); 1900*4f1223e8SApple OSS Distributions dispatch_queue_t dq = dispatch_queue_create("signalqueue", NULL); 1901*4f1223e8SApple OSS Distributions dispatch_queue_t machdq = dispatch_queue_create("machqueue", NULL); 1902*4f1223e8SApple OSS Distributions dispatch_source_t sig_src; 1903*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 1904*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 1905*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 1906*4f1223e8SApple OSS Distributions char *client_args[] = { path, "-n", "srp_client", NULL }; 1907*4f1223e8SApple OSS Distributions pid_t client_pid; 1908*4f1223e8SApple OSS Distributions int sp_ret; 1909*4f1223e8SApple OSS Distributions kern_return_t kr; 1910*4f1223e8SApple OSS Distributions mach_port_t port; 1911*4f1223e8SApple OSS Distributions 1912*4f1223e8SApple OSS Distributions /* setup the signal handler in the parent (server) */ 1913*4f1223e8SApple OSS Distributions T_LOG("setup sig handlers"); 1914*4f1223e8SApple OSS Distributions signal(SIGUSR1, SIG_IGN); 1915*4f1223e8SApple OSS Distributions sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq); 1916*4f1223e8SApple OSS Distributions 1917*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(sig_src, ^{ 1918*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(can_continue); 1919*4f1223e8SApple OSS Distributions }); 1920*4f1223e8SApple OSS Distributions dispatch_activate(sig_src); 1921*4f1223e8SApple OSS Distributions 1922*4f1223e8SApple OSS Distributions /* register with the mach service name so the client can lookup and send a message to the parent (server) */ 1923*4f1223e8SApple OSS Distributions T_LOG("Server about to check in"); 1924*4f1223e8SApple OSS Distributions kr = bootstrap_check_in(bootstrap_port, SRP_SERVICE_NAME, &port); 1925*4f1223e8SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "server bootstrap_check_in"); 1926*4f1223e8SApple OSS Distributions 1927*4f1223e8SApple OSS Distributions T_LOG("Launching client"); 1928*4f1223e8SApple OSS Distributions sp_ret = posix_spawn(&client_pid, client_args[0], NULL, NULL, client_args, NULL); 1929*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", client_args[0], client_pid); 1930*4f1223e8SApple OSS Distributions T_LOG("Spawned client as PID %d", client_pid); 1931*4f1223e8SApple OSS Distributions 1932*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 1933*4f1223e8SApple OSS Distributions T_LOG("Ready to take stackshot, but waiting 1s for the coast to clear"); 1934*4f1223e8SApple OSS Distributions 1935*4f1223e8SApple OSS Distributions /* 1936*4f1223e8SApple OSS Distributions * can_continue indicates the client has signaled us, but we want to make 1937*4f1223e8SApple OSS Distributions * sure they've actually blocked sending their mach message. It's cheesy, but 1938*4f1223e8SApple OSS Distributions * sleep() works for this. 1939*4f1223e8SApple OSS Distributions */ 1940*4f1223e8SApple OSS Distributions sleep(1); 1941*4f1223e8SApple OSS Distributions 1942*4f1223e8SApple OSS Distributions /* 1943*4f1223e8SApple OSS Distributions * take the stackshot without calling receive to verify that the stackshot wait 1944*4f1223e8SApple OSS Distributions * info shows our (the server) thread for the scenario where the server has yet to 1945*4f1223e8SApple OSS Distributions * receive the message. 1946*4f1223e8SApple OSS Distributions */ 1947*4f1223e8SApple OSS Distributions T_LOG("Taking stackshot for part 1 coverage"); 1948*4f1223e8SApple OSS Distributions check_srp_test("srp", SRP_TEST_THREAD); 1949*4f1223e8SApple OSS Distributions 1950*4f1223e8SApple OSS Distributions /* 1951*4f1223e8SApple OSS Distributions * receive the message from the client (which should copy the send once right into 1952*4f1223e8SApple OSS Distributions * our address space). 1953*4f1223e8SApple OSS Distributions */ 1954*4f1223e8SApple OSS Distributions struct { 1955*4f1223e8SApple OSS Distributions mach_msg_header_t header; 1956*4f1223e8SApple OSS Distributions mach_msg_body_t body; 1957*4f1223e8SApple OSS Distributions mach_msg_port_descriptor_t port_descriptor; 1958*4f1223e8SApple OSS Distributions } rcv_msg = { 1959*4f1223e8SApple OSS Distributions .header = 1960*4f1223e8SApple OSS Distributions { 1961*4f1223e8SApple OSS Distributions .msgh_remote_port = MACH_PORT_NULL, 1962*4f1223e8SApple OSS Distributions .msgh_local_port = port, 1963*4f1223e8SApple OSS Distributions .msgh_size = sizeof(rcv_msg), 1964*4f1223e8SApple OSS Distributions }, 1965*4f1223e8SApple OSS Distributions }; 1966*4f1223e8SApple OSS Distributions 1967*4f1223e8SApple OSS Distributions T_LOG("server: starting sync receive\n"); 1968*4f1223e8SApple OSS Distributions 1969*4f1223e8SApple OSS Distributions mach_msg_return_t mr; 1970*4f1223e8SApple OSS Distributions mr = mach_msg(&(rcv_msg.header), 1971*4f1223e8SApple OSS Distributions (MACH_RCV_MSG | MACH_RCV_TIMEOUT), 1972*4f1223e8SApple OSS Distributions 0, 1973*4f1223e8SApple OSS Distributions 4096, 1974*4f1223e8SApple OSS Distributions port, 1975*4f1223e8SApple OSS Distributions 10000, 1976*4f1223e8SApple OSS Distributions MACH_PORT_NULL); 1977*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(mr, "mach_msg() recieve of message from client"); 1978*4f1223e8SApple OSS Distributions 1979*4f1223e8SApple OSS Distributions /* 1980*4f1223e8SApple OSS Distributions * take the stackshot to verify that the stackshot wait info shows our (the server) PID 1981*4f1223e8SApple OSS Distributions * for the scenario where the server has received the message and copied in the send-once right. 1982*4f1223e8SApple OSS Distributions */ 1983*4f1223e8SApple OSS Distributions T_LOG("Taking stackshot for part 2 coverage"); 1984*4f1223e8SApple OSS Distributions check_srp_test("srp", SRP_TEST_PID); 1985*4f1223e8SApple OSS Distributions 1986*4f1223e8SApple OSS Distributions /* cleanup - kill the client */ 1987*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(client_pid, SIGKILL), "killing client"); 1988*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(client_pid, NULL, 0), "waiting for the client to exit"); 1989*4f1223e8SApple OSS Distributions 1990*4f1223e8SApple OSS Distributions // do it again, but using kevents 1991*4f1223e8SApple OSS Distributions T_LOG("Launching client"); 1992*4f1223e8SApple OSS Distributions sp_ret = posix_spawn(&client_pid, client_args[0], NULL, NULL, client_args, NULL); 1993*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", client_args[0], client_pid); 1994*4f1223e8SApple OSS Distributions T_LOG("Spawned client as PID %d", client_pid); 1995*4f1223e8SApple OSS Distributions 1996*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 1997*4f1223e8SApple OSS Distributions T_LOG("Ready to take stackshot, but waiting 1s for the coast to clear"); 1998*4f1223e8SApple OSS Distributions 1999*4f1223e8SApple OSS Distributions /* 2000*4f1223e8SApple OSS Distributions * can_continue indicates the client has signaled us, but we want to make 2001*4f1223e8SApple OSS Distributions * sure they've actually blocked sending their mach message. It's cheesy, but 2002*4f1223e8SApple OSS Distributions * sleep() works for this. 2003*4f1223e8SApple OSS Distributions */ 2004*4f1223e8SApple OSS Distributions sleep(1); 2005*4f1223e8SApple OSS Distributions 2006*4f1223e8SApple OSS Distributions dispatch_mach_t dispatch_mach = dispatch_mach_create(SRP_SERVICE_NAME, machdq, 2007*4f1223e8SApple OSS Distributions ^(dispatch_mach_reason_t reason, 2008*4f1223e8SApple OSS Distributions dispatch_mach_msg_t message, 2009*4f1223e8SApple OSS Distributions mach_error_t error __unused) { 2010*4f1223e8SApple OSS Distributions switch (reason) { 2011*4f1223e8SApple OSS Distributions case DISPATCH_MACH_MESSAGE_RECEIVED: { 2012*4f1223e8SApple OSS Distributions size_t size = 0; 2013*4f1223e8SApple OSS Distributions mach_msg_header_t *msg __unused = dispatch_mach_msg_get_msg(message, &size); 2014*4f1223e8SApple OSS Distributions T_LOG("server: recieved %ld byte message", size); 2015*4f1223e8SApple OSS Distributions check_srp_test("turnstile_port_thread", SRP_TEST_THREAD); 2016*4f1223e8SApple OSS Distributions T_LOG("server: letting client go"); 2017*4f1223e8SApple OSS Distributions // drop the message on the ground, we'll kill the client later 2018*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(can_continue); 2019*4f1223e8SApple OSS Distributions break; 2020*4f1223e8SApple OSS Distributions } 2021*4f1223e8SApple OSS Distributions default: 2022*4f1223e8SApple OSS Distributions break; 2023*4f1223e8SApple OSS Distributions } 2024*4f1223e8SApple OSS Distributions }); 2025*4f1223e8SApple OSS Distributions 2026*4f1223e8SApple OSS Distributions dispatch_mach_connect(dispatch_mach, port, MACH_PORT_NULL, NULL); 2027*4f1223e8SApple OSS Distributions 2028*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 2029*4f1223e8SApple OSS Distributions 2030*4f1223e8SApple OSS Distributions /* cleanup - kill the client */ 2031*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(client_pid, SIGKILL), "killing client"); 2032*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(client_pid, NULL, 0), "waiting for the client to exit"); 2033*4f1223e8SApple OSS Distributions} 2034*4f1223e8SApple OSS Distributions 2035*4f1223e8SApple OSS DistributionsT_HELPER_DECL(throtlled_sp_client, 2036*4f1223e8SApple OSS Distributions "client that uses a connection port to send a message to a server") 2037*4f1223e8SApple OSS Distributions{ 2038*4f1223e8SApple OSS Distributions mach_port_t conn_port, service_port, reply_port, *stash; 2039*4f1223e8SApple OSS Distributions mach_msg_type_number_t stash_cnt = 0; 2040*4f1223e8SApple OSS Distributions 2041*4f1223e8SApple OSS Distributions kern_return_t kr = mach_ports_lookup(mach_task_self(), &stash, &stash_cnt); 2042*4f1223e8SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_ports_lookup"); 2043*4f1223e8SApple OSS Distributions 2044*4f1223e8SApple OSS Distributions service_port = stash[0]; 2045*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(MACH_PORT_VALID(service_port), "valid service port"); 2046*4f1223e8SApple OSS Distributions mig_deallocate((vm_address_t)stash, stash_cnt * sizeof(stash[0])); 2047*4f1223e8SApple OSS Distributions 2048*4f1223e8SApple OSS Distributions mach_port_options_t opts = { 2049*4f1223e8SApple OSS Distributions .flags = MPO_INSERT_SEND_RIGHT 2050*4f1223e8SApple OSS Distributions | MPO_CONNECTION_PORT, 2051*4f1223e8SApple OSS Distributions .service_port_name = service_port, 2052*4f1223e8SApple OSS Distributions }; 2053*4f1223e8SApple OSS Distributions 2054*4f1223e8SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0ull, &conn_port); 2055*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct"); 2056*4f1223e8SApple OSS Distributions 2057*4f1223e8SApple OSS Distributions mach_port_options_t opts2 = { 2058*4f1223e8SApple OSS Distributions .flags = MPO_REPLY_PORT 2059*4f1223e8SApple OSS Distributions }; 2060*4f1223e8SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts2, 0ull, &reply_port); 2061*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct"); 2062*4f1223e8SApple OSS Distributions 2063*4f1223e8SApple OSS Distributions /* XPC-like check-in message */ 2064*4f1223e8SApple OSS Distributions struct { 2065*4f1223e8SApple OSS Distributions mach_msg_header_t header; 2066*4f1223e8SApple OSS Distributions mach_msg_port_descriptor_t recvp; 2067*4f1223e8SApple OSS Distributions mach_msg_port_descriptor_t sendp; 2068*4f1223e8SApple OSS Distributions } checkin_message = { 2069*4f1223e8SApple OSS Distributions .header = 2070*4f1223e8SApple OSS Distributions { 2071*4f1223e8SApple OSS Distributions .msgh_remote_port = service_port, 2072*4f1223e8SApple OSS Distributions .msgh_local_port = MACH_PORT_NULL, 2073*4f1223e8SApple OSS Distributions .msgh_size = sizeof(checkin_message), 2074*4f1223e8SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0), 2075*4f1223e8SApple OSS Distributions }, 2076*4f1223e8SApple OSS Distributions .recvp = 2077*4f1223e8SApple OSS Distributions { 2078*4f1223e8SApple OSS Distributions .type = MACH_MSG_PORT_DESCRIPTOR, 2079*4f1223e8SApple OSS Distributions .name = conn_port, 2080*4f1223e8SApple OSS Distributions .disposition = MACH_MSG_TYPE_MOVE_RECEIVE, 2081*4f1223e8SApple OSS Distributions }, 2082*4f1223e8SApple OSS Distributions .sendp = 2083*4f1223e8SApple OSS Distributions { 2084*4f1223e8SApple OSS Distributions .type = MACH_MSG_PORT_DESCRIPTOR, 2085*4f1223e8SApple OSS Distributions .name = reply_port, 2086*4f1223e8SApple OSS Distributions .disposition = MACH_MSG_TYPE_MAKE_SEND, 2087*4f1223e8SApple OSS Distributions } 2088*4f1223e8SApple OSS Distributions }; 2089*4f1223e8SApple OSS Distributions dispatch_mach_msg_t dmsg = dispatch_mach_msg_create((mach_msg_header_t *)&checkin_message, sizeof(checkin_message), 2090*4f1223e8SApple OSS Distributions DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT, NULL); 2091*4f1223e8SApple OSS Distributions 2092*4f1223e8SApple OSS Distributions dispatch_queue_t machdq = dispatch_queue_create("machqueue", NULL); 2093*4f1223e8SApple OSS Distributions dispatch_mach_t dchannel = dispatch_mach_create(THROTTLED_SERVICE_NAME, machdq, 2094*4f1223e8SApple OSS Distributions ^(dispatch_mach_reason_t reason, 2095*4f1223e8SApple OSS Distributions dispatch_mach_msg_t message __unused, 2096*4f1223e8SApple OSS Distributions mach_error_t error __unused) { 2097*4f1223e8SApple OSS Distributions switch (reason) { 2098*4f1223e8SApple OSS Distributions case DISPATCH_MACH_CONNECTED: 2099*4f1223e8SApple OSS Distributions T_LOG("mach channel connected"); 2100*4f1223e8SApple OSS Distributions break; 2101*4f1223e8SApple OSS Distributions case DISPATCH_MACH_MESSAGE_SENT: 2102*4f1223e8SApple OSS Distributions T_LOG("sent mach message"); 2103*4f1223e8SApple OSS Distributions break; 2104*4f1223e8SApple OSS Distributions default: 2105*4f1223e8SApple OSS Distributions T_ASSERT_FAIL("Unexpected reply to channel reason %lu", reason); 2106*4f1223e8SApple OSS Distributions } 2107*4f1223e8SApple OSS Distributions }); 2108*4f1223e8SApple OSS Distributions dispatch_mach_connect(dchannel, reply_port, service_port, dmsg); 2109*4f1223e8SApple OSS Distributions dispatch_release(dmsg); 2110*4f1223e8SApple OSS Distributions 2111*4f1223e8SApple OSS Distributions struct { 2112*4f1223e8SApple OSS Distributions mach_msg_header_t header; 2113*4f1223e8SApple OSS Distributions uint64_t request_id; 2114*4f1223e8SApple OSS Distributions } request = { 2115*4f1223e8SApple OSS Distributions .header = 2116*4f1223e8SApple OSS Distributions { 2117*4f1223e8SApple OSS Distributions .msgh_size = sizeof(request), 2118*4f1223e8SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE), 2119*4f1223e8SApple OSS Distributions }, 2120*4f1223e8SApple OSS Distributions .request_id = 1, 2121*4f1223e8SApple OSS Distributions }; 2122*4f1223e8SApple OSS Distributions dispatch_mach_msg_t dmsg2 = dispatch_mach_msg_create((mach_msg_header_t *)&request, sizeof(request), 2123*4f1223e8SApple OSS Distributions DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT, NULL); 2124*4f1223e8SApple OSS Distributions 2125*4f1223e8SApple OSS Distributions dispatch_mach_reason_t reason; 2126*4f1223e8SApple OSS Distributions mach_error_t error; 2127*4f1223e8SApple OSS Distributions 2128*4f1223e8SApple OSS Distributions /* send the check-in message and the request message */ 2129*4f1223e8SApple OSS Distributions dispatch_mach_msg_t dreply = dispatch_mach_send_with_result_and_wait_for_reply(dchannel, 2130*4f1223e8SApple OSS Distributions dmsg2, 0, DISPATCH_MACH_SEND_DEFAULT, &reason, &error); 2131*4f1223e8SApple OSS Distributions dispatch_release(dmsg2); 2132*4f1223e8SApple OSS Distributions 2133*4f1223e8SApple OSS Distributions /* not expected to execute as parent will SIGKILL client */ 2134*4f1223e8SApple OSS Distributions T_ASSERT_FAIL("client process exiting after receiving %s reply", dreply ? "non-null" : "null"); 2135*4f1223e8SApple OSS Distributions} 2136*4f1223e8SApple OSS Distributions 2137*4f1223e8SApple OSS Distributionsstatic void 2138*4f1223e8SApple OSS Distributionscheck_throttled_sp(const char *test_name, uint64_t context, bool is_throttled) 2139*4f1223e8SApple OSS Distributions{ 2140*4f1223e8SApple OSS Distributions struct scenario scenario = { 2141*4f1223e8SApple OSS Distributions .name = test_name, 2142*4f1223e8SApple OSS Distributions .quiet = false, 2143*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_THREAD_WAITINFO | STACKSHOT_KCDATA_FORMAT), 2144*4f1223e8SApple OSS Distributions }; 2145*4f1223e8SApple OSS Distributions 2146*4f1223e8SApple OSS Distributions T_LOG("taking stackshot %s", test_name); 2147*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 2148*4f1223e8SApple OSS Distributions parse_stackshot(PARSE_STACKSHOT_THROTTLED_SP, ssbuf, sslen, 2149*4f1223e8SApple OSS Distributions @{sp_throttled_expected_ctxt_key: @(context), 2150*4f1223e8SApple OSS Distributions sp_throttled_expect_flag: @(is_throttled)}); 2151*4f1223e8SApple OSS Distributions }); 2152*4f1223e8SApple OSS Distributions} 2153*4f1223e8SApple OSS Distributions 2154*4f1223e8SApple OSS Distributions/* Take stackshot when a client is blocked on the service port of a process, in the scenario when 2155*4f1223e8SApple OSS Distributions * the process with the receive right for the service port is: 2156*4f1223e8SApple OSS Distributions * (a) Monitoring the service port using kevents 2157*4f1223e8SApple OSS Distributions * (b) Not monitoring the service port 2158*4f1223e8SApple OSS Distributions */ 2159*4f1223e8SApple OSS DistributionsT_DECL(throttled_sp, 2160*4f1223e8SApple OSS Distributions "test that service port throttled flag is propagated to the stackshot correctly", T_META_TAG_VM_PREFERRED) 2161*4f1223e8SApple OSS Distributions{ 2162*4f1223e8SApple OSS Distributions mach_port_t service_port; 2163*4f1223e8SApple OSS Distributions __block dispatch_semaphore_t can_continue = dispatch_semaphore_create(0); 2164*4f1223e8SApple OSS Distributions 2165*4f1223e8SApple OSS Distributions char path[PATH_MAX]; 2166*4f1223e8SApple OSS Distributions uint32_t path_size = sizeof(path); 2167*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath"); 2168*4f1223e8SApple OSS Distributions char *client_args[] = { path, "-n", "throtlled_sp_client", NULL }; 2169*4f1223e8SApple OSS Distributions 2170*4f1223e8SApple OSS Distributions __block uint64_t thread_id = 0; 2171*4f1223e8SApple OSS Distributions pid_t client_pid; 2172*4f1223e8SApple OSS Distributions int mark_throttled; 2173*4f1223e8SApple OSS Distributions 2174*4f1223e8SApple OSS Distributions struct mach_service_port_info sp_info = {}; 2175*4f1223e8SApple OSS Distributions strcpy(sp_info.mspi_string_name, THROTTLED_SERVICE_NAME); 2176*4f1223e8SApple OSS Distributions sp_info.mspi_domain_type = (uint8_t)1; 2177*4f1223e8SApple OSS Distributions kern_return_t kr; 2178*4f1223e8SApple OSS Distributions 2179*4f1223e8SApple OSS Distributions mach_port_options_t opts = { 2180*4f1223e8SApple OSS Distributions .flags = MPO_SERVICE_PORT | MPO_INSERT_SEND_RIGHT | MPO_CONTEXT_AS_GUARD | MPO_STRICT | MPO_TEMPOWNER, 2181*4f1223e8SApple OSS Distributions .service_port_info = &sp_info, 2182*4f1223e8SApple OSS Distributions }; 2183*4f1223e8SApple OSS Distributions 2184*4f1223e8SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0ull, &service_port); 2185*4f1223e8SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct %u", service_port); 2186*4f1223e8SApple OSS Distributions 2187*4f1223e8SApple OSS Distributions /* Setup a dispatch source to monitor the service port similar to how launchd does. */ 2188*4f1223e8SApple OSS Distributions dispatch_queue_t machdq = dispatch_queue_create("machqueue", NULL); 2189*4f1223e8SApple OSS Distributions dispatch_source_t mach_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, service_port, 2190*4f1223e8SApple OSS Distributions DISPATCH_MACH_RECV_SYNC_PEEK, machdq); 2191*4f1223e8SApple OSS Distributions dispatch_source_set_event_handler(mach_src, ^{ 2192*4f1223e8SApple OSS Distributions pthread_threadid_np(NULL, &thread_id); 2193*4f1223e8SApple OSS Distributions dispatch_semaphore_signal(can_continue); 2194*4f1223e8SApple OSS Distributions }); 2195*4f1223e8SApple OSS Distributions dispatch_activate(mach_src); 2196*4f1223e8SApple OSS Distributions 2197*4f1223e8SApple OSS Distributions /* Stash the port in task to make sure child also gets it */ 2198*4f1223e8SApple OSS Distributions kr = mach_ports_register(mach_task_self(), &service_port, 1); 2199*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_ports_register service port"); 2200*4f1223e8SApple OSS Distributions 2201*4f1223e8SApple OSS Distributions mark_throttled = 1; 2202*4f1223e8SApple OSS Distributions kr = mach_port_set_attributes(mach_task_self(), service_port, MACH_PORT_SERVICE_THROTTLED, (mach_port_info_t)(&mark_throttled), 2203*4f1223e8SApple OSS Distributions MACH_PORT_SERVICE_THROTTLED_COUNT); 2204*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mark service port as throttled"); 2205*4f1223e8SApple OSS Distributions 2206*4f1223e8SApple OSS Distributions int rc = posix_spawn(&client_pid, client_args[0], NULL, NULL, client_args, NULL); 2207*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(rc, "spawned process '%s' with PID %d", client_args[0], client_pid); 2208*4f1223e8SApple OSS Distributions T_LOG("Spawned client as PID %d", client_pid); 2209*4f1223e8SApple OSS Distributions 2210*4f1223e8SApple OSS Distributions dispatch_semaphore_wait(can_continue, DISPATCH_TIME_FOREVER); 2211*4f1223e8SApple OSS Distributions 2212*4f1223e8SApple OSS Distributions /* The service port has received the check-in message. Take stackshot for scenario (a). */ 2213*4f1223e8SApple OSS Distributions check_throttled_sp("throttled_service_port_monitored", thread_id, true); 2214*4f1223e8SApple OSS Distributions 2215*4f1223e8SApple OSS Distributions /* This simulates a throttled spawn when the service port is no longer monitored. */ 2216*4f1223e8SApple OSS Distributions dispatch_source_cancel(mach_src); 2217*4f1223e8SApple OSS Distributions 2218*4f1223e8SApple OSS Distributions /* Take stackshot for scenario (b) */ 2219*4f1223e8SApple OSS Distributions check_throttled_sp("throttled_service_port_unmonitored", (uint64_t)getpid(), true); 2220*4f1223e8SApple OSS Distributions 2221*4f1223e8SApple OSS Distributions mark_throttled = 0; 2222*4f1223e8SApple OSS Distributions kr = mach_port_set_attributes(mach_task_self(), service_port, MACH_PORT_SERVICE_THROTTLED, (mach_port_info_t)(&mark_throttled), 2223*4f1223e8SApple OSS Distributions MACH_PORT_SERVICE_THROTTLED_COUNT); 2224*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "unmark service port as throttled"); 2225*4f1223e8SApple OSS Distributions 2226*4f1223e8SApple OSS Distributions /* Throttled flag should not be set when the port is not throttled. */ 2227*4f1223e8SApple OSS Distributions check_throttled_sp("unthrottled_service_port_unmonitored", (uint64_t)getpid(), false); 2228*4f1223e8SApple OSS Distributions 2229*4f1223e8SApple OSS Distributions /* cleanup - kill the client */ 2230*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(client_pid, SIGKILL), "killing client"); 2231*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(client_pid, NULL, 0), "waiting for the client to exit"); 2232*4f1223e8SApple OSS Distributions} 2233*4f1223e8SApple OSS Distributions 2234*4f1223e8SApple OSS Distributions 2235*4f1223e8SApple OSS Distributionschar *const clpcctrl_path = "/usr/local/bin/clpcctrl"; 2236*4f1223e8SApple OSS Distributions 2237*4f1223e8SApple OSS Distributionsstatic void 2238*4f1223e8SApple OSS Distributionsrun_clpcctrl(char *const argv[]) { 2239*4f1223e8SApple OSS Distributions posix_spawnattr_t sattr; 2240*4f1223e8SApple OSS Distributions pid_t pid; 2241*4f1223e8SApple OSS Distributions int wstatus; 2242*4f1223e8SApple OSS Distributions 2243*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL), "spawn clpcctrl"); 2244*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &wstatus, 0), "wait for clpcctrl"); 2245*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(WIFEXITED(wstatus), "clpcctrl exited normally"); 2246*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(WEXITSTATUS(wstatus), "clpcctrl exited successfully"); 2247*4f1223e8SApple OSS Distributions 2248*4f1223e8SApple OSS Distributions uint64_t sched_recommended_cores = 1; 2249*4f1223e8SApple OSS Distributions size_t sched_recommended_cores_sz = sizeof(uint64_t); 2250*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS( 2251*4f1223e8SApple OSS Distributions sysctlbyname("kern.sched_recommended_cores", &sched_recommended_cores, &sched_recommended_cores_sz, NULL, 0), 2252*4f1223e8SApple OSS Distributions "get kern.sched_recommended_cores"); 2253*4f1223e8SApple OSS Distributions T_LOG("Recommended cores: 0x%llx", sched_recommended_cores); 2254*4f1223e8SApple OSS Distributions} 2255*4f1223e8SApple OSS Distributions 2256*4f1223e8SApple OSS Distributionsstatic void 2257*4f1223e8SApple OSS Distributionsrestore_clpcctrl() { 2258*4f1223e8SApple OSS Distributions run_clpcctrl((char *const []) { clpcctrl_path, "-d", NULL }); 2259*4f1223e8SApple OSS Distributions} 2260*4f1223e8SApple OSS Distributions 2261*4f1223e8SApple OSS Distributions#define CLUSTER_TYPE_SMP 0 2262*4f1223e8SApple OSS Distributions#define CLUSTER_TYPE_E 1 2263*4f1223e8SApple OSS Distributions#define CLUSTER_TYPE_P 2 2264*4f1223e8SApple OSS Distributions 2265*4f1223e8SApple OSS Distributionsvoid test_stackshot_cpu_info(void *ssbuf, size_t sslen, int exp_cpus, NSArray *exp_cluster_types) { 2266*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2267*4f1223e8SApple OSS Distributions bool seen = false; 2268*4f1223e8SApple OSS Distributions int singlethread_override = 0; 2269*4f1223e8SApple OSS Distributions size_t singlethread_override_sz = sizeof(singlethread_override); 2270*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS( 2271*4f1223e8SApple OSS Distributions sysctlbyname("kern.stackshot_single_thread", &singlethread_override, &singlethread_override_sz, NULL, 0), 2272*4f1223e8SApple OSS Distributions "get kern.stackshot_single_thread"); 2273*4f1223e8SApple OSS Distributions if (singlethread_override) { 2274*4f1223e8SApple OSS Distributions T_LOG("skipping cpu count/type check due to single-thread override (kern.stackshot_single_thread=1)"); 2275*4f1223e8SApple OSS Distributions return; 2276*4f1223e8SApple OSS Distributions } 2277*4f1223e8SApple OSS Distributions 2278*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2279*4f1223e8SApple OSS Distributions if ((kcdata_iter_type(iter) != KCDATA_TYPE_ARRAY) || (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_LATENCY_INFO_CPU)) { 2280*4f1223e8SApple OSS Distributions continue; 2281*4f1223e8SApple OSS Distributions } 2282*4f1223e8SApple OSS Distributions 2283*4f1223e8SApple OSS Distributions seen = true; 2284*4f1223e8SApple OSS Distributions 2285*4f1223e8SApple OSS Distributions /* Check ncpus */ 2286*4f1223e8SApple OSS Distributions int ncpus = kcdata_iter_array_elem_count(iter); 2287*4f1223e8SApple OSS Distributions if (exp_cpus != -1) { 2288*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(exp_cpus, ncpus, "Expected number of CPUs matches number of CPUs used for stackshot"); 2289*4f1223e8SApple OSS Distributions } 2290*4f1223e8SApple OSS Distributions 2291*4f1223e8SApple OSS Distributions if (exp_cluster_types == nil) { 2292*4f1223e8SApple OSS Distributions continue; 2293*4f1223e8SApple OSS Distributions } 2294*4f1223e8SApple OSS Distributions 2295*4f1223e8SApple OSS Distributions /* Check cluster types */ 2296*4f1223e8SApple OSS Distributions struct stackshot_latency_cpu *latencies = (struct stackshot_latency_cpu *) kcdata_iter_payload(iter); 2297*4f1223e8SApple OSS Distributions for (int i = 0; i < ncpus; i++) { 2298*4f1223e8SApple OSS Distributions NSNumber *cluster_type = [NSNumber numberWithInt:latencies[i].cluster_type]; 2299*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE([exp_cluster_types containsObject:cluster_type], "Type of CPU cluster in expected CPU cluster types"); 2300*4f1223e8SApple OSS Distributions } 2301*4f1223e8SApple OSS Distributions } 2302*4f1223e8SApple OSS Distributions 2303*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(seen || !is_development_kernel(), "Seen CPU latency info or is release kernel"); 2304*4f1223e8SApple OSS Distributions} 2305*4f1223e8SApple OSS Distributions 2306*4f1223e8SApple OSS Distributionsvoid test_stackshot_with_clpcctrl(char *const name, char *const argv[], int exp_cpus, NSArray *exp_cluster_types) { 2307*4f1223e8SApple OSS Distributions T_LOG("Stackshot CLPC scenario %s", name); 2308*4f1223e8SApple OSS Distributions run_clpcctrl(argv); 2309*4f1223e8SApple OSS Distributions struct scenario scenario = { 2310*4f1223e8SApple OSS Distributions .name = name, 2311*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_KCDATA_FORMAT | STACKSHOT_SAVE_LOADINFO | 2312*4f1223e8SApple OSS Distributions STACKSHOT_THREAD_WAITINFO | STACKSHOT_GET_GLOBAL_MEM_STATS) 2313*4f1223e8SApple OSS Distributions }; 2314*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 2315*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 2316*4f1223e8SApple OSS Distributions test_stackshot_cpu_info(ssbuf, sslen, exp_cpus, exp_cluster_types); 2317*4f1223e8SApple OSS Distributions }); 2318*4f1223e8SApple OSS Distributions} 2319*4f1223e8SApple OSS Distributions 2320*4f1223e8SApple OSS DistributionsT_DECL(core_masks, 2321*4f1223e8SApple OSS Distributions "test that stackshot works under various core masks on ARM systems", 2322*4f1223e8SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm64", 1), 2323*4f1223e8SApple OSS Distributions T_META_REQUIRES_SYSCTL_NE("kern.kasan.available", 1), /* rdar://115577993 */ 2324*4f1223e8SApple OSS Distributions XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL, 2325*4f1223e8SApple OSS Distributions T_META_REQUIRE_NOT_VIRTUALIZED, 2326*4f1223e8SApple OSS Distributions T_META_RUN_CONCURRENTLY(false), 2327*4f1223e8SApple OSS Distributions T_META_TAG_VM_NOT_ELIGIBLE, 2328*4f1223e8SApple OSS Distributions T_META_ENABLED(!TARGET_OS_VISION)) // disable for visionOS: device may not be stable with many cores masked off (127904530) 2329*4f1223e8SApple OSS Distributions{ 2330*4f1223e8SApple OSS Distributions /* 2331*4f1223e8SApple OSS Distributions * Make sure we're not in a release kernel 2332*4f1223e8SApple OSS Distributions * (cannot check with T_META; only one sysctl T_META at a time will work) 2333*4f1223e8SApple OSS Distributions */ 2334*4f1223e8SApple OSS Distributions if (!is_development_kernel()) { 2335*4f1223e8SApple OSS Distributions T_SKIP("test was not run because kernel is release; cannot set core masks"); 2336*4f1223e8SApple OSS Distributions return; 2337*4f1223e8SApple OSS Distributions } 2338*4f1223e8SApple OSS Distributions 2339*4f1223e8SApple OSS Distributions /* 2340*4f1223e8SApple OSS Distributions * rdar://115577993 - CLPC compiles as release in KASAN-variant builds, 2341*4f1223e8SApple OSS Distributions * preventing clpcctrl from working. For now, skip this. (Cannot check 2342*4f1223e8SApple OSS Distributions * with T_META; only one sysctl T_META at a time will work) 2343*4f1223e8SApple OSS Distributions */ 2344*4f1223e8SApple OSS Distributions int kasan_avail = 0; 2345*4f1223e8SApple OSS Distributions size_t kasan_avail_sz = sizeof(kasan_avail); 2346*4f1223e8SApple OSS Distributions sysctlbyname("kern.kasan.available", &kasan_avail, &kasan_avail_sz, NULL, 0); 2347*4f1223e8SApple OSS Distributions if (kasan_avail) { 2348*4f1223e8SApple OSS Distributions T_SKIP("test was not run because kernel is KASAN; cannot set core masks (see rdar://115577993)"); 2349*4f1223e8SApple OSS Distributions return; 2350*4f1223e8SApple OSS Distributions } 2351*4f1223e8SApple OSS Distributions 2352*4f1223e8SApple OSS Distributions 2353*4f1223e8SApple OSS Distributions T_ATEND(restore_clpcctrl); 2354*4f1223e8SApple OSS Distributions 2355*4f1223e8SApple OSS Distributions /* Test with 1 and 2 CPUs for basic functionality */ 2356*4f1223e8SApple OSS Distributions test_stackshot_with_clpcctrl( 2357*4f1223e8SApple OSS Distributions "core_masks_1cpu", (char *const[]) {clpcctrl_path, "-c", "1", NULL}, 2358*4f1223e8SApple OSS Distributions 1, nil); 2359*4f1223e8SApple OSS Distributions 2360*4f1223e8SApple OSS Distributions test_stackshot_with_clpcctrl( 2361*4f1223e8SApple OSS Distributions "core_masks_2cpus", (char *const[]) {clpcctrl_path, "-c", "2", NULL}, 2362*4f1223e8SApple OSS Distributions 2, nil); 2363*4f1223e8SApple OSS Distributions 2364*4f1223e8SApple OSS Distributions /* Check nperflevels to see if we're on an AMP system */ 2365*4f1223e8SApple OSS Distributions int nperflevels = 1; 2366*4f1223e8SApple OSS Distributions size_t nperflevels_sz = sizeof(int); 2367*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS( 2368*4f1223e8SApple OSS Distributions sysctlbyname("hw.nperflevels", &nperflevels, &nperflevels_sz, NULL, 0), 2369*4f1223e8SApple OSS Distributions "get hw.nperflevels"); 2370*4f1223e8SApple OSS Distributions if (nperflevels == 1) { 2371*4f1223e8SApple OSS Distributions T_LOG("On SMP system, skipping stackshot core_masks AMP tests"); 2372*4f1223e8SApple OSS Distributions return; 2373*4f1223e8SApple OSS Distributions } 2374*4f1223e8SApple OSS Distributions 2375*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(nperflevels, 2, "nperflevels is 1 or 2"); 2376*4f1223e8SApple OSS Distributions T_LOG("On AMP system, performing stackshot core_masks AMP tests"); 2377*4f1223e8SApple OSS Distributions 2378*4f1223e8SApple OSS Distributions /* Perform AMP tests with different cluster types active */ 2379*4f1223e8SApple OSS Distributions test_stackshot_with_clpcctrl( 2380*4f1223e8SApple OSS Distributions "core_masks_amp_allcpus", 2381*4f1223e8SApple OSS Distributions (char *const[]) {clpcctrl_path, "-C", "all", NULL}, 2382*4f1223e8SApple OSS Distributions -1, @[@CLUSTER_TYPE_E, @CLUSTER_TYPE_P]); 2383*4f1223e8SApple OSS Distributions 2384*4f1223e8SApple OSS Distributions test_stackshot_with_clpcctrl( 2385*4f1223e8SApple OSS Distributions "core_masks_amp_ecpus", 2386*4f1223e8SApple OSS Distributions (char *const[]) {clpcctrl_path, "-C", "e", NULL}, 2387*4f1223e8SApple OSS Distributions -1, @[@CLUSTER_TYPE_E]); 2388*4f1223e8SApple OSS Distributions 2389*4f1223e8SApple OSS Distributions test_stackshot_with_clpcctrl( 2390*4f1223e8SApple OSS Distributions "core_masks_amp_pcpus", 2391*4f1223e8SApple OSS Distributions (char *const[]) {clpcctrl_path, "-C", "p", NULL}, 2392*4f1223e8SApple OSS Distributions -1, @[@CLUSTER_TYPE_P]); 2393*4f1223e8SApple OSS Distributions} 2394*4f1223e8SApple OSS Distributions 2395*4f1223e8SApple OSS Distributions#pragma mark performance tests 2396*4f1223e8SApple OSS Distributions 2397*4f1223e8SApple OSS Distributions#define SHOULD_REUSE_SIZE_HINT 0x01 2398*4f1223e8SApple OSS Distributions#define SHOULD_USE_DELTA 0x02 2399*4f1223e8SApple OSS Distributions#define SHOULD_TARGET_SELF 0x04 2400*4f1223e8SApple OSS Distributions 2401*4f1223e8SApple OSS Distributionsstatic void 2402*4f1223e8SApple OSS Distributionsstackshot_perf(unsigned int options) 2403*4f1223e8SApple OSS Distributions{ 2404*4f1223e8SApple OSS Distributions struct scenario scenario = { 2405*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS 2406*4f1223e8SApple OSS Distributions | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT), 2407*4f1223e8SApple OSS Distributions }; 2408*4f1223e8SApple OSS Distributions 2409*4f1223e8SApple OSS Distributions dt_stat_t size = dt_stat_create("bytes", "size"); 2410*4f1223e8SApple OSS Distributions dt_stat_time_t duration = dt_stat_time_create("duration"); 2411*4f1223e8SApple OSS Distributions scenario.timer = duration; 2412*4f1223e8SApple OSS Distributions 2413*4f1223e8SApple OSS Distributions if (options & SHOULD_TARGET_SELF) { 2414*4f1223e8SApple OSS Distributions scenario.target_pid = getpid(); 2415*4f1223e8SApple OSS Distributions } 2416*4f1223e8SApple OSS Distributions 2417*4f1223e8SApple OSS Distributions while (!dt_stat_stable(duration) || !dt_stat_stable(size)) { 2418*4f1223e8SApple OSS Distributions __block uint64_t last_time = 0; 2419*4f1223e8SApple OSS Distributions __block uint32_t size_hint = 0; 2420*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 2421*4f1223e8SApple OSS Distributions dt_stat_add(size, (double)sslen); 2422*4f1223e8SApple OSS Distributions last_time = stackshot_timestamp(ssbuf, sslen); 2423*4f1223e8SApple OSS Distributions size_hint = (uint32_t)sslen; 2424*4f1223e8SApple OSS Distributions }); 2425*4f1223e8SApple OSS Distributions if (options & SHOULD_USE_DELTA) { 2426*4f1223e8SApple OSS Distributions scenario.since_timestamp = last_time; 2427*4f1223e8SApple OSS Distributions scenario.flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT; 2428*4f1223e8SApple OSS Distributions } 2429*4f1223e8SApple OSS Distributions if (options & SHOULD_REUSE_SIZE_HINT) { 2430*4f1223e8SApple OSS Distributions scenario.size_hint = size_hint; 2431*4f1223e8SApple OSS Distributions } 2432*4f1223e8SApple OSS Distributions } 2433*4f1223e8SApple OSS Distributions 2434*4f1223e8SApple OSS Distributions dt_stat_finalize(duration); 2435*4f1223e8SApple OSS Distributions dt_stat_finalize(size); 2436*4f1223e8SApple OSS Distributions} 2437*4f1223e8SApple OSS Distributions 2438*4f1223e8SApple OSS Distributionsstatic void 2439*4f1223e8SApple OSS Distributionsstackshot_flag_perf_noclobber(uint64_t flag, char *flagname) 2440*4f1223e8SApple OSS Distributions{ 2441*4f1223e8SApple OSS Distributions struct scenario scenario = { 2442*4f1223e8SApple OSS Distributions .quiet = true, 2443*4f1223e8SApple OSS Distributions .flags = (flag | STACKSHOT_KCDATA_FORMAT), 2444*4f1223e8SApple OSS Distributions }; 2445*4f1223e8SApple OSS Distributions 2446*4f1223e8SApple OSS Distributions dt_stat_t duration = dt_stat_create("nanoseconds per thread", "%s_duration", flagname); 2447*4f1223e8SApple OSS Distributions dt_stat_t size = dt_stat_create("bytes per thread", "%s_size", flagname); 2448*4f1223e8SApple OSS Distributions T_LOG("Testing \"%s\" = 0x%" PRIx64, flagname, flag); 2449*4f1223e8SApple OSS Distributions 2450*4f1223e8SApple OSS Distributions while (!dt_stat_stable(duration) || !dt_stat_stable(size)) { 2451*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 2452*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2453*4f1223e8SApple OSS Distributions unsigned long no_threads = 0; 2454*4f1223e8SApple OSS Distributions mach_timebase_info_data_t timebase = {0, 0}; 2455*4f1223e8SApple OSS Distributions uint64_t stackshot_duration = 0; 2456*4f1223e8SApple OSS Distributions int found = 0; 2457*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "stackshot buffer"); 2458*4f1223e8SApple OSS Distributions 2459*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2460*4f1223e8SApple OSS Distributions switch(kcdata_iter_type(iter)) { 2461*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_THREAD_SNAPSHOT: { 2462*4f1223e8SApple OSS Distributions found |= 1; 2463*4f1223e8SApple OSS Distributions no_threads ++; 2464*4f1223e8SApple OSS Distributions break; 2465*4f1223e8SApple OSS Distributions } 2466*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_STACKSHOT_DURATION: { 2467*4f1223e8SApple OSS Distributions struct stackshot_duration *ssd = kcdata_iter_payload(iter); 2468*4f1223e8SApple OSS Distributions stackshot_duration = ssd->stackshot_duration; 2469*4f1223e8SApple OSS Distributions found |= 2; 2470*4f1223e8SApple OSS Distributions break; 2471*4f1223e8SApple OSS Distributions } 2472*4f1223e8SApple OSS Distributions case KCDATA_TYPE_TIMEBASE: { 2473*4f1223e8SApple OSS Distributions found |= 4; 2474*4f1223e8SApple OSS Distributions mach_timebase_info_data_t *tb = kcdata_iter_payload(iter); 2475*4f1223e8SApple OSS Distributions memcpy(&timebase, tb, sizeof(timebase)); 2476*4f1223e8SApple OSS Distributions break; 2477*4f1223e8SApple OSS Distributions } 2478*4f1223e8SApple OSS Distributions } 2479*4f1223e8SApple OSS Distributions } 2480*4f1223e8SApple OSS Distributions 2481*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(found, 0x7, "found everything needed"); 2482*4f1223e8SApple OSS Distributions 2483*4f1223e8SApple OSS Distributions uint64_t ns = (stackshot_duration * timebase.numer) / timebase.denom; 2484*4f1223e8SApple OSS Distributions uint64_t per_thread_ns = ns / no_threads; 2485*4f1223e8SApple OSS Distributions uint64_t per_thread_size = sslen / no_threads; 2486*4f1223e8SApple OSS Distributions 2487*4f1223e8SApple OSS Distributions dt_stat_add(duration, per_thread_ns); 2488*4f1223e8SApple OSS Distributions dt_stat_add(size, per_thread_size); 2489*4f1223e8SApple OSS Distributions }); 2490*4f1223e8SApple OSS Distributions } 2491*4f1223e8SApple OSS Distributions 2492*4f1223e8SApple OSS Distributions dt_stat_finalize(duration); 2493*4f1223e8SApple OSS Distributions dt_stat_finalize(size); 2494*4f1223e8SApple OSS Distributions} 2495*4f1223e8SApple OSS Distributions 2496*4f1223e8SApple OSS Distributionsstatic void 2497*4f1223e8SApple OSS Distributionsstackshot_flag_perf(uint64_t flag, char *flagname) 2498*4f1223e8SApple OSS Distributions{ 2499*4f1223e8SApple OSS Distributions /* 2500*4f1223e8SApple OSS Distributions * STACKSHOT_NO_IO_STATS disables data collection, so set it for 2501*4f1223e8SApple OSS Distributions * more accurate perfdata collection. 2502*4f1223e8SApple OSS Distributions */ 2503*4f1223e8SApple OSS Distributions flag |= STACKSHOT_NO_IO_STATS; 2504*4f1223e8SApple OSS Distributions 2505*4f1223e8SApple OSS Distributions stackshot_flag_perf_noclobber(flag, flagname); 2506*4f1223e8SApple OSS Distributions} 2507*4f1223e8SApple OSS Distributions 2508*4f1223e8SApple OSS Distributions 2509*4f1223e8SApple OSS DistributionsT_DECL(flag_perf, "test stackshot performance with different flags set", T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2510*4f1223e8SApple OSS Distributions{ 2511*4f1223e8SApple OSS Distributions stackshot_flag_perf_noclobber(STACKSHOT_NO_IO_STATS, "baseline"); 2512*4f1223e8SApple OSS Distributions stackshot_flag_perf_noclobber(0, "io_stats"); 2513*4f1223e8SApple OSS Distributions 2514*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_THREAD_WAITINFO, "thread_waitinfo"); 2515*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_GET_DQ, "get_dq"); 2516*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_LOADINFO, "save_loadinfo"); 2517*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_GET_GLOBAL_MEM_STATS, "get_global_mem_stats"); 2518*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_KEXT_LOADINFO, "save_kext_loadinfo"); 2519*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_IMP_DONATION_PIDS, "save_imp_donation_pids"); 2520*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_ENABLE_BT_FAULTING, "enable_bt_faulting"); 2521*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT, "collect_sharedcache_layout"); 2522*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_ENABLE_UUID_FAULTING, "enable_uuid_faulting"); 2523*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_THREAD_GROUP, "thread_group"); 2524*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SAVE_JETSAM_COALITIONS, "save_jetsam_coalitions"); 2525*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_INSTRS_CYCLES, "instrs_cycles"); 2526*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_ASID, "asid"); 2527*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_EXCLAVES, "all_exclaves"); 2528*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_EXCLAVES | STACKSHOT_ASID, "all_exclaves_and_asid"); 2529*4f1223e8SApple OSS Distributions stackshot_flag_perf(STACKSHOT_SKIP_EXCLAVES, "skip_exclaves"); 2530*4f1223e8SApple OSS Distributions} 2531*4f1223e8SApple OSS Distributions 2532*4f1223e8SApple OSS DistributionsT_DECL(perf_no_size_hint, "test stackshot performance with no size hint", 2533*4f1223e8SApple OSS Distributions T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2534*4f1223e8SApple OSS Distributions{ 2535*4f1223e8SApple OSS Distributions stackshot_perf(0); 2536*4f1223e8SApple OSS Distributions} 2537*4f1223e8SApple OSS Distributions 2538*4f1223e8SApple OSS DistributionsT_DECL(perf_size_hint, "test stackshot performance with size hint", 2539*4f1223e8SApple OSS Distributions T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2540*4f1223e8SApple OSS Distributions{ 2541*4f1223e8SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT); 2542*4f1223e8SApple OSS Distributions} 2543*4f1223e8SApple OSS Distributions 2544*4f1223e8SApple OSS DistributionsT_DECL(perf_process, "test stackshot performance targeted at process", 2545*4f1223e8SApple OSS Distributions T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2546*4f1223e8SApple OSS Distributions{ 2547*4f1223e8SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_TARGET_SELF); 2548*4f1223e8SApple OSS Distributions} 2549*4f1223e8SApple OSS Distributions 2550*4f1223e8SApple OSS DistributionsT_DECL(perf_delta, "test delta stackshot performance", 2551*4f1223e8SApple OSS Distributions T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2552*4f1223e8SApple OSS Distributions{ 2553*4f1223e8SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA); 2554*4f1223e8SApple OSS Distributions} 2555*4f1223e8SApple OSS Distributions 2556*4f1223e8SApple OSS DistributionsT_DECL(perf_delta_no_exclaves, "test delta stackshot performance without Exclaves", 2557*4f1223e8SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.exclaves_status", 1), 2558*4f1223e8SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.exclaves_inspection_status", 1), 2559*4f1223e8SApple OSS Distributions T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2560*4f1223e8SApple OSS Distributions{ 2561*4f1223e8SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA | STACKSHOT_SKIP_EXCLAVES); 2562*4f1223e8SApple OSS Distributions} 2563*4f1223e8SApple OSS Distributions 2564*4f1223e8SApple OSS DistributionsT_DECL(perf_delta_process, "test delta stackshot performance targeted at a process", 2565*4f1223e8SApple OSS Distributions T_META_TAG_PERF, T_META_TAG_VM_NOT_ELIGIBLE) 2566*4f1223e8SApple OSS Distributions{ 2567*4f1223e8SApple OSS Distributions stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA | SHOULD_TARGET_SELF); 2568*4f1223e8SApple OSS Distributions} 2569*4f1223e8SApple OSS Distributions 2570*4f1223e8SApple OSS DistributionsT_DECL(stackshot_entitlement_report_test, "test stackshot entitlement report", T_META_TAG_VM_PREFERRED) 2571*4f1223e8SApple OSS Distributions{ 2572*4f1223e8SApple OSS Distributions int sysctlValue = 1; 2573*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS( 2574*4f1223e8SApple OSS Distributions sysctlbyname("debug.stackshot_entitlement_send_batch", NULL, NULL, &sysctlValue, sizeof(sysctlValue)), 2575*4f1223e8SApple OSS Distributions "set debug.stackshot_entitlement_send_batch=1"); 2576*4f1223e8SApple OSS Distributions // having a way to verify that the coreanalytics event was received would be even better 2577*4f1223e8SApple OSS Distributions // See rdar://74197197 2578*4f1223e8SApple OSS Distributions T_PASS("entitlement test ran"); 2579*4f1223e8SApple OSS Distributions} 2580*4f1223e8SApple OSS Distributions 2581*4f1223e8SApple OSS Distributionsstatic void 2582*4f1223e8SApple OSS Distributionsexpect_os_build_version_in_stackshot(void *ssbuf, size_t sslen) 2583*4f1223e8SApple OSS Distributions{ 2584*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2585*4f1223e8SApple OSS Distributions 2586*4f1223e8SApple OSS Distributions bool saw_os_build_version = false; 2587*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 2588*4f1223e8SApple OSS Distributions 2589*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2590*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 2591*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_OS_BUILD_VERSION: 2592*4f1223e8SApple OSS Distributions saw_os_build_version = true; 2593*4f1223e8SApple OSS Distributions T_LOG("Found os build version in stackshot: %s", kcdata_iter_payload(iter)); 2594*4f1223e8SApple OSS Distributions return; 2595*4f1223e8SApple OSS Distributions 2596*4f1223e8SApple OSS Distributions default: 2597*4f1223e8SApple OSS Distributions break; 2598*4f1223e8SApple OSS Distributions } 2599*4f1223e8SApple OSS Distributions } 2600*4f1223e8SApple OSS Distributions 2601*4f1223e8SApple OSS Distributions T_ASSERT_FAIL("didn't see os build version in stackshot"); 2602*4f1223e8SApple OSS Distributions} 2603*4f1223e8SApple OSS Distributions 2604*4f1223e8SApple OSS DistributionsT_DECL(os_build_version, "test stackshot contains os build version", T_META_TAG_VM_PREFERRED) 2605*4f1223e8SApple OSS Distributions{ 2606*4f1223e8SApple OSS Distributions 2607*4f1223e8SApple OSS Distributions struct scenario scenario = { 2608*4f1223e8SApple OSS Distributions .name = "os-build-version", 2609*4f1223e8SApple OSS Distributions .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT), 2610*4f1223e8SApple OSS Distributions }; 2611*4f1223e8SApple OSS Distributions 2612*4f1223e8SApple OSS Distributions T_LOG("attempting to take stackshot with an os build version"); 2613*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 2614*4f1223e8SApple OSS Distributions expect_os_build_version_in_stackshot(ssbuf, sslen); 2615*4f1223e8SApple OSS Distributions }); 2616*4f1223e8SApple OSS Distributions} 2617*4f1223e8SApple OSS Distributions 2618*4f1223e8SApple OSS Distributionsstatic uint64_t 2619*4f1223e8SApple OSS Distributionsstackshot_timestamp(void *ssbuf, size_t sslen) 2620*4f1223e8SApple OSS Distributions{ 2621*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2622*4f1223e8SApple OSS Distributions 2623*4f1223e8SApple OSS Distributions uint32_t type = kcdata_iter_type(iter); 2624*4f1223e8SApple OSS Distributions if (type != KCDATA_BUFFER_BEGIN_STACKSHOT && type != KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT) { 2625*4f1223e8SApple OSS Distributions T_ASSERT_FAIL("invalid kcdata type %u", kcdata_iter_type(iter)); 2626*4f1223e8SApple OSS Distributions } 2627*4f1223e8SApple OSS Distributions 2628*4f1223e8SApple OSS Distributions iter = kcdata_iter_find_type(iter, KCDATA_TYPE_MACH_ABSOLUTE_TIME); 2629*4f1223e8SApple OSS Distributions T_QUIET; 2630*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_valid(iter), "timestamp found in stackshot"); 2631*4f1223e8SApple OSS Distributions 2632*4f1223e8SApple OSS Distributions return *(uint64_t *)kcdata_iter_payload(iter); 2633*4f1223e8SApple OSS Distributions} 2634*4f1223e8SApple OSS Distributions 2635*4f1223e8SApple OSS Distributions#define TEST_THREAD_NAME "stackshot_test_thread" 2636*4f1223e8SApple OSS Distributions 2637*4f1223e8SApple OSS Distributionsstatic void 2638*4f1223e8SApple OSS Distributionsparse_thread_group_stackshot(void **ssbuf, size_t sslen) 2639*4f1223e8SApple OSS Distributions{ 2640*4f1223e8SApple OSS Distributions bool seen_thread_group_snapshot = false; 2641*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2642*4f1223e8SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, 2643*4f1223e8SApple OSS Distributions "buffer provided is a stackshot"); 2644*4f1223e8SApple OSS Distributions 2645*4f1223e8SApple OSS Distributions NSMutableSet *thread_groups = [[NSMutableSet alloc] init]; 2646*4f1223e8SApple OSS Distributions 2647*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 2648*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2649*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 2650*4f1223e8SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 2651*4f1223e8SApple OSS Distributions T_QUIET; 2652*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_array_valid(iter), 2653*4f1223e8SApple OSS Distributions "checked that array is valid"); 2654*4f1223e8SApple OSS Distributions 2655*4f1223e8SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT) { 2656*4f1223e8SApple OSS Distributions continue; 2657*4f1223e8SApple OSS Distributions } 2658*4f1223e8SApple OSS Distributions 2659*4f1223e8SApple OSS Distributions seen_thread_group_snapshot = true; 2660*4f1223e8SApple OSS Distributions 2661*4f1223e8SApple OSS Distributions if (kcdata_iter_array_elem_size(iter) >= sizeof(struct thread_group_snapshot_v3)) { 2662*4f1223e8SApple OSS Distributions struct thread_group_snapshot_v3 *tgs_array = kcdata_iter_payload(iter); 2663*4f1223e8SApple OSS Distributions for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) { 2664*4f1223e8SApple OSS Distributions struct thread_group_snapshot_v3 *tgs = tgs_array + j; 2665*4f1223e8SApple OSS Distributions [thread_groups addObject:@(tgs->tgs_id)]; 2666*4f1223e8SApple OSS Distributions } 2667*4f1223e8SApple OSS Distributions } 2668*4f1223e8SApple OSS Distributions else { 2669*4f1223e8SApple OSS Distributions struct thread_group_snapshot *tgs_array = kcdata_iter_payload(iter); 2670*4f1223e8SApple OSS Distributions for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) { 2671*4f1223e8SApple OSS Distributions struct thread_group_snapshot *tgs = tgs_array + j; 2672*4f1223e8SApple OSS Distributions [thread_groups addObject:@(tgs->tgs_id)]; 2673*4f1223e8SApple OSS Distributions } 2674*4f1223e8SApple OSS Distributions } 2675*4f1223e8SApple OSS Distributions break; 2676*4f1223e8SApple OSS Distributions } 2677*4f1223e8SApple OSS Distributions } 2678*4f1223e8SApple OSS Distributions } 2679*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2680*4f1223e8SApple OSS Distributions NSError *error = nil; 2681*4f1223e8SApple OSS Distributions 2682*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 2683*4f1223e8SApple OSS Distributions 2684*4f1223e8SApple OSS Distributions case KCDATA_TYPE_CONTAINER_BEGIN: { 2685*4f1223e8SApple OSS Distributions T_QUIET; 2686*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_container_valid(iter), 2687*4f1223e8SApple OSS Distributions "checked that container is valid"); 2688*4f1223e8SApple OSS Distributions 2689*4f1223e8SApple OSS Distributions if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_THREAD) { 2690*4f1223e8SApple OSS Distributions break; 2691*4f1223e8SApple OSS Distributions } 2692*4f1223e8SApple OSS Distributions 2693*4f1223e8SApple OSS Distributions NSDictionary *container = parseKCDataContainer(&iter, &error); 2694*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(container, "parsed thread container from stackshot"); 2695*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container"); 2696*4f1223e8SApple OSS Distributions 2697*4f1223e8SApple OSS Distributions int tg = [container[@"thread_snapshots"][@"thread_group"] intValue]; 2698*4f1223e8SApple OSS Distributions 2699*4f1223e8SApple OSS Distributions T_ASSERT_TRUE([thread_groups containsObject:@(tg)], "check that the thread group the thread is in exists"); 2700*4f1223e8SApple OSS Distributions 2701*4f1223e8SApple OSS Distributions break; 2702*4f1223e8SApple OSS Distributions }; 2703*4f1223e8SApple OSS Distributions 2704*4f1223e8SApple OSS Distributions } 2705*4f1223e8SApple OSS Distributions } 2706*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(seen_thread_group_snapshot, "check that we have seen a thread group snapshot"); 2707*4f1223e8SApple OSS Distributions} 2708*4f1223e8SApple OSS Distributions 2709*4f1223e8SApple OSS Distributionsstatic void 2710*4f1223e8SApple OSS Distributionsverify_stackshot_sharedcache_layout(struct dyld_uuid_info_64 *uuids, uint32_t uuid_count) 2711*4f1223e8SApple OSS Distributions{ 2712*4f1223e8SApple OSS Distributions uuid_t cur_shared_cache_uuid; 2713*4f1223e8SApple OSS Distributions __block uint32_t lib_index = 0, libs_found = 0; 2714*4f1223e8SApple OSS Distributions 2715*4f1223e8SApple OSS Distributions _dyld_get_shared_cache_uuid(cur_shared_cache_uuid); 2716*4f1223e8SApple OSS Distributions int result = dyld_shared_cache_iterate_text(cur_shared_cache_uuid, ^(const dyld_shared_cache_dylib_text_info* info) { 2717*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LT(lib_index, uuid_count, "dyld_shared_cache_iterate_text exceeded number of libraries returned by kernel"); 2718*4f1223e8SApple OSS Distributions 2719*4f1223e8SApple OSS Distributions libs_found++; 2720*4f1223e8SApple OSS Distributions struct dyld_uuid_info_64 *cur_stackshot_uuid_entry = &uuids[lib_index]; 2721*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(memcmp(info->dylibUuid, cur_stackshot_uuid_entry->imageUUID, sizeof(info->dylibUuid)), 0, 2722*4f1223e8SApple OSS Distributions "dyld returned UUID doesn't match kernel returned UUID"); 2723*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(info->loadAddressUnslid, cur_stackshot_uuid_entry->imageLoadAddress, 2724*4f1223e8SApple OSS Distributions "dyld returned load address doesn't match kernel returned load address"); 2725*4f1223e8SApple OSS Distributions lib_index++; 2726*4f1223e8SApple OSS Distributions }); 2727*4f1223e8SApple OSS Distributions 2728*4f1223e8SApple OSS Distributions T_ASSERT_EQ(result, 0, "iterate shared cache layout"); 2729*4f1223e8SApple OSS Distributions T_ASSERT_EQ(libs_found, uuid_count, "dyld iterator returned same number of libraries as kernel"); 2730*4f1223e8SApple OSS Distributions 2731*4f1223e8SApple OSS Distributions T_LOG("verified %d libraries from dyld shared cache", libs_found); 2732*4f1223e8SApple OSS Distributions} 2733*4f1223e8SApple OSS Distributions 2734*4f1223e8SApple OSS Distributionsstatic void 2735*4f1223e8SApple OSS Distributionscheck_shared_cache_uuid(uuid_t imageUUID) 2736*4f1223e8SApple OSS Distributions{ 2737*4f1223e8SApple OSS Distributions static uuid_t shared_cache_uuid; 2738*4f1223e8SApple OSS Distributions static dispatch_once_t read_shared_cache_uuid; 2739*4f1223e8SApple OSS Distributions 2740*4f1223e8SApple OSS Distributions dispatch_once(&read_shared_cache_uuid, ^{ 2741*4f1223e8SApple OSS Distributions T_QUIET; 2742*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(_dyld_get_shared_cache_uuid(shared_cache_uuid), "retrieve current shared cache UUID"); 2743*4f1223e8SApple OSS Distributions }); 2744*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(uuid_compare(shared_cache_uuid, imageUUID), 0, 2745*4f1223e8SApple OSS Distributions "dyld returned UUID doesn't match kernel returned UUID for system shared cache"); 2746*4f1223e8SApple OSS Distributions} 2747*4f1223e8SApple OSS Distributions 2748*4f1223e8SApple OSS Distributions/* 2749*4f1223e8SApple OSS Distributions * extra dictionary contains data relevant for the given flags: 2750*4f1223e8SApple OSS Distributions * PARSE_STACKSHOT_ZOMBIE: zombie_child_pid_key -> @(pid) 2751*4f1223e8SApple OSS Distributions * PARSE_STACKSHOT_POSTEXEC: postexec_child_unique_pid_key -> @(unique_pid) 2752*4f1223e8SApple OSS Distributions */ 2753*4f1223e8SApple OSS Distributionsstatic void 2754*4f1223e8SApple OSS Distributionsparse_stackshot(uint64_t stackshot_parsing_flags, void *ssbuf, size_t sslen, NSDictionary *extra) 2755*4f1223e8SApple OSS Distributions{ 2756*4f1223e8SApple OSS Distributions bool delta = (stackshot_parsing_flags & PARSE_STACKSHOT_DELTA); 2757*4f1223e8SApple OSS Distributions bool expect_sharedcache_child = (stackshot_parsing_flags & PARSE_STACKSHOT_SHAREDCACHE_FLAGS); 2758*4f1223e8SApple OSS Distributions bool expect_zombie_child = (stackshot_parsing_flags & PARSE_STACKSHOT_ZOMBIE); 2759*4f1223e8SApple OSS Distributions bool expect_postexec_child = (stackshot_parsing_flags & PARSE_STACKSHOT_POSTEXEC); 2760*4f1223e8SApple OSS Distributions bool expect_cseg_waitinfo = (stackshot_parsing_flags & PARSE_STACKSHOT_WAITINFO_CSEG); 2761*4f1223e8SApple OSS Distributions bool expect_translated_child = (stackshot_parsing_flags & PARSE_STACKSHOT_TRANSLATED); 2762*4f1223e8SApple OSS Distributions bool expect_shared_cache_layout = false; 2763*4f1223e8SApple OSS Distributions bool expect_shared_cache_uuid = !delta; 2764*4f1223e8SApple OSS Distributions bool expect_dispatch_queue_label = (stackshot_parsing_flags & PARSE_STACKSHOT_DISPATCH_QUEUE_LABEL); 2765*4f1223e8SApple OSS Distributions bool expect_turnstile_lock = (stackshot_parsing_flags & PARSE_STACKSHOT_TURNSTILEINFO); 2766*4f1223e8SApple OSS Distributions bool expect_srp_waitinfo = (stackshot_parsing_flags & PARSE_STACKSHOT_WAITINFO_SRP); 2767*4f1223e8SApple OSS Distributions bool expect_sp_throttled = (stackshot_parsing_flags & PARSE_STACKSHOT_THROTTLED_SP); 2768*4f1223e8SApple OSS Distributions bool expect_exec_inprogress = (stackshot_parsing_flags & PARSE_STACKSHOT_EXEC_INPROGRESS); 2769*4f1223e8SApple OSS Distributions bool expect_transitioning_task = (stackshot_parsing_flags & PARSE_STACKSHOT_TRANSITIONING); 2770*4f1223e8SApple OSS Distributions bool expect_asyncstack = (stackshot_parsing_flags & PARSE_STACKSHOT_ASYNCSTACK); 2771*4f1223e8SApple OSS Distributions bool expect_driverkit = (stackshot_parsing_flags & PARSE_STACKSHOT_DRIVERKIT); 2772*4f1223e8SApple OSS Distributions bool expect_suspendinfo = (stackshot_parsing_flags & PARSE_STACKSHOT_SUSPENDINFO); 2773*4f1223e8SApple OSS Distributions bool found_zombie_child = false, found_postexec_child = false, found_shared_cache_layout = false, found_shared_cache_uuid = false; 2774*4f1223e8SApple OSS Distributions bool found_translated_child = false, found_transitioning_task = false; 2775*4f1223e8SApple OSS Distributions bool found_dispatch_queue_label = false, found_turnstile_lock = false; 2776*4f1223e8SApple OSS Distributions bool found_cseg_waitinfo = false, found_srp_waitinfo = false; 2777*4f1223e8SApple OSS Distributions bool found_sharedcache_child = false, found_sharedcache_badflags = false, found_sharedcache_self = false; 2778*4f1223e8SApple OSS Distributions bool found_asyncstack = false; 2779*4f1223e8SApple OSS Distributions bool found_throttled_service = false; 2780*4f1223e8SApple OSS Distributions bool found_exclaves = false; 2781*4f1223e8SApple OSS Distributions bool expect_single_task = (stackshot_parsing_flags & PARSE_STACKSHOT_TARGETPID); 2782*4f1223e8SApple OSS Distributions uint64_t srp_expected_threadid = 0; 2783*4f1223e8SApple OSS Distributions pid_t zombie_child_pid = -1, srp_expected_pid = -1, sharedcache_child_pid = -1, throttled_service_ctx = -1; 2784*4f1223e8SApple OSS Distributions pid_t translated_child_pid = -1, transistioning_task_pid = -1; 2785*4f1223e8SApple OSS Distributions bool sharedcache_child_sameaddr = false, is_throttled = false; 2786*4f1223e8SApple OSS Distributions uint64_t postexec_child_unique_pid = 0, cseg_expected_threadid = 0; 2787*4f1223e8SApple OSS Distributions uint64_t sharedcache_child_flags = 0, sharedcache_self_flags = 0; 2788*4f1223e8SApple OSS Distributions uint64_t asyncstack_threadid = 0; 2789*4f1223e8SApple OSS Distributions NSArray *asyncstack_stack = nil; 2790*4f1223e8SApple OSS Distributions char *inflatedBufferBase = NULL; 2791*4f1223e8SApple OSS Distributions pid_t exec_inprogress_pid = -1; 2792*4f1223e8SApple OSS Distributions void (^exec_inprogress_cb)(uint64_t, uint64_t) = NULL; 2793*4f1223e8SApple OSS Distributions int exec_inprogress_found = 0; 2794*4f1223e8SApple OSS Distributions uint64_t exec_inprogress_containerid = 0; 2795*4f1223e8SApple OSS Distributions void (^driverkit_cb)(pid_t) = NULL; 2796*4f1223e8SApple OSS Distributions NSMutableDictionary *sharedCaches = [NSMutableDictionary new]; 2797*4f1223e8SApple OSS Distributions uint64_t expected_num_threads = 0, expected_num_tasks = 0, found_percpu_threads = 0, found_tasks = 0, found_percpu_tasks = 0; 2798*4f1223e8SApple OSS Distributions NSMutableSet *seen_tasks = [NSMutableSet new]; 2799*4f1223e8SApple OSS Distributions 2800*4f1223e8SApple OSS Distributions if (expect_shared_cache_uuid) { 2801*4f1223e8SApple OSS Distributions uuid_t shared_cache_uuid; 2802*4f1223e8SApple OSS Distributions if (!_dyld_get_shared_cache_uuid(shared_cache_uuid)) { 2803*4f1223e8SApple OSS Distributions T_LOG("Skipping verifying shared cache UUID in stackshot data because not running with a shared cache"); 2804*4f1223e8SApple OSS Distributions expect_shared_cache_uuid = false; 2805*4f1223e8SApple OSS Distributions } 2806*4f1223e8SApple OSS Distributions } 2807*4f1223e8SApple OSS Distributions 2808*4f1223e8SApple OSS Distributions if (stackshot_parsing_flags & PARSE_STACKSHOT_SHAREDCACHE_LAYOUT) { 2809*4f1223e8SApple OSS Distributions size_t shared_cache_length = 0; 2810*4f1223e8SApple OSS Distributions const void *cache_header = _dyld_get_shared_cache_range(&shared_cache_length); 2811*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cache_header, "current process running with shared cache"); 2812*4f1223e8SApple 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"); 2813*4f1223e8SApple OSS Distributions 2814*4f1223e8SApple OSS Distributions if (_dyld_shared_cache_is_locally_built()) { 2815*4f1223e8SApple OSS Distributions T_LOG("device running with locally built shared cache, expect shared cache layout"); 2816*4f1223e8SApple OSS Distributions expect_shared_cache_layout = true; 2817*4f1223e8SApple OSS Distributions } else { 2818*4f1223e8SApple OSS Distributions T_LOG("device running with B&I built shared-cache, no shared cache layout expected"); 2819*4f1223e8SApple OSS Distributions } 2820*4f1223e8SApple OSS Distributions } 2821*4f1223e8SApple OSS Distributions 2822*4f1223e8SApple OSS Distributions if (expect_sharedcache_child) { 2823*4f1223e8SApple OSS Distributions NSNumber* pid_num = extra[sharedcache_child_pid_key]; 2824*4f1223e8SApple OSS Distributions NSNumber* sameaddr_num = extra[sharedcache_child_sameaddr_key]; 2825*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "sharedcache child pid provided"); 2826*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(sameaddr_num, "sharedcache child addrsame provided"); 2827*4f1223e8SApple OSS Distributions sharedcache_child_pid = [pid_num intValue]; 2828*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(sharedcache_child_pid, 0, "sharedcache child pid greater than zero"); 2829*4f1223e8SApple OSS Distributions sharedcache_child_sameaddr = [sameaddr_num intValue]; 2830*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GE([sameaddr_num intValue], 0, "sharedcache child sameaddr is boolean (0 or 1)"); 2831*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE([sameaddr_num intValue], 1, "sharedcache child sameaddr is boolean (0 or 1)"); 2832*4f1223e8SApple OSS Distributions } 2833*4f1223e8SApple OSS Distributions 2834*4f1223e8SApple OSS Distributions if (expect_transitioning_task) { 2835*4f1223e8SApple OSS Distributions NSNumber* pid_num = extra[transitioning_pid_key]; 2836*4f1223e8SApple OSS Distributions T_ASSERT_NOTNULL(pid_num, "transitioning task pid provided"); 2837*4f1223e8SApple OSS Distributions transistioning_task_pid = [pid_num intValue]; 2838*4f1223e8SApple OSS Distributions } 2839*4f1223e8SApple OSS Distributions 2840*4f1223e8SApple OSS Distributions if (expect_zombie_child) { 2841*4f1223e8SApple OSS Distributions NSNumber* pid_num = extra[zombie_child_pid_key]; 2842*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "zombie child pid provided"); 2843*4f1223e8SApple OSS Distributions zombie_child_pid = [pid_num intValue]; 2844*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(zombie_child_pid, 0, "zombie child pid greater than zero"); 2845*4f1223e8SApple OSS Distributions } 2846*4f1223e8SApple OSS Distributions 2847*4f1223e8SApple OSS Distributions if (expect_postexec_child) { 2848*4f1223e8SApple OSS Distributions NSNumber* unique_pid_num = extra[postexec_child_unique_pid_key]; 2849*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(unique_pid_num, "postexec child unique pid provided"); 2850*4f1223e8SApple OSS Distributions postexec_child_unique_pid = [unique_pid_num unsignedLongLongValue]; 2851*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(postexec_child_unique_pid, 0ull, "postexec child unique pid greater than zero"); 2852*4f1223e8SApple OSS Distributions } 2853*4f1223e8SApple OSS Distributions 2854*4f1223e8SApple OSS Distributions if (expect_cseg_waitinfo) { 2855*4f1223e8SApple OSS Distributions NSNumber* tid_num = extra[cseg_expected_threadid_key]; 2856*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(tid_num, "cseg's expected thread id provided"); 2857*4f1223e8SApple OSS Distributions cseg_expected_threadid = tid_num.unsignedLongValue; 2858*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(cseg_expected_threadid, UINT64_C(0), "compressor segment thread is present"); 2859*4f1223e8SApple OSS Distributions } 2860*4f1223e8SApple OSS Distributions 2861*4f1223e8SApple OSS Distributions if (expect_srp_waitinfo) { 2862*4f1223e8SApple OSS Distributions NSNumber* threadid_num = extra[srp_expected_threadid_key]; 2863*4f1223e8SApple OSS Distributions NSNumber* pid_num = extra[srp_expected_pid_key]; 2864*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(threadid_num != nil || pid_num != nil, "expected SRP threadid or pid"); 2865*4f1223e8SApple OSS Distributions if (threadid_num != nil) { 2866*4f1223e8SApple OSS Distributions srp_expected_threadid = [threadid_num unsignedLongLongValue]; 2867*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(srp_expected_threadid, 0ull, "srp_expected_threadid greater than zero"); 2868*4f1223e8SApple OSS Distributions } 2869*4f1223e8SApple OSS Distributions if (pid_num != nil) { 2870*4f1223e8SApple OSS Distributions srp_expected_pid = [pid_num intValue]; 2871*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(srp_expected_pid, 0, "srp_expected_pid greater than zero"); 2872*4f1223e8SApple OSS Distributions } 2873*4f1223e8SApple OSS Distributions T_LOG("looking for SRP pid: %d threadid: %llu", srp_expected_pid, srp_expected_threadid); 2874*4f1223e8SApple OSS Distributions } 2875*4f1223e8SApple OSS Distributions 2876*4f1223e8SApple OSS Distributions if (expect_sp_throttled) { 2877*4f1223e8SApple OSS Distributions NSNumber* ctx = extra[sp_throttled_expected_ctxt_key]; 2878*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(ctx != nil, "expected pid"); 2879*4f1223e8SApple OSS Distributions throttled_service_ctx = [ctx intValue]; 2880*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(throttled_service_ctx, 0, "expected pid greater than zero"); 2881*4f1223e8SApple OSS Distributions 2882*4f1223e8SApple OSS Distributions NSNumber *throttled = extra[sp_throttled_expect_flag]; 2883*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(throttled != nil, "expected flag value"); 2884*4f1223e8SApple OSS Distributions is_throttled = ([throttled intValue] != 0); 2885*4f1223e8SApple OSS Distributions 2886*4f1223e8SApple OSS Distributions T_LOG("Looking for service with ctxt: %d, thottled:%d", throttled_service_ctx, is_throttled); 2887*4f1223e8SApple OSS Distributions } 2888*4f1223e8SApple OSS Distributions 2889*4f1223e8SApple OSS Distributions if (expect_translated_child) { 2890*4f1223e8SApple OSS Distributions NSNumber* pid_num = extra[translated_child_pid_key]; 2891*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "translated child pid provided"); 2892*4f1223e8SApple OSS Distributions translated_child_pid = [pid_num intValue]; 2893*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(translated_child_pid, 0, "translated child pid greater than zero"); 2894*4f1223e8SApple OSS Distributions } 2895*4f1223e8SApple OSS Distributions if (expect_exec_inprogress) { 2896*4f1223e8SApple OSS Distributions NSNumber* pid_num = extra[exec_inprogress_pid_key]; 2897*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pid_num, "exec inprogress pid provided"); 2898*4f1223e8SApple OSS Distributions exec_inprogress_pid = [pid_num intValue]; 2899*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(exec_inprogress_pid, 0, "exec inprogress pid greater than zero"); 2900*4f1223e8SApple OSS Distributions 2901*4f1223e8SApple OSS Distributions exec_inprogress_cb = extra[exec_inprogress_found_key]; 2902*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(exec_inprogress_cb, "exec inprogress found callback provided"); 2903*4f1223e8SApple OSS Distributions } 2904*4f1223e8SApple OSS Distributions if (expect_driverkit) { 2905*4f1223e8SApple OSS Distributions driverkit_cb = extra[driverkit_found_key]; 2906*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(driverkit_cb, "driverkit found callback provided"); 2907*4f1223e8SApple OSS Distributions } 2908*4f1223e8SApple OSS Distributions 2909*4f1223e8SApple OSS Distributions if (expect_asyncstack) { 2910*4f1223e8SApple OSS Distributions NSNumber* threadid_id = extra[asyncstack_expected_threadid_key]; 2911*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(threadid_id, "asyncstack threadid provided"); 2912*4f1223e8SApple OSS Distributions asyncstack_threadid = [threadid_id unsignedLongLongValue]; 2913*4f1223e8SApple OSS Distributions asyncstack_stack = extra[asyncstack_expected_stack_key]; 2914*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(asyncstack_stack, "asyncstack expected stack provided"); 2915*4f1223e8SApple OSS Distributions } 2916*4f1223e8SApple OSS Distributions 2917*4f1223e8SApple OSS Distributions kcdata_iter_t iter = kcdata_iter(ssbuf, sslen); 2918*4f1223e8SApple OSS Distributions if (delta) { 2919*4f1223e8SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT, 2920*4f1223e8SApple OSS Distributions "buffer provided is a delta stackshot"); 2921*4f1223e8SApple OSS Distributions 2922*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 2923*4f1223e8SApple OSS Distributions } else { 2924*4f1223e8SApple OSS Distributions if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_COMPRESSED) { 2925*4f1223e8SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, 2926*4f1223e8SApple OSS Distributions "buffer provided is a stackshot"); 2927*4f1223e8SApple OSS Distributions 2928*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 2929*4f1223e8SApple OSS Distributions } else { 2930*4f1223e8SApple OSS Distributions /* we are dealing with a compressed buffer */ 2931*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 2932*4f1223e8SApple OSS Distributions uint64_t compression_type = 0, totalout = 0, totalin = 0; 2933*4f1223e8SApple OSS Distributions 2934*4f1223e8SApple OSS Distributions uint64_t *data; 2935*4f1223e8SApple OSS Distributions char *desc; 2936*4f1223e8SApple OSS Distributions for (int i = 0; i < 3; i ++) { 2937*4f1223e8SApple OSS Distributions kcdata_iter_get_data_with_desc(iter, &desc, (void **)&data, NULL); 2938*4f1223e8SApple OSS Distributions if (strcmp(desc, "kcd_c_type") == 0) { 2939*4f1223e8SApple OSS Distributions compression_type = *data; 2940*4f1223e8SApple OSS Distributions } else if (strcmp(desc, "kcd_c_totalout") == 0){ 2941*4f1223e8SApple OSS Distributions totalout = *data; 2942*4f1223e8SApple OSS Distributions } else if (strcmp(desc, "kcd_c_totalin") == 0){ 2943*4f1223e8SApple OSS Distributions totalin = *data; 2944*4f1223e8SApple OSS Distributions } 2945*4f1223e8SApple OSS Distributions 2946*4f1223e8SApple OSS Distributions iter = kcdata_iter_next(iter); 2947*4f1223e8SApple OSS Distributions } 2948*4f1223e8SApple OSS Distributions 2949*4f1223e8SApple OSS Distributions T_ASSERT_EQ(compression_type, UINT64_C(1), "zlib compression is used"); 2950*4f1223e8SApple OSS Distributions T_ASSERT_GT(totalout, UINT64_C(0), "successfully gathered how long the compressed buffer is"); 2951*4f1223e8SApple OSS Distributions T_ASSERT_GT(totalin, UINT64_C(0), "successfully gathered how long the uncompressed buffer will be at least"); 2952*4f1223e8SApple OSS Distributions 2953*4f1223e8SApple OSS Distributions /* progress to the next kcdata item */ 2954*4f1223e8SApple OSS Distributions T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT, "compressed stackshot found"); 2955*4f1223e8SApple OSS Distributions 2956*4f1223e8SApple OSS Distributions char *bufferBase = kcdata_iter_payload(iter); 2957*4f1223e8SApple OSS Distributions 2958*4f1223e8SApple OSS Distributions /* 2959*4f1223e8SApple OSS Distributions * zlib is used, allocate a buffer based on the metadata, plus 2960*4f1223e8SApple OSS Distributions * extra scratch space (+12.5%) in case totalin was inconsistent 2961*4f1223e8SApple OSS Distributions */ 2962*4f1223e8SApple OSS Distributions size_t inflatedBufferSize = totalin + (totalin >> 3); 2963*4f1223e8SApple OSS Distributions inflatedBufferBase = malloc(inflatedBufferSize); 2964*4f1223e8SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(inflatedBufferBase, "allocated temporary output buffer"); 2965*4f1223e8SApple OSS Distributions 2966*4f1223e8SApple OSS Distributions z_stream zs; 2967*4f1223e8SApple OSS Distributions memset(&zs, 0, sizeof(zs)); 2968*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(inflateInit(&zs), Z_OK, "inflateInit OK"); 2969*4f1223e8SApple OSS Distributions zs.next_in = (unsigned char *)bufferBase; 2970*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE(totalout, (uint64_t)UINT_MAX, "stackshot is not too large"); 2971*4f1223e8SApple OSS Distributions zs.avail_in = (uInt)totalout; 2972*4f1223e8SApple OSS Distributions zs.next_out = (unsigned char *)inflatedBufferBase; 2973*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE(inflatedBufferSize, (size_t)UINT_MAX, "output region is not too large"); 2974*4f1223e8SApple OSS Distributions zs.avail_out = (uInt)inflatedBufferSize; 2975*4f1223e8SApple OSS Distributions T_ASSERT_EQ(inflate(&zs, Z_FINISH), Z_STREAM_END, "inflated buffer"); 2976*4f1223e8SApple OSS Distributions inflateEnd(&zs); 2977*4f1223e8SApple OSS Distributions 2978*4f1223e8SApple OSS Distributions T_ASSERT_EQ((uint64_t)zs.total_out, totalin, "expected number of bytes inflated"); 2979*4f1223e8SApple OSS Distributions 2980*4f1223e8SApple OSS Distributions /* copy the data after the compressed area */ 2981*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GE((void *)bufferBase, ssbuf, 2982*4f1223e8SApple OSS Distributions "base of compressed stackshot is after the returned stackshot buffer"); 2983*4f1223e8SApple OSS Distributions size_t header_size = (size_t)(bufferBase - (char *)ssbuf); 2984*4f1223e8SApple OSS Distributions size_t data_after_compressed_size = sslen - totalout - header_size; 2985*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE(data_after_compressed_size, 2986*4f1223e8SApple OSS Distributions inflatedBufferSize - zs.total_out, 2987*4f1223e8SApple OSS Distributions "footer fits in the buffer"); 2988*4f1223e8SApple OSS Distributions memcpy(inflatedBufferBase + zs.total_out, 2989*4f1223e8SApple OSS Distributions bufferBase + totalout, 2990*4f1223e8SApple OSS Distributions data_after_compressed_size); 2991*4f1223e8SApple OSS Distributions 2992*4f1223e8SApple OSS Distributions iter = kcdata_iter(inflatedBufferBase, inflatedBufferSize); 2993*4f1223e8SApple OSS Distributions } 2994*4f1223e8SApple OSS Distributions } 2995*4f1223e8SApple OSS Distributions 2996*4f1223e8SApple OSS Distributions KCDATA_ITER_FOREACH(iter) { 2997*4f1223e8SApple OSS Distributions NSError *error = nil; 2998*4f1223e8SApple OSS Distributions 2999*4f1223e8SApple OSS Distributions switch (kcdata_iter_type(iter)) { 3000*4f1223e8SApple OSS Distributions case KCDATA_TYPE_ARRAY: { 3001*4f1223e8SApple OSS Distributions T_QUIET; 3002*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_array_valid(iter), 3003*4f1223e8SApple OSS Distributions "checked that array is valid"); 3004*4f1223e8SApple OSS Distributions 3005*4f1223e8SApple OSS Distributions NSMutableDictionary *array = parseKCDataArray(iter, &error); 3006*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(array, "parsed array from stackshot"); 3007*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing array"); 3008*4f1223e8SApple OSS Distributions 3009*4f1223e8SApple OSS Distributions if (kcdata_iter_array_elem_type(iter) == STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT) { 3010*4f1223e8SApple OSS Distributions struct dyld_uuid_info_64 *shared_cache_uuids = kcdata_iter_payload(iter); 3011*4f1223e8SApple OSS Distributions uint32_t uuid_count = kcdata_iter_array_elem_count(iter); 3012*4f1223e8SApple OSS Distributions T_ASSERT_NOTNULL(shared_cache_uuids, "parsed shared cache layout array"); 3013*4f1223e8SApple OSS Distributions T_ASSERT_GT(uuid_count, 0, "returned valid number of UUIDs from shared cache"); 3014*4f1223e8SApple OSS Distributions verify_stackshot_sharedcache_layout(shared_cache_uuids, uuid_count); 3015*4f1223e8SApple OSS Distributions found_shared_cache_layout = true; 3016*4f1223e8SApple OSS Distributions } 3017*4f1223e8SApple OSS Distributions 3018*4f1223e8SApple OSS Distributions break; 3019*4f1223e8SApple OSS Distributions } 3020*4f1223e8SApple OSS Distributions case KCDATA_TYPE_CONTAINER_BEGIN: { 3021*4f1223e8SApple OSS Distributions T_QUIET; 3022*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(kcdata_iter_container_valid(iter), 3023*4f1223e8SApple OSS Distributions "checked that container is valid"); 3024*4f1223e8SApple OSS Distributions 3025*4f1223e8SApple OSS Distributions uint64_t containerid = kcdata_iter_container_id(iter); 3026*4f1223e8SApple OSS Distributions uint32_t container_type = kcdata_iter_container_type(iter); 3027*4f1223e8SApple OSS Distributions 3028*4f1223e8SApple OSS Distributions if (container_type == STACKSHOT_KCCONTAINER_SHAREDCACHE) { 3029*4f1223e8SApple OSS Distributions NSDictionary *container = parseKCDataContainer(&iter, &error); 3030*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(container, "parsed sharedcache container from stackshot"); 3031*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing sharedcache container"); 3032*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_EQ(sharedCaches[@(containerid)], nil, "sharedcache containerid %lld should be unique", containerid); 3033*4f1223e8SApple OSS Distributions sharedCaches[@(containerid)] = container; 3034*4f1223e8SApple OSS Distributions break; 3035*4f1223e8SApple OSS Distributions } 3036*4f1223e8SApple OSS Distributions 3037*4f1223e8SApple OSS Distributions if (container_type == STACKSHOT_KCCONTAINER_EXCLAVES) { 3038*4f1223e8SApple OSS Distributions found_exclaves = true; 3039*4f1223e8SApple OSS Distributions break; 3040*4f1223e8SApple OSS Distributions } 3041*4f1223e8SApple OSS Distributions 3042*4f1223e8SApple OSS Distributions /* 3043*4f1223e8SApple OSS Distributions * treat containers other than tasks/transitioning_tasks 3044*4f1223e8SApple OSS Distributions * as expanded in-line. 3045*4f1223e8SApple OSS Distributions */ 3046*4f1223e8SApple OSS Distributions if (container_type != STACKSHOT_KCCONTAINER_TASK && 3047*4f1223e8SApple OSS Distributions container_type != STACKSHOT_KCCONTAINER_TRANSITIONING_TASK) { 3048*4f1223e8SApple OSS Distributions T_LOG("container skipped: %d", container_type); 3049*4f1223e8SApple OSS Distributions break; 3050*4f1223e8SApple OSS Distributions } 3051*4f1223e8SApple OSS Distributions NSDictionary *container = parseKCDataContainer(&iter, &error); 3052*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(container, "parsed task/transitioning_task container from stackshot"); 3053*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container"); 3054*4f1223e8SApple OSS Distributions 3055*4f1223e8SApple OSS Distributions found_tasks++; 3056*4f1223e8SApple OSS Distributions 3057*4f1223e8SApple OSS Distributions NSDictionary* task_snapshot = container[@"task_snapshots"][@"task_snapshot"]; 3058*4f1223e8SApple OSS Distributions NSDictionary* task_delta_snapshot = container[@"task_snapshots"][@"task_delta_snapshot"]; 3059*4f1223e8SApple OSS Distributions NSDictionary* transitioning_task_snapshot = container[@"transitioning_task_snapshots"][@"transitioning_task_snapshot"]; 3060*4f1223e8SApple OSS Distributions 3061*4f1223e8SApple OSS Distributions NSNumber *task_pid = NULL; 3062*4f1223e8SApple OSS Distributions if (task_snapshot) { 3063*4f1223e8SApple OSS Distributions task_pid = task_snapshot[@"ts_unique_pid"]; 3064*4f1223e8SApple OSS Distributions } else if(task_delta_snapshot) { 3065*4f1223e8SApple OSS Distributions task_pid = task_snapshot[@"tds_unique_pid"]; 3066*4f1223e8SApple OSS Distributions } else if(transitioning_task_snapshot) { 3067*4f1223e8SApple OSS Distributions task_pid = transitioning_task_snapshot[@"tts_pid"]; 3068*4f1223e8SApple OSS Distributions } 3069*4f1223e8SApple OSS Distributions 3070*4f1223e8SApple OSS Distributions if (task_pid && [seen_tasks containsObject:task_pid]) { 3071*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_FALSE([seen_tasks containsObject:task_pid], "No duplicate PIDs in stackshot"); 3072*4f1223e8SApple OSS Distributions [seen_tasks addObject:task_pid]; 3073*4f1223e8SApple OSS Distributions } 3074*4f1223e8SApple OSS Distributions 3075*4f1223e8SApple OSS Distributions /* 3076*4f1223e8SApple OSS Distributions * Having processed the container, we now only check it 3077*4f1223e8SApple OSS Distributions * if it's the correct type. 3078*4f1223e8SApple OSS Distributions */ 3079*4f1223e8SApple OSS Distributions if ((!expect_transitioning_task && (container_type != STACKSHOT_KCCONTAINER_TASK)) || 3080*4f1223e8SApple OSS Distributions (expect_transitioning_task && (container_type != STACKSHOT_KCCONTAINER_TRANSITIONING_TASK))) { 3081*4f1223e8SApple OSS Distributions break; 3082*4f1223e8SApple OSS Distributions } 3083*4f1223e8SApple OSS Distributions if (!expect_transitioning_task) { 3084*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(!!task_snapshot != !!task_delta_snapshot, "Either task_snapshot xor task_delta_snapshot provided"); 3085*4f1223e8SApple OSS Distributions } 3086*4f1223e8SApple OSS Distributions 3087*4f1223e8SApple OSS Distributions if (expect_dispatch_queue_label && !found_dispatch_queue_label) { 3088*4f1223e8SApple OSS Distributions for (id thread_key in container[@"task_snapshots"][@"thread_snapshots"]) { 3089*4f1223e8SApple OSS Distributions NSMutableDictionary *thread = container[@"task_snapshots"][@"thread_snapshots"][thread_key]; 3090*4f1223e8SApple OSS Distributions NSString *dql = thread[@"dispatch_queue_label"]; 3091*4f1223e8SApple OSS Distributions 3092*4f1223e8SApple OSS Distributions if ([dql isEqualToString:@TEST_STACKSHOT_QUEUE_LABEL]) { 3093*4f1223e8SApple OSS Distributions found_dispatch_queue_label = true; 3094*4f1223e8SApple OSS Distributions break; 3095*4f1223e8SApple OSS Distributions } 3096*4f1223e8SApple OSS Distributions } 3097*4f1223e8SApple OSS Distributions } 3098*4f1223e8SApple OSS Distributions 3099*4f1223e8SApple OSS Distributions if (expect_transitioning_task && !found_transitioning_task) { 3100*4f1223e8SApple OSS Distributions if (transitioning_task_snapshot) { 3101*4f1223e8SApple OSS Distributions uint64_t the_pid = [transitioning_task_snapshot[@"tts_pid"] unsignedLongLongValue]; 3102*4f1223e8SApple OSS Distributions if (the_pid == (uint64_t)transistioning_task_pid) { 3103*4f1223e8SApple OSS Distributions found_transitioning_task = true; 3104*4f1223e8SApple OSS Distributions 3105*4f1223e8SApple OSS Distributions T_PASS("FOUND Transitioning task %llu has a transitioning task snapshot", (uint64_t) transistioning_task_pid); 3106*4f1223e8SApple OSS Distributions break; 3107*4f1223e8SApple OSS Distributions } 3108*4f1223e8SApple OSS Distributions } 3109*4f1223e8SApple OSS Distributions } 3110*4f1223e8SApple OSS Distributions 3111*4f1223e8SApple OSS Distributions if (expect_postexec_child && !found_postexec_child) { 3112*4f1223e8SApple OSS Distributions if (task_snapshot) { 3113*4f1223e8SApple OSS Distributions uint64_t unique_pid = [task_snapshot[@"ts_unique_pid"] unsignedLongLongValue]; 3114*4f1223e8SApple OSS Distributions if (unique_pid == postexec_child_unique_pid) { 3115*4f1223e8SApple OSS Distributions found_postexec_child = true; 3116*4f1223e8SApple OSS Distributions 3117*4f1223e8SApple OSS Distributions T_PASS("post-exec child %llu has a task snapshot", postexec_child_unique_pid); 3118*4f1223e8SApple OSS Distributions 3119*4f1223e8SApple OSS Distributions break; 3120*4f1223e8SApple OSS Distributions } 3121*4f1223e8SApple OSS Distributions } 3122*4f1223e8SApple OSS Distributions 3123*4f1223e8SApple OSS Distributions if (task_delta_snapshot) { 3124*4f1223e8SApple OSS Distributions uint64_t unique_pid = [task_delta_snapshot[@"tds_unique_pid"] unsignedLongLongValue]; 3125*4f1223e8SApple OSS Distributions if (unique_pid == postexec_child_unique_pid) { 3126*4f1223e8SApple OSS Distributions found_postexec_child = true; 3127*4f1223e8SApple OSS Distributions 3128*4f1223e8SApple OSS Distributions T_FAIL("post-exec child %llu shouldn't have a delta task snapshot", postexec_child_unique_pid); 3129*4f1223e8SApple OSS Distributions 3130*4f1223e8SApple OSS Distributions break; 3131*4f1223e8SApple OSS Distributions } 3132*4f1223e8SApple OSS Distributions } 3133*4f1223e8SApple OSS Distributions } 3134*4f1223e8SApple OSS Distributions 3135*4f1223e8SApple OSS Distributions int pid = [task_snapshot[@"ts_pid"] intValue]; 3136*4f1223e8SApple OSS Distributions 3137*4f1223e8SApple OSS Distributions if (pid && expect_shared_cache_uuid && !found_shared_cache_uuid) { 3138*4f1223e8SApple OSS Distributions id ptr = container[@"task_snapshots"][@"shared_cache_dyld_load_info"]; 3139*4f1223e8SApple OSS Distributions if (ptr) { 3140*4f1223e8SApple OSS Distributions id uuid = ptr[@"imageUUID"]; 3141*4f1223e8SApple OSS Distributions 3142*4f1223e8SApple OSS Distributions uint8_t uuid_p[16]; 3143*4f1223e8SApple OSS Distributions for (unsigned int i = 0; i < 16; i ++) { 3144*4f1223e8SApple OSS Distributions NSNumber *uuidByte = uuid[i]; 3145*4f1223e8SApple OSS Distributions uuid_p[i] = (uint8_t)uuidByte.charValue; 3146*4f1223e8SApple OSS Distributions } 3147*4f1223e8SApple OSS Distributions 3148*4f1223e8SApple OSS Distributions check_shared_cache_uuid(uuid_p); 3149*4f1223e8SApple OSS Distributions 3150*4f1223e8SApple OSS Distributions uint64_t baseAddress = (uint64_t)((NSNumber *)ptr[@"imageSlidBaseAddress"]).longLongValue; 3151*4f1223e8SApple OSS Distributions uint64_t firstMapping = (uint64_t)((NSNumber *)ptr[@"sharedCacheSlidFirstMapping"]).longLongValue; 3152*4f1223e8SApple OSS Distributions 3153*4f1223e8SApple OSS Distributions T_EXPECT_LE(baseAddress, firstMapping, 3154*4f1223e8SApple OSS Distributions "in per-task shared_cache_dyld_load_info, " 3155*4f1223e8SApple OSS Distributions "baseAddress <= firstMapping"); 3156*4f1223e8SApple OSS Distributions T_EXPECT_GE(baseAddress + (7ull << 32) + (1ull << 29), 3157*4f1223e8SApple OSS Distributions firstMapping, 3158*4f1223e8SApple OSS Distributions "in per-task shared_cache_dyld_load_info, " 3159*4f1223e8SApple OSS Distributions "baseAddress + 28.5gig >= firstMapping"); 3160*4f1223e8SApple OSS Distributions 3161*4f1223e8SApple OSS Distributions size_t shared_cache_len; 3162*4f1223e8SApple OSS Distributions const void *addr = _dyld_get_shared_cache_range(&shared_cache_len); 3163*4f1223e8SApple OSS Distributions T_EXPECT_EQ((uint64_t)addr, firstMapping, 3164*4f1223e8SApple OSS Distributions "SlidFirstMapping should match shared_cache_range"); 3165*4f1223e8SApple OSS Distributions 3166*4f1223e8SApple OSS Distributions /* 3167*4f1223e8SApple OSS Distributions * check_shared_cache_uuid() will assert on failure, so if 3168*4f1223e8SApple OSS Distributions * we get here, then we have found the shared cache UUID 3169*4f1223e8SApple OSS Distributions * and it's correct 3170*4f1223e8SApple OSS Distributions */ 3171*4f1223e8SApple OSS Distributions found_shared_cache_uuid = true; 3172*4f1223e8SApple OSS Distributions } 3173*4f1223e8SApple OSS Distributions } 3174*4f1223e8SApple OSS Distributions 3175*4f1223e8SApple OSS Distributions if (expect_sharedcache_child) { 3176*4f1223e8SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 3177*4f1223e8SApple OSS Distributions uint64_t sharedregion_flags = (task_flags & (kTaskSharedRegionNone | kTaskSharedRegionSystem | kTaskSharedRegionOther)); 3178*4f1223e8SApple OSS Distributions id sharedregion_info = container[@"task_snapshots"][@"shared_cache_dyld_load_info"]; 3179*4f1223e8SApple OSS Distributions id sharedcache_id = container[@"task_snapshots"][@"sharedCacheID"]; 3180*4f1223e8SApple OSS Distributions if (!found_sharedcache_badflags) { 3181*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_NE(sharedregion_flags, 0ll, "one of the kTaskSharedRegion flags should be set on all tasks"); 3182*4f1223e8SApple OSS Distributions bool multiple = (sharedregion_flags & (sharedregion_flags - 1)) != 0; 3183*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(multiple, "only one kTaskSharedRegion flag should be set on each task"); 3184*4f1223e8SApple OSS Distributions found_sharedcache_badflags = (sharedregion_flags == 0 || multiple); 3185*4f1223e8SApple OSS Distributions } 3186*4f1223e8SApple OSS Distributions if (pid == 0) { 3187*4f1223e8SApple OSS Distributions T_ASSERT_EQ(sharedregion_flags, (uint64_t)kTaskSharedRegionNone, "Kernel proc (pid 0) should have no shared region"); 3188*4f1223e8SApple OSS Distributions } else if (pid == sharedcache_child_pid) { 3189*4f1223e8SApple OSS Distributions found_sharedcache_child = true; 3190*4f1223e8SApple OSS Distributions sharedcache_child_flags = sharedregion_flags; 3191*4f1223e8SApple OSS Distributions } else if (pid == getpid()) { 3192*4f1223e8SApple OSS Distributions found_sharedcache_self = true; 3193*4f1223e8SApple OSS Distributions sharedcache_self_flags = sharedregion_flags; 3194*4f1223e8SApple OSS Distributions } 3195*4f1223e8SApple OSS Distributions if (sharedregion_flags == kTaskSharedRegionOther && !(task_flags & kTaskSharedRegionInfoUnavailable)) { 3196*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_NOTNULL(sharedregion_info, "kTaskSharedRegionOther should have a shared_cache_dyld_load_info struct"); 3197*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_NOTNULL(sharedcache_id, "kTaskSharedRegionOther should have a sharedCacheID"); 3198*4f1223e8SApple OSS Distributions if (sharedcache_id != nil) { 3199*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_NOTNULL(sharedCaches[sharedcache_id], "sharedCacheID %d should exist", [sharedcache_id intValue]); 3200*4f1223e8SApple OSS Distributions } 3201*4f1223e8SApple OSS Distributions } else { 3202*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_NULL(sharedregion_info, "non-kTaskSharedRegionOther should have no shared_cache_dyld_load_info struct"); 3203*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_NULL(sharedcache_id, "non-kTaskSharedRegionOther should have no sharedCacheID"); 3204*4f1223e8SApple OSS Distributions } 3205*4f1223e8SApple OSS Distributions } 3206*4f1223e8SApple OSS Distributions 3207*4f1223e8SApple OSS Distributions if (expect_zombie_child && (pid == zombie_child_pid)) { 3208*4f1223e8SApple OSS Distributions found_zombie_child = true; 3209*4f1223e8SApple OSS Distributions 3210*4f1223e8SApple OSS Distributions expected_num_tasks += 1; 3211*4f1223e8SApple OSS Distributions 3212*4f1223e8SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 3213*4f1223e8SApple OSS Distributions T_ASSERT_TRUE((task_flags & kTerminatedSnapshot) == kTerminatedSnapshot, "child zombie marked as terminated"); 3214*4f1223e8SApple OSS Distributions 3215*4f1223e8SApple OSS Distributions continue; 3216*4f1223e8SApple OSS Distributions } 3217*4f1223e8SApple OSS Distributions 3218*4f1223e8SApple OSS Distributions if (expect_translated_child && (pid == translated_child_pid)) { 3219*4f1223e8SApple OSS Distributions found_translated_child = true; 3220*4f1223e8SApple OSS Distributions 3221*4f1223e8SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 3222*4f1223e8SApple OSS Distributions T_EXPECT_BITS_SET(task_flags, kTaskIsTranslated, "child marked as translated"); 3223*4f1223e8SApple OSS Distributions 3224*4f1223e8SApple OSS Distributions continue; 3225*4f1223e8SApple OSS Distributions } 3226*4f1223e8SApple OSS Distributions if (expect_exec_inprogress && (pid == exec_inprogress_pid || pid == -exec_inprogress_pid)) { 3227*4f1223e8SApple OSS Distributions exec_inprogress_found++; 3228*4f1223e8SApple OSS Distributions T_LOG("found exec task with pid %d, instance %d", pid, exec_inprogress_found); 3229*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_LE(exec_inprogress_found, 2, "no more than two with the expected pid"); 3230*4f1223e8SApple OSS Distributions if (exec_inprogress_found == 2) { 3231*4f1223e8SApple OSS Distributions T_LOG("found 2 tasks with pid %d", exec_inprogress_pid); 3232*4f1223e8SApple OSS Distributions exec_inprogress_cb(containerid, exec_inprogress_containerid); 3233*4f1223e8SApple OSS Distributions } else { 3234*4f1223e8SApple OSS Distributions exec_inprogress_containerid = containerid; 3235*4f1223e8SApple OSS Distributions } 3236*4f1223e8SApple OSS Distributions } 3237*4f1223e8SApple OSS Distributions if (expect_driverkit && driverkit_cb != NULL) { 3238*4f1223e8SApple OSS Distributions driverkit_cb(pid); 3239*4f1223e8SApple OSS Distributions } 3240*4f1223e8SApple OSS Distributions if (expect_cseg_waitinfo) { 3241*4f1223e8SApple OSS Distributions NSArray *winfos = container[@"task_snapshots"][@"thread_waitinfo"]; 3242*4f1223e8SApple OSS Distributions 3243*4f1223e8SApple OSS Distributions for (id i in winfos) { 3244*4f1223e8SApple OSS Distributions NSNumber *waitType = i[@"wait_type"]; 3245*4f1223e8SApple OSS Distributions NSNumber *owner = i[@"owner"]; 3246*4f1223e8SApple OSS Distributions if (waitType.intValue == kThreadWaitCompressor && 3247*4f1223e8SApple OSS Distributions owner.unsignedLongValue == cseg_expected_threadid) { 3248*4f1223e8SApple OSS Distributions found_cseg_waitinfo = true; 3249*4f1223e8SApple OSS Distributions break; 3250*4f1223e8SApple OSS Distributions } 3251*4f1223e8SApple OSS Distributions } 3252*4f1223e8SApple OSS Distributions } 3253*4f1223e8SApple OSS Distributions 3254*4f1223e8SApple OSS Distributions if (expect_srp_waitinfo) { 3255*4f1223e8SApple OSS Distributions NSArray *tinfos = container[@"task_snapshots"][@"thread_turnstileinfo"]; 3256*4f1223e8SApple OSS Distributions NSArray *winfos = container[@"task_snapshots"][@"thread_waitinfo"]; 3257*4f1223e8SApple OSS Distributions for (id i in tinfos) { 3258*4f1223e8SApple OSS Distributions if (!found_srp_waitinfo) { 3259*4f1223e8SApple OSS Distributions bool found_thread = false; 3260*4f1223e8SApple OSS Distributions bool found_pid = false; 3261*4f1223e8SApple OSS Distributions if (([i[@"turnstile_flags"] intValue] & STACKSHOT_TURNSTILE_STATUS_THREAD) && 3262*4f1223e8SApple OSS Distributions [i[@"turnstile_context"] unsignedLongLongValue] == srp_expected_threadid && 3263*4f1223e8SApple OSS Distributions srp_expected_threadid != 0) { 3264*4f1223e8SApple OSS Distributions found_thread = true; 3265*4f1223e8SApple OSS Distributions } 3266*4f1223e8SApple OSS Distributions if (([i[@"turnstile_flags"] intValue] & STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK) && 3267*4f1223e8SApple OSS Distributions [i[@"turnstile_context"] intValue] == srp_expected_pid && 3268*4f1223e8SApple OSS Distributions srp_expected_pid != -1) { 3269*4f1223e8SApple OSS Distributions found_pid = true; 3270*4f1223e8SApple OSS Distributions } 3271*4f1223e8SApple OSS Distributions if (found_pid || found_thread) { 3272*4f1223e8SApple OSS Distributions T_LOG("found SRP %s %lld waiter: %d", (found_thread ? "thread" : "pid"), 3273*4f1223e8SApple OSS Distributions [i[@"turnstile_context"] unsignedLongLongValue], [i[@"waiter"] intValue]); 3274*4f1223e8SApple OSS Distributions /* we found something that is blocking the correct threadid */ 3275*4f1223e8SApple OSS Distributions for (id j in winfos) { 3276*4f1223e8SApple OSS Distributions if ([j[@"waiter"] intValue] == [i[@"waiter"] intValue] && 3277*4f1223e8SApple OSS Distributions [j[@"wait_type"] intValue] == kThreadWaitPortReceive) { 3278*4f1223e8SApple OSS Distributions found_srp_waitinfo = true; 3279*4f1223e8SApple OSS Distributions T_EXPECT_EQ([j[@"wait_flags"] intValue], STACKSHOT_WAITINFO_FLAGS_SPECIALREPLY, 3280*4f1223e8SApple OSS Distributions "SRP waitinfo should be marked as a special reply"); 3281*4f1223e8SApple OSS Distributions break; 3282*4f1223e8SApple OSS Distributions } 3283*4f1223e8SApple OSS Distributions } 3284*4f1223e8SApple OSS Distributions 3285*4f1223e8SApple OSS Distributions if (found_srp_waitinfo) { 3286*4f1223e8SApple OSS Distributions break; 3287*4f1223e8SApple OSS Distributions } 3288*4f1223e8SApple OSS Distributions } 3289*4f1223e8SApple OSS Distributions } 3290*4f1223e8SApple OSS Distributions } 3291*4f1223e8SApple OSS Distributions } 3292*4f1223e8SApple OSS Distributions 3293*4f1223e8SApple OSS Distributions if (expect_sp_throttled) { 3294*4f1223e8SApple OSS Distributions NSArray *tinfos = container[@"task_snapshots"][@"thread_turnstileinfo"]; 3295*4f1223e8SApple OSS Distributions for (id i in tinfos) { 3296*4f1223e8SApple OSS Distributions if (([i[@"turnstile_flags"] intValue] & STACKSHOT_TURNSTILE_STATUS_PORTFLAGS) 3297*4f1223e8SApple OSS Distributions && [i[@"turnstile_context"] intValue] == throttled_service_ctx) { 3298*4f1223e8SApple OSS Distributions int portlabel_id = [i[@"portlabel_id"] intValue]; 3299*4f1223e8SApple OSS Distributions T_LOG("[pid:%d] Turnstile (flags = 0x%x, ctx = %d, portlabel_id = %d)", pid, 3300*4f1223e8SApple OSS Distributions [i[@"turnstile_flags"] intValue], [i[@"turnstile_context"] intValue], portlabel_id); 3301*4f1223e8SApple OSS Distributions for (id portid in container[@"task_snapshots"][@"portlabels"]) { 3302*4f1223e8SApple OSS Distributions if (portlabel_id != [portid intValue]) { 3303*4f1223e8SApple OSS Distributions continue; 3304*4f1223e8SApple OSS Distributions } 3305*4f1223e8SApple OSS Distributions 3306*4f1223e8SApple OSS Distributions NSMutableDictionary *portlabel = container[@"task_snapshots"][@"portlabels"][portid]; 3307*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(portlabel != nil, "Found portlabel id: %d", [portid intValue]); 3308*4f1223e8SApple OSS Distributions NSString *portlabel_name = portlabel[@"portlabel_name"]; 3309*4f1223e8SApple OSS Distributions T_EXPECT_TRUE(portlabel_name != nil, "Found portlabel %s", portlabel_name.UTF8String); 3310*4f1223e8SApple OSS Distributions T_EXPECT_EQ_STR(portlabel_name.UTF8String, THROTTLED_SERVICE_NAME, "throttled service port name matches"); 3311*4f1223e8SApple OSS Distributions T_EXPECT_EQ(([portlabel[@"portlabel_flags"] intValue] & STACKSHOT_PORTLABEL_THROTTLED) != 0, 3312*4f1223e8SApple OSS Distributions is_throttled, "Port %s throttled", is_throttled ? "is" : "isn't"); 3313*4f1223e8SApple OSS Distributions found_throttled_service = true; 3314*4f1223e8SApple OSS Distributions break; 3315*4f1223e8SApple OSS Distributions } 3316*4f1223e8SApple OSS Distributions } 3317*4f1223e8SApple OSS Distributions 3318*4f1223e8SApple OSS Distributions if (found_throttled_service) { 3319*4f1223e8SApple OSS Distributions break; 3320*4f1223e8SApple OSS Distributions } 3321*4f1223e8SApple OSS Distributions } 3322*4f1223e8SApple OSS Distributions } 3323*4f1223e8SApple OSS Distributions 3324*4f1223e8SApple OSS Distributions if (expect_suspendinfo) { 3325*4f1223e8SApple OSS Distributions // TODO: rdar://112563110 3326*4f1223e8SApple OSS Distributions } 3327*4f1223e8SApple OSS Distributions 3328*4f1223e8SApple OSS Distributions 3329*4f1223e8SApple OSS Distributions if (pid != getpid()) { 3330*4f1223e8SApple OSS Distributions break; 3331*4f1223e8SApple OSS Distributions } 3332*4f1223e8SApple OSS Distributions 3333*4f1223e8SApple OSS Distributions T_EXPECT_EQ_STR(current_process_name(), 3334*4f1223e8SApple OSS Distributions [task_snapshot[@"ts_p_comm"] UTF8String], 3335*4f1223e8SApple OSS Distributions "current process name matches in stackshot"); 3336*4f1223e8SApple OSS Distributions 3337*4f1223e8SApple OSS Distributions uint64_t task_flags = [task_snapshot[@"ts_ss_flags"] unsignedLongLongValue]; 3338*4f1223e8SApple OSS Distributions T_ASSERT_BITS_NOTSET(task_flags, kTerminatedSnapshot, "current process not marked as terminated"); 3339*4f1223e8SApple OSS Distributions T_ASSERT_BITS_NOTSET(task_flags, kTaskIsTranslated, "current process not marked as translated"); 3340*4f1223e8SApple OSS Distributions 3341*4f1223e8SApple OSS Distributions T_QUIET; 3342*4f1223e8SApple OSS Distributions T_EXPECT_LE(pid, [task_snapshot[@"ts_unique_pid"] intValue], 3343*4f1223e8SApple OSS Distributions "unique pid is greater than pid"); 3344*4f1223e8SApple OSS Distributions 3345*4f1223e8SApple OSS Distributions NSDictionary* task_cpu_architecture = container[@"task_snapshots"][@"task_cpu_architecture"]; 3346*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(task_cpu_architecture[@"cputype"], "have cputype"); 3347*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(task_cpu_architecture[@"cpusubtype"], "have cputype"); 3348*4f1223e8SApple OSS Distributions int cputype = [task_cpu_architecture[@"cputype"] intValue]; 3349*4f1223e8SApple OSS Distributions int cpusubtype = [task_cpu_architecture[@"cpusubtype"] intValue]; 3350*4f1223e8SApple OSS Distributions 3351*4f1223e8SApple OSS Distributions struct proc_archinfo archinfo; 3352*4f1223e8SApple OSS Distributions int retval = proc_pidinfo(pid, PROC_PIDARCHINFO, 0, &archinfo, sizeof(archinfo)); 3353*4f1223e8SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_GT(retval, 0, "proc_pidinfo(PROC_PIDARCHINFO) returned a value > 0"); 3354*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(retval, (int)sizeof(struct proc_archinfo), "proc_pidinfo call for PROC_PIDARCHINFO returned expected size"); 3355*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_EQ(cputype, archinfo.p_cputype, "cpu type is correct"); 3356*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_EQ(cpusubtype, archinfo.p_cpusubtype, "cpu subtype is correct"); 3357*4f1223e8SApple OSS Distributions 3358*4f1223e8SApple OSS Distributions NSDictionary * codesigning_info = container[@"task_snapshots"][@"stackshot_task_codesigning_info"]; 3359*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(codesigning_info[@"csflags"], "have csflags"); 3360*4f1223e8SApple OSS Distributions uint64_t flags = [codesigning_info[@"csflags"] unsignedLongLongValue]; 3361*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_GT(flags, 0, "nonzero csflags"); 3362*4f1223e8SApple OSS Distributions 3363*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(container[@"task_snapshots"][@"jetsam_coalition"], "have jetsam coalition"); 3364*4f1223e8SApple OSS Distributions uint64_t jetsam_coalition = [container[@"task_snapshots"][@"jetsam_coalition"] unsignedLongLongValue]; 3365*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_GT(jetsam_coalition, 0, "nonzero jetsam coalition"); 3366*4f1223e8SApple OSS Distributions 3367*4f1223e8SApple OSS Distributions bool found_main_thread = false; 3368*4f1223e8SApple OSS Distributions uint64_t main_thread_id = -1ULL; 3369*4f1223e8SApple OSS Distributions bool found_null_kernel_frame = false; 3370*4f1223e8SApple OSS Distributions for (id thread_key in container[@"task_snapshots"][@"thread_snapshots"]) { 3371*4f1223e8SApple OSS Distributions NSMutableDictionary *thread = container[@"task_snapshots"][@"thread_snapshots"][thread_key]; 3372*4f1223e8SApple OSS Distributions NSDictionary *thread_snap = thread[@"thread_snapshot"]; 3373*4f1223e8SApple OSS Distributions 3374*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_thread_id"] intValue], 0, 3375*4f1223e8SApple OSS Distributions "thread ID of thread in current task is valid"); 3376*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_base_priority"] intValue], 0, 3377*4f1223e8SApple OSS Distributions "base priority of thread in current task is valid"); 3378*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_sched_priority"] intValue], 0, 3379*4f1223e8SApple OSS Distributions "scheduling priority of thread in current task is valid"); 3380*4f1223e8SApple OSS Distributions 3381*4f1223e8SApple OSS Distributions NSString *pth_name = thread[@"pth_name"]; 3382*4f1223e8SApple OSS Distributions if (pth_name != nil && [pth_name isEqualToString:@TEST_THREAD_NAME]) { 3383*4f1223e8SApple OSS Distributions found_main_thread = true; 3384*4f1223e8SApple OSS Distributions main_thread_id = [thread_snap[@"ths_thread_id"] unsignedLongLongValue]; 3385*4f1223e8SApple OSS Distributions 3386*4f1223e8SApple OSS Distributions T_QUIET; T_EXPECT_GT([thread_snap[@"ths_total_syscalls"] intValue], 0, 3387*4f1223e8SApple OSS Distributions "total syscalls of current thread is valid"); 3388*4f1223e8SApple OSS Distributions 3389*4f1223e8SApple OSS Distributions NSDictionary *cpu_times = thread[@"cpu_times"]; 3390*4f1223e8SApple OSS Distributions T_EXPECT_GE([cpu_times[@"runnable_time"] intValue], 3391*4f1223e8SApple OSS Distributions [cpu_times[@"system_time"] intValue] + 3392*4f1223e8SApple OSS Distributions [cpu_times[@"user_time"] intValue], 3393*4f1223e8SApple OSS Distributions "runnable time of current thread is valid"); 3394*4f1223e8SApple OSS Distributions } 3395*4f1223e8SApple OSS Distributions if (!found_null_kernel_frame) { 3396*4f1223e8SApple OSS Distributions for (NSNumber *frame in thread[@"kernel_frames"]) { 3397*4f1223e8SApple OSS Distributions if (frame.unsignedLongValue == 0) { 3398*4f1223e8SApple OSS Distributions found_null_kernel_frame = true; 3399*4f1223e8SApple OSS Distributions break; 3400*4f1223e8SApple OSS Distributions } 3401*4f1223e8SApple OSS Distributions } 3402*4f1223e8SApple OSS Distributions } 3403*4f1223e8SApple OSS Distributions if (expect_asyncstack && !found_asyncstack && 3404*4f1223e8SApple OSS Distributions asyncstack_threadid == [thread_snap[@"ths_thread_id"] unsignedLongLongValue]) { 3405*4f1223e8SApple OSS Distributions found_asyncstack = true; 3406*4f1223e8SApple OSS Distributions NSArray* async_stack = thread[@"user_async_stack_frames"]; 3407*4f1223e8SApple OSS Distributions NSNumber* start_idx = thread[@"user_async_start_index"]; 3408*4f1223e8SApple OSS Distributions NSArray* user_stack = thread[@"user_stack_frames"]; 3409*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(async_stack, "async thread %#llx has user_async_stack_frames", asyncstack_threadid); 3410*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(start_idx, "async thread %#llx has user_async_start_index", asyncstack_threadid); 3411*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(user_stack, "async thread %#llx has user_stack_frames", asyncstack_threadid); 3412*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ(async_stack.count, asyncstack_stack.count, 3413*4f1223e8SApple OSS Distributions "actual async_stack count == expected async_stack count"); 3414*4f1223e8SApple OSS Distributions for (size_t i = 0; i < async_stack.count; i++) { 3415*4f1223e8SApple OSS Distributions T_EXPECT_EQ([async_stack[i][@"lr"] unsignedLongLongValue], 3416*4f1223e8SApple OSS Distributions [asyncstack_stack[i] unsignedLongLongValue], "frame %zu matches", i); 3417*4f1223e8SApple OSS Distributions } 3418*4f1223e8SApple OSS Distributions } 3419*4f1223e8SApple OSS Distributions } 3420*4f1223e8SApple OSS Distributions T_EXPECT_TRUE(found_main_thread, "found main thread for current task in stackshot"); 3421*4f1223e8SApple OSS Distributions T_EXPECT_FALSE(found_null_kernel_frame, "should not see any NULL kernel frames"); 3422*4f1223e8SApple OSS Distributions 3423*4f1223e8SApple OSS Distributions if (expect_turnstile_lock && !found_turnstile_lock) { 3424*4f1223e8SApple OSS Distributions NSArray *tsinfos = container[@"task_snapshots"][@"thread_turnstileinfo"]; 3425*4f1223e8SApple OSS Distributions 3426*4f1223e8SApple OSS Distributions for (id i in tsinfos) { 3427*4f1223e8SApple OSS Distributions if ([i[@"turnstile_context"] unsignedLongLongValue] == main_thread_id) { 3428*4f1223e8SApple OSS Distributions found_turnstile_lock = true; 3429*4f1223e8SApple OSS Distributions break; 3430*4f1223e8SApple OSS Distributions } 3431*4f1223e8SApple OSS Distributions } 3432*4f1223e8SApple OSS Distributions } 3433*4f1223e8SApple OSS Distributions break; 3434*4f1223e8SApple OSS Distributions } 3435*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO: { 3436*4f1223e8SApple OSS Distributions // Legacy shared cache info 3437*4f1223e8SApple OSS Distributions struct dyld_shared_cache_loadinfo *payload = kcdata_iter_payload(iter); 3438*4f1223e8SApple OSS Distributions T_ASSERT_EQ((size_t)kcdata_iter_size(iter), sizeof(*payload), "valid dyld_shared_cache_loadinfo struct"); 3439*4f1223e8SApple OSS Distributions 3440*4f1223e8SApple OSS Distributions check_shared_cache_uuid(payload->sharedCacheUUID); 3441*4f1223e8SApple OSS Distributions 3442*4f1223e8SApple OSS Distributions T_EXPECT_LE(payload->sharedCacheUnreliableSlidBaseAddress, 3443*4f1223e8SApple OSS Distributions payload->sharedCacheSlidFirstMapping, 3444*4f1223e8SApple OSS Distributions "SlidBaseAddress <= SlidFirstMapping"); 3445*4f1223e8SApple OSS Distributions T_EXPECT_GE(payload->sharedCacheUnreliableSlidBaseAddress + (7ull << 32) + (1ull << 29), 3446*4f1223e8SApple OSS Distributions payload->sharedCacheSlidFirstMapping, 3447*4f1223e8SApple OSS Distributions "SlidFirstMapping should be within 28.5gigs of SlidBaseAddress"); 3448*4f1223e8SApple OSS Distributions 3449*4f1223e8SApple OSS Distributions size_t shared_cache_len; 3450*4f1223e8SApple OSS Distributions const void *addr = _dyld_get_shared_cache_range(&shared_cache_len); 3451*4f1223e8SApple OSS Distributions T_EXPECT_EQ((uint64_t)addr, payload->sharedCacheSlidFirstMapping, 3452*4f1223e8SApple OSS Distributions "SlidFirstMapping should match shared_cache_range"); 3453*4f1223e8SApple OSS Distributions 3454*4f1223e8SApple OSS Distributions /* 3455*4f1223e8SApple OSS Distributions * check_shared_cache_uuid() asserts on failure, so we must have 3456*4f1223e8SApple OSS Distributions * found the shared cache UUID to be correct. 3457*4f1223e8SApple OSS Distributions */ 3458*4f1223e8SApple OSS Distributions found_shared_cache_uuid = true; 3459*4f1223e8SApple OSS Distributions break; 3460*4f1223e8SApple OSS Distributions } 3461*4f1223e8SApple OSS Distributions case KCDATA_TYPE_UINT64_DESC: { 3462*4f1223e8SApple OSS Distributions char *desc; 3463*4f1223e8SApple OSS Distributions uint64_t *data; 3464*4f1223e8SApple OSS Distributions uint32_t size; 3465*4f1223e8SApple OSS Distributions kcdata_iter_get_data_with_desc(iter, &desc, &data, &size); 3466*4f1223e8SApple OSS Distributions 3467*4f1223e8SApple OSS Distributions if (strcmp(desc, "stackshot_tasks_count") == 0) { 3468*4f1223e8SApple OSS Distributions expected_num_tasks = *data; 3469*4f1223e8SApple OSS Distributions } else if (strcmp(desc, "stackshot_threads_count") == 0) { 3470*4f1223e8SApple OSS Distributions expected_num_threads = *data; 3471*4f1223e8SApple OSS Distributions } 3472*4f1223e8SApple OSS Distributions 3473*4f1223e8SApple OSS Distributions break; 3474*4f1223e8SApple OSS Distributions } 3475*4f1223e8SApple OSS Distributions case STACKSHOT_KCTYPE_LATENCY_INFO_CPU: { 3476*4f1223e8SApple OSS Distributions struct stackshot_latency_cpu *cpu_latency = kcdata_iter_payload(iter); 3477*4f1223e8SApple OSS Distributions found_percpu_tasks += cpu_latency->tasks_processed; 3478*4f1223e8SApple OSS Distributions found_percpu_threads += cpu_latency->threads_processed; 3479*4f1223e8SApple OSS Distributions break; 3480*4f1223e8SApple OSS Distributions } 3481*4f1223e8SApple OSS Distributions } 3482*4f1223e8SApple OSS Distributions } 3483*4f1223e8SApple OSS Distributions 3484*4f1223e8SApple OSS Distributions if (expect_sharedcache_child) { 3485*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_sharedcache_child, "found sharedcache child in kcdata"); 3486*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_sharedcache_self, "found self in kcdata"); 3487*4f1223e8SApple OSS Distributions if (found_sharedcache_child && found_sharedcache_self) { 3488*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NE(sharedcache_child_flags, (uint64_t)kTaskSharedRegionNone, "sharedcache child should have shared region"); 3489*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_NE(sharedcache_self_flags, (uint64_t)kTaskSharedRegionNone, "sharedcache: self should have shared region"); 3490*4f1223e8SApple OSS Distributions if (sharedcache_self_flags == kTaskSharedRegionSystem && !sharedcache_child_sameaddr) { 3491*4f1223e8SApple OSS Distributions /* If we're in the system shared region, and the child has a different address, child must have an Other shared region */ 3492*4f1223e8SApple OSS Distributions T_ASSERT_EQ(sharedcache_child_flags, (uint64_t)kTaskSharedRegionOther, 3493*4f1223e8SApple OSS Distributions "sharedcache child should have Other shared region"); 3494*4f1223e8SApple OSS Distributions } 3495*4f1223e8SApple OSS Distributions } 3496*4f1223e8SApple OSS Distributions } 3497*4f1223e8SApple OSS Distributions 3498*4f1223e8SApple OSS Distributions if (expect_transitioning_task) { 3499*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_transitioning_task, "found transitioning_task child in kcdata"); 3500*4f1223e8SApple OSS Distributions } 3501*4f1223e8SApple OSS Distributions 3502*4f1223e8SApple OSS Distributions if (expect_exec_inprogress) { 3503*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_GT(exec_inprogress_found, 0, "found at least 1 task for execing process"); 3504*4f1223e8SApple OSS Distributions } 3505*4f1223e8SApple OSS Distributions 3506*4f1223e8SApple OSS Distributions if (expect_zombie_child) { 3507*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_zombie_child, "found zombie child in kcdata"); 3508*4f1223e8SApple OSS Distributions } 3509*4f1223e8SApple OSS Distributions 3510*4f1223e8SApple OSS Distributions if (expect_postexec_child) { 3511*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_postexec_child, "found post-exec child in kcdata"); 3512*4f1223e8SApple OSS Distributions } 3513*4f1223e8SApple OSS Distributions 3514*4f1223e8SApple OSS Distributions if (expect_translated_child) { 3515*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_translated_child, "found translated child in kcdata"); 3516*4f1223e8SApple OSS Distributions } 3517*4f1223e8SApple OSS Distributions 3518*4f1223e8SApple OSS Distributions if (expect_shared_cache_layout) { 3519*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_shared_cache_layout, "shared cache layout found in kcdata"); 3520*4f1223e8SApple OSS Distributions } 3521*4f1223e8SApple OSS Distributions 3522*4f1223e8SApple OSS Distributions if (expect_shared_cache_uuid) { 3523*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_shared_cache_uuid, "shared cache UUID found in kcdata"); 3524*4f1223e8SApple OSS Distributions } 3525*4f1223e8SApple OSS Distributions 3526*4f1223e8SApple OSS Distributions if (expect_dispatch_queue_label) { 3527*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_dispatch_queue_label, "dispatch queue label found in kcdata"); 3528*4f1223e8SApple OSS Distributions } 3529*4f1223e8SApple OSS Distributions 3530*4f1223e8SApple OSS Distributions if (expect_turnstile_lock) { 3531*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_turnstile_lock, "found expected deadlock"); 3532*4f1223e8SApple OSS Distributions } 3533*4f1223e8SApple OSS Distributions 3534*4f1223e8SApple OSS Distributions if (expect_cseg_waitinfo) { 3535*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_cseg_waitinfo, "found c_seg waitinfo"); 3536*4f1223e8SApple OSS Distributions } 3537*4f1223e8SApple OSS Distributions 3538*4f1223e8SApple OSS Distributions if (expect_srp_waitinfo) { 3539*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_srp_waitinfo, "found special reply port waitinfo"); 3540*4f1223e8SApple OSS Distributions } 3541*4f1223e8SApple OSS Distributions 3542*4f1223e8SApple OSS Distributions if (expect_sp_throttled) { 3543*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_throttled_service, "found the throttled service"); 3544*4f1223e8SApple OSS Distributions } 3545*4f1223e8SApple OSS Distributions 3546*4f1223e8SApple OSS Distributions if (expect_asyncstack) { 3547*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(found_asyncstack, "found async stack threadid"); 3548*4f1223e8SApple OSS Distributions } 3549*4f1223e8SApple OSS Distributions 3550*4f1223e8SApple OSS Distributions if ([extra objectForKey:no_exclaves_key] != nil) { 3551*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_FALSE(found_exclaves, "did not find any Exclaves data"); 3552*4f1223e8SApple OSS Distributions } 3553*4f1223e8SApple OSS Distributions 3554*4f1223e8SApple OSS Distributions 3555*4f1223e8SApple OSS Distributions bool check_counts = !delta && !found_transitioning_task && !expect_single_task && !expect_driverkit; 3556*4f1223e8SApple OSS Distributions 3557*4f1223e8SApple OSS Distributions if (check_counts && (expected_num_threads != 0) && (found_percpu_threads != 0)) { 3558*4f1223e8SApple OSS Distributions /* If the task counts below check out, we can be sure that the per-cpu reported thread counts are accurate. */ 3559*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ_ULLONG(found_percpu_threads, expected_num_threads, "number of threads reported by CPUs matches expected count"); 3560*4f1223e8SApple OSS Distributions } 3561*4f1223e8SApple OSS Distributions 3562*4f1223e8SApple OSS Distributions if (check_counts && (expected_num_tasks != 0)) { 3563*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ_ULLONG(found_tasks, expected_num_tasks, "number of tasks in kcdata matches expected count"); 3564*4f1223e8SApple OSS Distributions if (found_percpu_tasks != 0) { 3565*4f1223e8SApple OSS Distributions T_QUIET; T_ASSERT_EQ_ULLONG(found_percpu_tasks, expected_num_tasks, "number of tasks reported by CPUs matches expected count"); 3566*4f1223e8SApple OSS Distributions } 3567*4f1223e8SApple OSS Distributions } 3568*4f1223e8SApple OSS Distributions 3569*4f1223e8SApple OSS Distributions T_ASSERT_FALSE(KCDATA_ITER_FOREACH_FAILED(iter), "successfully iterated kcdata"); 3570*4f1223e8SApple OSS Distributions 3571*4f1223e8SApple OSS Distributions free(inflatedBufferBase); 3572*4f1223e8SApple OSS Distributions} 3573*4f1223e8SApple OSS Distributions 3574*4f1223e8SApple OSS Distributionsstatic const char * 3575*4f1223e8SApple OSS Distributionscurrent_process_name(void) 3576*4f1223e8SApple OSS Distributions{ 3577*4f1223e8SApple OSS Distributions static char name[64]; 3578*4f1223e8SApple OSS Distributions 3579*4f1223e8SApple OSS Distributions if (!name[0]) { 3580*4f1223e8SApple OSS Distributions int ret = proc_name(getpid(), name, sizeof(name)); 3581*4f1223e8SApple OSS Distributions T_QUIET; 3582*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "proc_name failed for current process"); 3583*4f1223e8SApple OSS Distributions } 3584*4f1223e8SApple OSS Distributions 3585*4f1223e8SApple OSS Distributions return name; 3586*4f1223e8SApple OSS Distributions} 3587*4f1223e8SApple OSS Distributions 3588*4f1223e8SApple OSS Distributionsstatic void 3589*4f1223e8SApple OSS Distributionsinitialize_thread(void) 3590*4f1223e8SApple OSS Distributions{ 3591*4f1223e8SApple OSS Distributions int ret = pthread_setname_np(TEST_THREAD_NAME); 3592*4f1223e8SApple OSS Distributions T_QUIET; 3593*4f1223e8SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "set thread name to %s", TEST_THREAD_NAME); 3594*4f1223e8SApple OSS Distributions} 3595*4f1223e8SApple OSS Distributions 3596*4f1223e8SApple OSS DistributionsT_DECL(dirty_buffer, "test that stackshot works with a dirty input buffer from kernel", T_META_TAG_VM_PREFERRED) 3597*4f1223e8SApple OSS Distributions{ 3598*4f1223e8SApple OSS Distributions const char *test_sysctl = "stackshot_dirty_buffer"; 3599*4f1223e8SApple OSS Distributions int64_t result; 3600*4f1223e8SApple OSS Distributions 3601*4f1223e8SApple OSS Distributions T_LOG("running sysctl to trigger kernel-driven stackshot"); 3602*4f1223e8SApple OSS Distributions result = run_sysctl_test(test_sysctl, 0); 3603*4f1223e8SApple OSS Distributions T_ASSERT_EQ_LLONG(result, 1, "sysctl result indicated success"); 3604*4f1223e8SApple OSS Distributions} 3605*4f1223e8SApple OSS Distributions 3606*4f1223e8SApple OSS DistributionsT_DECL(kernel_initiated, "smoke test that stackshot works with kernel-initiated stackshots", T_META_TAG_VM_PREFERRED) 3607*4f1223e8SApple OSS Distributions{ 3608*4f1223e8SApple OSS Distributions const char *test_sysctl = "stackshot_kernel_initiator"; 3609*4f1223e8SApple OSS Distributions int64_t result; 3610*4f1223e8SApple OSS Distributions __block bool did_get_stackshot = false; 3611*4f1223e8SApple OSS Distributions 3612*4f1223e8SApple OSS Distributions initialize_thread(); // must run before the stackshots to keep parse_stackshot happy 3613*4f1223e8SApple OSS Distributions 3614*4f1223e8SApple OSS Distributions T_LOG("running sysctl to trigger kernel-driven stackshot type 1"); 3615*4f1223e8SApple OSS Distributions result = run_sysctl_test(test_sysctl, 1); 3616*4f1223e8SApple OSS Distributions T_ASSERT_EQ_LLONG(result, 1, "sysctl result indicated success"); 3617*4f1223e8SApple OSS Distributions 3618*4f1223e8SApple OSS Distributions T_LOG("running sysctl to trigger kernel-driven stackshot type 2"); 3619*4f1223e8SApple OSS Distributions result = run_sysctl_test(test_sysctl, 2); 3620*4f1223e8SApple OSS Distributions T_ASSERT_EQ_LLONG(result, 1, "sysctl result indicated success"); 3621*4f1223e8SApple OSS Distributions 3622*4f1223e8SApple OSS Distributions struct scenario scenario = { 3623*4f1223e8SApple OSS Distributions .name = "from_kernel_initiated", 3624*4f1223e8SApple OSS Distributions .flags = STACKSHOT_RETRIEVE_EXISTING_BUFFER, 3625*4f1223e8SApple OSS Distributions }; 3626*4f1223e8SApple OSS Distributions 3627*4f1223e8SApple OSS Distributions T_LOG("attempting to fetch stored in-kernel stackshot"); 3628*4f1223e8SApple OSS Distributions take_stackshot(&scenario, false, ^(void *ssbuf, size_t sslen) { 3629*4f1223e8SApple OSS Distributions T_ASSERT_NOTNULL(ssbuf, "non-null kernel stackshot"); 3630*4f1223e8SApple OSS Distributions T_ASSERT_GT(sslen, 0, "non-zero stackshot size"); 3631*4f1223e8SApple OSS Distributions parse_stackshot(0, ssbuf, sslen, nil); 3632*4f1223e8SApple OSS Distributions did_get_stackshot = true; 3633*4f1223e8SApple OSS Distributions }); 3634*4f1223e8SApple OSS Distributions 3635*4f1223e8SApple OSS Distributions T_ASSERT_TRUE(did_get_stackshot, "got stackshot from kernel type 2"); 3636*4f1223e8SApple OSS Distributions} 3637