Skip to content

Commit cb43d82

Browse files
authored
Rollup merge of rust-lang#66662 - RalfJung:miri-test-liballoc, r=dtolnay
Miri: run panic-catching tests in liballoc I also converted two tests from using `thread::spawn(...).join()` just for catching panics, to `catch_panic`, so that Miri can run them.
2 parents 6110d3e + a229979 commit cb43d82

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/liballoc/tests/binary_heap.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ fn assert_covariance() {
347347
// Destructors must be called exactly once per element.
348348
// FIXME: re-enable emscripten once it can unwind again
349349
#[test]
350-
#[cfg(not(any(miri, target_os = "emscripten")))] // Miri does not support catching panics
350+
#[cfg(not(target_os = "emscripten"))]
351351
fn panic_safe() {
352352
use std::cmp;
353353
use std::panic::{self, AssertUnwindSafe};
@@ -376,7 +376,10 @@ fn panic_safe() {
376376
}
377377
let mut rng = thread_rng();
378378
const DATASZ: usize = 32;
379+
#[cfg(not(miri))] // Miri is too slow
379380
const NTEST: usize = 10;
381+
#[cfg(miri)]
382+
const NTEST: usize = 1;
380383

381384
// don't use 0 in the data -- we want to catch the zeroed-out case.
382385
let data = (1..=DATASZ).collect::<Vec<_>>();

src/liballoc/tests/slice.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::mem;
44
use std::panic;
55
use std::rc::Rc;
66
use std::sync::atomic::{Ordering::Relaxed, AtomicUsize};
7-
use std::thread;
87

98
use rand::{Rng, RngCore, thread_rng};
109
use rand::seq::SliceRandom;
@@ -1406,11 +1405,9 @@ fn test_box_slice_clone() {
14061405
#[test]
14071406
#[allow(unused_must_use)] // here, we care about the side effects of `.clone()`
14081407
#[cfg_attr(target_os = "emscripten", ignore)]
1409-
#[cfg(not(miri))] // Miri does not support threads
14101408
fn test_box_slice_clone_panics() {
14111409
use std::sync::Arc;
14121410
use std::sync::atomic::{AtomicUsize, Ordering};
1413-
use std::thread::spawn;
14141411

14151412
struct Canary {
14161413
count: Arc<AtomicUsize>,
@@ -1446,15 +1443,14 @@ fn test_box_slice_clone_panics() {
14461443
panics: true,
14471444
};
14481445

1449-
spawn(move || {
1446+
std::panic::catch_unwind(move || {
14501447
// When xs is dropped, +5.
14511448
let xs = vec![canary.clone(), canary.clone(), canary.clone(), panic, canary]
14521449
.into_boxed_slice();
14531450

14541451
// When panic is cloned, +3.
14551452
xs.clone();
14561453
})
1457-
.join()
14581454
.unwrap_err();
14591455

14601456
// Total = 8
@@ -1566,7 +1562,7 @@ macro_rules! test {
15661562
}
15671563

15681564
let v = $input.to_owned();
1569-
let _ = thread::spawn(move || {
1565+
let _ = std::panic::catch_unwind(move || {
15701566
let mut v = v;
15711567
let mut panic_countdown = panic_countdown;
15721568
v.$func(|a, b| {
@@ -1577,7 +1573,7 @@ macro_rules! test {
15771573
panic_countdown -= 1;
15781574
a.cmp(b)
15791575
})
1580-
}).join();
1576+
});
15811577

15821578
// Check that the number of things dropped is exactly
15831579
// what we expect (i.e., the contents of `v`).
@@ -1598,7 +1594,6 @@ thread_local!(static SILENCE_PANIC: Cell<bool> = Cell::new(false));
15981594

15991595
#[test]
16001596
#[cfg_attr(target_os = "emscripten", ignore)] // no threads
1601-
#[cfg(not(miri))] // Miri does not support threads
16021597
fn panic_safe() {
16031598
let prev = panic::take_hook();
16041599
panic::set_hook(Box::new(move |info| {
@@ -1609,8 +1604,18 @@ fn panic_safe() {
16091604

16101605
let mut rng = thread_rng();
16111606

1612-
for len in (1..20).chain(70..MAX_LEN) {
1613-
for &modulus in &[5, 20, 50] {
1607+
#[cfg(not(miri))] // Miri is too slow
1608+
let lens = (1..20).chain(70..MAX_LEN);
1609+
#[cfg(not(miri))] // Miri is too slow
1610+
let moduli = &[5, 20, 50];
1611+
1612+
#[cfg(miri)]
1613+
let lens = (1..13);
1614+
#[cfg(miri)]
1615+
let moduli = &[10];
1616+
1617+
for len in lens {
1618+
for &modulus in moduli {
16141619
for &has_runs in &[false, true] {
16151620
let mut input = (0..len)
16161621
.map(|id| {
@@ -1643,6 +1648,9 @@ fn panic_safe() {
16431648
}
16441649
}
16451650
}
1651+
1652+
// Set default panic hook again.
1653+
drop(panic::take_hook());
16461654
}
16471655

16481656
#[test]

src/liballoc/tests/vec.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,9 @@ fn drain_filter_complex() {
944944
}
945945
}
946946

947-
// Miri does not support catching panics
948947
// FIXME: re-enable emscripten once it can unwind again
949948
#[test]
950-
#[cfg(not(any(miri, target_os = "emscripten")))]
949+
#[cfg(not(target_os = "emscripten"))]
951950
fn drain_filter_consumed_panic() {
952951
use std::rc::Rc;
953952
use std::sync::Mutex;
@@ -999,7 +998,7 @@ fn drain_filter_consumed_panic() {
999998

1000999
// FIXME: Re-enable emscripten once it can catch panics
10011000
#[test]
1002-
#[cfg(not(any(miri, target_os = "emscripten")))] // Miri does not support catching panics
1001+
#[cfg(not(target_os = "emscripten"))]
10031002
fn drain_filter_unconsumed_panic() {
10041003
use std::rc::Rc;
10051004
use std::sync::Mutex;

0 commit comments

Comments
 (0)