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 | |
9 | void *Thread1(void *x) { |
10 | Global[1]++; |
11 | barrier_wait(barrier: &barrier); |
12 | return NULL; |
13 | } |
14 | |
15 | void *Thread2(void *x) { |
16 | barrier_wait(barrier: &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 | |
24 | int main() { |
25 | barrier_init(barrier: &barrier, count: 2); |
26 | pthread_t t[2]; |
27 | pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL); |
28 | pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL); |
29 | pthread_join(th: t[0], NULL); |
30 | pthread_join(th: t[1], NULL); |
31 | fprintf(stderr, format: "Pass\n" ); |
32 | // CHECK: ThreadSanitizer: data race |
33 | // CHECK: Pass |
34 | return 0; |
35 | } |
36 | |