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 = strrchr(z_name, '.') + 1;
43 T_QUIET; T_ASSERT_NOTNULL(size_ptr, "couldn't find size in name");
44 size = strtoul(size_ptr, NULL, 10);
45 T_LOG("ZONE NAME: %-25s ELEMENT SIZE: %llu", z_name, size);
46 T_QUIET; T_ASSERT_EQ(size, info[i].mzi_elem_size, "kalloc zone name and element size don't match");
47 }
48 }
49
50 if ((name != NULL) && (nameCnt != 0)) {
51 kr = vm_deallocate(mach_task_self(), (vm_address_t) name,
52 (vm_size_t) (nameCnt * sizeof *name));
53 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate name");
54 }
55
56 if ((info != NULL) && (infoCnt != 0)) {
57 kr = vm_deallocate(mach_task_self(), (vm_address_t) info,
58 (vm_size_t) (infoCnt * sizeof *info));
59 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate info");
60 }
61
62 if ((wiredInfo != NULL) && (wiredInfoCnt != 0)) {
63 kr = vm_deallocate(mach_task_self(), (vm_address_t) wiredInfo,
64 (vm_size_t) (wiredInfoCnt * sizeof *wiredInfo));
65 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate wiredInfo");
66 }
67
68 T_END;
69 }
70
71 T_DECL( verify_kalloc_config,
72 "verifies that the kalloc zones are configured correctly",
73 T_META_ASROOT(true), T_META_TAG_VM_PREFERRED)
74 {
75 run_test();
76 }
77