blob: fb299da8e72e649546e2fd5bd8873d40512fd22e [file] [log] [blame]
Renato Golin1f422862016-04-15 12:34:00 +00001// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
Kostya Serebryany4d7efba2013-03-26 08:31:02 +00002// Race between an aligned access and an unaligned access, which
3// touches the same memory region.
Dmitry Vyukov312ad252015-01-27 20:19:12 +00004#include "test.h"
Kostya Serebryany4d7efba2013-03-26 08:31:02 +00005#include <stdint.h>
6
7uint64_t Global[2];
8
9void *Thread1(void *x) {
10 Global[1]++;
Dmitry Vyukov312ad252015-01-27 20:19:12 +000011 barrier_wait(&barrier);
Kostya Serebryany4d7efba2013-03-26 08:31:02 +000012 return NULL;
13}
14
15void *Thread2(void *x) {
Dmitry Vyukov312ad252015-01-27 20:19:12 +000016 barrier_wait(&barrier);
Kostya Serebryany4d7efba2013-03-26 08:31:02 +000017 char *p1 = reinterpret_cast<char *>(&Global[0]);
Dmitry Vyukov312ad252015-01-27 20:19:12 +000018 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++;
Kostya Serebryany4d7efba2013-03-26 08:31:02 +000021 return NULL;
22}
23
24int main() {
Dmitry Vyukov312ad252015-01-27 20:19:12 +000025 barrier_init(&barrier, 2);
Kostya Serebryany4d7efba2013-03-26 08:31:02 +000026 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);
Renato Golin1f422862016-04-15 12:34:00 +000031 fprintf(stderr, "Pass\n");
Dmitry Vyukov312ad252015-01-27 20:19:12 +000032 // CHECK: ThreadSanitizer: data race
Alexey Samsonov059e61f2013-03-27 09:25:06 +000033 // CHECK: Pass
Alexey Samsonovf903a9e2013-03-27 10:22:51 +000034 return 0;
Kostya Serebryany4d7efba2013-03-26 08:31:02 +000035}