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 int x[4], z[4]; 9 10 void *MemCpyThread(void *a) { 11 memcpy((int*)a, z, 16); 12 return NULL; 13 } 14 15 void *MemSetThread(void *a) { 16 sleep(1); 17 memset((int*)a, 0, 16); 18 return NULL; 19 } 20 21 int main() { 22 pthread_t t[2]; 23 // Race on x between memcpy and memset 24 pthread_create(&t[0], NULL, MemCpyThread, x); 25 pthread_create(&t[1], NULL, MemSetThread, x); 26 pthread_join(t[0], NULL); 27 pthread_join(t[1], NULL); 28 printf("PASS\n"); 29 return 0; 30 } 31 32 // CHECK: WARNING: ThreadSanitizer: data race 33 // CHECK: #0 memset 34 // CHECK: #1 MemSetThread 35 // CHECK: Previous write 36 // CHECK: #0 memcpy 37 // CHECK: #1 MemCpyThread 38 39