Skip to content

Commit 27b0444

Browse files
committed
add some Miri-only tests
1 parent ac66baa commit 27b0444

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

library/alloc/src/sync/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,22 @@ fn test_arc_cyclic_two_refs() {
618618
assert_eq!(Arc::strong_count(&two_refs), 3);
619619
assert_eq!(Arc::weak_count(&two_refs), 2);
620620
}
621+
622+
/// Test for Arc::drop bug (https://github.com/rust-lang/rust/issues/55005)
623+
#[test]
624+
#[cfg(miri)] // relies on Stacked Borrows in Miri
625+
fn arc_drop_dereferenceable_race() {
626+
// The bug seems to take up to 700 iterations to reproduce with most seeds (tested 0-9).
627+
for _ in 0..750 {
628+
let arc_1 = Arc::new(());
629+
let arc_2 = arc_1.clone();
630+
let thread = thread::spawn(|| drop(arc_2));
631+
// Spin a bit; makes the race more likely to appear
632+
let mut i = 0;
633+
while i < 256 {
634+
i += 1;
635+
}
636+
drop(arc_1);
637+
thread.join().unwrap();
638+
}
639+
}

library/std/src/thread/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,22 @@ fn test_scoped_threads_nll() {
329329
let x = 42_u8;
330330
foo(&x);
331331
}
332+
333+
// Regression test for https://github.com/rust-lang/rust/issues/98498.
334+
#[test]
335+
#[cfg(miri)] // relies on Miri's data race detector
336+
fn scope_join_race() {
337+
for _ in 0..100 {
338+
let a_bool = AtomicBool::new(false);
339+
340+
thread::scope(|s| {
341+
for _ in 0..5 {
342+
s.spawn(|| a_bool.load(Ordering::Relaxed));
343+
}
344+
345+
for _ in 0..5 {
346+
s.spawn(|| a_bool.load(Ordering::Relaxed));
347+
}
348+
});
349+
}
350+
}

0 commit comments

Comments
 (0)