1*8d741a5dSApple OSS Distributions#ifdef T_NAMESPACE 2*8d741a5dSApple OSS Distributions#undef T_NAMESPACE 3*8d741a5dSApple OSS Distributions#endif 4*8d741a5dSApple OSS Distributions#include <darwintest.h> 5*8d741a5dSApple OSS Distributions#include <darwintest_utils.h> 6*8d741a5dSApple OSS Distributions 7*8d741a5dSApple OSS Distributions#include <kdd.h> 8*8d741a5dSApple OSS Distributions#include <kern/kcdata.h> 9*8d741a5dSApple OSS Distributions#include <kern/debug.h> 10*8d741a5dSApple OSS Distributions#include <kern/block_hint.h> 11*8d741a5dSApple OSS Distributions#include <mach/mach.h> 12*8d741a5dSApple OSS Distributions#include <mach/mach_init.h> 13*8d741a5dSApple OSS Distributions#include <mach/mach_traps.h> 14*8d741a5dSApple OSS Distributions#include <mach/message.h> 15*8d741a5dSApple OSS Distributions#include <mach/port.h> 16*8d741a5dSApple OSS Distributions#include <mach/semaphore.h> 17*8d741a5dSApple OSS Distributions#include <mach/task.h> 18*8d741a5dSApple OSS Distributions#include <os/lock.h> 19*8d741a5dSApple OSS Distributions#include <pthread.h> 20*8d741a5dSApple OSS Distributions#include <signal.h> 21*8d741a5dSApple OSS Distributions#include <sys/sysctl.h> 22*8d741a5dSApple OSS Distributions#include <sys/stackshot.h> 23*8d741a5dSApple OSS Distributions#include <sys/types.h> 24*8d741a5dSApple OSS Distributions#include <stdlib.h> 25*8d741a5dSApple OSS Distributions#include <unistd.h> 26*8d741a5dSApple OSS Distributions#include <TargetConditionals.h> 27*8d741a5dSApple OSS Distributions 28*8d741a5dSApple OSS Distributions#if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 29*8d741a5dSApple OSS Distributions#include <pcre.h> 30*8d741a5dSApple OSS Distributions#endif 31*8d741a5dSApple OSS Distributions 32*8d741a5dSApple OSS Distributions 33*8d741a5dSApple OSS DistributionsT_GLOBAL_META( 34*8d741a5dSApple OSS Distributions T_META_NAMESPACE("xnu.scheduler"), 35*8d741a5dSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 36*8d741a5dSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("stackshot"), 37*8d741a5dSApple OSS Distributions T_META_OWNER("jonathan_w_adams"), 38*8d741a5dSApple OSS Distributions T_META_ASROOT(true) 39*8d741a5dSApple OSS Distributions); 40*8d741a5dSApple OSS Distributions 41*8d741a5dSApple OSS Distributions#include <Foundation/Foundation.h> 42*8d741a5dSApple OSS Distributions 43*8d741a5dSApple OSS Distributions#define SENDS_TO_BLOCK 6 44*8d741a5dSApple OSS Distributions#define NUMRETRIES 5 45*8d741a5dSApple OSS Distributions#define KRWLCK_STORES_EXCL_OWNER 0 46*8d741a5dSApple OSS Distributions 47*8d741a5dSApple OSS Distributions#define KMUTEX_SYSCTL_CHECK_EXISTS 0 48*8d741a5dSApple OSS Distributions#define KMUTEX_SYSCTL_ACQUIRE_WAIT 1 49*8d741a5dSApple OSS Distributions#define KMUTEX_SYSCTL_ACQUIRE_NOWAIT 2 50*8d741a5dSApple OSS Distributions#define KMUTEX_SYSCTL_SIGNAL 3 51*8d741a5dSApple OSS Distributions#define KMUTEX_SYSCTL_TEARDOWN 4 52*8d741a5dSApple OSS Distributions 53*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_CHECK_EXISTS 0 54*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_RACQUIRE_NOWAIT 1 55*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_RACQUIRE_WAIT 2 56*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_WACQUIRE_NOWAIT 3 57*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_WACQUIRE_WAIT 4 58*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_SIGNAL 5 59*8d741a5dSApple OSS Distributions#define KRWLCK_SYSCTL_TEARDOWN 6 60*8d741a5dSApple OSS Distributions 61*8d741a5dSApple OSS Distributionsstatic const char kmutex_ctl[] = "debug.test_MutexOwnerCtl"; 62*8d741a5dSApple OSS Distributionsstatic const char krwlck_ctl[] = "debug.test_RWLockOwnerCtl"; 63*8d741a5dSApple OSS Distributions 64*8d741a5dSApple OSS Distributionsstatic mach_port_t test_send_port = MACH_PORT_NULL; 65*8d741a5dSApple OSS Distributionsstatic mach_port_t test_recv_port = MACH_PORT_NULL; 66*8d741a5dSApple OSS Distributions 67*8d741a5dSApple OSS Distributionsstatic void * 68*8d741a5dSApple OSS Distributionstake_stackshot(uint32_t extra_flags, uint64_t since_timestamp) 69*8d741a5dSApple OSS Distributions{ 70*8d741a5dSApple OSS Distributions void * stackshot = NULL; 71*8d741a5dSApple OSS Distributions int ret = 0; 72*8d741a5dSApple OSS Distributions uint32_t stackshot_flags = STACKSHOT_SAVE_LOADINFO | 73*8d741a5dSApple OSS Distributions STACKSHOT_GET_GLOBAL_MEM_STATS | 74*8d741a5dSApple OSS Distributions STACKSHOT_SAVE_IMP_DONATION_PIDS | 75*8d741a5dSApple OSS Distributions STACKSHOT_KCDATA_FORMAT; 76*8d741a5dSApple OSS Distributions 77*8d741a5dSApple OSS Distributions if (since_timestamp != 0) 78*8d741a5dSApple OSS Distributions stackshot_flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT; 79*8d741a5dSApple OSS Distributions 80*8d741a5dSApple OSS Distributions stackshot_flags |= extra_flags; 81*8d741a5dSApple OSS Distributions 82*8d741a5dSApple OSS Distributions stackshot = stackshot_config_create(); 83*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(stackshot, "Allocating stackshot config"); 84*8d741a5dSApple OSS Distributions 85*8d741a5dSApple OSS Distributions ret = stackshot_config_set_flags(stackshot, stackshot_flags); 86*8d741a5dSApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "Setting flags on stackshot config"); 87*8d741a5dSApple OSS Distributions 88*8d741a5dSApple OSS Distributions ret = stackshot_config_set_pid(stackshot, getpid()); 89*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Setting target pid on stackshot config"); 90*8d741a5dSApple OSS Distributions 91*8d741a5dSApple OSS Distributions if (since_timestamp != 0) { 92*8d741a5dSApple OSS Distributions ret = stackshot_config_set_delta_timestamp(stackshot, since_timestamp); 93*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Setting prev snapshot time on stackshot config"); 94*8d741a5dSApple OSS Distributions } 95*8d741a5dSApple OSS Distributions 96*8d741a5dSApple OSS Distributions for (int retries = NUMRETRIES; retries > 0; retries--) { 97*8d741a5dSApple OSS Distributions ret = stackshot_capture_with_config(stackshot); 98*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_TRUE(ret == 0 || ret == EBUSY || ret == ETIMEDOUT, 99*8d741a5dSApple OSS Distributions "Attempting to take stackshot (error %d)...", ret); 100*8d741a5dSApple OSS Distributions if (retries == 0 && (ret == EBUSY || ret == ETIMEDOUT)) 101*8d741a5dSApple OSS Distributions T_ASSERT_FAIL("Failed to take stackshot after %d retries: got %d (%s)", NUMRETRIES, ret, strerror(ret)); 102*8d741a5dSApple OSS Distributions if (ret == 0) 103*8d741a5dSApple OSS Distributions break; 104*8d741a5dSApple OSS Distributions } 105*8d741a5dSApple OSS Distributions return stackshot; 106*8d741a5dSApple OSS Distributions} 107*8d741a5dSApple OSS Distributions 108*8d741a5dSApple OSS Distributionsstatic void 109*8d741a5dSApple OSS Distributionssave_stackshot(void *stackshot, const char *filename) 110*8d741a5dSApple OSS Distributions{ 111*8d741a5dSApple OSS Distributions void *buf = stackshot_config_get_stackshot_buffer(stackshot); 112*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(buf, "buf"); 113*8d741a5dSApple OSS Distributions size_t size = stackshot_config_get_stackshot_size(stackshot); 114*8d741a5dSApple OSS Distributions FILE *f = fopen(filename, "w"); 115*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(f, "f"); 116*8d741a5dSApple OSS Distributions fwrite(buf, size, 1, f); 117*8d741a5dSApple OSS Distributions fclose(f); 118*8d741a5dSApple OSS Distributions} 119*8d741a5dSApple OSS Distributions 120*8d741a5dSApple OSS Distributionsstatic 121*8d741a5dSApple OSS Distributionsvoid check_python(void *stackshot, const char *func, const char *fmt, ...) 122*8d741a5dSApple OSS Distributions{ 123*8d741a5dSApple OSS Distributions char sspath[MAXPATHLEN]; 124*8d741a5dSApple OSS Distributions strlcpy(sspath, func, sizeof(sspath)); 125*8d741a5dSApple OSS Distributions strlcat(sspath, ".kcdata", sizeof(sspath)); 126*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(dt_resultfile(sspath, sizeof(sspath)), 127*8d741a5dSApple OSS Distributions "create result file path"); 128*8d741a5dSApple OSS Distributions 129*8d741a5dSApple OSS Distributions save_stackshot(stackshot, sspath); 130*8d741a5dSApple OSS Distributions 131*8d741a5dSApple OSS Distributions#if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 132*8d741a5dSApple OSS Distributions va_list args; 133*8d741a5dSApple OSS Distributions va_start(args, fmt); 134*8d741a5dSApple OSS Distributions char *re_string = NULL; 135*8d741a5dSApple OSS Distributions vasprintf(&re_string, fmt, args); 136*8d741a5dSApple OSS Distributions va_end(args); 137*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(re_string, "vasprintf"); 138*8d741a5dSApple OSS Distributions 139*8d741a5dSApple OSS Distributions const char *pcreErrorStr; 140*8d741a5dSApple OSS Distributions int pcreErrorOffset; 141*8d741a5dSApple OSS Distributions pcre *re = pcre_compile(re_string, 0, &pcreErrorStr, &pcreErrorOffset, NULL); 142*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(re, "pcre_compile"); 143*8d741a5dSApple OSS Distributions 144*8d741a5dSApple OSS Distributions char *kcdata_invoke; 145*8d741a5dSApple OSS Distributions asprintf(&kcdata_invoke, "/usr/local/bin/kcdata --pretty %s", sspath); 146*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(kcdata_invoke, "asprintf"); 147*8d741a5dSApple OSS Distributions 148*8d741a5dSApple OSS Distributions bool found = false; 149*8d741a5dSApple OSS Distributions FILE *p = popen(kcdata_invoke, "r"); 150*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(p, "popen"); 151*8d741a5dSApple OSS Distributions while (1) { 152*8d741a5dSApple OSS Distributions char *line = NULL; 153*8d741a5dSApple OSS Distributions size_t linecap = 0; 154*8d741a5dSApple OSS Distributions ssize_t linesize = getline(&line, &linecap, p); 155*8d741a5dSApple OSS Distributions if (linesize < 0) { 156*8d741a5dSApple OSS Distributions if (line) 157*8d741a5dSApple OSS Distributions free(line); 158*8d741a5dSApple OSS Distributions break; 159*8d741a5dSApple OSS Distributions } 160*8d741a5dSApple OSS Distributions int pcre_ret = pcre_exec(re, NULL, line, strlen(line), 0, 0, NULL, 0); 161*8d741a5dSApple OSS Distributions if (pcre_ret == 0){ 162*8d741a5dSApple OSS Distributions T_LOG("line: %s", line); 163*8d741a5dSApple OSS Distributions found = true; 164*8d741a5dSApple OSS Distributions } 165*8d741a5dSApple OSS Distributions free(line); 166*8d741a5dSApple OSS Distributions } 167*8d741a5dSApple OSS Distributions T_EXPECT_TRUE(found, "found a match to \"%s\" in output of \"%s\"", re_string, kcdata_invoke); 168*8d741a5dSApple OSS Distributions pclose(p); 169*8d741a5dSApple OSS Distributions pcre_free(re); 170*8d741a5dSApple OSS Distributions free(re_string); 171*8d741a5dSApple OSS Distributions free(kcdata_invoke); 172*8d741a5dSApple OSS Distributions#endif 173*8d741a5dSApple OSS Distributions} 174*8d741a5dSApple OSS Distributions 175*8d741a5dSApple OSS Distributions 176*8d741a5dSApple OSS Distributions// waitinfo can be NULL, but len must be non-null and point to the length of the waitinfo array. 177*8d741a5dSApple OSS Distributions// when the function returns, len will be set to the number of waitinfo structs found in the stackshot. 178*8d741a5dSApple OSS Distributionsstatic void 179*8d741a5dSApple OSS Distributionsfind_blocking_info(void * stackshot, struct stackshot_thread_waitinfo *waitinfo, int *len) 180*8d741a5dSApple OSS Distributions{ 181*8d741a5dSApple OSS Distributions void *buf = NULL; 182*8d741a5dSApple OSS Distributions uint32_t t = 0; 183*8d741a5dSApple OSS Distributions uint32_t buflen = 0; 184*8d741a5dSApple OSS Distributions NSError *error = nil; 185*8d741a5dSApple OSS Distributions NSMutableDictionary *parsed_container = nil; 186*8d741a5dSApple OSS Distributions NSArray *parsed_waitinfo = nil; 187*8d741a5dSApple OSS Distributions 188*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(len, "Length pointer shouldn't be NULL"); 189*8d741a5dSApple OSS Distributions int oldlen = *len; 190*8d741a5dSApple OSS Distributions *len = 0; 191*8d741a5dSApple OSS Distributions 192*8d741a5dSApple OSS Distributions buf = stackshot_config_get_stackshot_buffer(stackshot); 193*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(buf, "Getting stackshot buffer"); 194*8d741a5dSApple OSS Distributions buflen = stackshot_config_get_stackshot_size(stackshot); 195*8d741a5dSApple OSS Distributions 196*8d741a5dSApple OSS Distributions kcdata_iter_t iter = kcdata_iter(buf, buflen); 197*8d741a5dSApple OSS Distributions 198*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_TRUE(kcdata_iter_type(iter) == KCDATA_BUFFER_BEGIN_STACKSHOT || 199*8d741a5dSApple OSS Distributions kcdata_iter_type(iter) == KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT, 200*8d741a5dSApple OSS Distributions "Checking start of stackshot buffer"); 201*8d741a5dSApple OSS Distributions 202*8d741a5dSApple OSS Distributions iter = kcdata_iter_next(iter); 203*8d741a5dSApple OSS Distributions KCDATA_ITER_FOREACH(iter) 204*8d741a5dSApple OSS Distributions { 205*8d741a5dSApple OSS Distributions t = kcdata_iter_type(iter); 206*8d741a5dSApple OSS Distributions 207*8d741a5dSApple OSS Distributions if (t != KCDATA_TYPE_CONTAINER_BEGIN) { 208*8d741a5dSApple OSS Distributions continue; 209*8d741a5dSApple OSS Distributions } 210*8d741a5dSApple OSS Distributions 211*8d741a5dSApple OSS Distributions if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) { 212*8d741a5dSApple OSS Distributions continue; 213*8d741a5dSApple OSS Distributions } 214*8d741a5dSApple OSS Distributions 215*8d741a5dSApple OSS Distributions parsed_container = parseKCDataContainer(&iter, &error); 216*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_TRUE(!error, "Error while parsing container: %d (%s)", 217*8d741a5dSApple OSS Distributions (int)error.code, [error.domain UTF8String]); 218*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_TRUE(parsed_container && !error, "Parsing container"); 219*8d741a5dSApple OSS Distributions 220*8d741a5dSApple OSS Distributions parsed_waitinfo = parsed_container[@"task_snapshots"][@"thread_waitinfo"]; 221*8d741a5dSApple OSS Distributions for (id elem in parsed_waitinfo) { 222*8d741a5dSApple OSS Distributions /* check to see that tid matches expected idle status */ 223*8d741a5dSApple OSS Distributions uint8_t type = [elem[@"wait_type"] unsignedCharValue]; 224*8d741a5dSApple OSS Distributions if (type != kThreadWaitNone) { 225*8d741a5dSApple OSS Distributions if (waitinfo && *len < oldlen) { 226*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo *curr = &waitinfo[*len]; 227*8d741a5dSApple OSS Distributions curr->wait_type = type; 228*8d741a5dSApple OSS Distributions curr->owner = [elem[@"owner"] unsignedLongLongValue]; 229*8d741a5dSApple OSS Distributions curr->waiter = [elem[@"waiter"] unsignedLongLongValue]; 230*8d741a5dSApple OSS Distributions curr->context = [elem[@"context"] unsignedLongLongValue]; 231*8d741a5dSApple OSS Distributions } 232*8d741a5dSApple OSS Distributions (*len)++; 233*8d741a5dSApple OSS Distributions } 234*8d741a5dSApple OSS Distributions } 235*8d741a5dSApple OSS Distributions [parsed_container release]; 236*8d741a5dSApple OSS Distributions } 237*8d741a5dSApple OSS Distributions} 238*8d741a5dSApple OSS Distributions 239*8d741a5dSApple OSS Distributions/* perform various actions with a mutex in kernel memory. note that, since we aren't allowed 240*8d741a5dSApple OSS Distributions * to go to user space while still holding a mutex, the lock-acquiring actions in this kernel 241*8d741a5dSApple OSS Distributions * sysctl will either lock and immediately release the lock, or lock and wait until a semaphore 242*8d741a5dSApple OSS Distributions * is signalled, then unlock. if called with CHECK_EXISTS, returns whether or not the sysctl 243*8d741a5dSApple OSS Distributions * exist in the kernel (to determine if we're running with CONFIG_XNUPOST defined). Else, 244*8d741a5dSApple OSS Distributions * returns 1. */ 245*8d741a5dSApple OSS Distributionsstatic int kmutex_action(int action) 246*8d741a5dSApple OSS Distributions{ 247*8d741a5dSApple OSS Distributions int ret = 0; 248*8d741a5dSApple OSS Distributions if (action == KMUTEX_SYSCTL_CHECK_EXISTS) { 249*8d741a5dSApple OSS Distributions ret = sysctlbyname(krwlck_ctl, NULL, NULL, NULL, 0); 250*8d741a5dSApple OSS Distributions return !(ret == -1); 251*8d741a5dSApple OSS Distributions } 252*8d741a5dSApple OSS Distributions 253*8d741a5dSApple OSS Distributions char * action_name = ""; 254*8d741a5dSApple OSS Distributions switch(action) { 255*8d741a5dSApple OSS Distributions case KMUTEX_SYSCTL_ACQUIRE_WAIT: 256*8d741a5dSApple OSS Distributions action_name = "lock (and wait)"; 257*8d741a5dSApple OSS Distributions break; 258*8d741a5dSApple OSS Distributions case KMUTEX_SYSCTL_ACQUIRE_NOWAIT: 259*8d741a5dSApple OSS Distributions action_name = "lock"; 260*8d741a5dSApple OSS Distributions break; 261*8d741a5dSApple OSS Distributions case KMUTEX_SYSCTL_SIGNAL: 262*8d741a5dSApple OSS Distributions action_name = "signal to holder of"; 263*8d741a5dSApple OSS Distributions break; 264*8d741a5dSApple OSS Distributions case KMUTEX_SYSCTL_TEARDOWN: 265*8d741a5dSApple OSS Distributions action_name = "tear down"; 266*8d741a5dSApple OSS Distributions break; 267*8d741a5dSApple OSS Distributions default: 268*8d741a5dSApple OSS Distributions T_ASSERT_FAIL("Somebody passed the wrong argument to kmutex_action: %d", action); 269*8d741a5dSApple OSS Distributions break; 270*8d741a5dSApple OSS Distributions } 271*8d741a5dSApple OSS Distributions 272*8d741a5dSApple OSS Distributions ret = sysctlbyname(kmutex_ctl, NULL, NULL, &action, sizeof(int)); 273*8d741a5dSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctl: %s kernel mutex", action_name); 274*8d741a5dSApple OSS Distributions return 1; 275*8d741a5dSApple OSS Distributions} 276*8d741a5dSApple OSS Distributions 277*8d741a5dSApple OSS Distributionsstatic void 278*8d741a5dSApple OSS Distributionssysctl_kmutex_test_match(uint64_t context) 279*8d741a5dSApple OSS Distributions{ 280*8d741a5dSApple OSS Distributions int ret = 0; 281*8d741a5dSApple OSS Distributions unsigned long long unslid_kmutex_address = 0; 282*8d741a5dSApple OSS Distributions size_t addrsize = sizeof(unslid_kmutex_address); 283*8d741a5dSApple OSS Distributions 284*8d741a5dSApple OSS Distributions ret = sysctlbyname(kmutex_ctl, &unslid_kmutex_address, &addrsize, NULL, 0); 285*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Getting unslid location of kernel mutex. Size is %llu", 286*8d741a5dSApple OSS Distributions (unsigned long long)addrsize); 287*8d741a5dSApple OSS Distributions T_EXPECT_EQ(context, unslid_kmutex_address, 288*8d741a5dSApple OSS Distributions "Context should match unslid location of mutex in kernel memory"); 289*8d741a5dSApple OSS Distributions} 290*8d741a5dSApple OSS Distributions 291*8d741a5dSApple OSS Distributions/* We don't really care what goes into these messages, we're just sending something to a port. */ 292*8d741a5dSApple OSS Distributionsstatic void 293*8d741a5dSApple OSS Distributionsmsg_send_helper(mach_port_t remote_port) 294*8d741a5dSApple OSS Distributions{ 295*8d741a5dSApple OSS Distributions int ret; 296*8d741a5dSApple OSS Distributions mach_msg_header_t * msg = NULL; 297*8d741a5dSApple OSS Distributions 298*8d741a5dSApple OSS Distributions ret = vm_allocate(mach_task_self(), 299*8d741a5dSApple OSS Distributions (vm_address_t *)&msg, 300*8d741a5dSApple OSS Distributions PAGE_SIZE, 301*8d741a5dSApple OSS Distributions VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE); 302*8d741a5dSApple OSS Distributions 303*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating vm page %p", (void*)msg); 304*8d741a5dSApple OSS Distributions msg->msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0); 305*8d741a5dSApple OSS Distributions msg->msgh_size = PAGE_SIZE; 306*8d741a5dSApple OSS Distributions msg->msgh_remote_port = remote_port; 307*8d741a5dSApple OSS Distributions msg->msgh_local_port = MACH_PORT_NULL; 308*8d741a5dSApple OSS Distributions msg->msgh_voucher_port = MACH_PORT_NULL; 309*8d741a5dSApple OSS Distributions ret = mach_msg(msg, 310*8d741a5dSApple OSS Distributions MACH_SEND_MSG | MACH_MSG_OPTION_NONE, 311*8d741a5dSApple OSS Distributions PAGE_SIZE, 312*8d741a5dSApple OSS Distributions 0, 313*8d741a5dSApple OSS Distributions MACH_PORT_NULL, 314*8d741a5dSApple OSS Distributions MACH_MSG_TIMEOUT_NONE, 315*8d741a5dSApple OSS Distributions MACH_PORT_NULL); 316*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Sending message to port %d", remote_port); 317*8d741a5dSApple OSS Distributions 318*8d741a5dSApple OSS Distributions vm_deallocate(mach_task_self(), (vm_address_t)msg, PAGE_SIZE); 319*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Deallocating vm page %p", (void*)msg); 320*8d741a5dSApple OSS Distributions} 321*8d741a5dSApple OSS Distributions 322*8d741a5dSApple OSS Distributionsstatic void 323*8d741a5dSApple OSS Distributionsmsg_recv_helper(mach_port_t local_port) 324*8d741a5dSApple OSS Distributions{ 325*8d741a5dSApple OSS Distributions int ret = 0; 326*8d741a5dSApple OSS Distributions mach_msg_size_t size = 2*PAGE_SIZE; 327*8d741a5dSApple OSS Distributions mach_msg_header_t * msg = NULL; 328*8d741a5dSApple OSS Distributions ret = vm_allocate(mach_task_self(), 329*8d741a5dSApple OSS Distributions (vm_address_t *)&msg, 330*8d741a5dSApple OSS Distributions size, 331*8d741a5dSApple OSS Distributions VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE ); 332*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating page %p for message", (void*)msg); 333*8d741a5dSApple OSS Distributions 334*8d741a5dSApple OSS Distributions ret = mach_msg(msg, 335*8d741a5dSApple OSS Distributions MACH_RCV_MSG, 336*8d741a5dSApple OSS Distributions 0, 337*8d741a5dSApple OSS Distributions size, 338*8d741a5dSApple OSS Distributions local_port, 339*8d741a5dSApple OSS Distributions MACH_MSG_TIMEOUT_NONE, 340*8d741a5dSApple OSS Distributions MACH_PORT_NULL); 341*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Received message on port %d", local_port); 342*8d741a5dSApple OSS Distributions ret = vm_deallocate(mach_task_self(), (vm_address_t)msg, PAGE_SIZE); 343*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Deallocating page %p", (void*)msg); 344*8d741a5dSApple OSS Distributions} 345*8d741a5dSApple OSS Distributions 346*8d741a5dSApple OSS Distributions/* perform various actions with a rwlock in kernel memory. note that, since we aren't allowed 347*8d741a5dSApple OSS Distributions * to go to user space while still holding a rwlock, the lock-acquiring actions in this kernel 348*8d741a5dSApple OSS Distributions * sysctl will either lock and immediately release the lock, or lock and wait until a semaphore 349*8d741a5dSApple OSS Distributions * is signalled, then unlock. if called with CHECK_EXISTS, returns whether or not the sysctl 350*8d741a5dSApple OSS Distributions * exist in the kernel (to determine if we're running with CONFIG_XNUPOST defined). Else, 351*8d741a5dSApple OSS Distributions * returns 1. */ 352*8d741a5dSApple OSS Distributionsstatic int 353*8d741a5dSApple OSS Distributionskrwlck_action(int action) 354*8d741a5dSApple OSS Distributions{ 355*8d741a5dSApple OSS Distributions int ret = 0; 356*8d741a5dSApple OSS Distributions if (action == KRWLCK_SYSCTL_CHECK_EXISTS) { 357*8d741a5dSApple OSS Distributions ret = sysctlbyname(krwlck_ctl, NULL, NULL, NULL, 0); 358*8d741a5dSApple OSS Distributions return !(ret == -1); 359*8d741a5dSApple OSS Distributions } 360*8d741a5dSApple OSS Distributions 361*8d741a5dSApple OSS Distributions char * action_name = ""; 362*8d741a5dSApple OSS Distributions switch(action) { 363*8d741a5dSApple OSS Distributions case KRWLCK_SYSCTL_RACQUIRE_NOWAIT: 364*8d741a5dSApple OSS Distributions action_name = "shared lock"; 365*8d741a5dSApple OSS Distributions break; 366*8d741a5dSApple OSS Distributions case KRWLCK_SYSCTL_RACQUIRE_WAIT: 367*8d741a5dSApple OSS Distributions action_name = "shared lock (and wait)"; 368*8d741a5dSApple OSS Distributions break; 369*8d741a5dSApple OSS Distributions case KRWLCK_SYSCTL_WACQUIRE_NOWAIT: 370*8d741a5dSApple OSS Distributions action_name = "exclusive lock"; 371*8d741a5dSApple OSS Distributions break; 372*8d741a5dSApple OSS Distributions case KRWLCK_SYSCTL_WACQUIRE_WAIT: 373*8d741a5dSApple OSS Distributions action_name = "exclusive lock (and wait)"; 374*8d741a5dSApple OSS Distributions break; 375*8d741a5dSApple OSS Distributions case KRWLCK_SYSCTL_SIGNAL: 376*8d741a5dSApple OSS Distributions action_name = "signal to holder of"; 377*8d741a5dSApple OSS Distributions break; 378*8d741a5dSApple OSS Distributions case KRWLCK_SYSCTL_TEARDOWN: 379*8d741a5dSApple OSS Distributions action_name = "tear down"; 380*8d741a5dSApple OSS Distributions break; 381*8d741a5dSApple OSS Distributions default: 382*8d741a5dSApple OSS Distributions T_ASSERT_FAIL("Somebody passed the wrong argument to krwlck_action: %d", action); 383*8d741a5dSApple OSS Distributions break; 384*8d741a5dSApple OSS Distributions } 385*8d741a5dSApple OSS Distributions 386*8d741a5dSApple OSS Distributions ret = sysctlbyname(krwlck_ctl, NULL, NULL, &action, sizeof(int)); 387*8d741a5dSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctl: %s kernel rwlock", action_name); 388*8d741a5dSApple OSS Distributions return 1; 389*8d741a5dSApple OSS Distributions} 390*8d741a5dSApple OSS Distributions 391*8d741a5dSApple OSS Distributionsstatic void 392*8d741a5dSApple OSS Distributionssysctl_krwlck_test_match(uint64_t context) 393*8d741a5dSApple OSS Distributions{ 394*8d741a5dSApple OSS Distributions int ret = 0; 395*8d741a5dSApple OSS Distributions unsigned long long unslid_krwlck_address = 0; 396*8d741a5dSApple OSS Distributions size_t addrsize = sizeof(unslid_krwlck_address); 397*8d741a5dSApple OSS Distributions 398*8d741a5dSApple OSS Distributions ret = sysctlbyname(krwlck_ctl, &unslid_krwlck_address, &addrsize, NULL, 0); 399*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Getting unslid location of kernel rwlock"); 400*8d741a5dSApple OSS Distributions T_EXPECT_EQ(context, unslid_krwlck_address, "Context should match unslid location of rwlock in kernel memory"); 401*8d741a5dSApple OSS Distributions} 402*8d741a5dSApple OSS Distributions 403*8d741a5dSApple OSS Distributions/* "Grabbing" threads: only purpose is to grab a sync primitive and hang. */ 404*8d741a5dSApple OSS Distributions 405*8d741a5dSApple OSS Distributionsstatic void * 406*8d741a5dSApple OSS Distributionskmutex_grabbing_thread(void * arg) 407*8d741a5dSApple OSS Distributions{ 408*8d741a5dSApple OSS Distributions (void)arg; 409*8d741a5dSApple OSS Distributions kmutex_action(KMUTEX_SYSCTL_ACQUIRE_NOWAIT); 410*8d741a5dSApple OSS Distributions return NULL; 411*8d741a5dSApple OSS Distributions} 412*8d741a5dSApple OSS Distributions 413*8d741a5dSApple OSS Distributionsstatic void * 414*8d741a5dSApple OSS Distributionskmutex_grab_and_wait_thread(void * arg) 415*8d741a5dSApple OSS Distributions{ 416*8d741a5dSApple OSS Distributions (void)arg; 417*8d741a5dSApple OSS Distributions kmutex_action(KMUTEX_SYSCTL_ACQUIRE_WAIT); 418*8d741a5dSApple OSS Distributions return NULL; 419*8d741a5dSApple OSS Distributions} 420*8d741a5dSApple OSS Distributions 421*8d741a5dSApple OSS Distributionsstatic void * 422*8d741a5dSApple OSS Distributionssem_grabbing_thread(void * arg) 423*8d741a5dSApple OSS Distributions{ 424*8d741a5dSApple OSS Distributions semaphore_t *sem = (semaphore_t *)arg; 425*8d741a5dSApple OSS Distributions semaphore_wait(*sem); 426*8d741a5dSApple OSS Distributions return NULL; 427*8d741a5dSApple OSS Distributions} 428*8d741a5dSApple OSS Distributions 429*8d741a5dSApple OSS Distributionsstatic void * 430*8d741a5dSApple OSS Distributionsmsg_blocking_thread(void * arg) 431*8d741a5dSApple OSS Distributions{ 432*8d741a5dSApple OSS Distributions (void)arg; 433*8d741a5dSApple OSS Distributions msg_recv_helper(test_send_port); 434*8d741a5dSApple OSS Distributions 435*8d741a5dSApple OSS Distributions for (int i = 0; i < SENDS_TO_BLOCK; i++) 436*8d741a5dSApple OSS Distributions msg_send_helper(test_recv_port); // will block on test_send_port until message is received 437*8d741a5dSApple OSS Distributions return NULL; 438*8d741a5dSApple OSS Distributions} 439*8d741a5dSApple OSS Distributions 440*8d741a5dSApple OSS Distributionsstatic void * 441*8d741a5dSApple OSS Distributionsulock_blocking_thread(void * arg) 442*8d741a5dSApple OSS Distributions{ 443*8d741a5dSApple OSS Distributions os_unfair_lock_t oul = (os_unfair_lock_t)arg; 444*8d741a5dSApple OSS Distributions os_unfair_lock_lock(oul); 445*8d741a5dSApple OSS Distributions os_unfair_lock_unlock(oul); 446*8d741a5dSApple OSS Distributions return NULL; 447*8d741a5dSApple OSS Distributions} 448*8d741a5dSApple OSS Distributions 449*8d741a5dSApple OSS Distributions// acquires a kernel rwlock for writing, and then waits on a kernel semaphore. 450*8d741a5dSApple OSS Distributionsstatic void * 451*8d741a5dSApple OSS Distributionskrwlck_write_waiting_thread(void * arg) 452*8d741a5dSApple OSS Distributions{ 453*8d741a5dSApple OSS Distributions (void)arg; 454*8d741a5dSApple OSS Distributions krwlck_action(KRWLCK_SYSCTL_WACQUIRE_WAIT); 455*8d741a5dSApple OSS Distributions return NULL; 456*8d741a5dSApple OSS Distributions} 457*8d741a5dSApple OSS Distributions 458*8d741a5dSApple OSS Distributions// attempts to acquire a kernel rwlock for reading, and doesn't wait on a semaphore afterwards. 459*8d741a5dSApple OSS Distributionsstatic void * 460*8d741a5dSApple OSS Distributionskrwlck_read_grabbing_thread(void * arg) 461*8d741a5dSApple OSS Distributions{ 462*8d741a5dSApple OSS Distributions (void)arg; 463*8d741a5dSApple OSS Distributions krwlck_action(KRWLCK_SYSCTL_RACQUIRE_NOWAIT); 464*8d741a5dSApple OSS Distributions return NULL; 465*8d741a5dSApple OSS Distributions} 466*8d741a5dSApple OSS Distributions 467*8d741a5dSApple OSS Distributionsstatic void * 468*8d741a5dSApple OSS Distributionspthread_mutex_blocking_thread(void * arg) 469*8d741a5dSApple OSS Distributions{ 470*8d741a5dSApple OSS Distributions pthread_mutex_t *mtx = (pthread_mutex_t *)arg; 471*8d741a5dSApple OSS Distributions pthread_mutex_lock(mtx); 472*8d741a5dSApple OSS Distributions pthread_mutex_unlock(mtx); 473*8d741a5dSApple OSS Distributions return NULL; 474*8d741a5dSApple OSS Distributions} 475*8d741a5dSApple OSS Distributions 476*8d741a5dSApple OSS Distributionsstatic void * 477*8d741a5dSApple OSS Distributionspthread_rwlck_blocking_thread(void * arg) 478*8d741a5dSApple OSS Distributions{ 479*8d741a5dSApple OSS Distributions pthread_rwlock_t *rwlck = (pthread_rwlock_t *)arg; 480*8d741a5dSApple OSS Distributions pthread_rwlock_rdlock(rwlck); 481*8d741a5dSApple OSS Distributions pthread_rwlock_unlock(rwlck); 482*8d741a5dSApple OSS Distributions return NULL; 483*8d741a5dSApple OSS Distributions} 484*8d741a5dSApple OSS Distributions 485*8d741a5dSApple OSS Distributionsstatic void * 486*8d741a5dSApple OSS Distributionspthread_cond_blocking_thread(void * arg) 487*8d741a5dSApple OSS Distributions{ 488*8d741a5dSApple OSS Distributions pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; 489*8d741a5dSApple OSS Distributions pthread_cond_t *cond = (pthread_cond_t *)arg; 490*8d741a5dSApple OSS Distributions pthread_cond_wait(cond, &mtx); 491*8d741a5dSApple OSS Distributions pthread_mutex_unlock(&mtx); 492*8d741a5dSApple OSS Distributions return NULL; 493*8d741a5dSApple OSS Distributions} 494*8d741a5dSApple OSS Distributions 495*8d741a5dSApple OSS Distributionsstatic void * 496*8d741a5dSApple OSS Distributionswaitpid_blocking_thread(void * arg) 497*8d741a5dSApple OSS Distributions{ 498*8d741a5dSApple OSS Distributions pid_t pid = (pid_t)arg; 499*8d741a5dSApple OSS Distributions 500*8d741a5dSApple OSS Distributions int ret = waitpid(pid, NULL, 0); 501*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Reaping child."); 502*8d741a5dSApple OSS Distributions return NULL; 503*8d741a5dSApple OSS Distributions} 504*8d741a5dSApple OSS Distributions 505*8d741a5dSApple OSS Distributions/* 506*8d741a5dSApple OSS Distributions * Uses a debug sysctl to initialize a kernel mutex. 507*8d741a5dSApple OSS Distributions * 508*8d741a5dSApple OSS Distributions * The 'waiting' thread grabs this kernel mutex, and immediately waits on a kernel semaphore. 509*8d741a5dSApple OSS Distributions * The 'grabbing' thread just attempts to lock the kernel mutex. 510*8d741a5dSApple OSS Distributions * When the semaphore is signalled, the 'waiting' thread will unlock the kernel mutex, 511*8d741a5dSApple OSS Distributions * giving the opportunity for the 'grabbing' thread to lock it and then immediately unlock it. 512*8d741a5dSApple OSS Distributions * This allows us to create a situation in the kernel where we know a thread to be blocked 513*8d741a5dSApple OSS Distributions * on a kernel mutex. 514*8d741a5dSApple OSS Distributions */ 515*8d741a5dSApple OSS Distributionsstatic void 516*8d741a5dSApple OSS Distributionstest_kmutex_blocking(void) 517*8d741a5dSApple OSS Distributions{ 518*8d741a5dSApple OSS Distributions int ret = 0; 519*8d741a5dSApple OSS Distributions int len = 2; 520*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo[2] = { { 0 }, { 0 } }; 521*8d741a5dSApple OSS Distributions uint64_t thread_id = 0; 522*8d741a5dSApple OSS Distributions pthread_t grabbing, waiting; 523*8d741a5dSApple OSS Distributions 524*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 525*8d741a5dSApple OSS Distributions ret = pthread_create(&waiting, NULL, kmutex_grab_and_wait_thread, NULL); // thread will block until we signal it 526*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Spawning grab and wait thread"); 527*8d741a5dSApple OSS Distributions sleep(1); // give time for thread to block 528*8d741a5dSApple OSS Distributions ret = pthread_create(&grabbing, NULL, kmutex_grabbing_thread, NULL); // thread should immediately block 529*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Spawning waiting thread"); 530*8d741a5dSApple OSS Distributions sleep(3); // give (lots of) time for thread to give up spinning on lock 531*8d741a5dSApple OSS Distributions 532*8d741a5dSApple OSS Distributions void * stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 533*8d741a5dSApple OSS Distributions 534*8d741a5dSApple OSS Distributions ret = pthread_threadid_np(waiting, &thread_id); // this is the thread that currently holds the kernel mutex 535*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id"); 536*8d741a5dSApple OSS Distributions 537*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: semaphore port \\w+ with unknown owner"); 538*8d741a5dSApple OSS Distributions 539*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 540*8d741a5dSApple OSS Distributions 541*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 2, "There should only be two blocking threads"); 542*8d741a5dSApple OSS Distributions for (int i = 0; i < len; i++) { 543*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo *curr = &waitinfo[i]; 544*8d741a5dSApple OSS Distributions if (curr->wait_type == kThreadWaitSemaphore) 545*8d741a5dSApple OSS Distributions continue; 546*8d741a5dSApple OSS Distributions T_EXPECT_EQ(curr->wait_type, kThreadWaitKernelMutex, "Wait type should match expected KernelMutex value"); 547*8d741a5dSApple OSS Distributions T_EXPECT_EQ(curr->owner, thread_id, "Thread ID of blocking thread should match 'owner' field in stackshot"); 548*8d741a5dSApple OSS Distributions sysctl_kmutex_test_match(curr->context); 549*8d741a5dSApple OSS Distributions 550*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: kernel mutex %llx owned by thread %lld", curr->context, thread_id); 551*8d741a5dSApple OSS Distributions } 552*8d741a5dSApple OSS Distributions 553*8d741a5dSApple OSS Distributions kmutex_action(KMUTEX_SYSCTL_SIGNAL); // waiting thread should now unblock. 554*8d741a5dSApple OSS Distributions ret = pthread_join(waiting, NULL); 555*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on waiting thread"); 556*8d741a5dSApple OSS Distributions ret = pthread_join(grabbing, NULL); 557*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on grabber thread"); 558*8d741a5dSApple OSS Distributions kmutex_action(KMUTEX_SYSCTL_TEARDOWN); 559*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 560*8d741a5dSApple OSS Distributions} 561*8d741a5dSApple OSS Distributions 562*8d741a5dSApple OSS Distributions/* Initialize a userspace semaphore, and spawn a thread to block on it. */ 563*8d741a5dSApple OSS Distributionsstatic void 564*8d741a5dSApple OSS Distributionstest_semaphore_blocking(void) 565*8d741a5dSApple OSS Distributions{ 566*8d741a5dSApple OSS Distributions int ret = 0; 567*8d741a5dSApple OSS Distributions semaphore_t sem; 568*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 569*8d741a5dSApple OSS Distributions int len = 1; 570*8d741a5dSApple OSS Distributions uint64_t pid = 0; 571*8d741a5dSApple OSS Distributions 572*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 573*8d741a5dSApple OSS Distributions ret = semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, 0); 574*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Creating semaphore"); 575*8d741a5dSApple OSS Distributions pthread_t tid; 576*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, sem_grabbing_thread, (void*)&sem); // thread should immediately block 577*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating semaphore grabbing thread"); 578*8d741a5dSApple OSS Distributions 579*8d741a5dSApple OSS Distributions sleep(1); // give time for thread to block 580*8d741a5dSApple OSS Distributions 581*8d741a5dSApple OSS Distributions void * stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 582*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 583*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 584*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitSemaphore, "Wait type should match expected Semaphore value"); 585*8d741a5dSApple OSS Distributions 586*8d741a5dSApple OSS Distributions pid = (uint64_t)getpid(); 587*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.owner, pid, "Owner value should match process ID"); 588*8d741a5dSApple OSS Distributions 589*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: semaphore port \\w+ owned by pid %d", (int)pid); 590*8d741a5dSApple OSS Distributions 591*8d741a5dSApple OSS Distributions ret = semaphore_signal(sem); 592*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Signalling semaphore"); 593*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); 594*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on grabber thread"); 595*8d741a5dSApple OSS Distributions ret = semaphore_destroy(mach_task_self(), sem); 596*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Destroying semaphore"); 597*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 598*8d741a5dSApple OSS Distributions} 599*8d741a5dSApple OSS Distributions 600*8d741a5dSApple OSS Distributions/* Spawn a process to send a message to, and block while both sending and receiving in different contexts. */ 601*8d741a5dSApple OSS Distributionsstatic void 602*8d741a5dSApple OSS Distributionstest_mach_msg_blocking(void) 603*8d741a5dSApple OSS Distributions{ 604*8d741a5dSApple OSS Distributions int ret = 0; 605*8d741a5dSApple OSS Distributions pthread_t tid; 606*8d741a5dSApple OSS Distributions void *stackshot = NULL; 607*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 608*8d741a5dSApple OSS Distributions int len = 1; 609*8d741a5dSApple OSS Distributions 610*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 611*8d741a5dSApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &test_send_port); 612*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating send port"); 613*8d741a5dSApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &test_recv_port); 614*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating recv port"); 615*8d741a5dSApple OSS Distributions ret = mach_port_insert_right(mach_task_self(), test_send_port, test_send_port, MACH_MSG_TYPE_MAKE_SEND); 616*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Getting send right to send port"); 617*8d741a5dSApple OSS Distributions ret = mach_port_insert_right(mach_task_self(), test_recv_port, test_recv_port, MACH_MSG_TYPE_MAKE_SEND); 618*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Getting send right to recv port"); 619*8d741a5dSApple OSS Distributions 620*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, msg_blocking_thread, (void*)&test_send_port); // thread should block on test_recv_port soon 621*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating message blocking thread"); 622*8d741a5dSApple OSS Distributions 623*8d741a5dSApple OSS Distributions sleep(1); // give time for thread to block 624*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 625*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 626*8d741a5dSApple OSS Distributions 627*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 628*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPortReceive, "Wait type should match expected PortReceive value"); 629*8d741a5dSApple OSS Distributions 630*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: mach_msg receive on port \\w+ name %llx", (long long)test_send_port); 631*8d741a5dSApple OSS Distributions 632*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 633*8d741a5dSApple OSS Distributions 634*8d741a5dSApple OSS Distributions msg_send_helper(test_send_port); // ping! msg_blocking_thread will now try to test_send_port us stuff, and block until we receive. 635*8d741a5dSApple OSS Distributions 636*8d741a5dSApple OSS Distributions sleep(1); // give time for thread to block 637*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 638*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 639*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 640*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPortSend, "Wait type should match expected PortSend value"); 641*8d741a5dSApple OSS Distributions 642*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: mach_msg send on port \\w+ owned by pid %d", (int)getpid()); 643*8d741a5dSApple OSS Distributions 644*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 645*8d741a5dSApple OSS Distributions 646*8d741a5dSApple OSS Distributions msg_recv_helper(test_recv_port); // thread should block until we receive one of its messages 647*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); 648*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on blocking thread"); 649*8d741a5dSApple OSS Distributions} 650*8d741a5dSApple OSS Distributions 651*8d741a5dSApple OSS Distributionsstatic void 652*8d741a5dSApple OSS Distributionstest_ulock_blocking(void) 653*8d741a5dSApple OSS Distributions{ 654*8d741a5dSApple OSS Distributions int ret = 0; 655*8d741a5dSApple OSS Distributions void *stackshot = NULL; 656*8d741a5dSApple OSS Distributions uint64_t thread_id = 0; 657*8d741a5dSApple OSS Distributions pthread_t tid; 658*8d741a5dSApple OSS Distributions struct os_unfair_lock_s ouls = OS_UNFAIR_LOCK_INIT; 659*8d741a5dSApple OSS Distributions os_unfair_lock_t oul = &ouls; 660*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 661*8d741a5dSApple OSS Distributions int len = 1; 662*8d741a5dSApple OSS Distributions 663*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 664*8d741a5dSApple OSS Distributions os_unfair_lock_lock(oul); 665*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, ulock_blocking_thread, (void*)oul); 666*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating ulock blocking thread"); 667*8d741a5dSApple OSS Distributions sleep(3); // give time for thread to spawn, fall back to kernel for contention, and block 668*8d741a5dSApple OSS Distributions 669*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 670*8d741a5dSApple OSS Distributions 671*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 672*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 673*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitUserLock, "Wait type should match expected UserLock value"); 674*8d741a5dSApple OSS Distributions 675*8d741a5dSApple OSS Distributions os_unfair_lock_unlock(oul); 676*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); // wait for thread to unblock and exit 677*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on blocking thread"); 678*8d741a5dSApple OSS Distributions 679*8d741a5dSApple OSS Distributions ret = pthread_threadid_np(NULL, &thread_id); // this thread is the "owner" of the ulock 680*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id"); 681*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.owner, thread_id, "Thread ID of blocking thread should match 'owner' field in stackshot"); 682*8d741a5dSApple OSS Distributions 683*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: unfair lock \\w+ owned by thread %lld", thread_id); 684*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 685*8d741a5dSApple OSS Distributions return; 686*8d741a5dSApple OSS Distributions} 687*8d741a5dSApple OSS Distributions 688*8d741a5dSApple OSS Distributionsstatic void 689*8d741a5dSApple OSS Distributionstest_krwlock_blocking(void) 690*8d741a5dSApple OSS Distributions{ 691*8d741a5dSApple OSS Distributions int ret = 0; 692*8d741a5dSApple OSS Distributions void *stackshot = NULL; 693*8d741a5dSApple OSS Distributions uint64_t thread_id = 0; 694*8d741a5dSApple OSS Distributions pthread_t waiting, grabbing; 695*8d741a5dSApple OSS Distributions int len = 2; 696*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo[2] = { { 0 }, { 0 } }; 697*8d741a5dSApple OSS Distributions 698*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 699*8d741a5dSApple OSS Distributions // this thread should spawn, acquire a kernel rwlock for write, and then wait on a semaphore 700*8d741a5dSApple OSS Distributions ret = pthread_create(&waiting, NULL, krwlck_write_waiting_thread, NULL); 701*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating krwlck write waiting thread"); 702*8d741a5dSApple OSS Distributions sleep(1); // give time for thread to block 703*8d741a5dSApple OSS Distributions // this thread should spawn and try to acquire the same kernel rwlock for read, but block 704*8d741a5dSApple OSS Distributions ret = pthread_create(&grabbing, NULL, krwlck_read_grabbing_thread, NULL); 705*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating krwlck read grabbing thread"); 706*8d741a5dSApple OSS Distributions sleep(1); // give time for thread to block 707*8d741a5dSApple OSS Distributions 708*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 709*8d741a5dSApple OSS Distributions 710*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: semaphore port \\w+ with unknown owner"); 711*8d741a5dSApple OSS Distributions 712*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 713*8d741a5dSApple OSS Distributions 714*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 2, "There should only be two blocking threads"); 715*8d741a5dSApple OSS Distributions for (int i = 0; i < len; i++) { 716*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo *curr = &waitinfo[i]; 717*8d741a5dSApple OSS Distributions if (curr->wait_type == kThreadWaitSemaphore) 718*8d741a5dSApple OSS Distributions continue; 719*8d741a5dSApple OSS Distributions T_EXPECT_EQ(curr->wait_type, kThreadWaitKernelRWLockRead, "Wait type should match expected KRWLockRead value"); 720*8d741a5dSApple OSS Distributions sysctl_krwlck_test_match(curr->context); 721*8d741a5dSApple OSS Distributions 722*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: krwlock %llx for reading", curr->context); 723*8d741a5dSApple OSS Distributions 724*8d741a5dSApple OSS Distributions#if KRWLCK_STORES_EXCL_OWNER /* A future planned enhancement */ 725*8d741a5dSApple OSS Distributions ret = pthread_threadid_np(waiting, &thread_id); // this is the thread that currently holds the kernel mutex 726*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id"); 727*8d741a5dSApple OSS Distributions T_EXPECT_EQ(curr->owner, thread_id, "Thread ID of blocking thread should match 'owner' field in stackshot"); 728*8d741a5dSApple OSS Distributions#else 729*8d741a5dSApple OSS Distributions (void)thread_id; // suppress compiler warning about unused variable 730*8d741a5dSApple OSS Distributions#endif /* RWLCK_STORES_EXCL_OWNER */ 731*8d741a5dSApple OSS Distributions } 732*8d741a5dSApple OSS Distributions 733*8d741a5dSApple OSS Distributions krwlck_action(KRWLCK_SYSCTL_SIGNAL); // pthread should now unblock & finish 734*8d741a5dSApple OSS Distributions ret = pthread_join(waiting, NULL); 735*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on waiting thread"); 736*8d741a5dSApple OSS Distributions ret = pthread_join(grabbing, NULL); 737*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on grabbing thread"); 738*8d741a5dSApple OSS Distributions krwlck_action(KRWLCK_SYSCTL_TEARDOWN); 739*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 740*8d741a5dSApple OSS Distributions} 741*8d741a5dSApple OSS Distributions 742*8d741a5dSApple OSS Distributions 743*8d741a5dSApple OSS Distributionsstatic void 744*8d741a5dSApple OSS Distributionstest_pthread_mutex_blocking(void) 745*8d741a5dSApple OSS Distributions{ 746*8d741a5dSApple OSS Distributions int ret = 0; 747*8d741a5dSApple OSS Distributions void *stackshot = NULL; 748*8d741a5dSApple OSS Distributions uint64_t thread_id = 0; 749*8d741a5dSApple OSS Distributions pthread_t tid; 750*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 751*8d741a5dSApple OSS Distributions pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; 752*8d741a5dSApple OSS Distributions int len = 1; 753*8d741a5dSApple OSS Distributions 754*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 755*8d741a5dSApple OSS Distributions 756*8d741a5dSApple OSS Distributions ret = pthread_threadid_np(NULL, &thread_id); // this thread is the "owner" of the mutex 757*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id"); 758*8d741a5dSApple OSS Distributions 759*8d741a5dSApple OSS Distributions pthread_mutex_lock(&mtx); 760*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, pthread_mutex_blocking_thread, (void*)&mtx); 761*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating pthread mutex blocking thread"); 762*8d741a5dSApple OSS Distributions sleep(2); // give time for thread to block 763*8d741a5dSApple OSS Distributions 764*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 765*8d741a5dSApple OSS Distributions 766*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: pthread mutex %llx owned by thread %lld", &mtx, thread_id); 767*8d741a5dSApple OSS Distributions 768*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 769*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 770*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPThreadMutex, 771*8d741a5dSApple OSS Distributions "Wait type should match expected PThreadMutex value"); 772*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 773*8d741a5dSApple OSS Distributions 774*8d741a5dSApple OSS Distributions pthread_mutex_unlock(&mtx); 775*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); // wait for thread to unblock and exit 776*8d741a5dSApple OSS Distributions 777*8d741a5dSApple OSS Distributions 778*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.owner, thread_id, 779*8d741a5dSApple OSS Distributions "Thread ID of blocking thread should match 'owner' field in stackshot"); 780*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.context, (uint64_t)&mtx, 781*8d741a5dSApple OSS Distributions "Userspace address of mutex should match 'context' field in stackshot"); 782*8d741a5dSApple OSS Distributions} 783*8d741a5dSApple OSS Distributions 784*8d741a5dSApple OSS Distributionsstatic void 785*8d741a5dSApple OSS Distributionstest_pthread_rwlck_blocking(void) 786*8d741a5dSApple OSS Distributions{ 787*8d741a5dSApple OSS Distributions int ret = 0; 788*8d741a5dSApple OSS Distributions void *stackshot = NULL; 789*8d741a5dSApple OSS Distributions pthread_t tid; 790*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 791*8d741a5dSApple OSS Distributions pthread_rwlock_t rwlck = PTHREAD_RWLOCK_INITIALIZER; 792*8d741a5dSApple OSS Distributions int len = 1; 793*8d741a5dSApple OSS Distributions 794*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 795*8d741a5dSApple OSS Distributions pthread_rwlock_wrlock(&rwlck); 796*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, pthread_rwlck_blocking_thread, (void*)&rwlck); 797*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating pthread rwlck blocking thread"); 798*8d741a5dSApple OSS Distributions sleep(2); 799*8d741a5dSApple OSS Distributions 800*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 801*8d741a5dSApple OSS Distributions 802*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: pthread rwlock %llx for reading", (long long)&rwlck); 803*8d741a5dSApple OSS Distributions 804*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 805*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 806*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPThreadRWLockRead, 807*8d741a5dSApple OSS Distributions "Wait type should match expected PThreadRWLockRead value"); 808*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 809*8d741a5dSApple OSS Distributions 810*8d741a5dSApple OSS Distributions pthread_rwlock_unlock(&rwlck); 811*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); // wait for thread to unblock and exit 812*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.context, (uint64_t)&rwlck, 813*8d741a5dSApple OSS Distributions "Userspace address of rwlck should match 'context' field in stackshot"); 814*8d741a5dSApple OSS Distributions} 815*8d741a5dSApple OSS Distributions 816*8d741a5dSApple OSS Distributions 817*8d741a5dSApple OSS Distributions 818*8d741a5dSApple OSS Distributionsstatic void 819*8d741a5dSApple OSS Distributionstest_pthread_cond_blocking(void) 820*8d741a5dSApple OSS Distributions{ 821*8d741a5dSApple OSS Distributions int ret = 0; 822*8d741a5dSApple OSS Distributions void *stackshot = NULL; 823*8d741a5dSApple OSS Distributions pthread_t tid; 824*8d741a5dSApple OSS Distributions pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 825*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 826*8d741a5dSApple OSS Distributions int len = 1; 827*8d741a5dSApple OSS Distributions 828*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 829*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, pthread_cond_blocking_thread, (void*)&cond); 830*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating pthread condvar blocking thread"); 831*8d741a5dSApple OSS Distributions sleep(2); 832*8d741a5dSApple OSS Distributions 833*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 834*8d741a5dSApple OSS Distributions 835*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: pthread condvar %llx", (long long)&cond); 836*8d741a5dSApple OSS Distributions 837*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 838*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 839*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPThreadCondVar, 840*8d741a5dSApple OSS Distributions "Wait type should match expected PThreadCondVar value"); 841*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 842*8d741a5dSApple OSS Distributions 843*8d741a5dSApple OSS Distributions pthread_cond_signal(&cond); 844*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); // wait for thread to unblock and exit 845*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.context, (uint64_t)&cond, 846*8d741a5dSApple OSS Distributions "Userspace address of condvar should match 'context' field in stackshot"); 847*8d741a5dSApple OSS Distributions pthread_cond_destroy(&cond); 848*8d741a5dSApple OSS Distributions} 849*8d741a5dSApple OSS Distributions 850*8d741a5dSApple OSS Distributionsstatic void 851*8d741a5dSApple OSS Distributionstest_waitpid_blocking(void) 852*8d741a5dSApple OSS Distributions{ 853*8d741a5dSApple OSS Distributions int ret = 0; 854*8d741a5dSApple OSS Distributions pid_t pid = 0; 855*8d741a5dSApple OSS Distributions void *stackshot = NULL; 856*8d741a5dSApple OSS Distributions struct stackshot_thread_waitinfo waitinfo = { 0 }; 857*8d741a5dSApple OSS Distributions int len = 1; 858*8d741a5dSApple OSS Distributions pthread_t tid; 859*8d741a5dSApple OSS Distributions 860*8d741a5dSApple OSS Distributions T_LOG("Starting %s", __FUNCTION__); 861*8d741a5dSApple OSS Distributions if ((pid = fork()) == 0) { 862*8d741a5dSApple OSS Distributions pause(); 863*8d741a5dSApple OSS Distributions } else { 864*8d741a5dSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "Running in parent. Child pid is %d", pid); 865*8d741a5dSApple OSS Distributions 866*8d741a5dSApple OSS Distributions sleep(1); // allow enough time for child to run & sleep 867*8d741a5dSApple OSS Distributions ret = pthread_create(&tid, NULL, waitpid_blocking_thread, (void*)pid); 868*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating waitpid blocking thread"); 869*8d741a5dSApple OSS Distributions 870*8d741a5dSApple OSS Distributions sleep(1); // allow enough time for reaping thread to waitpid & block 871*8d741a5dSApple OSS Distributions stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0); 872*8d741a5dSApple OSS Distributions find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len); 873*8d741a5dSApple OSS Distributions T_EXPECT_EQ(len, 1, "Only one blocking thread should exist"); 874*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitOnProcess, 875*8d741a5dSApple OSS Distributions "Wait type should match expected WaitOnProcess value"); 876*8d741a5dSApple OSS Distributions 877*8d741a5dSApple OSS Distributions check_python(stackshot, __func__, "thread \\d+: waitpid, for pid %d", (int)pid); 878*8d741a5dSApple OSS Distributions 879*8d741a5dSApple OSS Distributions stackshot_config_dealloc(stackshot); 880*8d741a5dSApple OSS Distributions T_EXPECT_EQ(waitinfo.owner, pid, 881*8d741a5dSApple OSS Distributions "Process ID of blocking process should match 'owner' field in stackshot"); 882*8d741a5dSApple OSS Distributions 883*8d741a5dSApple OSS Distributions ret = kill(pid, SIGUSR1); // wake up child so waitpid thread can reap it & exit 884*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Send SIGUSR1 to child process"); 885*8d741a5dSApple OSS Distributions ret = pthread_join(tid, NULL); 886*8d741a5dSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Join on waitpid thread"); 887*8d741a5dSApple OSS Distributions } 888*8d741a5dSApple OSS Distributions} 889*8d741a5dSApple OSS Distributions 890*8d741a5dSApple OSS Distributions/* 891*8d741a5dSApple OSS Distributions * 892*8d741a5dSApple OSS Distributions * Test declarations 893*8d741a5dSApple OSS Distributions * 894*8d741a5dSApple OSS Distributions */ 895*8d741a5dSApple OSS Distributions 896*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_klocks, "tests stackshot block owner for kernel locks", T_META_TAG_VM_PREFERRED) { 897*8d741a5dSApple OSS Distributions /* check to see if kmutex sysctl exists before running kmutex test */ 898*8d741a5dSApple OSS Distributions if (kmutex_action(KMUTEX_SYSCTL_CHECK_EXISTS)) 899*8d741a5dSApple OSS Distributions test_kmutex_blocking(); 900*8d741a5dSApple OSS Distributions /* check to see if krwlck sysctl exists before running krwlck test */ 901*8d741a5dSApple OSS Distributions if (krwlck_action(KRWLCK_SYSCTL_CHECK_EXISTS)) 902*8d741a5dSApple OSS Distributions test_krwlock_blocking(); 903*8d741a5dSApple OSS Distributions test_ulock_blocking(); 904*8d741a5dSApple OSS Distributions} 905*8d741a5dSApple OSS Distributions 906*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_pthread_mutex, "tests stackshot block owner: pthread mutex", T_META_TAG_VM_PREFERRED) { 907*8d741a5dSApple OSS Distributions test_pthread_mutex_blocking(); 908*8d741a5dSApple OSS Distributions} 909*8d741a5dSApple OSS Distributions 910*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_pthread_rwlck, "tests stackshot block owner: pthread rw locks", T_META_TAG_VM_PREFERRED) { 911*8d741a5dSApple OSS Distributions test_pthread_rwlck_blocking(); 912*8d741a5dSApple OSS Distributions} 913*8d741a5dSApple OSS Distributions 914*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_pthread_condvar, "tests stackshot block owner: pthread condvar", T_META_TAG_VM_PREFERRED) { 915*8d741a5dSApple OSS Distributions test_pthread_cond_blocking(); 916*8d741a5dSApple OSS Distributions} 917*8d741a5dSApple OSS Distributions 918*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_semaphore, "tests stackshot block owner: semaphore", T_META_TAG_VM_PREFERRED) { 919*8d741a5dSApple OSS Distributions test_semaphore_blocking(); 920*8d741a5dSApple OSS Distributions} 921*8d741a5dSApple OSS Distributions 922*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_mach_msg, "tests stackshot block owner: mach messaging", T_META_TAG_VM_PREFERRED) { 923*8d741a5dSApple OSS Distributions test_mach_msg_blocking(); 924*8d741a5dSApple OSS Distributions} 925*8d741a5dSApple OSS Distributions 926*8d741a5dSApple OSS DistributionsT_DECL(stackshot_block_owner_waitpid, "tests stackshot block owner: waitpid", T_META_TAG_VM_PREFERRED) { 927*8d741a5dSApple OSS Distributions test_waitpid_blocking(); 928*8d741a5dSApple OSS Distributions} 929