1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int Global;
5
6void __attribute__((noinline)) foo1() {
7 Global = 42;
8}
9
10void __attribute__((noinline)) bar1() {
11 volatile int tmp = 42;
12 int tmp2 = tmp;
13 (void)tmp2;
14 foo1();
15}
16
17void __attribute__((noinline)) foo2() {
18 volatile int tmp = Global;
19 int tmp2 = tmp;
20 (void)tmp2;
21}
22
23void __attribute__((noinline)) bar2() {
24 volatile int tmp = 42;
25 int tmp2 = tmp;
26 (void)tmp2;
27 foo2();
28}
29
30void *Thread1(void *x) {
31 barrier_wait(barrier: &barrier);
32 bar1();
33 return NULL;
34}
35
36int main() {
37 barrier_init(barrier: &barrier, count: 2);
38 pthread_t t;
39 pthread_create(newthread: &t, NULL, start_routine: Thread1, NULL);
40 bar2();
41 barrier_wait(barrier: &barrier);
42 pthread_join(th: t, NULL);
43}
44
45// CHECK: WARNING: ThreadSanitizer: data race
46// CHECK-NEXT: Write of size 4 at {{.*}} by thread T1:
47// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack2.cpp:[[@LINE-40]]{{(:10)?}} ({{.*}})
48// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack2.cpp:[[@LINE-34]]{{(:3)?}} ({{.*}})
49// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack2.cpp:[[@LINE-17]]{{(:3)?}} ({{.*}})
50// CHECK: Previous read of size 4 at {{.*}} by main thread:
51// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack2.cpp:[[@LINE-33]]{{(:22)?}} ({{.*}})
52// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack2.cpp:[[@LINE-25]]{{(:3)?}} ({{.*}})
53// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack2.cpp:[[@LINE-13]]{{(:3)?}} ({{.*}})
54

source code of compiler-rt/test/tsan/simple_stack2.cpp