xref: /xnu-10002.1.13/tests/jumbo_va_spaces_common.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1 #include <errno.h>
2 #include <unistd.h>
3 #include <sys/mman.h>
4 
5 #include <darwintest.h>
6 
7 #include "jumbo_va_spaces_common.h"
8 
9 #define GB (1ULL * 1024 * 1024 * 1024)
10 
11 /*
12  * This test expects the entitlement to be the enabling factor for a process to
13  * allocate at least this many GB of VA space. i.e. with the entitlement, n GB
14  * must be allocatable; whereas without it, it must be less.
15  * This value was determined experimentally to fit on applicable devices and to
16  * be clearly distinguishable from the default VA limit.
17  */
18 #define ALLOC_TEST_GB 53
19 
20 void
verify_jumbo_va(bool entitled)21 verify_jumbo_va(bool entitled)
22 {
23 	T_LOG("Attemping to allocate VA space in 1 GB chunks.");
24 	void *res;
25 	int i;
26 
27 	for (i = 0; i < (ALLOC_TEST_GB * 2); i++) {
28 		res = mmap(NULL, 1 * GB, PROT_NONE, MAP_PRIVATE | MAP_ANON, 0, 0);
29 		if (res == MAP_FAILED) {
30 			if (errno != ENOMEM) {
31 				T_WITH_ERRNO;
32 				T_LOG("mmap failed: stopped at %d of %d GB allocated", i, ALLOC_TEST_GB);
33 			}
34 			break;
35 		} else {
36 			T_LOG("%d: %p\n", i, res);
37 		}
38 	}
39 
40 	if (entitled) {
41 		T_EXPECT_GE_INT(i, ALLOC_TEST_GB, "Allocate at least %d GB of VA space", ALLOC_TEST_GB);
42 	} else {
43 		T_EXPECT_LT_INT(i, ALLOC_TEST_GB, "Not permitted to allocate %d GB of VA space", ALLOC_TEST_GB);
44 	}
45 }
46