Skip to content

Commit 0f33e9f

Browse files
author
katelyn a. martin
committed
implement unwinding abi's (RFC 2945)
### Changes This commit implements unwind ABI's, specified in RFC 2945. We adjust the `rustc_middle::ty::layout::fn_can_unwind` function, used to compute whether or not a `FnAbi` object represents a function that should be able to unwind when `panic=unwind` is in use. Changes are also made to `rustc_mir_build::build::should_abort_on_panic` so that the function ABI is used to determind whether it should abort, assuming that the `panic=unwind` strategy is being used, and no explicit unwind attribute was provided. ### Tests Unit tests, checking that the behavior is correct for `C-unwind`, `stdcall-unwind`, `system-unwind`, and `thiscall-unwind`, are included. These alternative `unwind` ABI strings are specified in RFC 2945, in the "_Other `unwind` ABI strings_" section. Additionally, a test case is included to assert that the LLVM IR generated for an external function defined with the `C-unwind` ABI will be appropriately labeled with the `nounwind` LLVM attribute when the `panic=abort` compilation flag is used. ### Ignore Directives This commit uses `ignore-*` directives in two of our `*-unwind` ABI test cases. Specifically, the `stdcall-unwind` and `thiscall-unwind` test cases ignore architectures that do not support `stdcall` and `thiscall`, respectively. These directives are cribbed from `src/test/ui/c-variadic/variadic-ffi-1.rs` for `stdcall`, and `src/test/ui/extern/extern-thiscall.rs` for `thiscall`.
1 parent df45c57 commit 0f33e9f

File tree

7 files changed

+176
-16
lines changed

7 files changed

+176
-16
lines changed

compiler/rustc_middle/src/ty/layout.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,7 @@ fn fn_can_unwind(
25622562
panic_strategy: PanicStrategy,
25632563
codegen_fn_attr_flags: CodegenFnAttrFlags,
25642564
call_conv: Conv,
2565+
abi: SpecAbi,
25652566
) -> bool {
25662567
if panic_strategy != PanicStrategy::Unwind {
25672568
// In panic=abort mode we assume nothing can unwind anywhere, so
@@ -2586,17 +2587,16 @@ fn fn_can_unwind(
25862587
//
25872588
// 2. A Rust item using a non-Rust ABI (like `extern "C" fn foo() { ... }`).
25882589
//
2589-
// Foreign items (case 1) are assumed to not unwind; it is
2590-
// UB otherwise. (At least for now; see also
2591-
// rust-lang/rust#63909 and Rust RFC 2753.)
2592-
//
2593-
// Items defined in Rust with non-Rust ABIs (case 2) are also
2594-
// not supposed to unwind. Whether this should be enforced
2595-
// (versus stating it is UB) and *how* it would be enforced
2596-
// is currently under discussion; see rust-lang/rust#58794.
2597-
//
2598-
// In either case, we mark item as explicitly nounwind.
2599-
false
2590+
// In both of these cases, we should refer to the ABI to determine whether or not we
2591+
// should unwind. See Rust RFC 2945 for more information on this behavior, here:
2592+
// https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md
2593+
use SpecAbi::*;
2594+
match abi {
2595+
C { unwind } | Stdcall { unwind } | System { unwind } | Thiscall { unwind } => {
2596+
unwind
2597+
}
2598+
_ => false,
2599+
}
26002600
}
26012601
}
26022602
}
@@ -2823,7 +2823,12 @@ where
28232823
c_variadic: sig.c_variadic,
28242824
fixed_count: inputs.len(),
28252825
conv,
2826-
can_unwind: fn_can_unwind(cx.tcx().sess.panic_strategy(), codegen_fn_attr_flags, conv),
2826+
can_unwind: fn_can_unwind(
2827+
cx.tcx().sess.panic_strategy(),
2828+
codegen_fn_attr_flags,
2829+
conv,
2830+
sig.abi,
2831+
),
28272832
};
28282833
fn_abi.adjust_for_abi(cx, sig.abi);
28292834
debug!("FnAbi::new_internal = {:?}", fn_abi);

compiler/rustc_mir_build/src/build/mod.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ macro_rules! unpack {
548548
}};
549549
}
550550

551-
fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, _abi: Abi) -> bool {
551+
fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, abi: Abi) -> bool {
552552
// Validate `#[unwind]` syntax regardless of platform-specific panic strategy.
553553
let attrs = &tcx.get_attrs(fn_def_id.to_def_id());
554554
let unwind_attr = attr::find_unwind_attr(&tcx.sess, attrs);
@@ -558,12 +558,26 @@ fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, _abi: Abi) -> b
558558
return false;
559559
}
560560

