Skip to content

Commit d8ddb47

Browse files
committed
Allow testing pointers for inboundedness while forbidding dangling pointers
1 parent 74ebf02 commit d8ddb47

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
262262
Scalar::Ptr(ptr) => {
263263
// check this is not NULL -- which we can ensure only if this is in-bounds
264264
// of some (potentially dead) allocation.
265-
let align = self.check_bounds_ptr_maybe_dead(ptr)?;
265+
let align = self.check_bounds_ptr(ptr, InboundsCheck::MaybeDead)?;
266266
(ptr.offset.bytes(), align)
267267
}
268268
Scalar::Bits { bits, size } => {
@@ -297,17 +297,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
297297
/// Check if the pointer is "in-bounds". Notice that a pointer pointing at the end
298298
/// of an allocation (i.e., at the first *inaccessible* location) *is* considered
299299
/// in-bounds! This follows C's/LLVM's rules.
300-
/// This function also works for deallocated allocations.
301-
/// Use `.get(ptr.alloc_id)?.check_bounds_ptr(ptr)` if you want to force the allocation
302-
/// to still be live.
303300
/// If you want to check bounds before doing a memory access, better first obtain
304301
/// an `Allocation` and call `check_bounds`.
305-
pub fn check_bounds_ptr_maybe_dead(
302+
pub fn check_bounds_ptr(
306303
&self,
307304
ptr: Pointer<M::PointerTag>,
305+
liveness: InboundsCheck,
308306
) -> EvalResult<'tcx, Align> {
309307
let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id);
310-
ptr.check_in_alloc(allocation_size, InboundsCheck::MaybeDead)?;
308+
ptr.check_in_alloc(allocation_size, liveness)?;
311309
Ok(align)
312310
}
313311
}

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::mir;
1717
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx};
1818

1919
use rustc::mir::interpret::{
20-
GlobalId, AllocId,
20+
GlobalId, AllocId, InboundsCheck,
2121
ConstValue, Pointer, Scalar,
2222
EvalResult, EvalErrorKind,
2323
};
@@ -647,7 +647,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
647647
ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) => {
648648
// The niche must be just 0 (which an inbounds pointer value never is)
649649
let ptr_valid = niche_start == 0 && variants_start == variants_end &&
650-
self.memory.check_bounds_ptr_maybe_dead(ptr).is_ok();
650+
self.memory.check_bounds_ptr(ptr, InboundsCheck::MaybeDead).is_ok();
651651
if !ptr_valid {
652652
return err!(InvalidDiscriminant(raw_discr.erase_tag()));
653653
}

0 commit comments

Comments
 (0)