now we can make scalar_to_ptr a method on Scalar
This commit is contained in:
parent
665a7e8f56
commit
4e89a7c293
10 changed files with 41 additions and 45 deletions
|
@ -173,15 +173,14 @@ pub(super) fn op_to_const<'tcx>(
|
||||||
Immediate::ScalarPair(a, b) => {
|
Immediate::ScalarPair(a, b) => {
|
||||||
debug!("ScalarPair(a: {:?}, b: {:?})", a, b);
|
debug!("ScalarPair(a: {:?}, b: {:?})", a, b);
|
||||||
// We know `offset` is relative to the allocation, so we can use `into_parts`.
|
// We know `offset` is relative to the allocation, so we can use `into_parts`.
|
||||||
let (data, start) =
|
let (data, start) = match a.to_pointer(ecx).unwrap().into_parts() {
|
||||||
match ecx.scalar_to_ptr(a.check_init().unwrap()).unwrap().into_parts() {
|
|
||||||
(Some(alloc_id), offset) => {
|
(Some(alloc_id), offset) => {
|
||||||
(ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
|
(ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
|
||||||
}
|
}
|
||||||
(None, _offset) => (
|
(None, _offset) => (
|
||||||
ecx.tcx.intern_const_alloc(
|
ecx.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable(
|
||||||
Allocation::from_bytes_byte_aligned_immutable(b"" as &[u8]),
|
b"" as &[u8],
|
||||||
),
|
)),
|
||||||
0,
|
0,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
|
@ -180,7 +180,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
assert!(cast_ty.is_integral());
|
assert!(cast_ty.is_integral());
|
||||||
|
|
||||||
let scalar = src.to_scalar()?;
|
let scalar = src.to_scalar()?;
|
||||||
let ptr = self.scalar_to_ptr(scalar)?;
|
let ptr = scalar.to_pointer(self)?;
|
||||||
match ptr.into_pointer_or_addr() {
|
match ptr.into_pointer_or_addr() {
|
||||||
Ok(ptr) => M::expose_ptr(self, ptr)?,
|
Ok(ptr) => M::expose_ptr(self, ptr)?,
|
||||||
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
|
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
|
||||||
|
@ -299,7 +299,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
}
|
}
|
||||||
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
|
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
|
||||||
let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?;
|
let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?;
|
||||||
let old_vptr = self.scalar_to_ptr(old_vptr)?;
|
let old_vptr = old_vptr.to_pointer(self)?;
|
||||||
let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?;
|
let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?;
|
||||||
if old_trait != data_a.principal() {
|
if old_trait != data_a.principal() {
|
||||||
throw_ub_format!("upcast on a pointer whose vtable does not match its type");
|
throw_ub_format!("upcast on a pointer whose vtable does not match its type");
|
||||||
|
|
|
@ -629,7 +629,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
Ok(Some((size, align)))
|
Ok(Some((size, align)))
|
||||||
}
|
}
|
||||||
ty::Dynamic(..) => {
|
ty::Dynamic(..) => {
|
||||||
let vtable = self.scalar_to_ptr(metadata.unwrap_meta())?;
|
let vtable = metadata.unwrap_meta().to_pointer(self)?;
|
||||||
// Read size and align from vtable (already checks size).
|
// Read size and align from vtable (already checks size).
|
||||||
Ok(Some(self.get_vtable_size_and_align(vtable)?))
|
Ok(Some(self.get_vtable_size_and_align(vtable)?))
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,7 +245,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
|
||||||
if let ty::Dynamic(..) =
|
if let ty::Dynamic(..) =
|
||||||
tcx.struct_tail_erasing_lifetimes(referenced_ty, self.ecx.param_env).kind()
|
tcx.struct_tail_erasing_lifetimes(referenced_ty, self.ecx.param_env).kind()
|
||||||
{
|
{
|
||||||
let ptr = self.ecx.scalar_to_ptr(mplace.meta.unwrap_meta())?;
|
let ptr = mplace.meta.unwrap_meta().to_pointer(&tcx)?;
|
||||||
if let Some(alloc_id) = ptr.provenance {
|
if let Some(alloc_id) = ptr.provenance {
|
||||||
// Explicitly choose const mode here, since vtables are immutable, even
|
// Explicitly choose const mode here, since vtables are immutable, even
|
||||||
// if the reference of the fat pointer is mutable.
|
// if the reference of the fat pointer is mutable.
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
use std::assert_matches::assert_matches;
|
use std::assert_matches::assert_matches;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
|
@ -1172,26 +1171,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
|
|
||||||
/// Machine pointer introspection.
|
/// Machine pointer introspection.
|
||||||
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
pub fn scalar_to_ptr(
|
|
||||||
&self,
|
|
||||||
scalar: Scalar<M::Provenance>,
|
|
||||||
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
|
||||||
// We use `to_bits_or_ptr_internal` since we are just implementing the method people need to
|
|
||||||
// call to force getting out a pointer.
|
|
||||||
Ok(
|
|
||||||
match scalar
|
|
||||||
.to_bits_or_ptr_internal(self.pointer_size())
|
|
||||||
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
|
|
||||||
{
|
|
||||||
Err(ptr) => ptr.into(),
|
|
||||||
Ok(bits) => {
|
|
||||||
let addr = u64::try_from(bits).unwrap();
|
|
||||||
Pointer::from_addr(addr)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Test if this value might be null.
|
/// Test if this value might be null.
|
||||||
/// If the machine does not support ptr-to-int casts, this is conservative.
|
/// If the machine does not support ptr-to-int casts, this is conservative.
|
||||||
pub fn scalar_may_be_null(&self, scalar: Scalar<M::Provenance>) -> InterpResult<'tcx, bool> {
|
pub fn scalar_may_be_null(&self, scalar: Scalar<M::Provenance>) -> InterpResult<'tcx, bool> {
|
||||||
|
@ -1199,7 +1178,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
Ok(int) => int.is_null(),
|
Ok(int) => int.is_null(),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
// Can only happen during CTFE.
|
// Can only happen during CTFE.
|
||||||
let ptr = self.scalar_to_ptr(scalar)?;
|
let ptr = scalar.to_pointer(self)?;
|
||||||
match self.ptr_try_get_alloc_id(ptr) {
|
match self.ptr_try_get_alloc_id(ptr) {
|
||||||
Ok((alloc_id, offset, _)) => {
|
Ok((alloc_id, offset, _)) => {
|
||||||
let (size, _align, _kind) = self.get_alloc_info(alloc_id);
|
let (size, _align, _kind) = self.get_alloc_info(alloc_id);
|
||||||
|
|
|
@ -14,7 +14,7 @@ use rustc_target::abi::{VariantIdx, Variants};
|
||||||
use super::{
|
use super::{
|
||||||
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
|
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
|
||||||
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer,
|
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer,
|
||||||
PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit,
|
Provenance, Scalar, ScalarMaybeUninit,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An `Immediate` represents a single immediate self-contained Rust value.
|
/// An `Immediate` represents a single immediate self-contained Rust value.
|
||||||
|
@ -455,7 +455,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
&self,
|
&self,
|
||||||
op: &OpTy<'tcx, M::Provenance>,
|
op: &OpTy<'tcx, M::Provenance>,
|
||||||
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
||||||
self.scalar_to_ptr(self.read_scalar(op)?.check_init()?)
|
self.read_scalar(op)?.to_pointer(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn the wide MPlace into a string (must already be dereferenced!)
|
/// Turn the wide MPlace into a string (must already be dereferenced!)
|
||||||
|
|
|
@ -331,7 +331,7 @@ where
|
||||||
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
|
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mplace = MemPlace { ptr: self.scalar_to_ptr(ptr.check_init()?)?, meta };
|
let mplace = MemPlace { ptr: ptr.to_pointer(self)?, meta };
|
||||||
// When deref'ing a pointer, the *static* alignment given by the type is what matters.
|
// When deref'ing a pointer, the *static* alignment given by the type is what matters.
|
||||||
let align = layout.align.abi;
|
let align = layout.align.abi;
|
||||||
Ok(MPlaceTy { mplace, layout, align })
|
Ok(MPlaceTy { mplace, layout, align })
|
||||||
|
@ -889,7 +889,7 @@ where
|
||||||
&self,
|
&self,
|
||||||
mplace: &MPlaceTy<'tcx, M::Provenance>,
|
mplace: &MPlaceTy<'tcx, M::Provenance>,
|
||||||
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
||||||
let vtable = self.scalar_to_ptr(mplace.vtable())?; // also sanity checks the type
|
let vtable = mplace.vtable().to_pointer(self)?; // also sanity checks the type
|
||||||
let (ty, _) = self.get_ptr_vtable(vtable)?;
|
let (ty, _) = self.get_ptr_vtable(vtable)?;
|
||||||
let layout = self.layout_of(ty)?;
|
let layout = self.layout_of(ty)?;
|
||||||
|
|
||||||
|
|
|
@ -561,7 +561,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the required information from the vtable.
|
// Get the required information from the vtable.
|
||||||
let vptr = self.scalar_to_ptr(receiver_place.meta.unwrap_meta())?;
|
let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?;
|
||||||
let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?;
|
let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?;
|
||||||
if dyn_trait != data.principal() {
|
if dyn_trait != data.principal() {
|
||||||
throw_ub_format!(
|
throw_ub_format!(
|
||||||
|
|
|
@ -312,7 +312,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
|
||||||
let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env);
|
let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env);
|
||||||
match tail.kind() {
|
match tail.kind() {
|
||||||
ty::Dynamic(..) => {
|
ty::Dynamic(..) => {
|
||||||
let vtable = self.ecx.scalar_to_ptr(meta.unwrap_meta())?;
|
let vtable = meta.unwrap_meta().to_pointer(self.ecx)?;
|
||||||
// Make sure it is a genuine vtable pointer.
|
// Make sure it is a genuine vtable pointer.
|
||||||
let (_ty, _trait) = try_validation!(
|
let (_ty, _trait) = try_validation!(
|
||||||
self.ecx.get_ptr_vtable(vtable),
|
self.ecx.get_ptr_vtable(vtable),
|
||||||
|
@ -566,7 +566,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
|
||||||
|
|
||||||
// If we check references recursively, also check that this points to a function.
|
// If we check references recursively, also check that this points to a function.
|
||||||
if let Some(_) = self.ref_tracking {
|
if let Some(_) = self.ref_tracking {
|
||||||
let ptr = self.ecx.scalar_to_ptr(value)?;
|
let ptr = value.to_pointer(self.ecx)?;
|
||||||
let _fn = try_validation!(
|
let _fn = try_validation!(
|
||||||
self.ecx.get_ptr_fn(ptr),
|
self.ecx.get_ptr_fn(ptr),
|
||||||
self.path,
|
self.path,
|
||||||
|
|
|
@ -331,6 +331,19 @@ impl<Prov> Scalar<Prov> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx, Prov: Provenance> Scalar<Prov> {
|
impl<'tcx, Prov: Provenance> Scalar<Prov> {
|
||||||
|
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
|
||||||
|
match self
|
||||||
|
.to_bits_or_ptr_internal(cx.pointer_size())
|
||||||
|
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
|
||||||
|
{
|
||||||
|
Err(ptr) => Ok(ptr.into()),
|
||||||
|
Ok(bits) => {
|
||||||
|
let addr = u64::try_from(bits).unwrap();
|
||||||
|
Ok(Pointer::from_addr(addr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
|
/// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
|
||||||
/// likely want to use instead.
|
/// likely want to use instead.
|
||||||
///
|
///
|
||||||
|
@ -546,6 +559,11 @@ impl<Prov> ScalarMaybeUninit<Prov> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx, Prov: Provenance> ScalarMaybeUninit<Prov> {
|
impl<'tcx, Prov: Provenance> ScalarMaybeUninit<Prov> {
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
|
||||||
|
self.check_init()?.to_pointer(cx)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
|
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
|
||||||
self.check_init()?.to_bool()
|
self.check_init()?.to_bool()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue