montest/
main.rs

1#![feature(linkage)]
2#![feature(native_link_modifiers_as_needed)]
3
4use std::sync::atomic::{AtomicBool, Ordering};
5
6extern crate montest_lib;
7extern crate secgate;
8
9secgate::secgate_prelude!();
10
11#[link(name = "montest_lib", kind = "dylib", modifiers = "-as-needed")]
12extern "C" {}
13
14extern crate tracing;
15extern crate tracing_subscriber;
16extern crate twizzler_runtime;
17
18fn main() {
19    setup_logging();
20    montest_lib::test_global_call_count().unwrap();
21    if std::env::args().find(|p| p == "-e").is_some() {
22        unsafe {
23            let nul = core::ptr::null_mut::<u32>();
24            let v = nul.read_volatile();
25            std::hint::black_box(v);
26        }
27    } else if std::env::args().find(|p| p == "-p").is_some() {
28        panic!("montest test panic");
29    }
30}
31use tracing::Level;
32fn setup_logging() {
33    let _ = tracing::subscriber::set_global_default(
34        tracing_subscriber::fmt()
35            .with_max_level(Level::DEBUG)
36            .finish(),
37    );
38}
39
40#[cfg(test)]
41mod tests {
42    use std::sync::atomic::Ordering;
43
44    use monitor_api::CompartmentHandle;
45
46    use crate::montest_lib;
47    extern crate secgate;
48
49    use super::setup_logging;
50    use crate::WAS_CTOR_RUN;
51
52    #[test]
53    fn test_tl_count() {
54        setup_logging();
55        assert_eq!(
56            secgate::SecGateReturn::Success(1),
57            montest_lib::test_thread_local_call_count()
58        );
59    }
60
61    #[test]
62    fn test_gl_count() {
63        setup_logging();
64        assert_eq!(
65            secgate::SecGateReturn::Success(1),
66            montest_lib::test_global_call_count()
67        );
68    }
69
70    #[test]
71    fn test_uncaught_internal_panic() {
72        setup_logging();
73        assert_eq!(
74            secgate::SecGateReturn::CalleePanic,
75            montest_lib::test_internal_panic(false)
76        );
77    }
78
79    #[test]
80    fn test_internal_panic() {
81        setup_logging();
82        assert_eq!(
83            secgate::SecGateReturn::Success(1),
84            montest_lib::test_internal_panic(true)
85        );
86    }
87
88    #[test]
89    fn test_lib_ctors() {
90        setup_logging();
91        assert_eq!(
92            secgate::SecGateReturn::Success(true),
93            montest_lib::test_was_ctor_run()
94        );
95    }
96
97    #[test]
98    fn test_bin_ctors() {
99        setup_logging();
100        assert_eq!(true, WAS_CTOR_RUN.load(Ordering::SeqCst))
101    }
102
103    #[test]
104    fn test_dynamic_secgate() {
105        let current = CompartmentHandle::current();
106        let name = format!("{}::libmontest_lib.so", current.info().name);
107        let comp = CompartmentHandle::lookup(&name)
108            .expect(&format!("failed to open compartment: {}", &name));
109        let gate = unsafe { comp.dynamic_gate::<(u32,), u32>("dynamic_test") }.unwrap();
110        let ret = unsafe { secgate::dynamic_gate_call(gate, (3,)).ok().unwrap() };
111        assert_eq!(ret, 45);
112    }
113}
114
115static WAS_CTOR_RUN: AtomicBool = AtomicBool::new(false);
116
117#[used]
118#[doc(hidden)]
119#[allow(non_upper_case_globals)]
120#[link_section = ".init_array"]
121static ___cons_test___ctor: unsafe extern "C" fn() = {
122    #[allow(non_snake_case)]
123    #[link_section = ".text.startup"]
124    unsafe extern "C" fn ___cons_test___ctor() {
125        cons_test()
126    }
127    ___cons_test___ctor
128};
129unsafe fn cons_test() {
130    WAS_CTOR_RUN.store(true, Ordering::SeqCst);
131}