Auto merge of #1177 - RalfJung:debug-assert, r=RalfJung

Make sure we evaluate debug assertions in local crate and libstd

Fixes https://github.com/rust-lang/miri/issues/1126
This commit is contained in:
bors 2020-02-16 13:04:06 +00:00
commit daaf9f7e28
6 changed files with 34 additions and 7 deletions

View file

@ -62,5 +62,5 @@ pub use crate::stacked_borrows::{
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
/// set per default, for maximal validation power.
pub fn miri_default_args() -> &'static [&'static str] {
&["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri"]
&["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri", "-Cdebug-assertions=on"]
}

View file

@ -1,8 +1,14 @@
//error-pattern: invalid use of NULL pointer
#![feature(intrinsics)]
// Directly call intrinsic to avoid debug assertions in libstd
extern "rust-intrinsic" {
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
}
fn main() {
let mut data = [0u16; 4];
let ptr = &mut data[0] as *mut u16;
// Even copying 0 elements from NULL should error.
unsafe { ptr.copy_from(std::ptr::null(), 0); }
unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); }
}

View file

@ -1,12 +1,16 @@
#![feature(core_intrinsics)]
//error-pattern: copy_nonoverlapping called on overlapping ranges
#![feature(intrinsics)]
// Directly call intrinsic to avoid debug assertions in libstd
extern "rust-intrinsic" {
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
}
fn main() {
let mut data = [0u8; 16];
unsafe {
let a = data.as_mut_ptr();
let b = a.wrapping_offset(1) as *mut _;
std::ptr::copy_nonoverlapping(a, b, 2);
copy_nonoverlapping(a, b, 2);
}
}

View file

@ -1,8 +1,14 @@
//error-pattern: tried to access memory with alignment 1, but alignment 2 is required
#![feature(intrinsics)]
// Directly call intrinsic to avoid debug assertions in libstd
extern "rust-intrinsic" {
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
}
fn main() {
let mut data = [0u16; 8];
let ptr = (&mut data[0] as *mut u16 as *mut u8).wrapping_add(1) as *mut u16;
// Even copying 0 elements to something unaligned should error
unsafe { ptr.copy_from(&data[5], 0); }
unsafe { copy_nonoverlapping(&data[5], ptr, 0); }
}

View file

@ -1,4 +1,5 @@
// ignore-windows: Unwind panicking does not currently work on Windows
// normalize-stderr-test "[^ ]*libcore/macros/mod.rs[0-9:]*" -> "$$LOC"
#![feature(never_type)]
#![allow(const_err)]
use std::panic::{catch_unwind, AssertUnwindSafe};
@ -11,7 +12,6 @@ thread_local! {
}
struct DropTester;
impl Drop for DropTester {
fn drop(&mut self) {
DROPPED.with(|c| {
@ -61,6 +61,11 @@ fn main() {
test(|_old_val| { let _val = [0, 1, 2][4]; loop {} });
test(|_old_val| { let _val = 1/0; loop {} });
// Assertion and debug assertion
test(|_old_val| { assert!(false); loop {} });
test(|_old_val| { debug_assert!(false); loop {} });
test(|_old_val| { unsafe { (1 as *const i32).read() }; loop {} }); // trigger debug-assertion in libstd
// Cleanup: reset to default hook.
drop(std::panic::take_hook());

View file

@ -16,4 +16,10 @@ thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'
Caught panic message (String): index out of bounds: the len is 3 but the index is 4
thread 'main' panicked at 'attempt to divide by zero', $DIR/catch_panic.rs:62:34
Caught panic message (String): attempt to divide by zero
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:65:23
Caught panic message (&str): assertion failed: false
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:66:23
Caught panic message (&str): assertion failed: false
thread 'main' panicked at 'attempt to copy from unaligned or null pointer', $LOC
Caught panic message (String): attempt to copy from unaligned or null pointer
Success!