Skip to content

Commit

Permalink
Auto merge of rust-lang#116088 - nbdd0121:unwind, r=<try>
Browse files Browse the repository at this point in the history
Stabilise `c_unwind`

Fix rust-lang#74990
Fix rust-lang#115285

Marking as draft PR for now due to `compiler_builtins` issues

r? `@Amanieu`
  • Loading branch information
bors committed Dec 10, 2023
2 parents 42dfac5 + cff15e8 commit 2b9b2b6
Show file tree
Hide file tree
Showing 53 changed files with 40 additions and 144 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ declare_features! (
(accepted, braced_empty_structs, "1.8.0", Some(29720), None),
/// Allows `c"foo"` literals.
(accepted, c_str_literals, "CURRENT_RUSTC_VERSION", Some(105723), None),
/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries and treat `extern "C" fn` as nounwind.
(accepted, c_unwind, "CURRENT_RUSTC_VERSION", Some(74990), None),
/// Allows `#[cfg_attr(predicate, multiple, attributes, here)]`.
(accepted, cfg_attr_multi, "1.33.0", Some(54881), None),
/// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ declare_features! (
(unstable, async_fn_track_caller, "1.73.0", Some(110011), None),
/// Allows builtin # foo() syntax
(unstable, builtin_syntax, "1.71.0", Some(110680), None),
/// Treat `extern "C"` function as nounwind.
(unstable, c_unwind, "1.52.0", Some(74990), None),
/// Allows using C-variadics.
(unstable, c_variadic, "1.34.0", Some(44930), None),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
Expand Down
36 changes: 1 addition & 35 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,37 +1197,6 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) ->
// ABIs have such an option. Otherwise the only other thing here is Rust
// itself, and those ABIs are determined by the panic strategy configured
// for this compilation.
//
// Unfortunately at this time there's also another caveat. Rust [RFC
// 2945][rfc] has been accepted and is in the process of being implemented
// and stabilized. In this interim state we need to deal with historical
// rustc behavior as well as plan for future rustc behavior.
//
// Historically functions declared with `extern "C"` were marked at the
// codegen layer as `nounwind`. This happened regardless of `panic=unwind`
// or not. This is UB for functions in `panic=unwind` mode that then
// actually panic and unwind. Note that this behavior is true for both
// externally declared functions as well as Rust-defined function.
//
// To fix this UB rustc would like to change in the future to catch unwinds
// from function calls that may unwind within a Rust-defined `extern "C"`
// function and forcibly abort the process, thereby respecting the
// `nounwind` attribute emitted for `extern "C"`. This behavior change isn't
// ready to roll out, so determining whether or not the `C` family of ABIs
// unwinds is conditional not only on their definition but also whether the
// `#![feature(c_unwind)]` feature gate is active.
//
// Note that this means that unlike historical compilers rustc now, by
// default, unconditionally thinks that the `C` ABI may unwind. This will
// prevent some optimization opportunities, however, so we try to scope this
// change and only assume that `C` unwinds with `panic=unwind` (as opposed
// to `panic=abort`).
//
// Eventually the check against `c_unwind` here will ideally get removed and
// this'll be a little cleaner as it'll be a straightforward check of the
// ABI.
//
// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md
use SpecAbi::*;
match abi {
C { unwind }
Expand All @@ -1239,10 +1208,7 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) ->
| Thiscall { unwind }
| Aapcs { unwind }
| Win64 { unwind }
| SysV64 { unwind } => {
unwind
|| (!tcx.features().c_unwind && tcx.sess.panic_strategy() == PanicStrategy::Unwind)
}
| SysV64 { unwind } => unwind,
PtxKernel
| Msp430Interrupt
| X86Interrupt
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(c_unwind))]
#![cfg_attr(not(test), feature(coroutine_trait))]
#![cfg_attr(test, feature(panic_update_hook))]
#![cfg_attr(test, feature(test))]
#![feature(allocator_internals)]
#![feature(allow_internal_unstable)]
#![feature(associated_type_bounds)]
#![feature(c_unwind)]
#![feature(cfg_sanitize)]
#![feature(const_mut_refs)]
#![feature(const_precise_live_drops)]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(c_unwind))]
#![feature(abi_unadjusted)]
#![feature(adt_const_params)]
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)]
#![feature(asm_const)]
#![feature(associated_type_bounds)]
#![feature(auto_traits)]
#![feature(c_unwind)]
#![feature(cfg_sanitize)]
#![feature(cfg_target_has_atomic)]
#![feature(cfg_target_has_atomic_equal_alignment)]
Expand Down
2 changes: 1 addition & 1 deletion library/panic_abort/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#![feature(std_internals)]
#![feature(staged_api)]
#![feature(rustc_attrs)]
#![feature(c_unwind)]
#![cfg_attr(bootstrap, feature(c_unwind))]
#![allow(internal_features)]

