about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/0weak_memory/consistency_sc.rs
blob: cb8535b8ad74b731f2727832df8e92bd4b64c063 (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//@compile-flags: -Zmiri-ignore-leaks -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
// This test's runtime explodes if the GC interval is set to 1 (which we do in CI), so we
// override it internally back to the default frequency.
//@compile-flags: -Zmiri-provenance-gc=10000

// The following tests check whether our weak memory emulation produces
// any inconsistent execution outcomes
// This file here focuses on SC accesses and fences.

use std::sync::atomic::Ordering::*;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering, fence};
use std::thread::spawn;

// We can't create static items because we need to run each test
// multiple times
fn static_atomic(val: i32) -> &'static AtomicI32 {
    Box::leak(Box::new(AtomicI32::new(val)))
}
fn static_atomic_bool(val: bool) -> &'static AtomicBool {
    Box::leak(Box::new(AtomicBool::new(val)))
}

/// Spins until it acquires a pre-determined value.
fn spin_until_i32(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
    while loc.load(ord) != val {
        std::hint::spin_loop();
    }
    val
}

/// Spins until it acquires a pre-determined boolean.
fn spin_until_bool(loc: &AtomicBool, ord: Ordering, val: bool) -> bool {
    while loc.load(ord) != val {
        std::hint::spin_loop();
    }
    val
}

// Test case SB taken from Repairing Sequential Consistency in C/C++11
// by Lahav et al.
// https://plv.mpi-sws.org/scfix/paper.pdf
fn test_sc_store_buffering() {
    let x = static_atomic(0);
    let y = static_atomic(0);

    let j1 = spawn(move || {
        x.store(1, SeqCst);
        y.load(SeqCst)
    });

    let j2 = spawn(move || {
        y.store(1, SeqCst);
        x.load(SeqCst)
    });

    let a = j1.join().unwrap();
    let b = j2.join().unwrap();

    assert_ne!((a, b), (0, 0));
}

// Test case by @SabrinaJewson
// https://github.com/rust-lang/miri/issues/2301#issuecomment-1221502757
// Demonstrating C++20 SC access changes
fn test_iriw_sc_rlx() {
    let x = static_atomic_bool(false);
    let y = static_atomic_bool(false);

    let a = spawn(move || x.store(true, Relaxed));
    let b = spawn(move || y.store(true, Relaxed));
    let c = spawn(move || {
        spin_until_bool(x, SeqCst, true);
        y.load(SeqCst)
    });
    let d = spawn(move || {
        spin_until_bool(y, SeqCst, true);
        x.load(SeqCst)
    });

    a.join().unwrap();
    b.join().unwrap();
    let c = c.join().unwrap();
    let d = d.join().unwrap();

    assert!(c || d);
}

// Similar to `test_iriw_sc_rlx` but with fences instead of SC accesses.
fn test_cpp20_sc_fence_fix() {
    let x = static_atomic_bool(false);
    let y = static_atomic_bool(false);

    let thread1 = spawn(|| {
        let a = x.load(Relaxed);
        fence(SeqCst);
        let b = y.load(Relaxed);
        (a, b)
    });

    let thread2 = spawn(|| {
        x.store(true, Relaxed);
    });
    let thread3 = spawn(|| {
        y.store(true, Relaxed);
    });

    let thread4 = spawn(|| {
        let c = y.load(Relaxed);
        fence(SeqCst);
        let d = x.load(Relaxed);
        (c, d)
    });

    let (a, b) = thread1.join().unwrap();
    thread2.join().unwrap();
    thread3.join().unwrap();
    let (c, d) = thread4.join().unwrap();
    let bad = a == true && b == false && c == true && d == false;
    assert!(!bad);
}

// https://plv.mpi-sws.org/scfix/paper.pdf
// 2.2 Second Problem: SC Fences are Too Weak
fn test_cpp20_rwc_syncs() {
    /*
    int main() {
        atomic_int x = 0;
        atomic_int y = 0;
        {{{ x.store(1,mo_relaxed);
        ||| { r1=x.load(mo_relaxed).readsvalue(1);
              fence(mo_seq_cst);
              r2=y.load(mo_relaxed); }
        ||| { y.store(1,mo_relaxed);
              fence(mo_seq_cst);
              r3=x.load(mo_relaxed); }
        }}}
        return 0;
    }
    */
    let x = static_atomic(0);
    let y = static_atomic(0);

    let j1 = spawn(move || {
        x.store(1, Relaxed);
    });

    let j2 = spawn(move || {
        spin_until_i32(&x, Relaxed, 1);
        fence(SeqCst);
        y.load(Relaxed)
    });

    let j3 = spawn(move || {
        y.store(1, Relaxed);
        fence(SeqCst);
        x.load(Relaxed)
    });

    j1.join().unwrap();
    let b = j2.join().unwrap();
    let c = j3.join().unwrap();

    assert!((b, c) != (0, 0));
}

/// This checks that the *last* thing the SC fence does is act like a release fence.
/// See <https://github.com/rust-lang/miri/pull/4057#issuecomment-2522296601>.
/// Test by Ori Lahav.
fn test_sc_fence_release() {
    let x = static_atomic(0);
    let y = static_atomic(0);
    let z = static_atomic(0);
    let k = static_atomic(0);

    let j1 = spawn(move || {
        x.store(1, Relaxed);
        fence(SeqCst);
        k.store(1, Relaxed);
    });
    let j2 = spawn(move || {
        y.store(1, Relaxed);
        fence(SeqCst);
        z.store(1, Relaxed);
    });

    let j3 = spawn(move || {
        let kval = k.load(Acquire); // bad case: loads 1
        let yval = y.load(Relaxed); // bad case: loads 0
        (kval, yval)
    });
    let j4 = spawn(move || {
        let zval = z.load(Acquire); // bad case: loads 1
        let xval = x.load(Relaxed); // bad case: loads 0
        (zval, xval)
    });

    j1.join().unwrap();
    j2.join().unwrap();
    let (kval, yval) = j3.join().unwrap();
    let (zval, xval) = j4.join().unwrap();

    let bad = kval == 1 && yval == 0 && zval == 1 && xval == 0;
    assert!(!bad);
}

