Skip to content

Commit 592255c

Browse files
authored
Rollup merge of rust-lang#97103 - luqmana:asm-unwind-cleanup, r=Amanieu,tmiasko
Update MIR passes to handle unwinding Inline Asm Some more follow up fixes from rust-lang#95864 (comment) r? `@Amanieu`
2 parents 7f292b4 + 39cd1da commit 592255c

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::MirPass;
2+
use rustc_ast::InlineAsmOptions;
23
use rustc_hir::def::DefKind;
34
use rustc_middle::mir::*;
45
use rustc_middle::ty::layout;
@@ -85,6 +86,12 @@ impl<'tcx> MirPass<'tcx> for AbortUnwindingCalls {
8586
TerminatorKind::Assert { .. } | TerminatorKind::FalseUnwind { .. } => {
8687
layout::fn_can_unwind(tcx, None, Abi::Rust)
8788
}
89+
TerminatorKind::InlineAsm { options, .. } => {
90+
options.contains(InlineAsmOptions::MAY_UNWIND)
91+
}
92+
_ if terminator.unwind().is_some() => {
93+
span_bug!(span, "unexpected terminator that may unwind {:?}", terminator)
94+
}
8895
_ => continue,
8996
};
9097

compiler/rustc_mir_transform/src/generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,7 @@ fn can_unwind<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
10421042
| TerminatorKind::Unreachable
10431043
| TerminatorKind::GeneratorDrop
10441044
| TerminatorKind::FalseEdge { .. }
1045-
| TerminatorKind::FalseUnwind { .. }
1046-
| TerminatorKind::InlineAsm { .. } => {}
1045+
| TerminatorKind::FalseUnwind { .. } => {}
10471046

10481047
// Resume will *continue* unwinding, but if there's no other unwinding terminator it
10491048
// will never be reached.
@@ -1057,6 +1056,7 @@ fn can_unwind<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool {
10571056
TerminatorKind::Drop { .. }
10581057
| TerminatorKind::DropAndReplace { .. }
10591058
| TerminatorKind::Call { .. }
1059+
| TerminatorKind::InlineAsm { .. }
10601060
| TerminatorKind::Assert { .. } => return true,
10611061
}
10621062
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// MIR for `main` after AbortUnwindingCalls
2+
3+
fn main() -> () {
4+
let mut _0: (); // return place in scope 0 at $DIR/asm_unwind_panic_abort.rs:11:11: 11:11
5+
let _1: (); // in scope 0 at $DIR/asm_unwind_panic_abort.rs:13:9: 13:49
6+
scope 1 {
7+
}
8+
9+
bb0: {
10+
StorageLive(_1); // scope 1 at $DIR/asm_unwind_panic_abort.rs:13:9: 13:49
11+
_1 = const (); // scope 1 at $DIR/asm_unwind_panic_abort.rs:13:9: 13:49
12+
asm!("", options(MAY_UNWIND)) -> [return: bb1, unwind: bb2]; // scope 1 at $DIR/asm_unwind_panic_abort.rs:13:9: 13:49
13+
}
14+
15+
bb1: {
16+
StorageDead(_1); // scope 1 at $DIR/asm_unwind_panic_abort.rs:13:48: 13:49
17+
_0 = const (); // scope 1 at $DIR/asm_unwind_panic_abort.rs:12:5: 14:6
18+
return; // scope 0 at $DIR/asm_unwind_panic_abort.rs:15:2: 15:2
19+
}
20+
21+
bb2 (cleanup): {
22+
abort; // scope 0 at $DIR/asm_unwind_panic_abort.rs:11:1: 15:2
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Tests that unwinding from an asm block is caught and forced to abort
2+
//! when `-C panic=abort`.
3+
4+
// min-llvm-version: 13.0.0
5+
// compile-flags: -C panic=abort
6+
// no-prefer-dynamic
7+
8+
#![feature(asm_unwind)]
9+
10+
// EMIT_MIR asm_unwind_panic_abort.main.AbortUnwindingCalls.after.mir
11+
fn main() {
12+
unsafe {
13+
std::arch::asm!("", options(may_unwind));
14+
}
15+
}

0 commit comments

Comments
 (0)