1// RUN: %clangxx_tsan -O1 %s -o %t -DORDER1 && %deflake %run %t | FileCheck %s
2// RUN: %clangxx_tsan -O1 %s -o %t -DORDER2 && %deflake %run %t | FileCheck %s
3#include "test.h"
4
5volatile int X;
6volatile int N;
7void (*volatile F)();
8
9static void foo() {
10 if (--N == 0)
11 X = 42;
12 else
13 F();
14}
15
16void *Thread(void *p) {
17#ifdef ORDER1
18 barrier_wait(&barrier);
19#endif
20 F();
21#ifdef ORDER2
22 barrier_wait(&barrier);
23#endif
24 return 0;
25}
26
27static size_t RoundUp(size_t n, size_t to) {
28 return ((n + to - 1) / to) * to;
29}
30
31int main() {
32 barrier_init(barrier: &barrier, count: 2);
33 N = 50000;
34 F = foo;
35 pthread_t t;
36 pthread_attr_t a;
37 pthread_attr_init(attr: &a);
38 size_t stack_size = N * 256 + (1 << 20);
39 stack_size = RoundUp(n: stack_size, to: 0x10000); // round the stack size to 64k
40 int ret = pthread_attr_setstacksize(attr: &a, stacksize: stack_size);
41 if (ret) abort();
42 pthread_create(newthread: &t, attr: &a, start_routine: Thread, arg: 0);
43#ifdef ORDER2
44 barrier_wait(&barrier);
45#endif
46 X = 43;
47#ifdef ORDER1
48 barrier_wait(&barrier);
49#endif
50
51 pthread_join(th: t, thread_return: 0);
52}
53
54// CHECK: WARNING: ThreadSanitizer: data race
55// CHECK: #100 foo
56// We must output sufficiently large stack (at least 100 frames)
57
58

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