Skip to content

Commit

Permalink
Prevent unwinding when -C panic=abort is used regardless declared ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed May 11, 2022
1 parent 6dd6840 commit c586bc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,14 @@ pub fn fn_can_unwind<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: Option<DefId>, abi: Spe
return false;
}

// With `-C panic=abort`, all non-FFI functions are required to not unwind.
//
// Note that this is true regardless ABI specified on the function -- a `extern "C-unwind"`
// function defined in Rust is also required to abort.
if tcx.sess.panic_strategy() == PanicStrategy::Abort && !tcx.is_foreign_item(did) {
return false;
}

// With -Z panic-in-drop=abort, drop_in_place never unwinds.
//
// This is not part of `codegen_fn_attrs` as it can differ between crates
Expand Down
6 changes: 3 additions & 3 deletions src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// compile-flags: -C panic=abort

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

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

0 comments on commit c586bc3

Please sign in to comment.