xref: /xnu-8796.121.2/tests/vm/anon_max_size.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <darwintest.h>
2*c54f35caSApple OSS Distributions 
3*c54f35caSApple OSS Distributions #include <errno.h>
4*c54f35caSApple OSS Distributions #include <stdio.h>
5*c54f35caSApple OSS Distributions #include <stdlib.h>
6*c54f35caSApple OSS Distributions #include <string.h>
7*c54f35caSApple OSS Distributions 
8*c54f35caSApple OSS Distributions #include <sys/mman.h>
9*c54f35caSApple OSS Distributions 
10*c54f35caSApple OSS Distributions #include <mach/mach_error.h>
11*c54f35caSApple OSS Distributions #include <mach/mach_init.h>
12*c54f35caSApple OSS Distributions #include <mach/mach_vm.h>
13*c54f35caSApple OSS Distributions 
14*c54f35caSApple OSS Distributions #define ANON_MAX_PAGES 0xFFFFFFFFULL
15*c54f35caSApple OSS Distributions #define ANON_MAX_SIZE (ANON_MAX_PAGES * vm_kernel_page_size)
16*c54f35caSApple OSS Distributions 
17*c54f35caSApple OSS Distributions T_DECL(anon_max_size, "Test an ALLOC_MAX_SIZE allocation",
18*c54f35caSApple OSS Distributions     T_META_NAMESPACE("xnu.vm"),
19*c54f35caSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
20*c54f35caSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("VM"),
21*c54f35caSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1))
22*c54f35caSApple OSS Distributions {
23*c54f35caSApple OSS Distributions 	kern_return_t kr;
24*c54f35caSApple OSS Distributions 	mach_vm_address_t vm_addr;
25*c54f35caSApple OSS Distributions 	mach_vm_size_t vm_size;
26*c54f35caSApple OSS Distributions 	unsigned char *cp;
27*c54f35caSApple OSS Distributions 	int ret;
28*c54f35caSApple OSS Distributions 	unsigned char vec;
29*c54f35caSApple OSS Distributions 
30*c54f35caSApple OSS Distributions 	/* allocate the largest anonymous size possible */
31*c54f35caSApple OSS Distributions 	vm_size = ANON_MAX_SIZE;
32*c54f35caSApple OSS Distributions 	/* truncate to avoid going over actual max size when rounding up */
33*c54f35caSApple OSS Distributions 	vm_size &= ~PAGE_MASK;
34*c54f35caSApple OSS Distributions 	vm_addr = 0;
35*c54f35caSApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(),
36*c54f35caSApple OSS Distributions 	    &vm_addr,
37*c54f35caSApple OSS Distributions 	    vm_size,
38*c54f35caSApple OSS Distributions 	    VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE);
39*c54f35caSApple OSS Distributions 	if (kr == KERN_NO_SPACE) {
40*c54f35caSApple OSS Distributions 		T_SKIP("not enough address space...");
41*c54f35caSApple OSS Distributions 	}
42*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(0x%llx)", ANON_MAX_SIZE);
43*c54f35caSApple OSS Distributions 
44*c54f35caSApple OSS Distributions 	/* dirty the first and last pages */
45*c54f35caSApple OSS Distributions 	cp = (unsigned char *)(uintptr_t)vm_addr;
46*c54f35caSApple OSS Distributions 	cp[0] = 'a';
47*c54f35caSApple OSS Distributions 	cp[vm_size - 1] = 'z';
48*c54f35caSApple OSS Distributions 
49*c54f35caSApple OSS Distributions 	/* trigger the VM compressor for that VM object */
50*c54f35caSApple OSS Distributions 	ret = madvise(cp, (size_t)vm_size, MADV_PAGEOUT);
51*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "madvise(MADV_PAGEOUT)");
52*c54f35caSApple OSS Distributions 
53*c54f35caSApple OSS Distributions 	/* wait for the pages to be (asynchronously) compressed */
54*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("waiting for first page to be paged out...");
55*c54f35caSApple OSS Distributions 	do {
56*c54f35caSApple OSS Distributions 		ret = mincore(&cp[0], 1, (char *)&vec);
57*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore(1st)");
58*c54f35caSApple OSS Distributions 	} while (vec & MINCORE_INCORE);
59*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("waiting for last page to be paged out...");
60*c54f35caSApple OSS Distributions 	do {
61*c54f35caSApple OSS Distributions 		ret = mincore(&cp[vm_size - 1], 1, (char *)&vec);
62*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore(last)");
63*c54f35caSApple OSS Distributions 	} while (vec & MINCORE_INCORE);
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions 	/* trigger pageins and check the contents */
66*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(cp[0], 'a', "first page intact");
67*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(cp[vm_size - 1], 'z', "last page intact");
68*c54f35caSApple OSS Distributions 
69*c54f35caSApple OSS Distributions 	/* success */
70*c54f35caSApple OSS Distributions 	return;
71*c54f35caSApple OSS Distributions }
72