]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/test/tsan/aligned_vs_unaligned_race.cc
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / compiler-rt / test / tsan / aligned_vs_unaligned_race.cc
CommitLineData
92a42be0 1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
1a4d82fc
JJ
2// Race between an aligned access and an unaligned access, which
3// touches the same memory region.
92a42be0 4#include "test.h"
1a4d82fc
JJ
5#include <stdint.h>
6
7uint64_t Global[2];
8
9void *Thread1(void *x) {
10 Global[1]++;
92a42be0 11 barrier_wait(&barrier);
1a4d82fc
JJ
12 return NULL;
13}
14
15void *Thread2(void *x) {
92a42be0 16 barrier_wait(&barrier);
1a4d82fc 17 char *p1 = reinterpret_cast<char *>(&Global[0]);
92a42be0
SL
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++;
1a4d82fc
JJ
21 return NULL;
22}
23
24int main() {
92a42be0 25 barrier_init(&barrier, 2);
1a4d82fc
JJ
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 printf("Pass\n");
92a42be0 32 // CHECK: ThreadSanitizer: data race
1a4d82fc
JJ
33 // CHECK: Pass
34 return 0;
35}