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        for (a_pred, b_pred) in std::iter::zip(sorted_vtable, sorted_expected) {
90            let is_eq = match (a_pred.skip_binder(), b_pred.skip_binder()) {
91                (
92                    ty::ExistentialPredicate::Trait(a_data),
93                    ty::ExistentialPredicate::Trait(b_data),
94                ) => self.eq_in_param_env(a_pred.rebind(a_data), b_pred.rebind(b_data)),
95
96                (
97                    ty::ExistentialPredicate::Projection(a_data),
98                    ty::ExistentialPredicate::Projection(b_data),
99                ) => self.eq_in_param_env(a_pred.rebind(a_data), b_pred.rebind(b_data)),
100
101                _ => false,
102            };
103            if !is_eq {
104                throw_ub!(InvalidVTableTrait { vtable_dyn_type, expected_dyn_type });
105            }
106        }
107
108        interp_ok(())
109    }
110
111    /// Turn a place with a `dyn Trait` type into a place with the actual dynamic type.
112    pub(super) fn unpack_dyn_trait(
113        &self,
114        mplace: &MPlaceTy<'tcx, M::Provenance>,
115        expected_trait: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
116    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
117        assert!(
118            matches!(mplace.layout.ty.kind(), ty::Dynamic(_, _, ty::Dyn)),
119            "`unpack_dyn_trait` only makes sense on `dyn*` types"
120        );
121        let vtable = mplace.meta().unwrap_meta().to_pointer(self)?;
122        let ty = self.get_ptr_vtable_ty(vtable, Some(expected_trait))?;
123        // This is a kind of transmute, from a place with unsized type and metadata to
124        // a place with sized type and no metadata.
125        let layout = self.layout_of(ty)?;
126        let mplace = mplace.offset_with_meta(
127            Size::ZERO,
128            OffsetMode::Wrapping,
129            MemPlaceMeta::None,
130            layout,
131            self,
132        )?;
133        interp_ok(mplace)
134    }
135
136    /// Turn a `dyn* Trait` type into an value with the actual dynamic type.
137    pub(super) fn unpack_dyn_star<P: Projectable<'tcx, M::Provenance>>(
138        &self,
139        val: &P,
140        expected_trait: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
141    ) -> InterpResult<'tcx, P> {
142        assert!(
143            matches!(val.layout().ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
144            "`unpack_dyn_star` only makes sense on `dyn*` types"
145        );
146        let data = self.project_field(val, 0)?;
147        let vtable = self.project_field(val, 1)?;
148        let vtable = self.read_pointer(&vtable.to_op(self)?)?;
149        let ty = self.get_ptr_vtable_ty(vtable, Some(expected_trait))?;
150        // `data` is already the right thing but has the wrong type. So we transmute it.
151        let layout = self.layout_of(ty)?;
152        let data = data.transmute(layout, self)?;
153        interp_ok(data)
154    }
155}