1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2 // Race between an aligned access and an unaligned access, which
3 // touches the same memory region.
4 #include "test.h"
5 #include <stdint.h>
6 
7 uint64_t Global[2];
8 
Thread1(void * x)9 void *Thread1(void *x) {
10   Global[1]++;
11   barrier_wait(&barrier);
12   return NULL;
13 }
14 
Thread2(void * x)15 void *Thread2(void *x) {
16   barrier_wait(&barrier);
17   char *p1 = reinterpret_cast<char *>(&Global[0]);
18   struct __attribute__((packed, aligned(1))) u_uint64_t { uint64_t val; };
19   u_uint64_t *p4 = reinterpret_cast<u_uint64_t *>(p1 + 1);
20   (*p4).val++;
21   return NULL;
22 }
23 
main()24 int main() {
25   barrier_init(&barrier, 2);
26   pthread_t t[2];
27   pthread_create(&t[0], NULL, Thread1, NULL);
28   pthread_create(&t[1], NULL, Thread2, NULL);
29   pthread_join(t[0], NULL);
30   pthread_join(t[1], NULL);
31   fprintf(stderr, "Pass\n");
32   // CHECK: ThreadSanitizer: data race
33   // CHECK: Pass
34   return 0;
35 }
36