Skip to content

Commit

Permalink
let caller of check_ptr_access_align control the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 4, 2019
1 parent f49f388 commit b4dde36
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
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

0 comments on commit b4dde36

Please sign in to comment.