Skip to content

Commit 00755e4

Browse files
committed
Auto merge of rust-lang#96959 - nbdd0121:unwind, r=Amanieu
Prevent unwinding when `-C panic=abort` is used regardless declared ABI Ensures that Rust code will abort with `-C panic=abort` regardless ABI used. ```rust extern "C-unwind" { fn may_unwind(); } // Will be nounwind with `-C panic=abort`, despite `C-unwind` ABI. pub unsafe extern "C-unwind" fn rust_item_that_can_unwind() { may_unwind(); } ``` Current behaviour is that unwind will propagate through. While the current behaviour won't cause unsoundness it is inconsistent with the text reading of [RFC2945](https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html). I tweaked `fn_can_unwind` instead of tweaking `AbortUnwindingCalls` because this approach would allow Rust (non-direct) callers to also see that this function is nounwind, so it can prevent excessive landing pads generation. For more discussions: https://rust-lang.zulipchat.com/#narrow/stream/210922-project-ffi-unwind/topic/soundness.20in.20mixed.20panic.20mode. cc `@alexcrichton,` `@BatmanAoD` r? `@Amanieu` `@rustbot` label: T-compiler T-lang F-c_unwind
2 parents 3655175 + f86e409 commit 00755e4

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
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
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
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)]
88

9-
extern "C-unwind" {
10-
fn may_unwind();
11-
}
12-
13-
// CHECK: @rust_item_that_can_unwind() unnamed_addr #0
9+
// CHECK: @rust_item_that_can_unwind() unnamed_addr [[ATTR0:#[0-9]+]]
1410
#[no_mangle]
1511
pub unsafe extern "C-unwind" fn rust_item_that_can_unwind() {
12+
// CHECK: call void @_ZN4core9panicking15panic_no_unwind
1613
may_unwind();
1714
}
1815

16+
extern "C-unwind" {
17+
// CHECK: @may_unwind() unnamed_addr [[ATTR1:#[0-9]+]]
18+
fn may_unwind();
19+
}
20+
1921
// Now, make sure that the LLVM attributes for this functions are correct. First, make
2022
// sure that the first item is correctly marked with the `nounwind` attribute:
2123
//
22-
// CHECK-NOT: attributes #0 = { {{.*}}nounwind{{.*}} }
24+
// CHECK: attributes [[ATTR0]] = { {{.*}}nounwind{{.*}} }
25+
//
26+
// Now, check that foreign item is correctly marked without the `nounwind` attribute.
27+
// CHECK-NOT: attributes [[ATTR1]] = { {{.*}}nounwind{{.*}} }

0 commit comments

Comments
 (0)