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