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