@@ -28,9 +28,9 @@ use rustc_data_structures::fx::{FxHashSet, FxHashMap};
2828use syntax:: ast:: Mutability ;
2929
3030use super :: {
31- Pointer , AllocId , Allocation , GlobalId , AllocationExtra , InboundsCheck ,
31+ Pointer , AllocId , Allocation , GlobalId , AllocationExtra ,
3232 EvalResult , Scalar , EvalErrorKind , AllocType , PointerArithmetic ,
33- Machine , AllocMap , MayLeak , ErrorHandled , AllocationExtra ,
33+ Machine , AllocMap , MayLeak , ErrorHandled , InboundsCheck ,
3434} ;
3535
3636#[ derive( Debug , PartialEq , Eq , Copy , Clone , Hash ) ]
@@ -251,9 +251,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
251251 Scalar :: Ptr ( ptr) => {
252252 // check this is not NULL -- which we can ensure only if this is in-bounds
253253 // of some (potentially dead) allocation.
254- self . check_bounds_ptr ( ptr, InboundsCheck :: MaybeDead ) ?;
255- // data required for alignment check
256- let ( _, align) = self . get_size_and_align ( ptr. alloc_id ) ;
254+ let align = self . check_bounds_ptr_maybe_dead ( ptr) ?;
257255 ( ptr. offset . bytes ( ) , align)
258256 }
259257 Scalar :: Bits { bits, size } => {
@@ -284,6 +282,23 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
284282 } )
285283 }
286284 }
285+
286+ /// Check if the pointer is "in-bounds". Notice that a pointer pointing at the end
287+ /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
288+ /// in-bounds! This follows C's/LLVM's rules.
289+ /// This function also works for deallocated allocations.
290+ /// Use `.get(ptr.alloc_id)?.check_bounds_ptr(ptr)` if you want to force the allocation
291+ /// to still be live.
292+ /// If you want to check bounds before doing a memory access, better first obtain
293+ /// an `Allocation` and call `check_bounds`.
294+ pub fn check_bounds_ptr_maybe_dead (
295+ & self ,
296+ ptr : Pointer < M :: PointerTag > ,
297+ ) -> EvalResult < ' tcx , Align > {
298+ let ( allocation_size, align) = self . get_size_and_align ( ptr. alloc_id ) ;
299+ ptr. check_in_alloc ( allocation_size, InboundsCheck :: MaybeDead ) ?;
300+ Ok ( align)
301+ }
287302}
288303
289304/// Allocation accessors
0 commit comments