Skip to content

Commit a72c005

Browse files
committed
Auto merge of #803 - christianpoveda:intptrcast-explicit-casts, r=RalfJung
Add tests for Intptrcast when doing explicit casts r? @RalfJung
2 parents 3525943 + 9b58492 commit a72c005

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7e08576e4276a97b523c25bfd196d419c39c7b87
1+
088b987307b91612ab164026e1dcdd0129fdb62b

src/shims/foreign_items.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5050
) -> InterpResult<'tcx> {
5151
let this = self.eval_context_mut();
5252
if !ptr.is_null_ptr(this) {
53+
let ptr = this.force_ptr(ptr)?;
5354
this.memory_mut().deallocate(
54-
ptr.to_ptr()?,
55+
ptr,
5556
None,
5657
MiriMemoryKind::C.into(),
5758
)?;
@@ -78,7 +79,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7879
Ok(Scalar::Ptr(new_ptr))
7980
}
8081
} else {
81-
let old_ptr = old_ptr.to_ptr()?;
82+
let old_ptr = this.force_ptr(old_ptr)?;
8283
let memory = this.memory_mut();
8384
let old_size = Size::from_bytes(memory.get(old_ptr.alloc_id)?.bytes.len() as u64);
8485
if new_size == 0 {
@@ -234,7 +235,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
234235
this.write_scalar(Scalar::Ptr(ptr), dest)?;
235236
}
236237
"__rust_dealloc" => {
237-
let ptr = this.read_scalar(args[0])?.to_ptr()?;
238+
let ptr = this.read_scalar(args[0])?.not_undef()?;
238239
let old_size = this.read_scalar(args[1])?.to_usize(this)?;
239240
let align = this.read_scalar(args[2])?.to_usize(this)?;
240241
if old_size == 0 {
@@ -243,6 +244,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
243244
if !align.is_power_of_two() {
244245
return err!(HeapAllocNonPowerOfTwoAlignment(align));
245246
}
247+
let ptr = this.force_ptr(ptr)?;
246248
this.memory_mut().deallocate(
247249
ptr,
248250
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),

test-cargo-miri/run-test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ def test_cargo_miri_run():
5252
)
5353

5454
def test_cargo_miri_test():
55+
# FIXME: enable validation again, once that no longer conflicts with intptrcast
5556
test("cargo miri test",
56-
cargo_miri("test") + ["--", "-Zmiri-seed=feed"],
57+
cargo_miri("test") + ["--", "-Zmiri-seed=feed", "-Zmiri-disable-validation"],
5758
"test.stdout.ref", "test.stderr.ref"
5859
)
5960
test("cargo miri test (with filter)",

tests/compiletest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn miri_pass(path: &str, target: &str, opt: bool, noseed: bool) {
8383
flags.push("-Zmir-opt-level=3".to_owned());
8484
} else if !noseed {
8585
// Run with intptrcast. Avoid test matrix explosion by doing either this or opt-level=3.
86+
#[cfg(not(windows))] // FIXME re-enable on Windows
8687
flags.push("-Zmiri-seed=".to_owned());
8788
}
8889

tests/run-pass-noseed/intptrcast.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// compile-flags: -Zmiri-seed=0000000000000000
22

3+
// This returns a miri pointer at type usize, if the argument is a proper pointer
4+
fn transmute_ptr_to_int<T>(x: *const T) -> usize {
5+
unsafe { std::mem::transmute(x) }
6+
}
7+
38
fn main() {
49
// Some casting-to-int with arithmetic.
5-
let x = &42 as *const i32 as usize;
10+
let x = &42 as *const i32 as usize;
611
let y = x * 2;
712
assert_eq!(y, x + x);
813
let z = y as u8 as usize;
@@ -11,4 +16,11 @@ fn main() {
1116
// Pointer string formatting! We can't check the output as it changes when libstd changes,
1217
// but we can make sure Miri does not error.
1318
format!("{:?}", &mut 13 as *mut _);
19+
20+
// Check that intptrcast is triggered for explicit casts and that it is consistent with
21+
// transmuting.
22+
let a: *const i32 = &42;
23+
let b = transmute_ptr_to_int(a) as u8;
24+
let c = a as usize as u8;
25+
assert_eq!(b, c);
1426
}

tests/run-pass-noseed/ptr_int_casts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// FIXME move this to run-pass, it should work with intptrcast.
12
use std::mem;
23
use std::ptr;
34

tests/run-pass/ptr_offset.rs renamed to tests/run-pass-noseed/ptr_offset.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FIXME move this to run-pass, it should work with intptrcast.
2+
13
fn f() -> i32 { 42 }
24

35
fn main() {

0 commit comments

Comments
 (0)