]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys/hermit/mod.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / library / std / src / sys / hermit / mod.rs
CommitLineData
e74abb32
XL
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
29967ef6
XL
16#![allow(unsafe_op_in_unsafe_fn)]
17
e74abb32 18use crate::intrinsics;
60c5eb7d 19use crate::os::raw::c_char;
e74abb32
XL
20
21pub mod alloc;
22pub mod args;
cdc7bbd5 23#[path = "../unix/cmath.rs"]
e74abb32 24pub mod cmath;
60c5eb7d 25pub mod condvar;
e74abb32 26pub mod env;
e74abb32 27pub mod fd;
60c5eb7d 28pub mod fs;
cdc7bbd5 29#[path = "../unsupported/io.rs"]
60c5eb7d
XL
30pub mod io;
31pub mod memchr;
32pub mod mutex;
e74abb32 33pub mod net;
60c5eb7d 34pub mod os;
cdc7bbd5 35#[path = "../unix/path.rs"]
e74abb32 36pub mod path;
6a06907d 37#[path = "../unsupported/pipe.rs"]
e74abb32 38pub mod pipe;
29967ef6 39#[path = "../unsupported/process.rs"]
e74abb32 40pub mod process;
60c5eb7d 41pub mod rwlock;
60c5eb7d
XL
42pub mod stdio;
43pub mod thread;
3dfed10e 44pub mod thread_local_dtor;
cdc7bbd5 45#[path = "../unsupported/thread_local_key.rs"]
3dfed10e 46pub mod thread_local_key;
60c5eb7d 47pub mod time;
e74abb32 48
e74abb32 49use crate::io::ErrorKind;
60c5eb7d 50pub use crate::sys_common::os_str_bytes as os_str;
e74abb32
XL
51
52#[allow(unused_extern_crates)]
53pub extern crate hermit_abi as abi;
54
55pub fn unsupported<T>() -> crate::io::Result<T> {
56 Err(unsupported_err())
57}
58
59pub fn unsupported_err() -> crate::io::Error {
cdc7bbd5
XL
60 crate::io::Error::new_const(
61 crate::io::ErrorKind::Unsupported,
62 &"operation not supported on HermitCore yet",
63 )
e74abb32
XL
64}
65
e74abb32
XL
66pub unsafe fn strlen(start: *const c_char) -> usize {
67 let mut str = start;
68
69 while *str != 0 {
70 str = str.offset(1);
71 }
72
73 (str as usize) - (start as usize)
74}
75
76#[no_mangle]
77pub extern "C" fn floor(x: f64) -> f64 {
60c5eb7d 78 unsafe { intrinsics::floorf64(x) }
e74abb32
XL
79}
80
f9f354fc
XL
81pub fn abort_internal() -> ! {
82 unsafe {
83 abi::abort();
84 }
e74abb32
XL
85}
86
87// FIXME: just a workaround to test the system
88pub fn hashmap_random_keys() -> (u64, u64) {
89 (1, 2)
90}
91
92// This function is needed by the panic runtime. The symbol is named in
93// pre-link args for the target specification, so keep that in sync.
94#[cfg(not(test))]
95#[no_mangle]
96// NB. used by both libunwind and libpanic_abort
f9f354fc 97pub extern "C" fn __rust_abort() {
e74abb32
XL
98 abort_internal();
99}
100
cdc7bbd5
XL
101// SAFETY: must be called only once during runtime initialization.
102// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
103pub unsafe fn init(argc: isize, argv: *const *const u8) {
ba9703b0 104 let _ = net::init();
cdc7bbd5
XL
105 args::init(argc, argv);
106}
107
108// SAFETY: must be called only once during runtime cleanup.
109// NOTE: this is not guaranteed to run, for example when the program aborts.
110pub unsafe fn cleanup() {
111 args::cleanup();
e74abb32
XL
112}
113
114#[cfg(not(test))]
115#[no_mangle]
60c5eb7d
XL
116pub unsafe extern "C" fn runtime_entry(
117 argc: i32,
118 argv: *const *const c_char,
119 env: *const *const c_char,
120) -> ! {
3dfed10e 121 use crate::sys::hermit::thread_local_dtor::run_dtors;
e74abb32
XL
122 extern "C" {
123 fn main(argc: isize, argv: *const *const c_char) -> i32;
124 }
125
126 // initialize environment
127 os::init_environment(env as *const *const i8);
128
129 let result = main(argc as isize, argv);
130
ba9703b0 131 run_dtors();
e74abb32
XL
132 abi::exit(result);
133}
134
135pub fn decode_error_kind(errno: i32) -> ErrorKind {
136 match errno {
137 x if x == 13 as i32 => ErrorKind::PermissionDenied,
138 x if x == 98 as i32 => ErrorKind::AddrInUse,
139 x if x == 99 as i32 => ErrorKind::AddrNotAvailable,
140 x if x == 11 as i32 => ErrorKind::WouldBlock,
141 x if x == 103 as i32 => ErrorKind::ConnectionAborted,
142 x if x == 111 as i32 => ErrorKind::ConnectionRefused,
143 x if x == 104 as i32 => ErrorKind::ConnectionReset,
144 x if x == 17 as i32 => ErrorKind::AlreadyExists,
145 x if x == 4 as i32 => ErrorKind::Interrupted,
146 x if x == 22 as i32 => ErrorKind::InvalidInput,
147 x if x == 2 as i32 => ErrorKind::NotFound,
148 x if x == 107 as i32 => ErrorKind::NotConnected,
149 x if x == 1 as i32 => ErrorKind::PermissionDenied,
150 x if x == 32 as i32 => ErrorKind::BrokenPipe,
151 x if x == 110 as i32 => ErrorKind::TimedOut,
152 _ => ErrorKind::Other,
153 }
154}
155
156pub fn cvt(result: i32) -> crate::io::Result<usize> {
60c5eb7d 157 if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) }
e74abb32 158}