xref: /xnu-8796.101.5/tests/thread_call_race_71455282.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 #include <darwintest.h>
2 #include <pthread.h>
3 #include <sys/syscall.h>
4 #include <unistd.h>
5 
6 #include <mach/mach_init.h>
7 #include <mach/mach_port.h>
8 #include <mach/mk_timer.h>
9 #include <mach/task.h>
10 
11 #define die(w) errx(1, (w))
12 #define edie(w) err(1, (w))
13 #define expect(e) if (-1 == (e)) edie(#e)
14 
15 static void *
racer(void * data)16 racer(void *data)
17 {
18 	for (;;) {
19 		mk_timer_destroy(*(mach_port_t *)data);
20 	}
21 
22 	return NULL;
23 }
24 
25 T_DECL(thread_call_race_71455282,
26     "rdar://71455282",
27     T_META_IGNORECRASHES(".*thread_call_race_71455282.*"))
28 {
29 	mach_port_t timer = MACH_PORT_NULL;
30 	pthread_t t;
31 	size_t n;
32 
33 	/* we will violate mach rules so ignore crashes here */
34 	T_ASSERT_MACH_SUCCESS(task_set_exc_guard_behavior(mach_task_self(), 0),
35 	    "task_set_exc_guard_behavior");
36 
37 	for (n = 0; n < 4; ++n) {
38 		T_ASSERT_POSIX_SUCCESS(pthread_create(&t, NULL, racer, &timer),
39 		    "pthread_create");
40 	}
41 
42 	T_LOG("racing");
43 	for (size_t i = 0; i < 1000; i++) {
44 		timer = mk_timer_create();
45 		mk_timer_arm(timer, 1);
46 		mk_timer_destroy(timer);
47 		timer = MACH_PORT_NULL;
48 	}
49 
50 	T_PASS("didn't panic");
51 	T_END;
52 }
53