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