#[cfg(target_os = "android")]
Expand Down
2 changes: 1 addition & 1 deletion library/panic_unwind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#![feature(rustc_attrs)]
#![panic_runtime]
#![feature(panic_runtime)]
#![feature(c_unwind)]
#![cfg_attr(bootstrap, feature(c_unwind))]
// `real_imp` is unused with Miri, so silence warnings.
#![cfg_attr(miri, allow(dead_code))]
#![allow(internal_features)]
Expand Down
10 changes: 6 additions & 4 deletions library/proc_macro/src/bridge/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub struct Buffer {
data: *mut u8,
len: usize,
capacity: usize,
reserve: extern "C" fn(Buffer, usize) -> Buffer,
drop: extern "C" fn(Buffer),
// HACK(nbdd0121): These should be `extern "C"` but LLVM < 17.0.4 will misoptimise if we do so.
// Remove this when we drop support for them or when we require patches to be present.
reserve: extern "C-unwind" fn(Buffer, usize) -> Buffer,
drop: extern "C-unwind" fn(Buffer),
}

unsafe impl Sync for Buffer {}
Expand Down Expand Up @@ -141,13 +143,13 @@ impl From<Vec<u8>> for Buffer {
}
}

extern "C" fn reserve(b: Buffer, additional: usize) -> Buffer {
extern "C-unwind" fn reserve(b: Buffer, additional: usize) -> Buffer {
let mut v = to_vec(b);
v.reserve(additional);
Buffer::from(v)
}

extern "C" fn drop(b: Buffer) {
extern "C-unwind" fn drop(b: Buffer) {
mem::drop(to_vec(b));
}

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(c_unwind))]
#![feature(alloc_error_handler)]
#![feature(allocator_internals)]
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)]
#![feature(c_unwind)]
#![feature(cfg_target_thread_local)]
#![feature(cfi_encoding)]
#![feature(concat_idents)]
Expand Down
2 changes: 1 addition & 1 deletion library/unwind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![unstable(feature = "panic_unwind", issue = "32837")]
#![feature(link_cfg)]
#![feature(staged_api)]
#![feature(c_unwind)]
#![cfg_attr(bootstrap, feature(c_unwind))]
#![feature(cfg_target_abi)]
#![feature(strict_provenance)]
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
Expand Down
26 changes: 0 additions & 26 deletions src/doc/unstable-book/src/language-features/c-unwind.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

//! Unwinding past the top frame of a stack is Undefined Behavior.

#![feature(c_unwind)]

use std::{mem, ptr};

extern "C-unwind" fn thread_start(_null: *mut libc::c_void) -> *mut libc::c_void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@compile-flags: -Zmiri-disable-abi-check
#![feature(c_unwind)]

#[no_mangle]
extern "C-unwind" fn unwind() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
//@[definition,both]error-in-other-file: aborted execution
#![feature(rustc_attrs, c_unwind)]
#![feature(rustc_attrs)]

#[cfg_attr(any(definition, both), rustc_nounwind)]
#[no_mangle]
Expand Down
1 change: 0 additions & 1 deletion src/tools/miri/tests/fail/panic/bad_miri_start_panic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@compile-flags: -Zmiri-disable-abi-check
// This feature is required to trigger the error using the "C" ABI.
#![feature(c_unwind)]

extern "C" {
fn miri_start_panic(payload: *mut u8) -> !;
Expand Down
2 changes: 0 additions & 2 deletions src/tools/miri/tests/fail/panic/bad_unwind.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(c_unwind)]

//! Unwinding when the caller ABI is "C" (without "-unwind") is UB.

