Skip to content

Commit c586bc3

Browse files
committed
Prevent unwinding when -C panic=abort is used regardless declared ABI
1 parent 6dd6840 commit c586bc3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

compiler/rustc_middle/src/ty/layout.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,14 @@ pub fn fn_can_unwind<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: Option<DefId>, abi: Spe
28882888
return false;
28892889
}
28902890

2891+
// With `-C panic=abort`, all non-FFI functions are required to not unwind.
2892+
//
2893+
// Note that this is true regardless ABI specified on the function -- a `extern "C-unwind"`
2894+
// function defined in Rust is also required to abort.
2895+
if tcx.sess.panic_strategy() == PanicStrategy::Abort && !tcx.is_foreign_item(did) {
2896+
return false;
2897+
}
2898+
28912899
// With -Z panic-in-drop=abort, drop_in_place never unwinds.
28922900
//
28932901
// This is not part of `codegen_fn_attrs` as it can differ between crates

src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// compile-flags: -C panic=abort
22

3-
// Test that `nounwind` atributes are not applied to `C-unwind` extern functions
4-
// even when the code is compiled with `panic=abort`.
3+
// Test that `nounwind` atributes are also applied to extern `C-unwind` Rust functions
4+
// when the code is compiled with `panic=abort`.
55

66
#![crate_type = "lib"]
77
#![feature(c_unwind)]
@@ -19,4 +19,4 @@ pub unsafe extern "C-unwind" fn rust_item_that_can_unwind() {
1919
// Now, make sure that the LLVM attributes for this functions are correct. First, make
2020
// sure that the first item is correctly marked with the `nounwind` attribute:
2121
//
22-
// CHECK-NOT: attributes #0 = { {{.*}}nounwind{{.*}} }
22+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }

0 commit comments

Comments
 (0)