montest_srv/
lib.rs

1#![feature(thread_local)]
2#![feature(linkage)]
3
4use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
5
6use twizzler_rt_abi::Result;
7
8#[secgate::entry(lib = "montest-lib")]
9pub fn test_thread_local_call_count() -> Result<usize> {
10    #[thread_local]
11    static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
12    Ok(CALL_COUNT.fetch_add(1, Ordering::SeqCst) + 1)
13}
14
15#[secgate::entry(lib = "montest-lib")]
16pub fn test_global_call_count() -> Result<usize> {
17    static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
18    Ok(CALL_COUNT.fetch_add(1, Ordering::SeqCst) + 1)
19}
20
21#[secgate::entry(lib = "montest-lib")]
22pub fn test_internal_panic(catch_it: bool) -> Result<usize> {
23    if catch_it {
24        let x = std::panic::catch_unwind(|| {
25            panic!("test_panic (to be caught)");
26        });
27        return Ok(if x.is_err() { 1 } else { 0 });
28    }
29    panic!("test_panic (not caught)");
30}
31
32#[secgate::entry(lib = "montest-lib")]
33pub fn test_was_ctor_run() -> Result<bool> {
34    Ok(WAS_CTOR_RUN.load(Ordering::SeqCst))
35}
36
37#[secgate::entry(lib = "montest-lib")]
38pub fn dynamic_test(x: u32) -> Result<u32> {
39    Ok(42 + x)
40}
41
42static WAS_CTOR_RUN: AtomicBool = AtomicBool::new(false);
43
44#[used]
45#[doc(hidden)]
46#[allow(non_upper_case_globals)]
47#[link_section = ".init_array"]
48static ___cons_test___ctor: unsafe extern "C" fn() = {
49    #[allow(non_snake_case)]
50    #[link_section = ".text.startup"]
51    unsafe extern "C" fn ___cons_test___ctor() {
52        cons_test()
53    }
54    ___cons_test___ctor
55};
56unsafe fn cons_test() {
57    WAS_CTOR_RUN.store(true, Ordering::SeqCst);
58}