xref: /xnu-8019.80.24/tests/jumbo_va_spaces_28530648.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 #include <stdio.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/mman.h>
6 
7 #include <darwintest.h>
8 #include <darwintest_utils.h>
9 
10 T_GLOBAL_META(
11 	T_META_NAMESPACE("xnu.vm"),
12 	T_META_RADAR_COMPONENT_NAME("xnu"),
13 	T_META_RADAR_COMPONENT_VERSION("VM"),
14 	T_META_RUN_CONCURRENTLY(true));
15 
16 #define GB (1ULL * 1024 * 1024 * 1024)
17 
18 /*
19  * This test expects the entitlement to be the enabling factor for a process to
20  * allocate at least this many GB of VA space. i.e. with the entitlement, n GB
21  * must be allocatable; whereas without it, it must be less.
22  * This value was determined experimentally to fit on applicable devices and to
23  * be clearly distinguishable from the default VA limit.
24  */
25 #define ALLOC_TEST_GB 53
26 
27 T_DECL(TESTNAME,
28 	"Verify that a required entitlement is present in order to be granted an extra-large "
29 	"VA space on arm64",
30 	T_META_CHECK_LEAKS(false))
31 {
32 	int	i;
33 	void	*res;
34 
35 	if (!dt_64_bit_kernel()) {
36 		T_SKIP("This test is only applicable to arm64");
37 	}
38 
39 	T_LOG("Attemping to allocate VA space in 1 GB chunks.");
40 
41 	for (i = 0; i < (ALLOC_TEST_GB * 2); i++) {
42 		res = mmap(NULL, 1 * GB, PROT_NONE, MAP_PRIVATE | MAP_ANON, 0, 0);
43 		if (res == MAP_FAILED) {
44 			if (errno != ENOMEM) {
45 				T_WITH_ERRNO;
46 				T_LOG("mmap failed: stopped at %d of %d GB allocated", i, ALLOC_TEST_GB);
47 			}
48 			break;
49 		} else {
50 			T_LOG("%d: %p\n", i, res);
51 		}
52 	}
53 
54 #if defined(ENTITLED)
55 	T_EXPECT_GE_INT(i, ALLOC_TEST_GB, "Allocate at least %d GB of VA space", ALLOC_TEST_GB);
56 #else
57 	T_EXPECT_LT_INT(i, ALLOC_TEST_GB, "Not permitted to allocate %d GB of VA space", ALLOC_TEST_GB);
58 #endif
59 }
60