4 #include <darwintest.h>
7 #include <kern/kcdata.h>
8 #include <kern/debug.h>
9 #include <kern/block_hint.h>
10 #include <mach/mach.h>
11 #include <mach/mach_init.h>
12 #include <mach/mach_traps.h>
13 #include <mach/message.h>
14 #include <mach/port.h>
15 #include <mach/semaphore.h>
16 #include <mach/task.h>
19 #include <sys/sysctl.h>
20 #include <sys/stackshot.h>
21 #include <sys/types.h>
24 #include <TargetConditionals.h>
26 #if !TARGET_OS_EMBEDDED
32 T_META_NAMESPACE("xnu.scheduler"),
36 #include <Foundation/Foundation.h>
38 #define SENDS_TO_BLOCK 6
40 #define KRWLCK_STORES_EXCL_OWNER 0
42 #define KMUTEX_SYSCTL_CHECK_EXISTS 0
43 #define KMUTEX_SYSCTL_ACQUIRE_WAIT 1
44 #define KMUTEX_SYSCTL_ACQUIRE_NOWAIT 2
45 #define KMUTEX_SYSCTL_SIGNAL 3
46 #define KMUTEX_SYSCTL_TEARDOWN 4
48 #define KRWLCK_SYSCTL_CHECK_EXISTS 0
49 #define KRWLCK_SYSCTL_RACQUIRE_NOWAIT 1
50 #define KRWLCK_SYSCTL_RACQUIRE_WAIT 2
51 #define KRWLCK_SYSCTL_WACQUIRE_NOWAIT 3
52 #define KRWLCK_SYSCTL_WACQUIRE_WAIT 4
53 #define KRWLCK_SYSCTL_SIGNAL 5
54 #define KRWLCK_SYSCTL_TEARDOWN 6
56 static const char kmutex_ctl[] = "debug.test_MutexOwnerCtl";
57 static const char krwlck_ctl[] = "debug.test_RWLockOwnerCtl";
59 static mach_port_t send = MACH_PORT_NULL;
60 static mach_port_t recv = MACH_PORT_NULL;
63 take_stackshot(uint32_t extra_flags, uint64_t since_timestamp)
65 void * stackshot = NULL;
67 uint32_t stackshot_flags = STACKSHOT_SAVE_LOADINFO |
68 STACKSHOT_GET_GLOBAL_MEM_STATS |
69 STACKSHOT_SAVE_IMP_DONATION_PIDS |
70 STACKSHOT_KCDATA_FORMAT;
72 if (since_timestamp != 0)
73 stackshot_flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT;
75 stackshot_flags |= extra_flags;
77 stackshot = stackshot_config_create();
78 T_QUIET; T_ASSERT_NOTNULL(stackshot, "Allocating stackshot config");
80 ret = stackshot_config_set_flags(stackshot, stackshot_flags);
81 T_ASSERT_POSIX_ZERO(ret, "Setting flags on stackshot config");
83 ret = stackshot_config_set_pid(stackshot, getpid());
84 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Setting target pid on stackshot config");
86 if (since_timestamp != 0) {
87 ret = stackshot_config_set_delta_timestamp(stackshot, since_timestamp);
88 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Setting prev snapshot time on stackshot config");
91 for (int retries = NUMRETRIES; retries > 0; retries--) {
92 ret = stackshot_capture_with_config(stackshot);
93 T_QUIET; T_ASSERT_TRUE(ret == 0 || ret == EBUSY || ret == ETIMEDOUT,
94 "Attempting to take stackshot (error %d)...", ret);
95 if (retries == 0 && (ret == EBUSY || ret == ETIMEDOUT))
96 T_ASSERT_FAIL("Failed to take stackshot after %d retries: got %d (%s)", NUMRETRIES, ret, strerror(ret));
104 save_stackshot(void *stackshot, const char *filename)
106 void *buf = stackshot_config_get_stackshot_buffer(stackshot);
107 T_QUIET; T_ASSERT_NOTNULL(buf, "buf");
108 size_t size = stackshot_config_get_stackshot_size(stackshot);
109 FILE *f = fopen(filename, "w");
110 T_QUIET; T_ASSERT_NOTNULL(f, "f");
111 fwrite(buf, size, 1, f);
116 void check_python(void *stackshot, const char *fmt, ...)
118 save_stackshot(stackshot, "/tmp/ss");
120 #if !TARGET_OS_EMBEDDED
123 char *re_string = NULL;
124 vasprintf(&re_string, fmt, args);
126 T_QUIET; T_ASSERT_NOTNULL(re_string, "vasprintf");
128 const char *pcreErrorStr;
130 pcre *re = pcre_compile(re_string, 0, &pcreErrorStr, &pcreErrorOffset, NULL);
131 T_QUIET; T_ASSERT_NOTNULL(re, "pcre_compile");
134 FILE *p = popen("/usr/local/bin/kcdata --pretty /tmp/ss", "r");
135 T_QUIET; T_ASSERT_NOTNULL(p, "popen");
139 ssize_t linesize = getline(&line, &linecap, p);
145 int pcre_ret = pcre_exec(re, NULL, line, strlen(line), 0, 0, NULL, 0);
147 T_LOG("line: %s", line);
152 T_EXPECT_TRUE(found, "found the waitinfo in kcdata.py output");
160 // waitinfo can be NULL, but len must be non-null and point to the length of the waitinfo array.
161 // when the function returns, len will be set to the number of waitinfo structs found in the stackshot.
163 find_blocking_info(void * stackshot, struct stackshot_thread_waitinfo *waitinfo, int *len)
168 NSError *error = nil;
169 NSMutableDictionary *parsed_container = nil;
170 NSArray *parsed_waitinfo = nil;
172 T_QUIET; T_ASSERT_NOTNULL(len, "Length pointer shouldn't be NULL");
176 buf = stackshot_config_get_stackshot_buffer(stackshot);
177 T_QUIET; T_ASSERT_NOTNULL(buf, "Getting stackshot buffer");
178 buflen = stackshot_config_get_stackshot_size(stackshot);
180 kcdata_iter_t iter = kcdata_iter(buf, buflen);
182 T_QUIET; T_ASSERT_TRUE(kcdata_iter_type(iter) == KCDATA_BUFFER_BEGIN_STACKSHOT ||
183 kcdata_iter_type(iter) == KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT,
184 "Checking start of stackshot buffer");
186 iter = kcdata_iter_next(iter);
187 KCDATA_ITER_FOREACH(iter)
189 t = kcdata_iter_type(iter);
191 if (t != KCDATA_TYPE_CONTAINER_BEGIN) {
195 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) {
199 parsed_container = parseKCDataContainer(&iter, &error);
200 T_QUIET; T_ASSERT_TRUE(!error, "Error while parsing container: %d (%s)",
201 (int)error.code, [error.domain UTF8String]);
202 T_QUIET; T_ASSERT_TRUE(parsed_container && !error, "Parsing container");
204 parsed_waitinfo = parsed_container[@"task_snapshots"][@"thread_waitinfo"];
205 for (id elem in parsed_waitinfo) {
206 /* check to see that tid matches expected idle status */
207 uint8_t type = [elem[@"wait_type"] unsignedCharValue];
208 if (type != kThreadWaitNone) {
209 if (waitinfo && *len < oldlen) {
210 struct stackshot_thread_waitinfo *curr = &waitinfo[*len];
211 curr->wait_type = type;
212 curr->owner = [elem[@"owner"] unsignedLongLongValue];
213 curr->waiter = [elem[@"waiter"] unsignedLongLongValue];
214 curr->context = [elem[@"context"] unsignedLongLongValue];
219 [parsed_container release];
223 /* perform various actions with a mutex in kernel memory. note that, since we aren't allowed
224 * to go to user space while still holding a mutex, the lock-acquiring actions in this kernel
225 * sysctl will either lock and immediately release the lock, or lock and wait until a semaphore
226 * is signalled, then unlock. if called with CHECK_EXISTS, returns whether or not the sysctl
227 * exist in the kernel (to determine if we're running with CONFIG_XNUPOST defined). Else,
229 static int kmutex_action(int action)
232 if (action == KMUTEX_SYSCTL_CHECK_EXISTS) {
233 ret = sysctlbyname(krwlck_ctl, NULL, NULL, NULL, 0);
237 char * action_name = "";
239 case KMUTEX_SYSCTL_ACQUIRE_WAIT:
240 action_name = "lock (and wait)";
242 case KMUTEX_SYSCTL_ACQUIRE_NOWAIT:
243 action_name = "lock";
245 case KMUTEX_SYSCTL_SIGNAL:
246 action_name = "signal to holder of";
248 case KMUTEX_SYSCTL_TEARDOWN:
249 action_name = "tear down";
252 T_ASSERT_FAIL("Somebody passed the wrong argument to kmutex_action: %d", action);
256 ret = sysctlbyname(kmutex_ctl, NULL, NULL, &action, sizeof(int));
257 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: %s kernel mutex", action_name);
262 sysctl_kmutex_test_match(uint64_t context)
265 unsigned long long unslid_kmutex_address = 0;
266 size_t addrsize = sizeof(unslid_kmutex_address);
268 ret = sysctlbyname(kmutex_ctl, &unslid_kmutex_address, &addrsize, NULL, 0);
269 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Getting unslid location of kernel mutex. Size is %llu",
270 (unsigned long long)addrsize);
271 T_EXPECT_EQ(context, unslid_kmutex_address,
272 "Context should match unslid location of mutex in kernel memory");
275 /* We don't really care what goes into these messages, we're just sending something to a port. */
277 msg_send_helper(mach_port_t remote_port)
280 mach_msg_header_t * msg = NULL;
282 ret = vm_allocate(mach_task_self(),
283 (vm_address_t *)&msg,
285 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE);
287 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating vm page %p", (void*)msg);
288 msg->msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
289 msg->msgh_size = PAGE_SIZE;
290 msg->msgh_remote_port = remote_port;
291 msg->msgh_local_port = MACH_PORT_NULL;
292 msg->msgh_voucher_port = MACH_PORT_NULL;
294 MACH_SEND_MSG | MACH_MSG_OPTION_NONE,
298 MACH_MSG_TIMEOUT_NONE,
300 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Sending message to port %d", remote_port);
302 vm_deallocate(mach_task_self(), (vm_address_t)msg, PAGE_SIZE);
303 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Deallocating vm page %p", (void*)msg);
307 msg_recv_helper(mach_port_t local_port)
310 mach_msg_size_t size = 2*PAGE_SIZE;
311 mach_msg_header_t * msg = NULL;
312 ret = vm_allocate(mach_task_self(),
313 (vm_address_t *)&msg,
315 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE );
316 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating page %p for message", (void*)msg);
323 MACH_MSG_TIMEOUT_NONE,
325 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Received message on port %d", local_port);
326 ret = vm_deallocate(mach_task_self(), (vm_address_t)msg, PAGE_SIZE);
327 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Deallocating page %p", (void*)msg);
330 /* perform various actions with a rwlock in kernel memory. note that, since we aren't allowed
331 * to go to user space while still holding a rwlock, the lock-acquiring actions in this kernel
332 * sysctl will either lock and immediately release the lock, or lock and wait until a semaphore
333 * is signalled, then unlock. if called with CHECK_EXISTS, returns whether or not the sysctl
334 * exist in the kernel (to determine if we're running with CONFIG_XNUPOST defined). Else,
337 krwlck_action(int action)
340 if (action == KRWLCK_SYSCTL_CHECK_EXISTS) {
341 ret = sysctlbyname(krwlck_ctl, NULL, NULL, NULL, 0);
345 char * action_name = "";
347 case KRWLCK_SYSCTL_RACQUIRE_NOWAIT:
348 action_name = "shared lock";
350 case KRWLCK_SYSCTL_RACQUIRE_WAIT:
351 action_name = "shared lock (and wait)";
353 case KRWLCK_SYSCTL_WACQUIRE_NOWAIT:
354 action_name = "exclusive lock";
356 case KRWLCK_SYSCTL_WACQUIRE_WAIT:
357 action_name = "exclusive lock (and wait)";
359 case KRWLCK_SYSCTL_SIGNAL:
360 action_name = "signal to holder of";
362 case KRWLCK_SYSCTL_TEARDOWN:
363 action_name = "tear down";
366 T_ASSERT_FAIL("Somebody passed the wrong argument to krwlck_action: %d", action);
370 ret = sysctlbyname(krwlck_ctl, NULL, NULL, &action, sizeof(int));
371 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: %s kernel rwlock", action_name);
376 sysctl_krwlck_test_match(uint64_t context)
379 unsigned long long unslid_krwlck_address = 0;
380 size_t addrsize = sizeof(unslid_krwlck_address);
382 ret = sysctlbyname(krwlck_ctl, &unslid_krwlck_address, &addrsize, NULL, 0);
383 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "Getting unslid location of kernel rwlock");
384 T_EXPECT_EQ(context, unslid_krwlck_address, "Context should match unslid location of rwlock in kernel memory");
387 /* "Grabbing" threads: only purpose is to grab a sync primitive and hang. */
390 kmutex_grabbing_thread(void * arg)
393 kmutex_action(KMUTEX_SYSCTL_ACQUIRE_NOWAIT);
398 kmutex_grab_and_wait_thread(void * arg)
401 kmutex_action(KMUTEX_SYSCTL_ACQUIRE_WAIT);
406 sem_grabbing_thread(void * arg)
408 semaphore_t *sem = (semaphore_t *)arg;
409 semaphore_wait(*sem);
414 msg_blocking_thread(void * arg)
417 msg_recv_helper(send);
419 for (int i = 0; i < SENDS_TO_BLOCK; i++)
420 msg_send_helper(recv); // will block on send until message is received
425 ulock_blocking_thread(void * arg)
427 os_unfair_lock_t oul = (os_unfair_lock_t)arg;
428 os_unfair_lock_lock(oul);
429 os_unfair_lock_unlock(oul);
433 // acquires a kernel rwlock for writing, and then waits on a kernel semaphore.
435 krwlck_write_waiting_thread(void * arg)
438 krwlck_action(KRWLCK_SYSCTL_WACQUIRE_WAIT);
442 // attempts to acquire a kernel rwlock for reading, and doesn't wait on a semaphore afterwards.
444 krwlck_read_grabbing_thread(void * arg)
447 krwlck_action(KRWLCK_SYSCTL_RACQUIRE_NOWAIT);
452 pthread_mutex_blocking_thread(void * arg)
454 pthread_mutex_t *mtx = (pthread_mutex_t *)arg;
455 pthread_mutex_lock(mtx);
456 pthread_mutex_unlock(mtx);
461 pthread_rwlck_blocking_thread(void * arg)
463 pthread_rwlock_t *rwlck = (pthread_rwlock_t *)arg;
464 pthread_rwlock_rdlock(rwlck);
465 pthread_rwlock_unlock(rwlck);
470 pthread_cond_blocking_thread(void * arg)
472 pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
473 pthread_cond_t *cond = (pthread_cond_t *)arg;
474 pthread_cond_wait(cond, &mtx);
475 pthread_mutex_unlock(&mtx);
480 * Uses a debug sysctl to initialize a kernel mutex.
482 * The 'waiting' thread grabs this kernel mutex, and immediately waits on a kernel semaphore.
483 * The 'grabbing' thread just attempts to lock the kernel mutex.
484 * When the semaphore is signalled, the 'waiting' thread will unlock the kernel mutex,
485 * giving the opportunity for the 'grabbing' thread to lock it and then immediately unlock it.
486 * This allows us to create a situation in the kernel where we know a thread to be blocked
490 test_kmutex_blocking(void)
494 struct stackshot_thread_waitinfo waitinfo[2] = { { 0 }, { 0 } };
495 uint64_t thread_id = 0;
496 pthread_t grabbing, waiting;
498 T_LOG("Starting %s", __FUNCTION__);
499 ret = pthread_create(&waiting, NULL, kmutex_grab_and_wait_thread, NULL); // thread will block until we signal it
500 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Spawning grab and wait thread");
501 sleep(1); // give time for thread to block
502 ret = pthread_create(&grabbing, NULL, kmutex_grabbing_thread, NULL); // thread should immediately block
503 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Spawning waiting thread");
504 sleep(3); // give (lots of) time for thread to give up spinning on lock
506 void * stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
508 ret = pthread_threadid_np(waiting, &thread_id); // this is the thread that currently holds the kernel mutex
509 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id");
511 check_python(stackshot, "thread \\d+: semaphore port \\w+ with unknown owner");
513 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
515 T_EXPECT_EQ(len, 2, "There should only be two blocking threads");
516 for (int i = 0; i < len; i++) {
517 struct stackshot_thread_waitinfo *curr = &waitinfo[i];
518 if (curr->wait_type == kThreadWaitSemaphore)
520 T_EXPECT_EQ(curr->wait_type, kThreadWaitKernelMutex, "Wait type should match expected KernelMutex value");
521 T_EXPECT_EQ(curr->owner, thread_id, "Thread ID of blocking thread should match 'owner' field in stackshot");
522 sysctl_kmutex_test_match(curr->context);
524 check_python(stackshot, "thread \\d+: kernel mutex %llx owned by thread %lld", curr->context, thread_id);
527 kmutex_action(KMUTEX_SYSCTL_SIGNAL); // waiting thread should now unblock.
528 ret = pthread_join(waiting, NULL);
529 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on waiting thread");
530 ret = pthread_join(grabbing, NULL);
531 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on grabber thread");
532 kmutex_action(KMUTEX_SYSCTL_TEARDOWN);
533 stackshot_config_dealloc(stackshot);
536 /* Initialize a userspace semaphore, and spawn a thread to block on it. */
538 test_semaphore_blocking(void)
542 struct stackshot_thread_waitinfo waitinfo = { 0 };
546 T_LOG("Starting %s", __FUNCTION__);
547 ret = semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, 0);
548 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Creating semaphore");
550 ret = pthread_create(&tid, NULL, sem_grabbing_thread, (void*)&sem); // thread should immediately block
551 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating semaphore grabbing thread");
553 sleep(1); // give time for thread to block
555 void * stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
556 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
557 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
558 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitSemaphore, "Wait type should match expected Semaphore value");
560 pid = (uint64_t)getpid();
561 T_EXPECT_EQ(waitinfo.owner, pid, "Owner value should match process ID");
563 check_python(stackshot, "thread \\d+: semaphore port \\w+ owned by pid %d", (int)pid);
565 ret = semaphore_signal(sem);
566 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Signalling semaphore");
567 ret = pthread_join(tid, NULL);
568 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on grabber thread");
569 ret = semaphore_destroy(mach_task_self(), sem);
570 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Destroying semaphore");
571 stackshot_config_dealloc(stackshot);
574 /* Spawn a process to send a message to, and block while both sending and receiving in different contexts. */
576 test_mach_msg_blocking(void)
580 void *stackshot = NULL;
581 struct stackshot_thread_waitinfo waitinfo = { 0 };
584 T_LOG("Starting %s", __FUNCTION__);
585 ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &send);
586 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating send port");
587 ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &recv);
588 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating recv port");
589 ret = mach_port_insert_right(mach_task_self(), send, send, MACH_MSG_TYPE_MAKE_SEND);
590 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Getting send right to send port");
591 ret = mach_port_insert_right(mach_task_self(), recv, recv, MACH_MSG_TYPE_MAKE_SEND);
592 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Getting send right to recv port");
594 ret = pthread_create(&tid, NULL, msg_blocking_thread, (void*)&send); // thread should block on recv soon
595 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating message blocking thread");
597 sleep(1); // give time for thread to block
598 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
599 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
601 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
602 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPortReceive, "Wait type should match expected PortReceive value");
604 check_python(stackshot, "thread \\d+: mach_msg receive on port \\w+ name %llx", (long long)send);
606 stackshot_config_dealloc(stackshot);
608 msg_send_helper(send); // ping! msg_blocking_thread will now try to send us stuff, and block until we receive.
610 sleep(1); // give time for thread to block
611 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
612 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
613 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
614 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPortSend, "Wait type should match expected PortSend value");
616 check_python(stackshot, "thread \\d+: mach_msg send on port \\w+ owned by pid %d", (int)getpid());
618 stackshot_config_dealloc(stackshot);
620 msg_recv_helper(recv); // thread should block until we receive one of its messages
621 ret = pthread_join(tid, NULL);
622 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on blocking thread");
626 test_ulock_blocking(void)
629 void *stackshot = NULL;
630 uint64_t thread_id = 0;
632 struct os_unfair_lock_s ouls = OS_UNFAIR_LOCK_INIT;
633 os_unfair_lock_t oul = &ouls;
634 struct stackshot_thread_waitinfo waitinfo = { 0 };
637 T_LOG("Starting %s", __FUNCTION__);
638 os_unfair_lock_lock(oul);
639 ret = pthread_create(&tid, NULL, ulock_blocking_thread, (void*)oul);
640 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating ulock blocking thread");
641 sleep(3); // give time for thread to spawn, fall back to kernel for contention, and block
643 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
645 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
646 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
647 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitUserLock, "Wait type should match expected UserLock value");
649 os_unfair_lock_unlock(oul);
650 ret = pthread_join(tid, NULL); // wait for thread to unblock and exit
651 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on blocking thread");
653 ret = pthread_threadid_np(NULL, &thread_id); // this thread is the "owner" of the ulock
654 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id");
655 T_EXPECT_EQ(waitinfo.owner, thread_id, "Thread ID of blocking thread should match 'owner' field in stackshot");
657 check_python(stackshot, "thread \\d+: unfair lock \\w+ owned by thread %lld", thread_id);
658 stackshot_config_dealloc(stackshot);
663 test_krwlock_blocking(void)
666 void *stackshot = NULL;
667 uint64_t thread_id = 0;
668 pthread_t waiting, grabbing;
670 struct stackshot_thread_waitinfo waitinfo[2] = { { 0 }, { 0 } };
672 T_LOG("Starting %s", __FUNCTION__);
673 // this thread should spawn, acquire a kernel rwlock for write, and then wait on a semaphore
674 ret = pthread_create(&waiting, NULL, krwlck_write_waiting_thread, NULL);
675 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating krwlck write waiting thread");
676 sleep(1); // give time for thread to block
677 // this thread should spawn and try to acquire the same kernel rwlock for read, but block
678 ret = pthread_create(&grabbing, NULL, krwlck_read_grabbing_thread, NULL);
679 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating krwlck read grabbing thread");
680 sleep(1); // give time for thread to block
682 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
684 check_python(stackshot, "thread \\d+: semaphore port \\w+ with unknown owner");
686 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
688 T_EXPECT_EQ(len, 2, "There should only be two blocking threads");
689 for (int i = 0; i < len; i++) {
690 struct stackshot_thread_waitinfo *curr = &waitinfo[i];
691 if (curr->wait_type == kThreadWaitSemaphore)
693 T_EXPECT_EQ(curr->wait_type, kThreadWaitKernelRWLockRead, "Wait type should match expected KRWLockRead value");
694 sysctl_krwlck_test_match(curr->context);
696 check_python(stackshot, "thread \\d+: krwlock %llx for reading", curr->context);
698 #if KRWLCK_STORES_EXCL_OWNER /* A future planned enhancement */
699 ret = pthread_threadid_np(waiting, &thread_id); // this is the thread that currently holds the kernel mutex
700 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id");
701 T_EXPECT_EQ(curr->owner, thread_id, "Thread ID of blocking thread should match 'owner' field in stackshot");
703 (void)thread_id; // suppress compiler warning about unused variable
704 #endif /* RWLCK_STORES_EXCL_OWNER */
707 krwlck_action(KRWLCK_SYSCTL_SIGNAL); // pthread should now unblock & finish
708 ret = pthread_join(waiting, NULL);
709 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on waiting thread");
710 ret = pthread_join(grabbing, NULL);
711 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Joining on grabbing thread");
712 krwlck_action(KRWLCK_SYSCTL_TEARDOWN);
713 stackshot_config_dealloc(stackshot);
718 test_pthread_mutex_blocking(void)
721 void *stackshot = NULL;
722 uint64_t thread_id = 0;
724 struct stackshot_thread_waitinfo waitinfo = { 0 };
725 pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
728 T_LOG("Starting %s", __FUNCTION__);
730 ret = pthread_threadid_np(NULL, &thread_id); // this thread is the "owner" of the mutex
731 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Getting integer value of thread id");
733 pthread_mutex_lock(&mtx);
734 ret = pthread_create(&tid, NULL, pthread_mutex_blocking_thread, (void*)&mtx);
735 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating pthread mutex blocking thread");
736 sleep(2); // give time for thread to block
738 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
740 check_python(stackshot, "thread \\d+: pthread mutex %llx owned by thread %lld", &mtx, thread_id);
742 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
743 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
744 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPThreadMutex,
745 "Wait type should match expected PThreadMutex value");
746 stackshot_config_dealloc(stackshot);
748 pthread_mutex_unlock(&mtx);
749 ret = pthread_join(tid, NULL); // wait for thread to unblock and exit
752 T_EXPECT_EQ(waitinfo.owner, thread_id,
753 "Thread ID of blocking thread should match 'owner' field in stackshot");
754 T_EXPECT_EQ(waitinfo.context, (uint64_t)&mtx,
755 "Userspace address of mutex should match 'context' field in stackshot");
759 test_pthread_rwlck_blocking(void)
762 void *stackshot = NULL;
764 struct stackshot_thread_waitinfo waitinfo = { 0 };
765 pthread_rwlock_t rwlck = PTHREAD_RWLOCK_INITIALIZER;
768 T_LOG("Starting %s", __FUNCTION__);
769 pthread_rwlock_wrlock(&rwlck);
770 ret = pthread_create(&tid, NULL, pthread_rwlck_blocking_thread, (void*)&rwlck);
771 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating pthread rwlck blocking thread");
774 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
776 check_python(stackshot, "thread \\d+: pthread rwlock %llx for reading", (long long)&rwlck);
778 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
779 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
780 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPThreadRWLockRead,
781 "Wait type should match expected PThreadRWLockRead value");
782 stackshot_config_dealloc(stackshot);
784 pthread_rwlock_unlock(&rwlck);
785 ret = pthread_join(tid, NULL); // wait for thread to unblock and exit
786 T_EXPECT_EQ(waitinfo.context, (uint64_t)&rwlck,
787 "Userspace address of rwlck should match 'context' field in stackshot");
793 test_pthread_cond_blocking(void)
796 void *stackshot = NULL;
798 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
799 struct stackshot_thread_waitinfo waitinfo = { 0 };
802 T_LOG("Starting %s", __FUNCTION__);
803 ret = pthread_create(&tid, NULL, pthread_cond_blocking_thread, (void*)&cond);
804 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Creating pthread condvar blocking thread");
807 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
809 check_python(stackshot, "thread \\d+: pthread condvar %llx", (long long)&cond);
811 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
812 T_EXPECT_EQ(len, 1, "Only one blocking thread should exist");
813 T_EXPECT_EQ(waitinfo.wait_type, kThreadWaitPThreadCondVar,
814 "Wait type should match expected PThreadCondVar value");
815 stackshot_config_dealloc(stackshot);
817 pthread_cond_signal(&cond);
818 ret = pthread_join(tid, NULL); // wait for thread to unblock and exit
819 T_EXPECT_EQ(waitinfo.context, (uint64_t)&cond,
820 "Userspace address of condvar should match 'context' field in stackshot");
821 pthread_cond_destroy(&cond);
830 T_DECL(stackshot_block_owner_klocks, "tests stackshot block owner for kernel locks") {
831 /* check to see if kmutex sysctl exists before running kmutex test */
832 if (kmutex_action(KMUTEX_SYSCTL_CHECK_EXISTS))
833 test_kmutex_blocking();
834 /* check to see if krwlck sysctl exists before running krwlck test */
835 if (krwlck_action(KRWLCK_SYSCTL_CHECK_EXISTS))
836 test_krwlock_blocking();
837 test_ulock_blocking();
840 T_DECL(stackshot_block_owner_pthread_mutex, "tests stackshot block owner: pthread mutex") {
841 test_pthread_mutex_blocking();
844 T_DECL(stackshot_block_owner_pthread_rwlck, "tests stackshot block owner: pthread rw locks") {
845 test_pthread_rwlck_blocking();
848 T_DECL(stackshot_block_owner_pthread_condvar, "tests stackshot block owner: pthread condvar") {
849 test_pthread_cond_blocking();
852 T_DECL(stackshot_block_owner_semaphore, "tests stackshot block owner: semaphore") {
853 test_semaphore_blocking();
856 T_DECL(stackshot_block_owner_mach_msg, "tests stackshot block owner: mach messaging") {
857 test_mach_msg_blocking();