extern "C-unwind" fn unwind() {
Expand Down
2 changes: 0 additions & 2 deletions src/tools/miri/tests/fail/terminate-terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Enable MIR inlining to ensure that `TerminatorKind::UnwindTerminate` is generated
// instead of just `UnwindAction::Terminate`.

#![feature(c_unwind)]

struct Foo;

impl Drop for Foo {
Expand Down
2 changes: 0 additions & 2 deletions src/tools/miri/tests/fail/unwind-action-terminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//@normalize-stderr-test: "\| +\^+" -> "| ^"
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
#![feature(c_unwind)]

extern "C" fn panic_abort() {
panic!()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// found in this form" errors works without `-C prefer-dynamic` (`panic!` calls foreign function
// `__rust_start_panic`).
// no-prefer-dynamic
#![feature(c_unwind, unboxed_closures)]
#![feature(unboxed_closures)]

use std::panic;

Expand Down
2 changes: 1 addition & 1 deletion tests/assembly/asm/aarch64-modifiers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// assembly-output: emit-asm
// compile-flags: -O
// compile-flags: -O -C panic=abort
// compile-flags: --target aarch64-unknown-linux-gnu
// needs-llvm-components: aarch64

Expand Down
2 changes: 1 addition & 1 deletion tests/assembly/asm/arm-modifiers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// assembly-output: emit-asm
// compile-flags: -O
// compile-flags: -O -C panic=abort
// compile-flags: --target armv7-unknown-linux-gnueabihf
// compile-flags: -C target-feature=+neon
// needs-llvm-components: arm
Expand Down
2 changes: 1 addition & 1 deletion tests/assembly/asm/x86-modifiers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// revisions: x86_64 i686
// assembly-output: emit-asm
// compile-flags: -O
// compile-flags: -O -C panic=abort
//[x86_64] compile-flags: --target x86_64-unknown-linux-gnu
//[x86_64] needs-llvm-components: x86
//[i686] compile-flags: --target i686-unknown-linux-gnu
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/avr/avr-func-addrspace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
// compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib -C panic=abort
// needs-llvm-components: avr

// This test validates that function pointers can be stored in global variables
Expand Down
1 change: 0 additions & 1 deletion tests/codegen/catch-unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// ignore-loongarch64 FIXME

#![crate_type = "lib"]
#![feature(c_unwind)]

extern "C" {
fn bar();
Expand Down
1 change: 0 additions & 1 deletion tests/codegen/cffi/c-variadic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#![crate_type = "lib"]
#![feature(c_variadic)]
#![feature(c_unwind)]
#![no_std]
use core::ffi::VaList;

Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/debuginfo-inline-callsite-location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#![crate_type = "lib"]

#[no_mangle]
extern "C" fn add_numbers(x: &Option<i32>, y: &Option<i32>) -> i32 {
extern "C-unwind" fn add_numbers(x: &Option<i32>, y: &Option<i32>) -> i32 {
let x1 = x.unwrap();
let y1 = y.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/unwind-abis/aapcs-unwind-abi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// needs-llvm-components: arm
// compile-flags: --target=armv7-unknown-linux-gnueabihf --crate-type=rlib -Cno-prepopulate-passes
#![no_core]
#![feature(no_core, lang_items, c_unwind)]
#![feature(no_core, lang_items)]
#[lang="sized"]
trait Sized { }

Expand Down
1 change: 0 additions & 1 deletion tests/codegen/unwind-abis/c-unwind-abi-panic-abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// when the code is compiled with `panic=abort`.

#![crate_type = "lib"]
#![feature(c_unwind)]

// CHECK: @rust_item_that_can_unwind() unnamed_addr [[ATTR0:#[0-9]+]]
#[no_mangle]
Expand Down
1 change: 0 additions & 1 deletion tests/codegen/unwind-abis/c-unwind-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// to prevent LLVM from inferring the attribute.

#![crate_type = "lib"]
#![feature(c_unwind)]

// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
#[no_mangle]
Expand Down
1 change: 0 additions & 1 deletion tests/codegen/unwind-abis/cdecl-unwind-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// disable optimizations above to prevent LLVM from inferring the attribute.

#![crate_type = "lib"]
#![feature(c_unwind)]

// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/unwind-abis/fastcall-unwind-abi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// needs-llvm-components: x86
// compile-flags: --target=i686-pc-windows-msvc --crate-type=rlib -Cno-prepopulate-passes
#![no_core]
#![feature(no_core, lang_items, c_unwind)]
#![feature(no_core, lang_items)]
#[lang="sized"]
trait Sized { }

Expand Down
18 changes: 0 additions & 18 deletions tests/codegen/unwind-abis/nounwind-on-stable-panic-unwind.rs

This file was deleted.

1 change: 0 additions & 1 deletion tests/codegen/unwind-abis/nounwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// ignore-wasm32-bare compiled with panic=abort by default

#![crate_type = "lib"]
#![feature(c_unwind)]

// We disable optimizations to prevent LLVM from inferring the attribute.

Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/unwind-abis/stdcall-unwind-abi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// needs-llvm-components: x86
// compile-flags: --target=i686-pc-windows-msvc --crate-type=rlib -Cno-prepopulate-passes
#![no_core]
#![feature(no_core, lang_items, c_unwind)]
#![feature(no_core, lang_items)]
#[lang="sized"]
trait Sized { }

Expand Down
1 change: 0 additions & 1 deletion tests/codegen/unwind-abis/system-unwind-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// optimizations above to prevent LLVM from inferring the attribute.

#![crate_type = "lib"]
#![feature(c_unwind)]

// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
#[no_mangle]
Expand Down
Loading

0 comments on commit 2b9b2b6

Please sign in to comment.