1 // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
4 // Test case https://github.com/google/sanitizers/issues/494
5 // Tsan sees false HB edge on address pointed to by syncp variable.
6 // It is false because when acquire is done syncp points to a var in one frame,
7 // and during release it points to a var in a different frame.
8 // The code is somewhat tricky because it prevents compiler from optimizing
9 // our accesses away, structured to not introduce other data races and
10 // not introduce other synchronization, and to arrange the vars in different
11 // frames to occupy the same address.
13 // The data race CHECK-NOT below actually must be CHECK, because the program
14 // does contain the data race on global.
16 // CHECK-NOT: WARNING: ThreadSanitizer: data race
24 void *Thread(void *x
) {
25 while (__atomic_load_n(&syncp
, __ATOMIC_ACQUIRE
) == 0)
26 usleep(1000); // spin wait
28 __atomic_store_n(syncp
, 1, __ATOMIC_RELEASE
);
29 __atomic_store_n(&syncp
, 0, __ATOMIC_RELAXED
);
33 void __attribute__((noinline
)) foobar() {
34 __attribute__((aligned(64))) long s
;
37 __atomic_store_n(&s
, 0, __ATOMIC_RELAXED
);
38 __atomic_store_n(&syncp
, &s
, __ATOMIC_RELEASE
);
39 while (__atomic_load_n(&syncp
, __ATOMIC_RELAXED
) != 0)
40 usleep(1000); // spin wait
43 void __attribute__((noinline
)) barfoo() {
44 __attribute__((aligned(64))) long s
;
47 printf("address mismatch addr=%p &s=%p\n", addr
, &s
);
50 __atomic_store_n(&addr
, &s
, __ATOMIC_RELAXED
);
51 __atomic_store_n(&s
, 0, __ATOMIC_RELAXED
);
52 sink
= __atomic_load_n(&s
, __ATOMIC_ACQUIRE
);
58 pthread_create(&t
, 0, Thread
, 0);
64 fprintf(stderr
, "DONE\n");