From a331c22665d000577a812f07b513ccf7a365bed8 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sun, 7 May 2023 00:10:38 +0100 Subject: [PATCH] Use libc abort instead intrinsic abort when available --- src/panic.rs | 4 ++-- src/panic_handler.rs | 4 ++-- src/panic_handler_dummy.rs | 2 +- src/util.rs | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/panic.rs b/src/panic.rs index 8d5bd9d..d61f5e9 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -18,7 +18,7 @@ impl Drop for DropGuard { { drop_panic(); } - core::intrinsics::abort(); + crate::util::abort(); } } @@ -57,7 +57,7 @@ pub fn catch_unwind R>(f: F) -> Result> { foreign_exception(); } - core::intrinsics::abort(); + crate::util::abort(); } Some(e) => { #[cfg(feature = "panic-handler")] diff --git a/src/panic_handler.rs b/src/panic_handler.rs index 508ad8c..beca6ff 100644 --- a/src/panic_handler.rs +++ b/src/panic_handler.rs @@ -80,7 +80,7 @@ fn do_panic(msg: Box) -> ! { if PANIC_COUNT.get() >= 1 { stack_trace(); eprintln!("thread panicked while processing panic. aborting."); - core::intrinsics::abort(); + crate::util::abort(); } PANIC_COUNT.set(1); if check_env() { @@ -88,7 +88,7 @@ fn do_panic(msg: Box) -> ! { } let code = crate::panic::begin_panic(Box::new(msg)); eprintln!("failed to initiate panic, error {}", code.0); - core::intrinsics::abort(); + crate::util::abort(); } #[panic_handler] diff --git a/src/panic_handler_dummy.rs b/src/panic_handler_dummy.rs index 74c8a5d..6770705 100644 --- a/src/panic_handler_dummy.rs +++ b/src/panic_handler_dummy.rs @@ -2,5 +2,5 @@ use core::panic::PanicInfo; #[panic_handler] fn panic(_info: &PanicInfo<'_>) -> ! { - core::intrinsics::abort(); + crate::util::abort(); } diff --git a/src/util.rs b/src/util.rs index 1d738ce..e28a8a0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -23,3 +23,19 @@ pub use libc::c_int; #[cfg(not(feature = "libc"))] #[allow(non_camel_case_types)] pub type c_int = i32; + +#[cfg(all( + any(feature = "panicking", feature = "panic-handler-dummy"), + feature = "libc" +))] +pub fn abort() -> ! { + unsafe { libc::abort() }; +} + +#[cfg(all( + any(feature = "panicking", feature = "panic-handler-dummy"), + not(feature = "libc") +))] +pub fn abort() -> ! { + core::intrinsics::abort(); +}