2020-03-21 16:17:01 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-05-17 01:20:14 +00:00
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
use rustc_middle::mir::interpret::{InterpResult, Pointer, PointerArithmetic};
|
2021-06-14 10:02:53 +00:00
|
|
|
use rustc_middle::ty::{
|
2022-05-27 03:22:28 +00:00
|
|
|
self, Ty, TyCtxt, COMMON_VTABLE_ENTRIES_ALIGN, COMMON_VTABLE_ENTRIES_DROPINPLACE,
|
|
|
|
COMMON_VTABLE_ENTRIES_SIZE,
|
2021-06-14 10:02:53 +00:00
|
|
|
};
|
2021-06-20 09:43:25 +00:00
|
|
|
use rustc_target::abi::{Align, Size};
|
2018-06-08 02:47:26 +00:00
|
|
|
|
2020-07-24 12:16:54 +00:00
|
|
|
use super::util::ensure_monomorphic_enough;
|
2021-06-20 09:43:25 +00:00
|
|
|
use super::{FnVal, InterpCx, Machine};
|
2020-03-21 12:49:02 +00:00
|
|
|
|
2020-03-16 22:12:42 +00:00
|
|
|
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.
|
2016-09-09 10:51:14 +00:00
|
|
|
///
|
2019-05-17 01:20:14 +00:00
|
|
|
/// The `trait_ref` encodes the erased self type. Hence, if we are
|
2016-09-09 10:51:14 +00:00
|
|
|
/// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
|
2019-05-17 01:20:14 +00:00
|
|
|
/// `trait_ref` would map `T: Trait`.
|
2017-08-10 15:48:38 +00:00
|
|
|
pub fn get_vtable(
|
|
|
|
&mut self,
|
|
|
|
ty: Ty<'tcx>,
|
2018-12-04 11:28:06 +00:00
|
|
|
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
|
2021-08-01 12:09:22 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
|
2018-10-17 10:36:18 +00:00
|
|
|
trace!("get_vtable(trait_ref={:?})", poly_trait_ref);
|
2016-09-09 10:51:14 +00:00
|
|
|
|
2020-10-24 00:21:18 +00:00
|
|
|
let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref));
|
2018-10-12 14:45:17 +00:00
|
|
|
|
2019-08-12 15:59:45 +00:00
|
|
|
// All vtables must be monomorphic, bail out otherwise.
|
2020-07-24 12:16:54 +00:00
|
|
|
ensure_monomorphic_enough(*self.tcx, ty)?;
|
|
|
|
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
|
2019-08-12 15:59:45 +00:00
|
|
|
|
2021-10-07 09:29:01 +00:00
|
|
|
let vtable_allocation = self.tcx.vtable_allocation((ty, poly_trait_ref));
|
2020-12-11 18:42:36 +00:00
|
|
|
|
2022-04-03 17:05:49 +00:00
|
|
|
let vtable_ptr = self.global_base_pointer(Pointer::from(vtable_allocation))?;
|
2016-09-10 13:17:08 +00:00
|
|
|
|
2021-08-01 12:09:22 +00:00
|
|
|
Ok(vtable_ptr.into())
|
2016-09-09 10:51:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
/// Resolves the function at the specified slot in the provided
|
2022-05-27 03:22:28 +00:00
|
|
|
/// vtable. Currently an index of '3' (`TyCtxt::COMMON_VTABLE_ENTRIES.len()`)
|
2021-06-14 10:02:53 +00:00
|
|
|
/// 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,
|
2021-07-12 16:22:15 +00:00
|
|
|
vtable: Pointer<Option<M::PointerTag>>,
|
2020-03-21 12:49:02 +00:00
|
|
|
idx: u64,
|
2019-10-28 23:09:54 +00:00
|
|
|
) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
|
|
|
|
let ptr_size = self.pointer_size();
|
2021-07-12 16:22:15 +00:00
|
|
|
let vtable_slot = vtable.offset(ptr_size * idx, self)?;
|
2019-10-28 23:09:54 +00:00
|
|
|
let vtable_slot = self
|
2022-04-03 17:05:49 +00:00
|
|
|
.get_ptr_alloc(vtable_slot, ptr_size, self.tcx.data_layout.pointer_align.abi)?
|
2019-10-28 23:09:54 +00:00
|
|
|
.expect("cannot be a ZST");
|
2022-04-07 20:22:09 +00:00
|
|
|
let fn_ptr = self.scalar_to_ptr(vtable_slot.read_ptr_sized(Size::ZERO)?.check_init()?)?;
|
2022-04-03 17:05:49 +00:00
|
|
|
self.get_ptr_fn(fn_ptr)
|
2019-10-28 23:09:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
/// Returns the drop fn instance as well as the actual dynamic type.
|
2017-08-10 15:48:38 +00:00
|
|
|
pub fn read_drop_type_from_vtable(
|
|
|
|
&self,
|
2021-07-12 16:22:15 +00:00
|
|
|
vtable: Pointer<Option<M::PointerTag>>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, (ty::Instance<'tcx>, Ty<'tcx>)> {
|
2021-06-14 10:02:53 +00:00
|
|
|
let pointer_size = self.pointer_size();
|
2019-05-17 01:20:14 +00:00
|
|
|
// We don't care about the pointee type; we just want a pointer.
|
2019-06-23 12:26:36 +00:00
|
|
|
let vtable = self
|
2022-04-03 17:05:49 +00:00
|
|
|
.get_ptr_alloc(
|
2021-06-14 10:02:53 +00:00
|
|
|
vtable,
|
2022-05-27 03:22:28 +00:00
|
|
|
pointer_size * u64::try_from(TyCtxt::COMMON_VTABLE_ENTRIES.len()).unwrap(),
|
2021-06-14 10:02:53 +00:00
|
|
|
self.tcx.data_layout.pointer_align.abi,
|
|
|
|
)?
|
2019-06-23 12:26:36 +00:00
|
|
|
.expect("cannot be a ZST");
|
2021-06-14 10:02:53 +00:00
|
|
|
let drop_fn = vtable
|
|
|
|
.read_ptr_sized(
|
|
|
|
pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES_DROPINPLACE).unwrap(),
|
|
|
|
)?
|
|
|
|
.check_init()?;
|
2019-06-30 11:51:18 +00:00
|
|
|
// We *need* an instance here, no other kind of function value, to be able
|
|
|
|
// to determine the type.
|
2022-04-07 20:22:09 +00:00
|
|
|
let drop_instance = self.get_ptr_fn(self.scalar_to_ptr(drop_fn)?)?.as_instance()?;
|
2018-08-25 12:36:24 +00:00
|
|
|
trace!("Found drop fn: {:?}", drop_instance);
|
2020-06-22 12:57:03 +00:00
|
|
|
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);
|
2019-06-23 12:26:36 +00:00
|
|
|
// 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 {
|
2021-06-12 11:13:38 +00:00
|
|
|
throw_ub!(InvalidVtableDropFn(fn_sig));
|
2019-12-12 14:23:27 +00:00
|
|
|
}
|
2021-06-12 11:13:38 +00:00
|
|
|
let ty =
|
|
|
|
args[0].builtin_deref(true).ok_or_else(|| err_ub!(InvalidVtableDropFn(fn_sig)))?.ty;
|
2018-08-25 12:36:24 +00:00
|
|
|
Ok((drop_instance, ty))
|
2017-03-22 16:48:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
pub fn read_size_and_align_from_vtable(
|
|
|
|
&self,
|
2021-07-12 16:22:15 +00:00
|
|
|
vtable: Pointer<Option<M::PointerTag>>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, (Size, Align)> {
|
2018-08-26 18:42:52 +00:00
|
|
|
let pointer_size = self.pointer_size();
|
2019-05-17 01:20:14 +00:00
|
|
|
// 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-06-23 12:26:36 +00:00
|
|
|
let vtable = self
|
2022-04-03 17:05:49 +00:00
|
|
|
.get_ptr_alloc(
|
2021-06-14 10:02:53 +00:00
|
|
|
vtable,
|
2022-05-27 03:22:28 +00:00
|
|
|
pointer_size * u64::try_from(TyCtxt::COMMON_VTABLE_ENTRIES.len()).unwrap(),
|
2021-06-14 10:02:53 +00:00
|
|
|
self.tcx.data_layout.pointer_align.abi,
|
|
|
|
)?
|
2019-06-23 12:26:36 +00:00
|
|
|
.expect("cannot be a ZST");
|
2021-06-14 10:02:53 +00:00
|
|
|
let size = vtable
|
|
|
|
.read_ptr_sized(pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES_SIZE).unwrap())?
|
|
|
|
.check_init()?;
|
2021-07-12 16:22:15 +00:00
|
|
|
let size = size.to_machine_usize(self)?;
|
2022-03-27 23:34:16 +00:00
|
|
|
let size = Size::from_bytes(size);
|
2021-06-14 10:02:53 +00:00
|
|
|
let align = vtable
|
|
|
|
.read_ptr_sized(pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES_ALIGN).unwrap())?
|
|
|
|
.check_init()?;
|
2021-07-12 16:22:15 +00:00
|
|
|
let align = align.to_machine_usize(self)?;
|
2021-06-12 11:13:38 +00:00
|
|
|
let align = Align::from_bytes(align).map_err(|e| err_ub!(InvalidVtableAlignment(e)))?;
|
2019-08-27 10:54:46 +00:00
|
|
|
|
2022-03-27 23:34:16 +00:00
|
|
|
if size > self.max_size_of_val() {
|
2021-06-12 11:13:38 +00:00
|
|
|
throw_ub!(InvalidVtableSize);
|
2019-08-27 10:54:46 +00:00
|
|
|
}
|
2022-03-27 23:34:16 +00:00
|
|
|
Ok((size, align))
|
2017-02-10 11:12:33 +00:00
|
|
|
}
|
2021-07-31 14:46:23 +00:00
|
|
|
|
|
|
|
pub fn read_new_vtable_after_trait_upcasting_from_vtable(
|
|
|
|
&self,
|
|
|
|
vtable: Pointer<Option<M::PointerTag>>,
|
|
|
|
idx: u64,
|
2021-08-01 12:09:22 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
|
2021-07-31 14:46:23 +00:00
|
|
|
let pointer_size = self.pointer_size();
|
|
|
|
|
2021-08-01 12:09:22 +00:00
|
|
|
let vtable_slot = vtable.offset(pointer_size * idx, self)?;
|
|
|
|
let new_vtable = self
|
2022-04-03 17:05:49 +00:00
|
|
|
.get_ptr_alloc(vtable_slot, pointer_size, self.tcx.data_layout.pointer_align.abi)?
|
2021-07-31 14:46:23 +00:00
|
|
|
.expect("cannot be a ZST");
|
2021-08-01 12:09:22 +00:00
|
|
|
|
2022-04-07 20:22:09 +00:00
|
|
|
let new_vtable =
|
|
|
|
self.scalar_to_ptr(new_vtable.read_ptr_sized(Size::ZERO)?.check_init()?)?;
|
2021-07-31 14:46:23 +00:00
|
|
|
|
|
|
|
Ok(new_vtable)
|
|
|
|
}
|
2016-09-09 10:51:14 +00:00
|
|
|
}
|