From f68bba9906c85a508531daaa1f64da723185b6c4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 11 Jul 2020 20:56:47 +0200 Subject: [PATCH] test casting a dangling ptr back from an int --- tests/run-pass/intptrcast.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/run-pass/intptrcast.rs b/tests/run-pass/intptrcast.rs index c2711f9845d..6e72d30d412 100644 --- a/tests/run-pass/intptrcast.rs +++ b/tests/run-pass/intptrcast.rs @@ -12,6 +12,20 @@ fn cast() { assert_eq!(z, y % 256); } +/// Test usize->ptr cast for dangling and OOB address. +/// That is safe, and thus has to work. +fn cast_dangling() { + let b = Box::new(0); + let x = &*b as *const i32 as usize; + drop(b); + let _val = x as *const i32; + + let b = Box::new(0); + let mut x = &*b as *const i32 as usize; + x += 0x100; + let _val = x as *const i32; +} + fn format() { // Pointer string formatting! We can't check the output as it changes when libstd changes, // but we can make sure Miri does not error. @@ -47,8 +61,7 @@ fn ptr_eq_dangling() { drop(b); let b = Box::new(0); let y = &*b as *const i32; // different allocation - // We cannot compare these even though both are inbounds -- they *could* be - // equal if memory was reused. + // They *could* be equal if memory was reused, but probably are not. assert!(x != y); } @@ -57,27 +70,27 @@ fn ptr_eq_out_of_bounds() { let x = (&*b as *const i32).wrapping_sub(0x800); // out-of-bounds let b = Box::new(0); let y = &*b as *const i32; // different allocation - // We cannot compare these even though both allocations are live -- they *could* be - // equal (with the right base addresses). + // They *could* be equal (with the right base addresses), but probably are not. assert!(x != y); } fn ptr_eq_out_of_bounds_null() { let b = Box::new(0); let x = (&*b as *const i32).wrapping_sub(0x800); // out-of-bounds - // We cannot compare this with NULL. After all, this *could* be NULL (with the right base address). + // This *could* be NULL (with the right base address), but probably is not. assert!(x != std::ptr::null()); } fn ptr_eq_integer() { let b = Box::new(0); let x = &*b as *const i32; - // We cannot compare this with a non-NULL integer. After all, these *could* be equal (with the right base address). + // These *could* be equal (with the right base address), but probably are not. assert!(x != 64 as *const i32); } fn main() { cast(); + cast_dangling(); format(); transmute(); ptr_bitops1(); -- 2.44.0