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