Add force_bits and force_ptr methods

This commit is contained in:
Christian Poveda 2019-06-12 12:49:46 -05:00 committed by Christian Poveda
parent 374c63e0fc
commit 3bd1734ea2
3 changed files with 46 additions and 1 deletions

View File

@ -765,4 +765,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
truncate(value, ty.size)
}
#[inline(always)]
pub fn force_ptr(
&self,
scalar: Scalar<M::PointerTag>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
self.memory.force_ptr(scalar)
}
#[inline(always)]
pub fn force_bits(&self, scalar: Scalar<M::PointerTag>) -> InterpResult<'tcx, u128> {
self.memory.force_bits(scalar)
}
}

View File

@ -11,7 +11,8 @@ use rustc::ty::{self, query::TyCtxtAt};
use super::{
Allocation, AllocId, InterpResult, Scalar, AllocationExtra,
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind,
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer,
InterpErrorInfo, InterpError
};
/// Whether this kind of memory is allowed to leak
@ -208,4 +209,18 @@ pub trait Machine<'mir, 'tcx>: Sized {
ecx: &mut InterpretCx<'mir, 'tcx, Self>,
extra: Self::FrameExtra,
) -> InterpResult<'tcx>;
fn int_to_ptr(
_int: u64,
_extra: &Self::MemoryExtra,
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
Err(InterpErrorInfo::from(InterpError::ReadBytesAsPointer))
}
fn ptr_to_int(
_ptr: Pointer<Self::PointerTag>,
_extra: &Self::MemoryExtra,
) -> InterpResult<'tcx, u64> {
Err(InterpErrorInfo::from(InterpError::ReadPointerAsBytes))
}
}

View File

@ -874,4 +874,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
}
Ok(())
}
pub fn force_ptr(
&self,
scalar: Scalar<M::PointerTag>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
match scalar {
Scalar::Ptr(ptr) => Ok(ptr),
_ => M::int_to_ptr(scalar.to_usize(self)?, &self.extra)
}
}
pub fn force_bits(&self, scalar: Scalar<M::PointerTag>) -> InterpResult<'tcx, u128> {
match scalar.to_bits_or_ptr(self.pointer_size(), self) {
Ok(bits) => Ok(bits),
Err(ptr) => Ok(M::ptr_to_int(ptr, &self.extra)? as u128)
}
}
}