Skip to content

Add tests for Intptrcast when doing explicit casts #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 5, 2019
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7e08576e4276a97b523c25bfd196d419c39c7b87
088b987307b91612ab164026e1dcdd0129fdb62b
10 changes: 9 additions & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
8 changes: 5 additions & 3 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)?;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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())),
Expand Down
3 changes: 2 additions & 1 deletion test-cargo-miri/run-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
4 changes: 3 additions & 1 deletion tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &[&()]) {
Expand Down
14 changes: 13 additions & 1 deletion tests/run-pass-noseed/intptrcast.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions tests/run-pass-noseed/ptr_int_casts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// FIXME move this to run-pass, it should work with intptrcast.
use std::mem;
use std::ptr;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FIXME move this to run-pass, it should work with intptrcast.

fn f() -> i32 { 42 }

fn main() {
Expand Down