]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/stackshot_block_owner_14362384.m
xnu-4570.71.2.tar.gz
[apple/xnu.git] / tools / tests / darwintests / stackshot_block_owner_14362384.m
1 #ifdef T_NAMESPACE
2 #undef T_NAMESPACE
3 #endif
4 #include <darwintest.h>
5
6 #include <kdd.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>
17 #include <os/lock.h>
18 #include <pthread.h>
19 #include <sys/sysctl.h>
20 #include <sys/stackshot.h>
21 #include <sys/types.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <TargetConditionals.h>
25
26 #if !TARGET_OS_EMBEDDED
27 #include <pcre.h>
28 #endif
29
30
31 T_GLOBAL_META(
32 T_META_NAMESPACE("xnu.scheduler"),
33 T_META_ASROOT(true)
34 );
35
36 #include <Foundation/Foundation.h>
37
38 #define SENDS_TO_BLOCK 6
39 #define NUMRETRIES 5
40 #define KRWLCK_STORES_EXCL_OWNER 0
41
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
47
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
55
56 static const char kmutex_ctl[] = "debug.test_MutexOwnerCtl";
57 static const char krwlck_ctl[] = "debug.test_RWLockOwnerCtl";
58
59 static mach_port_t send = MACH_PORT_NULL;
60 static mach_port_t recv = MACH_PORT_NULL;
61
62 static void *
63 take_stackshot(uint32_t extra_flags, uint64_t since_timestamp)
64 {
65 void * stackshot = NULL;
66 int ret = 0;
67 uint32_t stackshot_flags = STACKSHOT_SAVE_LOADINFO |
68 STACKSHOT_GET_GLOBAL_MEM_STATS |
69 STACKSHOT_SAVE_IMP_DONATION_PIDS |
70 STACKSHOT_KCDATA_FORMAT;
71
72 if (since_timestamp != 0)
73 stackshot_flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT;
74
75 stackshot_flags |= extra_flags;
76
77 stackshot = stackshot_config_create();
78 T_QUIET; T_ASSERT_NOTNULL(stackshot, "Allocating stackshot config");
79
80 ret = stackshot_config_set_flags(stackshot, stackshot_flags);
81 T_ASSERT_POSIX_ZERO(ret, "Setting flags on stackshot config");
82
83 ret = stackshot_config_set_pid(stackshot, getpid());
84 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "Setting target pid on stackshot config");
85
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");
89 }
90
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));
97 if (ret == 0)
98 break;
99 }
100 return stackshot;
101 }
102
103 static void
104 save_stackshot(void *stackshot, const char *filename)
105 {
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);
112 fclose(f);
113 }
114
115 static
116 void check_python(void *stackshot, const char *fmt, ...)
117 {
118 save_stackshot(stackshot, "/tmp/ss");
119
120 #if !TARGET_OS_EMBEDDED
121 va_list args;
122 va_start(args, fmt);
123 char *re_string = NULL;
124 vasprintf(&re_string, fmt, args);
125 va_end(args);
126 T_QUIET; T_ASSERT_NOTNULL(re_string, "vasprintf");
127
128 const char *pcreErrorStr;
129 int pcreErrorOffset;
130 pcre *re = pcre_compile(re_string, 0, &pcreErrorStr, &pcreErrorOffset, NULL);
131 T_QUIET; T_ASSERT_NOTNULL(re, "pcre_compile");
132
133 bool found = false;
134 FILE *p = popen("/usr/local/bin/kcdata --pretty /tmp/ss", "r");
135 T_QUIET; T_ASSERT_NOTNULL(p, "popen");
136 while (1) {
137 char *line = NULL;
138 size_t linecap = 0;
139 ssize_t linesize = getline(&line, &linecap, p);
140 if (linesize < 0) {
141 if (line)
142 free(line);
143 break;
144 }
145 int pcre_ret = pcre_exec(re, NULL, line, strlen(line), 0, 0, NULL, 0);
146 if (pcre_ret == 0){
147 T_LOG("line: %s", line);
148 found = true;
149 }
150 free(line);
151 }
152 T_EXPECT_TRUE(found, "found the waitinfo in kcdata.py output");
153 pclose(p);
154 pcre_free(re);
155 free(re_string);
156 #endif
157 }
158
159
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.
162 static void
163 find_blocking_info(void * stackshot, struct stackshot_thread_waitinfo *waitinfo, int *len)
164 {
165 void *buf = NULL;
166 uint32_t t = 0;
167 uint32_t buflen = 0;
168 NSError *error = nil;
169 NSMutableDictionary *parsed_container = nil;
170 NSArray *parsed_waitinfo = nil;
171
172 T_QUIET; T_ASSERT_NOTNULL(len, "Length pointer shouldn't be NULL");
173 int oldlen = *len;
174 *len = 0;
175
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);
179
180 kcdata_iter_t iter = kcdata_iter(buf, buflen);
181
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");
185
186 iter = kcdata_iter_next(iter);
187 KCDATA_ITER_FOREACH(iter)
188 {
189 t = kcdata_iter_type(iter);
190
191 if (t != KCDATA_TYPE_CONTAINER_BEGIN) {
192 continue;
193 }
194
195 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) {
196 continue;
197 }
198
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");
203
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];
215 }
216 (*len)++;
217 }
218 }
219 [parsed_container release];
220 }
221 }
222
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,
228 * returns 1. */
229 static int kmutex_action(int action)
230 {
231 int ret = 0;
232 if (action == KMUTEX_SYSCTL_CHECK_EXISTS) {
233 ret = sysctlbyname(krwlck_ctl, NULL, NULL, NULL, 0);
234 return !(ret == -1);
235 }
236
237 char * action_name = "";
238 switch(action) {
239 case KMUTEX_SYSCTL_ACQUIRE_WAIT:
240 action_name = "lock (and wait)";
241 break;
242 case KMUTEX_SYSCTL_ACQUIRE_NOWAIT:
243 action_name = "lock";
244 break;
245 case KMUTEX_SYSCTL_SIGNAL:
246 action_name = "signal to holder of";
247 break;
248 case KMUTEX_SYSCTL_TEARDOWN:
249 action_name = "tear down";
250 break;
251 default:
252 T_ASSERT_FAIL("Somebody passed the wrong argument to kmutex_action: %d", action);
253 break;
254 }
255
256 ret = sysctlbyname(kmutex_ctl, NULL, NULL, &action, sizeof(int));
257 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: %s kernel mutex", action_name);
258 return 1;
259 }
260
261 static void
262 sysctl_kmutex_test_match(uint64_t context)
263 {
264 int ret = 0;
265 unsigned long long unslid_kmutex_address = 0;
266 size_t addrsize = sizeof(unslid_kmutex_address);
267
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");
273 }
274
275 /* We don't really care what goes into these messages, we're just sending something to a port. */
276 static void
277 msg_send_helper(mach_port_t remote_port)
278 {
279 int ret;
280 mach_msg_header_t * msg = NULL;
281
282 ret = vm_allocate(mach_task_self(),
283 (vm_address_t *)&msg,
284 PAGE_SIZE,
285 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE);
286
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;
293 ret = mach_msg(msg,
294 MACH_SEND_MSG | MACH_MSG_OPTION_NONE,
295 PAGE_SIZE,
296 0,
297 MACH_PORT_NULL,
298 MACH_MSG_TIMEOUT_NONE,
299 MACH_PORT_NULL);
300 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Sending message to port %d", remote_port);
301
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);
304 }
305
306 static void
307 msg_recv_helper(mach_port_t local_port)
308 {
309 int ret = 0;
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,
314 size,
315 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE );
316 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "Allocating page %p for message", (void*)msg);
317
318 ret = mach_msg(msg,
319 MACH_RCV_MSG,
320 0,
321 size,
322 local_port,
323 MACH_MSG_TIMEOUT_NONE,
324 MACH_PORT_NULL);
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);
328 }
329
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,
335 * returns 1. */
336 static int
337 krwlck_action(int action)
338 {
339 int ret = 0;
340 if (action == KRWLCK_SYSCTL_CHECK_EXISTS) {
341 ret = sysctlbyname(krwlck_ctl, NULL, NULL, NULL, 0);
342 return !(ret == -1);
343 }
344
345 char * action_name = "";
346 switch(action) {
347 case KRWLCK_SYSCTL_RACQUIRE_NOWAIT:
348 action_name = "shared lock";
349 break;
350 case KRWLCK_SYSCTL_RACQUIRE_WAIT:
351 action_name = "shared lock (and wait)";
352 break;
353 case KRWLCK_SYSCTL_WACQUIRE_NOWAIT:
354 action_name = "exclusive lock";
355 break;
356 case KRWLCK_SYSCTL_WACQUIRE_WAIT:
357 action_name = "exclusive lock (and wait)";
358 break;
359 case KRWLCK_SYSCTL_SIGNAL:
360 action_name = "signal to holder of";
361 break;
362 case KRWLCK_SYSCTL_TEARDOWN:
363 action_name = "tear down";
364 break;
365 default:
366 T_ASSERT_FAIL("Somebody passed the wrong argument to krwlck_action: %d", action);
367 break;
368 }
369
370 ret = sysctlbyname(krwlck_ctl, NULL, NULL, &action, sizeof(int));
371 T_ASSERT_POSIX_SUCCESS(ret, "sysctl: %s kernel rwlock", action_name);
372 return 1;
373 }
374
375 static void
376 sysctl_krwlck_test_match(uint64_t context)
377 {
378 int ret = 0;
379 unsigned long long unslid_krwlck_address = 0;
380 size_t addrsize = sizeof(unslid_krwlck_address);
381
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");
385 }
386
387 /* "Grabbing" threads: only purpose is to grab a sync primitive and hang. */
388
389 static void *
390 kmutex_grabbing_thread(void * arg)
391 {
392 (void)arg;
393 kmutex_action(KMUTEX_SYSCTL_ACQUIRE_NOWAIT);
394 return NULL;
395 }
396
397 static void *
398 kmutex_grab_and_wait_thread(void * arg)
399 {
400 (void)arg;
401 kmutex_action(KMUTEX_SYSCTL_ACQUIRE_WAIT);
402 return NULL;
403 }
404
405 static void *
406 sem_grabbing_thread(void * arg)
407 {
408 semaphore_t *sem = (semaphore_t *)arg;
409 semaphore_wait(*sem);
410 return NULL;
411 }
412
413 static void *
414 msg_blocking_thread(void * arg)
415 {
416 (void)arg;
417 msg_recv_helper(send);
418
419 for (int i = 0; i < SENDS_TO_BLOCK; i++)
420 msg_send_helper(recv); // will block on send until message is received
421 return NULL;
422 }
423
424 static void *
425 ulock_blocking_thread(void * arg)
426 {
427 os_unfair_lock_t oul = (os_unfair_lock_t)arg;
428 os_unfair_lock_lock(oul);
429 os_unfair_lock_unlock(oul);
430 return NULL;
431 }
432
433 // acquires a kernel rwlock for writing, and then waits on a kernel semaphore.
434 static void *
435 krwlck_write_waiting_thread(void * arg)
436 {
437 (void)arg;
438 krwlck_action(KRWLCK_SYSCTL_WACQUIRE_WAIT);
439 return NULL;
440 }
441
442 // attempts to acquire a kernel rwlock for reading, and doesn't wait on a semaphore afterwards.
443 static void *
444 krwlck_read_grabbing_thread(void * arg)
445 {
446 (void)arg;
447 krwlck_action(KRWLCK_SYSCTL_RACQUIRE_NOWAIT);
448 return NULL;
449 }
450
451 static void *
452 pthread_mutex_blocking_thread(void * arg)
453 {
454 pthread_mutex_t *mtx = (pthread_mutex_t *)arg;
455 pthread_mutex_lock(mtx);
456 pthread_mutex_unlock(mtx);
457 return NULL;
458 }
459
460 static void *
461 pthread_rwlck_blocking_thread(void * arg)
462 {
463 pthread_rwlock_t *rwlck = (pthread_rwlock_t *)arg;
464 pthread_rwlock_rdlock(rwlck);
465 pthread_rwlock_unlock(rwlck);
466 return NULL;
467 }
468
469 static void *
470 pthread_cond_blocking_thread(void * arg)
471 {
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);
476 return NULL;
477 }
478
479 /*
480 * Uses a debug sysctl to initialize a kernel mutex.
481 *
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
487 * on a kernel mutex.
488 */
489 static void
490 test_kmutex_blocking(void)
491 {
492 int ret = 0;
493 int len = 2;
494 struct stackshot_thread_waitinfo waitinfo[2] = { { 0 }, { 0 } };
495 uint64_t thread_id = 0;
496 pthread_t grabbing, waiting;
497
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
505
506 void * stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
507
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");
510
511 check_python(stackshot, "thread \\d+: semaphore port \\w+ with unknown owner");
512
513 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
514
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)
519 continue;
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);
523
524 check_python(stackshot, "thread \\d+: kernel mutex %llx owned by thread %lld", curr->context, thread_id);
525 }
526
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);
534 }
535
536 /* Initialize a userspace semaphore, and spawn a thread to block on it. */
537 static void
538 test_semaphore_blocking(void)
539 {
540 int ret = 0;
541 semaphore_t sem;
542 struct stackshot_thread_waitinfo waitinfo = { 0 };
543 int len = 1;
544 uint64_t pid = 0;
545
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");
549 pthread_t tid;
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");
552
553 sleep(1); // give time for thread to block
554
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");
559
560 pid = (uint64_t)getpid();
561 T_EXPECT_EQ(waitinfo.owner, pid, "Owner value should match process ID");
562
563 check_python(stackshot, "thread \\d+: semaphore port \\w+ owned by pid %d", (int)pid);
564
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);
572 }
573
574 /* Spawn a process to send a message to, and block while both sending and receiving in different contexts. */
575 static void
576 test_mach_msg_blocking(void)
577 {
578 int ret = 0;
579 pthread_t tid;
580 void *stackshot = NULL;
581 struct stackshot_thread_waitinfo waitinfo = { 0 };
582 int len = 1;
583
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");
593
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");
596
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);
600
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");
603
604 check_python(stackshot, "thread \\d+: mach_msg receive on port \\w+ name %llx", (long long)send);
605
606 stackshot_config_dealloc(stackshot);
607
608 msg_send_helper(send); // ping! msg_blocking_thread will now try to send us stuff, and block until we receive.
609
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");
615
616 check_python(stackshot, "thread \\d+: mach_msg send on port \\w+ owned by pid %d", (int)getpid());
617
618 stackshot_config_dealloc(stackshot);
619
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");
623 }
624
625 static void
626 test_ulock_blocking(void)
627 {
628 int ret = 0;
629 void *stackshot = NULL;
630 uint64_t thread_id = 0;
631 pthread_t tid;
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 };
635 int len = 1;
636
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
642
643 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
644
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");
648
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");
652
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");
656
657 check_python(stackshot, "thread \\d+: unfair lock \\w+ owned by thread %lld", thread_id);
658 stackshot_config_dealloc(stackshot);
659 return;
660 }
661
662 static void
663 test_krwlock_blocking(void)
664 {
665 int ret = 0;
666 void *stackshot = NULL;
667 uint64_t thread_id = 0;
668 pthread_t waiting, grabbing;
669 int len = 2;
670 struct stackshot_thread_waitinfo waitinfo[2] = { { 0 }, { 0 } };
671
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
681
682 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
683
684 check_python(stackshot, "thread \\d+: semaphore port \\w+ with unknown owner");
685
686 find_blocking_info(stackshot, (struct stackshot_thread_waitinfo *)&waitinfo, &len);
687
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)
692 continue;
693 T_EXPECT_EQ(curr->wait_type, kThreadWaitKernelRWLockRead, "Wait type should match expected KRWLockRead value");
694 sysctl_krwlck_test_match(curr->context);
695
696 check_python(stackshot, "thread \\d+: krwlock %llx for reading", curr->context);
697
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");
702 #else
703 (void)thread_id; // suppress compiler warning about unused variable
704 #endif /* RWLCK_STORES_EXCL_OWNER */
705 }
706
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);
714 }
715
716
717 static void
718 test_pthread_mutex_blocking(void)
719 {
720 int ret = 0;
721 void *stackshot = NULL;
722 uint64_t thread_id = 0;
723 pthread_t tid;
724 struct stackshot_thread_waitinfo waitinfo = { 0 };
725 pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
726 int len = 1;
727
728 T_LOG("Starting %s", __FUNCTION__);
729
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");
732
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
737
738 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
739
740 check_python(stackshot, "thread \\d+: pthread mutex %llx owned by thread %lld", &mtx, thread_id);
741
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);
747
748 pthread_mutex_unlock(&mtx);
749 ret = pthread_join(tid, NULL); // wait for thread to unblock and exit
750
751
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");
756 }
757
758 static void
759 test_pthread_rwlck_blocking(void)
760 {
761 int ret = 0;
762 void *stackshot = NULL;
763 pthread_t tid;
764 struct stackshot_thread_waitinfo waitinfo = { 0 };
765 pthread_rwlock_t rwlck = PTHREAD_RWLOCK_INITIALIZER;
766 int len = 1;
767
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");
772 sleep(2);
773
774 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
775
776 check_python(stackshot, "thread \\d+: pthread rwlock %llx for reading", (long long)&rwlck);
777
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);
783
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");
788 }
789
790
791
792 static void
793 test_pthread_cond_blocking(void)
794 {
795 int ret = 0;
796 void *stackshot = NULL;
797 pthread_t tid;
798 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
799 struct stackshot_thread_waitinfo waitinfo = { 0 };
800 int len = 1;
801
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");
805 sleep(2);
806
807 stackshot = take_stackshot(STACKSHOT_THREAD_WAITINFO, 0);
808
809 check_python(stackshot, "thread \\d+: pthread condvar %llx", (long long)&cond);
810
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);
816
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);
822 }
823
824 /*
825 *
826 * Test declarations
827 *
828 */
829
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();
838 }
839
840 T_DECL(stackshot_block_owner_pthread_mutex, "tests stackshot block owner: pthread mutex") {
841 test_pthread_mutex_blocking();
842 }
843
844 T_DECL(stackshot_block_owner_pthread_rwlck, "tests stackshot block owner: pthread rw locks") {
845 test_pthread_rwlck_blocking();
846 }
847
848 T_DECL(stackshot_block_owner_pthread_condvar, "tests stackshot block owner: pthread condvar") {
849 test_pthread_cond_blocking();
850 }
851
852 T_DECL(stackshot_block_owner_semaphore, "tests stackshot block owner: semaphore") {
853 test_semaphore_blocking();
854 }
855
856 T_DECL(stackshot_block_owner_mach_msg, "tests stackshot block owner: mach messaging") {
857 test_mach_msg_blocking();
858 }