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