[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
This commit is contained in:
Kuba Brecka 2016-05-19 15:31:42 +00:00
parent 99c901fc47
commit ed29c21d5d
2 changed files with 64 additions and 0 deletions

View File

@ -184,6 +184,29 @@ DISPATCH_INTERCEPT_SYNC_B(dispatch_barrier_sync)
DISPATCH_INTERCEPT_SYNC_F(dispatch_sync_f) DISPATCH_INTERCEPT_SYNC_F(dispatch_sync_f)
DISPATCH_INTERCEPT_SYNC_F(dispatch_barrier_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 // 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 // and it's inlined into user's code. Furthermore, this fast path doesn't
// establish a proper happens-before relations between the initialization and // establish a proper happens-before relations between the initialization and

View File

@ -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 <Foundation/Foundation.h>
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