Skip to content

Commit 1aaab8b

Browse files
committed
Auto merge of #116088 - nbdd0121:unwind, r=Amanieu,RalfJung
Stabilise `c_unwind` Fix #74990 Fix #115285 (that's also where FCP is happening) Marking as draft PR for now due to `compiler_builtins` issues r? `@Amanieu`
2 parents 1d96de2 + bb2716e commit 1aaab8b

File tree

64 files changed

+52
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+52
-186
lines changed

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ declare_features! (
8080
(accepted, braced_empty_structs, "1.8.0", Some(29720)),
8181
/// Allows `c"foo"` literals.
8282
(accepted, c_str_literals, "1.77.0", Some(105723)),
83+
/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries and treat `extern "C" fn` as nounwind.
84+
(accepted, c_unwind, "CURRENT_RUSTC_VERSION", Some(74990)),
8385
/// Allows `#[cfg_attr(predicate, multiple, attributes, here)]`.
8486
(accepted, cfg_attr_multi, "1.33.0", Some(54881)),
8587
/// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,6 @@ declare_features! (
363363
(unstable, async_for_loop, "1.77.0", Some(118898)),
364364
/// Allows builtin # foo() syntax
365365
(unstable, builtin_syntax, "1.71.0", Some(110680)),
366-
/// Treat `extern "C"` function as nounwind.
367-
(unstable, c_unwind, "1.52.0", Some(74990)),
368366
/// Allows using C-variadics.
369367
(unstable, c_variadic, "1.34.0", Some(44930)),
370368
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.

compiler/rustc_middle/src/ty/layout.rs

+1-35
Original file line numberDiff line numberDiff line change
@@ -1182,37 +1182,6 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) ->
11821182
// ABIs have such an option. Otherwise the only other thing here is Rust
11831183
// itself, and those ABIs are determined by the panic strategy configured
11841184
// for this compilation.
1185-
//
1186-
// Unfortunately at this time there's also another caveat. Rust [RFC
1187-
// 2945][rfc] has been accepted and is in the process of being implemented
1188-
// and stabilized. In this interim state we need to deal with historical
1189-
// rustc behavior as well as plan for future rustc behavior.
1190-
//
1191-
// Historically functions declared with `extern "C"` were marked at the
1192-
// codegen layer as `nounwind`. This happened regardless of `panic=unwind`
1193-
// or not. This is UB for functions in `panic=unwind` mode that then
1194-
// actually panic and unwind. Note that this behavior is true for both
1195-
// externally declared functions as well as Rust-defined function.
1196-
//
1197-
// To fix this UB rustc would like to change in the future to catch unwinds
1198-
// from function calls that may unwind within a Rust-defined `extern "C"`
1199-
// function and forcibly abort the process, thereby respecting the
1200-
// `nounwind` attribute emitted for `extern "C"`. This behavior change isn't
1201-
// ready to roll out, so determining whether or not the `C` family of ABIs
1202-
// unwinds is conditional not only on their definition but also whether the
1203-
// `#![feature(c_unwind)]` feature gate is active.
1204-
//
1205-
// Note that this means that unlike historical compilers rustc now, by
1206-
// default, unconditionally thinks that the `C` ABI may unwind. This will
1207-
// prevent some optimization opportunities, however, so we try to scope this
1208-
// change and only assume that `C` unwinds with `panic=unwind` (as opposed
1209-
// to `panic=abort`).
1210-
//
1211-
// Eventually the check against `c_unwind` here will ideally get removed and
1212-
// this'll be a little cleaner as it'll be a straightforward check of the
1213-
// ABI.
1214-
//
1215-
// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md
12161185
use SpecAbi::*;
12171186
match abi {
12181187
C { unwind }
@@ -1224,10 +1193,7 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) ->
12241193
| Thiscall { unwind }
12251194
| Aapcs { unwind }
12261195
| Win64 { unwind }
1227-
| SysV64 { unwind } => {
1228-
unwind
1229-
|| (!tcx.features().c_unwind && tcx.sess.panic_strategy() == PanicStrategy::Unwind)
1230-
}
1196+
| SysV64 { unwind } => unwind,
12311197
PtxKernel
12321198
| Msp430Interrupt
12331199
| X86Interrupt

compiler/rustc_mir_transform/src/ffi_unwind_calls.rs

