Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store trigger function as *mut () rather than *mut fn() #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ pub type Trigger = fn(crate_name: &'static str, file_name: &'static str, line_nu
///
/// [`FAULT_INJECT_COUNTER`]: FAULT_INJECT_COUNTER
pub fn set_trigger_function(f: Trigger) {
TRIGGER_FN.store(
f as usize as *mut Trigger,
core::sync::atomic::Ordering::Release,
);
TRIGGER_FN.store(f as *mut (), core::sync::atomic::Ordering::Release);
}

#[doc(hidden)]
pub static TRIGGER_FN: core::sync::atomic::AtomicPtr<Trigger> =
core::sync::atomic::AtomicPtr::new(0 as _);
pub static TRIGGER_FN: core::sync::atomic::AtomicPtr<()> =
core::sync::atomic::AtomicPtr::new(core::ptr::null_mut());

/// Similar to the `try!` macro or `?` operator,
/// but externally controllable to inject faults
Expand Down Expand Up @@ -137,8 +134,9 @@ macro_rules! maybe {
{
let trigger_fn = fault_injection::TRIGGER_FN.load(core::sync::atomic::Ordering::Acquire);
if !trigger_fn.is_null() {
// SAFETY: `trigger_fn` is non-null and was set to a valid function pointer with `set_trigger_function`.
unsafe {
let f: &fault_injection::Trigger = std::mem::transmute(&trigger_fn);
let f: fault_injection::Trigger = core::mem::transmute(trigger_fn);
(f)(CRATE_NAME, file!(), line!());
}
}
Expand Down