Skip to content

Commit 67a9bcb

Browse files
committed
Auto merge of #97103 - luqmana:asm-unwind-cleanup, r=Amanieu,tmiasko
Update MIR passes to handle unwinding Inline Asm Some more follow up fixes from #95864 (comment) r? `@Amanieu`
2 parents 50872bd + af7f53d commit 67a9bcb

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

+7
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

+2-2
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
}
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:12:11: 12:11
5+
let _1: (); // in scope 0 at $DIR/asm_unwind_panic_abort.rs:14:9: 14:49
6+
scope 1 {
7+
}
8+
9+
bb0: {
10+
StorageLive(_1); // scope 1 at $DIR/asm_unwind_panic_abort.rs:14:9: 14:49
11+
_1 = const (); // scope 1 at $DIR/asm_unwind_panic_abort.rs:14:9: 14:49
12+
asm!("", options(MAY_UNWIND)) -> [return: bb1, unwind: bb2]; // scope 1 at $DIR/asm_unwind_panic_abort.rs:14:9: 14:49
13+
}
14+
15+
bb1: {
16+
StorageDead(_1); // scope 1 at $DIR/asm_unwind_panic_abort.rs:14:48: 14:49
17+
_0 = const (); // scope 1 at $DIR/asm_unwind_panic_abort.rs:13:5: 15:6
18+
return; // scope 0 at $DIR/asm_unwind_panic_abort.rs:16:2: 16:2
19+
}
20+
21+
bb2 (cleanup): {
22+
abort; // scope 0 at $DIR/asm_unwind_panic_abort.rs:12:1: 16:2
23+
}
24+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// only-x86_64
6+
// compile-flags: -C panic=abort
7+
// no-prefer-dynamic
8+
9+
#![feature(asm_unwind)]
10+
11+
// EMIT_MIR asm_unwind_panic_abort.main.AbortUnwindingCalls.after.mir
12+
fn main() {
13+
unsafe {
14+
std::arch::asm!("", options(may_unwind));
15+
}
16+
}

0 commit comments

Comments
 (0)