1 use std::convert::TryFrom;
2 use std::fmt;
3 
4 use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar, ScalarMaybeUninit};
5 use crate::ty::{self, Instance, PolyTraitRef, Ty, TyCtxt};
6 use rustc_ast::Mutability;
7 
8 #[derive(Clone, Copy, PartialEq, HashStable)]
9 pub enum VtblEntry<'tcx> {
10     /// destructor of this type (used in vtable header)
11     MetadataDropInPlace,
12     /// layout size of this type (used in vtable header)
13     MetadataSize,
14     /// layout align of this type (used in vtable header)
15     MetadataAlign,
16     /// non-dispatchable associated function that is excluded from trait object
17     Vacant,
18     /// dispatchable associated function
19     Method(Instance<'tcx>),
20     /// pointer to a separate supertrait vtable, can be used by trait upcasting coercion
21     TraitVPtr(PolyTraitRef<'tcx>),
22 }
23 
24 impl<'tcx> fmt::Debug for VtblEntry<'tcx> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result25     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26         // We want to call `Display` on `Instance` and `PolyTraitRef`,
27         // so we implement this manually.
28         match self {
29             VtblEntry::MetadataDropInPlace => write!(f, "MetadataDropInPlace"),
30             VtblEntry::MetadataSize => write!(f, "MetadataSize"),
31             VtblEntry::MetadataAlign => write!(f, "MetadataAlign"),
32             VtblEntry::Vacant => write!(f, "Vacant"),
33             VtblEntry::Method(instance) => write!(f, "Method({})", instance),
34             VtblEntry::TraitVPtr(trait_ref) => write!(f, "TraitVPtr({})", trait_ref),
35         }
36     }
37 }
38 
39 pub const COMMON_VTABLE_ENTRIES: &[VtblEntry<'_>] =
40     &[VtblEntry::MetadataDropInPlace, VtblEntry::MetadataSize, VtblEntry::MetadataAlign];
41 
42 pub const COMMON_VTABLE_ENTRIES_DROPINPLACE: usize = 0;
43 pub const COMMON_VTABLE_ENTRIES_SIZE: usize = 1;
44 pub const COMMON_VTABLE_ENTRIES_ALIGN: usize = 2;
45 
46 /// Retrieves an allocation that represents the contents of a vtable.
47 /// Since this is a query, allocations are cached and not duplicated.
vtable_allocation_provider<'tcx>( tcx: TyCtxt<'tcx>, key: (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), ) -> AllocId48 pub(super) fn vtable_allocation_provider<'tcx>(
49     tcx: TyCtxt<'tcx>,
50     key: (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>),
51 ) -> AllocId {
52     let (ty, poly_trait_ref) = key;
53 
54     let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
55         let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
56         let trait_ref = tcx.erase_regions(trait_ref);
57 
58         tcx.vtable_entries(trait_ref)
59     } else {
60         COMMON_VTABLE_ENTRIES
61     };
62 
63     let layout = tcx
64         .layout_of(ty::ParamEnv::reveal_all().and(ty))
65         .expect("failed to build vtable representation");
66     assert!(!layout.is_unsized(), "can't create a vtable for an unsized type");
67     let size = layout.size.bytes();
68     let align = layout.align.abi.bytes();
69 
70     let ptr_size = tcx.data_layout.pointer_size;
71     let ptr_align = tcx.data_layout.pointer_align.abi;
72 
73     let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap();
74     let mut vtable = Allocation::uninit(vtable_size, ptr_align, /* panic_on_fail */ true).unwrap();
75 
76     // No need to do any alignment checks on the memory accesses below, because we know the
77     // allocation is correctly aligned as we created it above. Also we're only offsetting by
78     // multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.
79 
80     for (idx, entry) in vtable_entries.iter().enumerate() {
81         let idx: u64 = u64::try_from(idx).unwrap();
82         let scalar = match entry {
83             VtblEntry::MetadataDropInPlace => {
84                 let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
85                 let fn_alloc_id = tcx.create_fn_alloc(instance);
86                 let fn_ptr = Pointer::from(fn_alloc_id);
87                 ScalarMaybeUninit::from_pointer(fn_ptr, &tcx)
88             }
89             VtblEntry::MetadataSize => Scalar::from_uint(size, ptr_size).into(),
90             VtblEntry::MetadataAlign => Scalar::from_uint(align, ptr_size).into(),
91             VtblEntry::Vacant => continue,
92             VtblEntry::Method(instance) => {
93                 // Prepare the fn ptr we write into the vtable.
94                 let instance = instance.polymorphize(tcx);
95                 let fn_alloc_id = tcx.create_fn_alloc(instance);
96                 let fn_ptr = Pointer::from(fn_alloc_id);
97                 ScalarMaybeUninit::from_pointer(fn_ptr, &tcx)
98             }
99             VtblEntry::TraitVPtr(trait_ref) => {
100                 let super_trait_ref = trait_ref
101                     .map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
102                 let supertrait_alloc_id = tcx.vtable_allocation((ty, Some(super_trait_ref)));
103                 let vptr = Pointer::from(supertrait_alloc_id);
104                 ScalarMaybeUninit::from_pointer(vptr, &tcx)
105             }
106         };
107         vtable
108             .write_scalar(&tcx, alloc_range(ptr_size * idx, ptr_size), scalar)
109             .expect("failed to build vtable representation");
110     }
111 
112     vtable.mutability = Mutability::Not;
113     tcx.create_memory_alloc(tcx.intern_const_alloc(vtable))
114 }
115