Skip to content

Improve miri error reporting in check_in_alloc #59627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
725199c
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 2, 2019
7b4bc69
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 2, 2019
705d75e
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 2, 2019
4c4dbb1
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 2, 2019
3449fa9
Merge branch 'master' into issue_57128_improve_miri_error_reporting_i…
LooMaclin Apr 2, 2019
2a738bb
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 8, 2019
980db98
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 8, 2019
e5b6fab
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 8, 2019
11464e7
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 8, 2019
32ba4bd
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 8, 2019
9147e26
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 9, 2019
30a9626
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 10, 2019
0d97ad3
Update src/librustc/mir/interpret/allocation.rs
RalfJung Apr 16, 2019
15d50de
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 18, 2019
a54e3cc
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 18, 2019
fc7ffa6
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 23, 2019
b1c829b
Improve miri's error reporting in check_in_alloc
LooMaclin Apr 23, 2019
ffd0dc7
Improve miri's error reporting in check_in_alloc
LooMaclin May 26, 2019
9e643e6
Improve miri's error reporting in check_in_alloc
LooMaclin May 26, 2019
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
39 changes: 32 additions & 7 deletions src/librustc/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{

use crate::ty::layout::{Size, Align};
use syntax::ast::Mutability;
use std::iter;
use std::{iter, fmt::{self, Display}};
use crate::mir;
use std::ops::{Deref, DerefMut};
use rustc_data_structures::sorted_map::SortedMap;
Expand All @@ -22,6 +22,28 @@ pub enum InboundsCheck {
MaybeDead,
}

/// Used by `check_in_alloc` to indicate context of check
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
pub enum CheckInAllocMsg {
MemoryAccessTest,
NullPointerTest,
PointerArithmeticTest,
InboundsTest,
}

impl Display for CheckInAllocMsg {
/// When this is printed as an error the context looks like this
/// "{test name} failed: pointer must be in-bounds at offset..."
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match *self {
CheckInAllocMsg::MemoryAccessTest => "Memory access",
CheckInAllocMsg::NullPointerTest => "Null pointer test",
CheckInAllocMsg::PointerArithmeticTest => "Pointer arithmetic",
CheckInAllocMsg::InboundsTest => "Inbounds test",
})
}
}

#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Allocation<Tag=(),Extra=()> {
/// The actual bytes of the allocation.
Expand Down Expand Up @@ -140,9 +162,10 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
fn check_bounds_ptr(
&self,
ptr: Pointer<Tag>,
msg: CheckInAllocMsg,
) -> EvalResult<'tcx> {
let allocation_size = self.bytes.len() as u64;
ptr.check_in_alloc(Size::from_bytes(allocation_size), InboundsCheck::Live)
ptr.check_in_alloc(Size::from_bytes(allocation_size), msg)
}

/// Checks if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
Expand All @@ -152,9 +175,10 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
cx: &impl HasDataLayout,
ptr: Pointer<Tag>,
size: Size,
msg: CheckInAllocMsg,
) -> EvalResult<'tcx> {
// if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
self.check_bounds_ptr(ptr.offset(size, cx)?)
self.check_bounds_ptr(ptr.offset(size, cx)?, msg)
}
}

