]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/sgx/mod.rs
Rework `at_exit` to `cleanup`
[rust.git] / library / std / src / sys / sgx / mod.rs
1 //! System bindings for the Fortanix SGX platform
2 //!
3 //! This module contains the facade (aka platform-specific) implementations of
4 //! OS level functionality for Fortanix SGX.
5 #![deny(unsafe_op_in_unsafe_fn)]
6
7 use crate::io::ErrorKind;
8 use crate::os::raw::c_char;
9 use crate::sync::atomic::{AtomicBool, Ordering};
10
11 pub mod abi;
12 mod waitqueue;
13
14 pub mod alloc;
15 pub mod args;
16 pub mod cmath;
17 pub mod condvar;
18 pub mod env;
19 pub mod ext;
20 pub mod fd;
21 #[path = "../unsupported/fs.rs"]
22 pub mod fs;
23 #[path = "../unsupported/io.rs"]
24 pub mod io;
25 pub mod memchr;
26 pub mod mutex;
27 pub mod net;
28 pub mod os;
29 pub mod path;
30 #[path = "../unsupported/pipe.rs"]
31 pub mod pipe;
32 #[path = "../unsupported/process.rs"]
33 pub mod process;
34 pub mod rwlock;
35 pub mod stack_overflow;
36 pub mod stdio;
37 pub mod thread;
38 pub mod thread_local_key;
39 pub mod time;
40
41 pub use crate::sys_common::os_str_bytes as os_str;
42
43 // SAFETY: must be called only once during runtime initialization.
44 pub unsafe fn init() {}
45
46 // SAFETY: must be called only once during runtime cleanup.
47 pub unsafe fn cleanup() {}
48
49 /// This function is used to implement functionality that simply doesn't exist.
50 /// Programs relying on this functionality will need to deal with the error.
51 pub fn unsupported<T>() -> crate::io::Result<T> {
52     Err(unsupported_err())
53 }
54
55 pub fn unsupported_err() -> crate::io::Error {
56     crate::io::Error::new_const(ErrorKind::Unsupported, &"operation not supported on SGX yet")
57 }
58
59 /// This function is used to implement various functions that doesn't exist,
60 /// but the lack of which might not be reason for error. If no error is
61 /// returned, the program might very well be able to function normally. This is
62 /// what happens when `SGX_INEFFECTIVE_ERROR` is set to `true`. If it is
63 /// `false`, the behavior is the same as `unsupported`.
64 pub fn sgx_ineffective<T>(v: T) -> crate::io::Result<T> {
65     static SGX_INEFFECTIVE_ERROR: AtomicBool = AtomicBool::new(false);
66     if SGX_INEFFECTIVE_ERROR.load(Ordering::Relaxed) {
67         Err(crate::io::Error::new_const(
68             ErrorKind::Other,
69             &"operation can't be trusted to have any effect on SGX",
70         ))
71     } else {
72         Ok(v)
73     }
74 }
75
76 pub fn decode_error_kind(code: i32) -> ErrorKind {
77     use fortanix_sgx_abi::Error;
78
79     // FIXME: not sure how to make sure all variants of Error are covered
80     if code == Error::NotFound as _ {
81         ErrorKind::NotFound
82     } else if code == Error::PermissionDenied as _ {
83         ErrorKind::PermissionDenied
84     } else if code == Error::ConnectionRefused as _ {
85         ErrorKind::ConnectionRefused
86     } else if code == Error::ConnectionReset as _ {
87         ErrorKind::ConnectionReset
88     } else if code == Error::ConnectionAborted as _ {
89         ErrorKind::ConnectionAborted
90     } else if code == Error::NotConnected as _ {
91         ErrorKind::NotConnected
92     } else if code == Error::AddrInUse as _ {
93         ErrorKind::AddrInUse
94     } else if code == Error::AddrNotAvailable as _ {
95         ErrorKind::AddrNotAvailable
96     } else if code == Error::BrokenPipe as _ {
97         ErrorKind::BrokenPipe
98     } else if code == Error::AlreadyExists as _ {
99         ErrorKind::AlreadyExists
100     } else if code == Error::WouldBlock as _ {
101         ErrorKind::WouldBlock
102     } else if code == Error::InvalidInput as _ {
103         ErrorKind::InvalidInput
104     } else if code == Error::InvalidData as _ {
105         ErrorKind::InvalidData
106     } else if code == Error::TimedOut as _ {
107         ErrorKind::TimedOut
108     } else if code == Error::WriteZero as _ {
109         ErrorKind::WriteZero
110     } else if code == Error::Interrupted as _ {
111         ErrorKind::Interrupted
112     } else if code == Error::Other as _ {
113         ErrorKind::Other
114     } else if code == Error::UnexpectedEof as _ {
115         ErrorKind::UnexpectedEof
116     } else {
117         ErrorKind::Other
118     }
119 }
120
121 pub unsafe fn strlen(mut s: *const c_char) -> usize {
122     let mut n = 0;
123     while unsafe { *s } != 0 {
124         n += 1;
125         s = unsafe { s.offset(1) };
126     }
127     return n;
128 }
129
130 pub fn abort_internal() -> ! {
131     abi::usercalls::exit(true)
132 }
133
134 // This function is needed by the panic runtime. The symbol is named in
135 // pre-link args for the target specification, so keep that in sync.
136 #[cfg(not(test))]
137 #[no_mangle]
138 // NB. used by both libunwind and libpanic_abort
139 pub extern "C" fn __rust_abort() {
140     abort_internal();
141 }
142
143 pub mod rand {
144     pub fn rdrand64() -> u64 {
145         unsafe {
146             let mut ret: u64 = 0;
147             for _ in 0..10 {
148                 if crate::arch::x86_64::_rdrand64_step(&mut ret) == 1 {
149                     return ret;
150                 }
151             }
152             rtabort!("Failed to obtain random data");
153         }
154     }
155 }
156
157 pub fn hashmap_random_keys() -> (u64, u64) {
158     (self::rand::rdrand64(), self::rand::rdrand64())
159 }
160
161 pub use crate::sys_common::{AsInner, FromInner, IntoInner};
162
163 pub trait TryIntoInner<Inner>: Sized {
164     fn try_into_inner(self) -> Result<Inner, Self>;
165 }