Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
align: Align,
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
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<M::PointerTag>,
size: Size,
align: Option<Align>,
msg: CheckInAllocMsg,
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
if offset % align.bytes() == 0 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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) => {
Expand Down