Expand All @@ -173,11 +197,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
ptr: Pointer<Tag>,
size: Size,
check_defined_and_ptr: bool,
msg: CheckInAllocMsg,
) -> EvalResult<'tcx, &[u8]>
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
where Extra: AllocationExtra<Tag, MemoryExtra>
{
self.check_bounds(cx, ptr, size)?;
self.check_bounds(cx, ptr, size, msg)?;

if check_defined_and_ptr {
self.check_defined(ptr, size)?;
Expand Down Expand Up @@ -205,7 +230,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
where Extra: AllocationExtra<Tag, MemoryExtra>
{
self.get_bytes_internal(cx, ptr, size, true)
self.get_bytes_internal(cx, ptr, size, true, CheckInAllocMsg::MemoryAccessTest)
}

/// It is the caller's responsibility to handle undefined and pointer bytes.
Expand All @@ -220,7 +245,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
where Extra: AllocationExtra<Tag, MemoryExtra>
{
self.get_bytes_internal(cx, ptr, size, false)
self.get_bytes_internal(cx, ptr, size, false, CheckInAllocMsg::MemoryAccessTest)
}

/// Just calling this already marks everything as defined and removes relocations,
Expand All @@ -235,7 +260,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
where Extra: AllocationExtra<Tag, MemoryExtra>
{
assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
self.check_bounds(cx, ptr, size)?;
self.check_bounds(cx, ptr, size, CheckInAllocMsg::MemoryAccessTest)?;

self.mark_definedness(ptr, size, true)?;
self.clear_relocations(cx, ptr, size)?;
Expand Down
16 changes: 6 additions & 10 deletions src/librustc/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ty::layout::{Size, Align, LayoutError};
use rustc_target::spec::abi::Abi;
use rustc_macros::HashStable;

use super::{RawConst, Pointer, InboundsCheck, ScalarMaybeUndef};
use super::{RawConst, Pointer, CheckInAllocMsg, ScalarMaybeUndef};

use backtrace::Backtrace;

Expand Down Expand Up @@ -243,7 +243,7 @@ pub enum InterpError<'tcx, O> {
InvalidDiscriminant(ScalarMaybeUndef),
PointerOutOfBounds {
ptr: Pointer,
check: InboundsCheck,
msg: CheckInAllocMsg,
allocation_size: Size,
},
InvalidNullPointerUsage,
Expand Down Expand Up @@ -460,14 +460,10 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for InterpError<'tcx, O> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::InterpError::*;
match *self {
PointerOutOfBounds { ptr, check, allocation_size } => {
write!(f, "Pointer must be in-bounds{} at offset {}, but is outside bounds of \
allocation {} which has size {}",
match check {
InboundsCheck::Live => " and live",
InboundsCheck::MaybeDead => "",
},
ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
PointerOutOfBounds { ptr, msg, allocation_size } => {
write!(f, "{} failed: pointer must be in-bounds at offset {}, \
but is outside bounds of allocation {} which has size {}",
msg, ptr.offset.bytes(), ptr.alloc_id, allocation_size.bytes())
},
ValidationFailure(ref err) => {
write!(f, "type validation failed: {}", err)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue};

pub use self::allocation::{
InboundsCheck, Allocation, AllocationExtra,
Relocations, UndefMask,
Relocations, UndefMask, CheckInAllocMsg,
};

pub use self::pointer::{Pointer, PointerArithmetic};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::ty::layout::{self, HasDataLayout, Size};
use rustc_macros::HashStable;

use super::{
AllocId, EvalResult, InboundsCheck,
AllocId, EvalResult, CheckInAllocMsg
};

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -157,12 +157,12 @@ impl<'tcx, Tag> Pointer<Tag> {
pub fn check_in_alloc(
self,
allocation_size: Size,
check: InboundsCheck,
msg: CheckInAllocMsg,
) -> EvalResult<'tcx, ()> {
if self.offset > allocation_size {
err!(PointerOutOfBounds {
ptr: self.erase_tag(),
check,
msg,
allocation_size,
})
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use syntax::ast::Mutability;
use super::{
Pointer, AllocId, Allocation, GlobalId, AllocationExtra,
EvalResult, Scalar, InterpError, AllocKind, PointerArithmetic,
Machine, AllocMap, MayLeak, ErrorHandled, InboundsCheck,
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, InboundsCheck,
};

#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
Expand Down Expand Up @@ -252,7 +252,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
Scalar::Ptr(ptr) => {
// check this is not NULL -- which we can ensure only if this is in-bounds
// of some (potentially dead) allocation.
let align = self.check_bounds_ptr(ptr, InboundsCheck::MaybeDead)?;
let align = self.check_bounds_ptr(ptr, InboundsCheck::MaybeDead,
CheckInAllocMsg::NullPointerTest)?;
(ptr.offset.bytes(), align)
}
Scalar::Bits { bits, size } => {
Expand Down Expand Up @@ -293,9 +294,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
&self,
ptr: Pointer<M::PointerTag>,
liveness: InboundsCheck,
msg: CheckInAllocMsg,
) -> EvalResult<'tcx, Align> {
let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id, liveness)?;
ptr.check_in_alloc(allocation_size, liveness)?;
ptr.check_in_alloc(allocation_size, msg)?;
Ok(align)
}
}
Expand Down Expand Up @@ -419,7 +421,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {

/// Obtain the size and alignment of an allocation, even if that allocation has been deallocated
///
/// If `liveness` is `InboundsCheck::Dead`, this function always returns `Ok`
/// If `liveness` is `InboundsCheck::MaybeDead`, this function always returns `Ok`
pub fn get_size_and_align(
&self,
id: AllocId,
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use rustc::{mir, ty};
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx};

use rustc::mir::interpret::{
GlobalId, AllocId, InboundsCheck,
GlobalId, AllocId, CheckInAllocMsg,
ConstValue, Pointer, Scalar,
EvalResult, InterpError,
EvalResult, InterpError, InboundsCheck,
sign_extend, truncate,
};
use super::{
Expand Down Expand Up @@ -667,7 +667,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) => {
// The niche must be just 0 (which an inbounds pointer value never is)
let ptr_valid = niche_start == 0 && variants_start == variants_end &&
self.memory.check_bounds_ptr(ptr, InboundsCheck::MaybeDead).is_ok();
self.memory.check_bounds_ptr(ptr, InboundsCheck::MaybeDead,
CheckInAllocMsg::NullPointerTest).is_ok();
if !ptr_valid {
return err!(InvalidDiscriminant(raw_discr.erase_tag()));
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc::ty::layout::{self, Size, Align, TyLayout, LayoutOf, VariantIdx};
use rustc::ty;
use rustc_data_structures::fx::FxHashSet;
use rustc::mir::interpret::{
Scalar, AllocKind, EvalResult, InterpError,
Scalar, AllocKind, EvalResult, InterpError, CheckInAllocMsg,
};

use super::{
Expand Down Expand Up @@ -394,7 +394,7 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
try_validation!(
self.ecx.memory
.get(ptr.alloc_id)?
.check_bounds(self.ecx, ptr, size),
.check_bounds(self.ecx, ptr, size, CheckInAllocMsg::InboundsTest),
"dangling (not entirely in bounds) reference", self.path);
}
// Check if we have encountered this pointer+layout combination
Expand Down