1 #include <string.h>
2 #include <stdlib.h>
3 #include <mach/mach.h>
4 #include <mach_debug/mach_debug.h>
5 #include <darwintest.h>
6
7 T_GLOBAL_META(
8 T_META_NAMESPACE("xnu.vm"),
9 T_META_RADAR_COMPONENT_NAME("xnu"),
10 T_META_RADAR_COMPONENT_VERSION("zalloc"),
11 T_META_CHECK_LEAKS(false),
12 T_META_RUN_CONCURRENTLY(true)
13 );
14
15 static void run_test(void);
16
17 static void
run_test(void)18 run_test(void)
19 {
20 kern_return_t kr;
21 uint64_t size, i;
22 mach_zone_name_t *name = NULL;
23 unsigned int nameCnt = 0;
24 mach_zone_info_t *info = NULL;
25 unsigned int infoCnt = 0;
26 mach_memory_info_t *wiredInfo = NULL;
27 unsigned int wiredInfoCnt = 0;
28 const char kalloc_str[] = "kalloc.";
29 const char type_str[] = "type";
30 size_t kt_name_len = strlen(kalloc_str) + strlen(type_str);
31
32 kr = mach_memory_info(mach_host_self(),
33 &name, &nameCnt, &info, &infoCnt,
34 &wiredInfo, &wiredInfoCnt);
35 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_memory_info");
36 T_QUIET; T_ASSERT_EQ(nameCnt, infoCnt, "zone name and info counts don't match");
37
38 /* Match the names of the kalloc zones against their element sizes. */
39 for (i = 0; i < nameCnt; i++) {
40 const char *z_name = &name[i].mzn_name;
41 if (strncmp(z_name, kalloc_str, strlen(kalloc_str)) == 0) {
42 const char *size_ptr = &z_name[strlen(kalloc_str)];
43 /*
44 * Adjust size pointer for kalloc type zones
45 */
46 if (strlen(z_name) > kt_name_len &&
47 strncmp(&z_name[strlen(kalloc_str)], type_str,
48 strlen(type_str)) == 0) {
49 size_ptr = strchr(&z_name[kt_name_len], '.') + 1;
50 }
51 size = strtoul(size_ptr, NULL, 10);
52 T_LOG("ZONE NAME: %-25s ELEMENT SIZE: %llu", z_name, size);
53 T_QUIET; T_ASSERT_EQ(size, info[i].mzi_elem_size, "kalloc zone name and element size don't match");
54 }
55 }
56
57 if ((name != NULL) && (nameCnt != 0)) {
58 kr = vm_deallocate(mach_task_self(), (vm_address_t) name,
59 (vm_size_t) (nameCnt * sizeof *name));
60 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate name");
61 }
62
63 if ((info != NULL) && (infoCnt != 0)) {
64 kr = vm_deallocate(mach_task_self(), (vm_address_t) info,
65 (vm_size_t) (infoCnt * sizeof *info));
66 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate info");
67 }
68
69 if ((wiredInfo != NULL) && (wiredInfoCnt != 0)) {
70 kr = vm_deallocate(mach_task_self(), (vm_address_t) wiredInfo,
71 (vm_size_t) (wiredInfoCnt * sizeof *wiredInfo));
72 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate wiredInfo");
73 }
74
75 T_END;
76 }
77
78 T_DECL( verify_kalloc_config,
79 "verifies that the kalloc zones are configured correctly",
80 T_META_ASROOT(true))
81 {
82 run_test();
83 }
84