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.
7 use rustc::ty::{self, Ty, TyCtxt};
9 use crate::transform::{MirPass, MirSource};
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.)
20 use rustc::mir::Place::*;
23 // Locals and statics have stable addresses, for sure
24 Base(PlaceBase::Local { .. }) |
25 Base(PlaceBase::Static { .. }) =>
27 // Recurse for projections
28 Projection(ref proj) => {
30 // Which place this evaluates to can change with any memory write,
31 // so cannot assume this to be stable.
32 ProjectionElem::Deref =>
34 // Array indices are intersting, but MIR building generates a *fresh*
35 // temporary for every array access, so the index cannot be changed as
37 ProjectionElem::Index { .. } |
38 // The rest is completely boring, they just offset by a constant.
39 ProjectionElem::Field { .. } |
40 ProjectionElem::ConstantIndex { .. } |
41 ProjectionElem::Subslice { .. } |
42 ProjectionElem::Downcast { .. } =>
43 is_stable(&proj.base),
49 /// Determine whether this type may have a reference in it, recursing below compound types but
50 /// not below references.
51 fn may_have_reference<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
53 // Primitive types that are not references
55 ty::Float(_) | ty::Int(_) | ty::Uint(_) |
56 ty::RawPtr(..) | ty::FnPtr(..) |
57 ty::Str | ty::FnDef(..) | ty::Never =>
61 ty::Adt(..) if ty.is_box() => true,
63 ty::Array(ty, ..) | ty::Slice(ty) =>
64 may_have_reference(ty, tcx),
66 tys.iter().any(|ty| may_have_reference(ty.expect_ty(), tcx)),
67 ty::Adt(adt, substs) =>
68 adt.variants.iter().any(|v| v.fields.iter().any(|f|
69 may_have_reference(f.ty(tcx, substs), tcx)
71 // Conservative fallback
76 impl MirPass for AddRetag {
77 fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) {
78 if !tcx.sess.opts.debugging_opts.mir_emit_retag {
81 let (span, arg_count) = (body.span, body.arg_count);
82 let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
83 let needs_retag = |place: &Place<'tcx>| {
84 // FIXME: Instead of giving up for unstable places, we should introduce
85 // a temporary and retag on that.
86 is_stable(place) && may_have_reference(place.ty(&*local_decls, tcx).ty, tcx)
90 // Retag arguments at the beginning of the start block.
92 let source_info = SourceInfo {
93 scope: OUTERMOST_SOURCE_SCOPE,
94 span: span, // FIXME: Consider using just the span covering the function
95 // argument declaration.
97 // Gather all arguments, skip return value.
98 let places = local_decls.iter_enumerated().skip(1).take(arg_count)
99 .map(|(local, _)| Place::from(local))
101 .collect::<Vec<_>>();
102 // Emit their retags.
103 basic_blocks[START_BLOCK].statements.splice(0..0,
104 places.into_iter().map(|place| Statement {
106 kind: StatementKind::Retag(RetagKind::FnEntry, place),
112 // Retag return values of functions. Also escape-to-raw the argument of `drop`.
113 // We collect the return destinations because we cannot mutate while iterating.
114 let mut returns: Vec<(SourceInfo, Place<'tcx>, BasicBlock)> = Vec::new();
115 for block_data in basic_blocks.iter_mut() {
116 match block_data.terminator().kind {
117 TerminatorKind::Call { ref destination, .. } => {
118 // Remember the return destination for later
119 if let Some(ref destination) = destination {
120 if needs_retag(&destination.0) {
122 block_data.terminator().source_info,
123 destination.0.clone(),
129 TerminatorKind::Drop { .. } |
130 TerminatorKind::DropAndReplace { .. } => {
131 // `Drop` is also a call, but it doesn't return anything so we are good.
134 // Not a block ending in a Call -> ignore.
138 // Now we go over the returns we collected to retag the return values.
139 for (source_info, dest_place, dest_block) in returns {
140 basic_blocks[dest_block].statements.insert(0, Statement {
142 kind: StatementKind::Retag(RetagKind::Default, dest_place),
147 // Add retag after assignment.
148 for block_data in basic_blocks {
149 // We want to insert statements as we iterate. To this end, we
150 // iterate backwards using indices.
151 for i in (0..block_data.statements.len()).rev() {
152 let (retag_kind, place) = match block_data.statements[i].kind {
153 // If we are casting *from* a reference, we may have to retag-as-raw.
154 StatementKind::Assign(ref place, box Rvalue::Cast(
159 let src_ty = src.ty(&*local_decls, tcx);
160 if src_ty.is_region_ptr() {
161 // The only `Misc` casts on references are those creating raw pointers.
162 assert!(dest_ty.is_unsafe_ptr());
163 (RetagKind::Raw, place.clone())
165 // Some other cast, no retag
169 // Assignments of reference or ptr type are the ones where we may have
170 // to update tags. This includes `x = &[mut] ...` and hence
171 // we also retag after taking a reference!
172 StatementKind::Assign(ref place, box ref rvalue) if needs_retag(place) => {
173 let kind = match rvalue {
174 Rvalue::Ref(_, borrow_kind, _)
175 if borrow_kind.allows_two_phase_borrow()
181 (kind, place.clone())
183 // Do nothing for the rest
186 // Insert a retag after the statement.
187 let source_info = block_data.statements[i].source_info;
188 block_data.statements.insert(i+1, Statement {
190 kind: StatementKind::Retag(retag_kind, place),