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