Home | History | Annotate | Download | only in tsan
      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 char *data = new char[10];
      9 char *data1 = new char[10];
     10 char *data2 = new char[10];
     11 
     12 void *Thread1(void *x) {
     13   static volatile int size = 1;
     14   memcpy(data+5, data1, size);
     15   return NULL;
     16 }
     17 
     18 void *Thread2(void *x) {
     19   static volatile int size = 4;
     20   sleep(1);
     21   memcpy(data+3, data2, size);
     22   return NULL;
     23 }
     24 
     25 int main() {
     26   fprintf(stderr, "addr=%p\n", &data[5]);
     27   pthread_t t[2];
     28   pthread_create(&t[0], NULL, Thread1, NULL);
     29   pthread_create(&t[1], NULL, Thread2, NULL);
     30   pthread_join(t[0], NULL);
     31   pthread_join(t[1], NULL);
     32   return 0;
     33 }
     34 
     35 // CHECK: addr=[[ADDR:0x[0-9,a-f]+]]
     36 // CHECK: WARNING: ThreadSanitizer: data race
     37 // CHECK:   Write of size 1 at [[ADDR]] by thread T2:
     38 // CHECK:     #0 memcpy
     39 // CHECK:     #1 Thread2
     40 // CHECK:   Previous write of size 1 at [[ADDR]] by thread T1:
     41 // CHECK:     #0 memcpy
     42 // CHECK:     #1 Thread1
     43