llvm-project
129 строк · 3.2 Кб
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3#include <stdint.h>
4
5#define NOINLINE __attribute__((noinline))
6
7volatile uint64_t objs[8*2*(2 + 4 + 8)][2];
8
9// All this mess is to generate unique stack for each race,
10// otherwise tsan will suppress similar stacks.
11
12static NOINLINE void access(volatile void *p, int sz, int rw) {
13if (rw) {
14switch (sz) {
15case 0: __sanitizer_unaligned_store16((void *)p, 0); break;
16case 1: __sanitizer_unaligned_store32((void *)p, 0); break;
17case 2: __sanitizer_unaligned_store64((void *)p, 0); break;
18default: exit(1);
19}
20} else {
21switch (sz) {
22case 0: __sanitizer_unaligned_load16((void *)p); break;
23case 1: __sanitizer_unaligned_load32((void *)p); break;
24case 2: __sanitizer_unaligned_load64((void *)p); break;
25default: exit(1);
26}
27}
28}
29
30static int accesssize(int sz) {
31switch (sz) {
32case 0: return 2;
33case 1: return 4;
34case 2: return 8;
35}
36exit(1);
37}
38
39template<int off, int off2>
40static NOINLINE void access3(bool main, int sz1, bool rw, volatile char *p) {
41p += off;
42if (main) {
43access(p, sz1, true);
44} else {
45p += off2;
46if (rw) {
47*p = 42;
48} else {
49if (*p == 42)
50printf("bingo!\n");
51}
52}
53}
54
55template<int off>
56static NOINLINE void
57access2(bool main, int sz1, int off2, bool rw, volatile char *obj) {
58if (off2 == 0)
59access3<off, 0>(main, sz1, rw, obj);
60else if (off2 == 1)
61access3<off, 1>(main, sz1, rw, obj);
62else if (off2 == 2)
63access3<off, 2>(main, sz1, rw, obj);
64else if (off2 == 3)
65access3<off, 3>(main, sz1, rw, obj);
66else if (off2 == 4)
67access3<off, 4>(main, sz1, rw, obj);
68else if (off2 == 5)
69access3<off, 5>(main, sz1, rw, obj);
70else if (off2 == 6)
71access3<off, 6>(main, sz1, rw, obj);
72else if (off2 == 7)
73access3<off, 7>(main, sz1, rw, obj);
74}
75
76static NOINLINE void
77access1(bool main, int off, int sz1, int off2, bool rw, char *obj) {
78if (off == 0)
79access2<0>(main, sz1, off2, rw, obj);
80else if (off == 1)
81access2<1>(main, sz1, off2, rw, obj);
82else if (off == 2)
83access2<2>(main, sz1, off2, rw, obj);
84else if (off == 3)
85access2<3>(main, sz1, off2, rw, obj);
86else if (off == 4)
87access2<4>(main, sz1, off2, rw, obj);
88else if (off == 5)
89access2<5>(main, sz1, off2, rw, obj);
90else if (off == 6)
91access2<6>(main, sz1, off2, rw, obj);
92else if (off == 7)
93access2<7>(main, sz1, off2, rw, obj);
94}
95
96NOINLINE void Test(bool main) {
97volatile uint64_t *obj = objs[0];
98for (int off = 0; off < 8; off++) {
99for (int sz1 = 0; sz1 < 3; sz1++) {
100for (int off2 = 0; off2 < accesssize(sz1); off2++) {
101for (int rw = 0; rw < 2; rw++) {
102// printf("thr=%d off=%d sz1=%d off2=%d rw=%d p=%p\n",
103// main, off, sz1, off2, rw, obj);
104access1(main, off, sz1, off2, rw, (char*)obj);
105obj += 2;
106}
107}
108}
109}
110}
111
112void *Thread(void *p) {
113(void)p;
114barrier_wait(&barrier);
115Test(false);
116return 0;
117}
118
119int main() {
120barrier_init(&barrier, 2);
121pthread_t th;
122pthread_create(&th, 0, Thread, 0);
123Test(true);
124barrier_wait(&barrier);
125pthread_join(th, 0);
126}
127
128// CHECK: WARNING: ThreadSanitizer: data race
129// CHECK: ThreadSanitizer: reported 224 warnings
130