1// RUN: rm -rf %t-dir
2// RUN: mkdir %t-dir
3
4// RUN: %clangxx_tsan -O1 %s -o %t-dir/global_race.cpp.exe && %deflake %run %t-dir/global_race.cpp.exe 2>&1 \
5// RUN: | FileCheck %s
6
7// Also check that memory access instrumentation can be configured by either
8// driver or legacy flags:
9
10// RUN: %clangxx_tsan -O1 %s -o %t-dir/global_race.cpp.exe -fno-sanitize-thread-memory-access && not %deflake %run %t-dir/global_race.cpp.exe 2>&1 \
11// RUN: | FileCheck --allow-empty --check-prefix=CHECK-MEMORY-ACCESS-OFF %s
12// RUN: %clangxx_tsan -O1 %s -o %t-dir/global_race.cpp.exe -mllvm -tsan-instrument-memory-accesses=0 && not %deflake %run %t-dir/global_race.cpp.exe 2>&1 \
13// RUN: | FileCheck --allow-empty --check-prefix=CHECK-MEMORY-ACCESS-OFF %s
14
15#include "test.h"
16
17int GlobalData[10];
18
19void *Thread(void *a) {
20 barrier_wait(barrier: &barrier);
21 GlobalData[2] = 42;
22 return 0;
23}
24
25int main() {
26 barrier_init(barrier: &barrier, count: 2);
27 print_address("addr=", 1, GlobalData);
28 pthread_t t;
29 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
30 GlobalData[2] = 43;
31 barrier_wait(barrier: &barrier);
32 pthread_join(th: t, thread_return: 0);
33}
34
35// CHECK: addr=[[ADDR:0x[0-9,a-f]+]]
36// CHECK: WARNING: ThreadSanitizer: data race
37// CHECK: Location is global 'GlobalData' {{(of size 40 )?}}at [[ADDR]] (global_race.cpp.exe+0x{{[0-9,a-f]+}})
38
39// CHECK-MEMORY-ACCESS-OFF-NOT: WARNING: ThreadSanitizer: data race
40

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