From 1e91e4e20a5d6e50e281c6e87a3a61cc735e266f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 22 Nov 2019 23:11:56 +0100 Subject: [PATCH 1/4] enable panic-catching tests in Miri --- src/liballoc/tests/binary_heap.rs | 2 +- src/liballoc/tests/vec.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/liballoc/tests/binary_heap.rs b/src/liballoc/tests/binary_heap.rs index a44cf1eaf6df0..81b22ca815b25 100644 --- a/src/liballoc/tests/binary_heap.rs +++ b/src/liballoc/tests/binary_heap.rs @@ -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"))] fn panic_safe() { use std::cmp; use std::panic::{self, AssertUnwindSafe}; diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index 80537217697ad..58b555ecb4eee 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -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; @@ -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; From 3277209af5c3369cbf1786d25cbf48c9a131996b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 22 Nov 2019 23:21:20 +0100 Subject: [PATCH 2/4] use catch_panic instead of thread::spawn to catch panics --- src/liballoc/tests/slice.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/liballoc/tests/slice.rs b/src/liballoc/tests/slice.rs index ad2cd7c95eb8f..62b33c02cae37 100644 --- a/src/liballoc/tests/slice.rs +++ b/src/liballoc/tests/slice.rs @@ -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; @@ -1406,11 +1405,10 @@ 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 +#[cfg(not(miri))] // Miri does not support catching panics fn test_box_slice_clone_panics() { use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; - use std::thread::spawn; struct Canary { count: Arc, @@ -1446,7 +1444,7 @@ 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(); @@ -1454,7 +1452,6 @@ fn test_box_slice_clone_panics() { // When panic is cloned, +3. xs.clone(); }) - .join() .unwrap_err(); // Total = 8 @@ -1566,7 +1563,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| { @@ -1577,7 +1574,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`). @@ -1598,7 +1595,7 @@ thread_local!(static SILENCE_PANIC: Cell = Cell::new(false)); #[test] #[cfg_attr(target_os = "emscripten", ignore)] // no threads -#[cfg(not(miri))] // Miri does not support threads +#[cfg(not(miri))] // Miri does not support catching panics fn panic_safe() { let prev = panic::take_hook(); panic::set_hook(Box::new(move |info| { From 8af2f22985c672bf4cb23b615ac97af00031a2ca Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 22 Nov 2019 23:35:56 +0100 Subject: [PATCH 3/4] enable panic-catching tests in Miri --- src/liballoc/tests/slice.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/liballoc/tests/slice.rs b/src/liballoc/tests/slice.rs index 62b33c02cae37..6433cbd1842d7 100644 --- a/src/liballoc/tests/slice.rs +++ b/src/liballoc/tests/slice.rs @@ -1405,7 +1405,6 @@ 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 catching panics fn test_box_slice_clone_panics() { use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -1595,7 +1594,6 @@ thread_local!(static SILENCE_PANIC: Cell = Cell::new(false)); #[test] #[cfg_attr(target_os = "emscripten", ignore)] // no threads -#[cfg(not(miri))] // Miri does not support catching panics fn panic_safe() { let prev = panic::take_hook(); panic::set_hook(Box::new(move |info| { @@ -1606,7 +1604,12 @@ fn panic_safe() { let mut rng = thread_rng(); - for len in (1..20).chain(70..MAX_LEN) { + #[cfg(not(miri))] // Miri is too slow + let large_range = 70..MAX_LEN; + #[cfg(miri)] + let large_range = 0..0; // empty range + + for len in (1..20).chain(large_range) { for &modulus in &[5, 20, 50] { for &has_runs in &[false, true] { let mut input = (0..len) From a2299799e6193799f4d2cb546e56589c5dd587aa Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 23 Nov 2019 08:53:53 +0100 Subject: [PATCH 4/4] enable more panic-catching tests in Miri --- src/liballoc/tests/binary_heap.rs | 3 +++ src/liballoc/tests/slice.rs | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/liballoc/tests/binary_heap.rs b/src/liballoc/tests/binary_heap.rs index 81b22ca815b25..a896a1064d9e1 100644 --- a/src/liballoc/tests/binary_heap.rs +++ b/src/liballoc/tests/binary_heap.rs @@ -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::>(); diff --git a/src/liballoc/tests/slice.rs b/src/liballoc/tests/slice.rs index 6433cbd1842d7..d9707b9574078 100644 --- a/src/liballoc/tests/slice.rs +++ b/src/liballoc/tests/slice.rs @@ -1605,12 +1605,17 @@ fn panic_safe() { let mut rng = thread_rng(); #[cfg(not(miri))] // Miri is too slow - let large_range = 70..MAX_LEN; + let lens = (1..20).chain(70..MAX_LEN); + #[cfg(not(miri))] // Miri is too slow + let moduli = &[5, 20, 50]; + #[cfg(miri)] - let large_range = 0..0; // empty range + let lens = (1..13); + #[cfg(miri)] + let moduli = &[10]; - for len in (1..20).chain(large_range) { - for &modulus in &[5, 20, 50] { + for len in lens { + for &modulus in moduli { for &has_runs in &[false, true] { let mut input = (0..len) .map(|id| { @@ -1643,6 +1648,9 @@ fn panic_safe() { } } } + + // Set default panic hook again. + drop(panic::take_hook()); } #[test]