1*19c3b8c2SApple OSS Distributions #include <darwintest.h>
2*19c3b8c2SApple OSS Distributions #include <darwintest_utils.h>
3*19c3b8c2SApple OSS Distributions #include <errno.h>
4*19c3b8c2SApple OSS Distributions #include <mach/mach.h>
5*19c3b8c2SApple OSS Distributions #include <mach/mach_error.h>
6*19c3b8c2SApple OSS Distributions #include <mach/policy.h>
7*19c3b8c2SApple OSS Distributions #include <mach/task_info.h>
8*19c3b8c2SApple OSS Distributions #include <mach/thread_info.h>
9*19c3b8c2SApple OSS Distributions #include <signal.h>
10*19c3b8c2SApple OSS Distributions #include <stdio.h>
11*19c3b8c2SApple OSS Distributions #include <stdlib.h>
12*19c3b8c2SApple OSS Distributions #include <sys/mman.h>
13*19c3b8c2SApple OSS Distributions #include <sys/sysctl.h>
14*19c3b8c2SApple OSS Distributions #include <unistd.h>
15*19c3b8c2SApple OSS Distributions
16*19c3b8c2SApple OSS Distributions #include "test_utils.h"
17*19c3b8c2SApple OSS Distributions
18*19c3b8c2SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
19*19c3b8c2SApple OSS Distributions
20*19c3b8c2SApple OSS Distributions /* *************************************************************************************
21*19c3b8c2SApple OSS Distributions * Test the task_info API.
22*19c3b8c2SApple OSS Distributions *
23*19c3b8c2SApple OSS Distributions * This is a functional test of the following APIs:
24*19c3b8c2SApple OSS Distributions * TASK_BASIC_INFO_32
25*19c3b8c2SApple OSS Distributions * TASK_BASIC2_INFO_32
26*19c3b8c2SApple OSS Distributions * TASK_BASIC_INFO_64
27*19c3b8c2SApple OSS Distributions * TASK_BASIC_INFO_64_2
28*19c3b8c2SApple OSS Distributions * TASK_POWER_INFO_V2
29*19c3b8c2SApple OSS Distributions * TASK_FLAGS_INFO
30*19c3b8c2SApple OSS Distributions * TASK_AFFINITY_TAG_INFO
31*19c3b8c2SApple OSS Distributions * TASK_THREAD_TIMES_INFO
32*19c3b8c2SApple OSS Distributions * TASK_ABSOLUTE_TIME_INFO
33*19c3b8c2SApple OSS Distributions * <rdar://problem/22242021> Add tests to increase code coverage for the task_info API
34*19c3b8c2SApple OSS Distributions * *************************************************************************************
35*19c3b8c2SApple OSS Distributions */
36*19c3b8c2SApple OSS Distributions #define TESTPHYSFOOTPRINTVAL 5
37*19c3b8c2SApple OSS Distributions #define CANARY 0x0f0f0f0f0f0f0f0fULL
38*19c3b8c2SApple OSS Distributions #if !defined(CONFIG_EMBEDDED)
39*19c3b8c2SApple OSS Distributions #define ABSOLUTE_MIN_USER_TIME_DIFF 150
40*19c3b8c2SApple OSS Distributions #define ABSOLUTE_MIN_SYSTEM_TIME_DIFF 300
41*19c3b8c2SApple OSS Distributions #endif
42*19c3b8c2SApple OSS Distributions
43*19c3b8c2SApple OSS Distributions enum info_kind { INFO_32, INFO_64, INFO_32_2, INFO_64_2, INFO_MACH, INFO_MAX };
44*19c3b8c2SApple OSS Distributions
45*19c3b8c2SApple OSS Distributions enum info_get { GET_SUSPEND_COUNT, GET_RESIDENT_SIZE, GET_VIRTUAL_SIZE, GET_USER_TIME, GET_SYS_TIME, GET_POLICY, GET_MAX_RES };
46*19c3b8c2SApple OSS Distributions
47*19c3b8c2SApple OSS Distributions /*
48*19c3b8c2SApple OSS Distributions * This function uses CPU cycles by doing a factorial computation.
49*19c3b8c2SApple OSS Distributions */
50*19c3b8c2SApple OSS Distributions static void do_factorial_task(void);
51*19c3b8c2SApple OSS Distributions
52*19c3b8c2SApple OSS Distributions void test_task_basic_info_32(void);
53*19c3b8c2SApple OSS Distributions void test_task_basic_info_64(void);
54*19c3b8c2SApple OSS Distributions void task_basic_info_32_debug(void);
55*19c3b8c2SApple OSS Distributions void task_basic2_info_32_warmup(void);
56*19c3b8c2SApple OSS Distributions void test_task_basic_info(enum info_kind kind);
57*19c3b8c2SApple OSS Distributions uint64_t info_get(enum info_kind kind, enum info_get get, void * data);
58*19c3b8c2SApple OSS Distributions
59*19c3b8c2SApple OSS Distributions T_DECL(task_vm_info, "tests task vm info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
60*19c3b8c2SApple OSS Distributions {
61*19c3b8c2SApple OSS Distributions kern_return_t err;
62*19c3b8c2SApple OSS Distributions task_vm_info_data_t vm_info;
63*19c3b8c2SApple OSS Distributions
64*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
65*19c3b8c2SApple OSS Distributions
66*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
67*19c3b8c2SApple OSS Distributions
68*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
69*19c3b8c2SApple OSS Distributions
70*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info return value !=0 for virtual_size\n");
71*19c3b8c2SApple OSS Distributions
72*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.phys_footprint, 0ULL, "task_info return value !=0 for phys_footprint\n");
73*19c3b8c2SApple OSS Distributions
74*19c3b8c2SApple OSS Distributions /*
75*19c3b8c2SApple OSS Distributions * Test the REV0 version of TASK_VM_INFO. It should not change the value of phys_footprint.
76*19c3b8c2SApple OSS Distributions */
77*19c3b8c2SApple OSS Distributions
78*19c3b8c2SApple OSS Distributions count = TASK_VM_INFO_REV0_COUNT;
79*19c3b8c2SApple OSS Distributions vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
80*19c3b8c2SApple OSS Distributions vm_info.min_address = CANARY;
81*19c3b8c2SApple OSS Distributions vm_info.max_address = CANARY;
82*19c3b8c2SApple OSS Distributions
83*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
84*19c3b8c2SApple OSS Distributions
85*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
86*19c3b8c2SApple OSS Distributions
87*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(count, TASK_VM_INFO_REV0_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV0_COUNT", count);
88*19c3b8c2SApple OSS Distributions
89*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev0 call does not return 0 for virtual_size");
90*19c3b8c2SApple OSS Distributions
91*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
92*19c3b8c2SApple OSS Distributions "task_info --rev0 call returned value %llu for vm_info.phys_footprint. Expected %u since this value should not be "
93*19c3b8c2SApple OSS Distributions "modified by rev0",
94*19c3b8c2SApple OSS Distributions vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
95*19c3b8c2SApple OSS Distributions
96*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(vm_info.min_address, CANARY,
97*19c3b8c2SApple OSS Distributions "task_info --rev0 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not "
98*19c3b8c2SApple OSS Distributions "be modified by rev0",
99*19c3b8c2SApple OSS Distributions vm_info.min_address, CANARY);
100*19c3b8c2SApple OSS Distributions
101*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(vm_info.max_address, CANARY,
102*19c3b8c2SApple OSS Distributions "task_info --rev0 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not "
103*19c3b8c2SApple OSS Distributions "be modified by rev0",
104*19c3b8c2SApple OSS Distributions vm_info.max_address, CANARY);
105*19c3b8c2SApple OSS Distributions
106*19c3b8c2SApple OSS Distributions /*
107*19c3b8c2SApple OSS Distributions * Test the REV1 version of TASK_VM_INFO.
108*19c3b8c2SApple OSS Distributions */
109*19c3b8c2SApple OSS Distributions
110*19c3b8c2SApple OSS Distributions count = TASK_VM_INFO_REV1_COUNT;
111*19c3b8c2SApple OSS Distributions vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
112*19c3b8c2SApple OSS Distributions vm_info.min_address = CANARY;
113*19c3b8c2SApple OSS Distributions vm_info.max_address = CANARY;
114*19c3b8c2SApple OSS Distributions
115*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
116*19c3b8c2SApple OSS Distributions
117*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
118*19c3b8c2SApple OSS Distributions
119*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(count, TASK_VM_INFO_REV1_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV1_COUNT", count);
120*19c3b8c2SApple OSS Distributions
121*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev1 call does not return 0 for virtual_size");
122*19c3b8c2SApple OSS Distributions
123*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
124*19c3b8c2SApple OSS Distributions "task_info --rev1 call returned value %llu for vm_info.phys_footprint. Expected value is anything other than %u "
125*19c3b8c2SApple OSS Distributions "since this value should not be modified by rev1",
126*19c3b8c2SApple OSS Distributions vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
127*19c3b8c2SApple OSS Distributions
128*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(vm_info.min_address, CANARY,
129*19c3b8c2SApple OSS Distributions "task_info --rev1 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not "
130*19c3b8c2SApple OSS Distributions "be modified by rev1",
131*19c3b8c2SApple OSS Distributions vm_info.min_address, CANARY);
132*19c3b8c2SApple OSS Distributions
133*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(vm_info.max_address, CANARY,
134*19c3b8c2SApple OSS Distributions "task_info --rev1 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not "
135*19c3b8c2SApple OSS Distributions "be modified by rev1",
136*19c3b8c2SApple OSS Distributions vm_info.max_address, CANARY);
137*19c3b8c2SApple OSS Distributions
138*19c3b8c2SApple OSS Distributions /*
139*19c3b8c2SApple OSS Distributions * Test the REV2 version of TASK_VM_INFO.
140*19c3b8c2SApple OSS Distributions */
141*19c3b8c2SApple OSS Distributions
142*19c3b8c2SApple OSS Distributions count = TASK_VM_INFO_REV2_COUNT;
143*19c3b8c2SApple OSS Distributions vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
144*19c3b8c2SApple OSS Distributions vm_info.min_address = CANARY;
145*19c3b8c2SApple OSS Distributions vm_info.max_address = CANARY;
146*19c3b8c2SApple OSS Distributions
147*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
148*19c3b8c2SApple OSS Distributions
149*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
150*19c3b8c2SApple OSS Distributions
151*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(count, TASK_VM_INFO_REV2_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV2_COUNT\n", count);
152*19c3b8c2SApple OSS Distributions
153*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev2 call does not return 0 for virtual_size\n");
154*19c3b8c2SApple OSS Distributions
155*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
156*19c3b8c2SApple OSS Distributions "task_info --rev2 call returned value %llu for vm_info.phys_footprint. Expected anything other than %u since this "
157*19c3b8c2SApple OSS Distributions "value should be modified by rev2",
158*19c3b8c2SApple OSS Distributions vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
159*19c3b8c2SApple OSS Distributions
160*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.min_address, CANARY,
161*19c3b8c2SApple OSS Distributions "task_info --rev2 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since "
162*19c3b8c2SApple OSS Distributions "this value should be modified by rev2",
163*19c3b8c2SApple OSS Distributions vm_info.min_address, CANARY);
164*19c3b8c2SApple OSS Distributions
165*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.max_address, CANARY,
166*19c3b8c2SApple OSS Distributions "task_info --rev2 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since "
167*19c3b8c2SApple OSS Distributions "this value should be modified by rev2",
168*19c3b8c2SApple OSS Distributions vm_info.max_address, CANARY);
169*19c3b8c2SApple OSS Distributions
170*19c3b8c2SApple OSS Distributions /*
171*19c3b8c2SApple OSS Distributions * Test the REV4 version of TASK_VM_INFO.
172*19c3b8c2SApple OSS Distributions */
173*19c3b8c2SApple OSS Distributions
174*19c3b8c2SApple OSS Distributions count = TASK_VM_INFO_REV4_COUNT;
175*19c3b8c2SApple OSS Distributions vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
176*19c3b8c2SApple OSS Distributions vm_info.min_address = CANARY;
177*19c3b8c2SApple OSS Distributions vm_info.max_address = CANARY;
178*19c3b8c2SApple OSS Distributions vm_info.limit_bytes_remaining = CANARY;
179*19c3b8c2SApple OSS Distributions
180*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
181*19c3b8c2SApple OSS Distributions
182*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
183*19c3b8c2SApple OSS Distributions
184*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(count, TASK_VM_INFO_REV4_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV4_COUNT\n", count);
185*19c3b8c2SApple OSS Distributions
186*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
187*19c3b8c2SApple OSS Distributions "task_info --rev4 call returned value %llu for vm_info.phys_footprint. Expected anything other than %u since this "
188*19c3b8c2SApple OSS Distributions "value should be modified by rev4",
189*19c3b8c2SApple OSS Distributions vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
190*19c3b8c2SApple OSS Distributions
191*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.min_address, CANARY,
192*19c3b8c2SApple OSS Distributions "task_info --rev4 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since "
193*19c3b8c2SApple OSS Distributions "this value should be modified by rev4",
194*19c3b8c2SApple OSS Distributions vm_info.min_address, CANARY);
195*19c3b8c2SApple OSS Distributions
196*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.max_address, CANARY,
197*19c3b8c2SApple OSS Distributions "task_info --rev4 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since "
198*19c3b8c2SApple OSS Distributions "this value should be modified by rev4",
199*19c3b8c2SApple OSS Distributions vm_info.max_address, CANARY);
200*19c3b8c2SApple OSS Distributions
201*19c3b8c2SApple OSS Distributions T_EXPECT_NE(vm_info.limit_bytes_remaining, CANARY,
202*19c3b8c2SApple OSS Distributions "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected anything other than 0x%llx since "
203*19c3b8c2SApple OSS Distributions "this value should be modified by rev4",
204*19c3b8c2SApple OSS Distributions vm_info.limit_bytes_remaining, CANARY);
205*19c3b8c2SApple OSS Distributions }
206*19c3b8c2SApple OSS Distributions
207*19c3b8c2SApple OSS Distributions T_DECL(host_debug_info, "tests host debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
208*19c3b8c2SApple OSS Distributions {
209*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
210*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
211*19c3b8c2SApple OSS Distributions T_QUIET;
212*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
213*19c3b8c2SApple OSS Distributions T_SETUPEND;
214*19c3b8c2SApple OSS Distributions
215*19c3b8c2SApple OSS Distributions kern_return_t err;
216*19c3b8c2SApple OSS Distributions mach_port_t host;
217*19c3b8c2SApple OSS Distributions host_debug_info_internal_data_t debug_info;
218*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = HOST_DEBUG_INFO_INTERNAL_COUNT;
219*19c3b8c2SApple OSS Distributions host = mach_host_self();
220*19c3b8c2SApple OSS Distributions err = host_info(host, HOST_DEBUG_INFO_INTERNAL, (host_info_t)&debug_info, &count);
221*19c3b8c2SApple OSS Distributions
222*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify host_info call succeeded");
223*19c3b8c2SApple OSS Distributions }
224*19c3b8c2SApple OSS Distributions
225*19c3b8c2SApple OSS Distributions T_DECL(task_debug_info, "tests task debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
226*19c3b8c2SApple OSS Distributions {
227*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
228*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
229*19c3b8c2SApple OSS Distributions T_QUIET;
230*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
231*19c3b8c2SApple OSS Distributions T_SETUPEND;
232*19c3b8c2SApple OSS Distributions
233*19c3b8c2SApple OSS Distributions kern_return_t err;
234*19c3b8c2SApple OSS Distributions task_debug_info_internal_data_t debug_info;
235*19c3b8c2SApple OSS Distributions
236*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_DEBUG_INFO_INTERNAL_COUNT;
237*19c3b8c2SApple OSS Distributions
238*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &count);
239*19c3b8c2SApple OSS Distributions
240*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
241*19c3b8c2SApple OSS Distributions }
242*19c3b8c2SApple OSS Distributions
243*19c3b8c2SApple OSS Distributions T_DECL(thread_debug_info, "tests thread debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
244*19c3b8c2SApple OSS Distributions {
245*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
246*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
247*19c3b8c2SApple OSS Distributions T_QUIET;
248*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
249*19c3b8c2SApple OSS Distributions T_SETUPEND;
250*19c3b8c2SApple OSS Distributions
251*19c3b8c2SApple OSS Distributions kern_return_t err;
252*19c3b8c2SApple OSS Distributions thread_debug_info_internal_data_t debug_info;
253*19c3b8c2SApple OSS Distributions
254*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = THREAD_DEBUG_INFO_INTERNAL_COUNT;
255*19c3b8c2SApple OSS Distributions
256*19c3b8c2SApple OSS Distributions err = thread_info(mach_thread_self(), THREAD_DEBUG_INFO_INTERNAL, (thread_info_t)&debug_info, &count);
257*19c3b8c2SApple OSS Distributions
258*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
259*19c3b8c2SApple OSS Distributions }
260*19c3b8c2SApple OSS Distributions
261*19c3b8c2SApple OSS Distributions static void
do_factorial_task()262*19c3b8c2SApple OSS Distributions do_factorial_task()
263*19c3b8c2SApple OSS Distributions {
264*19c3b8c2SApple OSS Distributions int number = 20;
265*19c3b8c2SApple OSS Distributions int factorial = 1;
266*19c3b8c2SApple OSS Distributions int i;
267*19c3b8c2SApple OSS Distributions for (i = 1; i <= number; i++) {
268*19c3b8c2SApple OSS Distributions factorial *= i;
269*19c3b8c2SApple OSS Distributions }
270*19c3b8c2SApple OSS Distributions
271*19c3b8c2SApple OSS Distributions return;
272*19c3b8c2SApple OSS Distributions }
273*19c3b8c2SApple OSS Distributions
274*19c3b8c2SApple OSS Distributions T_DECL(task_thread_times_info, "tests task thread times info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
275*19c3b8c2SApple OSS Distributions {
276*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
277*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
278*19c3b8c2SApple OSS Distributions T_QUIET;
279*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
280*19c3b8c2SApple OSS Distributions T_SETUPEND;
281*19c3b8c2SApple OSS Distributions
282*19c3b8c2SApple OSS Distributions kern_return_t err;
283*19c3b8c2SApple OSS Distributions task_thread_times_info_data_t thread_times_info_data;
284*19c3b8c2SApple OSS Distributions task_thread_times_info_data_t thread_times_info_data_new;
285*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_THREAD_TIMES_INFO_COUNT;
286*19c3b8c2SApple OSS Distributions
287*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count);
288*19c3b8c2SApple OSS Distributions
289*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
290*19c3b8c2SApple OSS Distributions
291*19c3b8c2SApple OSS Distributions do_factorial_task();
292*19c3b8c2SApple OSS Distributions
293*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data_new, &count);
294*19c3b8c2SApple OSS Distributions
295*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
296*19c3b8c2SApple OSS Distributions
297*19c3b8c2SApple OSS Distributions /*
298*19c3b8c2SApple OSS Distributions * The difference is observed to be less than 30 microseconds for user_time
299*19c3b8c2SApple OSS Distributions * and less than 50 microseconds for system_time. This observation was done for over
300*19c3b8c2SApple OSS Distributions * 1000 runs.
301*19c3b8c2SApple OSS Distributions */
302*19c3b8c2SApple OSS Distributions
303*19c3b8c2SApple OSS Distributions T_EXPECT_FALSE((thread_times_info_data_new.user_time.seconds - thread_times_info_data.user_time.seconds) != 0 ||
304*19c3b8c2SApple OSS Distributions (thread_times_info_data_new.system_time.seconds - thread_times_info_data.system_time.seconds) != 0,
305*19c3b8c2SApple OSS Distributions "Tests whether the difference between thread times is greater than the allowed limit");
306*19c3b8c2SApple OSS Distributions
307*19c3b8c2SApple OSS Distributions /*
308*19c3b8c2SApple OSS Distributions * This is a negative case.
309*19c3b8c2SApple OSS Distributions */
310*19c3b8c2SApple OSS Distributions
311*19c3b8c2SApple OSS Distributions count--;
312*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count);
313*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
314*19c3b8c2SApple OSS Distributions "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
315*19c3b8c2SApple OSS Distributions }
316*19c3b8c2SApple OSS Distributions
317*19c3b8c2SApple OSS Distributions T_DECL(task_absolutetime_info, "tests task absolute time info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
318*19c3b8c2SApple OSS Distributions {
319*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
320*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
321*19c3b8c2SApple OSS Distributions T_QUIET;
322*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
323*19c3b8c2SApple OSS Distributions T_SETUPEND;
324*19c3b8c2SApple OSS Distributions
325*19c3b8c2SApple OSS Distributions kern_return_t err;
326*19c3b8c2SApple OSS Distributions uint64_t user_time_diff, system_time_diff;
327*19c3b8c2SApple OSS Distributions task_absolutetime_info_data_t absolute_time_info_data;
328*19c3b8c2SApple OSS Distributions task_absolutetime_info_data_t absolute_time_info_data_new;
329*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_ABSOLUTETIME_INFO_COUNT;
330*19c3b8c2SApple OSS Distributions
331*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data, &count);
332*19c3b8c2SApple OSS Distributions
333*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
334*19c3b8c2SApple OSS Distributions
335*19c3b8c2SApple OSS Distributions do_factorial_task();
336*19c3b8c2SApple OSS Distributions
337*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
338*19c3b8c2SApple OSS Distributions
339*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
340*19c3b8c2SApple OSS Distributions
341*19c3b8c2SApple OSS Distributions user_time_diff = absolute_time_info_data_new.total_user - absolute_time_info_data.total_user;
342*19c3b8c2SApple OSS Distributions system_time_diff = absolute_time_info_data_new.total_system - absolute_time_info_data.total_system;
343*19c3b8c2SApple OSS Distributions
344*19c3b8c2SApple OSS Distributions #if !defined(__arm64__)
345*19c3b8c2SApple OSS Distributions /*
346*19c3b8c2SApple OSS Distributions * On embedded devices the difference is always zero.
347*19c3b8c2SApple OSS Distributions * On non-embedded devices the difference occurs in this range. This was observed over ~10000 runs.
348*19c3b8c2SApple OSS Distributions */
349*19c3b8c2SApple OSS Distributions
350*19c3b8c2SApple OSS Distributions T_EXPECT_FALSE(user_time_diff < ABSOLUTE_MIN_USER_TIME_DIFF || system_time_diff < ABSOLUTE_MIN_SYSTEM_TIME_DIFF,
351*19c3b8c2SApple OSS Distributions "Tests whether the difference between thread times is greater than the expected range");
352*19c3b8c2SApple OSS Distributions #endif
353*19c3b8c2SApple OSS Distributions
354*19c3b8c2SApple OSS Distributions if (absolute_time_info_data.threads_user <= 0) {
355*19c3b8c2SApple OSS Distributions int precise_time_val = 0;
356*19c3b8c2SApple OSS Distributions size_t len = sizeof(size_t);
357*19c3b8c2SApple OSS Distributions
358*19c3b8c2SApple OSS Distributions T_LOG("User threads time is zero. This should only happen rarely and when precise_user_time is off");
359*19c3b8c2SApple OSS Distributions
360*19c3b8c2SApple OSS Distributions err = sysctlbyname("kern.precise_user_kernel_time", &precise_time_val, &len, NULL, 0);
361*19c3b8c2SApple OSS Distributions
362*19c3b8c2SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(err, "performing sysctl to check precise_user_time");
363*19c3b8c2SApple OSS Distributions
364*19c3b8c2SApple OSS Distributions T_LOG("kern.precise_user_kernel_time val = %d", precise_time_val);
365*19c3b8c2SApple OSS Distributions
366*19c3b8c2SApple OSS Distributions T_EXPECT_FALSE(precise_time_val, "user thread time should only be zero when precise_user_kernel_time is disabled");
367*19c3b8c2SApple OSS Distributions } else {
368*19c3b8c2SApple OSS Distributions T_PASS("task_info should return non-zero value for user threads time = %llu", absolute_time_info_data.threads_user);
369*19c3b8c2SApple OSS Distributions }
370*19c3b8c2SApple OSS Distributions
371*19c3b8c2SApple OSS Distributions #if !defined(__arm64__)
372*19c3b8c2SApple OSS Distributions /*
373*19c3b8c2SApple OSS Distributions * On iOS, system threads are always zero. On OS X this value can be some large positive number.
374*19c3b8c2SApple OSS Distributions * There is no real way to estimate the exact amount.
375*19c3b8c2SApple OSS Distributions */
376*19c3b8c2SApple OSS Distributions T_EXPECT_NE(absolute_time_info_data.threads_system, 0ULL,
377*19c3b8c2SApple OSS Distributions "task_info should return non-zero value for system threads time = %llu", absolute_time_info_data.threads_system);
378*19c3b8c2SApple OSS Distributions #endif
379*19c3b8c2SApple OSS Distributions
380*19c3b8c2SApple OSS Distributions /*
381*19c3b8c2SApple OSS Distributions * This is a negative case.
382*19c3b8c2SApple OSS Distributions */
383*19c3b8c2SApple OSS Distributions count--;
384*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
385*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
386*19c3b8c2SApple OSS Distributions "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
387*19c3b8c2SApple OSS Distributions }
388*19c3b8c2SApple OSS Distributions
389*19c3b8c2SApple OSS Distributions T_DECL(task_affinity_tag_info, "tests task_affinity_tag_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
390*19c3b8c2SApple OSS Distributions {
391*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
392*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
393*19c3b8c2SApple OSS Distributions T_QUIET;
394*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
395*19c3b8c2SApple OSS Distributions T_SETUPEND;
396*19c3b8c2SApple OSS Distributions
397*19c3b8c2SApple OSS Distributions kern_return_t err;
398*19c3b8c2SApple OSS Distributions task_affinity_tag_info_data_t affinity_tag_info_data;
399*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_AFFINITY_TAG_INFO_COUNT;
400*19c3b8c2SApple OSS Distributions
401*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
402*19c3b8c2SApple OSS Distributions
403*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
404*19c3b8c2SApple OSS Distributions
405*19c3b8c2SApple OSS Distributions /*
406*19c3b8c2SApple OSS Distributions * The affinity is not set by default, hence expecting a zero value.
407*19c3b8c2SApple OSS Distributions */
408*19c3b8c2SApple OSS Distributions T_ASSERT_FALSE(affinity_tag_info_data.min != 0 || affinity_tag_info_data.max != 0,
409*19c3b8c2SApple OSS Distributions "task_info call returns non-zero min or max value");
410*19c3b8c2SApple OSS Distributions
411*19c3b8c2SApple OSS Distributions /*
412*19c3b8c2SApple OSS Distributions * This is a negative case.
413*19c3b8c2SApple OSS Distributions */
414*19c3b8c2SApple OSS Distributions count--;
415*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
416*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
417*19c3b8c2SApple OSS Distributions "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
418*19c3b8c2SApple OSS Distributions }
419*19c3b8c2SApple OSS Distributions
420*19c3b8c2SApple OSS Distributions T_DECL(task_flags_info, "tests task_flags_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
421*19c3b8c2SApple OSS Distributions {
422*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
423*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
424*19c3b8c2SApple OSS Distributions T_QUIET;
425*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
426*19c3b8c2SApple OSS Distributions T_SETUPEND;
427*19c3b8c2SApple OSS Distributions
428*19c3b8c2SApple OSS Distributions kern_return_t err;
429*19c3b8c2SApple OSS Distributions task_flags_info_data_t flags_info_data;
430*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_FLAGS_INFO_COUNT;
431*19c3b8c2SApple OSS Distributions
432*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
433*19c3b8c2SApple OSS Distributions
434*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
435*19c3b8c2SApple OSS Distributions
436*19c3b8c2SApple OSS Distributions /* Change for 32-bit arch possibility?*/
437*19c3b8c2SApple OSS Distributions T_ASSERT_EQ((flags_info_data.flags & (unsigned int)(~(TF_LP64 | TF_64B_DATA))), 0U,
438*19c3b8c2SApple OSS Distributions "task_info should only give out 64-bit addr/data flags");
439*19c3b8c2SApple OSS Distributions
440*19c3b8c2SApple OSS Distributions /*
441*19c3b8c2SApple OSS Distributions * This is a negative case.
442*19c3b8c2SApple OSS Distributions */
443*19c3b8c2SApple OSS Distributions
444*19c3b8c2SApple OSS Distributions count--;
445*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
446*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
447*19c3b8c2SApple OSS Distributions "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
448*19c3b8c2SApple OSS Distributions }
449*19c3b8c2SApple OSS Distributions
450*19c3b8c2SApple OSS Distributions T_DECL(task_power_info_v2, "tests task_power_info_v2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
451*19c3b8c2SApple OSS Distributions {
452*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
453*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
454*19c3b8c2SApple OSS Distributions T_QUIET;
455*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
456*19c3b8c2SApple OSS Distributions T_SETUPEND;
457*19c3b8c2SApple OSS Distributions
458*19c3b8c2SApple OSS Distributions kern_return_t err;
459*19c3b8c2SApple OSS Distributions task_power_info_v2_data_t power_info_data_v2;
460*19c3b8c2SApple OSS Distributions task_power_info_v2_data_t power_info_data_v2_new;
461*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = TASK_POWER_INFO_V2_COUNT;
462*19c3b8c2SApple OSS Distributions
463*19c3b8c2SApple OSS Distributions sleep(1);
464*19c3b8c2SApple OSS Distributions
465*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
466*19c3b8c2SApple OSS Distributions
467*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
468*19c3b8c2SApple OSS Distributions
469*19c3b8c2SApple OSS Distributions T_ASSERT_LE(power_info_data_v2.gpu_energy.task_gpu_utilisation, 0ULL,
470*19c3b8c2SApple OSS Distributions "verified task_info call shows zero GPU utilization for non-GPU task");
471*19c3b8c2SApple OSS Distributions
472*19c3b8c2SApple OSS Distributions do_factorial_task();
473*19c3b8c2SApple OSS Distributions
474*19c3b8c2SApple OSS Distributions /*
475*19c3b8c2SApple OSS Distributions * Verify the cpu_energy parameters.
476*19c3b8c2SApple OSS Distributions */
477*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2_new, &count);
478*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
479*19c3b8c2SApple OSS Distributions
480*19c3b8c2SApple OSS Distributions #if !defined(__arm64__)
481*19c3b8c2SApple OSS Distributions /*
482*19c3b8c2SApple OSS Distributions * iOS does not have system_time.
483*19c3b8c2SApple OSS Distributions */
484*19c3b8c2SApple OSS Distributions T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_user, power_info_data_v2.cpu_energy.total_user,
485*19c3b8c2SApple OSS Distributions "task_info call returns valid user time");
486*19c3b8c2SApple OSS Distributions T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_system, power_info_data_v2.cpu_energy.total_system,
487*19c3b8c2SApple OSS Distributions "task_info call returns valid system time");
488*19c3b8c2SApple OSS Distributions #endif
489*19c3b8c2SApple OSS Distributions
490*19c3b8c2SApple OSS Distributions T_ASSERT_GE(power_info_data_v2.cpu_energy.task_interrupt_wakeups, 1ULL,
491*19c3b8c2SApple OSS Distributions "verify task_info call returns non-zero value for interrupt_wakeup (ret value = %llu)",
492*19c3b8c2SApple OSS Distributions power_info_data_v2.cpu_energy.task_interrupt_wakeups);
493*19c3b8c2SApple OSS Distributions
494*19c3b8c2SApple OSS Distributions #if !defined(__arm64__)
495*19c3b8c2SApple OSS Distributions if (power_info_data_v2.cpu_energy.task_platform_idle_wakeups != 0) {
496*19c3b8c2SApple OSS Distributions T_LOG("task_info call returned %llu for platform_idle_wakeup", power_info_data_v2.cpu_energy.task_platform_idle_wakeups);
497*19c3b8c2SApple OSS Distributions }
498*19c3b8c2SApple OSS Distributions #endif
499*19c3b8c2SApple OSS Distributions
500*19c3b8c2SApple OSS Distributions count = TASK_POWER_INFO_V2_COUNT_OLD;
501*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
502*19c3b8c2SApple OSS Distributions
503*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
504*19c3b8c2SApple OSS Distributions
505*19c3b8c2SApple OSS Distributions /*
506*19c3b8c2SApple OSS Distributions * This is a negative case.
507*19c3b8c2SApple OSS Distributions */
508*19c3b8c2SApple OSS Distributions count--;
509*19c3b8c2SApple OSS Distributions err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
510*19c3b8c2SApple OSS Distributions
511*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
512*19c3b8c2SApple OSS Distributions "Negative test case: task_info should verify that count is at least equal to what is defined in API. Call "
513*19c3b8c2SApple OSS Distributions "returns errno %d:%s",
514*19c3b8c2SApple OSS Distributions err, mach_error_string(err));
515*19c3b8c2SApple OSS Distributions }
516*19c3b8c2SApple OSS Distributions
517*19c3b8c2SApple OSS Distributions T_DECL(test_task_basic_info_32, "tests TASK_BASIC_INFO_32", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
518*19c3b8c2SApple OSS Distributions {
519*19c3b8c2SApple OSS Distributions test_task_basic_info(INFO_32);
520*19c3b8c2SApple OSS Distributions }
521*19c3b8c2SApple OSS Distributions
522*19c3b8c2SApple OSS Distributions T_DECL(test_task_basic_info_32_2, "tests TASK_BASIC_INFO_32_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
523*19c3b8c2SApple OSS Distributions {
524*19c3b8c2SApple OSS Distributions test_task_basic_info(INFO_32_2);
525*19c3b8c2SApple OSS Distributions }
526*19c3b8c2SApple OSS Distributions
527*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
528*19c3b8c2SApple OSS Distributions T_DECL(test_task_basic_info_64i_2, "tests TASK_BASIC_INFO_64_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
529*19c3b8c2SApple OSS Distributions {
530*19c3b8c2SApple OSS Distributions test_task_basic_info(INFO_64_2);
531*19c3b8c2SApple OSS Distributions }
532*19c3b8c2SApple OSS Distributions #else
533*19c3b8c2SApple OSS Distributions T_DECL(test_task_basic_info_64, "tests TASK_BASIC_INFO_64", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
534*19c3b8c2SApple OSS Distributions {
535*19c3b8c2SApple OSS Distributions test_task_basic_info(INFO_64);
536*19c3b8c2SApple OSS Distributions }
537*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
538*19c3b8c2SApple OSS Distributions
539*19c3b8c2SApple OSS Distributions T_DECL(test_mach_task_basic_info, "tests MACH_TASK_BASIC_INFO", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
540*19c3b8c2SApple OSS Distributions {
541*19c3b8c2SApple OSS Distributions test_task_basic_info(INFO_MACH);
542*19c3b8c2SApple OSS Distributions }
543*19c3b8c2SApple OSS Distributions
544*19c3b8c2SApple OSS Distributions void
test_task_basic_info(enum info_kind kind)545*19c3b8c2SApple OSS Distributions test_task_basic_info(enum info_kind kind)
546*19c3b8c2SApple OSS Distributions {
547*19c3b8c2SApple OSS Distributions #define BEFORE 0
548*19c3b8c2SApple OSS Distributions #define AFTER 1
549*19c3b8c2SApple OSS Distributions
550*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
551*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
552*19c3b8c2SApple OSS Distributions T_QUIET;
553*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
554*19c3b8c2SApple OSS Distributions T_SETUPEND;
555*19c3b8c2SApple OSS Distributions
556*19c3b8c2SApple OSS Distributions task_info_t info_data[2];
557*19c3b8c2SApple OSS Distributions task_basic_info_32_data_t basic_info_32_data[2];
558*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
559*19c3b8c2SApple OSS Distributions task_basic_info_64_2_data_t basic_info_64_2_data[2];
560*19c3b8c2SApple OSS Distributions #else
561*19c3b8c2SApple OSS Distributions task_basic_info_64_data_t basic_info_64_data[2];
562*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
563*19c3b8c2SApple OSS Distributions mach_task_basic_info_data_t mach_basic_info_data[2];
564*19c3b8c2SApple OSS Distributions
565*19c3b8c2SApple OSS Distributions kern_return_t kr;
566*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count;
567*19c3b8c2SApple OSS Distributions task_flavor_t flavor = 0;
568*19c3b8c2SApple OSS Distributions integer_t suspend_count;
569*19c3b8c2SApple OSS Distributions uint64_t resident_size_diff;
570*19c3b8c2SApple OSS Distributions uint64_t virtual_size_diff;
571*19c3b8c2SApple OSS Distributions
572*19c3b8c2SApple OSS Distributions void * tmp_map = NULL;
573*19c3b8c2SApple OSS Distributions pid_t child_pid;
574*19c3b8c2SApple OSS Distributions mach_port_name_t child_task;
575*19c3b8c2SApple OSS Distributions /*for dt_waitpid*/
576*19c3b8c2SApple OSS Distributions int timeout = 10; // change to max timeout
577*19c3b8c2SApple OSS Distributions int exit_status = 0;
578*19c3b8c2SApple OSS Distributions
579*19c3b8c2SApple OSS Distributions switch (kind) {
580*19c3b8c2SApple OSS Distributions case INFO_32:
581*19c3b8c2SApple OSS Distributions case INFO_32_2:
582*19c3b8c2SApple OSS Distributions info_data[BEFORE] = (task_info_t)&basic_info_32_data[BEFORE];
583*19c3b8c2SApple OSS Distributions info_data[AFTER] = (task_info_t)&basic_info_32_data[AFTER];
584*19c3b8c2SApple OSS Distributions count = TASK_BASIC_INFO_32_COUNT;
585*19c3b8c2SApple OSS Distributions flavor = TASK_BASIC_INFO_32;
586*19c3b8c2SApple OSS Distributions
587*19c3b8c2SApple OSS Distributions if (kind == INFO_32_2) {
588*19c3b8c2SApple OSS Distributions flavor = TASK_BASIC2_INFO_32;
589*19c3b8c2SApple OSS Distributions }
590*19c3b8c2SApple OSS Distributions
591*19c3b8c2SApple OSS Distributions break;
592*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
593*19c3b8c2SApple OSS Distributions case INFO_64:
594*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("invalid basic info kind");
595*19c3b8c2SApple OSS Distributions break;
596*19c3b8c2SApple OSS Distributions
597*19c3b8c2SApple OSS Distributions case INFO_64_2:
598*19c3b8c2SApple OSS Distributions info_data[BEFORE] = (task_info_t)&basic_info_64_2_data[BEFORE];
599*19c3b8c2SApple OSS Distributions info_data[AFTER] = (task_info_t)&basic_info_64_2_data[AFTER];
600*19c3b8c2SApple OSS Distributions count = TASK_BASIC_INFO_64_2_COUNT;
601*19c3b8c2SApple OSS Distributions flavor = TASK_BASIC_INFO_64_2;
602*19c3b8c2SApple OSS Distributions break;
603*19c3b8c2SApple OSS Distributions
604*19c3b8c2SApple OSS Distributions #else
605*19c3b8c2SApple OSS Distributions case INFO_64:
606*19c3b8c2SApple OSS Distributions info_data[BEFORE] = (task_info_t)&basic_info_64_data[BEFORE];
607*19c3b8c2SApple OSS Distributions info_data[AFTER] = (task_info_t)&basic_info_64_data[AFTER];
608*19c3b8c2SApple OSS Distributions count = TASK_BASIC_INFO_64_COUNT;
609*19c3b8c2SApple OSS Distributions flavor = TASK_BASIC_INFO_64;
610*19c3b8c2SApple OSS Distributions break;
611*19c3b8c2SApple OSS Distributions
612*19c3b8c2SApple OSS Distributions case INFO_64_2:
613*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("invalid basic info kind");
614*19c3b8c2SApple OSS Distributions break;
615*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
616*19c3b8c2SApple OSS Distributions case INFO_MACH:
617*19c3b8c2SApple OSS Distributions info_data[BEFORE] = (task_info_t)&mach_basic_info_data[BEFORE];
618*19c3b8c2SApple OSS Distributions info_data[AFTER] = (task_info_t)&mach_basic_info_data[AFTER];
619*19c3b8c2SApple OSS Distributions count = MACH_TASK_BASIC_INFO_COUNT;
620*19c3b8c2SApple OSS Distributions flavor = MACH_TASK_BASIC_INFO;
621*19c3b8c2SApple OSS Distributions break;
622*19c3b8c2SApple OSS Distributions case INFO_MAX:
623*19c3b8c2SApple OSS Distributions default:
624*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("invalid basic info kind");
625*19c3b8c2SApple OSS Distributions break;
626*19c3b8c2SApple OSS Distributions }
627*19c3b8c2SApple OSS Distributions
628*19c3b8c2SApple OSS Distributions kr = task_info(mach_task_self(), flavor, info_data[BEFORE], &count);
629*19c3b8c2SApple OSS Distributions
630*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info succeeded");
631*19c3b8c2SApple OSS Distributions
632*19c3b8c2SApple OSS Distributions do_factorial_task();
633*19c3b8c2SApple OSS Distributions
634*19c3b8c2SApple OSS Distributions /*
635*19c3b8c2SApple OSS Distributions * Allocate virtual and resident memory.
636*19c3b8c2SApple OSS Distributions */
637*19c3b8c2SApple OSS Distributions tmp_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
638*19c3b8c2SApple OSS Distributions
639*19c3b8c2SApple OSS Distributions T_WITH_ERRNO;
640*19c3b8c2SApple OSS Distributions T_EXPECT_NE(tmp_map, MAP_FAILED, "verify mmap call is successful");
641*19c3b8c2SApple OSS Distributions
642*19c3b8c2SApple OSS Distributions memset(tmp_map, 'm', PAGE_SIZE);
643*19c3b8c2SApple OSS Distributions
644*19c3b8c2SApple OSS Distributions child_pid = fork();
645*19c3b8c2SApple OSS Distributions
646*19c3b8c2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked");
647*19c3b8c2SApple OSS Distributions
648*19c3b8c2SApple OSS Distributions if (child_pid == 0) {
649*19c3b8c2SApple OSS Distributions /*
650*19c3b8c2SApple OSS Distributions * This will suspend the child process.
651*19c3b8c2SApple OSS Distributions */
652*19c3b8c2SApple OSS Distributions kr = task_suspend(mach_task_self());
653*19c3b8c2SApple OSS Distributions exit(kr);
654*19c3b8c2SApple OSS Distributions }
655*19c3b8c2SApple OSS Distributions
656*19c3b8c2SApple OSS Distributions /*
657*19c3b8c2SApple OSS Distributions * Wait for the child process to suspend itself.
658*19c3b8c2SApple OSS Distributions */
659*19c3b8c2SApple OSS Distributions sleep(1);
660*19c3b8c2SApple OSS Distributions
661*19c3b8c2SApple OSS Distributions kr = task_for_pid(mach_task_self(), child_pid, &child_task);
662*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed");
663*19c3b8c2SApple OSS Distributions
664*19c3b8c2SApple OSS Distributions /*
665*19c3b8c2SApple OSS Distributions * Verify the suspend_count for child and resume it.
666*19c3b8c2SApple OSS Distributions */
667*19c3b8c2SApple OSS Distributions
668*19c3b8c2SApple OSS Distributions kr = task_info(child_task, flavor, info_data[AFTER], &count);
669*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
670*19c3b8c2SApple OSS Distributions
671*19c3b8c2SApple OSS Distributions suspend_count = (integer_t)(info_get(kind, GET_SUSPEND_COUNT, info_data[AFTER]));
672*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count");
673*19c3b8c2SApple OSS Distributions
674*19c3b8c2SApple OSS Distributions kr = task_resume(child_task);
675*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_resume succeeded");
676*19c3b8c2SApple OSS Distributions
677*19c3b8c2SApple OSS Distributions /*
678*19c3b8c2SApple OSS Distributions * reap kr from task_suspend call in child
679*19c3b8c2SApple OSS Distributions */
680*19c3b8c2SApple OSS Distributions if (dt_waitpid(child_pid, &exit_status, NULL, timeout)) {
681*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(exit_status, "verify child task_suspend is successful");
682*19c3b8c2SApple OSS Distributions } else {
683*19c3b8c2SApple OSS Distributions T_FAIL("dt_waitpid failed");
684*19c3b8c2SApple OSS Distributions }
685*19c3b8c2SApple OSS Distributions
686*19c3b8c2SApple OSS Distributions kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
687*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
688*19c3b8c2SApple OSS Distributions
689*19c3b8c2SApple OSS Distributions resident_size_diff = info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]) - info_get(kind, GET_RESIDENT_SIZE, info_data[BEFORE]);
690*19c3b8c2SApple OSS Distributions virtual_size_diff = info_get(kind, GET_VIRTUAL_SIZE, info_data[AFTER]) - info_get(kind, GET_VIRTUAL_SIZE, info_data[BEFORE]);
691*19c3b8c2SApple OSS Distributions
692*19c3b8c2SApple OSS Distributions /*
693*19c3b8c2SApple OSS Distributions * INFO_32_2 gets the max resident size instead of the current resident size
694*19c3b8c2SApple OSS Distributions * 32 KB tolerance built into test. The returned value is generally between 0 and 16384
695*19c3b8c2SApple OSS Distributions *
696*19c3b8c2SApple OSS Distributions * max resident size is a discrete field in INFO_MACH, so it's handled differently
697*19c3b8c2SApple OSS Distributions */
698*19c3b8c2SApple OSS Distributions if (kind == INFO_32_2) {
699*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
700*19c3b8c2SApple OSS Distributions T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
701*19c3b8c2SApple OSS Distributions T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "verify task_info returns valid virtual_size");
702*19c3b8c2SApple OSS Distributions } else {
703*19c3b8c2SApple OSS Distributions T_EXPECT_GE(resident_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid resident_size");
704*19c3b8c2SApple OSS Distributions T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid virtual_size");
705*19c3b8c2SApple OSS Distributions }
706*19c3b8c2SApple OSS Distributions
707*19c3b8c2SApple OSS Distributions if (kind == INFO_MACH) {
708*19c3b8c2SApple OSS Distributions resident_size_diff = info_get(kind, GET_MAX_RES, info_data[AFTER]) - info_get(kind, GET_MAX_RES, info_data[BEFORE]);
709*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
710*19c3b8c2SApple OSS Distributions T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
711*19c3b8c2SApple OSS Distributions T_EXPECT_GE(info_get(kind, GET_MAX_RES, info_data[AFTER]), info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]),
712*19c3b8c2SApple OSS Distributions "verify max resident size is greater than or equal to curr resident size");
713*19c3b8c2SApple OSS Distributions }
714*19c3b8c2SApple OSS Distributions
715*19c3b8c2SApple OSS Distributions do_factorial_task();
716*19c3b8c2SApple OSS Distributions
717*19c3b8c2SApple OSS Distributions /*
718*19c3b8c2SApple OSS Distributions * These counters give time for threads that have terminated. We dont have any, so checking for zero.
719*19c3b8c2SApple OSS Distributions */
720*19c3b8c2SApple OSS Distributions
721*19c3b8c2SApple OSS Distributions time_value_t * user_tv = (time_value_t *)(info_get(kind, GET_USER_TIME, info_data[BEFORE]));
722*19c3b8c2SApple OSS Distributions T_EXPECT_EQ((user_tv->seconds + user_tv->microseconds / 1000000), 0, "verify task_info shows valid user time");
723*19c3b8c2SApple OSS Distributions
724*19c3b8c2SApple OSS Distributions time_value_t * sys_tv = (time_value_t *)(info_get(kind, GET_SYS_TIME, info_data[BEFORE]));
725*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(sys_tv->seconds + (sys_tv->microseconds / 1000000), 0, "verify task_info shows valid system time");
726*19c3b8c2SApple OSS Distributions
727*19c3b8c2SApple OSS Distributions /*
728*19c3b8c2SApple OSS Distributions * The default value for non-kernel tasks is TIMESHARE.
729*19c3b8c2SApple OSS Distributions */
730*19c3b8c2SApple OSS Distributions
731*19c3b8c2SApple OSS Distributions policy_t pt = (policy_t)info_get(kind, GET_POLICY, info_data[BEFORE]);
732*19c3b8c2SApple OSS Distributions
733*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(pt, POLICY_TIMESHARE, "verify task_info shows valid policy");
734*19c3b8c2SApple OSS Distributions
735*19c3b8c2SApple OSS Distributions /*
736*19c3b8c2SApple OSS Distributions * This is a negative case.
737*19c3b8c2SApple OSS Distributions */
738*19c3b8c2SApple OSS Distributions
739*19c3b8c2SApple OSS Distributions count--;
740*19c3b8c2SApple OSS Distributions kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
741*19c3b8c2SApple OSS Distributions
742*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT,
743*19c3b8c2SApple OSS Distributions "Negative test case: task_info should verify that count is at least equal to what is defined in API");
744*19c3b8c2SApple OSS Distributions
745*19c3b8c2SApple OSS Distributions /*
746*19c3b8c2SApple OSS Distributions * deallocate memory
747*19c3b8c2SApple OSS Distributions */
748*19c3b8c2SApple OSS Distributions munmap(tmp_map, PAGE_SIZE);
749*19c3b8c2SApple OSS Distributions
750*19c3b8c2SApple OSS Distributions return;
751*19c3b8c2SApple OSS Distributions
752*19c3b8c2SApple OSS Distributions #undef BEFORE
753*19c3b8c2SApple OSS Distributions #undef AFTER
754*19c3b8c2SApple OSS Distributions }
755*19c3b8c2SApple OSS Distributions
756*19c3b8c2SApple OSS Distributions T_DECL(test_sigcont_task_suspend_resume,
757*19c3b8c2SApple OSS Distributions "test to verify that SIGCONT on task_suspend()-ed process works",
758*19c3b8c2SApple OSS Distributions T_META_ASROOT(true),
759*19c3b8c2SApple OSS Distributions T_META_LTEPHASE(LTE_POSTINIT))
760*19c3b8c2SApple OSS Distributions {
761*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
762*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
763*19c3b8c2SApple OSS Distributions T_QUIET;
764*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
765*19c3b8c2SApple OSS Distributions T_SETUPEND;
766*19c3b8c2SApple OSS Distributions
767*19c3b8c2SApple OSS Distributions mach_task_basic_info_data_t mach_basic_info_data;
768*19c3b8c2SApple OSS Distributions task_info_t info_data = (task_info_t)&mach_basic_info_data;
769*19c3b8c2SApple OSS Distributions
770*19c3b8c2SApple OSS Distributions task_debug_info_internal_data_t debug_info;
771*19c3b8c2SApple OSS Distributions mach_msg_type_number_t debug_count = TASK_DEBUG_INFO_INTERNAL_COUNT;
772*19c3b8c2SApple OSS Distributions
773*19c3b8c2SApple OSS Distributions kern_return_t kr;
774*19c3b8c2SApple OSS Distributions int posix_ret;
775*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
776*19c3b8c2SApple OSS Distributions task_flavor_t flavor = MACH_TASK_BASIC_INFO;
777*19c3b8c2SApple OSS Distributions integer_t suspend_count;
778*19c3b8c2SApple OSS Distributions integer_t debug_suspend_count;
779*19c3b8c2SApple OSS Distributions pid_t child_pid = 0;
780*19c3b8c2SApple OSS Distributions mach_port_name_t child_task;
781*19c3b8c2SApple OSS Distributions /*for dt_waitpid*/
782*19c3b8c2SApple OSS Distributions int timeout = 5;
783*19c3b8c2SApple OSS Distributions int exit_status = 0;
784*19c3b8c2SApple OSS Distributions int signal_no = 0;
785*19c3b8c2SApple OSS Distributions
786*19c3b8c2SApple OSS Distributions child_pid = fork();
787*19c3b8c2SApple OSS Distributions
788*19c3b8c2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked");
789*19c3b8c2SApple OSS Distributions
790*19c3b8c2SApple OSS Distributions if (child_pid == 0) {
791*19c3b8c2SApple OSS Distributions /*
792*19c3b8c2SApple OSS Distributions * This will suspend the child process.
793*19c3b8c2SApple OSS Distributions */
794*19c3b8c2SApple OSS Distributions kr = task_suspend(mach_task_self());
795*19c3b8c2SApple OSS Distributions
796*19c3b8c2SApple OSS Distributions /*
797*19c3b8c2SApple OSS Distributions * When child resumes, it exits immediately
798*19c3b8c2SApple OSS Distributions */
799*19c3b8c2SApple OSS Distributions
800*19c3b8c2SApple OSS Distributions exit(kr);
801*19c3b8c2SApple OSS Distributions }
802*19c3b8c2SApple OSS Distributions
803*19c3b8c2SApple OSS Distributions /*
804*19c3b8c2SApple OSS Distributions * Wait for the child process to suspend itself.
805*19c3b8c2SApple OSS Distributions */
806*19c3b8c2SApple OSS Distributions sleep(1);
807*19c3b8c2SApple OSS Distributions
808*19c3b8c2SApple OSS Distributions kr = task_for_pid(mach_task_self(), child_pid, &child_task);
809*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed");
810*19c3b8c2SApple OSS Distributions
811*19c3b8c2SApple OSS Distributions /*
812*19c3b8c2SApple OSS Distributions * Verify the suspend_count for child and resume it.
813*19c3b8c2SApple OSS Distributions */
814*19c3b8c2SApple OSS Distributions
815*19c3b8c2SApple OSS Distributions kr = task_info(child_task, flavor, info_data, &count);
816*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
817*19c3b8c2SApple OSS Distributions
818*19c3b8c2SApple OSS Distributions suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data));
819*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count (1) (actually user stop count) ");
820*19c3b8c2SApple OSS Distributions
821*19c3b8c2SApple OSS Distributions kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
822*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
823*19c3b8c2SApple OSS Distributions
824*19c3b8c2SApple OSS Distributions debug_suspend_count = debug_info.suspend_count;
825*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(debug_info.suspend_count, 1, "verify debug_info shows correct suspend_count(1)");
826*19c3b8c2SApple OSS Distributions
827*19c3b8c2SApple OSS Distributions posix_ret = kill(child_pid, SIGCONT);
828*19c3b8c2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_ret, "verify signal call succeeded");
829*19c3b8c2SApple OSS Distributions
830*19c3b8c2SApple OSS Distributions /*
831*19c3b8c2SApple OSS Distributions * reap kr from task_suspend call in child
832*19c3b8c2SApple OSS Distributions */
833*19c3b8c2SApple OSS Distributions dt_waitpid(child_pid, &exit_status, &signal_no, timeout);
834*19c3b8c2SApple OSS Distributions
835*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(signal_no, 0, "child should be resumed and exit without signal");
836*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(exit_status, 0, "child should exit with 0");
837*19c3b8c2SApple OSS Distributions }
838*19c3b8c2SApple OSS Distributions
839*19c3b8c2SApple OSS Distributions T_DECL(test_sigcont_task_suspend2_resume,
840*19c3b8c2SApple OSS Distributions "test to verify that SIGCONT on task_suspend2()-ed process doesn't work",
841*19c3b8c2SApple OSS Distributions T_META_ASROOT(true),
842*19c3b8c2SApple OSS Distributions T_META_LTEPHASE(LTE_POSTINIT))
843*19c3b8c2SApple OSS Distributions {
844*19c3b8c2SApple OSS Distributions T_SETUPBEGIN;
845*19c3b8c2SApple OSS Distributions int is_dev = is_development_kernel();
846*19c3b8c2SApple OSS Distributions T_QUIET;
847*19c3b8c2SApple OSS Distributions T_ASSERT_TRUE(is_dev, "verify development kernel is running");
848*19c3b8c2SApple OSS Distributions T_SETUPEND;
849*19c3b8c2SApple OSS Distributions
850*19c3b8c2SApple OSS Distributions mach_task_basic_info_data_t mach_basic_info_data;
851*19c3b8c2SApple OSS Distributions task_info_t info_data = (task_info_t)&mach_basic_info_data;
852*19c3b8c2SApple OSS Distributions
853*19c3b8c2SApple OSS Distributions task_debug_info_internal_data_t debug_info;
854*19c3b8c2SApple OSS Distributions mach_msg_type_number_t debug_count = TASK_DEBUG_INFO_INTERNAL_COUNT;
855*19c3b8c2SApple OSS Distributions
856*19c3b8c2SApple OSS Distributions kern_return_t kr;
857*19c3b8c2SApple OSS Distributions int posix_ret;
858*19c3b8c2SApple OSS Distributions mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
859*19c3b8c2SApple OSS Distributions task_flavor_t flavor = MACH_TASK_BASIC_INFO;
860*19c3b8c2SApple OSS Distributions integer_t suspend_count = 0;
861*19c3b8c2SApple OSS Distributions integer_t debug_suspend_count = 0;
862*19c3b8c2SApple OSS Distributions pid_t child_pid = 0;
863*19c3b8c2SApple OSS Distributions mach_port_name_t child_task;
864*19c3b8c2SApple OSS Distributions task_suspension_token_t child_token = 0xFFFFF;
865*19c3b8c2SApple OSS Distributions
866*19c3b8c2SApple OSS Distributions /*
867*19c3b8c2SApple OSS Distributions * for dt_waitpid
868*19c3b8c2SApple OSS Distributions * We expect the test to fail right now, so I've set timeout to
869*19c3b8c2SApple OSS Distributions * be shorter than we may want it to be when the issue is fixed
870*19c3b8c2SApple OSS Distributions */
871*19c3b8c2SApple OSS Distributions int timeout = 1;
872*19c3b8c2SApple OSS Distributions int exit_status = 0;
873*19c3b8c2SApple OSS Distributions int signal_no = 0;
874*19c3b8c2SApple OSS Distributions
875*19c3b8c2SApple OSS Distributions /* for pipe */
876*19c3b8c2SApple OSS Distributions int fd[2];
877*19c3b8c2SApple OSS Distributions pipe(fd);
878*19c3b8c2SApple OSS Distributions int pipe_msg = 0;
879*19c3b8c2SApple OSS Distributions
880*19c3b8c2SApple OSS Distributions child_pid = fork();
881*19c3b8c2SApple OSS Distributions
882*19c3b8c2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked %d", child_pid);
883*19c3b8c2SApple OSS Distributions
884*19c3b8c2SApple OSS Distributions if (child_pid == 0) {
885*19c3b8c2SApple OSS Distributions close(fd[1]);
886*19c3b8c2SApple OSS Distributions T_LOG("Waiting to read from parent...");
887*19c3b8c2SApple OSS Distributions read(fd[0], &pipe_msg, sizeof(pipe_msg));
888*19c3b8c2SApple OSS Distributions T_LOG("Done reading from parent, about to exit...");
889*19c3b8c2SApple OSS Distributions exit(0);
890*19c3b8c2SApple OSS Distributions }
891*19c3b8c2SApple OSS Distributions /*
892*19c3b8c2SApple OSS Distributions * Wait for child to fork and block on read
893*19c3b8c2SApple OSS Distributions */
894*19c3b8c2SApple OSS Distributions sleep(1);
895*19c3b8c2SApple OSS Distributions
896*19c3b8c2SApple OSS Distributions close(fd[0]);
897*19c3b8c2SApple OSS Distributions
898*19c3b8c2SApple OSS Distributions kr = task_for_pid(mach_task_self(), child_pid, &child_task);
899*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed");
900*19c3b8c2SApple OSS Distributions
901*19c3b8c2SApple OSS Distributions kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
902*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
903*19c3b8c2SApple OSS Distributions
904*19c3b8c2SApple OSS Distributions debug_suspend_count = debug_info.suspend_count;
905*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(debug_suspend_count, 0, "verify debug_info shows correct (true) suspend_count(0)");
906*19c3b8c2SApple OSS Distributions
907*19c3b8c2SApple OSS Distributions kr = task_suspend2(child_task, &child_token);
908*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_suspend2 call succeeded");
909*19c3b8c2SApple OSS Distributions
910*19c3b8c2SApple OSS Distributions kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
911*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
912*19c3b8c2SApple OSS Distributions
913*19c3b8c2SApple OSS Distributions debug_suspend_count = debug_info.suspend_count;
914*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(debug_suspend_count, 1, "verify debug_info shows correct (true) suspend_count(1)");
915*19c3b8c2SApple OSS Distributions
916*19c3b8c2SApple OSS Distributions /*
917*19c3b8c2SApple OSS Distributions * Verify the suspend_count for child and resume it.
918*19c3b8c2SApple OSS Distributions */
919*19c3b8c2SApple OSS Distributions
920*19c3b8c2SApple OSS Distributions kr = task_info(child_task, flavor, info_data, &count);
921*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
922*19c3b8c2SApple OSS Distributions
923*19c3b8c2SApple OSS Distributions suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data));
924*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(suspend_count, 1, "verify task_info shows correct (user_stop_count) suspend_count (1)");
925*19c3b8c2SApple OSS Distributions
926*19c3b8c2SApple OSS Distributions posix_ret = kill(child_pid, SIGCONT);
927*19c3b8c2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_ret, "verify signal call succeeded");
928*19c3b8c2SApple OSS Distributions
929*19c3b8c2SApple OSS Distributions kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
930*19c3b8c2SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "verify task_info call succeeded");
931*19c3b8c2SApple OSS Distributions
932*19c3b8c2SApple OSS Distributions debug_suspend_count = debug_info.suspend_count;
933*19c3b8c2SApple OSS Distributions T_EXPECTFAIL_WITH_RADAR(33166654);
934*19c3b8c2SApple OSS Distributions T_EXPECT_EQ(debug_suspend_count, 1, "verify debug_info shows correct (true) suspend_count (1)");
935*19c3b8c2SApple OSS Distributions
936*19c3b8c2SApple OSS Distributions suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data));
937*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct (user_stop_count) suspend_count (1) after SIG_CONT");
938*19c3b8c2SApple OSS Distributions
939*19c3b8c2SApple OSS Distributions kr = task_resume(child_task);
940*19c3b8c2SApple OSS Distributions T_EXPECTFAIL_WITH_RADAR(33166654);
941*19c3b8c2SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "verify task_resume succeeded");
942*19c3b8c2SApple OSS Distributions
943*19c3b8c2SApple OSS Distributions /*
944*19c3b8c2SApple OSS Distributions * reap kr from task_suspend call in child
945*19c3b8c2SApple OSS Distributions */
946*19c3b8c2SApple OSS Distributions
947*19c3b8c2SApple OSS Distributions dt_waitpid(child_pid, &exit_status, &signal_no, timeout);
948*19c3b8c2SApple OSS Distributions
949*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(signal_no, SIG_DT_TIMEOUT, "dt_waitpid timed out as expected");
950*19c3b8c2SApple OSS Distributions
951*19c3b8c2SApple OSS Distributions // Resume properly using token and then wait
952*19c3b8c2SApple OSS Distributions
953*19c3b8c2SApple OSS Distributions kr = task_resume2(child_token);
954*19c3b8c2SApple OSS Distributions T_EXPECTFAIL_WITH_RADAR(33166654);
955*19c3b8c2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "verify task_resume2 succeeded");
956*19c3b8c2SApple OSS Distributions
957*19c3b8c2SApple OSS Distributions write(fd[1], &pipe_msg, sizeof(pipe_msg));
958*19c3b8c2SApple OSS Distributions
959*19c3b8c2SApple OSS Distributions /*
960*19c3b8c2SApple OSS Distributions * reap kr from task_suspend call in child
961*19c3b8c2SApple OSS Distributions */
962*19c3b8c2SApple OSS Distributions dt_waitpid(child_pid, &exit_status, &signal_no, timeout);
963*19c3b8c2SApple OSS Distributions
964*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(signal_no, 0, "child should be resumed and no signal should be returned");
965*19c3b8c2SApple OSS Distributions T_ASSERT_EQ(exit_status, 0, "child should exit with 0");
966*19c3b8c2SApple OSS Distributions }
967*19c3b8c2SApple OSS Distributions
968*19c3b8c2SApple OSS Distributions uint64_t
info_get(enum info_kind kind,enum info_get get,void * data)969*19c3b8c2SApple OSS Distributions info_get(enum info_kind kind, enum info_get get, void * data)
970*19c3b8c2SApple OSS Distributions {
971*19c3b8c2SApple OSS Distributions switch (get) {
972*19c3b8c2SApple OSS Distributions case GET_SUSPEND_COUNT:
973*19c3b8c2SApple OSS Distributions switch (kind) {
974*19c3b8c2SApple OSS Distributions case INFO_32:
975*19c3b8c2SApple OSS Distributions case INFO_32_2:
976*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_32_t)data)->suspend_count);
977*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
978*19c3b8c2SApple OSS Distributions case INFO_64:
979*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
980*19c3b8c2SApple OSS Distributions break;
981*19c3b8c2SApple OSS Distributions
982*19c3b8c2SApple OSS Distributions case INFO_64_2:
983*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_2_t)data)->suspend_count);
984*19c3b8c2SApple OSS Distributions #else
985*19c3b8c2SApple OSS Distributions case INFO_64:
986*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_t)data)->suspend_count);
987*19c3b8c2SApple OSS Distributions
988*19c3b8c2SApple OSS Distributions case INFO_64_2:
989*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
990*19c3b8c2SApple OSS Distributions break;
991*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
992*19c3b8c2SApple OSS Distributions case INFO_MACH:
993*19c3b8c2SApple OSS Distributions return (uint64_t)(((mach_task_basic_info_t)data)->suspend_count);
994*19c3b8c2SApple OSS Distributions case INFO_MAX:
995*19c3b8c2SApple OSS Distributions default:
996*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
997*19c3b8c2SApple OSS Distributions }
998*19c3b8c2SApple OSS Distributions case GET_RESIDENT_SIZE:
999*19c3b8c2SApple OSS Distributions switch (kind) {
1000*19c3b8c2SApple OSS Distributions case INFO_32:
1001*19c3b8c2SApple OSS Distributions case INFO_32_2:
1002*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_32_t)data)->resident_size);
1003*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
1004*19c3b8c2SApple OSS Distributions case INFO_64:
1005*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1006*19c3b8c2SApple OSS Distributions break;
1007*19c3b8c2SApple OSS Distributions
1008*19c3b8c2SApple OSS Distributions case INFO_64_2:
1009*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_2_t)data)->resident_size);
1010*19c3b8c2SApple OSS Distributions #else
1011*19c3b8c2SApple OSS Distributions case INFO_64:
1012*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_t)data)->resident_size);
1013*19c3b8c2SApple OSS Distributions
1014*19c3b8c2SApple OSS Distributions case INFO_64_2:
1015*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1016*19c3b8c2SApple OSS Distributions break;
1017*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
1018*19c3b8c2SApple OSS Distributions case INFO_MACH:
1019*19c3b8c2SApple OSS Distributions return (uint64_t)(((mach_task_basic_info_t)data)->resident_size);
1020*19c3b8c2SApple OSS Distributions case INFO_MAX:
1021*19c3b8c2SApple OSS Distributions default:
1022*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1023*19c3b8c2SApple OSS Distributions }
1024*19c3b8c2SApple OSS Distributions case GET_VIRTUAL_SIZE:
1025*19c3b8c2SApple OSS Distributions switch (kind) {
1026*19c3b8c2SApple OSS Distributions case INFO_32:
1027*19c3b8c2SApple OSS Distributions case INFO_32_2:
1028*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_32_t)data)->virtual_size);
1029*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
1030*19c3b8c2SApple OSS Distributions case INFO_64:
1031*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1032*19c3b8c2SApple OSS Distributions break;
1033*19c3b8c2SApple OSS Distributions
1034*19c3b8c2SApple OSS Distributions case INFO_64_2:
1035*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_2_t)data)->virtual_size);
1036*19c3b8c2SApple OSS Distributions #else
1037*19c3b8c2SApple OSS Distributions case INFO_64:
1038*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_t)data)->virtual_size);
1039*19c3b8c2SApple OSS Distributions
1040*19c3b8c2SApple OSS Distributions case INFO_64_2:
1041*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1042*19c3b8c2SApple OSS Distributions break;
1043*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
1044*19c3b8c2SApple OSS Distributions case INFO_MACH:
1045*19c3b8c2SApple OSS Distributions return (uint64_t)(((mach_task_basic_info_t)data)->virtual_size);
1046*19c3b8c2SApple OSS Distributions
1047*19c3b8c2SApple OSS Distributions case INFO_MAX:
1048*19c3b8c2SApple OSS Distributions default:
1049*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1050*19c3b8c2SApple OSS Distributions }
1051*19c3b8c2SApple OSS Distributions case GET_USER_TIME:
1052*19c3b8c2SApple OSS Distributions switch (kind) {
1053*19c3b8c2SApple OSS Distributions case INFO_32:
1054*19c3b8c2SApple OSS Distributions case INFO_32_2:
1055*19c3b8c2SApple OSS Distributions return (uint64_t) &(((task_basic_info_32_t)data)->user_time);
1056*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
1057*19c3b8c2SApple OSS Distributions case INFO_64:
1058*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1059*19c3b8c2SApple OSS Distributions break;
1060*19c3b8c2SApple OSS Distributions
1061*19c3b8c2SApple OSS Distributions case INFO_64_2:
1062*19c3b8c2SApple OSS Distributions return (uint64_t) &(((task_basic_info_64_2_t)data)->user_time);
1063*19c3b8c2SApple OSS Distributions #else
1064*19c3b8c2SApple OSS Distributions case INFO_64:
1065*19c3b8c2SApple OSS Distributions return (uint64_t) &(((task_basic_info_64_t)data)->user_time);
1066*19c3b8c2SApple OSS Distributions
1067*19c3b8c2SApple OSS Distributions case INFO_64_2:
1068*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1069*19c3b8c2SApple OSS Distributions break;
1070*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
1071*19c3b8c2SApple OSS Distributions case INFO_MACH:
1072*19c3b8c2SApple OSS Distributions return (uint64_t) &(((mach_task_basic_info_t)data)->user_time);
1073*19c3b8c2SApple OSS Distributions
1074*19c3b8c2SApple OSS Distributions case INFO_MAX:
1075*19c3b8c2SApple OSS Distributions default:
1076*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1077*19c3b8c2SApple OSS Distributions }
1078*19c3b8c2SApple OSS Distributions case GET_SYS_TIME:
1079*19c3b8c2SApple OSS Distributions switch (kind) {
1080*19c3b8c2SApple OSS Distributions case INFO_32:
1081*19c3b8c2SApple OSS Distributions case INFO_32_2:
1082*19c3b8c2SApple OSS Distributions return (uint64_t) &(((task_basic_info_32_t)data)->system_time);
1083*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
1084*19c3b8c2SApple OSS Distributions case INFO_64:
1085*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1086*19c3b8c2SApple OSS Distributions break;
1087*19c3b8c2SApple OSS Distributions
1088*19c3b8c2SApple OSS Distributions case INFO_64_2:
1089*19c3b8c2SApple OSS Distributions return (uint64_t) &(((task_basic_info_64_2_t)data)->system_time);
1090*19c3b8c2SApple OSS Distributions #else
1091*19c3b8c2SApple OSS Distributions case INFO_64:
1092*19c3b8c2SApple OSS Distributions return (uint64_t) &(((task_basic_info_64_t)data)->system_time);
1093*19c3b8c2SApple OSS Distributions
1094*19c3b8c2SApple OSS Distributions case INFO_64_2:
1095*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1096*19c3b8c2SApple OSS Distributions break;
1097*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
1098*19c3b8c2SApple OSS Distributions case INFO_MACH:
1099*19c3b8c2SApple OSS Distributions return (uint64_t) &(((mach_task_basic_info_t)data)->user_time);
1100*19c3b8c2SApple OSS Distributions case INFO_MAX:
1101*19c3b8c2SApple OSS Distributions default:
1102*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1103*19c3b8c2SApple OSS Distributions }
1104*19c3b8c2SApple OSS Distributions case GET_POLICY:
1105*19c3b8c2SApple OSS Distributions switch (kind) {
1106*19c3b8c2SApple OSS Distributions case INFO_32:
1107*19c3b8c2SApple OSS Distributions case INFO_32_2:
1108*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_32_t)data)->policy);
1109*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
1110*19c3b8c2SApple OSS Distributions case INFO_64:
1111*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1112*19c3b8c2SApple OSS Distributions break;
1113*19c3b8c2SApple OSS Distributions
1114*19c3b8c2SApple OSS Distributions case INFO_64_2:
1115*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_2_t)data)->policy);
1116*19c3b8c2SApple OSS Distributions #else
1117*19c3b8c2SApple OSS Distributions case INFO_64:
1118*19c3b8c2SApple OSS Distributions return (uint64_t)(((task_basic_info_64_t)data)->policy);
1119*19c3b8c2SApple OSS Distributions
1120*19c3b8c2SApple OSS Distributions case INFO_64_2:
1121*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1122*19c3b8c2SApple OSS Distributions break;
1123*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
1124*19c3b8c2SApple OSS Distributions case INFO_MACH:
1125*19c3b8c2SApple OSS Distributions return (uint64_t)(((mach_task_basic_info_t)data)->policy);
1126*19c3b8c2SApple OSS Distributions
1127*19c3b8c2SApple OSS Distributions case INFO_MAX:
1128*19c3b8c2SApple OSS Distributions default:
1129*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1130*19c3b8c2SApple OSS Distributions }
1131*19c3b8c2SApple OSS Distributions case GET_MAX_RES:
1132*19c3b8c2SApple OSS Distributions switch (kind) {
1133*19c3b8c2SApple OSS Distributions case INFO_32:
1134*19c3b8c2SApple OSS Distributions case INFO_32_2:
1135*19c3b8c2SApple OSS Distributions case INFO_64:
1136*19c3b8c2SApple OSS Distributions case INFO_64_2:
1137*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1138*19c3b8c2SApple OSS Distributions case INFO_MACH:
1139*19c3b8c2SApple OSS Distributions return (uint64_t)(((mach_task_basic_info_t)data)->resident_size_max);
1140*19c3b8c2SApple OSS Distributions case INFO_MAX:
1141*19c3b8c2SApple OSS Distributions default:
1142*19c3b8c2SApple OSS Distributions T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1143*19c3b8c2SApple OSS Distributions }
1144*19c3b8c2SApple OSS Distributions }
1145*19c3b8c2SApple OSS Distributions
1146*19c3b8c2SApple OSS Distributions __builtin_unreachable();
1147*19c3b8c2SApple OSS Distributions }
1148