From f2e14d931417f5037aea6212a5294a641caf5512 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 26 Dec 2018 13:16:47 +0100 Subject: [PATCH 1/2] use memory::check_bounds_ptr for offset check --- src/operator.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/operator.rs b/src/operator.rs index cc803c4ea954..4b110224a0a2 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -303,10 +303,9 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, ' if let Scalar::Ptr(ptr) = ptr { // Both old and new pointer must be in-bounds of a *live* allocation. // (Of the same allocation, but that part is trivial with our representation.) - let alloc = self.memory().get(ptr.alloc_id)?; - alloc.check_bounds_ptr(ptr)?; + self.memory().check_bounds_ptr(ptr, InboundsCheck::Live)?; let ptr = ptr.signed_offset(offset, self)?; - alloc.check_bounds_ptr(ptr)?; + self.memory().check_bounds_ptr(ptr, InboundsCheck::Live)?; Ok(Scalar::Ptr(ptr)) } else { // An integer pointer. They can only be offset by 0, and we pretend there From 3715245a3688de8f82ffbb127b09a0dd517458c3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 26 Dec 2018 16:23:04 +0100 Subject: [PATCH 2/2] add test for offseting fn ptr --- tests/run-pass/ptr_offset.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/run-pass/ptr_offset.rs b/tests/run-pass/ptr_offset.rs index 6add5212db9f..9e2e26fad365 100644 --- a/tests/run-pass/ptr_offset.rs +++ b/tests/run-pass/ptr_offset.rs @@ -1,6 +1,16 @@ +fn f() -> i32 { 42 } + fn main() { let v = [1i16, 2]; let x = &v as *const i16; let x = unsafe { x.offset(1) }; assert_eq!(unsafe { *x }, 2); + + // fn ptr offset + unsafe { + let p = f as fn() -> i32 as usize; + let x = (p as *mut u32).offset(0) as usize; + let f: fn() -> i32 = std::mem::transmute(x); + assert_eq!(f(), 42); + } }