1// Check that ignore_noninstrumented_modules=1 suppresses reporting races from
2// system libraries on OS X. There are currently false positives coming from
3// libxpc, libdispatch, CoreFoundation and others, because these libraries use
4// TSan-invisible atomics as synchronization.
5
6// RUN: %clang_tsan %s -o %t -framework Foundation
7
8// Check that without the flag, there are false positives.
9// RUN: %env_tsan_opts=ignore_noninstrumented_modules=0 %deflake %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RACE
10
11// With ignore_noninstrumented_modules=1, no races are reported.
12// RUN: %env_tsan_opts=ignore_noninstrumented_modules=1 %run %t 2>&1 | FileCheck %s
13
14// With ignore_noninstrumented_modules=1, races in user's code are still reported.
15// RUN: %env_tsan_opts=ignore_noninstrumented_modules=1 %deflake %run %t race 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-RACE
16
17#import <Foundation/Foundation.h>
18
19#import "../test.h"
20
21char global_buf[64];
22
23void *Thread1(void *x) {
24 barrier_wait(barrier: &barrier);
25 strcpy(global_buf, "hello world");
26 return NULL;
27}
28
29void *Thread2(void *x) {
30 strcpy(global_buf, "world hello");
31 barrier_wait(barrier: &barrier);
32 return NULL;
33}
34
35int main(int argc, char *argv[]) {
36 fprintf(stderr, format: "Hello world.\n");
37
38 // NSUserDefaults uses XPC which triggers the false positive.
39 NSDictionary *d = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
40 fprintf(stderr, "d = %p\n", d);
41
42 if (argc > 1 && strcmp(argv[1], "race") == 0) {
43 barrier_init(barrier: &barrier, count: 2);
44 pthread_t t[2];
45 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
46 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
47 pthread_join(th: t[0], NULL);
48 pthread_join(th: t[1], NULL);
49 }
50
51 fprintf(stderr, format: "Done.\n");
52}
53
54// CHECK: Hello world.
55// CHECK-RACE: SUMMARY: ThreadSanitizer: data race
56// CHECK: Done.
57

source code of compiler-rt/test/tsan/Darwin/ignore-noninstrumented.mm