Skip to content

Commit e5b6fab

Browse files
committed
Improve miri's error reporting in check_in_alloc
1 parent 980db98 commit e5b6fab

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax::ast::Mutability;
2020
use super::{
2121
Pointer, AllocId, Allocation, GlobalId, AllocationExtra,
2222
EvalResult, Scalar, InterpError, AllocKind, PointerArithmetic,
23-
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg,
23+
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, InboundsCheck,
2424
};
2525

2626
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
@@ -252,7 +252,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
252252
Scalar::Ptr(ptr) => {
253253
// check this is not NULL -- which we can ensure only if this is in-bounds
254254
// of some (potentially dead) allocation.
255-
let align = self.check_bounds_ptr(ptr, CheckInAllocMsg::NullPointer)?;
255+
let align = self.check_bounds_ptr(ptr, InboundsCheck::MaybeDead, CheckInAllocMsg::NullPointer)?;
256256
(ptr.offset.bytes(), align)
257257
}
258258
Scalar::Bits { bits, size } => {
@@ -292,9 +292,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
292292
pub fn check_bounds_ptr(
293293
&self,
294294
ptr: Pointer<M::PointerTag>,
295+
liveness: InboundsCheck,
295296
msg: CheckInAllocMsg,
296297
) -> EvalResult<'tcx, Align> {
297-
let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id, msg)?;
298+
let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id, liveness)?;
298299
ptr.check_in_alloc(allocation_size, msg)?;
299300
Ok(align)
300301
}
@@ -419,11 +420,11 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
419420

420421
/// Obtain the size and alignment of an allocation, even if that allocation has been deallocated
421422
///
422-
/// If `liveness` is `InboundsCheck::Dead`, this function always returns `Ok`
423+
/// If `liveness` is `InboundsCheck::MaybeDead`, this function always returns `Ok`
423424
pub fn get_size_and_align(
424425
&self,
425426
id: AllocId,
426-
msg: CheckInAllocMsg,
427+
liveness: InboundsCheck,
427428
) -> EvalResult<'static, (Size, Align)> {
428429
if let Ok(alloc) = self.get(id) {
429430
return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
@@ -439,8 +440,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
439440
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
440441
Ok((layout.size, layout.align.abi))
441442
}
442-
_ => match msg {
443-
CheckInAllocMsg::NullPointer | CheckInAllocMsg::OutOfBounds => {
443+
_ => match liveness {
444+
InboundsCheck::MaybeDead => {
444445
// Must be a deallocated pointer
445446
Ok(*self.dead_alloc_map.get(&id).expect(
446447
"allocation missing in dead_alloc_map"

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerEx
99
use rustc::mir::interpret::{
1010
GlobalId, AllocId, CheckInAllocMsg,
1111
ConstValue, Pointer, Scalar,
12-
EvalResult, InterpError,
12+
EvalResult, InterpError, InboundsCheck,
1313
sign_extend, truncate,
1414
};
1515
use super::{
@@ -667,7 +667,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
667667
ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) => {
668668
// The niche must be just 0 (which an inbounds pointer value never is)
669669
let ptr_valid = niche_start == 0 && variants_start == variants_end &&
670-
self.memory.check_bounds_ptr(ptr,
670+
self.memory.check_bounds_ptr(ptr, InboundsCheck::MaybeDead,
671671
CheckInAllocMsg::OutOfBounds).is_ok();
672672
if !ptr_valid {
673673
return err!(InvalidDiscriminant(raw_discr.erase_tag()));

0 commit comments

Comments
 (0)