]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/abort_unwinding_calls.rs
Rollup merge of #88305 - ijackson:exitstatus-debug, r=dtolnay
[rust.git] / compiler / rustc_mir_transform / src / abort_unwinding_calls.rs
1 use crate::MirPass;
2 use rustc_hir::def::DefKind;
3 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
4 use rustc_middle::mir::*;
5 use rustc_middle::ty::layout;
6 use rustc_middle::ty::{self, TyCtxt};
7 use rustc_target::spec::abi::Abi;
8 use rustc_target::spec::PanicStrategy;
9
10 /// A pass that runs which is targeted at ensuring that codegen guarantees about
11 /// unwinding are upheld for compilations of panic=abort programs.
12 ///
13 /// When compiling with panic=abort codegen backends generally want to assume
14 /// that all Rust-defined functions do not unwind, and it's UB if they actually
15 /// do unwind. Foreign functions, however, can be declared as "may unwind" via
16 /// their ABI (e.g. `extern "C-unwind"`). To uphold the guarantees that
17 /// Rust-defined functions never unwind a well-behaved Rust program needs to
18 /// catch unwinding from foreign functions and force them to abort.
19 ///
20 /// This pass walks over all functions calls which may possibly unwind,
21 /// and if any are found sets their cleanup to a block that aborts the process.
22 /// This forces all unwinds, in panic=abort mode happening in foreign code, to
23 /// trigger a process abort.
24 #[derive(PartialEq)]
25 pub struct AbortUnwindingCalls;
26
27 impl<'tcx> MirPass<'tcx> for AbortUnwindingCalls {
28     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
29         let def_id = body.source.def_id();
30         let kind = tcx.def_kind(def_id);
31
32         // We don't simplify the MIR of constants at this time because that
33         // namely results in a cyclic query when we call `tcx.type_of` below.
34         let is_function = match kind {
35             DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) => true,
36             _ => tcx.is_closure(def_id),
37         };
38         if !is_function {
39             return;
40         }
41
42         // This pass only runs on functions which themselves cannot unwind,
43         // forcibly changing the body of the function to structurally provide
44         // this guarantee by aborting on an unwind. If this function can unwind,
45         // then there's nothing to do because it already should work correctly.
46         //
47         // Here we test for this function itself whether its ABI allows
48         // unwinding or not.
49         let body_flags = tcx.codegen_fn_attrs(def_id).flags;
50         let body_ty = tcx.type_of(def_id);
51         let body_abi = match body_ty.kind() {
52             ty::FnDef(..) => body_ty.fn_sig(tcx).abi(),
53             ty::Closure(..) => Abi::RustCall,
54             ty::Generator(..) => Abi::Rust,
55             _ => span_bug!(body.span, "unexpected body ty: {:?}", body_ty),
56         };
57         let body_can_unwind = layout::fn_can_unwind(tcx, body_flags, body_abi);
58
59         // Look in this function body for any basic blocks which are terminated
60         // with a function call, and whose function we're calling may unwind.
61         // This will filter to functions with `extern "C-unwind"` ABIs, for
62         // example.
63         let mut calls_to_terminate = Vec::new();
64         let mut cleanups_to_remove = Vec::new();
65         for (id, block) in body.basic_blocks().iter_enumerated() {
66             if block.is_cleanup {
67                 continue;
68             }
69             let terminator = match &block.terminator {
70                 Some(terminator) => terminator,
71                 None => continue,
72             };
73             let span = terminator.source_info.span;
74
75             let call_can_unwind = match &terminator.kind {
76                 TerminatorKind::Call { func, .. } => {
77                     let ty = func.ty(body, tcx);
78                     let sig = ty.fn_sig(tcx);
79                     let flags = match ty.kind() {
80                         ty::FnPtr(_) => CodegenFnAttrFlags::empty(),
81                         ty::FnDef(def_id, _) => tcx.codegen_fn_attrs(*def_id).flags,
82                         _ => span_bug!(span, "invalid callee of type {:?}", ty),
83                     };
84                     layout::fn_can_unwind(tcx, flags, sig.abi())
85                 }
86                 TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => {
87                     tcx.sess.opts.debugging_opts.panic_in_drop == PanicStrategy::Unwind
88                         && layout::fn_can_unwind(tcx, CodegenFnAttrFlags::empty(), Abi::Rust)
89                 }
90                 TerminatorKind::Assert { .. } | TerminatorKind::FalseUnwind { .. } => {
91                     layout::fn_can_unwind(tcx, CodegenFnAttrFlags::empty(), Abi::Rust)
92                 }
93                 _ => continue,
94             };
95
96             // If this function call can't unwind, then there's no need for it
97             // to have a landing pad. This means that we can remove any cleanup
98             // registered for it.
99             if !call_can_unwind {
100                 cleanups_to_remove.push(id);
101                 continue;
102             }
103
104             // Otherwise if this function can unwind, then if the outer function
105             // can also unwind there's nothing to do. If the outer function
106             // can't unwind, however, we need to change the landing pad for this
107             // function call to one that aborts.
108             if !body_can_unwind {
109                 calls_to_terminate.push(id);
110             }
111         }
112
113         // For call instructions which need to be terminated, we insert a
114         // singular basic block which simply terminates, and then configure the
115         // `cleanup` attribute for all calls we found to this basic block we
116         // insert which means that any unwinding that happens in the functions
117         // will force an abort of the process.
118         if !calls_to_terminate.is_empty() {
119             let bb = BasicBlockData {
120                 statements: Vec::new(),
121                 is_cleanup: true,
122                 terminator: Some(Terminator {
123                     source_info: SourceInfo::outermost(body.span),
124                     kind: TerminatorKind::Abort,
125                 }),
126             };
127             let abort_bb = body.basic_blocks_mut().push(bb);
128
129             for bb in calls_to_terminate {
130                 let cleanup = body.basic_blocks_mut()[bb].terminator_mut().unwind_mut().unwrap();
131                 *cleanup = Some(abort_bb);
132             }
133         }
134
135         for id in cleanups_to_remove {
136             let cleanup = body.basic_blocks_mut()[id].terminator_mut().unwind_mut().unwrap();
137             *cleanup = None;
138         }
139
140         // We may have invalidated some `cleanup` blocks so clean those up now.
141         super::simplify::remove_dead_blocks(tcx, body);
142     }
143 }