rust/compiler/rustc_mir/src/interpret/traits.rs

210 lines
9.2 KiB
Rust
Raw Normal View History

use std::convert::TryFrom;
use rustc_middle::mir::interpret::{
AllocError, InterpError, InterpResult, Pointer, PointerArithmetic, Scalar,
UndefinedBehaviorInfo, UnsupportedOpInfo,
};
use rustc_middle::ty::{
self, Instance, Ty, VtblEntry, COMMON_VTABLE_ENTRIES, COMMON_VTABLE_ENTRIES_ALIGN,
COMMON_VTABLE_ENTRIES_DROPINPLACE, COMMON_VTABLE_ENTRIES_SIZE,
};
use rustc_target::abi::{Align, LayoutOf, Size};
use super::alloc_range;
use super::util::ensure_monomorphic_enough;
use super::{Allocation, FnVal, InterpCx, Machine};
fn vtable_alloc_error_to_interp_error<'tcx>(error: AllocError) -> InterpError<'tcx> {
match error {
AllocError::ReadPointerAsBytes => {
InterpError::Unsupported(UnsupportedOpInfo::ReadPointerAsBytes)
}
AllocError::InvalidUninitBytes(_info) => {
InterpError::UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(None))
}
}
}
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2016-09-11 09:06:44 +00:00
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
/// objects.
///
/// The `trait_ref` encodes the erased self type. Hence, if we are
/// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
/// `trait_ref` would map `T: Trait`.
pub fn get_vtable(
&mut self,
ty: Ty<'tcx>,
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
trace!("get_vtable(trait_ref={:?})", poly_trait_ref);
2020-10-24 00:21:18 +00:00
let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref));
// All vtables must be monomorphic, bail out otherwise.
ensure_monomorphic_enough(*self.tcx, ty)?;
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
if let Some(&vtable) = self.vtables.get(&(ty, poly_trait_ref)) {
// This means we guarantee that there are no duplicate vtables, we will
// always use the same vtable for the same (Type, Trait) combination.
// That's not what happens in rustc, but emulating per-crate deduplication
// does not sound like it actually makes anything any better.
return Ok(vtable);
}
let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
2020-06-14 13:02:51 +00:00
let trait_ref = poly_trait_ref.with_self_ty(*self.tcx, ty);
2020-10-24 00:21:18 +00:00
let trait_ref = self.tcx.erase_regions(trait_ref);
self.tcx.vtable_entries(trait_ref)
} else {
COMMON_VTABLE_ENTRIES
};
let layout = self.layout_of(ty)?;
assert!(!layout.is_unsized(), "can't create a vtable for an unsized type");
let size = layout.size.bytes();
let align = layout.align.abi.bytes();
2020-06-14 13:02:51 +00:00
let tcx = *self.tcx;
let ptr_size = self.pointer_size();
let ptr_align = tcx.data_layout.pointer_align.abi;
// /////////////////////////////////////////////////////////////////////////////////////////
// If you touch this code, be sure to also make the corresponding changes to
// `get_vtable` in `rust_codegen_llvm/meth.rs`.
// /////////////////////////////////////////////////////////////////////////////////////////
let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap();
let mut vtable = Allocation::uninit(vtable_size, ptr_align);
2019-05-23 17:45:22 +00:00
// No need to do any alignment checks on the memory accesses below, because we know the
// allocation is correctly aligned as we created it above. Also we're only offsetting by
// multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.
let scalars = vtable_entries
.iter()
.map(|entry| -> InterpResult<'tcx, _> {
match entry {
VtblEntry::MetadataDropInPlace => {
let instance = Instance::resolve_drop_in_place(tcx, ty);
let fn_alloc_id = tcx.create_fn_alloc(instance);
let fn_ptr = Pointer::from(fn_alloc_id);
Ok(Some(fn_ptr.into()))
}
VtblEntry::MetadataSize => Ok(Some(Scalar::from_uint(size, ptr_size).into())),
VtblEntry::MetadataAlign => Ok(Some(Scalar::from_uint(align, ptr_size).into())),
VtblEntry::Vacant => Ok(None),
VtblEntry::Method(def_id, substs) => {
// Prepare the fn ptr we write into the vtable.
let instance =
Instance::resolve_for_vtable(tcx, self.param_env, *def_id, substs)
.ok_or_else(|| err_inval!(TooGeneric))?;
let fn_alloc_id = tcx.create_fn_alloc(instance);
let fn_ptr = Pointer::from(fn_alloc_id);
Ok(Some(fn_ptr.into()))
}
}
})
.collect::<Result<Vec<_>, _>>()?;
for (idx, scalar) in scalars.into_iter().enumerate() {
if let Some(scalar) = scalar {
let idx: u64 = u64::try_from(idx).unwrap();
vtable
.write_scalar(self, alloc_range(ptr_size * idx, ptr_size), scalar)
.map_err(vtable_alloc_error_to_interp_error)?;
}
}
let vtable_id = tcx.create_memory_alloc(tcx.intern_const_alloc(vtable));
let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_id))?;
assert!(self.vtables.insert((ty, poly_trait_ref), vtable_ptr).is_none());
2016-09-10 13:17:08 +00:00
Ok(vtable_ptr)
}
/// Resolves the function at the specified slot in the provided
/// vtable. Currently an index of '3' (`COMMON_VTABLE_ENTRIES.len()`)
/// corresponds to the first method declared in the trait of the provided vtable.
2019-10-28 23:09:54 +00:00
pub fn get_vtable_slot(
&self,
vtable: Scalar<M::PointerTag>,
idx: u64,
2019-10-28 23:09:54 +00:00
) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
let ptr_size = self.pointer_size();
let vtable_slot = vtable.ptr_offset(ptr_size * idx, self)?;
2019-12-22 22:42:04 +00:00
let vtable_slot = self
.memory
.get(vtable_slot, ptr_size, self.tcx.data_layout.pointer_align.abi)?
2019-12-22 22:42:04 +00:00
.expect("cannot be a ZST");
let fn_ptr = vtable_slot.read_ptr_sized(Size::ZERO)?.check_init()?;
self.memory.get_fn(fn_ptr)
2019-10-28 23:09:54 +00:00
}
/// Returns the drop fn instance as well as the actual dynamic type.
pub fn read_drop_type_from_vtable(
&self,
vtable: Scalar<M::PointerTag>,
) -> InterpResult<'tcx, (ty::Instance<'tcx>, Ty<'tcx>)> {
let pointer_size = self.pointer_size();
// We don't care about the pointee type; we just want a pointer.
2019-12-22 22:42:04 +00:00
let vtable = self
.memory
.get(
vtable,
pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES.len()).unwrap(),
self.tcx.data_layout.pointer_align.abi,
)?
2019-12-22 22:42:04 +00:00
.expect("cannot be a ZST");
let drop_fn = vtable
.read_ptr_sized(
pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES_DROPINPLACE).unwrap(),
)?
.check_init()?;
// We *need* an instance here, no other kind of function value, to be able
// to determine the type.
let drop_instance = self.memory.get_fn(drop_fn)?.as_instance()?;
trace!("Found drop fn: {:?}", drop_instance);
let fn_sig = drop_instance.ty(*self.tcx, self.param_env).fn_sig(*self.tcx);
2020-10-24 00:21:18 +00:00
let fn_sig = self.tcx.normalize_erasing_late_bound_regions(self.param_env, fn_sig);
// The drop function takes `*mut T` where `T` is the type being dropped, so get that.
2019-12-12 14:23:27 +00:00
let args = fn_sig.inputs();
if args.len() != 1 {
throw_ub!(InvalidVtableDropFn(fn_sig));
2019-12-12 14:23:27 +00:00
}
let ty =
args[0].builtin_deref(true).ok_or_else(|| err_ub!(InvalidVtableDropFn(fn_sig)))?.ty;
Ok((drop_instance, ty))
2017-03-22 16:48:16 +00:00
}
pub fn read_size_and_align_from_vtable(
&self,
vtable: Scalar<M::PointerTag>,
) -> InterpResult<'tcx, (Size, Align)> {
let pointer_size = self.pointer_size();
// We check for `size = 3 * ptr_size`, which covers the drop fn (unused here),
2019-06-23 16:06:11 +00:00
// the size, and the align (which we read below).
2019-12-22 22:42:04 +00:00
let vtable = self
.memory
.get(
vtable,
pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES.len()).unwrap(),
self.tcx.data_layout.pointer_align.abi,
)?
2019-12-22 22:42:04 +00:00
.expect("cannot be a ZST");
let size = vtable
.read_ptr_sized(pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES_SIZE).unwrap())?
.check_init()?;
let size = u64::try_from(self.force_bits(size, pointer_size)?).unwrap();
let align = vtable
.read_ptr_sized(pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES_ALIGN).unwrap())?
.check_init()?;
let align = u64::try_from(self.force_bits(align, pointer_size)?).unwrap();
let align = Align::from_bytes(align).map_err(|e| err_ub!(InvalidVtableAlignment(e)))?;
if size >= self.tcx.data_layout.obj_size_bound() {
throw_ub!(InvalidVtableSize);
}
Ok((Size::from_bytes(size), align))
}
}