montest_lib/
lib.rs

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