|
| 1 | +//! Call graph metadata |
| 2 | +
|
| 3 | +use crate::hir::def_id::{DefId, LOCAL_CRATE}; |
| 4 | +use crate::ty::subst::SubstsRef; |
| 5 | +use crate::ty::{ |
| 6 | + ExistentialTraitRef, Instance, InstanceDef, ParamEnv, PolyTraitRef, TraitRef, Ty, TyCtxt, |
| 7 | +}; |
| 8 | +use crate::mir::interpret::{AllocKind, Allocation}; |
| 9 | + |
| 10 | +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
| 11 | +use rustc_macros::HashStable; |
| 12 | + |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +/// Call graph metadata loaded from an external crate |
| 16 | +#[derive(HashStable)] |
| 17 | +pub struct ExternCallGraphMetadata<'tcx> { |
| 18 | + /// list of functions casted / coerced into function pointers |
| 19 | + pub function_pointers: Vec<(DefId, SubstsRef<'tcx>)>, |
| 20 | + |
| 21 | + /// list of types casted / coerced into trait objects |
| 22 | + pub trait_objects: Vec<TraitRef<'tcx>>, |
| 23 | + |
| 24 | + /// list of types whose drop glue may be invoked by trait object drop glue |
| 25 | + pub dynamic_drop_glue: Vec<(Ty<'tcx>, Vec<ExistentialTraitRef<'tcx>>)>, |
| 26 | +} |
| 27 | + |
| 28 | +impl<'tcx> ExternCallGraphMetadata<'tcx> { |
| 29 | + pub fn empty() -> Self { |
| 30 | + ExternCallGraphMetadata { |
| 31 | + function_pointers: vec![], |
| 32 | + trait_objects: vec![], |
| 33 | + dynamic_drop_glue: vec![], |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Local call graph metadata |
| 39 | +#[derive(Clone, Default, HashStable)] |
| 40 | +pub struct LocalCallGraphMetadata<'tcx> { |
| 41 | + /// list of functions casted / coerced into function pointers |
| 42 | + pub function_pointers: FxHashSet<(DefId, SubstsRef<'tcx>)>, |
| 43 | + |
| 44 | + /// list of types casted / coerced into trait objects |
| 45 | + pub trait_objects: FxHashSet<TraitRef<'tcx>>, |
| 46 | + |
| 47 | + /// list of types whose drop glue may be invoked by trait object drop glue |
| 48 | + pub dynamic_drop_glue: FxHashMap<Ty<'tcx>, FxHashSet<ExistentialTraitRef<'tcx>>>, |
| 49 | +} |
| 50 | + |
| 51 | +impl<'tcx> LocalCallGraphMetadata<'tcx> { |
| 52 | + /// Registers `alloc` as a const-evaluated trait object |
| 53 | + /// |
| 54 | + /// Returns `false` if `alloc` was *not* a trait object |
| 55 | + pub fn register_const_trait_object(&mut self, |
| 56 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 57 | + alloc: &'tcx Allocation) -> bool { |
| 58 | + let has_drop_glue = alloc.relocations.values().any(|(_, inner)| { |
| 59 | + let kind = tcx.alloc_map.lock().get(*inner); |
| 60 | + |
| 61 | + if let Some(AllocKind::Function(instance)) = kind { |
| 62 | + if let InstanceDef::DropGlue(..) = instance.def { |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + false |
| 68 | + }); |
| 69 | + |
| 70 | + if has_drop_glue { |
| 71 | + for (_, inner) in alloc.relocations.values() { |
| 72 | + let kind = tcx.alloc_map.lock().get(*inner); |
| 73 | + |
| 74 | + if let Some(AllocKind::Function(method)) = kind { |
| 75 | + self.register_dynamic_dispatch(tcx, method); |
| 76 | + } else { |
| 77 | + bug!("unexpected `AllocKind`: {:?}", kind); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + has_drop_glue |
| 83 | + } |
| 84 | + |
| 85 | + /// Registers that `method` is dynamically dispatched |
| 86 | + pub fn register_dynamic_dispatch(&mut self, |
| 87 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 88 | + method: Instance<'tcx>) { |
| 89 | + if let Some((trait_ref, _)) = method.trait_ref_and_method(tcx) { |
| 90 | + self.trait_objects.insert(trait_ref); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// Registers that `function` is casted / coerced into a function pointer |
| 95 | + pub fn register_function_pointer(&mut self, function: Instance<'tcx>) { |
| 96 | + self.function_pointers.insert((function.def_id(), function.substs)); |
| 97 | + } |
| 98 | + |
| 99 | + /// Registers that `poly_trait_ref` is used as a trait object |
| 100 | + pub fn register_trait_object(&mut self, |
| 101 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 102 | + poly_trait_ref: PolyTraitRef<'tcx>) { |
| 103 | + let trait_ref = tcx.normalize_erasing_late_bound_regions( |
| 104 | + ParamEnv::reveal_all(), |
| 105 | + &poly_trait_ref, |
| 106 | + ); |
| 107 | + |
| 108 | + self.trait_objects.insert(trait_ref); |
| 109 | + |
| 110 | + let self_ty = trait_ref.self_ty(); |
| 111 | + let existential_trait_ref = ExistentialTraitRef::erase_self_ty(tcx, trait_ref); |
| 112 | + |
| 113 | + self.dynamic_drop_glue.entry(self_ty).or_default().insert(existential_trait_ref); |
| 114 | + } |
| 115 | + |
| 116 | + pub fn extend(&mut self, other: Self) { |
| 117 | + self.function_pointers.extend(other.function_pointers); |
| 118 | + self.trait_objects.extend(other.trait_objects); |
| 119 | + |
| 120 | + for (ty, traits) in other.dynamic_drop_glue { |
| 121 | + self.dynamic_drop_glue.entry(ty).or_default().extend(traits); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/// Local and external call graph metadata |
| 127 | +#[derive(HashStable)] |
| 128 | +pub struct AllCallGraphMetadata<'tcx> { |
| 129 | + /// list of functions casted / coerced into function pointers |
| 130 | + pub function_pointers: FxHashSet<(DefId, SubstsRef<'tcx>)>, |
| 131 | + |
| 132 | + /// list of types casted / coerced into trait objects |
| 133 | + pub trait_objects: FxHashSet<TraitRef<'tcx>>, |
| 134 | + |
| 135 | + /// list of types whose drop glue may be invoked by trait object drop glue |
| 136 | + pub dynamic_drop_glue: FxHashMap<Ty<'tcx>, Arc<FxHashSet<ExistentialTraitRef<'tcx>>>>, |
| 137 | +} |
| 138 | + |
| 139 | +pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> AllCallGraphMetadata<'tcx> { |
| 140 | + let mut cg = (*tcx.local_call_graph_metadata(LOCAL_CRATE)).clone(); |
| 141 | + |
| 142 | + for &cnum in tcx.crates().iter() { |
| 143 | + let ext = tcx.extern_call_graph_metadata(cnum); |
| 144 | + |
| 145 | + cg.function_pointers.extend(ext.function_pointers.iter().cloned()); |
| 146 | + cg.trait_objects.extend(ext.trait_objects.iter().cloned()); |
| 147 | + |
| 148 | + for (ty, traits) in &ext.dynamic_drop_glue { |
| 149 | + cg.dynamic_drop_glue.entry(*ty).or_default().extend(traits.iter().cloned()); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + AllCallGraphMetadata { |
| 154 | + function_pointers: cg.function_pointers, |
| 155 | + trait_objects: cg.trait_objects, |
| 156 | + dynamic_drop_glue: cg.dynamic_drop_glue.into_iter().map(|(ty, traits)| { |
| 157 | + (ty, Arc::new(traits)) |
| 158 | + }).collect(), |
| 159 | + } |
| 160 | +} |
0 commit comments