561-
// This is a special case: some functions have a C abi but are meant to
562-
// unwind anyway. Don't stop them.
563561
match unwind_attr {
564-
None => false, // FIXME(#58794); should be `!(abi == Abi::Rust || abi == Abi::RustCall)`
562+
// If an `#[unwind]` attribute was found, we should adhere to it.
565563
Some(UnwindAttr::Allowed) => false,
566564
Some(UnwindAttr::Aborts) => true,
565+
// If no attribute was found and the panic strategy is `unwind`, then we should examine
566+
// the function's ABI string to determine whether it should abort upon panic.
567+
None => {
568+
use Abi::*;
569+
match abi {
570+
// In the case of ABI's that have an `-unwind` equivalent, check whether the ABI
571+
// permits unwinding. If so, we should not abort. Otherwise, we should.
572+
C { unwind } | Stdcall { unwind } | System { unwind } | Thiscall { unwind } => {
573+
!unwind
574+
}
575+
// Rust and `rust-call` functions are allowed to unwind, and should not abort.
576+
Rust | RustCall => false,
577+
// Other ABI's should abort.
578+
_ => true,
579+
}
580+
}
567581
}
568582
}
569583

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -C panic=abort -C opt-level=0
2+
3+
// Test that `nounwind` atributes are applied to `C-unwind` extern functions when the
4+
// code is compiled with `panic=abort`. We disable optimizations above to prevent LLVM from
5+
// inferring the attribute.
6+
7+
#![crate_type = "lib"]
8+
#![feature(c_unwind)]
9+
10+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #0 {
11+
#[no_mangle]
12+
pub extern "C-unwind" fn rust_item_that_can_unwind() {
13+
}
14+
15+
// Now, make sure that the LLVM attributes for this functions are correct. First, make
16+
// sure that the first item is correctly marked with the `nounwind` attribute:
17+
//
18+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// compile-flags: -C opt-level=0
2+
3+
// Test that `nounwind` atributes are correctly applied to exported `C` and `C-unwind` extern
4+
// functions. `C-unwind` functions MUST NOT have this attribute. We disable optimizations above
5+
// to prevent LLVM from inferring the attribute.
6+
7+
#![crate_type = "lib"]
8+
#![feature(c_unwind)]
9+
10+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
11+
#[no_mangle]
12+
pub extern "C" fn rust_item_that_cannot_unwind() {
13+
}
14+
15+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1 {
16+
#[no_mangle]
17+
pub extern "C-unwind" fn rust_item_that_can_unwind() {
18+
}
19+
20+
// Now, make some assertions that the LLVM attributes for these functions are correct. First, make
21+
// sure that the first item is correctly marked with the `nounwind` attribute:
22+
//
23+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }
24+
//
25+
// Next, let's assert that the second item, which CAN unwind, does not have this attribute.
26+
//
27+
// CHECK: attributes #1 = {
28+
// CHECK-NOT: nounwind
29+
// CHECK: }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// compile-flags: -C opt-level=0
2+
// ignore-arm stdcall isn't supported
3+
// ignore-aarch64 stdcall isn't supported
4+
// ignore-riscv64 stdcall isn't supported
5+
6+
// Test that `nounwind` atributes are correctly applied to exported `stdcall` and `stdcall-unwind`
7+
// extern functions. `stdcall-unwind` functions MUST NOT have this attribute. We disable
8+
// optimizations above to prevent LLVM from inferring the attribute.
9+
10+
#![crate_type = "lib"]
11+
#![feature(c_unwind)]
12+
13+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
14+
#[no_mangle]
15+
pub extern "stdcall" fn rust_item_that_cannot_unwind() {
16+
}
17+
18+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1 {
19+
#[no_mangle]
20+
pub extern "stdcall-unwind" fn rust_item_that_can_unwind() {
21+
}
22+
23+
// Now, make some assertions that the LLVM attributes for these functions are correct. First, make
24+
// sure that the first item is correctly marked with the `nounwind` attribute:
25+
//
26+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }
27+
//
28+
// Next, let's assert that the second item, which CAN unwind, does not have this attribute.
29+
//
30+
// CHECK: attributes #1 = {
31+
// CHECK-NOT: nounwind
32+
// CHECK: }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// compile-flags: -C opt-level=0
2+
3+
// Test that `nounwind` atributes are correctly applied to exported `system` and `system-unwind`
4+
// extern functions. `system-unwind` functions MUST NOT have this attribute. We disable
5+
// optimizations above to prevent LLVM from inferring the attribute.
6+
7+
#![crate_type = "lib"]
8+
#![feature(c_unwind)]
9+
10+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
11+
#[no_mangle]
12+
pub extern "system" fn rust_item_that_cannot_unwind() {
13+
}
14+
15+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1 {
16+
#[no_mangle]
17+
pub extern "system-unwind" fn rust_item_that_can_unwind() {
18+
}
19+
20+
// Now, make some assertions that the LLVM attributes for these functions are correct. First, make
21+
// sure that the first item is correctly marked with the `nounwind` attribute:
22+
//
23+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }
24+
//
25+
// Next, let's assert that the second item, which CAN unwind, does not have this attribute.
26+
//
27+
// CHECK: attributes #1 = {
28+
// CHECK-NOT: nounwind
29+
// CHECK: }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// compile-flags: -C opt-level=0
2+
// ignore-arm thiscall isn't supported
3+
// ignore-aarch64 thiscall isn't supported
4+
// ignore-riscv64 thiscall isn't supported
5+
6+
// Test that `nounwind` atributes are correctly applied to exported `thiscall` and
7+
// `thiscall-unwind` extern functions. `thiscall-unwind` functions MUST NOT have this attribute. We
8+
// disable optimizations above to prevent LLVM from inferring the attribute.
9+
10+
#![crate_type = "lib"]
11+
#![feature(abi_thiscall)]
12+
#![feature(c_unwind)]
13+
14+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
15+
#[no_mangle]
16+
pub extern "thiscall" fn rust_item_that_cannot_unwind() {
17+
}
18+
19+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1 {
20+
#[no_mangle]
21+
pub extern "thiscall-unwind" fn rust_item_that_can_unwind() {
22+
}
23+
24+
// Now, make some assertions that the LLVM attributes for these functions are correct. First, make
25+
// sure that the first item is correctly marked with the `nounwind` attribute:
26+
//
27+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }
28+
//
29+
// Next, let's assert that the second item, which CAN unwind, does not have this attribute.
30+
//
31+
// CHECK: attributes #1 = {
32+
// CHECK-NOT: nounwind
33+
// CHECK: }

0 commit comments

Comments
 (0)