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 kern_return_t kr;
33
34 /* ensure we pass can at least create and destroy a timer once */
35 timer = mk_timer_create();
36 kr = mk_timer_arm(timer, 1);
37 T_ASSERT_MACH_SUCCESS(kr, "timer arm");
38 kr = mk_timer_destroy(timer);
39 T_ASSERT_MACH_SUCCESS(kr, "timer destroy");
40 timer = MACH_PORT_NULL;
41
42 /* we will violate mach rules so ignore crashes here */
43 T_ASSERT_MACH_SUCCESS(task_set_exc_guard_behavior(mach_task_self(), 0),
44 "task_set_exc_guard_behavior");
45
46 for (n = 0; n < 4; ++n) {
47 T_ASSERT_POSIX_SUCCESS(pthread_create(&t, NULL, racer, &timer),
48 "pthread_create");
49 }
50
51 T_LOG("racing");
52 for (size_t i = 0; i < 1000; i++) {
53 timer = mk_timer_create();
54 kr = mk_timer_arm(timer, 1);
55 kr = mk_timer_destroy(timer);
56 timer = MACH_PORT_NULL;
57 }
58
59 T_PASS("didn't panic");
60 T_END;
61 }
62