From ed29c21d5de6811d40c8c023ddad6b27d0ef635d Mon Sep 17 00:00:00 2001 From: Kuba Brecka Date: Thu, 19 May 2016 15:31:42 +0000 Subject: [PATCH] [tsan] Add support for GCD's dispatch_after and dispatch_after_f We're missing interceptors for dispatch_after and dispatch_after_f. Let's add them to avoid false positives. Added a test case. Differential Revision: http://reviews.llvm.org/D20426 llvm-svn: 270071 --- .../lib/tsan/rtl/tsan_libdispatch_mac.cc | 23 +++++++++++ compiler-rt/test/tsan/Darwin/gcd-after.mm | 41 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 compiler-rt/test/tsan/Darwin/gcd-after.mm diff --git a/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc b/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc index 30e5b4bb0ab3..7f1b8809b2a4 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc @@ -184,6 +184,29 @@ DISPATCH_INTERCEPT_SYNC_B(dispatch_barrier_sync) DISPATCH_INTERCEPT_SYNC_F(dispatch_sync_f) DISPATCH_INTERCEPT_SYNC_F(dispatch_barrier_sync_f) +TSAN_INTERCEPTOR(void, dispatch_after, dispatch_time_t when, + dispatch_queue_t queue, dispatch_block_t block) { + SCOPED_TSAN_INTERCEPTOR(dispatch_after, when, queue, block); + SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); + dispatch_block_t heap_block = Block_copy(block); + SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); + tsan_block_context_t *new_context = + AllocContext(thr, pc, queue, heap_block, &invoke_and_release_block); + Release(thr, pc, (uptr)new_context); + SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START(); + REAL(dispatch_after_f)(when, queue, new_context, dispatch_callback_wrap); + SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END(); +} + +TSAN_INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, + dispatch_queue_t queue, void *context, + dispatch_function_t work) { + SCOPED_TSAN_INTERCEPTOR(dispatch_after_f, when, queue, context, work); + WRAP(dispatch_after)(when, queue, ^(void) { + work(context); + }); +} + // GCD's dispatch_once implementation has a fast path that contains a racy read // and it's inlined into user's code. Furthermore, this fast path doesn't // establish a proper happens-before relations between the initialization and diff --git a/compiler-rt/test/tsan/Darwin/gcd-after.mm b/compiler-rt/test/tsan/Darwin/gcd-after.mm new file mode 100644 index 000000000000..49b6bc6f71e9 --- /dev/null +++ b/compiler-rt/test/tsan/Darwin/gcd-after.mm @@ -0,0 +1,41 @@ +// RUN: %clang_tsan %s -o %t -framework Foundation +// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s + +#import + +long my_global; +long my_global2; + +void callback(void *context) { + my_global2 = 42; + + dispatch_async(dispatch_get_main_queue(), ^{ + CFRunLoopStop(CFRunLoopGetMain()); + }); +} + +int main(int argc, const char *argv[]) { + fprintf(stderr, "start\n"); + + my_global = 10; + dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_MSEC)), q, ^{ + my_global = 42; + + dispatch_async(dispatch_get_main_queue(), ^{ + CFRunLoopStop(CFRunLoopGetMain()); + }); + }); + CFRunLoopRun(); + + my_global2 = 10; + dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_MSEC)), q, NULL, &callback); + CFRunLoopRun(); + + fprintf(stderr, "done\n"); + return 0; +} + +// CHECK: start +// CHECK: done +// CHECK-NOT: WARNING: ThreadSanitizer