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