1
Fork 0

Allow ptr_from_addr_cast to fail

This commit is contained in:
Ralf Jung 2022-06-05 10:53:35 -04:00
parent 4322a785cc
commit e1f0736927
2 changed files with 7 additions and 6 deletions

View file

@ -221,7 +221,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let addr = addr.to_machine_usize(self)?; let addr = addr.to_machine_usize(self)?;
// Then turn address into pointer. // Then turn address into pointer.
let ptr = M::ptr_from_addr_cast(&self, addr); let ptr = M::ptr_from_addr_cast(&self, addr)?;
Ok(Scalar::from_maybe_pointer(ptr, self).into()) Ok(Scalar::from_maybe_pointer(ptr, self).into())
} }

View file

@ -294,11 +294,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
fn ptr_from_addr_cast( fn ptr_from_addr_cast(
ecx: &InterpCx<'mir, 'tcx, Self>, ecx: &InterpCx<'mir, 'tcx, Self>,
addr: u64, addr: u64,
) -> Pointer<Option<Self::PointerTag>>; ) -> InterpResult<'tcx, Pointer<Option<Self::PointerTag>>>;
// FIXME: Transmuting an integer to a pointer should just always return a `None`
// provenance, but that causes problems with function pointers in Miri.
/// Hook for returning a pointer from a transmute-like operation on an addr. /// Hook for returning a pointer from a transmute-like operation on an addr.
/// This is only needed to support Miri's (unsound) "allow-ptr-int-transmute" flag.
fn ptr_from_addr_transmute( fn ptr_from_addr_transmute(
ecx: &InterpCx<'mir, 'tcx, Self>, ecx: &InterpCx<'mir, 'tcx, Self>,
addr: u64, addr: u64,
@ -519,8 +518,10 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
fn ptr_from_addr_cast( fn ptr_from_addr_cast(
_ecx: &InterpCx<$mir, $tcx, Self>, _ecx: &InterpCx<$mir, $tcx, Self>,
addr: u64, addr: u64,
) -> Pointer<Option<AllocId>> { ) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
Pointer::new(None, Size::from_bytes(addr)) // Allow these casts, but make the pointer not dereferenceable.
// (I.e., they behave like transmutation.)
Ok(Pointer::new(None, Size::from_bytes(addr)))
} }
#[inline(always)] #[inline(always)]