/// Test that SC fences and accesses sync correctly with each other.
/// Test by Ori Lahav.
fn test_sc_fence_access() {
    /*
        Wx1 sc
        Ry0 sc
        ||
        Wy1 rlx
        SC-fence
        Rx0 rlx
    */
    let x = static_atomic(0);
    let y = static_atomic(0);

    let j1 = spawn(move || {
        x.store(1, SeqCst);
        y.load(SeqCst)
    });
    let j2 = spawn(move || {
        y.store(1, Relaxed);
        fence(SeqCst);
        // If this sees a 0, the fence must have been *before* the x.store(1).
        x.load(Relaxed)
    });

    let yval = j1.join().unwrap();
    let xval = j2.join().unwrap();
    let bad = yval == 0 && xval == 0;
    assert!(!bad);
}

/// Test that SC fences and accesses sync correctly with each other
/// when mediated by a release-acquire pair.
/// Test by Ori Lahav (https://github.com/rust-lang/miri/pull/4057#issuecomment-2525268730).
fn test_sc_fence_access_relacq() {
    let x = static_atomic(0);
    let y = static_atomic(0);
    let z = static_atomic(0);

    let j1 = spawn(move || {
        x.store(1, SeqCst);
        y.load(SeqCst) // bad case: loads 0
    });
    let j2 = spawn(move || {
        y.store(1, Relaxed);
        z.store(1, Release)
    });
    let j3 = spawn(move || {
        let zval = z.load(Acquire); // bad case: loads 1
        // If we see 1 here, the rel-acq pair makes the fence happen after the z.store(1).
        fence(SeqCst);
        // If this sees a 0, the fence must have been *before* the x.store(1).
        let xval = x.load(Relaxed); // bad case: loads 0
        (zval, xval)
    });

    let yval = j1.join().unwrap();
    j2.join().unwrap();
    let (zval, xval) = j3.join().unwrap();
    let bad = yval == 0 && zval == 1 && xval == 0;
    assert!(!bad);
}

/// A test that involves multiple SC fences and accesses.
/// Test by Ori Lahav (https://github.com/rust-lang/miri/pull/4057#issuecomment-2525268730).
fn test_sc_multi_fence() {
    let x = static_atomic(0);
    let y = static_atomic(0);
    let z = static_atomic(0);

    let j1 = spawn(move || {
        x.store(1, SeqCst);
        y.load(SeqCst) // bad case: loads 0
    });
    let j2 = spawn(move || {
        y.store(1, Relaxed);
        // In the bad case this fence is *after* the j1 y.load, since
        // otherwise that load would pick up the 1 we just stored.
        fence(SeqCst);
        z.load(Relaxed) // bad case: loads 0
    });
    let j3 = spawn(move || {
        z.store(1, Relaxed);
    });
    let j4 = spawn(move || {
        let zval = z.load(Relaxed); // bad case: loads 1
        // In the bad case this fence is *after* the one above since
        // otherwise, the j2 load of z would load 1.
        fence(SeqCst);
        // Since that fence is in turn after the j1 y.load, our fence is
        // after the j1 x.store, which means we must pick up that store.
        let xval = x.load(Relaxed); // bad case: loads 0
        (zval, xval)
    });

    let yval = j1.join().unwrap();
    let zval1 = j2.join().unwrap();
    j3.join().unwrap();
    let (zval2, xval) = j4.join().unwrap();
    let bad = yval == 0 && zval1 == 0 && zval2 == 1 && xval == 0;
    assert!(!bad);
}

fn test_sc_relaxed() {
    /*
    y:=1 rlx
    Fence sc
    a:=x rlx
    Fence acq
    b:=z rlx // 0
    ||
    z:=1 rlx
    x:=1 sc
    c:=y sc // 0
    */

    let x = static_atomic(0);
    let y = static_atomic(0);
    let z = static_atomic(0);

    let j1 = spawn(move || {
        y.store(1, Relaxed);
        fence(SeqCst);
        // If the relaxed load here is removed, then the "bad" behavior becomes allowed
        // by C++20 (and by RC11 / scfix as well).
        let _a = x.load(Relaxed);
        fence(Acquire);
        // If we see 0 here this means in some sense we are "before" the store to z below.
        let b = z.load(Relaxed);
        b
    });
    let j2 = spawn(move || {
        z.store(1, Relaxed);
        x.store(1, SeqCst);
        // If we see 0 here, this means in some sense we are "before" the store to y above.
        let c = y.load(SeqCst);
        c
    });

    let b = j1.join().unwrap();
    let c = j2.join().unwrap();
    let bad = b == 0 && c == 0;
    assert!(!bad);
}

pub fn main() {
    for _ in 0..32 {
        test_sc_store_buffering();
        test_iriw_sc_rlx();
        test_cpp20_sc_fence_fix();
        test_cpp20_rwc_syncs();
        test_sc_fence_release();
        test_sc_fence_access();
        test_sc_fence_access_relacq();
        test_sc_multi_fence();
        test_sc_relaxed();
    }
}