xref: /xnu-11417.140.69/tests/proc_info_list_kthreads.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * proc_info_list_kthreads
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * list 64 bit thread ids of kernel_task
5*43a90889SApple OSS Distributions  */
6*43a90889SApple OSS Distributions 
7*43a90889SApple OSS Distributions #include <stdio.h>
8*43a90889SApple OSS Distributions #include <stdlib.h>
9*43a90889SApple OSS Distributions #include <assert.h>
10*43a90889SApple OSS Distributions #include <err.h>
11*43a90889SApple OSS Distributions 
12*43a90889SApple OSS Distributions #include <libproc.h>
13*43a90889SApple OSS Distributions #include <strings.h>
14*43a90889SApple OSS Distributions #include <darwintest.h>
15*43a90889SApple OSS Distributions #include <TargetConditionals.h>
16*43a90889SApple OSS Distributions 
17*43a90889SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
18*43a90889SApple OSS Distributions 
19*43a90889SApple OSS Distributions #if TARGET_OS_OSX
20*43a90889SApple OSS Distributions #define _ASROOT false
21*43a90889SApple OSS Distributions #else
22*43a90889SApple OSS Distributions #define _ASROOT true
23*43a90889SApple OSS Distributions #endif
24*43a90889SApple OSS Distributions 
25*43a90889SApple OSS Distributions #define MAX_TRIES 20
26*43a90889SApple OSS Distributions #define EXTRA_THREADS 15
27*43a90889SApple OSS Distributions 
28*43a90889SApple OSS Distributions T_DECL(proc_info_list_kthreads,
29*43a90889SApple OSS Distributions     "Test to verify PROC_PIDLISTTHREADIDS returns kernel thread IDs for pid 0",
30*43a90889SApple OSS Distributions     T_META_ASROOT(_ASROOT),
31*43a90889SApple OSS Distributions     T_META_CHECK_LEAKS(false),
32*43a90889SApple OSS Distributions     T_META_TAG_VM_PREFERRED,
33*43a90889SApple OSS Distributions     T_META_ENABLED(false /* rdar://133956230 */))
34*43a90889SApple OSS Distributions {
35*43a90889SApple OSS Distributions 	int buf_used = 0;
36*43a90889SApple OSS Distributions 
37*43a90889SApple OSS Distributions 	int thread_count = 0;
38*43a90889SApple OSS Distributions 	uint64_t *thread_list = NULL;
39*43a90889SApple OSS Distributions 
40*43a90889SApple OSS Distributions 	/*
41*43a90889SApple OSS Distributions 	 * To use PROC_PIDLISTTHREADIDS, we must pass a buffer of uint64_t's for each thread ID.
42*43a90889SApple OSS Distributions 	 * However, there is a TOCTOU race between asking for the thread count
43*43a90889SApple OSS Distributions 	 * and asking for the array of identifiers.
44*43a90889SApple OSS Distributions 	 *
45*43a90889SApple OSS Distributions 	 * Because the process could have allocated more threads since last we asked
46*43a90889SApple OSS Distributions 	 * how many threads there are, we instead pass an extra slot in the array,
47*43a90889SApple OSS Distributions 	 * and try again if it used that slot.
48*43a90889SApple OSS Distributions 	 */
49*43a90889SApple OSS Distributions 
50*43a90889SApple OSS Distributions 	int attempt = 1;
51*43a90889SApple OSS Distributions 	while (!thread_count && (attempt < MAX_TRIES)) {
52*43a90889SApple OSS Distributions 		struct proc_taskinfo ti;
53*43a90889SApple OSS Distributions 
54*43a90889SApple OSS Distributions 		buf_used = proc_pidinfo(0, PROC_PIDTASKINFO, 0, &ti, sizeof(ti));
55*43a90889SApple OSS Distributions 
56*43a90889SApple OSS Distributions 		T_QUIET; T_WITH_ERRNO; T_ASSERT_GT(buf_used, 0, "proc_pidinfo(PROC_PIDTASKINFO) returned a value > 0");
57*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(buf_used, (int)sizeof(ti), "proc_pidinfo(PROC_PIDTASKINFO) returned size %d == %lu", buf_used, sizeof(ti));
58*43a90889SApple OSS Distributions 
59*43a90889SApple OSS Distributions 		T_LOG("The kernel says it has %d threads", ti.pti_threadnum);
60*43a90889SApple OSS Distributions 
61*43a90889SApple OSS Distributions 		int expected_size  = ti.pti_threadnum * (int)sizeof(uint64_t);
62*43a90889SApple OSS Distributions 		/* tack on five extra to detect newly allocated threads */
63*43a90889SApple OSS Distributions 		int allocated_size = expected_size + EXTRA_THREADS * (int)sizeof(uint64_t);
64*43a90889SApple OSS Distributions 		uint64_t *thread_list_tmp = malloc((size_t)allocated_size);
65*43a90889SApple OSS Distributions 		T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(thread_list_tmp, "malloc(size = %d) failed", allocated_size);
66*43a90889SApple OSS Distributions 
67*43a90889SApple OSS Distributions 		buf_used = proc_pidinfo(0, PROC_PIDLISTTHREADIDS, 0, thread_list_tmp, (int)allocated_size);
68*43a90889SApple OSS Distributions 		T_LOG("proc_pidinfo(PROC_PIDLISTTHREADIDS) buf_used = %d, expected_size = %d", buf_used, expected_size);
69*43a90889SApple OSS Distributions 
70*43a90889SApple OSS Distributions 		if (buf_used == 0) {
71*43a90889SApple OSS Distributions 			T_WITH_ERRNO; T_ASSERT_FAIL("proc_pidinfo(PROC_PIDLISTTHREADIDS) failed");
72*43a90889SApple OSS Distributions 		}
73*43a90889SApple OSS Distributions 		if (buf_used == expected_size) {
74*43a90889SApple OSS Distributions 			/* success, we found the expected number of threads */
75*43a90889SApple OSS Distributions 			thread_list = thread_list_tmp;
76*43a90889SApple OSS Distributions 			thread_count = expected_size / (int)sizeof(uint64_t);
77*43a90889SApple OSS Distributions 		} else if (buf_used < expected_size) {
78*43a90889SApple OSS Distributions 			/* there were fewer threads than we expected, fix up the allocation */
79*43a90889SApple OSS Distributions 			thread_list = realloc(thread_list_tmp, (size_t)buf_used);
80*43a90889SApple OSS Distributions 			thread_count = buf_used / (int)sizeof(uint64_t);
81*43a90889SApple OSS Distributions 			T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(thread_list, "realloc(size = %d) failed", buf_used);
82*43a90889SApple OSS Distributions 		} else if (buf_used > expected_size) {
83*43a90889SApple OSS Distributions 			if (buf_used < allocated_size) {
84*43a90889SApple OSS Distributions 				thread_list = realloc(thread_list_tmp, (size_t)buf_used);
85*43a90889SApple OSS Distributions 				thread_count = buf_used / (int)sizeof(uint64_t);
86*43a90889SApple OSS Distributions 				T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(thread_list, "realloc(size = %d) failed", buf_used);
87*43a90889SApple OSS Distributions 			} else {
88*43a90889SApple OSS Distributions 				/*
89*43a90889SApple OSS Distributions 				 * it used all the extra slots, meaning there are more
90*43a90889SApple OSS Distributions 				 * threads than we thought, try again!
91*43a90889SApple OSS Distributions 				 */
92*43a90889SApple OSS Distributions 				T_LOG("expected %d threads, but saw an extra thread: %d",
93*43a90889SApple OSS Distributions 				    expected_size / (int)sizeof(uint64_t), buf_used / (int)sizeof(uint64_t));
94*43a90889SApple OSS Distributions 				free(thread_list_tmp);
95*43a90889SApple OSS Distributions 			}
96*43a90889SApple OSS Distributions 		}
97*43a90889SApple OSS Distributions 		attempt++;
98*43a90889SApple OSS Distributions 	}
99*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_LE(attempt, MAX_TRIES, "attempt <= MAX_TRIES");
100*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(thread_list, "thread_list != NULL");
101*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_GT(thread_count, 0, "thread_count > 0");
102*43a90889SApple OSS Distributions 
103*43a90889SApple OSS Distributions 	struct proc_threadinfo pthinfo_64;
104*43a90889SApple OSS Distributions 	for (int i = 0; i < thread_count; i++) {
105*43a90889SApple OSS Distributions 		bzero(&pthinfo_64, sizeof(struct proc_threadinfo));
106*43a90889SApple OSS Distributions 		int retval = proc_pidinfo(0, PROC_PIDTHREADID64INFO, thread_list[i],
107*43a90889SApple OSS Distributions 		    (void *)&pthinfo_64, (uint32_t)sizeof(pthinfo_64));
108*43a90889SApple OSS Distributions 		T_QUIET; T_WITH_ERRNO; T_EXPECT_GT(retval, 0, "proc_pidinfo(PROC_PIDTASKINFO) returned %d", retval);
109*43a90889SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(retval, (int)sizeof(pthinfo_64), "proc_pidinfo(PROC_PIDTASKINFO) returned size %d == %lu",
110*43a90889SApple OSS Distributions 		    retval, sizeof(pthinfo_64));
111*43a90889SApple OSS Distributions 	}
112*43a90889SApple OSS Distributions }
113