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