From de66d3aa2b4506795e9dec9503168e4d13c4f7b3 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 14 Sep 2024 01:34:05 -0400 Subject: [PATCH 1/3] add core::panic::abort_unwind --- library/core/src/panic.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 6c5236ed99ce8..3919bbd79586f 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -140,6 +140,36 @@ pub macro unreachable_2021 { ), } +/// Invokes a closure, aborting if the closure unwinds. +/// +/// When compiled with aborting panics, this function is effectively a no-op. +/// With unwinding panics, an unwind results in another call into the panic +/// hook followed by a process abort. +/// +/// # Notes +/// +/// Instead of using this function, code should attempt to support unwinding. +/// Implementing [`Drop`] allows you to restore invariants uniformly in both +/// return and unwind paths. +/// +/// If an unwind can lead to logical issues but not soundness issues, you +/// should allow the unwind. Opting out of [`UnwindSafe`] indicates to your +/// consumers that they need to consider correctness in the face of unwinds. +/// +/// If an unwind would be unsound, then this function should be used in order +/// to prevent unwinds. However, note that `extern "C" fn` will automatically +/// convert unwinds to aborts, so using this function isn't necessary for FFI. +#[unstable(feature = "abort_unwind", issue = "130338")] +pub fn abort_unwind R, R>(f: F) -> R { + // This attribute adds the "unwinding out of nounwind function" guard. + #[rustc_nounwind] + fn abort_unwind_inner R, R>(f: F) -> R { + f() + } + + abort_unwind_inner(f) +} + /// An internal trait used by std to pass data from std to `panic_unwind` and /// other panic runtimes. Not intended to be stabilized any time soon, do not /// use. From 7e7ccb25b48dac9c341c972fc8dfce3cb300989f Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 14 Sep 2024 01:41:00 -0400 Subject: [PATCH 2/3] add std::panic::abort_unwind --- library/std/src/panic.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 6f0952c41ede5..541cf42ab47e6 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -283,6 +283,9 @@ where { } +#[unstable(feature = "abort_unwind", issue = "130338")] +pub use core::panic::abort_unwind; + /// Invokes a closure, capturing the cause of an unwinding panic if one occurs. /// /// This function will return `Ok` with the closure's result if the closure From 42a44a04ee1b2a99ea78e942937b4ce3d8a3067e Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sun, 15 Sep 2024 14:27:24 -0400 Subject: [PATCH 3/3] simplify abort_unwind Co-authored-by: David Tolnay --- library/core/src/panic.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 3919bbd79586f..c95a000561c35 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -160,14 +160,9 @@ pub macro unreachable_2021 { /// to prevent unwinds. However, note that `extern "C" fn` will automatically /// convert unwinds to aborts, so using this function isn't necessary for FFI. #[unstable(feature = "abort_unwind", issue = "130338")] +#[rustc_nounwind] pub fn abort_unwind R, R>(f: F) -> R { - // This attribute adds the "unwinding out of nounwind function" guard. - #[rustc_nounwind] - fn abort_unwind_inner R, R>(f: F) -> R { - f() - } - - abort_unwind_inner(f) + f() } /// An internal trait used by std to pass data from std to `panic_unwind` and