1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
// This tests carefully crafted schedules to ensure they are not considered races.
//@compile-flags: -Zmiri-deterministic-concurrency
use std::sync::atomic::*;
use std::thread::{self, spawn};
#[derive(Copy, Clone)]
struct EvilSend<T>(pub T);
unsafe impl<T> Send for EvilSend<T> {}
unsafe impl<T> Sync for EvilSend<T> {}
fn test_fence_sync() {
static SYNC: AtomicUsize = AtomicUsize::new(0);
let mut var = 0u32;
let ptr = &mut var as *mut u32;
let evil_ptr = EvilSend(ptr);
let j1 = spawn(move || {
let evil_ptr = evil_ptr; // avoid field capturing
unsafe { *evil_ptr.0 = 1 };
fence(Ordering::Release);
SYNC.store(1, Ordering::Relaxed)
});
let j2 = spawn(move || {
let evil_ptr = evil_ptr; // avoid field capturing
if SYNC.load(Ordering::Relaxed) == 1 {
fence(Ordering::Acquire);
unsafe { *evil_ptr.0 }
} else {
panic!(); // relies on thread 2 going last
}
});
j1.join().unwrap();
j2.join().unwrap();
}
fn test_multiple_reads() {
let mut var = 42u32;
let ptr = &mut var as *mut u32;
let evil_ptr = EvilSend(ptr);
let j1 = spawn(move || unsafe { *{ evil_ptr }.0 });
let j2 = spawn(move || unsafe { *{ evil_ptr }.0 });
let j3 = spawn(move || unsafe { *{ evil_ptr }.0 });
let j4 = spawn(move || unsafe { *{ evil_ptr }.0 });
assert_eq!(j1.join().unwrap(), 42);
assert_eq!(j2.join().unwrap(), 42);
assert_eq!(j3.join().unwrap(), 42);
assert_eq!(j4.join().unwrap(), 42);
var = 10;
assert_eq!(var, 10);
}
pub fn test_rmw_no_block() {
static SYNC: AtomicUsize = AtomicUsize::new(0);
let mut a = 0u32;
let b = &mut a as *mut u32;
let c = EvilSend(b);
unsafe {
let j1 = spawn(move || {
let c = c; // avoid field capturing
*c.0 = 1;
SYNC.store(1, Ordering::Release);
});
let j2 = spawn(move || {
if SYNC.swap(2, Ordering::Relaxed) == 1 {
//No op, blocking store removed
}
});
let j3 = spawn(move || {
let c = c; // avoid field capturing
if SYNC.load(Ordering::Acquire) == 2 { *c.0 } else { 0 }
});
j1.join().unwrap();
j2.join().unwrap();
let v = j3.join().unwrap();
assert!(v == 1 || v == 2); // relies on thread 3 going last
}
}
pub fn test_simple_release() {
static SYNC: AtomicUsize = AtomicUsize::new(0);
let mut a = 0u32;
let b = &mut a as *mut u32;
let c = EvilSend(b);
unsafe {
let j1 = spawn(move || {
let c = c; // avoid field capturing
*c.0 = 1;
SYNC.store(1, Ordering::Release);
});
let j2 = spawn(move || {
let c = c; // avoid field capturing
if SYNC.load(Ordering::Acquire) == 1 { *c.0 } else { 0 }
});
j1.join().unwrap();
assert_eq!(j2.join().unwrap(), 1); // relies on thread 2 going last
}
}
fn test_local_variable_lazy_write() {
static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
// Create the local variable, and initialize it.
// This write happens before the thread is spanwed, so there is no data race.
let mut val: u8 = 0;
let t1 = std::thread::spawn(|| {
while P.load(Ordering::Relaxed).is_null() {
std::hint::spin_loop();
}
unsafe {
// Initialize `*P`.
let ptr = P.load(Ordering::Relaxed);
*ptr = 127;
}
});
// Actually generate memory for the local variable.
// This is the time its value is actually written to memory:
// that's *after* the thread above was spawned!
// This may hence look like a data race wrt the access in the thread above.
P.store(std::ptr::addr_of_mut!(val), Ordering::Relaxed);
// Wait for the thread to be done.
t1.join().unwrap();
// Read initialized value.
assert_eq!(val, 127);
}
// This test coverse the case where the non-atomic access come first.
fn test_read_read_race1() {
let a = AtomicU16::new(0);
thread::scope(|s| {
s.spawn(|| {
let ptr = &a as *const AtomicU16 as *mut u16;
unsafe { ptr.read() };
});
s.spawn(|| {
thread::yield_now();
a.load(Ordering::SeqCst);
});
});
}
// This test coverse the case where the atomic access come first.
fn test_read_read_race2() {
let a = AtomicU16::new(0);
thread::scope(|s| {
s.spawn(|| {
a.load(Ordering::SeqCst);
});
s.spawn(|| {
thread::yield_now();
let ptr = &a as *const AtomicU16 as *mut u16;
unsafe { ptr.read() };
});
});
}
fn mixed_size_read_read() {
fn convert(a: &AtomicU16) -> &[AtomicU8; 2] {
unsafe { std::mem::transmute(a) }
}
let a = AtomicU16::new(0);
let a16 = &a;
let a8 = convert(a16);
// Just two different-sized atomic reads without any writes are fine.
thread::scope(|s| {
s.spawn(|| {
a16.load(Ordering::SeqCst);
});
s.spawn(|| {
a8[0].load(Ordering::SeqCst);
});
});
}
fn failing_rmw_is_read() {
let a = AtomicUsize::new(0);
thread::scope(|s| {
s.spawn(|| unsafe {
// Non-atomic read.
let _val = *(&a as *const AtomicUsize).cast::<usize>();
});
s.spawn(|| {
// RMW that will fail.
// This is not considered a write, so there is no data race here.
a.compare_exchange(1, 2, Ordering::SeqCst, Ordering::SeqCst).unwrap_err();
});
});
}
pub fn main() {
test_fence_sync();
test_multiple_reads();
test_rmw_no_block();
test_simple_release();
test_local_variable_lazy_write();
test_read_read_race1();
test_read_read_race2();
mixed_size_read_read();
failing_rmw_is_read();
}
|