Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miri: run panic-catching tests in liballoc #66662

Merged
merged 4 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/liballoc/tests/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn assert_covariance() {
// Destructors must be called exactly once per element.
// FIXME: re-enable emscripten once it can unwind again
#[test]
#[cfg(not(any(miri, target_os = "emscripten")))] // Miri does not support catching panics
#[cfg(not(target_os = "emscripten"))]
dtolnay marked this conversation as resolved.
Show resolved Hide resolved
fn panic_safe() {
use std::cmp;
use std::panic::{self, AssertUnwindSafe};
Expand Down Expand Up @@ -376,7 +376,10 @@ fn panic_safe() {
}
let mut rng = thread_rng();
const DATASZ: usize = 32;
#[cfg(not(miri))] // Miri is too slow
const NTEST: usize = 10;
#[cfg(miri)]
const NTEST: usize = 1;

// don't use 0 in the data -- we want to catch the zeroed-out case.
let data = (1..=DATASZ).collect::<Vec<_>>();
Expand Down
28 changes: 18 additions & 10 deletions src/liballoc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::mem;
use std::panic;
use std::rc::Rc;
use std::sync::atomic::{Ordering::Relaxed, AtomicUsize};
use std::thread;

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

struct Canary {
count: Arc<AtomicUsize>,
Expand Down Expand Up @@ -1446,15 +1443,14 @@ fn test_box_slice_clone_panics() {
panics: true,
};

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

// When panic is cloned, +3.
xs.clone();
})
.join()
.unwrap_err();

// Total = 8
Expand Down Expand Up @@ -1566,7 +1562,7 @@ macro_rules! test {
}

let v = $input.to_owned();
let _ = thread::spawn(move || {
let _ = std::panic::catch_unwind(move || {
let mut v = v;
let mut panic_countdown = panic_countdown;
v.$func(|a, b| {
Expand All @@ -1577,7 +1573,7 @@ macro_rules! test {
panic_countdown -= 1;
a.cmp(b)
})
}).join();
});

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

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

let mut rng = thread_rng();

for len in (1..20).chain(70..MAX_LEN) {
for &modulus in &[5, 20, 50] {
#[cfg(not(miri))] // Miri is too slow
let lens = (1..20).chain(70..MAX_LEN);
#[cfg(not(miri))] // Miri is too slow
let moduli = &[5, 20, 50];

#[cfg(miri)]
let lens = (1..13);
#[cfg(miri)]
let moduli = &[10];

for len in lens {
for &modulus in moduli {
for &has_runs in &[false, true] {
let mut input = (0..len)
.map(|id| {
Expand Down Expand Up @@ -1643,6 +1648,9 @@ fn panic_safe() {
}
}
}

// Set default panic hook again.
drop(panic::take_hook());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the global panic hook in a test seems like a bad idea anyway as it affects other tests running in parallel, but this way at least we do not affect tests running later.

}

#[test]
Expand Down
5 changes: 2 additions & 3 deletions src/liballoc/tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,10 +944,9 @@ fn drain_filter_complex() {
}
}

// Miri does not support catching panics
// FIXME: re-enable emscripten once it can unwind again
#[test]
#[cfg(not(any(miri, target_os = "emscripten")))]
#[cfg(not(target_os = "emscripten"))]
fn drain_filter_consumed_panic() {
use std::rc::Rc;
use std::sync::Mutex;
Expand Down Expand Up @@ -999,7 +998,7 @@ fn drain_filter_consumed_panic() {

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