+1-33
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,6 @@ use rustc_target::spec::PanicStrategy;
1111

1212
use crate::errors;
1313

14-
/// Some of the functions declared as "may unwind" by `fn_can_unwind` can't actually unwind. In
15-
/// particular, `extern "C"` is still considered as can-unwind on stable, but we need to consider
16-
/// it cannot-unwind here. So below we check `fn_can_unwind() && abi_can_unwind()` before concluding
17-
/// that a function call can unwind.
18-
fn abi_can_unwind(abi: Abi) -> bool {
19-
use Abi::*;
20-
match abi {
21-
C { unwind }
22-
| System { unwind }
23-
| Cdecl { unwind }
24-
| Stdcall { unwind }
25-
| Fastcall { unwind }
26-
| Vectorcall { unwind }
27-
| Thiscall { unwind }
28-
| Aapcs { unwind }
29-
| Win64 { unwind }
30-
| SysV64 { unwind } => unwind,
31-
PtxKernel
32-
| Msp430Interrupt
33-
| X86Interrupt
34-
| EfiApi
35-
| AvrInterrupt
36-
| AvrNonBlockingInterrupt
37-
| RiscvInterruptM
38-
| RiscvInterruptS
39-
| CCmseNonSecureCall
40-
| Wasm
41-
| Unadjusted => false,
42-
RustIntrinsic | Rust | RustCall | RustCold => unreachable!(), // these ABIs are already skipped earlier
43-
}
44-
}
45-
4614
// Check if the body of this def_id can possibly leak a foreign unwind into Rust code.
4715
fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
4816
debug!("has_ffi_unwind_calls({local_def_id:?})");
@@ -103,7 +71,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
10371
_ => bug!("invalid callee of type {:?}", ty),
10472
};
10573

