xref: /xnu-8796.121.2/tests/restartable_ranges.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <darwintest.h>
2*c54f35caSApple OSS Distributions #include <kern/restartable.h>
3*c54f35caSApple OSS Distributions #include <mach/mach.h>
4*c54f35caSApple OSS Distributions #include <mach/task.h>
5*c54f35caSApple OSS Distributions #include <os/atomic_private.h>
6*c54f35caSApple OSS Distributions #include <pthread.h>
7*c54f35caSApple OSS Distributions #include <signal.h>
8*c54f35caSApple OSS Distributions #include <stdbool.h>
9*c54f35caSApple OSS Distributions #include <sys/mman.h>
10*c54f35caSApple OSS Distributions #include <unistd.h>
11*c54f35caSApple OSS Distributions #include <dispatch/dispatch.h>
12*c54f35caSApple OSS Distributions 
13*c54f35caSApple OSS Distributions T_GLOBAL_META(
14*c54f35caSApple OSS Distributions 	T_META_NAMESPACE("xnu"),
15*c54f35caSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
16*c54f35caSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("all"),
17*c54f35caSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
18*c54f35caSApple OSS Distributions 
19*c54f35caSApple OSS Distributions extern task_restartable_range_t ranges[2];
20*c54f35caSApple OSS Distributions static int step = 0;
21*c54f35caSApple OSS Distributions 
22*c54f35caSApple OSS Distributions extern void restartable_function(int *);
23*c54f35caSApple OSS Distributions 
24*c54f35caSApple OSS Distributions #if defined(__x86_64__)
25*c54f35caSApple OSS Distributions __asm__("    .align 4\n"
26*c54f35caSApple OSS Distributions 	"    .text\n"
27*c54f35caSApple OSS Distributions 	"    .private_extern _restartable_function\n"
28*c54f35caSApple OSS Distributions 	"_restartable_function:\n"
29*c54f35caSApple OSS Distributions 	"    incl   (%rdi)\n"
30*c54f35caSApple OSS Distributions 	"1:\n"
31*c54f35caSApple OSS Distributions 	"    pause\n"
32*c54f35caSApple OSS Distributions 	"    jmp 1b\n"
33*c54f35caSApple OSS Distributions 	"LExit_restartable_function:\n"
34*c54f35caSApple OSS Distributions 	"    ret\n");
35*c54f35caSApple OSS Distributions #elif defined(__arm64__)
36*c54f35caSApple OSS Distributions __asm__("    .align 4\n"
37*c54f35caSApple OSS Distributions 	"    .text\n"
38*c54f35caSApple OSS Distributions 	"    .private_extern _restartable_function\n"
39*c54f35caSApple OSS Distributions 	"_restartable_function:\n"
40*c54f35caSApple OSS Distributions 	"    ldr    x11, [x0]\n"
41*c54f35caSApple OSS Distributions 	"    add    x11, x11, #1\n"
42*c54f35caSApple OSS Distributions 	"    str    x11, [x0]\n"
43*c54f35caSApple OSS Distributions 	"1:\n"
44*c54f35caSApple OSS Distributions 	"    b 1b\n"
45*c54f35caSApple OSS Distributions 	"LExit_restartable_function:\n"
46*c54f35caSApple OSS Distributions 	"    ret\n");
47*c54f35caSApple OSS Distributions #else
48*c54f35caSApple OSS Distributions #define SKIP_TEST 1
49*c54f35caSApple OSS Distributions #endif
50*c54f35caSApple OSS Distributions 
51*c54f35caSApple OSS Distributions extern uint64_t __thread_selfid(void);
52*c54f35caSApple OSS Distributions extern void *fake_msgSend(void *);
53*c54f35caSApple OSS Distributions 
54*c54f35caSApple OSS Distributions #if defined(__x86_64__)
55*c54f35caSApple OSS Distributions __asm__("    .align 4\n"
56*c54f35caSApple OSS Distributions 	"    .text\n"
57*c54f35caSApple OSS Distributions 	"    .private_extern _fake_msgSend\n"
58*c54f35caSApple OSS Distributions 	"_fake_msgSend:\n"
59*c54f35caSApple OSS Distributions 	"    movq   (%rdi), %rax\n"             /* load isa */
60*c54f35caSApple OSS Distributions 	"1:\n"
61*c54f35caSApple OSS Distributions 	"    movq   16(%rax), %rcx\n"           /* load buckets */
62*c54f35caSApple OSS Distributions 	"    movq   (%rcx), %rcx\n"             /* load selector */
63*c54f35caSApple OSS Distributions 	"LRecover_fake_msgSend:\n"
64*c54f35caSApple OSS Distributions 	"    jmp    1b\n"
65*c54f35caSApple OSS Distributions 	"LExit_fake_msgSend:\n"
66*c54f35caSApple OSS Distributions 	"    ret\n");
67*c54f35caSApple OSS Distributions #elif defined(__arm64__)
68*c54f35caSApple OSS Distributions __asm__("    .align 4\n"
69*c54f35caSApple OSS Distributions 	"    .text\n"
70*c54f35caSApple OSS Distributions 	"    .private_extern _fake_msgSend\n"
71*c54f35caSApple OSS Distributions 	"_fake_msgSend:\n"
72*c54f35caSApple OSS Distributions 	"    ldr    x16, [x0]\n"                /* load isa */
73*c54f35caSApple OSS Distributions 	"1:\n"
74*c54f35caSApple OSS Distributions #if __LP64__
75*c54f35caSApple OSS Distributions 	"    ldr    x11, [x16, #16]\n"          /* load buckets */
76*c54f35caSApple OSS Distributions #else
77*c54f35caSApple OSS Distributions 	"    ldr    x11, [x16, #8]\n"           /* load buckets */
78*c54f35caSApple OSS Distributions #endif
79*c54f35caSApple OSS Distributions 	"    ldr    x17, [x11]\n"               /* load selector */
80*c54f35caSApple OSS Distributions 	"LRecover_fake_msgSend:\n"
81*c54f35caSApple OSS Distributions 	"    b      1b\n"
82*c54f35caSApple OSS Distributions 	"LExit_fake_msgSend:\n"
83*c54f35caSApple OSS Distributions 	"    ret\n");
84*c54f35caSApple OSS Distributions #else
85*c54f35caSApple OSS Distributions #define SKIP_TEST 1
86*c54f35caSApple OSS Distributions #endif
87*c54f35caSApple OSS Distributions 
88*c54f35caSApple OSS Distributions #ifndef SKIP_TEST
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions __asm__("    .align 4\n"
91*c54f35caSApple OSS Distributions 	"    .data\n"
92*c54f35caSApple OSS Distributions 	"    .private_extern _ranges\n"
93*c54f35caSApple OSS Distributions 	"_ranges:\n"
94*c54f35caSApple OSS Distributions #if __LP64__
95*c54f35caSApple OSS Distributions 	"    .quad _restartable_function\n"
96*c54f35caSApple OSS Distributions #else
97*c54f35caSApple OSS Distributions 	"    .long _restartable_function\n"
98*c54f35caSApple OSS Distributions 	"    .long 0\n"
99*c54f35caSApple OSS Distributions #endif
100*c54f35caSApple OSS Distributions 	"    .short LExit_restartable_function - _restartable_function\n"
101*c54f35caSApple OSS Distributions 	"    .short LExit_restartable_function - _restartable_function\n"
102*c54f35caSApple OSS Distributions 	"    .long 0\n"
103*c54f35caSApple OSS Distributions 	"\n"
104*c54f35caSApple OSS Distributions #if __LP64__
105*c54f35caSApple OSS Distributions 	"    .quad _fake_msgSend\n"
106*c54f35caSApple OSS Distributions #else
107*c54f35caSApple OSS Distributions 	"    .long _fake_msgSend\n"
108*c54f35caSApple OSS Distributions 	"    .long 0\n"
109*c54f35caSApple OSS Distributions #endif
110*c54f35caSApple OSS Distributions 	"    .short LExit_fake_msgSend - _fake_msgSend\n"
111*c54f35caSApple OSS Distributions 	"    .short LRecover_fake_msgSend - _fake_msgSend\n"
112*c54f35caSApple OSS Distributions 	"    .long 0\n");
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions static void
noop_signal(int signo __unused)115*c54f35caSApple OSS Distributions noop_signal(int signo __unused)
116*c54f35caSApple OSS Distributions {
117*c54f35caSApple OSS Distributions }
118*c54f35caSApple OSS Distributions 
119*c54f35caSApple OSS Distributions static void *
task_restartable_ranges_thread(void * _ctx)120*c54f35caSApple OSS Distributions task_restartable_ranges_thread(void *_ctx)
121*c54f35caSApple OSS Distributions {
122*c54f35caSApple OSS Distributions 	int *stepp = _ctx;
123*c54f35caSApple OSS Distributions 	restartable_function(stepp); // increments step
124*c54f35caSApple OSS Distributions 	T_PASS("was successfully restarted\n");
125*c54f35caSApple OSS Distributions 	(*stepp)++;
126*c54f35caSApple OSS Distributions 	return NULL;
127*c54f35caSApple OSS Distributions }
128*c54f35caSApple OSS Distributions 
129*c54f35caSApple OSS Distributions static void
wait_for_step(int which)130*c54f35caSApple OSS Distributions wait_for_step(int which)
131*c54f35caSApple OSS Distributions {
132*c54f35caSApple OSS Distributions 	for (int i = 0; step != which && i < 10; i++) {
133*c54f35caSApple OSS Distributions 		usleep(100000);
134*c54f35caSApple OSS Distributions 	}
135*c54f35caSApple OSS Distributions }
136*c54f35caSApple OSS Distributions 
137*c54f35caSApple OSS Distributions #endif
138*c54f35caSApple OSS Distributions 
139*c54f35caSApple OSS Distributions T_DECL(task_restartable_ranges, "test task_restartable_ranges")
140*c54f35caSApple OSS Distributions {
141*c54f35caSApple OSS Distributions #ifdef SKIP_TEST
142*c54f35caSApple OSS Distributions 	T_SKIP("Not supported");
143*c54f35caSApple OSS Distributions #else
144*c54f35caSApple OSS Distributions 	kern_return_t kr;
145*c54f35caSApple OSS Distributions 	pthread_t th;
146*c54f35caSApple OSS Distributions 	int rc;
147*c54f35caSApple OSS Distributions 
148*c54f35caSApple OSS Distributions 	signal(SIGUSR1, noop_signal);
149*c54f35caSApple OSS Distributions 
150*c54f35caSApple OSS Distributions 	kr = task_restartable_ranges_register(mach_task_self(), ranges, 2);
151*c54f35caSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_restartable_ranges_register");
152*c54f35caSApple OSS Distributions 
153*c54f35caSApple OSS Distributions 	{
154*c54f35caSApple OSS Distributions 		rc = pthread_create(&th, NULL, &task_restartable_ranges_thread, &step);
155*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
156*c54f35caSApple OSS Distributions 
157*c54f35caSApple OSS Distributions 		wait_for_step(1);
158*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(step, 1, "The thread started (sync)");
159*c54f35caSApple OSS Distributions 
160*c54f35caSApple OSS Distributions 		kr = task_restartable_ranges_synchronize(mach_task_self());
161*c54f35caSApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "task_restartable_ranges_synchronize");
162*c54f35caSApple OSS Distributions 
163*c54f35caSApple OSS Distributions 		T_LOG("wait for the function to be restarted (sync)");
164*c54f35caSApple OSS Distributions 		wait_for_step(2);
165*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(step, 2, "The thread exited (sync)");
166*c54f35caSApple OSS Distributions 		pthread_join(th, NULL);
167*c54f35caSApple OSS Distributions 	}
168*c54f35caSApple OSS Distributions 
169*c54f35caSApple OSS Distributions 	{
170*c54f35caSApple OSS Distributions 		rc = pthread_create(&th, NULL, &task_restartable_ranges_thread, &step);
171*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
172*c54f35caSApple OSS Distributions 
173*c54f35caSApple OSS Distributions 		wait_for_step(3);
174*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(step, 3, "The thread started (signal)");
175*c54f35caSApple OSS Distributions 
176*c54f35caSApple OSS Distributions 		rc = pthread_kill(th, SIGUSR1);
177*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(rc, "pthread_kill");
178*c54f35caSApple OSS Distributions 
179*c54f35caSApple OSS Distributions 		T_LOG("wait for the function to be restarted (signal)");
180*c54f35caSApple OSS Distributions 		wait_for_step(4);
181*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(step, 4, "The thread exited (signal)");
182*c54f35caSApple OSS Distributions 		pthread_join(th, NULL);
183*c54f35caSApple OSS Distributions 	}
184*c54f35caSApple OSS Distributions #endif
185*c54f35caSApple OSS Distributions }
186*c54f35caSApple OSS Distributions 
187*c54f35caSApple OSS Distributions #ifndef SKIP_TEST
188*c54f35caSApple OSS Distributions 
189*c54f35caSApple OSS Distributions #define N_BUCKETS 4
190*c54f35caSApple OSS Distributions struct bucket {
191*c54f35caSApple OSS Distributions 	char buf[PAGE_MAX_SIZE] __attribute__((aligned(PAGE_MAX_SIZE)));
192*c54f35caSApple OSS Distributions };
193*c54f35caSApple OSS Distributions 
194*c54f35caSApple OSS Distributions static struct bucket arena[N_BUCKETS];
195*c54f35caSApple OSS Distributions static size_t arena_cur = 1;
196*c54f35caSApple OSS Distributions 
197*c54f35caSApple OSS Distributions static void *cls[5] = { 0, 0, &arena[0], 0, 0 }; /* our fake objc Class */
198*c54f35caSApple OSS Distributions static void *obj[4] = { cls, 0, 0, 0, };         /* our fake objc object */
199*c54f35caSApple OSS Distributions 
200*c54f35caSApple OSS Distributions static volatile long syncs = 1;
201*c54f35caSApple OSS Distributions 
202*c54f35caSApple OSS Distributions static void *
arena_alloc(void)203*c54f35caSApple OSS Distributions arena_alloc(void)
204*c54f35caSApple OSS Distributions {
205*c54f35caSApple OSS Distributions 	struct bucket *p = &arena[arena_cur++ % N_BUCKETS];
206*c54f35caSApple OSS Distributions 
207*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(mprotect(p, PAGE_MAX_SIZE,
208*c54f35caSApple OSS Distributions 	    PROT_READ | PROT_WRITE), "arena_alloc");
209*c54f35caSApple OSS Distributions 
210*c54f35caSApple OSS Distributions 	return p;
211*c54f35caSApple OSS Distributions }
212*c54f35caSApple OSS Distributions 
213*c54f35caSApple OSS Distributions static void
arena_free(void * p)214*c54f35caSApple OSS Distributions arena_free(void *p)
215*c54f35caSApple OSS Distributions {
216*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(mprotect(p, PAGE_MAX_SIZE,
217*c54f35caSApple OSS Distributions 	    PROT_NONE), "arena_free");
218*c54f35caSApple OSS Distributions }
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions static void
task_restartable_ranges_race_fail(int signo)221*c54f35caSApple OSS Distributions task_restartable_ranges_race_fail(int signo)
222*c54f35caSApple OSS Distributions {
223*c54f35caSApple OSS Distributions 	T_FAIL("test crashed with signal %s after %d syncs",
224*c54f35caSApple OSS Distributions 	    strsignal(signo), syncs);
225*c54f35caSApple OSS Distributions 	T_END;
226*c54f35caSApple OSS Distributions }
227*c54f35caSApple OSS Distributions 
228*c54f35caSApple OSS Distributions #endif
229*c54f35caSApple OSS Distributions 
230*c54f35caSApple OSS Distributions T_DECL(task_restartable_ranges_race, "test for 88873668")
231*c54f35caSApple OSS Distributions {
232*c54f35caSApple OSS Distributions #ifdef SKIP_TEST
233*c54f35caSApple OSS Distributions 	T_SKIP("Not supported");
234*c54f35caSApple OSS Distributions #else
235*c54f35caSApple OSS Distributions 	kern_return_t kr;
236*c54f35caSApple OSS Distributions 	pthread_t th;
237*c54f35caSApple OSS Distributions 	void *old;
238*c54f35caSApple OSS Distributions 	int rc;
239*c54f35caSApple OSS Distributions 
240*c54f35caSApple OSS Distributions 	signal(SIGBUS, task_restartable_ranges_race_fail);
241*c54f35caSApple OSS Distributions 
242*c54f35caSApple OSS Distributions 	kr = task_restartable_ranges_register(mach_task_self(), ranges, 2);
243*c54f35caSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_restartable_ranges_register");
244*c54f35caSApple OSS Distributions 
245*c54f35caSApple OSS Distributions 	dispatch_async_f(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0),
246*c54f35caSApple OSS Distributions 	    obj, fake_msgSend);
247*c54f35caSApple OSS Distributions 
248*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(mprotect(&arena[1],
249*c54f35caSApple OSS Distributions 	    (N_BUCKETS - 1) * PAGE_MAX_SIZE, PROT_NONE), "arena_init");
250*c54f35caSApple OSS Distributions 
251*c54f35caSApple OSS Distributions 	long step  = 16 << 10;
252*c54f35caSApple OSS Distributions 	long count = 16;
253*c54f35caSApple OSS Distributions 
254*c54f35caSApple OSS Distributions 	for (syncs = 1; syncs <= count * step; syncs++) {
255*c54f35caSApple OSS Distributions 		/*
256*c54f35caSApple OSS Distributions 		 * Simulate obj-c's algorithm:
257*c54f35caSApple OSS Distributions 		 *
258*c54f35caSApple OSS Distributions 		 * 1. allocate a new bucket
259*c54f35caSApple OSS Distributions 		 * 2. publish it
260*c54f35caSApple OSS Distributions 		 * 3. synchronize
261*c54f35caSApple OSS Distributions 		 * 4. dealloc the old bucket
262*c54f35caSApple OSS Distributions 		 */
263*c54f35caSApple OSS Distributions 		old = os_atomic_xchg(&cls[2], arena_alloc(), release);
264*c54f35caSApple OSS Distributions 
265*c54f35caSApple OSS Distributions 		kr = task_restartable_ranges_synchronize(mach_task_self());
266*c54f35caSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
267*c54f35caSApple OSS Distributions 			T_FAIL("task_restartable_ranges_register failed");
268*c54f35caSApple OSS Distributions 			T_END;
269*c54f35caSApple OSS Distributions 		}
270*c54f35caSApple OSS Distributions 
271*c54f35caSApple OSS Distributions 		if (syncs % step == 0) {
272*c54f35caSApple OSS Distributions 			T_LOG("%d/%d", syncs / step, count);
273*c54f35caSApple OSS Distributions 		}
274*c54f35caSApple OSS Distributions 
275*c54f35caSApple OSS Distributions 		arena_free(old);
276*c54f35caSApple OSS Distributions 	}
277*c54f35caSApple OSS Distributions 
278*c54f35caSApple OSS Distributions 	T_PASS("survived without crashing");
279*c54f35caSApple OSS Distributions #endif
280*c54f35caSApple OSS Distributions }
281