xref: /xnu-12377.81.4/tests/os_thread_self_restrict_pagers.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #include <darwintest.h>
2*043036a2SApple OSS Distributions #include <darwintest_perf.h>
3*043036a2SApple OSS Distributions 
4*043036a2SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.vm"));
5*043036a2SApple OSS Distributions 
6*043036a2SApple OSS Distributions #include <machine/cpu_capabilities.h>
7*043036a2SApple OSS Distributions #include <sys/mman.h>
8*043036a2SApple OSS Distributions #include <errno.h>
9*043036a2SApple OSS Distributions #include <fcntl.h>
10*043036a2SApple OSS Distributions #include <stdint.h>
11*043036a2SApple OSS Distributions #include <libkern/OSCacheControl.h>
12*043036a2SApple OSS Distributions #include <unistd.h>
13*043036a2SApple OSS Distributions #include <signal.h>
14*043036a2SApple OSS Distributions #include <stdlib.h>
15*043036a2SApple OSS Distributions #include <sys/sysctl.h>
16*043036a2SApple OSS Distributions 
17*043036a2SApple OSS Distributions #include <mach/vm_param.h>
18*043036a2SApple OSS Distributions #include <pthread.h>
19*043036a2SApple OSS Distributions 
20*043036a2SApple OSS Distributions #include <os/thread_self_restrict.h>
21*043036a2SApple OSS Distributions 
22*043036a2SApple OSS Distributions #include <mach/mach.h>
23*043036a2SApple OSS Distributions #include <mach/mach_error.h>
24*043036a2SApple OSS Distributions #include <mach/mach_init.h>
25*043036a2SApple OSS Distributions #include <mach/mach_port.h>
26*043036a2SApple OSS Distributions #include <mach/mach_vm.h>
27*043036a2SApple OSS Distributions #include <mach/vm_map.h>
28*043036a2SApple OSS Distributions #include <mach/task.h>
29*043036a2SApple OSS Distributions 
30*043036a2SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions #if defined(__arm64__)
33*043036a2SApple OSS Distributions /* PAGE_SIZE on ARM64 is an expression derived from a non-const global variable */
34*043036a2SApple OSS Distributions #define PAD_SIZE        PAGE_MAX_SIZE
35*043036a2SApple OSS Distributions #else
36*043036a2SApple OSS Distributions #define PAD_SIZE        PAGE_MIN_SIZE
37*043036a2SApple OSS Distributions #endif
38*043036a2SApple OSS Distributions 
39*043036a2SApple OSS Distributions /* Enumerations */
40*043036a2SApple OSS Distributions typedef enum _access_type {
41*043036a2SApple OSS Distributions 	ACCESS_READ,
42*043036a2SApple OSS Distributions 	ACCESS_WRITE,
43*043036a2SApple OSS Distributions } access_type_t;
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions typedef enum _fault_strategy {
46*043036a2SApple OSS Distributions 	FAULT_STRAT_NONE,
47*043036a2SApple OSS Distributions 	FAULT_STRAT_RW_TPRO,
48*043036a2SApple OSS Distributions } fault_strategy_t;
49*043036a2SApple OSS Distributions 
50*043036a2SApple OSS Distributions /* Structures */
51*043036a2SApple OSS Distributions typedef struct {
52*043036a2SApple OSS Distributions 	uint64_t fault_count;
53*043036a2SApple OSS Distributions 	fault_strategy_t fault_strategy;
54*043036a2SApple OSS Distributions 	bool fault_expected;
55*043036a2SApple OSS Distributions } fault_state_t;
56*043036a2SApple OSS Distributions 
57*043036a2SApple OSS Distributions /* Globals */
58*043036a2SApple OSS Distributions static bool key_created = false;
59*043036a2SApple OSS Distributions static pthread_key_t fault_state_key;
60*043036a2SApple OSS Distributions 
61*043036a2SApple OSS Distributions /*
62*043036a2SApple OSS Distributions  * The pager will only map entries with TPRO if we need to perform fixups.
63*043036a2SApple OSS Distributions  * Otherwise it really is const. Ensure we forge a struct that will require
64*043036a2SApple OSS Distributions  * dynamic rebasing.
65*043036a2SApple OSS Distributions  */
66*043036a2SApple OSS Distributions typedef struct {
67*043036a2SApple OSS Distributions 	void *reloc;
68*043036a2SApple OSS Distributions 	uint32_t magic;
69*043036a2SApple OSS Distributions 	char bytes[PAD_SIZE - 12];
70*043036a2SApple OSS Distributions } const_page_t;
71*043036a2SApple OSS Distributions 
72*043036a2SApple OSS Distributions typedef struct {
73*043036a2SApple OSS Distributions 	const_page_t one;
74*043036a2SApple OSS Distributions 	const_page_t two;
75*043036a2SApple OSS Distributions 	char ro[PAD_SIZE];
76*043036a2SApple OSS Distributions } const_state_t;
77*043036a2SApple OSS Distributions 
78*043036a2SApple OSS Distributions #define MAGIC(state) (void *)&state->magic
79*043036a2SApple OSS Distributions 
80*043036a2SApple OSS Distributions /*
81*043036a2SApple OSS Distributions  * Force known data into our __DATA_CONST segment. The pager will be responsible
82*043036a2SApple OSS Distributions  * for handling the mapping of this.
83*043036a2SApple OSS Distributions  */
84*043036a2SApple OSS Distributions __attribute__((section("__DATA_CONST,__pager")))
85*043036a2SApple OSS Distributions __attribute__((aligned(PAD_SIZE)))
86*043036a2SApple OSS Distributions static const_state_t pager_state = {
87*043036a2SApple OSS Distributions 	.one.reloc = &pager_state,
88*043036a2SApple OSS Distributions 	.two.reloc = &pager_state,
89*043036a2SApple OSS Distributions 	.one.magic = 0x41414141,
90*043036a2SApple OSS Distributions 	.two.magic = 0x41414141,
91*043036a2SApple OSS Distributions 	.ro = "CCCC"
92*043036a2SApple OSS Distributions };
93*043036a2SApple OSS Distributions 
94*043036a2SApple OSS Distributions /* Allocate a fault_state_t, and associate it with the current thread. */
95*043036a2SApple OSS Distributions static fault_state_t *
fault_state_create(void)96*043036a2SApple OSS Distributions fault_state_create(void)
97*043036a2SApple OSS Distributions {
98*043036a2SApple OSS Distributions 	fault_state_t * fault_state = malloc(sizeof(fault_state_t));
99*043036a2SApple OSS Distributions 
100*043036a2SApple OSS Distributions 	if (fault_state) {
101*043036a2SApple OSS Distributions 		fault_state->fault_count = 0;
102*043036a2SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_NONE;
103*043036a2SApple OSS Distributions 		fault_state->fault_expected = false;
104*043036a2SApple OSS Distributions 
105*043036a2SApple OSS Distributions 		if (pthread_setspecific(fault_state_key, fault_state)) {
106*043036a2SApple OSS Distributions 			free(fault_state);
107*043036a2SApple OSS Distributions 			fault_state = NULL;
108*043036a2SApple OSS Distributions 		}
109*043036a2SApple OSS Distributions 	}
110*043036a2SApple OSS Distributions 
111*043036a2SApple OSS Distributions 	return fault_state;
112*043036a2SApple OSS Distributions }
113*043036a2SApple OSS Distributions 
114*043036a2SApple OSS Distributions /* Disassociate the given fault state from the current thread, and destroy it. */
115*043036a2SApple OSS Distributions static void
fault_state_destroy(void * fault_state)116*043036a2SApple OSS Distributions fault_state_destroy(void * fault_state)
117*043036a2SApple OSS Distributions {
118*043036a2SApple OSS Distributions 	if (fault_state == NULL) {
119*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Attempted to fault_state_destroy NULL");
120*043036a2SApple OSS Distributions 	}
121*043036a2SApple OSS Distributions 
122*043036a2SApple OSS Distributions 	free(fault_state);
123*043036a2SApple OSS Distributions }
124*043036a2SApple OSS Distributions 
125*043036a2SApple OSS Distributions /*
126*043036a2SApple OSS Distributions  * A signal handler that attempts to resolve anticipated faults through use of
127*043036a2SApple OSS Distributions  * the os_thread_self_restrict_rwx functions.
128*043036a2SApple OSS Distributions  */
129*043036a2SApple OSS Distributions static void
access_failed_handler(int signum)130*043036a2SApple OSS Distributions access_failed_handler(int signum)
131*043036a2SApple OSS Distributions {
132*043036a2SApple OSS Distributions 	fault_state_t * fault_state;
133*043036a2SApple OSS Distributions 
134*043036a2SApple OSS Distributions 	/* This handler should ONLY handle SIGBUS. */
135*043036a2SApple OSS Distributions 	if (signum != SIGBUS) {
136*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Unexpected signal sent to handler");
137*043036a2SApple OSS Distributions 	}
138*043036a2SApple OSS Distributions 
139*043036a2SApple OSS Distributions 	if (!(fault_state = pthread_getspecific(fault_state_key))) {
140*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Failed to retrieve fault state");
141*043036a2SApple OSS Distributions 	}
142*043036a2SApple OSS Distributions 
143*043036a2SApple OSS Distributions 	if (!(fault_state->fault_expected)) {
144*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Unexpected fault taken");
145*043036a2SApple OSS Distributions 	}
146*043036a2SApple OSS Distributions 
147*043036a2SApple OSS Distributions 	/* We should not see a second fault. */
148*043036a2SApple OSS Distributions 	fault_state->fault_expected = false;
149*043036a2SApple OSS Distributions 
150*043036a2SApple OSS Distributions 	switch (fault_state->fault_strategy) {
151*043036a2SApple OSS Distributions 	case FAULT_STRAT_NONE:
152*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("No fault strategy");
153*043036a2SApple OSS Distributions 
154*043036a2SApple OSS Distributions 		/* Just in case we try to do something different. */
155*043036a2SApple OSS Distributions 		break;
156*043036a2SApple OSS Distributions 	case FAULT_STRAT_RW_TPRO:
157*043036a2SApple OSS Distributions 		os_thread_self_restrict_tpro_to_rw();
158*043036a2SApple OSS Distributions 		break;
159*043036a2SApple OSS Distributions 	}
160*043036a2SApple OSS Distributions 
161*043036a2SApple OSS Distributions 	fault_state->fault_count++;
162*043036a2SApple OSS Distributions }
163*043036a2SApple OSS Distributions 
164*043036a2SApple OSS Distributions /*
165*043036a2SApple OSS Distributions  * Attempt the specified access; if the access faults, this will return true;
166*043036a2SApple OSS Distributions  * otherwise, it will return false.
167*043036a2SApple OSS Distributions  */
168*043036a2SApple OSS Distributions static bool
does_access_fault(access_type_t access_type,void * addr,uint32_t value)169*043036a2SApple OSS Distributions does_access_fault(access_type_t access_type, void * addr, uint32_t value)
170*043036a2SApple OSS Distributions {
171*043036a2SApple OSS Distributions 	uint64_t old_fault_count;
172*043036a2SApple OSS Distributions 	uint64_t new_fault_count;
173*043036a2SApple OSS Distributions 
174*043036a2SApple OSS Distributions 	fault_state_t * fault_state;
175*043036a2SApple OSS Distributions 
176*043036a2SApple OSS Distributions 	struct sigaction old_action; /* Save area for any existing action. */
177*043036a2SApple OSS Distributions 	struct sigaction new_action; /* The action we wish to install for SIGBUS. */
178*043036a2SApple OSS Distributions 
179*043036a2SApple OSS Distributions 	bool retval = false;
180*043036a2SApple OSS Distributions 
181*043036a2SApple OSS Distributions 	new_action.sa_handler = access_failed_handler; /* A handler for write failures. */
182*043036a2SApple OSS Distributions 	new_action.sa_mask    = 0;                     /* Don't modify the mask. */
183*043036a2SApple OSS Distributions 	new_action.sa_flags   = 0;                     /* Flags?  Who needs those? */
184*043036a2SApple OSS Distributions 
185*043036a2SApple OSS Distributions 	if (addr == NULL) {
186*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Access attempted against NULL");
187*043036a2SApple OSS Distributions 	}
188*043036a2SApple OSS Distributions 
189*043036a2SApple OSS Distributions 	if (!(fault_state = pthread_getspecific(fault_state_key))) {
190*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Failed to retrieve fault state");
191*043036a2SApple OSS Distributions 	}
192*043036a2SApple OSS Distributions 
193*043036a2SApple OSS Distributions 	old_fault_count = fault_state->fault_count;
194*043036a2SApple OSS Distributions 
195*043036a2SApple OSS Distributions 	/* Install a handler so that we can catch SIGBUS. */
196*043036a2SApple OSS Distributions 	sigaction(SIGBUS, &new_action, &old_action);
197*043036a2SApple OSS Distributions 
198*043036a2SApple OSS Distributions 	/* Perform the requested operation. */
199*043036a2SApple OSS Distributions 	switch (access_type) {
200*043036a2SApple OSS Distributions 	case ACCESS_READ:
201*043036a2SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_RW_TPRO;
202*043036a2SApple OSS Distributions 		fault_state->fault_expected = true;
203*043036a2SApple OSS Distributions 
204*043036a2SApple OSS Distributions 		__sync_synchronize();
205*043036a2SApple OSS Distributions 
206*043036a2SApple OSS Distributions #if defined(__arm64__)
207*043036a2SApple OSS Distributions 		uint8_t a = *((volatile uint8_t *)addr);
208*043036a2SApple OSS Distributions #endif
209*043036a2SApple OSS Distributions 		__sync_synchronize();
210*043036a2SApple OSS Distributions 
211*043036a2SApple OSS Distributions 		fault_state->fault_expected = false;
212*043036a2SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_NONE;
213*043036a2SApple OSS Distributions 
214*043036a2SApple OSS Distributions 		break;
215*043036a2SApple OSS Distributions 
216*043036a2SApple OSS Distributions 	case ACCESS_WRITE:
217*043036a2SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_RW_TPRO;
218*043036a2SApple OSS Distributions 		fault_state->fault_expected = true;
219*043036a2SApple OSS Distributions 
220*043036a2SApple OSS Distributions 		__sync_synchronize();
221*043036a2SApple OSS Distributions 
222*043036a2SApple OSS Distributions 		*((volatile uint32_t *)addr) = value;
223*043036a2SApple OSS Distributions 
224*043036a2SApple OSS Distributions 		__sync_synchronize();
225*043036a2SApple OSS Distributions 
226*043036a2SApple OSS Distributions 		fault_state->fault_expected = false;
227*043036a2SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_NONE;
228*043036a2SApple OSS Distributions 
229*043036a2SApple OSS Distributions 		break;
230*043036a2SApple OSS Distributions 	}
231*043036a2SApple OSS Distributions 
232*043036a2SApple OSS Distributions 	/* Restore the old SIGBUS handler. */
233*043036a2SApple OSS Distributions 	sigaction(SIGBUS, &old_action, NULL);
234*043036a2SApple OSS Distributions 
235*043036a2SApple OSS Distributions 	new_fault_count = fault_state->fault_count;
236*043036a2SApple OSS Distributions 
237*043036a2SApple OSS Distributions 	if (new_fault_count > old_fault_count) {
238*043036a2SApple OSS Distributions 		/* Indicate that we took a fault. */
239*043036a2SApple OSS Distributions 		retval = true;
240*043036a2SApple OSS Distributions 	}
241*043036a2SApple OSS Distributions 
242*043036a2SApple OSS Distributions 	return retval;
243*043036a2SApple OSS Distributions }
244*043036a2SApple OSS Distributions 
245*043036a2SApple OSS Distributions static bool
does_read_fault(void * addr)246*043036a2SApple OSS Distributions does_read_fault(void * addr)
247*043036a2SApple OSS Distributions {
248*043036a2SApple OSS Distributions 	return does_access_fault(ACCESS_READ, addr, 0);
249*043036a2SApple OSS Distributions }
250*043036a2SApple OSS Distributions 
251*043036a2SApple OSS Distributions static bool
does_write_fault(void * addr,uint32_t value)252*043036a2SApple OSS Distributions does_write_fault(void * addr, uint32_t value)
253*043036a2SApple OSS Distributions {
254*043036a2SApple OSS Distributions 	return does_access_fault(ACCESS_WRITE, addr, value);
255*043036a2SApple OSS Distributions }
256*043036a2SApple OSS Distributions 
257*043036a2SApple OSS Distributions static bool
has_pager_support(void)258*043036a2SApple OSS Distributions has_pager_support(void)
259*043036a2SApple OSS Distributions {
260*043036a2SApple OSS Distributions 	uint32_t enabled = false;
261*043036a2SApple OSS Distributions 	size_t output_size = sizeof(enabled);
262*043036a2SApple OSS Distributions 
263*043036a2SApple OSS Distributions 	(void)sysctlbyname("vm.pmap_tpro_pagers",
264*043036a2SApple OSS Distributions 	    &enabled, &output_size, NULL, 0);
265*043036a2SApple OSS Distributions 	return enabled;
266*043036a2SApple OSS Distributions }
267*043036a2SApple OSS Distributions 
268*043036a2SApple OSS Distributions static void
cleanup(void)269*043036a2SApple OSS Distributions cleanup(void)
270*043036a2SApple OSS Distributions {
271*043036a2SApple OSS Distributions 	fault_state_t * fault_state;
272*043036a2SApple OSS Distributions 
273*043036a2SApple OSS Distributions 	if (!(fault_state = pthread_getspecific(fault_state_key))) {
274*043036a2SApple OSS Distributions 		T_ASSERT_FAIL("Failed to retrieve fault state");
275*043036a2SApple OSS Distributions 
276*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_setspecific(fault_state_key, NULL), "Remove fault_state");
277*043036a2SApple OSS Distributions 		fault_state_destroy(fault_state);
278*043036a2SApple OSS Distributions 	}
279*043036a2SApple OSS Distributions 
280*043036a2SApple OSS Distributions 	if (key_created) {
281*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_key_delete(fault_state_key), "Delete fault state key");
282*043036a2SApple OSS Distributions 	}
283*043036a2SApple OSS Distributions 
284*043036a2SApple OSS Distributions 	return;
285*043036a2SApple OSS Distributions }
286*043036a2SApple OSS Distributions 
287*043036a2SApple OSS Distributions static void
288*043036a2SApple OSS Distributions thread_self_restrict_test(void (^test)(void))
289*043036a2SApple OSS Distributions {
290*043036a2SApple OSS Distributions 	int err = 0;
291*043036a2SApple OSS Distributions 
292*043036a2SApple OSS Distributions 	T_SETUPBEGIN;
293*043036a2SApple OSS Distributions 	T_ATEND(cleanup);
294*043036a2SApple OSS Distributions 
295*043036a2SApple OSS Distributions 	/* Set up the necessary state for the test. */
296*043036a2SApple OSS Distributions 	err = pthread_key_create(&fault_state_key, fault_state_destroy);
297*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(err, 0, "Create pthread key");
298*043036a2SApple OSS Distributions 	key_created = true;
299*043036a2SApple OSS Distributions 
300*043036a2SApple OSS Distributions 	T_ASSERT_NOTNULL(fault_state_create(), "Create fault state");
301*043036a2SApple OSS Distributions 	T_SETUPEND;
302*043036a2SApple OSS Distributions 
303*043036a2SApple OSS Distributions 	test();
304*043036a2SApple OSS Distributions }
305*043036a2SApple OSS Distributions 
306*043036a2SApple OSS Distributions static void
fork_child_test(const_page_t * state)307*043036a2SApple OSS Distributions fork_child_test(const_page_t *state)
308*043036a2SApple OSS Distributions {
309*043036a2SApple OSS Distributions 	pid_t pid;
310*043036a2SApple OSS Distributions 	int statloc;
311*043036a2SApple OSS Distributions 
312*043036a2SApple OSS Distributions 	pid = fork();
313*043036a2SApple OSS Distributions 	if (pid == 0) {
314*043036a2SApple OSS Distributions 		T_EXPECT_EQ(state->magic, 0x45454545, "Expected magic on fork");
315*043036a2SApple OSS Distributions 
316*043036a2SApple OSS Distributions 		os_thread_self_restrict_tpro_to_rw();
317*043036a2SApple OSS Distributions 		T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write in child");
318*043036a2SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault((void *)&state->bytes, 0x47474747), 0, "write to pager backed memory in child (no fault)");
319*043036a2SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x46464646), 0, "write to pager backed memory in child (no fault)");
320*043036a2SApple OSS Distributions 		exit(0);
321*043036a2SApple OSS Distributions 	}
322*043036a2SApple OSS Distributions 
323*043036a2SApple OSS Distributions 	if (pid < 0) {
324*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(pid, "fork");
325*043036a2SApple OSS Distributions 	}
326*043036a2SApple OSS Distributions 
327*043036a2SApple OSS Distributions 	waitpid(pid, &statloc, 0);
328*043036a2SApple OSS Distributions }
329*043036a2SApple OSS Distributions 
330*043036a2SApple OSS Distributions static void
pager_test(const_page_t * state)331*043036a2SApple OSS Distributions pager_test(const_page_t *state)
332*043036a2SApple OSS Distributions {
333*043036a2SApple OSS Distributions 	kern_return_t kr;
334*043036a2SApple OSS Distributions 	uint32_t pre;
335*043036a2SApple OSS Distributions 	vm_prot_t curprot, maxprot;
336*043036a2SApple OSS Distributions 	mach_vm_address_t addr = 0;
337*043036a2SApple OSS Distributions 	const_page_t *copy_state = NULL;
338*043036a2SApple OSS Distributions 	mach_port_t cow_port = MACH_PORT_NULL;
339*043036a2SApple OSS Distributions 	memory_object_size_t me_size = PAGE_SIZE;
340*043036a2SApple OSS Distributions 
341*043036a2SApple OSS Distributions 	/*
342*043036a2SApple OSS Distributions 	 * Validate our initial status quo. TPRO permissions should be RO,
343*043036a2SApple OSS Distributions 	 * so we should be able to read from our pager backed mapping but
344*043036a2SApple OSS Distributions 	 * should fault when trying to write to it.
345*043036a2SApple OSS Distributions 	 */
346*043036a2SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only");
347*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_read_fault(MAGIC(state)), 0, "read from pager backed memory");
348*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x43434343), 1, "write to pager backed memory (detect fault)");
349*043036a2SApple OSS Distributions 
350*043036a2SApple OSS Distributions 	/*
351*043036a2SApple OSS Distributions 	 * Toggle permissions to RW and attempt a write. We should succeed.
352*043036a2SApple OSS Distributions 	 */
353*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
354*043036a2SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write");
355*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x44444444), 0, "write to pager backed memory (no fault)");
356*043036a2SApple OSS Distributions 
357*043036a2SApple OSS Distributions 	/*
358*043036a2SApple OSS Distributions 	 * Toggle permissions to RO and attempt a write. We should detect
359*043036a2SApple OSS Distributions 	 * the fault
360*043036a2SApple OSS Distributions 	 */
361*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
362*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x45454545), 1, "write to pager backed memory (detect fault)");
363*043036a2SApple OSS Distributions 
364*043036a2SApple OSS Distributions 	/*
365*043036a2SApple OSS Distributions 	 * Fork a child process and ensure that writes into the pager backed
366*043036a2SApple OSS Distributions 	 * regions are not observed by the parent. They should now be COW.
367*043036a2SApple OSS Distributions 	 */
368*043036a2SApple OSS Distributions 	pre = state->magic;
369*043036a2SApple OSS Distributions 	fork_child_test(state);
370*043036a2SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write from child should not be observed");
371*043036a2SApple OSS Distributions 
372*043036a2SApple OSS Distributions 	/*
373*043036a2SApple OSS Distributions 	 * Ensure that if we remap the target region in a shared manner that we
374*043036a2SApple OSS Distributions 	 * inherit TPRO. Remapping should be successful but we still rely on
375*043036a2SApple OSS Distributions 	 * TPRO permissions to toggle r--/rw-
376*043036a2SApple OSS Distributions 	 */
377*043036a2SApple OSS Distributions 	kr = mach_vm_remap(mach_task_self(),
378*043036a2SApple OSS Distributions 	    &addr,
379*043036a2SApple OSS Distributions 	    PAGE_SIZE,
380*043036a2SApple OSS Distributions 	    0,                /* mask */
381*043036a2SApple OSS Distributions 	    VM_FLAGS_ANYWHERE,
382*043036a2SApple OSS Distributions 	    mach_task_self(),
383*043036a2SApple OSS Distributions 	    (mach_vm_address_t)state,
384*043036a2SApple OSS Distributions 	    FALSE,                /* copy */
385*043036a2SApple OSS Distributions 	    &curprot,
386*043036a2SApple OSS Distributions 	    &maxprot,
387*043036a2SApple OSS Distributions 	    VM_INHERIT_DEFAULT);
388*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_remap(SHARED)");
389*043036a2SApple OSS Distributions 	copy_state = (const_page_t *)addr;
390*043036a2SApple OSS Distributions 
391*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
392*043036a2SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO configured as read-only");
393*043036a2SApple OSS Distributions 	T_EXPECT_EQ(curprot, VM_PROT_READ, "TPRO region should be VM_PROT_READ");
394*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 1, "write to remapped region (detect fault)");
395*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
396*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 0, "write to remapped region (no fault)");
397*043036a2SApple OSS Distributions 	T_EXPECT_EQ(0x46464646, state->magic, "write into copied region should be observed");
398*043036a2SApple OSS Distributions 
399*043036a2SApple OSS Distributions 	/*
400*043036a2SApple OSS Distributions 	 * Ensure that if we remap the region that we do not observe writes to
401*043036a2SApple OSS Distributions 	 * the new copy in __DATA_CONST itself.
402*043036a2SApple OSS Distributions 	 */
403*043036a2SApple OSS Distributions 	kr = mach_vm_remap(mach_task_self(),
404*043036a2SApple OSS Distributions 	    (mach_vm_address_t *)&copy_state,
405*043036a2SApple OSS Distributions 	    PAGE_SIZE,
406*043036a2SApple OSS Distributions 	    0,                /* mask */
407*043036a2SApple OSS Distributions 	    VM_FLAGS_ANYWHERE,
408*043036a2SApple OSS Distributions 	    mach_task_self(),
409*043036a2SApple OSS Distributions 	    (mach_vm_address_t)state,
410*043036a2SApple OSS Distributions 	    TRUE,                /* copy */
411*043036a2SApple OSS Distributions 	    &curprot,
412*043036a2SApple OSS Distributions 	    &maxprot,
413*043036a2SApple OSS Distributions 	    VM_INHERIT_DEFAULT);
414*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_remap(COPY)");
415*043036a2SApple OSS Distributions 
416*043036a2SApple OSS Distributions 	/*
417*043036a2SApple OSS Distributions 	 * Toggle TPRO RW and write to the new copied region
418*043036a2SApple OSS Distributions 	 */
419*043036a2SApple OSS Distributions 	pre = state->magic;
420*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
421*043036a2SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write");
422*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 0, "write to pager backed memory (no fault)");
423*043036a2SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed");
424*043036a2SApple OSS Distributions 
425*043036a2SApple OSS Distributions 	/*
426*043036a2SApple OSS Distributions 	 * Make a memory entry for our target region and attempt to map it in
427*043036a2SApple OSS Distributions 	 * in a shared fashion. We should succeed but it should transparently
428*043036a2SApple OSS Distributions 	 * copy the target VM object as extracting TPRO VM entries will fail.
429*043036a2SApple OSS Distributions 	 * Writes to the new region should therefore not be observed.
430*043036a2SApple OSS Distributions 	 */
431*043036a2SApple OSS Distributions 	me_size = PAGE_SIZE;
432*043036a2SApple OSS Distributions 	kr = mach_make_memory_entry_64(mach_task_self(),
433*043036a2SApple OSS Distributions 	    &me_size,
434*043036a2SApple OSS Distributions 	    (mach_vm_address_t)state,
435*043036a2SApple OSS Distributions 	    MAP_MEM_VM_SHARE | VM_PROT_READ | VM_PROT_WRITE,
436*043036a2SApple OSS Distributions 	    &cow_port,
437*043036a2SApple OSS Distributions 	    MACH_PORT_NULL);
438*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_make_memory_entry_64(MAP_MEM_VM_SHARE)");
439*043036a2SApple OSS Distributions 
440*043036a2SApple OSS Distributions 	pre = state->magic;
441*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x48484849), 0, "write to mapped copy region (no fault)");
442*043036a2SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed");
443*043036a2SApple OSS Distributions 
444*043036a2SApple OSS Distributions 	copy_state = NULL;
445*043036a2SApple OSS Distributions 	kr = mach_vm_map(mach_task_self(),
446*043036a2SApple OSS Distributions 	    (mach_vm_address_t *)&copy_state,
447*043036a2SApple OSS Distributions 	    PAGE_SIZE,
448*043036a2SApple OSS Distributions 	    0,              /* mask */
449*043036a2SApple OSS Distributions 	    VM_FLAGS_ANYWHERE,
450*043036a2SApple OSS Distributions 	    cow_port,
451*043036a2SApple OSS Distributions 	    0,              /* offset */
452*043036a2SApple OSS Distributions 	    TRUE,           /* copy */
453*043036a2SApple OSS Distributions 	    VM_PROT_READ | VM_PROT_WRITE,
454*043036a2SApple OSS Distributions 	    VM_PROT_READ | VM_PROT_WRITE,
455*043036a2SApple OSS Distributions 	    VM_INHERIT_DEFAULT);
456*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_map(cow_port)");
457*043036a2SApple OSS Distributions 
458*043036a2SApple OSS Distributions 	/*
459*043036a2SApple OSS Distributions 	 * Pages of the copy will no longer be mapped in as TPRO. Both
460*043036a2SApple OSS Distributions 	 * read/writes should work even with TPRO toggled RO.
461*043036a2SApple OSS Distributions 	 */
462*043036a2SApple OSS Distributions 	pre = state->magic;
463*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
464*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x48484848), 0, "write to mapped copy region (no fault)");
465*043036a2SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed");
466*043036a2SApple OSS Distributions 
467*043036a2SApple OSS Distributions 	/*
468*043036a2SApple OSS Distributions 	 * We've explored a number of ways to perform copies on the target
469*043036a2SApple OSS Distributions 	 * objects in __DATA_CONST. Our first target page (&pager_state.one)
470*043036a2SApple OSS Distributions 	 * should now be marked RO without TPRO permissions to handle any
471*043036a2SApple OSS Distributions 	 * incoming write faults. Write to it directly again to ensure we
472*043036a2SApple OSS Distributions 	 * fault back in with TPRO permissions.
473*043036a2SApple OSS Distributions 	 */
474*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
475*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x49494949), 1, "write to pager backed memory (detect fault)");
476*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
477*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x4a4a4a4a), 0, "write to pager backed memory (no fault)");
478*043036a2SApple OSS Distributions 
479*043036a2SApple OSS Distributions 	/*
480*043036a2SApple OSS Distributions 	 * Now we attempt to have the page paged out. On systems which support the
481*043036a2SApple OSS Distributions 	 * compressor, we'll get paged out/compressed. On fault we should
482*043036a2SApple OSS Distributions 	 * be pmapped back in with TPRO permissions.
483*043036a2SApple OSS Distributions 	 */
484*043036a2SApple OSS Distributions 	mach_vm_behavior_set(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE, VM_BEHAVIOR_PAGEOUT);
485*043036a2SApple OSS Distributions 
486*043036a2SApple OSS Distributions 	/*
487*043036a2SApple OSS Distributions 	 * Can verify in debugger at this point that page(s) have been
488*043036a2SApple OSS Distributions 	 * paged out. If compressor pager is available the page should
489*043036a2SApple OSS Distributions 	 * not be resident and compressor pager should be tied to the
490*043036a2SApple OSS Distributions 	 * top level VM object.
491*043036a2SApple OSS Distributions 	 */
492*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
493*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x49494949), 1, "write to pager backed memory after pageout (detect fault)");
494*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
495*043036a2SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x4a4a4a4a), 0, "write to pager backed memory after pageout (no fault)");
496*043036a2SApple OSS Distributions 
497*043036a2SApple OSS Distributions 	/*
498*043036a2SApple OSS Distributions 	 * Try and reprotect the region. We should fail
499*043036a2SApple OSS Distributions 	 */
500*043036a2SApple OSS Distributions 	kr = vm_protect(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE, FALSE, VM_PROT_DEFAULT);
501*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_protect(RW) should fail");
502*043036a2SApple OSS Distributions 
503*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
504*043036a2SApple OSS Distributions }
505*043036a2SApple OSS Distributions 
506*043036a2SApple OSS Distributions static void
mmap_test(const_page_t * state)507*043036a2SApple OSS Distributions mmap_test(const_page_t *state)
508*043036a2SApple OSS Distributions {
509*043036a2SApple OSS Distributions 	void *mapping;
510*043036a2SApple OSS Distributions 
511*043036a2SApple OSS Distributions 	/*
512*043036a2SApple OSS Distributions 	 * Validate our initial status quo. TPRO permissions should be RO,
513*043036a2SApple OSS Distributions 	 * so we should be able to read from our pager backed mapping but
514*043036a2SApple OSS Distributions 	 * should fault when trying to write to it.
515*043036a2SApple OSS Distributions 	 */
516*043036a2SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only");
517*043036a2SApple OSS Distributions 
518*043036a2SApple OSS Distributions 	/*
519*043036a2SApple OSS Distributions 	 * Attempt to mmap a fixed allocation over our TPRO region.
520*043036a2SApple OSS Distributions 	 * TPRO region should be permanent and should disallow being
521*043036a2SApple OSS Distributions 	 * overwritten.
522*043036a2SApple OSS Distributions 	 */
523*043036a2SApple OSS Distributions 	mapping = mmap(state, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
524*043036a2SApple OSS Distributions 	T_ASSERT_EQ(mapping, MAP_FAILED, "Map over TPRO range should fail");
525*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
526*043036a2SApple OSS Distributions }
527*043036a2SApple OSS Distributions 
528*043036a2SApple OSS Distributions static void
vm_allocate_test(const_page_t * state)529*043036a2SApple OSS Distributions vm_allocate_test(const_page_t *state)
530*043036a2SApple OSS Distributions {
531*043036a2SApple OSS Distributions 	kern_return_t kr;
532*043036a2SApple OSS Distributions 	mach_vm_address_t addr = (mach_vm_address_t)state;
533*043036a2SApple OSS Distributions 	vm_region_basic_info_data_64_t info;
534*043036a2SApple OSS Distributions 	mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
535*043036a2SApple OSS Distributions 	mach_vm_size_t size = PAGE_SIZE;
536*043036a2SApple OSS Distributions 	mach_port_t unused = MACH_PORT_NULL;
537*043036a2SApple OSS Distributions 
538*043036a2SApple OSS Distributions 	/*
539*043036a2SApple OSS Distributions 	 * Validate our initial status quo. TPRO permissions should be RO,
540*043036a2SApple OSS Distributions 	 * so we should be able to read from our pager backed mapping but
541*043036a2SApple OSS Distributions 	 * should fault when trying to write to it.
542*043036a2SApple OSS Distributions 	 */
543*043036a2SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only");
544*043036a2SApple OSS Distributions 
545*043036a2SApple OSS Distributions 	/*
546*043036a2SApple OSS Distributions 	 * Deallocate the TPRO region. This should succeed but leave the region
547*043036a2SApple OSS Distributions 	 * intact with no permissions. Further allocations should not be able to
548*043036a2SApple OSS Distributions 	 * obtain the same address.
549*043036a2SApple OSS Distributions 	 */
550*043036a2SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE);
551*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(kr, KERN_SUCCESS, "vm_deallocate should succeed");
552*043036a2SApple OSS Distributions 
553*043036a2SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), (mach_vm_address_t *)&addr, PAGE_SIZE, VM_FLAGS_FIXED);
554*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(kr, KERN_NO_SPACE, "vm_allocate should fail with KERN_NO_SPACE");
555*043036a2SApple OSS Distributions 
556*043036a2SApple OSS Distributions 	/*
557*043036a2SApple OSS Distributions 	 * Lookup the target region and confirm that all permissions have been
558*043036a2SApple OSS Distributions 	 * removed.
559*043036a2SApple OSS Distributions 	 */
560*043036a2SApple OSS Distributions 	kr = mach_vm_region(mach_task_self(), &addr, &size, VM_REGION_BASIC_INFO_64, (vm_region_info_t)&info, &count, &unused);
561*043036a2SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_ERROR(kr, KERN_SUCCESS, "mach_vm_region should succeed");
562*043036a2SApple OSS Distributions 
563*043036a2SApple OSS Distributions 	T_ASSERT_EQ(info.protection, VM_PROT_NONE, "Entry should have no permissions");
564*043036a2SApple OSS Distributions 	T_ASSERT_EQ(info.max_protection, VM_PROT_NONE, "Entry should have no permissions");
565*043036a2SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
566*043036a2SApple OSS Distributions }
567*043036a2SApple OSS Distributions 
568*043036a2SApple OSS Distributions T_DECL(thread_self_restrict_pagers,
569*043036a2SApple OSS Distributions     "Verify that the TPRO pager interfaces work correctly", T_META_TAG_VM_PREFERRED)
570*043036a2SApple OSS Distributions {
571*043036a2SApple OSS Distributions #if __arm64__
572*043036a2SApple OSS Distributions 	/* Check to see that we support the necessary hardware features. */
573*043036a2SApple OSS Distributions 	if (!os_thread_self_restrict_tpro_is_supported() || !has_pager_support()) {
574*043036a2SApple OSS Distributions 		T_SKIP("no hardware TPRO support enabled on this system");
575*043036a2SApple OSS Distributions 	}
576*043036a2SApple OSS Distributions 
577*043036a2SApple OSS Distributions 	thread_self_restrict_test(^{
578*043036a2SApple OSS Distributions 		pager_test(&pager_state.one);
579*043036a2SApple OSS Distributions 
580*043036a2SApple OSS Distributions 		/*
581*043036a2SApple OSS Distributions 		 * Ensure that touching the second pager supported page exhibits
582*043036a2SApple OSS Distributions 		 * identical behaviour in order to validate the transitions between
583*043036a2SApple OSS Distributions 		 * VM entry & copy object chains.
584*043036a2SApple OSS Distributions 		 */
585*043036a2SApple OSS Distributions 		pager_test(&pager_state.two);
586*043036a2SApple OSS Distributions 
587*043036a2SApple OSS Distributions 		/*
588*043036a2SApple OSS Distributions 		 * Try and write to a normal __DATA_CONST page that isn't backed by
589*043036a2SApple OSS Distributions 		 * the dyld pager. The kernel will have mapped this directly but
590*043036a2SApple OSS Distributions 		 * should still maintain TPRO protection.
591*043036a2SApple OSS Distributions 		 */
592*043036a2SApple OSS Distributions 		os_thread_self_restrict_tpro_to_ro();
593*043036a2SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault(&pager_state.ro[0], 0x41414141), 1, "write to __DATA_CONST should succeed (no fault)");
594*043036a2SApple OSS Distributions 		os_thread_self_restrict_tpro_to_rw();
595*043036a2SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault(&pager_state.ro[0], 0x41414141), 0, "write to __DATA_CONST should fail (detect fault)");
596*043036a2SApple OSS Distributions 	});
597*043036a2SApple OSS Distributions #else
598*043036a2SApple OSS Distributions 	T_SKIP("thread_self_restrict_pagers not supported on this system");
599*043036a2SApple OSS Distributions #endif /* __arm64__ */
600*043036a2SApple OSS Distributions }
601*043036a2SApple OSS Distributions 
602*043036a2SApple OSS Distributions T_DECL(thread_self_restrict_tpro_permanent,
603*043036a2SApple OSS Distributions     "Verify that TPRO VM entries are permanent")
604*043036a2SApple OSS Distributions {
605*043036a2SApple OSS Distributions #if __arm64__
606*043036a2SApple OSS Distributions 	/* Check to see that we support the necessary hardware features. */
607*043036a2SApple OSS Distributions 	if (!os_thread_self_restrict_tpro_is_supported() || !has_pager_support()) {
608*043036a2SApple OSS Distributions 		T_SKIP("no hardware TPRO support enabled on this system");
609*043036a2SApple OSS Distributions 	}
610*043036a2SApple OSS Distributions 
611*043036a2SApple OSS Distributions 	thread_self_restrict_test(^{
612*043036a2SApple OSS Distributions 		mmap_test(&pager_state.one);
613*043036a2SApple OSS Distributions 		vm_allocate_test(&pager_state.two);
614*043036a2SApple OSS Distributions 	});
615*043036a2SApple OSS Distributions #else
616*043036a2SApple OSS Distributions 	T_SKIP("thread_self_restrict_tpro_permanent not supported on this system");
617*043036a2SApple OSS Distributions #endif /* __arm64__ */
618*043036a2SApple OSS Distributions }
619