1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Paranoia is a simple hack to see if some code was optimized
//! away (by never being called) or not. This is not guaranteed
//! to work, and serves only as a hint. But there are no false
//! negatives - if `marker_exists` returns false, you can be
//! certain that the call to marker was fully optimized away.
//!
//! # Examples
//!
//! `Cargo.toml`
//!
//! ```no_build
//! [dependencies]
//! paranoia_caller = "*"
//! paranoia = "*"
//! ```
//!
//! verify that it was optimized out:
//! ```
//! if false {
//!     paranoia_caller::mark();
//! }
//! assert!(!paranoia::marker_exists());
//! ```
//!
//! see if it was not able to be optimized out:
//! ```
//! if true {
//!     paranoia_caller::mark();
//! }
//! assert!(paranoia::marker_exists());
//! ```
use libc::{dlsym, RTLD_NEXT};
use std::ffi::CString;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Once,
};

/// Check to see if the marker has been optimized away or not.
pub fn marker_exists() -> bool {
    static DLSYM: Once = Once::new();
    static EXISTS: AtomicBool = AtomicBool::new(true);

    DLSYM.call_once(|| {
        let symbol = CString::new(b"___paranoia_present".to_vec()).unwrap();

        #[allow(unsafe_code)]
        let ptr = unsafe { dlsym(RTLD_NEXT, symbol.as_ptr()) };

        EXISTS.store(!ptr.is_null(), Ordering::SeqCst)
    });

    EXISTS.load(Ordering::Relaxed)
}