]> git.saurik.com Git - apple/xnu.git/blob - tests/stackshot.m
xnu-4903.241.1.tar.gz
[apple/xnu.git] / tests / stackshot.m
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <kern/debug.h>
4 #include <kern/kern_cdata.h>
5 #include <kdd.h>
6 #include <libproc.h>
7 #include <mach-o/dyld.h>
8 #include <mach-o/dyld_priv.h>
9 #include <sys/syscall.h>
10 #include <sys/stackshot.h>
11
12 /*
13 * mirrors the dyld_cache_header struct defined in dyld_cache_format.h from dyld source code
14 * TODO: remove once rdar://42361850 is in the build
15 */
16 struct dyld_cache_header
17 {
18 char magic[16]; // e.g. "dyld_v0 i386"
19 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
20 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
21 uint32_t imagesOffset; // file offset to first dyld_cache_image_info
22 uint32_t imagesCount; // number of dyld_cache_image_info entries
23 uint64_t dyldBaseAddress; // base address of dyld when cache was built
24 uint64_t codeSignatureOffset; // file offset of code signature blob
25 uint64_t codeSignatureSize; // size of code signature blob (zero means to end of file)
26 uint64_t slideInfoOffset; // file offset of kernel slid info
27 uint64_t slideInfoSize; // size of kernel slid info
28 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
29 uint64_t localSymbolsSize; // size of local symbols information
30 uint8_t uuid[16]; // unique value for each shared cache file
31 uint64_t cacheType; // 0 for development, 1 for production
32 uint32_t branchPoolsOffset; // file offset to table of uint64_t pool addresses
33 uint32_t branchPoolsCount; // number of uint64_t entries
34 uint64_t accelerateInfoAddr; // (unslid) address of optimization info
35 uint64_t accelerateInfoSize; // size of optimization info
36 uint64_t imagesTextOffset; // file offset to first dyld_cache_image_text_info
37 uint64_t imagesTextCount; // number of dyld_cache_image_text_info entries
38 uint64_t dylibsImageGroupAddr; // (unslid) address of ImageGroup for dylibs in this cache
39 uint64_t dylibsImageGroupSize; // size of ImageGroup for dylibs in this cache
40 uint64_t otherImageGroupAddr; // (unslid) address of ImageGroup for other OS dylibs
41 uint64_t otherImageGroupSize; // size of oImageGroup for other OS dylibs
42 uint64_t progClosuresAddr; // (unslid) address of list of program launch closures
43 uint64_t progClosuresSize; // size of list of program launch closures
44 uint64_t progClosuresTrieAddr; // (unslid) address of trie of indexes into program launch closures
45 uint64_t progClosuresTrieSize; // size of trie of indexes into program launch closures
46 uint32_t platform; // platform number (macOS=1, etc)
47 uint32_t formatVersion : 8, // dyld3::closure::kFormatVersion
48 dylibsExpectedOnDisk : 1, // dyld should expect the dylib exists on disk and to compare inode/mtime to see if cache is valid
49 simulator : 1, // for simulator of specified platform
50 locallyBuiltCache : 1, // 0 for B&I built cache, 1 for locally built cache
51 padding : 21; // TBD
52 };
53
54 T_GLOBAL_META(
55 T_META_NAMESPACE("xnu.stackshot"),
56 T_META_CHECK_LEAKS(false),
57 T_META_ASROOT(true)
58 );
59
60 static const char *current_process_name(void);
61 static void verify_stackshot_sharedcache_layout(struct dyld_uuid_info_64 *uuids, uint32_t uuid_count);
62 static void parse_stackshot(uint64_t stackshot_parsing_flags, void *ssbuf, size_t sslen, int child_pid);
63 static void parse_thread_group_stackshot(void **sbuf, size_t sslen);
64 static uint64_t stackshot_timestamp(void *ssbuf, size_t sslen);
65 static void initialize_thread(void);
66
67 #define DEFAULT_STACKSHOT_BUFFER_SIZE (1024 * 1024)
68 #define MAX_STACKSHOT_BUFFER_SIZE (6 * 1024 * 1024)
69
70 /* bit flags for parse_stackshot */
71 #define PARSE_STACKSHOT_DELTA 0x1
72 #define PARSE_STACKSHOT_ZOMBIE 0x2
73 #define PARSE_STACKSHOT_SHAREDCACHE_LAYOUT 0x4
74
75 T_DECL(microstackshots, "test the microstackshot syscall")
76 {
77 void *buf = NULL;
78 unsigned int size = DEFAULT_STACKSHOT_BUFFER_SIZE;
79
80 while (1) {
81 buf = malloc(size);
82 T_QUIET; T_ASSERT_NOTNULL(buf, "allocated stackshot buffer");
83
84 #pragma clang diagnostic push
85 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
86 int len = syscall(SYS_microstackshot, buf, size,
87 STACKSHOT_GET_MICROSTACKSHOT);
88 #pragma clang diagnostic pop
89 if (len == ENOSYS) {
90 T_SKIP("microstackshot syscall failed, likely not compiled with CONFIG_TELEMETRY");
91 }
92 if (len == -1 && errno == ENOSPC) {
93 /* syscall failed because buffer wasn't large enough, try again */
94 free(buf);
95 buf = NULL;
96 size *= 2;
97 T_ASSERT_LE(size, (unsigned int)MAX_STACKSHOT_BUFFER_SIZE,
98 "growing stackshot buffer to sane size");
99 continue;
100 }
101 T_ASSERT_POSIX_SUCCESS(len, "called microstackshot syscall");
102 break;
103 }
104
105 T_EXPECT_EQ(*(uint32_t *)buf,
106 (uint32_t)STACKSHOT_MICRO_SNAPSHOT_MAGIC,
107 "magic value for microstackshot matches");
108
109 free(buf);
110 }
111
112 struct scenario {
113 const char *name;
114 uint32_t flags;
115 bool should_fail;
116 bool maybe_unsupported;
117 pid_t target_pid;
118 uint64_t since_timestamp;
119 uint32_t size_hint;
120 dt_stat_time_t timer;
121 };
122
123 static void
124 quiet(struct scenario *scenario)
125 {
126 if (scenario->timer) {
127 T_QUIET;
128 }
129 }
130
131 static void
132 take_stackshot(struct scenario *scenario, void (^cb)(void *buf, size_t size))
133 {
134 initialize_thread();
135
136 void *config = stackshot_config_create();
137 quiet(scenario);
138 T_ASSERT_NOTNULL(config, "created stackshot config");
139
140 int ret = stackshot_config_set_flags(config, scenario->flags);
141 quiet(scenario);
142 T_ASSERT_POSIX_ZERO(ret, "set flags %#x on stackshot config", scenario->flags);
143
144 if (scenario->size_hint > 0) {
145 ret = stackshot_config_set_size_hint(config, scenario->size_hint);
146 quiet(scenario);
147 T_ASSERT_POSIX_ZERO(ret, "set size hint %" PRIu32 " on stackshot config",
148 scenario->size_hint);
149 }
150
151 if (scenario->target_pid > 0) {
152 ret = stackshot_config_set_pid(config, scenario->target_pid);
153 quiet(scenario);
154 T_ASSERT_POSIX_ZERO(ret, "set target pid %d on stackshot config",
155 scenario->target_pid);
156 }
157
158 if (scenario->since_timestamp > 0) {
159 ret = stackshot_config_set_delta_timestamp(config, scenario->since_timestamp);
160 quiet(scenario);
161 T_ASSERT_POSIX_ZERO(ret, "set since timestamp %" PRIu64 " on stackshot config",
162 scenario->since_timestamp);
163 }
164
165 int retries_remaining = 5;
166
167 retry: ;
168 uint64_t start_time = mach_absolute_time();
169 ret = stackshot_capture_with_config(config);
170 uint64_t end_time = mach_absolute_time();
171
172 if (scenario->should_fail) {
173 T_EXPECTFAIL;
174 T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config");
175 return;
176 }
177
178 if (ret == EBUSY || ret == ETIMEDOUT) {
179 if (retries_remaining > 0) {
180 if (!scenario->timer) {
181 T_LOG("stackshot_capture_with_config failed with %s (%d), retrying",
182 strerror(ret), ret);
183 }
184
185 retries_remaining--;
186 goto retry;
187 } else {
188 T_ASSERT_POSIX_ZERO(ret,
189 "called stackshot_capture_with_config (no retries remaining)");
190 }
191 } else if ((ret == ENOTSUP) && scenario->maybe_unsupported) {
192 T_SKIP("kernel indicated this stackshot configuration is not supported");
193 } else {
194 quiet(scenario);
195 T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config");
196 }
197
198 if (scenario->timer) {
199 dt_stat_mach_time_add(scenario->timer, end_time - start_time);
200 }
201 void *buf = stackshot_config_get_stackshot_buffer(config);
202 size_t size = stackshot_config_get_stackshot_size(config);
203 if (scenario->name) {
204 char sspath[MAXPATHLEN];
205 strlcpy(sspath, scenario->name, sizeof(sspath));
206 strlcat(sspath, ".kcdata", sizeof(sspath));
207 T_QUIET; T_ASSERT_POSIX_ZERO(dt_resultfile(sspath, sizeof(sspath)),
208 "create result file path");
209
210 T_LOG("writing stackshot to %s", sspath);
211
212 FILE *f = fopen(sspath, "w");
213 T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(f,
214 "open stackshot output file");
215
216 size_t written = fwrite(buf, size, 1, f);
217 T_QUIET; T_ASSERT_POSIX_SUCCESS(written, "wrote stackshot to file");
218
219 fclose(f);
220 }
221 cb(buf, size);
222
223 ret = stackshot_config_dealloc(config);
224 T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config");
225 }
226
227 T_DECL(kcdata, "test that kcdata stackshots can be taken and parsed")
228 {
229 struct scenario scenario = {
230 .name = "kcdata",
231 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS |
232 STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT),
233 };
234
235 T_LOG("taking kcdata stackshot");
236 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
237 parse_stackshot(0, ssbuf, sslen, -1);
238 });
239 }
240
241 T_DECL(kcdata_faulting, "test that kcdata stackshots while faulting can be taken and parsed")
242 {
243 struct scenario scenario = {
244 .name = "faulting",
245 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
246 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT
247 | STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING),
248 };
249
250 T_LOG("taking faulting stackshot");
251 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
252 parse_stackshot(0, ssbuf, sslen, -1);
253 });
254 }
255
256 T_DECL(bad_flags, "test a poorly-formed stackshot syscall")
257 {
258 struct scenario scenario = {
259 .flags = STACKSHOT_SAVE_IN_KERNEL_BUFFER /* not allowed from user space */,
260 .should_fail = true,
261 };
262
263 T_LOG("attempting to take stackshot with kernel-only flag");
264 take_stackshot(&scenario, ^(__unused void *ssbuf, __unused size_t sslen) {
265 T_ASSERT_FAIL("stackshot data callback called");
266 });
267 }
268
269 T_DECL(delta, "test delta stackshots")
270 {
271 struct scenario scenario = {
272 .name = "delta",
273 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
274 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT),
275 };
276
277 T_LOG("taking full stackshot");
278 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
279 uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen);
280
281 T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time);
282
283 parse_stackshot(0, ssbuf, sslen, -1);
284
285 struct scenario delta_scenario = {
286 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
287 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT
288 | STACKSHOT_COLLECT_DELTA_SNAPSHOT),
289 .since_timestamp = stackshot_time
290 };
291
292 take_stackshot(&delta_scenario, ^(void *dssbuf, size_t dsslen) {
293 parse_stackshot(PARSE_STACKSHOT_DELTA, dssbuf, dsslen, -1);
294 });
295 });
296 }
297
298 T_DECL(shared_cache_layout, "test stackshot inclusion of shared cache layout")
299 {
300 struct scenario scenario = {
301 .name = "shared_cache_layout",
302 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
303 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT |
304 STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT),
305 };
306
307 T_LOG("taking stackshot with STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT set");
308 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
309 parse_stackshot(PARSE_STACKSHOT_SHAREDCACHE_LAYOUT, ssbuf, sslen, -1);
310 });
311 }
312
313 static void *stuck_sysctl_thread(void *arg) {
314 int val = 1;
315 dispatch_semaphore_t child_thread_started = *(dispatch_semaphore_t *)arg;
316
317 dispatch_semaphore_signal(child_thread_started);
318 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.wedge_thread", NULL, NULL, &val, sizeof(val)), "wedge child thread");
319
320 return NULL;
321 }
322
323 T_HELPER_DECL(zombie_child, "child process to sample as a zombie")
324 {
325 pthread_t pthread;
326 dispatch_semaphore_t child_thread_started = dispatch_semaphore_create(0);
327 T_QUIET; T_ASSERT_NOTNULL(child_thread_started, "zombie child thread semaphore");
328
329 /* spawn another thread to get stuck in the kernel, then call exit() to become a zombie */
330 T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&pthread, NULL, stuck_sysctl_thread, &child_thread_started), "pthread_create");
331
332 dispatch_semaphore_wait(child_thread_started, DISPATCH_TIME_FOREVER);
333
334 /* sleep for a bit in the hope of ensuring that the other thread has called the sysctl before we signal the parent */
335 usleep(100);
336 T_ASSERT_POSIX_SUCCESS(kill(getppid(), SIGUSR1), "signaled parent to take stackshot");
337
338 exit(0);
339 }
340
341 T_DECL(zombie, "tests a stackshot of a zombie task with a thread stuck in the kernel")
342 {
343 char path[PATH_MAX];
344 uint32_t path_size = sizeof(path);
345 T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
346 char *args[] = { path, "-n", "zombie_child", NULL };
347
348 dispatch_source_t child_sig_src;
349 dispatch_semaphore_t child_ready_sem = dispatch_semaphore_create(0);
350 T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "zombie child semaphore");
351
352 dispatch_queue_t signal_processing_q = dispatch_queue_create("signal processing queue", NULL);
353 T_QUIET; T_ASSERT_NOTNULL(child_ready_sem, "signal processing queue");
354
355 pid_t pid;
356
357 T_LOG("spawning a child");
358
359 signal(SIGUSR1, SIG_IGN);
360 child_sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, signal_processing_q);
361 T_QUIET; T_ASSERT_NOTNULL(child_sig_src, "dispatch_source_create (child_sig_src)");
362
363 dispatch_source_set_event_handler(child_sig_src, ^{ dispatch_semaphore_signal(child_ready_sem); });
364 dispatch_activate(child_sig_src);
365
366 int sp_ret = posix_spawn(&pid, args[0], NULL, NULL, args, NULL);
367 T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid);
368
369 dispatch_semaphore_wait(child_ready_sem, DISPATCH_TIME_FOREVER);
370
371 T_LOG("received signal from child, capturing stackshot");
372
373 struct proc_bsdshortinfo bsdshortinfo;
374 int retval, iterations_to_wait = 10;
375
376 while (iterations_to_wait > 0) {
377 retval = proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 0, &bsdshortinfo, sizeof(bsdshortinfo));
378 if ((retval == 0) && errno == ESRCH) {
379 T_LOG("unable to find child using proc_pidinfo, assuming zombie");
380 break;
381 }
382
383 T_QUIET; T_WITH_ERRNO; T_ASSERT_GT(retval, 0, "proc_pidinfo(PROC_PIDT_SHORTBSDINFO) returned a value > 0");
384 T_QUIET; T_ASSERT_EQ(retval, (int)sizeof(bsdshortinfo), "proc_pidinfo call for PROC_PIDT_SHORTBSDINFO returned expected size");
385
386 if (bsdshortinfo.pbsi_flags & PROC_FLAG_INEXIT) {
387 T_LOG("child proc info marked as in exit");
388 break;
389 }
390
391 iterations_to_wait--;
392 if (iterations_to_wait == 0) {
393 /*
394 * This will mark the test as failed but let it continue so we
395 * don't leave a process stuck in the kernel.
396 */
397 T_FAIL("unable to discover that child is marked as exiting");
398 }
399
400 /* Give the child a few more seconds to make it to exit */
401 sleep(5);
402 }
403
404 /* Give the child some more time to make it through exit */
405 sleep(10);
406
407 struct scenario scenario = {
408 .name = "zombie",
409 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
410 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT),
411 };
412
413 take_stackshot(&scenario, ^( void *ssbuf, size_t sslen) {
414 /* First unwedge the child so we can reap it */
415 int val = 1, status;
416 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.unwedge_thread", NULL, NULL, &val, sizeof(val)), "unwedge child");
417
418 T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid on zombie child");
419
420 parse_stackshot(PARSE_STACKSHOT_ZOMBIE, ssbuf, sslen, pid);
421 });
422 }
423
424 static void
425 expect_instrs_cycles_in_stackshot(void *ssbuf, size_t sslen)
426 {
427 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
428
429 bool in_task = false;
430 bool in_thread = false;
431 bool saw_instrs_cycles = false;
432 iter = kcdata_iter_next(iter);
433
434 KCDATA_ITER_FOREACH(iter) {
435 switch (kcdata_iter_type(iter)) {
436 case KCDATA_TYPE_CONTAINER_BEGIN:
437 switch (kcdata_iter_container_type(iter)) {
438 case STACKSHOT_KCCONTAINER_TASK:
439 in_task = true;
440 saw_instrs_cycles = false;
441 break;
442
443 case STACKSHOT_KCCONTAINER_THREAD:
444 in_thread = true;
445 saw_instrs_cycles = false;
446 break;
447
448 default:
449 break;
450 }
451 break;
452
453 case STACKSHOT_KCTYPE_INSTRS_CYCLES:
454 saw_instrs_cycles = true;
455 break;
456
457 case KCDATA_TYPE_CONTAINER_END:
458 if (in_thread) {
459 T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles,
460 "saw instructions and cycles in thread");
461 in_thread = false;
462 } else if (in_task) {
463 T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles,
464 "saw instructions and cycles in task");
465 in_task = false;
466 }
467
468 default:
469 break;
470 }
471 }
472 }
473
474 static void
475 skip_if_monotonic_unsupported(void)
476 {
477 int supported = 0;
478 size_t supported_size = sizeof(supported);
479 int ret = sysctlbyname("kern.monotonic.supported", &supported,
480 &supported_size, 0, 0);
481 if (ret < 0 || !supported) {
482 T_SKIP("monotonic is unsupported");
483 }
484 }
485
486 T_DECL(instrs_cycles, "test a getting instructions and cycles in stackshot")
487 {
488 skip_if_monotonic_unsupported();
489
490 struct scenario scenario = {
491 .name = "instrs-cycles",
492 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES
493 | STACKSHOT_KCDATA_FORMAT),
494 };
495
496 T_LOG("attempting to take stackshot with instructions and cycles");
497 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
498 parse_stackshot(0, ssbuf, sslen, -1);
499 expect_instrs_cycles_in_stackshot(ssbuf, sslen);
500 });
501 }
502
503 T_DECL(delta_instrs_cycles,
504 "test delta stackshots with instructions and cycles")
505 {
506 skip_if_monotonic_unsupported();
507
508 struct scenario scenario = {
509 .name = "delta-instrs-cycles",
510 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES
511 | STACKSHOT_KCDATA_FORMAT),
512 };
513
514 T_LOG("taking full stackshot");
515 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
516 uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen);
517
518 T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time);
519
520 parse_stackshot(0, ssbuf, sslen, -1);
521 expect_instrs_cycles_in_stackshot(ssbuf, sslen);
522
523 struct scenario delta_scenario = {
524 .name = "delta-instrs-cycles-next",
525 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES
526 | STACKSHOT_KCDATA_FORMAT
527 | STACKSHOT_COLLECT_DELTA_SNAPSHOT),
528 .since_timestamp = stackshot_time,
529 };
530
531 take_stackshot(&delta_scenario, ^(void *dssbuf, size_t dsslen) {
532 parse_stackshot(PARSE_STACKSHOT_DELTA, dssbuf, dsslen, -1);
533 expect_instrs_cycles_in_stackshot(dssbuf, dsslen);
534 });
535 });
536 }
537
538 static void
539 check_thread_groups_supported()
540 {
541 int err;
542 int supported = 0;
543 size_t supported_size = sizeof(supported);
544 err = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size, NULL, 0);
545
546 if (err || !supported)
547 T_SKIP("thread groups not supported on this system");
548 }
549
550 T_DECL(thread_groups, "test getting thread groups in stackshot")
551 {
552 check_thread_groups_supported();
553
554 struct scenario scenario = {
555 .name = "thread-groups",
556 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_THREAD_GROUP
557 | STACKSHOT_KCDATA_FORMAT),
558 };
559
560 T_LOG("attempting to take stackshot with thread group flag");
561 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
562 parse_thread_group_stackshot(ssbuf, sslen);
563 });
564 }
565
566 static void
567 parse_page_table_asid_stackshot(void **ssbuf, size_t sslen)
568 {
569 bool seen_asid = false;
570 bool seen_page_table_snapshot = false;
571 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
572 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT,
573 "buffer provided is a stackshot");
574
575 iter = kcdata_iter_next(iter);
576 KCDATA_ITER_FOREACH(iter) {
577 switch (kcdata_iter_type(iter)) {
578 case KCDATA_TYPE_ARRAY: {
579 T_QUIET;
580 T_ASSERT_TRUE(kcdata_iter_array_valid(iter),
581 "checked that array is valid");
582
583 if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_PAGE_TABLES) {
584 continue;
585 }
586
587 T_ASSERT_FALSE(seen_page_table_snapshot, "check that we haven't yet seen a page table snapshot");
588 seen_page_table_snapshot = true;
589
590 T_ASSERT_EQ((size_t) kcdata_iter_array_elem_size(iter), sizeof(uint64_t),
591 "check that each element of the pagetable dump is the expected size");
592
593 uint64_t *pt_array = kcdata_iter_payload(iter);
594 uint32_t elem_count = kcdata_iter_array_elem_count(iter);
595 uint32_t j;
596 bool nonzero_tte = false;
597 for (j = 0; j < elem_count;) {
598 T_QUIET; T_ASSERT_LE(j + 4, elem_count, "check for valid page table segment header");
599 uint64_t pa = pt_array[j];
600 uint64_t num_entries = pt_array[j + 1];
601 uint64_t start_va = pt_array[j + 2];
602 uint64_t end_va = pt_array[j + 3];
603
604 T_QUIET; T_ASSERT_NE(pa, (uint64_t) 0, "check that the pagetable physical address is non-zero");
605 T_QUIET; T_ASSERT_EQ(pa % (num_entries * sizeof(uint64_t)), (uint64_t) 0, "check that the pagetable physical address is correctly aligned");
606 T_QUIET; T_ASSERT_NE(num_entries, (uint64_t) 0, "check that a pagetable region has more than 0 entries");
607 T_QUIET; T_ASSERT_LE(j + 4 + num_entries, (uint64_t) elem_count, "check for sufficient space in page table array");
608 T_QUIET; T_ASSERT_GT(end_va, start_va, "check for valid VA bounds in page table segment header");
609
610 for (uint32_t k = j + 4; k < (j + 4 + num_entries); ++k) {
611 if (pt_array[k] != 0) {
612 nonzero_tte = true;
613 T_QUIET; T_ASSERT_EQ((pt_array[k] >> 48) & 0xf, (uint64_t) 0, "check that bits[48:51] of arm64 TTE are clear");
614 // L0-L2 table and non-compressed L3 block entries should always have bit 1 set; assumes L0-L2 blocks will not be used outside the kernel
615 bool table = ((pt_array[k] & 0x2) != 0);
616 if (table) {
617 T_QUIET; T_ASSERT_NE(pt_array[k] & ((1ULL << 48) - 1) & ~((1ULL << 12) - 1), (uint64_t) 0, "check that arm64 TTE physical address is non-zero");
618 } else { // should be a compressed PTE
619 T_QUIET; T_ASSERT_NE(pt_array[k] & 0xC000000000000000ULL, (uint64_t) 0, "check that compressed PTE has at least one of bits [63:62] set");
620 T_QUIET; T_ASSERT_EQ(pt_array[k] & ~0xC000000000000000ULL, (uint64_t) 0, "check that compressed PTE has no other bits besides [63:62] set");
621 }
622 }
623 }
624
625 j += (4 + num_entries);
626 }
627 T_ASSERT_TRUE(nonzero_tte, "check that we saw at least one non-empty TTE");
628 T_ASSERT_EQ(j, elem_count, "check that page table dump size matches extent of last header");
629 break;
630 }
631 case STACKSHOT_KCTYPE_ASID: {
632 T_ASSERT_FALSE(seen_asid, "check that we haven't yet seen an ASID");
633 seen_asid = true;
634 }
635 }
636 }
637 T_ASSERT_TRUE(seen_page_table_snapshot, "check that we have seen a page table snapshot");
638 T_ASSERT_TRUE(seen_asid, "check that we have seen an ASID");
639 }
640
641 T_DECL(dump_page_tables, "test stackshot page table dumping support")
642 {
643 struct scenario scenario = {
644 .name = "asid-page-tables",
645 .flags = (STACKSHOT_KCDATA_FORMAT | STACKSHOT_ASID | STACKSHOT_PAGE_TABLES),
646 .size_hint = (1ULL << 23), // 8 MB
647 .target_pid = getpid(),
648 .maybe_unsupported = true,
649 };
650
651 T_LOG("attempting to take stackshot with ASID and page table flags");
652 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
653 parse_page_table_asid_stackshot(ssbuf, sslen);
654 });
655 }
656
657 #pragma mark performance tests
658
659 #define SHOULD_REUSE_SIZE_HINT 0x01
660 #define SHOULD_USE_DELTA 0x02
661 #define SHOULD_TARGET_SELF 0x04
662
663 static void
664 stackshot_perf(unsigned int options)
665 {
666 struct scenario scenario = {
667 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
668 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT),
669 };
670
671 dt_stat_t size = dt_stat_create("bytes", "size");
672 dt_stat_time_t duration = dt_stat_time_create("duration");
673 scenario.timer = duration;
674
675 if (options & SHOULD_TARGET_SELF) {
676 scenario.target_pid = getpid();
677 }
678
679 while (!dt_stat_stable(duration) || !dt_stat_stable(size)) {
680 __block uint64_t last_time = 0;
681 __block uint32_t size_hint = 0;
682 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
683 dt_stat_add(size, (double)sslen);
684 last_time = stackshot_timestamp(ssbuf, sslen);
685 size_hint = (uint32_t)sslen;
686 });
687 if (options & SHOULD_USE_DELTA) {
688 scenario.since_timestamp = last_time;
689 scenario.flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT;
690 }
691 if (options & SHOULD_REUSE_SIZE_HINT) {
692 scenario.size_hint = size_hint;
693 }
694 }
695
696 dt_stat_finalize(duration);
697 dt_stat_finalize(size);
698 }
699
700 T_DECL(perf_no_size_hint, "test stackshot performance with no size hint",
701 T_META_TAG_PERF)
702 {
703 stackshot_perf(0);
704 }
705
706 T_DECL(perf_size_hint, "test stackshot performance with size hint",
707 T_META_TAG_PERF)
708 {
709 stackshot_perf(SHOULD_REUSE_SIZE_HINT);
710 }
711
712 T_DECL(perf_process, "test stackshot performance targeted at process",
713 T_META_TAG_PERF)
714 {
715 stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_TARGET_SELF);
716 }
717
718 T_DECL(perf_delta, "test delta stackshot performance",
719 T_META_TAG_PERF)
720 {
721 stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA);
722 }
723
724 T_DECL(perf_delta_process, "test delta stackshot performance targeted at a process",
725 T_META_TAG_PERF)
726 {
727 stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA | SHOULD_TARGET_SELF);
728 }
729
730 static uint64_t
731 stackshot_timestamp(void *ssbuf, size_t sslen)
732 {
733 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
734
735 uint32_t type = kcdata_iter_type(iter);
736 if (type != KCDATA_BUFFER_BEGIN_STACKSHOT && type != KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT) {
737 T_ASSERT_FAIL("invalid kcdata type %u", kcdata_iter_type(iter));
738 }
739
740 iter = kcdata_iter_find_type(iter, KCDATA_TYPE_MACH_ABSOLUTE_TIME);
741 T_QUIET;
742 T_ASSERT_TRUE(kcdata_iter_valid(iter), "timestamp found in stackshot");
743
744 return *(uint64_t *)kcdata_iter_payload(iter);
745 }
746
747 #define TEST_THREAD_NAME "stackshot_test_thread"
748
749 static void
750 parse_thread_group_stackshot(void **ssbuf, size_t sslen)
751 {
752 bool seen_thread_group_snapshot = false;
753 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
754 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT,
755 "buffer provided is a stackshot");
756
757 NSMutableSet *thread_groups = [[NSMutableSet alloc] init];
758
759 iter = kcdata_iter_next(iter);
760 KCDATA_ITER_FOREACH(iter) {
761 switch (kcdata_iter_type(iter)) {
762 case KCDATA_TYPE_ARRAY: {
763 T_QUIET;
764 T_ASSERT_TRUE(kcdata_iter_array_valid(iter),
765 "checked that array is valid");
766
767 if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT) {
768 continue;
769 }
770
771 seen_thread_group_snapshot = true;
772
773 if (kcdata_iter_array_elem_size(iter) >= sizeof(struct thread_group_snapshot_v2)) {
774 struct thread_group_snapshot_v2 *tgs_array = kcdata_iter_payload(iter);
775 for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) {
776 struct thread_group_snapshot_v2 *tgs = tgs_array + j;
777 [thread_groups addObject:@(tgs->tgs_id)];
778 }
779
780 }
781 else {
782 struct thread_group_snapshot *tgs_array = kcdata_iter_payload(iter);
783 for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) {
784 struct thread_group_snapshot *tgs = tgs_array + j;
785 [thread_groups addObject:@(tgs->tgs_id)];
786 }
787 }
788 break;
789 }
790 }
791 }
792 KCDATA_ITER_FOREACH(iter) {
793 NSError *error = nil;
794
795 switch (kcdata_iter_type(iter)) {
796
797 case KCDATA_TYPE_CONTAINER_BEGIN: {
798 T_QUIET;
799 T_ASSERT_TRUE(kcdata_iter_container_valid(iter),
800 "checked that container is valid");
801
802 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_THREAD) {
803 break;
804 }
805
806 NSDictionary *container = parseKCDataContainer(&iter, &error);
807 T_QUIET; T_ASSERT_NOTNULL(container, "parsed container from stackshot");
808 T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container");
809
810 int tg = [container[@"thread_snapshots"][@"thread_group"] intValue];
811
812 T_ASSERT_TRUE([thread_groups containsObject:@(tg)], "check that the thread group the thread is in exists");
813
814 break;
815 };
816
817 }
818 }
819 T_ASSERT_TRUE(seen_thread_group_snapshot, "check that we have seen a thread group snapshot");
820 }
821
822 static void
823 verify_stackshot_sharedcache_layout(struct dyld_uuid_info_64 *uuids, uint32_t uuid_count)
824 {
825 uuid_t cur_shared_cache_uuid;
826 __block uint32_t lib_index = 0, libs_found = 0;
827
828 _dyld_get_shared_cache_uuid(cur_shared_cache_uuid);
829 int result = dyld_shared_cache_iterate_text(cur_shared_cache_uuid, ^(const dyld_shared_cache_dylib_text_info* info) {
830 T_QUIET; T_ASSERT_LT(lib_index, uuid_count, "dyld_shared_cache_iterate_text exceeded number of libraries returned by kernel");
831
832 libs_found++;
833 struct dyld_uuid_info_64 *cur_stackshot_uuid_entry = &uuids[lib_index];
834 T_QUIET; T_ASSERT_EQ(memcmp(info->dylibUuid, cur_stackshot_uuid_entry->imageUUID, sizeof(info->dylibUuid)), 0,
835 "dyld returned UUID doesn't match kernel returned UUID");
836 T_QUIET; T_ASSERT_EQ(info->loadAddressUnslid, cur_stackshot_uuid_entry->imageLoadAddress,
837 "dyld returned load address doesn't match kernel returned load address");
838 lib_index++;
839 });
840
841 T_ASSERT_EQ(result, 0, "iterate shared cache layout");
842 T_ASSERT_EQ(libs_found, uuid_count, "dyld iterator returned same number of libraries as kernel");
843
844 T_LOG("verified %d libraries from dyld shared cache", libs_found);
845 }
846
847 static void
848 parse_stackshot(uint64_t stackshot_parsing_flags, void *ssbuf, size_t sslen, int child_pid)
849 {
850 bool delta = (stackshot_parsing_flags & PARSE_STACKSHOT_DELTA);
851 bool expect_zombie_child = (stackshot_parsing_flags & PARSE_STACKSHOT_ZOMBIE);
852 bool expect_shared_cache_layout = false;
853 bool expect_shared_cache_uuid = !delta;
854 bool found_zombie_child = false, found_shared_cache_layout = false, found_shared_cache_uuid = false;
855
856 if (stackshot_parsing_flags & PARSE_STACKSHOT_SHAREDCACHE_LAYOUT) {
857 size_t shared_cache_length = 0;
858 const struct dyld_cache_header *cache_header = NULL;
859 cache_header = _dyld_get_shared_cache_range(&shared_cache_length);
860 T_QUIET; T_ASSERT_NOTNULL(cache_header, "current process running with shared cache");
861 T_QUIET; T_ASSERT_GT(shared_cache_length, sizeof(struct _dyld_cache_header), "valid shared cache length populated by _dyld_get_shared_cache_range");
862
863 if (cache_header->locallyBuiltCache) {
864 T_LOG("device running with locally built shared cache, expect shared cache layout");
865 expect_shared_cache_layout = true;
866 } else {
867 T_LOG("device running with B&I built shared-cache, no shared cache layout expected");
868 }
869 }
870
871 if (expect_zombie_child) {
872 T_QUIET; T_ASSERT_GT(child_pid, 0, "child pid greater than zero");
873 }
874
875 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
876 if (delta) {
877 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT,
878 "buffer provided is a delta stackshot");
879 } else {
880 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT,
881 "buffer provided is a stackshot");
882 }
883
884 iter = kcdata_iter_next(iter);
885 KCDATA_ITER_FOREACH(iter) {
886 NSError *error = nil;
887
888 switch (kcdata_iter_type(iter)) {
889 case KCDATA_TYPE_ARRAY: {
890 T_QUIET;
891 T_ASSERT_TRUE(kcdata_iter_array_valid(iter),
892 "checked that array is valid");
893
894 NSMutableDictionary *array = parseKCDataArray(iter, &error);
895 T_QUIET; T_ASSERT_NOTNULL(array, "parsed array from stackshot");
896 T_QUIET; T_ASSERT_NULL(error, "error unset after parsing array");
897
898 if (kcdata_iter_array_elem_type(iter) == STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT) {
899 struct dyld_uuid_info_64 *shared_cache_uuids = kcdata_iter_payload(iter);
900 uint32_t uuid_count = kcdata_iter_array_elem_count(iter);
901 T_ASSERT_NOTNULL(shared_cache_uuids, "parsed shared cache layout array");
902 T_ASSERT_GT(uuid_count, 0, "returned valid number of UUIDs from shared cache");
903 verify_stackshot_sharedcache_layout(shared_cache_uuids, uuid_count);
904 found_shared_cache_layout = true;
905 }
906
907 break;
908 }
909
910 case KCDATA_TYPE_CONTAINER_BEGIN: {
911 T_QUIET;
912 T_ASSERT_TRUE(kcdata_iter_container_valid(iter),
913 "checked that container is valid");
914
915 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) {
916 break;
917 }
918
919 NSDictionary *container = parseKCDataContainer(&iter, &error);
920 T_QUIET; T_ASSERT_NOTNULL(container, "parsed container from stackshot");
921 T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container");
922
923 int pid = [container[@"task_snapshots"][@"task_snapshot"][@"ts_pid"] intValue];
924 if (expect_zombie_child && (pid == child_pid)) {
925 found_zombie_child = true;
926
927 uint64_t task_flags = [container[@"task_snapshots"][@"task_snapshot"][@"ts_ss_flags"] unsignedLongLongValue];
928 T_ASSERT_TRUE((task_flags & kTerminatedSnapshot) == kTerminatedSnapshot, "child zombie marked as terminated");
929
930 continue;
931 } else if (pid != getpid()) {
932 break;
933 }
934
935 T_EXPECT_EQ_STR(current_process_name(),
936 [container[@"task_snapshots"][@"task_snapshot"][@"ts_p_comm"] UTF8String],
937 "current process name matches in stackshot");
938
939 uint64_t task_flags = [container[@"task_snapshots"][@"task_snapshot"][@"ts_ss_flags"] unsignedLongLongValue];
940 T_ASSERT_FALSE((task_flags & kTerminatedSnapshot) == kTerminatedSnapshot, "current process not marked as terminated");
941
942 T_QUIET;
943 T_EXPECT_LE(pid, [container[@"task_snapshots"][@"task_snapshot"][@"ts_unique_pid"] intValue],
944 "unique pid is greater than pid");
945
946 bool found_main_thread = false;
947 for (id thread_key in container[@"task_snapshots"][@"thread_snapshots"]) {
948 NSMutableDictionary *thread = container[@"task_snapshots"][@"thread_snapshots"][thread_key];
949 NSDictionary *thread_snap = thread[@"thread_snapshot"];
950
951 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_thread_id"] intValue], 0,
952 "thread ID of thread in current task is valid");
953 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_base_priority"] intValue], 0,
954 "base priority of thread in current task is valid");
955 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_sched_priority"] intValue], 0,
956 "scheduling priority of thread in current task is valid");
957
958 NSString *pth_name = thread[@"pth_name"];
959 if (pth_name != nil && [pth_name isEqualToString:@TEST_THREAD_NAME]) {
960 found_main_thread = true;
961
962 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_total_syscalls"] intValue], 0,
963 "total syscalls of current thread is valid");
964
965 NSDictionary *cpu_times = thread[@"cpu_times"];
966 T_EXPECT_GE([cpu_times[@"runnable_time"] intValue],
967 [cpu_times[@"system_time"] intValue] +
968 [cpu_times[@"user_time"] intValue],
969 "runnable time of current thread is valid");
970 }
971 }
972 T_EXPECT_TRUE(found_main_thread, "found main thread for current task in stackshot");
973 break;
974 }
975 case STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO: {
976 struct dyld_uuid_info_64_v2 *shared_cache_info = kcdata_iter_payload(iter);
977 uuid_t shared_cache_uuid;
978 T_QUIET; T_ASSERT_TRUE(_dyld_get_shared_cache_uuid(shared_cache_uuid), "retrieve current shared cache UUID");
979 T_QUIET; T_ASSERT_EQ(memcmp(shared_cache_info->imageUUID, shared_cache_uuid, sizeof(shared_cache_uuid)), 0,
980 "dyld returned UUID doesn't match kernel returned UUID for system shared cache");
981 found_shared_cache_uuid = true;
982 break;
983 }
984 }
985 }
986
987 if (expect_zombie_child) {
988 T_QUIET; T_ASSERT_TRUE(found_zombie_child, "found zombie child in kcdata");
989 }
990
991 if (expect_shared_cache_layout) {
992 T_QUIET; T_ASSERT_TRUE(found_shared_cache_layout, "shared cache layout found in kcdata");
993 }
994
995 if (expect_shared_cache_uuid) {
996 T_QUIET; T_ASSERT_TRUE(found_shared_cache_uuid, "shared cache UUID found in kcdata");
997 }
998
999 T_ASSERT_FALSE(KCDATA_ITER_FOREACH_FAILED(iter), "successfully iterated kcdata");
1000 }
1001
1002 static const char *
1003 current_process_name(void)
1004 {
1005 static char name[64];
1006
1007 if (!name[0]) {
1008 int ret = proc_name(getpid(), name, sizeof(name));
1009 T_QUIET;
1010 T_ASSERT_POSIX_SUCCESS(ret, "proc_name failed for current process");
1011 }
1012
1013 return name;
1014 }
1015
1016 static void
1017 initialize_thread(void)
1018 {
1019 int ret = pthread_setname_np(TEST_THREAD_NAME);
1020 T_QUIET;
1021 T_ASSERT_POSIX_ZERO(ret, "set thread name to %s", TEST_THREAD_NAME);
1022 }