From 7961ebbd45904481aeb55e38803334b23bdd6b6f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 31 Aug 2022 20:30:56 +0200 Subject: [PATCH 1/2] avoid abort_on_panic dependency --- libffi-rs/Cargo.toml | 1 - libffi-rs/src/high/mod.rs | 13 +++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libffi-rs/Cargo.toml b/libffi-rs/Cargo.toml index d65048aa..0f577638 100644 --- a/libffi-rs/Cargo.toml +++ b/libffi-rs/Cargo.toml @@ -12,7 +12,6 @@ edition = "2018" [dependencies] libffi-sys = { path = "../libffi-sys-rs", version = "^2.0"} -abort_on_panic = "2.0.0" libc = "0.2.65" [features] diff --git a/libffi-rs/src/high/mod.rs b/libffi-rs/src/high/mod.rs index c7730020..6427b4ea 100644 --- a/libffi-rs/src/high/mod.rs +++ b/libffi-rs/src/high/mod.rs @@ -68,8 +68,6 @@ //! //! Invoking the closure a second time will panic. -use abort_on_panic::abort_on_panic; - pub use crate::middle::{ffi_abi_FFI_DEFAULT_ABI, FfiAbi}; pub mod types; @@ -78,6 +76,17 @@ pub use types::{CType, Type}; pub mod call; pub use call::*; +macro_rules! abort_on_panic { + ($msg:literal, $body:expr) => { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { $body })) + .unwrap_or_else(|err| { + std::mem::forget(err); // defends against the issue that dropping `err` might panic + eprintln!($msg); + std::process::abort() + }) + } +} + macro_rules! define_closure_mod { ( $module:ident $cif:ident $fnptr:ident From 57afc1cfcc23c2dd2fc939e1675d5c32179b8792 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 1 Sep 2022 09:37:58 +0200 Subject: [PATCH 2/2] use a drop guard 'bomb' instead --- libffi-rs/src/high/mod.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/libffi-rs/src/high/mod.rs b/libffi-rs/src/high/mod.rs index 6427b4ea..3209b31b 100644 --- a/libffi-rs/src/high/mod.rs +++ b/libffi-rs/src/high/mod.rs @@ -77,14 +77,24 @@ pub mod call; pub use call::*; macro_rules! abort_on_panic { - ($msg:literal, $body:expr) => { - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { $body })) - .unwrap_or_else(|err| { - std::mem::forget(err); // defends against the issue that dropping `err` might panic - eprintln!($msg); - std::process::abort() - }) - } + ($msg:literal, $body:expr) => {{ + // Aborts when dropped (which will only happen due to an unwinding panic). + struct Bomb; + impl Drop for Bomb { + fn drop(&mut self) { + // We do our best to ignore errors that occur during printing. + // If this panics anyway, that'll still just be a double-panic which leads to abort. + let _ = writeln!(std::io::stderr(), $msg); + std::process::abort(); + } + } + + let b = Bomb; + // If this panics, `b` will be dropped, triggering the bomb. + $body; + // Defuse the bomb. + std::mem::forget(b); + }}; } macro_rules! define_closure_mod {