xref: /xnu-8796.141.3/tests/restrict_jit.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdlib.h>
2*1b191cb5SApple OSS Distributions #include <unistd.h>
3*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
4*1b191cb5SApple OSS Distributions #include <sys/mman.h>
5*1b191cb5SApple OSS Distributions 
6*1b191cb5SApple OSS Distributions #include <darwintest.h>
7*1b191cb5SApple OSS Distributions 
8*1b191cb5SApple OSS Distributions 
9*1b191cb5SApple OSS Distributions /*
10*1b191cb5SApple OSS Distributions  * macOS only test. Try to map 2 different MAP_JIT regions. 2nd should fail.
11*1b191cb5SApple OSS Distributions  */
12*1b191cb5SApple OSS Distributions T_DECL(restrict_jit, "macOS restricted JIT entitlement test")
13*1b191cb5SApple OSS Distributions {
14*1b191cb5SApple OSS Distributions #if TARGET_OS_OSX
15*1b191cb5SApple OSS Distributions 	void *addr1;
16*1b191cb5SApple OSS Distributions 	void *addr2;
17*1b191cb5SApple OSS Distributions 	size_t size = 64 * 1024;
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions 	addr1 = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0);
21*1b191cb5SApple OSS Distributions 	T_ASSERT_NE_PTR(addr1, MAP_FAILED, "First map MAP_JIT");
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions 	addr2 = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0);
24*1b191cb5SApple OSS Distributions 	if (addr2 == MAP_FAILED) {
25*1b191cb5SApple OSS Distributions 		T_PASS("Only one MAP_JIT was allowed");
26*1b191cb5SApple OSS Distributions 	} else {
27*1b191cb5SApple OSS Distributions 		T_FAIL("Second MAP_JIT was allowed");
28*1b191cb5SApple OSS Distributions 	}
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions #else
31*1b191cb5SApple OSS Distributions 	T_SKIP("Not macOS");
32*1b191cb5SApple OSS Distributions #endif
33*1b191cb5SApple OSS Distributions }
34