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