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