1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot #include "test.h"
3*7c3d14c8STreehugger Robot
4*7c3d14c8STreehugger Robot int Global;
5*7c3d14c8STreehugger Robot volatile int x;
6*7c3d14c8STreehugger Robot const int kSize = 64 << 10;
7*7c3d14c8STreehugger Robot volatile long data[kSize];
8*7c3d14c8STreehugger Robot
foo()9*7c3d14c8STreehugger Robot void __attribute__((noinline)) foo() {
10*7c3d14c8STreehugger Robot for (int i = 0; i < kSize; i++)
11*7c3d14c8STreehugger Robot data[i]++;
12*7c3d14c8STreehugger Robot }
13*7c3d14c8STreehugger Robot
Thread(void * a)14*7c3d14c8STreehugger Robot void *Thread(void *a) {
15*7c3d14c8STreehugger Robot __atomic_store_n(&x, 1, __ATOMIC_RELEASE);
16*7c3d14c8STreehugger Robot foo();
17*7c3d14c8STreehugger Robot data[0]++;
18*7c3d14c8STreehugger Robot if (a != 0)
19*7c3d14c8STreehugger Robot barrier_wait(&barrier);
20*7c3d14c8STreehugger Robot return 0;
21*7c3d14c8STreehugger Robot }
22*7c3d14c8STreehugger Robot
main()23*7c3d14c8STreehugger Robot int main() {
24*7c3d14c8STreehugger Robot barrier_init(&barrier, 2);
25*7c3d14c8STreehugger Robot for (int i = 0; i < 50; i++) {
26*7c3d14c8STreehugger Robot pthread_t t;
27*7c3d14c8STreehugger Robot pthread_create(&t, 0, Thread, 0);
28*7c3d14c8STreehugger Robot pthread_join(t, 0);
29*7c3d14c8STreehugger Robot }
30*7c3d14c8STreehugger Robot pthread_t t;
31*7c3d14c8STreehugger Robot pthread_create(&t, 0, Thread, (void*)1);
32*7c3d14c8STreehugger Robot barrier_wait(&barrier);
33*7c3d14c8STreehugger Robot for (int i = 0; i < kSize; i++)
34*7c3d14c8STreehugger Robot data[i]++;
35*7c3d14c8STreehugger Robot pthread_join(t, 0);
36*7c3d14c8STreehugger Robot fprintf(stderr, "DONE\n");
37*7c3d14c8STreehugger Robot return 0;
38*7c3d14c8STreehugger Robot }
39*7c3d14c8STreehugger Robot
40*7c3d14c8STreehugger Robot // Previously this test produced bogus stack traces like:
41*7c3d14c8STreehugger Robot // Previous write of size 8 at 0x0000006a8ff8 by thread T17:
42*7c3d14c8STreehugger Robot // #0 foo() restore_stack.cc:13:5 (restore_stack.cc.exe+0x00000040622c)
43*7c3d14c8STreehugger Robot // #1 Thread(void*) restore_stack.cc:18:3 (restore_stack.cc.exe+0x000000406283)
44*7c3d14c8STreehugger Robot // #2 __tsan_thread_start_func rtl/tsan_interceptors.cc:886 (restore_stack.cc.exe+0x00000040a749)
45*7c3d14c8STreehugger Robot // #3 Thread(void*) restore_stack.cc:18:3 (restore_stack.cc.exe+0x000000406283)
46*7c3d14c8STreehugger Robot
47*7c3d14c8STreehugger Robot // CHECK: WARNING: ThreadSanitizer: data race
48*7c3d14c8STreehugger Robot // CHECK-NOT: __tsan_thread_start_func
49*7c3d14c8STreehugger Robot // CHECK-NOT: #3 Thread
50*7c3d14c8STreehugger Robot // CHECK: DONE
51