1 //===-- tsan_interceptors_libdispatch.cpp ---------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of ThreadSanitizer (TSan), a race detector. 10 // 11 // Support for intercepting libdispatch (GCD). 12 //===----------------------------------------------------------------------===// 13 14 #include "sanitizer_common/sanitizer_common.h" 15 #include "interception/interception.h" 16 #include "tsan_interceptors.h" 17 #include "tsan_rtl.h" 18 19 #include "BlocksRuntime/Block.h" 20 #include "tsan_dispatch_defs.h" 21 22 #if SANITIZER_MAC 23 # include <Availability.h> 24 #endif 25 26 namespace __tsan { 27 typedef u16 uint16_t; 28 29 typedef struct { 30 dispatch_queue_t queue; 31 void *orig_context; 32 dispatch_function_t orig_work; 33 bool free_context_in_callback; 34 bool submitted_synchronously; 35 bool is_barrier_block; 36 uptr non_queue_sync_object; 37 } block_context_t; 38 39 // The offsets of different fields of the dispatch_queue_t structure, exported 40 // by libdispatch.dylib. 41 extern "C" struct dispatch_queue_offsets_s { 42 const uint16_t dqo_version; 43 const uint16_t dqo_label; 44 const uint16_t dqo_label_size; 45 const uint16_t dqo_flags; 46 const uint16_t dqo_flags_size; 47 const uint16_t dqo_serialnum; 48 const uint16_t dqo_serialnum_size; 49 const uint16_t dqo_width; 50 const uint16_t dqo_width_size; 51 const uint16_t dqo_running; 52 const uint16_t dqo_running_size; 53 const uint16_t dqo_suspend_cnt; 54 const uint16_t dqo_suspend_cnt_size; 55 const uint16_t dqo_target_queue; 56 const uint16_t dqo_target_queue_size; 57 const uint16_t dqo_priority; 58 const uint16_t dqo_priority_size; 59 } dispatch_queue_offsets; 60 61 static bool IsQueueSerial(dispatch_queue_t q) { 62 CHECK_EQ(dispatch_queue_offsets.dqo_width_size, 2); 63 uptr width = *(uint16_t *)(((uptr)q) + dispatch_queue_offsets.dqo_width); 64 CHECK_NE(width, 0); 65 return width == 1; 66 } 67 68 static dispatch_queue_t GetTargetQueueFromQueue(dispatch_queue_t q) { 69 CHECK_EQ(dispatch_queue_offsets.dqo_target_queue_size, 8); 70 dispatch_queue_t tq = *( 71 dispatch_queue_t *)(((uptr)q) + dispatch_queue_offsets.dqo_target_queue); 72 return tq; 73 } 74 75 static dispatch_queue_t GetTargetQueueFromSource(dispatch_source_t source) { 76 dispatch_queue_t tq = GetTargetQueueFromQueue((dispatch_queue_t)source); 77 CHECK_NE(tq, 0); 78 return tq; 79 } 80 81 static block_context_t *AllocContext(ThreadState *thr, uptr pc, 82 dispatch_queue_t queue, void *orig_context, 83 dispatch_function_t orig_work) { 84 block_context_t *new_context = 85 (block_context_t *)user_alloc_internal(thr, pc, sizeof(block_context_t)); 86 new_context->queue = queue; 87 new_context->orig_context = orig_context; 88 new_context->orig_work = orig_work; 89 new_context->free_context_in_callback = true; 90 new_context->submitted_synchronously = false; 91 new_context->is_barrier_block = false; 92 new_context->non_queue_sync_object = 0; 93 return new_context; 94 } 95 96 #define GET_QUEUE_SYNC_VARS(context, q) \ 97 bool is_queue_serial = q && IsQueueSerial(q); \ 98 uptr sync_ptr = (uptr)q ?: context->non_queue_sync_object; \ 99 uptr serial_sync = (uptr)sync_ptr; \ 100 uptr concurrent_sync = sync_ptr ? ((uptr)sync_ptr) + sizeof(uptr) : 0; \ 101 bool serial_task = context->is_barrier_block || is_queue_serial 102 103 static void dispatch_sync_pre_execute(ThreadState *thr, uptr pc, 104 block_context_t *context) { 105 uptr submit_sync = (uptr)context; 106 Acquire(thr, pc, submit_sync); 107 108 dispatch_queue_t q = context->queue; 109 do { 110 GET_QUEUE_SYNC_VARS(context, q); 111 if (serial_sync) Acquire(thr, pc, serial_sync); 112 if (serial_task && concurrent_sync) Acquire(thr, pc, concurrent_sync); 113 114 if (q) q = GetTargetQueueFromQueue(q); 115 } while (q); 116 } 117 118 static void dispatch_sync_post_execute(ThreadState *thr, uptr pc, 119 block_context_t *context) { 120 uptr submit_sync = (uptr)context; 121 if (context->submitted_synchronously) Release(thr, pc, submit_sync); 122 123 dispatch_queue_t q = context->queue; 124 do { 125 GET_QUEUE_SYNC_VARS(context, q); 126 if (serial_task && serial_sync) Release(thr, pc, serial_sync); 127 if (!serial_task && concurrent_sync) Release(thr, pc, concurrent_sync); 128 129 if (q) q = GetTargetQueueFromQueue(q); 130 } while (q); 131 } 132 133 static void dispatch_callback_wrap(void *param) { 134 SCOPED_INTERCEPTOR_RAW(dispatch_callback_wrap); 135 block_context_t *context = (block_context_t *)param; 136 137 dispatch_sync_pre_execute(thr, pc, context); 138 139 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 140 context->orig_work(context->orig_context); 141 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 142 143 dispatch_sync_post_execute(thr, pc, context); 144 145 if (context->free_context_in_callback) user_free(thr, pc, context); 146 } 147 148 static void invoke_block(void *param) { 149 dispatch_block_t block = (dispatch_block_t)param; 150 block(); 151 } 152 153 static void invoke_and_release_block(void *param) { 154 dispatch_block_t block = (dispatch_block_t)param; 155 block(); 156 Block_release(block); 157 } 158 159 #define DISPATCH_INTERCEPT_ASYNC_B(name, barrier) \ 160 TSAN_INTERCEPTOR(void, name, dispatch_queue_t q, dispatch_block_t block) { \ 161 SCOPED_TSAN_INTERCEPTOR(name, q, block); \ 162 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); \ 163 dispatch_block_t heap_block = Block_copy(block); \ 164 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); \ 165 block_context_t *new_context = \ 166 AllocContext(thr, pc, q, heap_block, &invoke_and_release_block); \ 167 new_context->is_barrier_block = barrier; \ 168 Release(thr, pc, (uptr)new_context); \ 169 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); \ 170 REAL(name##_f)(q, new_context, dispatch_callback_wrap); \ 171 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); \ 172 } 173 174 #define DISPATCH_INTERCEPT_SYNC_B(name, barrier) \ 175 TSAN_INTERCEPTOR(void, name, dispatch_queue_t q, \ 176 DISPATCH_NOESCAPE dispatch_block_t block) { \ 177 SCOPED_TSAN_INTERCEPTOR(name, q, block); \ 178 block_context_t new_context = { \ 179 q, block, &invoke_block, false, true, barrier, 0}; \ 180 Release(thr, pc, (uptr)&new_context); \ 181 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); \ 182 REAL(name##_f)(q, &new_context, dispatch_callback_wrap); \ 183 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); \ 184 Acquire(thr, pc, (uptr)&new_context); \ 185 } 186 187 #define DISPATCH_INTERCEPT_ASYNC_F(name, barrier) \ 188 TSAN_INTERCEPTOR(void, name, dispatch_queue_t q, void *context, \ 189 dispatch_function_t work) { \ 190 SCOPED_TSAN_INTERCEPTOR(name, q, context, work); \ 191 block_context_t *new_context = \ 192 AllocContext(thr, pc, q, context, work); \ 193 new_context->is_barrier_block = barrier; \ 194 Release(thr, pc, (uptr)new_context); \ 195 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); \ 196 REAL(name)(q, new_context, dispatch_callback_wrap); \ 197 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); \ 198 } 199 200 #define DISPATCH_INTERCEPT_SYNC_F(name, barrier) \ 201 TSAN_INTERCEPTOR(void, name, dispatch_queue_t q, void *context, \ 202 dispatch_function_t work) { \ 203 SCOPED_TSAN_INTERCEPTOR(name, q, context, work); \ 204 block_context_t new_context = { \ 205 q, context, work, false, true, barrier, 0}; \ 206 Release(thr, pc, (uptr)&new_context); \ 207 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); \ 208 REAL(name)(q, &new_context, dispatch_callback_wrap); \ 209 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); \ 210 Acquire(thr, pc, (uptr)&new_context); \ 211 } 212 213 #define DISPATCH_INTERCEPT(name, barrier) \ 214 DISPATCH_INTERCEPT_ASYNC_F(name##_async_f, barrier) \ 215 DISPATCH_INTERCEPT_ASYNC_B(name##_async, barrier) \ 216 DISPATCH_INTERCEPT_SYNC_F(name##_sync_f, barrier) \ 217 DISPATCH_INTERCEPT_SYNC_B(name##_sync, barrier) 218 219 // We wrap dispatch_async, dispatch_sync and friends where we allocate a new 220 // context, which is used to synchronize (we release the context before 221 // submitting, and the callback acquires it before executing the original 222 // callback). 223 DISPATCH_INTERCEPT(dispatch, false) 224 DISPATCH_INTERCEPT(dispatch_barrier, true) 225 226 // dispatch_async_and_wait() and friends were introduced in macOS 10.14. 227 // Linking of these interceptors fails when using an older SDK. 228 #if !SANITIZER_MAC || defined(__MAC_10_14) 229 // macOS 10.14 is greater than our minimal deployment target. To ensure we 230 // generate a weak reference so the TSan dylib continues to work on older 231 // systems, we need to forward declare the intercepted functions as "weak 232 // imports". Note that this file is multi-platform, so we cannot include the 233 // actual header file (#include <dispatch/dispatch.h>). 234 SANITIZER_WEAK_IMPORT void dispatch_async_and_wait( 235 dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block); 236 SANITIZER_WEAK_IMPORT void dispatch_async_and_wait_f( 237 dispatch_queue_t queue, void *context, dispatch_function_t work); 238 239 DISPATCH_INTERCEPT_SYNC_B(dispatch_async_and_wait, false) 240 DISPATCH_INTERCEPT_SYNC_F(dispatch_async_and_wait_f, false) 241 #endif 242 243 244 DECLARE_REAL(void, dispatch_after_f, dispatch_time_t when, 245 dispatch_queue_t queue, void *context, dispatch_function_t work) 246 247 TSAN_INTERCEPTOR(void, dispatch_after, dispatch_time_t when, 248 dispatch_queue_t queue, dispatch_block_t block) { 249 SCOPED_TSAN_INTERCEPTOR(dispatch_after, when, queue, block); 250 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 251 dispatch_block_t heap_block = Block_copy(block); 252 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 253 block_context_t *new_context = 254 AllocContext(thr, pc, queue, heap_block, &invoke_and_release_block); 255 Release(thr, pc, (uptr)new_context); 256 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 257 REAL(dispatch_after_f)(when, queue, new_context, dispatch_callback_wrap); 258 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 259 } 260 261 TSAN_INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, 262 dispatch_queue_t queue, void *context, 263 dispatch_function_t work) { 264 SCOPED_TSAN_INTERCEPTOR(dispatch_after_f, when, queue, context, work); 265 WRAP(dispatch_after)(when, queue, ^(void) { 266 work(context); 267 }); 268 } 269 270 // GCD's dispatch_once implementation has a fast path that contains a racy read 271 // and it's inlined into user's code. Furthermore, this fast path doesn't 272 // establish a proper happens-before relations between the initialization and 273 // code following the call to dispatch_once. We could deal with this in 274 // instrumented code, but there's not much we can do about it in system 275 // libraries. Let's disable the fast path (by never storing the value ~0 to 276 // predicate), so the interceptor is always called, and let's add proper release 277 // and acquire semantics. Since TSan does not see its own atomic stores, the 278 // race on predicate won't be reported - the only accesses to it that TSan sees 279 // are the loads on the fast path. Loads don't race. Secondly, dispatch_once is 280 // both a macro and a real function, we want to intercept the function, so we 281 // need to undefine the macro. 282 #undef dispatch_once 283 TSAN_INTERCEPTOR(void, dispatch_once, dispatch_once_t *predicate, 284 DISPATCH_NOESCAPE dispatch_block_t block) { 285 SCOPED_INTERCEPTOR_RAW(dispatch_once, predicate, block); 286 atomic_uint32_t *a = reinterpret_cast<atomic_uint32_t *>(predicate); 287 u32 v = atomic_load(a, memory_order_acquire); 288 if (v == 0 && 289 atomic_compare_exchange_strong(a, &v, 1, memory_order_relaxed)) { 290 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 291 block(); 292 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 293 Release(thr, pc, (uptr)a); 294 atomic_store(a, 2, memory_order_release); 295 } else { 296 while (v != 2) { 297 internal_sched_yield(); 298 v = atomic_load(a, memory_order_acquire); 299 } 300 Acquire(thr, pc, (uptr)a); 301 } 302 } 303 304 #undef dispatch_once_f 305 TSAN_INTERCEPTOR(void, dispatch_once_f, dispatch_once_t *predicate, 306 void *context, dispatch_function_t function) { 307 SCOPED_INTERCEPTOR_RAW(dispatch_once_f, predicate, context, function); 308 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 309 WRAP(dispatch_once)(predicate, ^(void) { 310 function(context); 311 }); 312 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 313 } 314 315 TSAN_INTERCEPTOR(long_t, dispatch_semaphore_signal, 316 dispatch_semaphore_t dsema) { 317 SCOPED_TSAN_INTERCEPTOR(dispatch_semaphore_signal, dsema); 318 Release(thr, pc, (uptr)dsema); 319 return REAL(dispatch_semaphore_signal)(dsema); 320 } 321 322 TSAN_INTERCEPTOR(long_t, dispatch_semaphore_wait, dispatch_semaphore_t dsema, 323 dispatch_time_t timeout) { 324 SCOPED_TSAN_INTERCEPTOR(dispatch_semaphore_wait, dsema, timeout); 325 long_t result = REAL(dispatch_semaphore_wait)(dsema, timeout); 326 if (result == 0) Acquire(thr, pc, (uptr)dsema); 327 return result; 328 } 329 330 TSAN_INTERCEPTOR(long_t, dispatch_group_wait, dispatch_group_t group, 331 dispatch_time_t timeout) { 332 SCOPED_TSAN_INTERCEPTOR(dispatch_group_wait, group, timeout); 333 long_t result = REAL(dispatch_group_wait)(group, timeout); 334 if (result == 0) Acquire(thr, pc, (uptr)group); 335 return result; 336 } 337 338 // Used, but not intercepted. 339 extern "C" void dispatch_group_enter(dispatch_group_t group); 340 341 TSAN_INTERCEPTOR(void, dispatch_group_leave, dispatch_group_t group) { 342 SCOPED_TSAN_INTERCEPTOR(dispatch_group_leave, group); 343 // Acquired in the group notification callback in dispatch_group_notify[_f]. 344 Release(thr, pc, (uptr)group); 345 REAL(dispatch_group_leave)(group); 346 } 347 348 TSAN_INTERCEPTOR(void, dispatch_group_async, dispatch_group_t group, 349 dispatch_queue_t queue, dispatch_block_t block) { 350 SCOPED_TSAN_INTERCEPTOR(dispatch_group_async, group, queue, block); 351 dispatch_retain(group); 352 dispatch_group_enter(group); 353 __block dispatch_block_t block_copy = (dispatch_block_t)Block_copy(block); 354 WRAP(dispatch_async)(queue, ^(void) { 355 block_copy(); 356 Block_release(block_copy); 357 WRAP(dispatch_group_leave)(group); 358 dispatch_release(group); 359 }); 360 } 361 362 TSAN_INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group, 363 dispatch_queue_t queue, void *context, 364 dispatch_function_t work) { 365 SCOPED_TSAN_INTERCEPTOR(dispatch_group_async_f, group, queue, context, work); 366 dispatch_retain(group); 367 dispatch_group_enter(group); 368 WRAP(dispatch_async)(queue, ^(void) { 369 work(context); 370 WRAP(dispatch_group_leave)(group); 371 dispatch_release(group); 372 }); 373 } 374 375 DECLARE_REAL(void, dispatch_group_notify_f, dispatch_group_t group, 376 dispatch_queue_t q, void *context, dispatch_function_t work) 377 378 TSAN_INTERCEPTOR(void, dispatch_group_notify, dispatch_group_t group, 379 dispatch_queue_t q, dispatch_block_t block) { 380 SCOPED_TSAN_INTERCEPTOR(dispatch_group_notify, group, q, block); 381 382 // To make sure the group is still available in the callback (otherwise 383 // it can be already destroyed). Will be released in the callback. 384 dispatch_retain(group); 385 386 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 387 dispatch_block_t heap_block = Block_copy(^(void) { 388 { 389 SCOPED_INTERCEPTOR_RAW(dispatch_read_callback); 390 // Released when leaving the group (dispatch_group_leave). 391 Acquire(thr, pc, (uptr)group); 392 } 393 dispatch_release(group); 394 block(); 395 }); 396 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 397 block_context_t *new_context = 398 AllocContext(thr, pc, q, heap_block, &invoke_and_release_block); 399 new_context->is_barrier_block = true; 400 Release(thr, pc, (uptr)new_context); 401 REAL(dispatch_group_notify_f)(group, q, new_context, dispatch_callback_wrap); 402 } 403 404 TSAN_INTERCEPTOR(void, dispatch_group_notify_f, dispatch_group_t group, 405 dispatch_queue_t q, void *context, dispatch_function_t work) { 406 WRAP(dispatch_group_notify)(group, q, ^(void) { work(context); }); 407 } 408 409 TSAN_INTERCEPTOR(void, dispatch_source_set_event_handler, 410 dispatch_source_t source, dispatch_block_t handler) { 411 SCOPED_TSAN_INTERCEPTOR(dispatch_source_set_event_handler, source, handler); 412 if (handler == nullptr) 413 return REAL(dispatch_source_set_event_handler)(source, nullptr); 414 dispatch_queue_t q = GetTargetQueueFromSource(source); 415 __block block_context_t new_context = { 416 q, handler, &invoke_block, false, false, false, 0 }; 417 dispatch_block_t new_handler = Block_copy(^(void) { 418 new_context.orig_context = handler; // To explicitly capture "handler". 419 dispatch_callback_wrap(&new_context); 420 }); 421 uptr submit_sync = (uptr)&new_context; 422 Release(thr, pc, submit_sync); 423 REAL(dispatch_source_set_event_handler)(source, new_handler); 424 Block_release(new_handler); 425 } 426 427 TSAN_INTERCEPTOR(void, dispatch_source_set_event_handler_f, 428 dispatch_source_t source, dispatch_function_t handler) { 429 SCOPED_TSAN_INTERCEPTOR(dispatch_source_set_event_handler_f, source, handler); 430 if (handler == nullptr) 431 return REAL(dispatch_source_set_event_handler)(source, nullptr); 432 dispatch_block_t block = ^(void) { 433 handler(dispatch_get_context(source)); 434 }; 435 WRAP(dispatch_source_set_event_handler)(source, block); 436 } 437 438 TSAN_INTERCEPTOR(void, dispatch_source_set_cancel_handler, 439 dispatch_source_t source, dispatch_block_t handler) { 440 SCOPED_TSAN_INTERCEPTOR(dispatch_source_set_cancel_handler, source, handler); 441 if (handler == nullptr) 442 return REAL(dispatch_source_set_cancel_handler)(source, nullptr); 443 dispatch_queue_t q = GetTargetQueueFromSource(source); 444 __block block_context_t new_context = { 445 q, handler, &invoke_block, false, false, false, 0}; 446 dispatch_block_t new_handler = Block_copy(^(void) { 447 new_context.orig_context = handler; // To explicitly capture "handler". 448 dispatch_callback_wrap(&new_context); 449 }); 450 uptr submit_sync = (uptr)&new_context; 451 Release(thr, pc, submit_sync); 452 REAL(dispatch_source_set_cancel_handler)(source, new_handler); 453 Block_release(new_handler); 454 } 455 456 TSAN_INTERCEPTOR(void, dispatch_source_set_cancel_handler_f, 457 dispatch_source_t source, dispatch_function_t handler) { 458 SCOPED_TSAN_INTERCEPTOR(dispatch_source_set_cancel_handler_f, source, 459 handler); 460 if (handler == nullptr) 461 return REAL(dispatch_source_set_cancel_handler)(source, nullptr); 462 dispatch_block_t block = ^(void) { 463 handler(dispatch_get_context(source)); 464 }; 465 WRAP(dispatch_source_set_cancel_handler)(source, block); 466 } 467 468 TSAN_INTERCEPTOR(void, dispatch_source_set_registration_handler, 469 dispatch_source_t source, dispatch_block_t handler) { 470 SCOPED_TSAN_INTERCEPTOR(dispatch_source_set_registration_handler, source, 471 handler); 472 if (handler == nullptr) 473 return REAL(dispatch_source_set_registration_handler)(source, nullptr); 474 dispatch_queue_t q = GetTargetQueueFromSource(source); 475 __block block_context_t new_context = { 476 q, handler, &invoke_block, false, false, false, 0}; 477 dispatch_block_t new_handler = Block_copy(^(void) { 478 new_context.orig_context = handler; // To explicitly capture "handler". 479 dispatch_callback_wrap(&new_context); 480 }); 481 uptr submit_sync = (uptr)&new_context; 482 Release(thr, pc, submit_sync); 483 REAL(dispatch_source_set_registration_handler)(source, new_handler); 484 Block_release(new_handler); 485 } 486 487 TSAN_INTERCEPTOR(void, dispatch_source_set_registration_handler_f, 488 dispatch_source_t source, dispatch_function_t handler) { 489 SCOPED_TSAN_INTERCEPTOR(dispatch_source_set_registration_handler_f, source, 490 handler); 491 if (handler == nullptr) 492 return REAL(dispatch_source_set_registration_handler)(source, nullptr); 493 dispatch_block_t block = ^(void) { 494 handler(dispatch_get_context(source)); 495 }; 496 WRAP(dispatch_source_set_registration_handler)(source, block); 497 } 498 499 TSAN_INTERCEPTOR(void, dispatch_apply, size_t iterations, 500 dispatch_queue_t queue, 501 DISPATCH_NOESCAPE void (^block)(size_t)) { 502 SCOPED_TSAN_INTERCEPTOR(dispatch_apply, iterations, queue, block); 503 504 u8 sync1, sync2; 505 uptr parent_to_child_sync = (uptr)&sync1; 506 uptr child_to_parent_sync = (uptr)&sync2; 507 508 Release(thr, pc, parent_to_child_sync); 509 void (^new_block)(size_t) = ^(size_t iteration) { 510 SCOPED_INTERCEPTOR_RAW(dispatch_apply); 511 Acquire(thr, pc, parent_to_child_sync); 512 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 513 block(iteration); 514 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 515 Release(thr, pc, child_to_parent_sync); 516 }; 517 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 518 REAL(dispatch_apply)(iterations, queue, new_block); 519 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 520 Acquire(thr, pc, child_to_parent_sync); 521 } 522 523 static void invoke_block_iteration(void *param, size_t iteration) { 524 auto block = (void (^)(size_t)) param; 525 block(iteration); 526 } 527 528 TSAN_INTERCEPTOR(void, dispatch_apply_f, size_t iterations, 529 dispatch_queue_t queue, void *context, 530 void (*work)(void *, size_t)) { 531 SCOPED_TSAN_INTERCEPTOR(dispatch_apply_f, iterations, queue, context, work); 532 533 // Unfortunately, we cannot delegate to dispatch_apply, since libdispatch 534 // implements dispatch_apply in terms of dispatch_apply_f. 535 u8 sync1, sync2; 536 uptr parent_to_child_sync = (uptr)&sync1; 537 uptr child_to_parent_sync = (uptr)&sync2; 538 539 Release(thr, pc, parent_to_child_sync); 540 void (^new_block)(size_t) = ^(size_t iteration) { 541 SCOPED_INTERCEPTOR_RAW(dispatch_apply_f); 542 Acquire(thr, pc, parent_to_child_sync); 543 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 544 work(context, iteration); 545 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 546 Release(thr, pc, child_to_parent_sync); 547 }; 548 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 549 REAL(dispatch_apply_f)(iterations, queue, new_block, invoke_block_iteration); 550 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 551 Acquire(thr, pc, child_to_parent_sync); 552 } 553 554 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr) 555 DECLARE_REAL_AND_INTERCEPTOR(int, munmap, void *addr, long_t sz) 556 557 TSAN_INTERCEPTOR(dispatch_data_t, dispatch_data_create, const void *buffer, 558 size_t size, dispatch_queue_t q, dispatch_block_t destructor) { 559 SCOPED_TSAN_INTERCEPTOR(dispatch_data_create, buffer, size, q, destructor); 560 if ((q == nullptr) || (destructor == DISPATCH_DATA_DESTRUCTOR_DEFAULT)) 561 return REAL(dispatch_data_create)(buffer, size, q, destructor); 562 563 if (destructor == DISPATCH_DATA_DESTRUCTOR_FREE) 564 destructor = ^(void) { WRAP(free)((void *)(uintptr_t)buffer); }; 565 else if (destructor == DISPATCH_DATA_DESTRUCTOR_MUNMAP) 566 destructor = ^(void) { WRAP(munmap)((void *)(uintptr_t)buffer, size); }; 567 568 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); 569 dispatch_block_t heap_block = Block_copy(destructor); 570 SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); 571 block_context_t *new_context = 572 AllocContext(thr, pc, q, heap_block, &invoke_and_release_block); 573 uptr submit_sync = (uptr)new_context; 574 Release(thr, pc, submit_sync); 575 return REAL(dispatch_data_create)(buffer, size, q, ^(void) { 576 dispatch_callback_wrap(new_context); 577 }); 578 } 579 580 typedef void (^fd_handler_t)(dispatch_data_t data, int error); 581 typedef void (^cleanup_handler_t)(int error); 582 583 TSAN_INTERCEPTOR(void, dispatch_read, dispatch_fd_t fd, size_t length, 584 dispatch_queue_t q, fd_handler_t h) { 585 SCOPED_TSAN_INTERCEPTOR(dispatch_read, fd, length, q, h); 586 __block block_context_t new_context = { 587 q, nullptr, &invoke_block, false, false, false, 0}; 588 fd_handler_t new_h = Block_copy(^(dispatch_data_t data, int error) { 589 new_context.orig_context = ^(void) { 590 h(data, error); 591 }; 592 dispatch_callback_wrap(&new_context); 593 }); 594 uptr submit_sync = (uptr)&new_context; 595 Release(thr, pc, submit_sync); 596 REAL(dispatch_read)(fd, length, q, new_h); 597 Block_release(new_h); 598 } 599 600 TSAN_INTERCEPTOR(void, dispatch_write, dispatch_fd_t fd, dispatch_data_t data, 601 dispatch_queue_t q, fd_handler_t h) { 602 SCOPED_TSAN_INTERCEPTOR(dispatch_write, fd, data, q, h); 603 __block block_context_t new_context = { 604 q, nullptr, &invoke_block, false, false, false, 0}; 605 fd_handler_t new_h = Block_copy(^(dispatch_data_t data, int error) { 606 new_context.orig_context = ^(void) { 607 h(data, error); 608 }; 609 dispatch_callback_wrap(&new_context); 610 }); 611 uptr submit_sync = (uptr)&new_context; 612 Release(thr, pc, submit_sync); 613 REAL(dispatch_write)(fd, data, q, new_h); 614 Block_release(new_h); 615 } 616 617 TSAN_INTERCEPTOR(void, dispatch_io_read, dispatch_io_t channel, off_t offset, 618 size_t length, dispatch_queue_t q, dispatch_io_handler_t h) { 619 SCOPED_TSAN_INTERCEPTOR(dispatch_io_read, channel, offset, length, q, h); 620 __block block_context_t new_context = { 621 q, nullptr, &invoke_block, false, false, false, 0}; 622 dispatch_io_handler_t new_h = 623 Block_copy(^(bool done, dispatch_data_t data, int error) { 624 new_context.orig_context = ^(void) { 625 h(done, data, error); 626 }; 627 dispatch_callback_wrap(&new_context); 628 }); 629 uptr submit_sync = (uptr)&new_context; 630 Release(thr, pc, submit_sync); 631 REAL(dispatch_io_read)(channel, offset, length, q, new_h); 632 Block_release(new_h); 633 } 634 635 TSAN_INTERCEPTOR(void, dispatch_io_write, dispatch_io_t channel, off_t offset, 636 dispatch_data_t data, dispatch_queue_t q, 637 dispatch_io_handler_t h) { 638 SCOPED_TSAN_INTERCEPTOR(dispatch_io_write, channel, offset, data, q, h); 639 __block block_context_t new_context = { 640 q, nullptr, &invoke_block, false, false, false, 0}; 641 dispatch_io_handler_t new_h = 642 Block_copy(^(bool done, dispatch_data_t data, int error) { 643 new_context.orig_context = ^(void) { 644 h(done, data, error); 645 }; 646 dispatch_callback_wrap(&new_context); 647 }); 648 uptr submit_sync = (uptr)&new_context; 649 Release(thr, pc, submit_sync); 650 REAL(dispatch_io_write)(channel, offset, data, q, new_h); 651 Block_release(new_h); 652 } 653 654 TSAN_INTERCEPTOR(void, dispatch_io_barrier, dispatch_io_t channel, 655 dispatch_block_t barrier) { 656 SCOPED_TSAN_INTERCEPTOR(dispatch_io_barrier, channel, barrier); 657 __block block_context_t new_context = { 658 nullptr, nullptr, &invoke_block, false, false, false, 0}; 659 new_context.non_queue_sync_object = (uptr)channel; 660 new_context.is_barrier_block = true; 661 dispatch_block_t new_block = Block_copy(^(void) { 662 new_context.orig_context = ^(void) { 663 barrier(); 664 }; 665 dispatch_callback_wrap(&new_context); 666 }); 667 uptr submit_sync = (uptr)&new_context; 668 Release(thr, pc, submit_sync); 669 REAL(dispatch_io_barrier)(channel, new_block); 670 Block_release(new_block); 671 } 672 673 TSAN_INTERCEPTOR(dispatch_io_t, dispatch_io_create, dispatch_io_type_t type, 674 dispatch_fd_t fd, dispatch_queue_t q, cleanup_handler_t h) { 675 SCOPED_TSAN_INTERCEPTOR(dispatch_io_create, type, fd, q, h); 676 __block dispatch_io_t new_channel = nullptr; 677 __block block_context_t new_context = { 678 q, nullptr, &invoke_block, false, false, false, 0}; 679 cleanup_handler_t new_h = Block_copy(^(int error) { 680 { 681 SCOPED_INTERCEPTOR_RAW(dispatch_io_create_callback); 682 Acquire(thr, pc, (uptr)new_channel); // Release() in dispatch_io_close. 683 } 684 new_context.orig_context = ^(void) { 685 h(error); 686 }; 687 dispatch_callback_wrap(&new_context); 688 }); 689 uptr submit_sync = (uptr)&new_context; 690 Release(thr, pc, submit_sync); 691 new_channel = REAL(dispatch_io_create)(type, fd, q, new_h); 692 Block_release(new_h); 693 return new_channel; 694 } 695 696 TSAN_INTERCEPTOR(dispatch_io_t, dispatch_io_create_with_path, 697 dispatch_io_type_t type, const char *path, int oflag, 698 mode_t mode, dispatch_queue_t q, cleanup_handler_t h) { 699 SCOPED_TSAN_INTERCEPTOR(dispatch_io_create_with_path, type, path, oflag, mode, 700 q, h); 701 __block dispatch_io_t new_channel = nullptr; 702 __block block_context_t new_context = { 703 q, nullptr, &invoke_block, false, false, false, 0}; 704 cleanup_handler_t new_h = Block_copy(^(int error) { 705 { 706 SCOPED_INTERCEPTOR_RAW(dispatch_io_create_callback); 707 Acquire(thr, pc, (uptr)new_channel); // Release() in dispatch_io_close. 708 } 709 new_context.orig_context = ^(void) { 710 h(error); 711 }; 712 dispatch_callback_wrap(&new_context); 713 }); 714 uptr submit_sync = (uptr)&new_context; 715 Release(thr, pc, submit_sync); 716 new_channel = 717 REAL(dispatch_io_create_with_path)(type, path, oflag, mode, q, new_h); 718 Block_release(new_h); 719 return new_channel; 720 } 721 722 TSAN_INTERCEPTOR(dispatch_io_t, dispatch_io_create_with_io, 723 dispatch_io_type_t type, dispatch_io_t io, dispatch_queue_t q, 724 cleanup_handler_t h) { 725 SCOPED_TSAN_INTERCEPTOR(dispatch_io_create_with_io, type, io, q, h); 726 __block dispatch_io_t new_channel = nullptr; 727 __block block_context_t new_context = { 728 q, nullptr, &invoke_block, false, false, false, 0}; 729 cleanup_handler_t new_h = Block_copy(^(int error) { 730 { 731 SCOPED_INTERCEPTOR_RAW(dispatch_io_create_callback); 732 Acquire(thr, pc, (uptr)new_channel); // Release() in dispatch_io_close. 733 } 734 new_context.orig_context = ^(void) { 735 h(error); 736 }; 737 dispatch_callback_wrap(&new_context); 738 }); 739 uptr submit_sync = (uptr)&new_context; 740 Release(thr, pc, submit_sync); 741 new_channel = REAL(dispatch_io_create_with_io)(type, io, q, new_h); 742 Block_release(new_h); 743 return new_channel; 744 } 745 746 TSAN_INTERCEPTOR(void, dispatch_io_close, dispatch_io_t channel, 747 dispatch_io_close_flags_t flags) { 748 SCOPED_TSAN_INTERCEPTOR(dispatch_io_close, channel, flags); 749 Release(thr, pc, (uptr)channel); // Acquire() in dispatch_io_create[_*]. 750 return REAL(dispatch_io_close)(channel, flags); 751 } 752 753 // Resuming a suspended queue needs to synchronize with all subsequent 754 // executions of blocks in that queue. 755 TSAN_INTERCEPTOR(void, dispatch_resume, dispatch_object_t o) { 756 SCOPED_TSAN_INTERCEPTOR(dispatch_resume, o); 757 Release(thr, pc, (uptr)o); // Synchronizes with the Acquire() on serial_sync 758 // in dispatch_sync_pre_execute 759 return REAL(dispatch_resume)(o); 760 } 761 762 void InitializeLibdispatchInterceptors() { 763 INTERCEPT_FUNCTION(dispatch_async); 764 INTERCEPT_FUNCTION(dispatch_async_f); 765 INTERCEPT_FUNCTION(dispatch_sync); 766 INTERCEPT_FUNCTION(dispatch_sync_f); 767 INTERCEPT_FUNCTION(dispatch_barrier_async); 768 INTERCEPT_FUNCTION(dispatch_barrier_async_f); 769 INTERCEPT_FUNCTION(dispatch_barrier_sync); 770 INTERCEPT_FUNCTION(dispatch_barrier_sync_f); 771 INTERCEPT_FUNCTION(dispatch_async_and_wait); 772 INTERCEPT_FUNCTION(dispatch_async_and_wait_f); 773 INTERCEPT_FUNCTION(dispatch_after); 774 INTERCEPT_FUNCTION(dispatch_after_f); 775 INTERCEPT_FUNCTION(dispatch_once); 776 INTERCEPT_FUNCTION(dispatch_once_f); 777 INTERCEPT_FUNCTION(dispatch_semaphore_signal); 778 INTERCEPT_FUNCTION(dispatch_semaphore_wait); 779 INTERCEPT_FUNCTION(dispatch_group_wait); 780 INTERCEPT_FUNCTION(dispatch_group_leave); 781 INTERCEPT_FUNCTION(dispatch_group_async); 782 INTERCEPT_FUNCTION(dispatch_group_async_f); 783 INTERCEPT_FUNCTION(dispatch_group_notify); 784 INTERCEPT_FUNCTION(dispatch_group_notify_f); 785 INTERCEPT_FUNCTION(dispatch_source_set_event_handler); 786 INTERCEPT_FUNCTION(dispatch_source_set_event_handler_f); 787 INTERCEPT_FUNCTION(dispatch_source_set_cancel_handler); 788 INTERCEPT_FUNCTION(dispatch_source_set_cancel_handler_f); 789 INTERCEPT_FUNCTION(dispatch_source_set_registration_handler); 790 INTERCEPT_FUNCTION(dispatch_source_set_registration_handler_f); 791 INTERCEPT_FUNCTION(dispatch_apply); 792 INTERCEPT_FUNCTION(dispatch_apply_f); 793 INTERCEPT_FUNCTION(dispatch_data_create); 794 INTERCEPT_FUNCTION(dispatch_read); 795 INTERCEPT_FUNCTION(dispatch_write); 796 INTERCEPT_FUNCTION(dispatch_io_read); 797 INTERCEPT_FUNCTION(dispatch_io_write); 798 INTERCEPT_FUNCTION(dispatch_io_barrier); 799 INTERCEPT_FUNCTION(dispatch_io_create); 800 INTERCEPT_FUNCTION(dispatch_io_create_with_path); 801 INTERCEPT_FUNCTION(dispatch_io_create_with_io); 802 INTERCEPT_FUNCTION(dispatch_io_close); 803 INTERCEPT_FUNCTION(dispatch_resume); 804 } 805 806 } // namespace __tsan 807