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