|
| 1 | +use rustc_data_structures::fx::FxIndexSet; |
| 2 | +use rustc_data_structures::graph::scc::Sccs; |
| 3 | +use rustc_data_structures::graph::{DirectedGraph, Successors}; |
| 4 | +use rustc_hir as hir; |
| 5 | +use rustc_index::{IndexVec, newtype_index}; |
| 6 | +use rustc_middle::mir::interpret::{AllocId, Allocation, ConstAllocation, GlobalAlloc}; |
| 7 | +use rustc_middle::ty::TyCtxt; |
| 8 | +use rustc_span::ErrorGuaranteed; |
| 9 | +use rustc_span::def_id::LocalDefId; |
| 10 | + |
| 11 | +// Graph indices |
| 12 | +newtype_index! { |
| 13 | + struct StaticNodeIdx {} |
| 14 | +} |
| 15 | +newtype_index! { |
| 16 | + #[derive(Ord, PartialOrd)] |
| 17 | + struct StaticSccIdx {} |
| 18 | +} |
| 19 | + |
| 20 | +// Adjacency-list graph |
| 21 | +struct StaticRefGraph { |
| 22 | + succ: IndexVec<StaticNodeIdx, Vec<StaticNodeIdx>>, |
| 23 | +} |
| 24 | + |
| 25 | +impl DirectedGraph for StaticRefGraph { |
| 26 | + type Node = StaticNodeIdx; |
| 27 | + |
| 28 | + fn num_nodes(&self) -> usize { |
| 29 | + self.succ.len() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl Successors for StaticRefGraph { |
| 34 | + fn successors(&self, n: StaticNodeIdx) -> impl Iterator<Item = StaticNodeIdx> { |
| 35 | + self.succ[n].iter().copied() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +pub(crate) fn check_static_initializer_acyclic( |
| 40 | + tcx: TyCtxt<'_>, |
| 41 | + _: (), |
| 42 | +) -> Result<(), ErrorGuaranteed> { |
| 43 | + // Collect local statics |
| 44 | + let statics: FxIndexSet<LocalDefId> = tcx |
| 45 | + .hir_free_items() |
| 46 | + .filter_map(|item_id| { |
| 47 | + let item = tcx.hir_item(item_id); |
| 48 | + match item.kind { |
| 49 | + hir::ItemKind::Static(..) => Some(item.owner_id.def_id), |
| 50 | + _ => None, |
| 51 | + } |
| 52 | + }) |
| 53 | + .collect(); |
| 54 | + |
| 55 | + // Fast path |
| 56 | + if statics.is_empty() { |
| 57 | + return Ok(()); |
| 58 | + } |
| 59 | + |
| 60 | + // For all statics collect all reachable statics to create a graph |
| 61 | + let graph = StaticRefGraph { |
| 62 | + succ: statics |
| 63 | + .iter() |
| 64 | + .map(|&id| { |
| 65 | + if let Ok(root_alloc) = tcx.eval_static_initializer(id) { |
| 66 | + collect_referenced_local_statics(tcx, root_alloc, &statics) |
| 67 | + } else { |
| 68 | + Vec::new() |
| 69 | + } |
| 70 | + }) |
| 71 | + .collect(), |
| 72 | + }; |
| 73 | + |
| 74 | + // Calculate all SCCs from the graph |
| 75 | + let sccs: Sccs<StaticNodeIdx, StaticSccIdx> = Sccs::new(&graph); |
| 76 | + // Group statics by SCCs |
| 77 | + let mut members: IndexVec<StaticSccIdx, Vec<StaticNodeIdx>> = |
| 78 | + IndexVec::from_elem_n(Vec::new(), sccs.num_sccs()); |
| 79 | + for i in graph.succ.indices() { |
| 80 | + members[sccs.scc(i)].push(i); |
| 81 | + } |
| 82 | + let mut first_guar: Option<ErrorGuaranteed> = None; |
| 83 | + |
| 84 | + for scc in sccs.all_sccs() { |
| 85 | + let nodes = &members[scc]; |
| 86 | + let acyclic = match nodes.len() { |
| 87 | + 0 => true, |
| 88 | + 1 => !graph.successors(nodes[0]).any(|x| x == nodes[0]), |
| 89 | + 2.. => false, |
| 90 | + }; |
| 91 | + |
| 92 | + if acyclic { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + let head_def = statics.get_index(nodes[0].into()).unwrap(); |
| 97 | + let head_span = tcx.def_span(*head_def); |
| 98 | + |
| 99 | + let mut diag = tcx.dcx().struct_span_err( |
| 100 | + head_span, |
| 101 | + format!( |
| 102 | + "static initializer forms a cycle involving `{}`", |
| 103 | + tcx.def_path_str(head_def.to_def_id()), |
| 104 | + ), |
| 105 | + ); |
| 106 | + diag.span_labels( |
| 107 | + nodes.iter().map(|&n| tcx.def_span(*statics.get_index(n.into()).unwrap())), |
| 108 | + "part of this cycle", |
| 109 | + ) |
| 110 | + .note(format!( |
| 111 | + "cyclic static initializer references are not supported for target `{}`", |
| 112 | + tcx.sess.target.llvm_target |
| 113 | + )); |
| 114 | + first_guar.get_or_insert(diag.emit()); |
| 115 | + } |
| 116 | + |
| 117 | + match first_guar { |
| 118 | + Some(g) => Err(g), |
| 119 | + None => Ok(()), |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Traverse allocations reachable from the static initializer allocation and collect local-static targets. |
| 124 | +fn collect_referenced_local_statics<'tcx>( |
| 125 | + tcx: TyCtxt<'tcx>, |
| 126 | + root_alloc: ConstAllocation<'tcx>, |
| 127 | + node_of: &FxIndexSet<LocalDefId>, |
| 128 | +) -> Vec<StaticNodeIdx> { |
| 129 | + let mut nodes: Vec<StaticNodeIdx> = Vec::default(); |
| 130 | + let mut alloc_ids: FxIndexSet<AllocId> = FxIndexSet::default(); |
| 131 | + |
| 132 | + let add_ids_from_alloc = |alloc: &Allocation, ids: &mut FxIndexSet<AllocId>| { |
| 133 | + ids.extend(alloc.provenance().ptrs().iter().map(|(_, prov)| prov.alloc_id())); |
| 134 | + }; |
| 135 | + |
| 136 | + // Scan the root allocation for pointers first. |
| 137 | + add_ids_from_alloc(root_alloc.inner(), &mut alloc_ids); |
| 138 | + |
| 139 | + let mut visited_allocs: usize = 0; |
| 140 | + |
| 141 | + while let Some(&alloc_id) = alloc_ids.get_index(visited_allocs) { |
| 142 | + match tcx.global_alloc(alloc_id) { |
| 143 | + GlobalAlloc::Static(def_id) => { |
| 144 | + if let Some(local_def) = def_id.as_local() |
| 145 | + && let Some(node) = node_of.get_index_of(&local_def) |
| 146 | + { |
| 147 | + nodes.push(node.into()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + GlobalAlloc::Memory(const_alloc) => { |
| 152 | + add_ids_from_alloc(const_alloc.inner(), &mut alloc_ids); |
| 153 | + } |
| 154 | + |
| 155 | + _ => { |
| 156 | + // Functions, vtables, etc: ignore |
| 157 | + } |
| 158 | + } |
| 159 | + visited_allocs += 1; |
| 160 | + } |
| 161 | + nodes |
| 162 | +} |
0 commit comments