1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3#include <string.h>
4
5char *data = new char[10];
6char *data1 = new char[10];
7char *data2 = new char[10];
8
9void *Thread1(void *x) {
10 static volatile int size = 1;
11 memcpy(dest: data+5, src: data1, n: size);
12 barrier_wait(barrier: &barrier);
13 return NULL;
14}
15
16void *Thread2(void *x) {
17 static volatile int size = 4;
18 barrier_wait(barrier: &barrier);
19 memcpy(dest: data+3, src: data2, n: size);
20 return NULL;
21}
22
23int main() {
24 barrier_init(barrier: &barrier, count: 2);
25 print_address("addr1=", 1, &data[3]);
26 print_address("addr2=", 1, &data[5]);
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: addr1=[[ADDR1:0x[0-9,a-f]+]]
36// CHECK: addr2=[[ADDR2:0x[0-9,a-f]+]]
37// CHECK: WARNING: ThreadSanitizer: data race
38// CHECK: Write of size 4 at [[ADDR1]] by thread T2:
39// CHECK: #0 {{.*mem(cpy|move)}}
40// CHECK: #{{[12]}} Thread2
41// CHECK: Previous write of size 1 at [[ADDR2]] by thread T1:
42// CHECK: #0 {{.*mem(cpy|move)}}
43// CHECK: #{{[12]}} Thread1
44

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