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