From 074e20eb7b3f36846c6c3a182060a5b82da46498 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Sat, 29 Jun 2019 13:17:15 -0500 Subject: [PATCH 1/8] Add intptrcast test for explicit casts --- tests/run-pass-noseed/intptrcast.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/run-pass-noseed/intptrcast.rs b/tests/run-pass-noseed/intptrcast.rs index 40b21f9a47..919083f98d 100644 --- a/tests/run-pass-noseed/intptrcast.rs +++ b/tests/run-pass-noseed/intptrcast.rs @@ -1,4 +1,7 @@ // compile-flags: -Zmiri-seed=0000000000000000 +fn transmute_ptr_to_int(x: *const T) -> usize { + unsafe { std::mem::transmute::<*const T, usize>(x) * 1 } +} fn main() { // Some casting-to-int with arithmetic. @@ -11,4 +14,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); } From 39d383d9e729965b406009033977cb942dc899b3 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 3 Jul 2019 13:28:30 -0500 Subject: [PATCH 2/8] Update rust-version --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 1733872825..9e4ddf2aca 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -7e08576e4276a97b523c25bfd196d419c39c7b87 +088b987307b91612ab164026e1dcdd0129fdb62b From 8dfb278ac5ad47f7fac3d161ebb30d9efb9246a3 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 3 Jul 2019 13:42:01 -0500 Subject: [PATCH 3/8] Fix explicit cast test --- tests/run-pass-noseed/intptrcast.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/run-pass-noseed/intptrcast.rs b/tests/run-pass-noseed/intptrcast.rs index 919083f98d..1b5251c911 100644 --- a/tests/run-pass-noseed/intptrcast.rs +++ b/tests/run-pass-noseed/intptrcast.rs @@ -1,11 +1,13 @@ // compile-flags: -Zmiri-seed=0000000000000000 -fn transmute_ptr_to_int(x: *const T) -> usize { - unsafe { std::mem::transmute::<*const T, usize>(x) * 1 } + +// This returns a miri pointer at type usize, if the argument is a proper pointer +fn transmute_ptr_to_int(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; From 802dcb7f890cf1e05a98fd21466f74d9c9b5b748 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 5 Jul 2019 00:06:41 +0200 Subject: [PATCH 4/8] temporarily disable ptr_offset, maybe that helps --- tests/run-pass-noseed/ptr_int_casts.rs | 1 + tests/{run-pass => run-pass-noseed}/ptr_offset.rs | 2 ++ 2 files changed, 3 insertions(+) rename tests/{run-pass => run-pass-noseed}/ptr_offset.rs (85%) diff --git a/tests/run-pass-noseed/ptr_int_casts.rs b/tests/run-pass-noseed/ptr_int_casts.rs index c279024f35..ebf65ac3fe 100644 --- a/tests/run-pass-noseed/ptr_int_casts.rs +++ b/tests/run-pass-noseed/ptr_int_casts.rs @@ -1,3 +1,4 @@ +// FIXME move this to run-pass, it should work with intptrcast. use std::mem; use std::ptr; diff --git a/tests/run-pass/ptr_offset.rs b/tests/run-pass-noseed/ptr_offset.rs similarity index 85% rename from tests/run-pass/ptr_offset.rs rename to tests/run-pass-noseed/ptr_offset.rs index 1c7f0eb717..a836e02812 100644 --- a/tests/run-pass/ptr_offset.rs +++ b/tests/run-pass-noseed/ptr_offset.rs @@ -1,3 +1,5 @@ +// FIXME move this to run-pass, it should work with intptrcast. + fn f() -> i32 { 42 } fn main() { From aad5fde70316f87f023b918001f02db21d7ff0e1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 5 Jul 2019 19:21:21 +0200 Subject: [PATCH 5/8] fix deallocating/reallocating with integer pointers --- src/shims/foreign_items.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index c9b10e02c9..540e3fc964 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -50,8 +50,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ) -> InterpResult<'tcx> { let this = self.eval_context_mut(); if !ptr.is_null_ptr(this) { + 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())), From 6c58d40a8d42650332a4c68b44ea3827263c395f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 5 Jul 2019 19:21:42 +0200 Subject: [PATCH 6/8] temporarily disable validation for 'cargo miri test' testing --- test-cargo-miri/run-test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-cargo-miri/run-test.py b/test-cargo-miri/run-test.py index 73515c74e4..a9aba008e9 100755 --- a/test-cargo-miri/run-test.py +++ b/test-cargo-miri/run-test.py @@ -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)", From 9b58492df179be10cc2004ad0fe0616cdfd9b505 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 5 Jul 2019 19:22:22 +0200 Subject: [PATCH 7/8] temporarily disable intptrcast advanced testing on Windows --- tests/compiletest.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/compiletest.rs b/tests/compiletest.rs index 1d32d3a732..b31be0a4f3 100644 --- a/tests/compiletest.rs +++ b/tests/compiletest.rs @@ -83,6 +83,7 @@ fn miri_pass(path: &str, target: &str, opt: bool, noseed: bool) { flags.push("-Zmir-opt-level=3".to_owned()); } else if !noseed { // Run with intptrcast. Avoid test matrix explosion by doing either this or opt-level=3. + #[cfg(not(windows))] // FIXME re-enable on Windows flags.push("-Zmiri-seed=".to_owned()); } From 4d76dd1f09db1b5a69435d3aa85c4c2f0a5887c1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 5 Jul 2019 21:26:58 +0200 Subject: [PATCH 8/8] temporarily disable validation on Windows --- src/eval.rs | 10 +++++++++- tests/compiletest.rs | 5 +++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index a1157af0e3..91a563fa56 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -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)?; diff --git a/tests/compiletest.rs b/tests/compiletest.rs index b31be0a4f3..076deca6a3 100644 --- a/tests/compiletest.rs +++ b/tests/compiletest.rs @@ -83,7 +83,6 @@ fn miri_pass(path: &str, target: &str, opt: bool, noseed: bool) { flags.push("-Zmir-opt-level=3".to_owned()); } else if !noseed { // Run with intptrcast. Avoid test matrix explosion by doing either this or opt-level=3. - #[cfg(not(windows))] // FIXME re-enable on Windows flags.push("-Zmiri-seed=".to_owned()); } @@ -113,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: &[&()]) {