rustc_const_eval/interpret/
traits.rs

1use rustc_abi::{Align, Size};
2use rustc_middle::mir::interpret::{InterpResult, Pointer};
3use rustc_middle::ty::layout::LayoutOf;
4use rustc_middle::ty::{self, ExistentialPredicateStableCmpExt, Ty, TyCtxt, VtblEntry};
5use tracing::trace;
6
7use super::util::ensure_monomorphic_enough;
8use super::{
9    InterpCx, MPlaceTy, Machine, MemPlaceMeta, OffsetMode, Projectable, interp_ok, throw_ub,
10};
11
12impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
13    /// Creates a dynamic vtable for the given type and vtable origin. This is used only for
14    /// objects.
15    ///
16    /// The `dyn_ty` encodes the erased self type. Hence, if we are making an object
17    /// `Foo<dyn Trait<Assoc = A> + Send>` from a value of type `Foo<T>`, then `dyn_ty`
18    /// would be `Trait<Assoc = A> + Send`. If this list doesn't have a principal trait ref,
19    /// we only need the basic vtable prefix (drop, size, align).
20    pub fn get_vtable_ptr(
21        &self,
22        ty: Ty<'tcx>,
23        dyn_ty: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
24    ) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
25        trace!("get_vtable(ty={ty:?}, dyn_ty={dyn_ty:?})");
26
27        let (ty, dyn_ty) = self.tcx.erase_regions((ty, dyn_ty));
28
29        // All vtables must be monomorphic, bail out otherwise.
30        ensure_monomorphic_enough(*self.tcx, ty)?;
31        ensure_monomorphic_enough(*self.tcx, dyn_ty)?;
32
33        let salt = M::get_global_alloc_salt(self, None);
34        let vtable_symbolic_allocation = self.tcx.reserve_and_set_vtable_alloc(ty, dyn_ty, salt);
35        let vtable_ptr = self.global_root_pointer(Pointer::from(vtable_symbolic_allocation))?;
36        interp_ok(vtable_ptr.into())
37    }
38
39    pub fn get_vtable_size_and_align(
40        &self,
41        vtable: Pointer<Option<M::Provenance>>,
42        expected_trait: Option<&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>>,
43    ) -> InterpResult<'tcx, (Size, Align)> {
44        let ty = self.get_ptr_vtable_ty(vtable, expected_trait)?;
45        let layout = self.layout_of(ty)?;
46        assert!(layout.is_sized(), "there are no vtables for unsized types");
47        interp_ok((layout.size, layout.align.abi))
48    }
49
50    pub(super) fn vtable_entries(
51        &self,
52        trait_: Option<ty::PolyExistentialTraitRef<'tcx>>,
53        dyn_ty: Ty<'tcx>,
54    ) -> &'tcx [VtblEntry<'tcx>] {
55        if let Some(trait_) = trait_ {
56            let trait_ref = trait_.with_self_ty(*self.tcx, dyn_ty);
57            let trait_ref =
58                self.tcx.erase_regions(self.tcx.instantiate_bound_regions_with_erased(trait_ref));
59            self.tcx.vtable_entries(trait_ref)
60        } else {
61            TyCtxt::COMMON_VTABLE_ENTRIES
62        }
63    }
64
65    /// Check that the given vtable trait is valid for a pointer/reference/place with the given
66    /// expected trait type.
67    pub(super) fn check_vtable_for_type(
68        &self,
69        vtable_dyn_type: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
70        expected_dyn_type: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
71    ) -> InterpResult<'tcx> {
72        // We check validity by comparing the lists of predicates for equality. We *could* instead
73        // check that the dynamic type to which the vtable belongs satisfies all the expected
74        // predicates, but that would likely be a lot slower and seems unnecessarily permissive.
75
76        // FIXME: we are skipping auto traits for now, but might revisit this in the future.
77        let mut sorted_vtable: Vec<_> = vtable_dyn_type.without_auto_traits().collect();
78        let mut sorted_expected: Vec<_> = expected_dyn_type.without_auto_traits().collect();
79        // `skip_binder` here is okay because `stable_cmp` doesn't look at binders
80        sorted_vtable.sort_by(|a, b| a.skip_binder().stable_cmp(*self.tcx, &b.skip_binder()));
81        sorted_vtable.dedup();
82        sorted_expected.sort_by(|a, b| a.skip_binder().stable_cmp(*self.tcx, &b.skip_binder()));
83        sorted_expected.dedup();
84
85        if sorted_vtable.len() != sorted_expected.len() {
86            throw_ub!(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type });
87        }
88
89        // This checks whether there is a subtyping relation between the predicates in either direction.
90        // For example:
91        // - casting between `dyn for<'a> Trait<fn(&'a u8)>` and `dyn Trait<fn(&'static u8)>` is OK
92        // - casting between `dyn Trait<for<'a> fn(&'a u8)>` and either of the above is UB
93        for (a_pred, b_pred) in std::iter::zip(sorted_vtable, sorted_expected) {
94            let a_pred = self.tcx.normalize_erasing_late_bound_regions(self.typing_env, a_pred);
95            let b_pred = self.tcx.normalize_erasing_late_bound_regions(self.typing_env, b_pred);
96
97            if a_pred != b_pred {
98                throw_ub!(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type });
99            }
100        }
101
102        interp_ok(())
103    }
104
105    /// Turn a place with a `dyn Trait` type into a place with the actual dynamic type.
106    pub(super) fn unpack_dyn_trait(
107        &self,
108        mplace: &MPlaceTy<'tcx, M::Provenance>,
109        expected_trait: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
110    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
111        assert!(
112            matches!(mplace.layout.ty.kind(), ty::Dynamic(_, _, ty::Dyn)),
113            "`unpack_dyn_trait` only makes sense on `dyn*` types"
114        );
115        let vtable = mplace.meta().unwrap_meta().to_pointer(self)?;
116        let ty = self.get_ptr_vtable_ty(vtable, Some(expected_trait))?;
117        // This is a kind of transmute, from a place with unsized type and metadata to
118        // a place with sized type and no metadata.
119        let layout = self.layout_of(ty)?;
120        let mplace = mplace.offset_with_meta(
121            Size::ZERO,
122            OffsetMode::Wrapping,
123            MemPlaceMeta::None,
124            layout,
125            self,
126        )?;
127        interp_ok(mplace)
128    }
129
130    /// Turn a `dyn* Trait` type into an value with the actual dynamic type.
131    pub(super) fn unpack_dyn_star<P: Projectable<'tcx, M::Provenance>>(
132        &self,
133        val: &P,
134        expected_trait: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
135    ) -> InterpResult<'tcx, P> {
136        assert!(
137            matches!(val.layout().ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
138            "`unpack_dyn_star` only makes sense on `dyn*` types"
139        );
140        let data = self.project_field(val, 0)?;
141        let vtable = self.project_field(val, 1)?;
142        let vtable = self.read_pointer(&vtable.to_op(self)?)?;
143        let ty = self.get_ptr_vtable_ty(vtable, Some(expected_trait))?;
144        // `data` is already the right thing but has the wrong type. So we transmute it.
145        let layout = self.layout_of(ty)?;
146        let data = data.transmute(layout, self)?;
147        interp_ok(data)
148    }
149}