1 //! System bindings for HermitCore
2 //!
3 //! This module contains the facade (aka platform-specific) implementations of
4 //! OS level functionality for HermitCore.
5 //!
6 //! This is all super highly experimental and not actually intended for
7 //! wide/production use yet, it's still all in the experimental category. This
8 //! will likely change over time.
9 //!
10 //! Currently all functions here are basically stubs that immediately return
11 //! errors. The hope is that with a portability lint we can turn actually just
12 //! remove all this and just omit parts of the standard library if we're
13 //! compiling for wasm. That way it's a compile time error for something that's
14 //! guaranteed to be a runtime error!
15 
16 #![allow(unsafe_op_in_unsafe_fn)]
17 
18 use crate::intrinsics;
19 use crate::os::raw::c_char;
20 
21 pub mod alloc;
22 pub mod args;
23 #[path = "../unix/cmath.rs"]
24 pub mod cmath;
25 pub mod condvar;
26 pub mod env;
27 pub mod fd;
28 pub mod fs;
29 #[path = "../unsupported/io.rs"]
30 pub mod io;
31 pub mod memchr;
32 pub mod mutex;
33 pub mod net;
34 pub mod os;
35 #[path = "../unix/os_str.rs"]
36 pub mod os_str;
37 #[path = "../unix/path.rs"]
38 pub mod path;
39 #[path = "../unsupported/pipe.rs"]
40 pub mod pipe;
41 #[path = "../unsupported/process.rs"]
42 pub mod process;
43 pub mod rwlock;
44 pub mod stdio;
45 pub mod thread;
46 pub mod thread_local_dtor;
47 #[path = "../unsupported/thread_local_key.rs"]
48 pub mod thread_local_key;
49 pub mod time;
50 
51 use crate::io::ErrorKind;
52 
53 #[allow(unused_extern_crates)]
54 pub extern crate hermit_abi as abi;
55 
unsupported<T>() -> crate::io::Result<T>56 pub fn unsupported<T>() -> crate::io::Result<T> {
57     Err(unsupported_err())
58 }
59 
unsupported_err() -> crate::io::Error60 pub fn unsupported_err() -> crate::io::Error {
61     crate::io::Error::new_const(
62         crate::io::ErrorKind::Unsupported,
63         &"operation not supported on HermitCore yet",
64     )
65 }
66 
strlen(start: *const c_char) -> usize67 pub unsafe fn strlen(start: *const c_char) -> usize {
68     let mut str = start;
69 
70     while *str != 0 {
71         str = str.offset(1);
72     }
73 
74     (str as usize) - (start as usize)
75 }
76 
77 #[no_mangle]
floor(x: f64) -> f6478 pub extern "C" fn floor(x: f64) -> f64 {
79     unsafe { intrinsics::floorf64(x) }
80 }
81 
abort_internal() -> !82 pub fn abort_internal() -> ! {
83     unsafe {
84         abi::abort();
85     }
86 }
87 
88 // FIXME: just a workaround to test the system
hashmap_random_keys() -> (u64, u64)89 pub fn hashmap_random_keys() -> (u64, u64) {
90     (1, 2)
91 }
92 
93 // This function is needed by the panic runtime. The symbol is named in
94 // pre-link args for the target specification, so keep that in sync.
95 #[cfg(not(test))]
96 #[no_mangle]
97 // NB. used by both libunwind and libpanic_abort
__rust_abort()98 pub extern "C" fn __rust_abort() {
99     abort_internal();
100 }
101 
102 // SAFETY: must be called only once during runtime initialization.
103 // NOTE: this is not guaranteed to run, for example when Rust code is called externally.
init(argc: isize, argv: *const *const u8)104 pub unsafe fn init(argc: isize, argv: *const *const u8) {
105     let _ = net::init();
106     args::init(argc, argv);
107 }
108 
109 // SAFETY: must be called only once during runtime cleanup.
110 // NOTE: this is not guaranteed to run, for example when the program aborts.
cleanup()111 pub unsafe fn cleanup() {
112     args::cleanup();
113 }
114 
115 #[cfg(not(test))]
116 #[no_mangle]
runtime_entry( argc: i32, argv: *const *const c_char, env: *const *const c_char, ) -> !117 pub unsafe extern "C" fn runtime_entry(
118     argc: i32,
119     argv: *const *const c_char,
120     env: *const *const c_char,
121 ) -> ! {
122     use crate::sys::hermit::thread_local_dtor::run_dtors;
123     extern "C" {
124         fn main(argc: isize, argv: *const *const c_char) -> i32;
125     }
126 
127     // initialize environment
128     os::init_environment(env as *const *const i8);
129 
130     let result = main(argc as isize, argv);
131 
132     run_dtors();
133     abi::exit(result);
134 }
135 
decode_error_kind(errno: i32) -> ErrorKind136 pub fn decode_error_kind(errno: i32) -> ErrorKind {
137     match errno {
138         x if x == 13 as i32 => ErrorKind::PermissionDenied,
139         x if x == 98 as i32 => ErrorKind::AddrInUse,
140         x if x == 99 as i32 => ErrorKind::AddrNotAvailable,
141         x if x == 11 as i32 => ErrorKind::WouldBlock,
142         x if x == 103 as i32 => ErrorKind::ConnectionAborted,
143         x if x == 111 as i32 => ErrorKind::ConnectionRefused,
144         x if x == 104 as i32 => ErrorKind::ConnectionReset,
145         x if x == 17 as i32 => ErrorKind::AlreadyExists,
146         x if x == 4 as i32 => ErrorKind::Interrupted,
147         x if x == 22 as i32 => ErrorKind::InvalidInput,
148         x if x == 2 as i32 => ErrorKind::NotFound,
149         x if x == 107 as i32 => ErrorKind::NotConnected,
150         x if x == 1 as i32 => ErrorKind::PermissionDenied,
151         x if x == 32 as i32 => ErrorKind::BrokenPipe,
152         x if x == 110 as i32 => ErrorKind::TimedOut,
153         _ => ErrorKind::Uncategorized,
154     }
155 }
156 
cvt(result: i32) -> crate::io::Result<usize>157 pub fn cvt(result: i32) -> crate::io::Result<usize> {
158     if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) }
159 }
160