tsan_interceptors_libdispatch.cpp 33 KB

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