• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 
Thread1(void * p)6 void *Thread1(void *p) {
7   *(int*)p = 42;
8   return 0;
9 }
10 
Thread2(void * p)11 void *Thread2(void *p) {
12   *(int*)p = 44;
13   return 0;
14 }
15 
alloc()16 void *alloc() {
17   return malloc(99);
18 }
19 
AllocThread(void * arg)20 void *AllocThread(void* arg) {
21   return alloc();
22 }
23 
main()24 int main() {
25   void *p = 0;
26   pthread_t t[2];
27   pthread_create(&t[0], 0, AllocThread, 0);
28   pthread_join(t[0], &p);
29   fprintf(stderr, "addr=%p\n", p);
30   pthread_create(&t[0], 0, Thread1, (char*)p + 16);
31   pthread_create(&t[1], 0, Thread2, (char*)p + 16);
32   pthread_join(t[0], 0);
33   pthread_join(t[1], 0);
34   return 0;
35 }
36 
37 // CHECK: addr=[[ADDR:0x[0-9,a-f]+]]
38 // CHECK: WARNING: ThreadSanitizer: data race
39 // ...
40 // CHECK: Location is heap block of size 99 at [[ADDR]] allocated by thread T1:
41 // CHCEK:     #0 malloc
42 // CHECK:     #{{1|2}} alloc
43 // CHECK:     #{{2|3}} AllocThread
44 // ...
45 // CHECK:   Thread T1 (tid={{.*}}, finished) created by main thread at:
46 // CHECK:     #0 pthread_create
47 // CHECK:     #1 main
48