[tsan] Avoid false positives with GCD data callbacks

This patch adds synchronization between the creation of the GCD data object and destructor’s execution. It’s far from perfect, because ideally we’d want to synchronize the destruction of the last reference (via dispatch_release) and the destructor’s execution, but intercepting objc_release is problematic.

Differential Revision: http://reviews.llvm.org/D21990

llvm-svn: 274749
This commit is contained in:
Kuba Brecka 2016-07-07 12:38:37 +00:00
parent 474c642172
commit 4446c216f5
2 changed files with 62 additions and 0 deletions

View File

@ -475,6 +475,32 @@ TSAN_INTERCEPTOR(void, dispatch_apply_f, size_t iterations,
WRAP(dispatch_apply)(iterations, queue, new_block); WRAP(dispatch_apply)(iterations, queue, new_block);
} }
DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
DECLARE_REAL_AND_INTERCEPTOR(int, munmap, void *addr, long_t sz)
TSAN_INTERCEPTOR(dispatch_data_t, dispatch_data_create, const void *buffer,
size_t size, dispatch_queue_t q, dispatch_block_t destructor) {
SCOPED_TSAN_INTERCEPTOR(dispatch_data_create, buffer, size, q, destructor);
if ((q == nullptr) || (destructor == DISPATCH_DATA_DESTRUCTOR_DEFAULT))
return REAL(dispatch_data_create)(buffer, size, q, destructor);
if (destructor == DISPATCH_DATA_DESTRUCTOR_FREE)
destructor = ^(void) { WRAP(free)((void *)buffer); };
else if (destructor == DISPATCH_DATA_DESTRUCTOR_MUNMAP)
destructor = ^(void) { WRAP(munmap)((void *)buffer, size); };
SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START();
dispatch_block_t heap_block = Block_copy(destructor);
SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END();
tsan_block_context_t *new_context =
AllocContext(thr, pc, q, heap_block, &invoke_and_release_block);
uptr submit_sync = (uptr)new_context;
Release(thr, pc, submit_sync);
return REAL(dispatch_data_create)(buffer, size, q, ^(void) {
dispatch_callback_wrap(new_context);
});
}
} // namespace __tsan } // namespace __tsan
#endif // SANITIZER_MAC #endif // SANITIZER_MAC

View File

@ -0,0 +1,36 @@
// 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 global;
int main(int argc, const char *argv[]) {
fprintf(stderr, "Hello world.\n");
dispatch_queue_t q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL);
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
global = 44;
dispatch_data_t data = dispatch_data_create("buffer", 6, q, ^{
fprintf(stderr, "Data destructor.\n");
global++;
dispatch_semaphore_signal(sem);
});
dispatch_release(data);
data = nil;
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
data = dispatch_data_create("buffer", 6, q, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_release(data);
data = nil;
fprintf(stderr, "Done.\n");
}
// CHECK: Hello world.
// CHECK: Data destructor.
// CHECK-NOT: WARNING: ThreadSanitizer
// CHECK: Done.