1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4// Ensure that we can restore a stack of a finished thread.
5
6int g_data;
7
8void __attribute__((noinline)) foobar(int *p) {
9 *p = 42;
10}
11
12void *Thread1(void *x) {
13 foobar(p: &g_data);
14 barrier_wait(barrier: &barrier);
15 return NULL;
16}
17
18void *Thread2(void *x) {
19 barrier_wait(barrier: &barrier);
20 sleep(seconds: 1); // let the thread finish and exit
21 g_data = 43;
22 return NULL;
23}
24
25int main() {
26 barrier_init(barrier: &barrier, count: 2);
27 pthread_t t[2];
28 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
29 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
30 pthread_join(th: t[0], NULL);
31 pthread_join(th: t[1], NULL);
32 return 0;
33}
34
35// CHECK: WARNING: ThreadSanitizer: data race
36// CHECK: Write of size 4 at {{.*}} by thread T2:
37// CHECK: Previous write of size 4 at {{.*}} by thread T1:
38// CHECK: #0 foobar
39// CHECK: #1 Thread1
40// CHECK: Thread T1 (tid={{.*}}, finished) created by main thread at:
41// CHECK: #0 pthread_create
42// CHECK: #1 main
43

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