Auto merge of #803 - christianpoveda:intptrcast-explicit-casts, r=RalfJung

Add tests for Intptrcast when doing explicit casts

r? @RalfJung
This commit is contained in:
bors 2019-07-05 20:46:34 +00:00
commit 285fc0d70e
8 changed files with 36 additions and 8 deletions

View File

@ -1 +1 @@
7e08576e4276a97b523c25bfd196d419c39c7b87
088b987307b91612ab164026e1dcdd0129fdb62b

View File

@ -35,8 +35,16 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
Evaluator::new(),
);
// FIXME(https://github.com/rust-lang/miri/pull/803): no validation on Windows.
let target_os = ecx.tcx.tcx.sess.target.target.target_os.to_lowercase();
let validate = if target_os == "windows" {
false
} else {
config.validate
};
// FIXME: InterpretCx::new should take an initial MemoryExtra
ecx.memory_mut().extra = MemoryExtra::new(config.seed.map(StdRng::seed_from_u64), config.validate);
ecx.memory_mut().extra = MemoryExtra::new(config.seed.map(StdRng::seed_from_u64), validate);
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
let main_mir = ecx.load_mir(main_instance.def)?;

View File

@ -50,8 +50,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
if !this.is_null(ptr)? {
let ptr = this.force_ptr(ptr)?;
this.memory_mut().deallocate(
ptr.to_ptr()?,
ptr,
None,
MiriMemoryKind::C.into(),
)?;
@ -78,7 +79,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Ok(Scalar::Ptr(new_ptr))
}
} else {
let old_ptr = old_ptr.to_ptr()?;
let old_ptr = this.force_ptr(old_ptr)?;
let memory = this.memory_mut();
let old_size = Size::from_bytes(memory.get(old_ptr.alloc_id)?.bytes.len() as u64);
if new_size == 0 {
@ -234,7 +235,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::Ptr(ptr), dest)?;
}
"__rust_dealloc" => {
let ptr = this.read_scalar(args[0])?.to_ptr()?;
let ptr = this.read_scalar(args[0])?.not_undef()?;
let old_size = this.read_scalar(args[1])?.to_usize(this)?;
let align = this.read_scalar(args[2])?.to_usize(this)?;
if old_size == 0 {
@ -243,6 +244,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
if !align.is_power_of_two() {
return err!(HeapAllocNonPowerOfTwoAlignment(align));
}
let ptr = this.force_ptr(ptr)?;
this.memory_mut().deallocate(
ptr,
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),

View File

@ -52,8 +52,9 @@ def test_cargo_miri_run():
)
def test_cargo_miri_test():
# FIXME: enable validation again, once that no longer conflicts with intptrcast
test("cargo miri test",
cargo_miri("test") + ["--", "-Zmiri-seed=feed"],
cargo_miri("test") + ["--", "-Zmiri-seed=feed", "-Zmiri-disable-validation"],
"test.stdout.ref", "test.stderr.ref"
)
test("cargo miri test (with filter)",

View File

@ -112,7 +112,9 @@ fn run_pass_miri(opt: bool) {
}
fn compile_fail_miri(opt: bool) {
compile_fail("tests/compile-fail", &get_target(), opt);
if !cfg!(windows) { // FIXME re-enable on Windows
compile_fail("tests/compile-fail", &get_target(), opt);
}
}
fn test_runner(_tests: &[&()]) {

View File

@ -1,8 +1,13 @@
// compile-flags: -Zmiri-seed=0000000000000000
// This returns a miri pointer at type usize, if the argument is a proper pointer
fn transmute_ptr_to_int<T>(x: *const T) -> usize {
unsafe { std::mem::transmute(x) }
}
fn main() {
// Some casting-to-int with arithmetic.
let x = &42 as *const i32 as usize;
let x = &42 as *const i32 as usize;
let y = x * 2;
assert_eq!(y, x + x);
let z = y as u8 as usize;
@ -11,4 +16,11 @@ fn main() {
// Pointer string formatting! We can't check the output as it changes when libstd changes,
// but we can make sure Miri does not error.
format!("{:?}", &mut 13 as *mut _);
// Check that intptrcast is triggered for explicit casts and that it is consistent with
// transmuting.
let a: *const i32 = &42;
let b = transmute_ptr_to_int(a) as u8;
let c = a as usize as u8;
assert_eq!(b, c);
}

View File

@ -1,3 +1,4 @@
// FIXME move this to run-pass, it should work with intptrcast.
use std::mem;
use std::ptr;

View File

@ -1,3 +1,5 @@
// FIXME move this to run-pass, it should work with intptrcast.
fn f() -> i32 { 42 }
fn main() {