106-
if layout::fn_can_unwind(tcx, fn_def_id, sig.abi()) && abi_can_unwind(sig.abi()) {
74+
if layout::fn_can_unwind(tcx, fn_def_id, sig.abi()) {
10775
// We have detected a call that can possibly leak foreign unwind.
10876
//
10977
// Because the function body itself can unwind, we are not aborting this function call

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@
165165
//
166166
// Language features:
167167
// tidy-alphabetical-start
168+
#![cfg_attr(bootstrap, feature(c_unwind))]
168169
#![cfg_attr(not(test), feature(coroutine_trait))]
169170
#![cfg_attr(test, feature(panic_update_hook))]
170171
#![cfg_attr(test, feature(test))]
171172
#![feature(allocator_internals)]
172173
#![feature(allow_internal_unstable)]
173-
#![feature(c_unwind)]
174174
#![feature(cfg_sanitize)]
175175
#![feature(const_mut_refs)]
176176
#![feature(const_precise_live_drops)]

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@
199199
//
200200
// Language features:
201201
// tidy-alphabetical-start
202+
#![cfg_attr(bootstrap, feature(c_unwind))]
202203
#![feature(abi_unadjusted)]
203204
#![feature(adt_const_params)]
204205
#![feature(allow_internal_unsafe)]
205206
#![feature(allow_internal_unstable)]
206207
#![feature(asm_const)]
207208
#![feature(auto_traits)]
208-
#![feature(c_unwind)]
209209
#![feature(cfg_sanitize)]
210210
#![feature(cfg_target_has_atomic)]
211211
#![feature(cfg_target_has_atomic_equal_alignment)]

library/panic_abort/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#![feature(std_internals)]
1515
#![feature(staged_api)]
1616
#![feature(rustc_attrs)]
17-
#![feature(c_unwind)]
17+
#![cfg_attr(bootstrap, feature(c_unwind))]
1818
#![allow(internal_features)]
1919

2020
#[cfg(target_os = "android")]

library/panic_unwind/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#![feature(rustc_attrs)]
2525
#![panic_runtime]
2626
#![feature(panic_runtime)]
27-
#![feature(c_unwind)]
27+
#![cfg_attr(bootstrap, feature(c_unwind))]
2828
// `real_imp` is unused with Miri, so silence warnings.
2929
#![cfg_attr(miri, allow(dead_code))]
3030
#![allow(internal_features)]

library/proc_macro/src/bridge/buffer.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ impl Write for Buffer {
119119
}
120120

121121
impl Drop for Buffer {
122-
#[inline]
122+
// HACK(nbdd0121): Hack to prevent LLVM < 17.0.4 from misoptimising,
123+
// change to `#[inline]` if fixed.
124+
#[inline(never)]
123125
fn drop(&mut self) {
124126
let b = self.take();
125127
(b.drop)(b);

library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@
273273
//
274274
// Language features:
275275
// tidy-alphabetical-start
276+
#![cfg_attr(bootstrap, feature(c_unwind))]
276277
#![feature(alloc_error_handler)]
277278
#![feature(allocator_internals)]
278279
#![feature(allow_internal_unsafe)]
279280
#![feature(allow_internal_unstable)]
280281
#![feature(asm_experimental_arch)]
281-
#![feature(c_unwind)]
282282
#![feature(cfg_sanitizer_cfi)]
283283
#![feature(cfg_target_thread_local)]
284284
#![feature(cfi_encoding)]

library/unwind/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![unstable(feature = "panic_unwind", issue = "32837")]
33
#![feature(link_cfg)]
44
#![feature(staged_api)]
5-
#![feature(c_unwind)]
5+
#![cfg_attr(bootstrap, feature(c_unwind))]
66
#![feature(strict_provenance)]
77
#![cfg_attr(target_arch = "wasm64", feature(simd_wasm64))]
88
#![cfg_attr(not(target_env = "msvc"), feature(libc))]

src/doc/unstable-book/src/language-features/c-unwind.md

-26
This file was deleted.

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind1.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(c_unwind)]
2-
31
#[no_mangle]
42
extern "C-unwind" fn unwind() {
53
panic!();

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
55
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
66
//@[definition,both]error-in-other-file: aborted execution
7-
#![feature(rustc_attrs, c_unwind)]
7+
#![feature(rustc_attrs)]
88

99
#[cfg_attr(any(definition, both), rustc_nounwind)]
1010
#[no_mangle]

src/tools/miri/tests/fail/panic/bad_unwind.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(c_unwind)]
2-
31
//! Unwinding when the caller ABI is "C" (without "-unwind") is UB.
42
// The opposite version (callee does not allow unwinding) is impossible to
53
// even write: MIR validation catches functions that have `UnwindContinue` but

src/tools/miri/tests/fail/terminate-terminator.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// Enable MIR inlining to ensure that `TerminatorKind::UnwindTerminate` is generated
88
// instead of just `UnwindAction::Terminate`.
99

10-
#![feature(c_unwind)]
11-
1210
struct Foo;
1311

1412
impl Drop for Foo {

src/tools/miri/tests/fail/unwind-action-terminate.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//@normalize-stderr-test: "\| +\^+" -> "| ^"
44
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
55
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
6-
#![feature(c_unwind)]
7-
86
extern "C" fn panic_abort() {
97
panic!()
108
}

src/tools/miri/tests/panic/function_calls/exported_symbol_good_unwind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// found in this form" errors works without `-C prefer-dynamic` (`panic!` calls foreign function
33
// `__rust_start_panic`).
44
// no-prefer-dynamic
5-
#![feature(c_unwind, unboxed_closures)]
5+
#![feature(unboxed_closures)]
66

77
use std::panic;
88

tests/assembly/asm/aarch64-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ assembly-output: emit-asm
2-
//@ compile-flags: -O
2+
//@ compile-flags: -O -C panic=abort
33
//@ compile-flags: --target aarch64-unknown-linux-gnu
44
//@ needs-llvm-components: aarch64
55

tests/assembly/asm/arm-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ assembly-output: emit-asm
2-
//@ compile-flags: -O
2+
//@ compile-flags: -O -C panic=abort
33
//@ compile-flags: --target armv7-unknown-linux-gnueabihf
44
//@ compile-flags: -C target-feature=+neon
55
//@ needs-llvm-components: arm

tests/assembly/asm/x86-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ revisions: x86_64 i686
22
//@ assembly-output: emit-asm
3-
//@ compile-flags: -O
3+
//@ compile-flags: -O -C panic=abort
44
//@[x86_64] compile-flags: --target x86_64-unknown-linux-gnu
55
//@[x86_64] needs-llvm-components: x86
66
//@[i686] compile-flags: --target i686-unknown-linux-gnu

tests/assembly/simd-bitmask.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//@ [aarch64] needs-llvm-components: aarch64
1212
//@ [aarch64] min-llvm-version: 18.0
1313
//@ assembly-output: emit-asm
14-
//@ compile-flags: --crate-type=lib -O
14+
//@ compile-flags: --crate-type=lib -O -C panic=abort
1515

1616
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1717
#![no_core]

tests/assembly/simd-intrinsic-gather.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//@ [x86-avx512] needs-llvm-components: x86
55
//@ [x86-avx512] min-llvm-version: 18.0
66
//@ assembly-output: emit-asm
7-
//@ compile-flags: --crate-type=lib -O
7+
//@ compile-flags: --crate-type=lib -O -C panic=abort
88

99
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1010
#![no_core]

tests/assembly/simd-intrinsic-mask-load.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//@ [x86-avx512] compile-flags: -C target-feature=+avx512f,+avx512vl,+avx512bw,+avx512dq
77
//@ [x86-avx512] needs-llvm-components: x86
88
//@ assembly-output: emit-asm
9-
//@ compile-flags: --crate-type=lib -O
9+
//@ compile-flags: --crate-type=lib -O -C panic=abort
1010

1111
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1212
#![no_core]

tests/assembly/simd-intrinsic-mask-reduce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//@ [aarch64] needs-llvm-components: aarch64
99
//@ [aarch64] min-llvm-version: 18.0
1010
//@ assembly-output: emit-asm
11-
//@ compile-flags: --crate-type=lib -O
11+
//@ compile-flags: --crate-type=lib -O -C panic=abort
1212

1313
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1414
#![no_core]

tests/assembly/simd-intrinsic-mask-store.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//@ [x86-avx512] compile-flags: -C target-feature=+avx512f,+avx512vl,+avx512bw,+avx512dq
77
//@ [x86-avx512] needs-llvm-components: x86
88
//@ assembly-output: emit-asm
9-
//@ compile-flags: --crate-type=lib -O
9+
//@ compile-flags: --crate-type=lib -O -C panic=abort
1010

1111
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1212
#![no_core]

tests/assembly/simd-intrinsic-scatter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//@ [x86-avx512] needs-llvm-components: x86
55
//@ [x86-avx512] min-llvm-version: 18.0
66
//@ assembly-output: emit-asm
7-
//@ compile-flags: --crate-type=lib -O
7+
//@ compile-flags: --crate-type=lib -O -C panic=abort
88

99
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1010
#![no_core]

tests/assembly/simd-intrinsic-select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//@ [aarch64] needs-llvm-components: aarch64
1010
//@ [aarch64] min-llvm-version: 18.0
1111
//@ assembly-output: emit-asm
12-
//@ compile-flags: --crate-type=lib -O
12+
//@ compile-flags: --crate-type=lib -O -C panic=abort
1313

1414
#![feature(no_core, lang_items, repr_simd, intrinsics)]
1515
#![no_core]

tests/assembly/wasm_exceptions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
#![crate_type = "lib"]
88
#![feature(core_intrinsics)]
9-
#![feature(rustc_attrs)]
109

11-
extern "C" {
10+
extern "C-unwind" {
1211
fn may_panic();
12+
}
1313

14-
#[rustc_nounwind]
14+
extern "C" {
1515
fn log_number(number: usize);
1616
}
1717

tests/codegen/align-byval-alignment-mismatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// ignore-tidy-linelength
22
//@ revisions:i686-linux x86_64-linux
33

4-
//@[i686-linux] compile-flags: --target i686-unknown-linux-gnu
4+
//@[i686-linux] compile-flags: --target i686-unknown-linux-gnu -C panic=abort
55
//@[i686-linux] needs-llvm-components: x86
6-
//@[x86_64-linux] compile-flags: --target x86_64-unknown-linux-gnu
6+
//@[x86_64-linux] compile-flags: --target x86_64-unknown-linux-gnu -C panic=abort
77
//@[x86_64-linux] needs-llvm-components: x86
88

99
// Tests that we correctly copy arguments into allocas when the alignment of the byval argument

tests/codegen/avr/avr-func-addrspace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
1+
//@ compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib -C panic=abort
22
//@ needs-llvm-components: avr
33

44
// This test validates that function pointers can be stored in global variables

tests/codegen/catch-unwind.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//@ ignore-loongarch64 FIXME
1515

1616
#![crate_type = "lib"]
17-
#![feature(c_unwind)]
1817

1918
extern "C" {
2019
fn bar();

0 commit comments

Comments
 (0)