add option to disable alignment checks

This commit is contained in:
Ralf Jung 2020-04-13 17:51:22 +02:00
parent fbbd4428d0
commit fd8beaf5c4
5 changed files with 36 additions and 3 deletions

View file

@ -128,6 +128,7 @@ fn main() {
// Parse our arguments and split them across `rustc` and `miri`.
let mut validate = true;
let mut stacked_borrows = true;
let mut check_alignment = true;
let mut communicate = false;
let mut ignore_leaks = false;
let mut seed: Option<u64> = None;
@ -152,6 +153,9 @@ fn main() {
"-Zmiri-disable-stacked-borrows" => {
stacked_borrows = false;
}
"-Zmiri-disable-alignment-check" => {
check_alignment = false;
}
"-Zmiri-disable-isolation" => {
communicate = true;
}
@ -243,6 +247,7 @@ fn main() {
let miri_config = miri::MiriConfig {
validate,
stacked_borrows,
check_alignment,
communicate,
ignore_leaks,
excluded_env_vars,

View file

@ -19,6 +19,8 @@ pub struct MiriConfig {
pub validate: bool,
/// Determines if Stacked Borrows is enabled.
pub stacked_borrows: bool,
/// Determines if alignment checking is enabled.
pub check_alignment: bool,
/// Determines if communication with the host environment is enabled.
pub communicate: bool,
/// Determines if memory leaks should be ignored.
@ -40,6 +42,7 @@ impl Default for MiriConfig {
MiriConfig {
validate: true,
stacked_borrows: true,
check_alignment: true,
communicate: false,
ignore_leaks: false,
excluded_env_vars: vec![],
@ -72,6 +75,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
config.stacked_borrows,
config.tracked_pointer_tag,
config.tracked_alloc_id,
config.check_alignment,
),
);
// Complete initialization.

View file

@ -118,10 +118,19 @@ pub struct MemoryExtra {
/// An allocation ID to report when it is being allocated
/// (helps for debugging memory leaks).
tracked_alloc_id: Option<AllocId>,
/// Controls whether alignment of memory accesses is being checked.
check_alignment: bool,
}
impl MemoryExtra {
pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option<PtrId>, tracked_alloc_id: Option<AllocId>) -> Self {
pub fn new(
rng: StdRng,
stacked_borrows: bool,
tracked_pointer_tag: Option<PtrId>,
tracked_alloc_id: Option<AllocId>,
check_alignment: bool,
) -> Self {
let stacked_borrows = if stacked_borrows {
Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag))))
} else {
@ -133,6 +142,7 @@ impl MemoryExtra {
extern_statics: FxHashMap::default(),
rng: RefCell::new(rng),
tracked_alloc_id,
check_alignment,
}
}
@ -299,7 +309,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
const GLOBAL_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Global);
const CHECK_ALIGN: bool = true;
#[inline(always)]
fn enforce_alignment(memory_extra: &MemoryExtra) -> bool {
memory_extra.check_alignment
}
#[inline(always)]
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {

View file

@ -2,7 +2,7 @@ fn main() {
// miri always gives allocations the worst possible alignment, so a `u8` array is guaranteed
// to be at the virtual location 1 (so one byte offset from the ultimate alignemnt location 0)
let mut x = [0u8; 20];
let x_ptr: *mut u8 = &mut x[0];
let x_ptr: *mut u8 = x.as_mut_ptr();
let y_ptr = x_ptr as *mut u64;
unsafe {
*y_ptr = 42; //~ ERROR accessing memory with alignment 1, but alignment

View file

@ -0,0 +1,11 @@
// compile-flags: -Zmiri-disable-alignment-check
fn main() {
let mut x = [0u8; 20];
let x_ptr: *mut u8 = x.as_mut_ptr();
// At least one of these is definitely unaligned.
unsafe {
*(x_ptr as *mut u64) = 42;
*(x_ptr.add(1) as *mut u64) = 42;
}
}