diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index d113ee33162d2..47b918248330a 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -314,16 +314,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { align: Align, ) -> InterpResult<'tcx, Option>> { let align = if M::CHECK_ALIGN { Some(align) } else { None }; - self.check_ptr_access_align(sptr, size, align) + self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest) } /// Like `check_ptr_access`, but *definitely* checks alignment when `align` - /// is `Some` (overriding `M::CHECK_ALIGN`). - pub(super) fn check_ptr_access_align( + /// is `Some` (overriding `M::CHECK_ALIGN`). Also lets the caller control + /// the error message for the out-of-bounds case. + pub fn check_ptr_access_align( &self, sptr: Scalar, size: Size, align: Option, + msg: CheckInAllocMsg, ) -> InterpResult<'tcx, Option>> { fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> { if offset % align.bytes() == 0 { @@ -368,7 +370,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { // It is sufficient to check this for the end pointer. The addition // checks for overflow. let end_ptr = ptr.offset(size, self)?; - end_ptr.check_inbounds_alloc(allocation_size, CheckInAllocMsg::MemoryAccessTest)?; + end_ptr.check_inbounds_alloc(allocation_size, msg)?; // Test align. Check this last; if both bounds and alignment are violated // we want the error to be about the bounds. if let Some(align) = align { diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 3444fb60f333b..82b8b28d72b7b 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashSet; use std::hash::Hash; use super::{ - GlobalAlloc, InterpResult, + GlobalAlloc, InterpResult, CheckInAllocMsg, Scalar, OpTy, Machine, InterpCx, ValueVisitor, MPlaceTy, }; @@ -424,7 +424,12 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> // alignment should take attributes into account). .unwrap_or_else(|| (layout.size, layout.align.abi)); let ptr: Option<_> = match - self.ecx.memory.check_ptr_access_align(ptr, size, Some(align)) + self.ecx.memory.check_ptr_access_align( + ptr, + size, + Some(align), + CheckInAllocMsg::InboundsTest, + ) { Ok(ptr) => ptr, Err(err) => {