]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/add_retag.rs
Rollup merge of #99582 - compiler-errors:issue-99566, r=cjgillot
[rust.git] / compiler / rustc_mir_transform / src / add_retag.rs
1 //! This pass adds validation calls (AcquireValid, ReleaseValid) where appropriate.
2 //! It has to be run really early, before transformations like inlining, because
3 //! introducing these calls *adds* UB -- so, conceptually, this pass is actually part
4 //! of MIR building, and only after this pass we think of the program has having the
5 //! normal MIR semantics.
6
7 use crate::MirPass;
8 use rustc_middle::mir::*;
9 use rustc_middle::ty::{self, Ty, TyCtxt};
10
11 pub struct AddRetag;
12
13 /// Determines whether this place is "stable": Whether, if we evaluate it again
14 /// after the assignment, we can be sure to obtain the same place value.
15 /// (Concurrent accesses by other threads are no problem as these are anyway non-atomic
16 /// copies.  Data races are UB.)
17 fn is_stable(place: PlaceRef<'_>) -> bool {
18     // Which place this evaluates to can change with any memory write,
19     // so cannot assume deref to be stable.
20     !place.has_deref()
21 }
22
23 /// Determine whether this type may contain a reference (or box), and thus needs retagging.
24 /// We will only recurse `depth` times into Tuples/ADTs to bound the cost of this.
25 fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> bool {
26     match ty.kind() {
27         // Primitive types that are not references
28         ty::Bool
29         | ty::Char
30         | ty::Float(_)
31         | ty::Int(_)
32         | ty::Uint(_)
33         | ty::RawPtr(..)
34         | ty::FnPtr(..)
35         | ty::Str
36         | ty::FnDef(..)
37         | ty::Never => false,
38         // References
39         ty::Ref(..) => true,
40         ty::Adt(..) if ty.is_box() => true,
41         // Compound types: recurse
42         ty::Array(ty, _) | ty::Slice(ty) => {
43             // This does not branch so we keep the depth the same.
44             may_contain_reference(*ty, depth, tcx)
45         }
46         ty::Tuple(tys) => {
47             depth == 0 || tys.iter().any(|ty| may_contain_reference(ty, depth - 1, tcx))
48         }
49         ty::Adt(adt, subst) => {
50             depth == 0
51                 || adt.variants().iter().any(|v| {
52                     v.fields.iter().any(|f| may_contain_reference(f.ty(tcx, subst), depth - 1, tcx))
53                 })
54         }
55         // Conservative fallback
56         _ => true,
57     }
58 }
59
60 impl<'tcx> MirPass<'tcx> for AddRetag {
61     fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
62         sess.opts.unstable_opts.mir_emit_retag
63     }
64
65     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
66         // We need an `AllCallEdges` pass before we can do any work.
67         super::add_call_guards::AllCallEdges.run_pass(tcx, body);
68
69         let (span, arg_count) = (body.span, body.arg_count);
70         let basic_blocks = body.basic_blocks.as_mut();
71         let local_decls = &body.local_decls;
72         let needs_retag = |place: &Place<'tcx>| {
73             // FIXME: Instead of giving up for unstable places, we should introduce
74             // a temporary and retag on that.
75             is_stable(place.as_ref())
76                 && may_contain_reference(place.ty(&*local_decls, tcx).ty, /*depth*/ 3, tcx)
77                 && !local_decls[place.local].is_deref_temp()
78         };
79         let place_base_raw = |place: &Place<'tcx>| {
80             // If this is a `Deref`, get the type of what we are deref'ing.
81             if place.has_deref() {
82                 let ty = &local_decls[place.local].ty;
83                 ty.is_unsafe_ptr()
84             } else {
85                 // Not a deref, and thus not raw.
86                 false
87             }
88         };
89
90         // PART 1
91         // Retag arguments at the beginning of the start block.
92         {
93             // FIXME: Consider using just the span covering the function
94             // argument declaration.
95             let source_info = SourceInfo::outermost(span);
96             // Gather all arguments, skip return value.
97             let places = local_decls
98                 .iter_enumerated()
99                 .skip(1)
100                 .take(arg_count)
101                 .map(|(local, _)| Place::from(local))
102                 .filter(needs_retag);
103             // Emit their retags.
104             basic_blocks[START_BLOCK].statements.splice(
105                 0..0,
106                 places.map(|place| Statement {
107                     source_info,
108                     kind: StatementKind::Retag(RetagKind::FnEntry, Box::new(place)),
109                 }),
110             );
111         }
112
113         // PART 2
114         // Retag return values of functions.  Also escape-to-raw the argument of `drop`.
115         // We collect the return destinations because we cannot mutate while iterating.
116         let returns = basic_blocks
117             .iter_mut()
118             .filter_map(|block_data| {
119                 match block_data.terminator().kind {
120                     TerminatorKind::Call { target: Some(target), destination, .. }
121                         if needs_retag(&destination) =>
122                     {
123                         // Remember the return destination for later
124                         Some((block_data.terminator().source_info, destination, target))
125                     }
126
127                     // `Drop` is also a call, but it doesn't return anything so we are good.
128                     TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => None,
129                     // Not a block ending in a Call -> ignore.
130                     _ => None,
131                 }
132             })
133             .collect::<Vec<_>>();
134         // Now we go over the returns we collected to retag the return values.
135         for (source_info, dest_place, dest_block) in returns {
136             basic_blocks[dest_block].statements.insert(
137                 0,
138                 Statement {
139                     source_info,
140                     kind: StatementKind::Retag(RetagKind::Default, Box::new(dest_place)),
141                 },
142             );
143         }
144
145         // PART 3
146         // Add retag after assignment.
147         for block_data in basic_blocks {
148             // We want to insert statements as we iterate.  To this end, we
149             // iterate backwards using indices.
150             for i in (0..block_data.statements.len()).rev() {
151                 let (retag_kind, place) = match block_data.statements[i].kind {
152                     // Retag-as-raw after escaping to a raw pointer, if the referent
153                     // is not already a raw pointer.
154                     StatementKind::Assign(box (lplace, Rvalue::AddressOf(_, ref rplace)))
155                         if !place_base_raw(rplace) =>
156                     {
157                         (RetagKind::Raw, lplace)
158                     }
159                     // Retag after assignments of reference type.
160                     StatementKind::Assign(box (ref place, ref rvalue)) if needs_retag(place) => {
161                         let kind = match rvalue {
162                             Rvalue::Ref(_, borrow_kind, _)
163                                 if borrow_kind.allows_two_phase_borrow() =>
164                             {
165                                 RetagKind::TwoPhase
166                             }
167                             _ => RetagKind::Default,
168                         };
169                         (kind, *place)
170                     }
171                     // Do nothing for the rest
172                     _ => continue,
173                 };
174                 // Insert a retag after the statement.
175                 let source_info = block_data.statements[i].source_info;
176                 block_data.statements.insert(
177                     i + 1,
178                     Statement {
179                         source_info,
180                         kind: StatementKind::Retag(retag_kind, Box::new(place)),
181                     },
182                 );
183             }
184         }
185     }
186 }