From 8c6c1dd3d3690859c8c8ee2056e7627f41a5afda Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 21 May 2020 16:43:59 -0700 Subject: [PATCH 1/7] Automatically calculate std::env::consts::ARCH. This simplifies the definition for ARCH. Note that this changes asmjs-unknown-emscripten ARCH to `wasm32`, which reflects the actual target arch. --- src/libstd/build.rs | 1 + src/libstd/env.rs | 77 +-------------------------------------------- 2 files changed, 2 insertions(+), 76 deletions(-) diff --git a/src/libstd/build.rs b/src/libstd/build.rs index 743a1778fbda3..f33af1ef7f668 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -63,4 +63,5 @@ fn main() { println!("cargo:rustc-link-lib=c"); println!("cargo:rustc-link-lib=compiler_rt"); } + println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap()); } diff --git a/src/libstd/env.rs b/src/libstd/env.rs index 97c20ca9459ef..6489e0709cb91 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -882,7 +882,7 @@ pub mod consts { /// - s390x /// - sparc64 #[stable(feature = "env", since = "1.0.0")] - pub const ARCH: &str = super::arch::ARCH; + pub const ARCH: &str = env!("STD_ENV_ARCH"); /// The family of the operating system. Example value is `unix`. /// @@ -966,81 +966,6 @@ pub mod consts { pub const EXE_EXTENSION: &str = os::EXE_EXTENSION; } -#[cfg(target_arch = "x86")] -mod arch { - pub const ARCH: &str = "x86"; -} - -#[cfg(target_arch = "x86_64")] -mod arch { - pub const ARCH: &str = "x86_64"; -} - -#[cfg(target_arch = "arm")] -mod arch { - pub const ARCH: &str = "arm"; -} - -#[cfg(target_arch = "aarch64")] -mod arch { - pub const ARCH: &str = "aarch64"; -} - -#[cfg(target_arch = "mips")] -mod arch { - pub const ARCH: &str = "mips"; -} - -#[cfg(target_arch = "mips64")] -mod arch { - pub const ARCH: &str = "mips64"; -} - -#[cfg(target_arch = "powerpc")] -mod arch { - pub const ARCH: &str = "powerpc"; -} - -#[cfg(target_arch = "powerpc64")] -mod arch { - pub const ARCH: &str = "powerpc64"; -} - -#[cfg(target_arch = "s390x")] -mod arch { - pub const ARCH: &str = "s390x"; -} - -#[cfg(target_arch = "sparc64")] -mod arch { - pub const ARCH: &str = "sparc64"; -} - -#[cfg(target_arch = "le32")] -mod arch { - pub const ARCH: &str = "le32"; -} - -#[cfg(target_arch = "asmjs")] -mod arch { - pub const ARCH: &str = "asmjs"; -} - -#[cfg(target_arch = "wasm32")] -mod arch { - pub const ARCH: &str = "wasm32"; -} - -#[cfg(target_arch = "hexagon")] -mod arch { - pub const ARCH: &'static str = "hexagon"; -} - -#[cfg(target_arch = "riscv64")] -mod arch { - pub const ARCH: &'static str = "riscv64"; -} - #[cfg(test)] mod tests { use super::*; From 432b4c14aad49f27bad3c59bb3bc85595e21a71b Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 21 May 2020 19:01:19 -0700 Subject: [PATCH 2/7] Use cfg_if in libtest. Simplifies some of the expressions, and provides a default. --- Cargo.lock | 1 + src/libtest/Cargo.toml | 1 + src/libtest/helpers/concurrency.rs | 171 ++++++++++++----------------- src/libtest/helpers/isatty.rs | 56 +++++----- 4 files changed, 98 insertions(+), 131 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5309c03ee23ae..386d5e960016a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4552,6 +4552,7 @@ dependencies = [ name = "test" version = "0.0.0" dependencies = [ + "cfg-if", "core", "getopts", "libc", diff --git a/src/libtest/Cargo.toml b/src/libtest/Cargo.toml index 170fbb984cf9b..7dddfd264ef8a 100644 --- a/src/libtest/Cargo.toml +++ b/src/libtest/Cargo.toml @@ -10,6 +10,7 @@ path = "lib.rs" crate-type = ["dylib", "rlib"] [dependencies] +cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] } getopts = { version = "0.2.21", features = ['rustc-dep-of-std'] } term = { path = "../libterm" } std = { path = "../libstd" } diff --git a/src/libtest/helpers/concurrency.rs b/src/libtest/helpers/concurrency.rs index e8f3820558a6d..2fe87247e3acf 100644 --- a/src/libtest/helpers/concurrency.rs +++ b/src/libtest/helpers/concurrency.rs @@ -14,61 +14,36 @@ pub fn get_concurrency() -> usize { } Err(..) => num_cpus(), }; +} - #[cfg(windows)] - #[allow(nonstandard_style)] - fn num_cpus() -> usize { - #[repr(C)] - struct SYSTEM_INFO { - wProcessorArchitecture: u16, - wReserved: u16, - dwPageSize: u32, - lpMinimumApplicationAddress: *mut u8, - lpMaximumApplicationAddress: *mut u8, - dwActiveProcessorMask: *mut u8, - dwNumberOfProcessors: u32, - dwProcessorType: u32, - dwAllocationGranularity: u32, - wProcessorLevel: u16, - wProcessorRevision: u16, - } - extern "system" { - fn GetSystemInfo(info: *mut SYSTEM_INFO) -> i32; - } - unsafe { - let mut sysinfo = std::mem::zeroed(); - GetSystemInfo(&mut sysinfo); - sysinfo.dwNumberOfProcessors as usize +cfg_if::cfg_if! { + if #[cfg(windows)] { + #[allow(nonstandard_style)] + fn num_cpus() -> usize { + #[repr(C)] + struct SYSTEM_INFO { + wProcessorArchitecture: u16, + wReserved: u16, + dwPageSize: u32, + lpMinimumApplicationAddress: *mut u8, + lpMaximumApplicationAddress: *mut u8, + dwActiveProcessorMask: *mut u8, + dwNumberOfProcessors: u32, + dwProcessorType: u32, + dwAllocationGranularity: u32, + wProcessorLevel: u16, + wProcessorRevision: u16, + } + extern "system" { + fn GetSystemInfo(info: *mut SYSTEM_INFO) -> i32; + } + unsafe { + let mut sysinfo = std::mem::zeroed(); + GetSystemInfo(&mut sysinfo); + sysinfo.dwNumberOfProcessors as usize + } } - } - - #[cfg(target_os = "vxworks")] - fn num_cpus() -> usize { - // FIXME: Implement num_cpus on vxWorks - 1 - } - - #[cfg(target_os = "redox")] - fn num_cpus() -> usize { - // FIXME: Implement num_cpus on Redox - 1 - } - - #[cfg(target_os = "hermit")] - fn num_cpus() -> usize { - // FIXME: Implement num_cpus on HermitCore - 1 - } - - #[cfg(any( - all(target_arch = "wasm32", not(target_os = "emscripten")), - all(target_vendor = "fortanix", target_env = "sgx") - ))] - fn num_cpus() -> usize { - 1 - } - - #[cfg(any( + } else if #[cfg(any( target_os = "android", target_os = "cloudabi", target_os = "emscripten", @@ -78,23 +53,46 @@ pub fn get_concurrency() -> usize { target_os = "macos", target_os = "solaris", target_os = "illumos", - ))] - fn num_cpus() -> usize { - unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize } - } - - #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] - fn num_cpus() -> usize { - use std::ptr; + ))] { + fn num_cpus() -> usize { + unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize } + } + } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] { + fn num_cpus() -> usize { + use std::ptr; - let mut cpus: libc::c_uint = 0; - let mut cpus_size = std::mem::size_of_val(&cpus); + let mut cpus: libc::c_uint = 0; + let mut cpus_size = std::mem::size_of_val(&cpus); - unsafe { - cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint; + unsafe { + cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint; + } + if cpus < 1 { + let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0]; + unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 2, + &mut cpus as *mut _ as *mut _, + &mut cpus_size as *mut _ as *mut _, + ptr::null_mut(), + 0, + ); + } + if cpus < 1 { + cpus = 1; + } + } + cpus as usize } - if cpus < 1 { + } else if #[cfg(target_os = "openbsd")] { + fn num_cpus() -> usize { + use std::ptr; + + let mut cpus: libc::c_uint = 0; + let mut cpus_size = std::mem::size_of_val(&cpus); let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0]; + unsafe { libc::sysctl( mib.as_mut_ptr(), @@ -108,43 +106,12 @@ pub fn get_concurrency() -> usize { if cpus < 1 { cpus = 1; } + cpus as usize } - cpus as usize - } - - #[cfg(target_os = "openbsd")] - fn num_cpus() -> usize { - use std::ptr; - - let mut cpus: libc::c_uint = 0; - let mut cpus_size = std::mem::size_of_val(&cpus); - let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0]; - - unsafe { - libc::sysctl( - mib.as_mut_ptr(), - 2, - &mut cpus as *mut _ as *mut _, - &mut cpus_size as *mut _ as *mut _, - ptr::null_mut(), - 0, - ); - } - if cpus < 1 { - cpus = 1; + } else { + // FIXME: implement on vxWorks, Redox, HermitCore, Haiku, l4re + fn num_cpus() -> usize { + 1 } - cpus as usize - } - - #[cfg(target_os = "haiku")] - fn num_cpus() -> usize { - // FIXME: implement - 1 - } - - #[cfg(target_os = "l4re")] - fn num_cpus() -> usize { - // FIXME: implement - 1 } } diff --git a/src/libtest/helpers/isatty.rs b/src/libtest/helpers/isatty.rs index 831094f7545c1..874ecc3764572 100644 --- a/src/libtest/helpers/isatty.rs +++ b/src/libtest/helpers/isatty.rs @@ -1,34 +1,32 @@ //! Helper module which provides a function to test //! if stdout is a tty. -#[cfg(any( - target_os = "cloudabi", - target_os = "hermit", - all(target_arch = "wasm32", not(target_os = "emscripten")), - all(target_vendor = "fortanix", target_env = "sgx") -))] -pub fn stdout_isatty() -> bool { - // FIXME: Implement isatty on SGX - false -} -#[cfg(unix)] -pub fn stdout_isatty() -> bool { - unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 } -} -#[cfg(windows)] -pub fn stdout_isatty() -> bool { - type DWORD = u32; - type BOOL = i32; - type HANDLE = *mut u8; - type LPDWORD = *mut u32; - const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD; - extern "system" { - fn GetStdHandle(which: DWORD) -> HANDLE; - fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL; - } - unsafe { - let handle = GetStdHandle(STD_OUTPUT_HANDLE); - let mut out = 0; - GetConsoleMode(handle, &mut out) != 0 +cfg_if::cfg_if! { + if #[cfg(unix)] { + pub fn stdout_isatty() -> bool { + unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 } + } + } else if #[cfg(windows)] { + pub fn stdout_isatty() -> bool { + type DWORD = u32; + type BOOL = i32; + type HANDLE = *mut u8; + type LPDWORD = *mut u32; + const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD; + extern "system" { + fn GetStdHandle(which: DWORD) -> HANDLE; + fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL; + } + unsafe { + let handle = GetStdHandle(STD_OUTPUT_HANDLE); + let mut out = 0; + GetConsoleMode(handle, &mut out) != 0 + } + } + } else { + // FIXME: Implement isatty on SGX + pub fn stdout_isatty() -> bool { + false + } } } From 9e58908e27d47683800ab0869a98502a6f485a62 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 21 May 2020 20:04:57 -0700 Subject: [PATCH 3/7] Use cfg_if in libpanic_abort. This allows setting a default abort using the core intrinsic. --- Cargo.lock | 1 + src/libpanic_abort/Cargo.toml | 1 + src/libpanic_abort/lib.rs | 34 ++++++++++++++++++---------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 386d5e960016a..28ff6b3b1ebf2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2056,6 +2056,7 @@ dependencies = [ name = "panic_abort" version = "0.0.0" dependencies = [ + "cfg-if", "compiler_builtins", "core", "libc", diff --git a/src/libpanic_abort/Cargo.toml b/src/libpanic_abort/Cargo.toml index 2bee0b716c750..dc385022440e0 100644 --- a/src/libpanic_abort/Cargo.toml +++ b/src/libpanic_abort/Cargo.toml @@ -11,6 +11,7 @@ bench = false doc = false [dependencies] +cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] } core = { path = "../libcore" } libc = { version = "0.2", default-features = false } compiler_builtins = "0.1.0" diff --git a/src/libpanic_abort/lib.rs b/src/libpanic_abort/lib.rs index 27056d5f934fd..497f40cafd3ed 100644 --- a/src/libpanic_abort/lib.rs +++ b/src/libpanic_abort/lib.rs @@ -40,23 +40,25 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen pub unsafe extern "C" fn __rust_start_panic(_payload: usize) -> u32 { abort(); - #[cfg(any(unix, target_os = "cloudabi"))] - unsafe fn abort() -> ! { - libc::abort(); - } - - #[cfg(any(windows, all(target_arch = "wasm32", not(target_os = "emscripten"))))] - unsafe fn abort() -> ! { - core::intrinsics::abort(); - } - - #[cfg(any(target_os = "hermit", all(target_vendor = "fortanix", target_env = "sgx")))] - unsafe fn abort() -> ! { - // call std::sys::abort_internal - extern "C" { - pub fn __rust_abort() -> !; + cfg_if::cfg_if! { + if #[cfg(any(unix, target_os = "cloudabi"))] { + unsafe fn abort() -> ! { + libc::abort(); + } + } else if #[cfg(any(target_os = "hermit", + all(target_vendor = "fortanix", target_env = "sgx")))] { + unsafe fn abort() -> ! { + // call std::sys::abort_internal + extern "C" { + pub fn __rust_abort() -> !; + } + __rust_abort(); + } + } else { + unsafe fn abort() -> ! { + core::intrinsics::abort(); + } } - __rust_abort(); } } From 6e9a1de0d146734e51cc7e761b288e9b9b138d4f Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 31 May 2020 18:09:25 -0700 Subject: [PATCH 4/7] Introduce restricted-std feature. --- src/libpanic_unwind/lib.rs | 2 +- src/libproc_macro/lib.rs | 1 + src/librustc_passes/stability.rs | 3 +- src/libstd/build.rs | 3 + src/libstd/lib.rs | 9 ++- src/libstd/sys/mod.rs | 3 +- src/libstd/sys/unsupported/alloc.rs | 22 +++++++ src/libstd/sys/unsupported/args.rs | 38 +++++++++++ src/libstd/sys/{wasm => unsupported}/cmath.rs | 0 src/libstd/sys/unsupported/common.rs | 48 ++++++++++++++ .../sys/{wasm => unsupported}/condvar.rs | 4 +- src/libstd/sys/unsupported/env.rs | 9 +++ src/libstd/sys/{wasm => unsupported}/fs.rs | 0 src/libstd/sys/{wasm => unsupported}/io.rs | 0 src/libstd/sys/unsupported/mod.rs | 24 +++++++ src/libstd/sys/{wasm => unsupported}/mutex.rs | 7 ++- src/libstd/sys/{wasm => unsupported}/net.rs | 0 src/libstd/sys/{wasm => unsupported}/os.rs | 21 +++---- src/libstd/sys/{wasm => unsupported}/path.rs | 0 src/libstd/sys/{wasm => unsupported}/pipe.rs | 0 .../sys/{wasm => unsupported}/process.rs | 0 .../sys/{wasm => unsupported}/rwlock.rs | 2 +- .../{wasm => unsupported}/stack_overflow.rs | 0 src/libstd/sys/{wasm => unsupported}/stdio.rs | 0 src/libstd/sys/unsupported/thread.rs | 41 ++++++++++++ .../thread_local_dtor.rs | 0 .../{wasm => unsupported}/thread_local_key.rs | 10 +-- src/libstd/sys/{wasm => unsupported}/time.rs | 4 +- src/libstd/sys/wasi/mod.rs | 45 ++++--------- src/libstd/sys/wasm/memchr.rs | 1 - src/libstd/sys/wasm/mod.rs | 63 ++++++------------- src/libstd/sys_common/mod.rs | 2 + src/libstd/sys_common/mutex.rs | 1 + src/libunwind/lib.rs | 11 +++- src/tools/rustc-std-workspace-std/lib.rs | 1 + 35 files changed, 266 insertions(+), 109 deletions(-) create mode 100644 src/libstd/sys/unsupported/alloc.rs create mode 100644 src/libstd/sys/unsupported/args.rs rename src/libstd/sys/{wasm => unsupported}/cmath.rs (100%) create mode 100644 src/libstd/sys/unsupported/common.rs rename src/libstd/sys/{wasm => unsupported}/condvar.rs (84%) create mode 100644 src/libstd/sys/unsupported/env.rs rename src/libstd/sys/{wasm => unsupported}/fs.rs (100%) rename src/libstd/sys/{wasm => unsupported}/io.rs (100%) create mode 100644 src/libstd/sys/unsupported/mod.rs rename src/libstd/sys/{wasm => unsupported}/mutex.rs (82%) rename src/libstd/sys/{wasm => unsupported}/net.rs (100%) rename src/libstd/sys/{wasm => unsupported}/os.rs (82%) rename src/libstd/sys/{wasm => unsupported}/path.rs (100%) rename src/libstd/sys/{wasm => unsupported}/pipe.rs (100%) rename src/libstd/sys/{wasm => unsupported}/process.rs (100%) rename src/libstd/sys/{wasm => unsupported}/rwlock.rs (95%) rename src/libstd/sys/{wasm => unsupported}/stack_overflow.rs (100%) rename src/libstd/sys/{wasm => unsupported}/stdio.rs (100%) create mode 100644 src/libstd/sys/unsupported/thread.rs rename src/libstd/sys/{wasm => unsupported}/thread_local_dtor.rs (100%) rename src/libstd/sys/{wasm => unsupported}/thread_local_key.rs (55%) rename src/libstd/sys/{wasm => unsupported}/time.rs (91%) delete mode 100644 src/libstd/sys/wasm/memchr.rs diff --git a/src/libpanic_unwind/lib.rs b/src/libpanic_unwind/lib.rs index f361354da2ac2..26b503cb7920b 100644 --- a/src/libpanic_unwind/lib.rs +++ b/src/libpanic_unwind/lib.rs @@ -41,7 +41,7 @@ cfg_if::cfg_if! { if #[cfg(target_os = "emscripten")] { #[path = "emcc.rs"] mod real_imp; - } else if #[cfg(target_arch = "wasm32")] { + } else if #[cfg(any(target_arch = "wasm32", target_os = "none"))] { #[path = "dummy.rs"] mod real_imp; } else if #[cfg(target_os = "hermit")] { diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 2d5bd7e872bd5..f960bdecc579f 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -26,6 +26,7 @@ #![feature(in_band_lifetimes)] #![feature(negative_impls)] #![feature(optin_builtin_traits)] +#![feature(restricted_std)] #![feature(rustc_attrs)] #![feature(min_specialization)] #![recursion_limit = "256"] diff --git a/src/librustc_passes/stability.rs b/src/librustc_passes/stability.rs index 76bc6b6c85f02..5d972c70d1392 100644 --- a/src/librustc_passes/stability.rs +++ b/src/librustc_passes/stability.rs @@ -502,7 +502,8 @@ impl Visitor<'tcx> for Checker<'tcx> { match item.kind { hir::ItemKind::ExternCrate(_) => { // compiler-generated `extern crate` items have a dummy span. - if item.span.is_dummy() { + // `std` is still checked for the `restricted-std` feature. + if item.span.is_dummy() && item.ident.as_str() != "std" { return; } diff --git a/src/libstd/build.rs b/src/libstd/build.rs index f33af1ef7f668..8317eedd53fff 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -64,4 +64,7 @@ fn main() { println!("cargo:rustc-link-lib=compiler_rt"); } println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap()); + if target.contains("-none") || target.contains("nvptx") { + println!("cargo:rustc-cfg=feature=\"restricted-std\""); + } } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 5215db7cdb3ce..23863b7706858 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -197,7 +197,8 @@ //! [primitive types]: ../book/ch03-02-data-types.html //! [rust-discord]: https://discord.gg/rust-lang -#![stable(feature = "rust1", since = "1.0.0")] +#![cfg_attr(not(feature = "restricted-std"), stable(feature = "rust1", since = "1.0.0"))] +#![cfg_attr(feature = "restricted-std", unstable(feature = "restricted_std", issue = "none"))] #![doc( html_root_url = "https://doc.rust-lang.org/nightly/", html_playground_url = "https://play.rust-lang.org/", @@ -552,3 +553,9 @@ include!("primitive_docs.rs"); // the rustdoc documentation for the existing keywords. Using `include!` // because rustdoc only looks for these modules at the crate level. include!("keyword_docs.rs"); + +// This is required to avoid an unstable error when `restricted-std` is not +// enabled. The use of #![feature(restricted_std)] in rustc-std-workspace-std +// is unconditional, so the unstable feature needs to be defined somewhere. +#[cfg_attr(not(feature = "restricted-std"), unstable(feature = "restricted_std", issue = "none"))] +mod __restricted_std_workaround {} diff --git a/src/libstd/sys/mod.rs b/src/libstd/sys/mod.rs index 875ff1af92013..7b5fac922d08a 100644 --- a/src/libstd/sys/mod.rs +++ b/src/libstd/sys/mod.rs @@ -48,7 +48,8 @@ cfg_if::cfg_if! { mod sgx; pub use self::sgx::*; } else { - compile_error!("libstd doesn't compile for this platform yet"); + mod unsupported; + pub use self::unsupported::*; } } diff --git a/src/libstd/sys/unsupported/alloc.rs b/src/libstd/sys/unsupported/alloc.rs new file mode 100644 index 0000000000000..8d5d0a2f5ccd1 --- /dev/null +++ b/src/libstd/sys/unsupported/alloc.rs @@ -0,0 +1,22 @@ +use crate::alloc::{GlobalAlloc, Layout, System}; + +#[stable(feature = "alloc_system_type", since = "1.28.0")] +unsafe impl GlobalAlloc for System { + #[inline] + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + 0 as *mut u8 + } + + #[inline] + unsafe fn alloc_zeroed(&self, _layout: Layout) -> *mut u8 { + 0 as *mut u8 + } + + #[inline] + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} + + #[inline] + unsafe fn realloc(&self, _ptr: *mut u8, _layout: Layout, _new_size: usize) -> *mut u8 { + 0 as *mut u8 + } +} diff --git a/src/libstd/sys/unsupported/args.rs b/src/libstd/sys/unsupported/args.rs new file mode 100644 index 0000000000000..71d0c5fa13e18 --- /dev/null +++ b/src/libstd/sys/unsupported/args.rs @@ -0,0 +1,38 @@ +use crate::ffi::OsString; + +pub unsafe fn init(_argc: isize, _argv: *const *const u8) {} +pub unsafe fn cleanup() {} + +pub struct Args {} + +pub fn args() -> Args { + Args {} +} + +impl Args { + pub fn inner_debug(&self) -> &[OsString] { + &[] + } +} + +impl Iterator for Args { + type Item = OsString; + fn next(&mut self) -> Option { + None + } + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } +} + +impl ExactSizeIterator for Args { + fn len(&self) -> usize { + 0 + } +} + +impl DoubleEndedIterator for Args { + fn next_back(&mut self) -> Option { + None + } +} diff --git a/src/libstd/sys/wasm/cmath.rs b/src/libstd/sys/unsupported/cmath.rs similarity index 100% rename from src/libstd/sys/wasm/cmath.rs rename to src/libstd/sys/unsupported/cmath.rs diff --git a/src/libstd/sys/unsupported/common.rs b/src/libstd/sys/unsupported/common.rs new file mode 100644 index 0000000000000..80311d26819ad --- /dev/null +++ b/src/libstd/sys/unsupported/common.rs @@ -0,0 +1,48 @@ +use crate::io as std_io; + +pub mod memchr { + pub use core::slice::memchr::{memchr, memrchr}; +} + +pub use crate::sys_common::os_str_bytes as os_str; + +// This is not necessarily correct. May want to consider making it part of the +// spec definition? +use crate::os::raw::c_char; + +#[cfg(not(test))] +pub fn init() {} + +pub fn unsupported() -> std_io::Result { + Err(unsupported_err()) +} + +pub fn unsupported_err() -> std_io::Error { + std_io::Error::new(std_io::ErrorKind::Other, "operation not supported on this platform") +} + +pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind { + crate::io::ErrorKind::Other +} + +pub fn abort_internal() -> ! { + core::intrinsics::abort(); +} + +pub fn hashmap_random_keys() -> (u64, u64) { + (1, 2) +} + +// This enum is used as the storage for a bunch of types which can't actually +// exist. +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] +pub enum Void {} + +pub unsafe fn strlen(mut s: *const c_char) -> usize { + let mut n = 0; + while *s != 0 { + n += 1; + s = s.offset(1); + } + return n; +} diff --git a/src/libstd/sys/wasm/condvar.rs b/src/libstd/sys/unsupported/condvar.rs similarity index 84% rename from src/libstd/sys/wasm/condvar.rs rename to src/libstd/sys/unsupported/condvar.rs index 9fd781c728215..a578eee8ccce2 100644 --- a/src/libstd/sys/wasm/condvar.rs +++ b/src/libstd/sys/unsupported/condvar.rs @@ -18,11 +18,11 @@ impl Condvar { pub unsafe fn notify_all(&self) {} pub unsafe fn wait(&self, _mutex: &Mutex) { - panic!("can't block with web assembly") + panic!("condvar wait not supported") } pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool { - panic!("can't block with web assembly"); + panic!("condvar wait not supported"); } #[inline] diff --git a/src/libstd/sys/unsupported/env.rs b/src/libstd/sys/unsupported/env.rs new file mode 100644 index 0000000000000..d2efec506c56b --- /dev/null +++ b/src/libstd/sys/unsupported/env.rs @@ -0,0 +1,9 @@ +pub mod os { + pub const FAMILY: &str = ""; + pub const OS: &str = ""; + pub const DLL_PREFIX: &str = ""; + pub const DLL_SUFFIX: &str = ""; + pub const DLL_EXTENSION: &str = ""; + pub const EXE_SUFFIX: &str = ""; + pub const EXE_EXTENSION: &str = ""; +} diff --git a/src/libstd/sys/wasm/fs.rs b/src/libstd/sys/unsupported/fs.rs similarity index 100% rename from src/libstd/sys/wasm/fs.rs rename to src/libstd/sys/unsupported/fs.rs diff --git a/src/libstd/sys/wasm/io.rs b/src/libstd/sys/unsupported/io.rs similarity index 100% rename from src/libstd/sys/wasm/io.rs rename to src/libstd/sys/unsupported/io.rs diff --git a/src/libstd/sys/unsupported/mod.rs b/src/libstd/sys/unsupported/mod.rs new file mode 100644 index 0000000000000..87f655eecd54e --- /dev/null +++ b/src/libstd/sys/unsupported/mod.rs @@ -0,0 +1,24 @@ +pub mod alloc; +pub mod args; +pub mod cmath; +pub mod condvar; +pub mod env; +pub mod fs; +pub mod io; +pub mod mutex; +pub mod net; +pub mod os; +pub mod path; +pub mod pipe; +pub mod process; +pub mod rwlock; +pub mod stack_overflow; +pub mod stdio; +pub mod thread; +#[cfg(target_thread_local)] +pub mod thread_local_dtor; +pub mod thread_local_key; +pub mod time; + +mod common; +pub use common::*; diff --git a/src/libstd/sys/wasm/mutex.rs b/src/libstd/sys/unsupported/mutex.rs similarity index 82% rename from src/libstd/sys/wasm/mutex.rs rename to src/libstd/sys/unsupported/mutex.rs index 7aaf1b3a343b6..9ef8af52eb5c2 100644 --- a/src/libstd/sys/wasm/mutex.rs +++ b/src/libstd/sys/unsupported/mutex.rs @@ -5,9 +5,10 @@ pub struct Mutex { } unsafe impl Send for Mutex {} -unsafe impl Sync for Mutex {} // no threads on wasm +unsafe impl Sync for Mutex {} // no threads on this platform impl Mutex { + #[rustc_const_stable(feature = "const_sys_mutex_new", since = "1.0.0")] pub const fn new() -> Mutex { Mutex { locked: UnsafeCell::new(false) } } @@ -42,8 +43,8 @@ impl Mutex { pub unsafe fn destroy(&self) {} } -// All empty stubs because wasm has no threads yet, so lock acquisition always -// succeeds. +// All empty stubs because this platform does not yet support threads, so lock +// acquisition always succeeds. pub struct ReentrantMutex {} impl ReentrantMutex { diff --git a/src/libstd/sys/wasm/net.rs b/src/libstd/sys/unsupported/net.rs similarity index 100% rename from src/libstd/sys/wasm/net.rs rename to src/libstd/sys/unsupported/net.rs diff --git a/src/libstd/sys/wasm/os.rs b/src/libstd/sys/unsupported/os.rs similarity index 82% rename from src/libstd/sys/wasm/os.rs rename to src/libstd/sys/unsupported/os.rs index 91afdc8a5a0cc..0615780c24212 100644 --- a/src/libstd/sys/wasm/os.rs +++ b/src/libstd/sys/unsupported/os.rs @@ -1,10 +1,9 @@ +use super::{unsupported, Void}; use crate::error::Error as StdError; use crate::ffi::{OsStr, OsString}; use crate::fmt; use crate::io; use crate::path::{self, PathBuf}; -use crate::str; -use crate::sys::{unsupported, Void}; pub fn errno() -> i32 { 0 @@ -48,14 +47,14 @@ where impl fmt::Display for JoinPathsError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - "not supported on wasm yet".fmt(f) + "not supported on this platform yet".fmt(f) } } impl StdError for JoinPathsError { #[allow(deprecated)] fn description(&self) -> &str { - "not supported on wasm yet" + "not supported on this platform yet" } } @@ -73,7 +72,7 @@ impl Iterator for Env { } pub fn env() -> Env { - panic!("not supported on web assembly") + panic!("not supported on this platform") } pub fn getenv(_: &OsStr) -> io::Result> { @@ -81,15 +80,15 @@ pub fn getenv(_: &OsStr) -> io::Result> { } pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> { - Err(io::Error::new(io::ErrorKind::Other, "cannot set env vars on wasm32-unknown-unknown")) + Err(io::Error::new(io::ErrorKind::Other, "cannot set env vars on this platform")) } pub fn unsetenv(_: &OsStr) -> io::Result<()> { - Err(io::Error::new(io::ErrorKind::Other, "cannot unset env vars on wasm32-unknown-unknown")) + Err(io::Error::new(io::ErrorKind::Other, "cannot unset env vars on this platform")) } pub fn temp_dir() -> PathBuf { - panic!("no filesystem on wasm") + panic!("no filesystem on this platform") } pub fn home_dir() -> Option { @@ -97,11 +96,9 @@ pub fn home_dir() -> Option { } pub fn exit(_code: i32) -> ! { - unsafe { - crate::arch::wasm32::unreachable(); - } + crate::intrinsics::abort() } pub fn getpid() -> u32 { - panic!("no pids on wasm") + panic!("no pids on this platform") } diff --git a/src/libstd/sys/wasm/path.rs b/src/libstd/sys/unsupported/path.rs similarity index 100% rename from src/libstd/sys/wasm/path.rs rename to src/libstd/sys/unsupported/path.rs diff --git a/src/libstd/sys/wasm/pipe.rs b/src/libstd/sys/unsupported/pipe.rs similarity index 100% rename from src/libstd/sys/wasm/pipe.rs rename to src/libstd/sys/unsupported/pipe.rs diff --git a/src/libstd/sys/wasm/process.rs b/src/libstd/sys/unsupported/process.rs similarity index 100% rename from src/libstd/sys/wasm/process.rs rename to src/libstd/sys/unsupported/process.rs diff --git a/src/libstd/sys/wasm/rwlock.rs b/src/libstd/sys/unsupported/rwlock.rs similarity index 95% rename from src/libstd/sys/wasm/rwlock.rs rename to src/libstd/sys/unsupported/rwlock.rs index a59944482e9e9..d37f34ac9352d 100644 --- a/src/libstd/sys/wasm/rwlock.rs +++ b/src/libstd/sys/unsupported/rwlock.rs @@ -5,7 +5,7 @@ pub struct RWLock { } unsafe impl Send for RWLock {} -unsafe impl Sync for RWLock {} // no threads on wasm +unsafe impl Sync for RWLock {} // no threads on this platform impl RWLock { pub const fn new() -> RWLock { diff --git a/src/libstd/sys/wasm/stack_overflow.rs b/src/libstd/sys/unsupported/stack_overflow.rs similarity index 100% rename from src/libstd/sys/wasm/stack_overflow.rs rename to src/libstd/sys/unsupported/stack_overflow.rs diff --git a/src/libstd/sys/wasm/stdio.rs b/src/libstd/sys/unsupported/stdio.rs similarity index 100% rename from src/libstd/sys/wasm/stdio.rs rename to src/libstd/sys/unsupported/stdio.rs diff --git a/src/libstd/sys/unsupported/thread.rs b/src/libstd/sys/unsupported/thread.rs new file mode 100644 index 0000000000000..20ae309db30d7 --- /dev/null +++ b/src/libstd/sys/unsupported/thread.rs @@ -0,0 +1,41 @@ +use super::{unsupported, Void}; +use crate::ffi::CStr; +use crate::io; +use crate::time::Duration; + +pub struct Thread(Void); + +pub const DEFAULT_MIN_STACK_SIZE: usize = 4096; + +impl Thread { + // unsafe: see thread::Builder::spawn_unchecked for safety requirements + pub unsafe fn new(_stack: usize, _p: Box) -> io::Result { + unsupported() + } + + pub fn yield_now() { + // do nothing + } + + pub fn set_name(_name: &CStr) { + // nope + } + + pub fn sleep(_dur: Duration) { + panic!("can't sleep"); + } + + pub fn join(self) { + match self.0 {} + } +} + +pub mod guard { + pub type Guard = !; + pub unsafe fn current() -> Option { + None + } + pub unsafe fn init() -> Option { + None + } +} diff --git a/src/libstd/sys/wasm/thread_local_dtor.rs b/src/libstd/sys/unsupported/thread_local_dtor.rs similarity index 100% rename from src/libstd/sys/wasm/thread_local_dtor.rs rename to src/libstd/sys/unsupported/thread_local_dtor.rs diff --git a/src/libstd/sys/wasm/thread_local_key.rs b/src/libstd/sys/unsupported/thread_local_key.rs similarity index 55% rename from src/libstd/sys/wasm/thread_local_key.rs rename to src/libstd/sys/unsupported/thread_local_key.rs index f8be9863ed56f..c31b61cbf56d3 100644 --- a/src/libstd/sys/wasm/thread_local_key.rs +++ b/src/libstd/sys/unsupported/thread_local_key.rs @@ -2,25 +2,25 @@ pub type Key = usize; #[inline] pub unsafe fn create(_dtor: Option) -> Key { - panic!("should not be used on the wasm target"); + panic!("should not be used on this target"); } #[inline] pub unsafe fn set(_key: Key, _value: *mut u8) { - panic!("should not be used on the wasm target"); + panic!("should not be used on this target"); } #[inline] pub unsafe fn get(_key: Key) -> *mut u8 { - panic!("should not be used on the wasm target"); + panic!("should not be used on this target"); } #[inline] pub unsafe fn destroy(_key: Key) { - panic!("should not be used on the wasm target"); + panic!("should not be used on this target"); } #[inline] pub fn requires_synchronized_create() -> bool { - panic!("should not be used on the wasm target"); + panic!("should not be used on this target"); } diff --git a/src/libstd/sys/wasm/time.rs b/src/libstd/sys/unsupported/time.rs similarity index 91% rename from src/libstd/sys/wasm/time.rs rename to src/libstd/sys/unsupported/time.rs index d9edc7fdc4451..8aaf1777f2427 100644 --- a/src/libstd/sys/wasm/time.rs +++ b/src/libstd/sys/unsupported/time.rs @@ -10,7 +10,7 @@ pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0)); impl Instant { pub fn now() -> Instant { - panic!("time not implemented on wasm32-unknown-unknown") + panic!("time not implemented on this platform") } pub const fn zero() -> Instant { @@ -36,7 +36,7 @@ impl Instant { impl SystemTime { pub fn now() -> SystemTime { - panic!("time not implemented on wasm32-unknown-unknown") + panic!("time not implemented on this platform") } pub fn sub_time(&self, other: &SystemTime) -> Result { diff --git a/src/libstd/sys/wasi/mod.rs b/src/libstd/sys/wasi/mod.rs index 85f5282034ff1..2704ff484f991 100644 --- a/src/libstd/sys/wasi/mod.rs +++ b/src/libstd/sys/wasi/mod.rs @@ -16,21 +16,18 @@ use crate::io as std_io; use crate::mem; -use crate::os::raw::c_char; pub mod alloc; pub mod args; -#[path = "../wasm/cmath.rs"] +#[path = "../unsupported/cmath.rs"] pub mod cmath; -#[path = "../wasm/condvar.rs"] +#[path = "../unsupported/condvar.rs"] pub mod condvar; pub mod env; pub mod fd; pub mod fs; pub mod io; -#[path = "../wasm/memchr.rs"] -pub mod memchr; -#[path = "../wasm/mutex.rs"] +#[path = "../unsupported/mutex.rs"] pub mod mutex; pub mod net; pub mod os; @@ -39,28 +36,22 @@ pub mod ext; pub mod path; pub mod pipe; pub mod process; -#[path = "../wasm/rwlock.rs"] +#[path = "../unsupported/rwlock.rs"] pub mod rwlock; -#[path = "../wasm/stack_overflow.rs"] +#[path = "../unsupported/stack_overflow.rs"] pub mod stack_overflow; pub mod stdio; pub mod thread; -#[path = "../wasm/thread_local_dtor.rs"] +#[path = "../unsupported/thread_local_dtor.rs"] pub mod thread_local_dtor; -#[path = "../wasm/thread_local_key.rs"] +#[path = "../unsupported/thread_local_key.rs"] pub mod thread_local_key; pub mod time; -#[cfg(not(test))] -pub fn init() {} - -pub fn unsupported() -> std_io::Result { - Err(unsupported_err()) -} - -pub fn unsupported_err() -> std_io::Error { - std_io::Error::new(std_io::ErrorKind::Other, "operation not supported on wasm yet") -} +#[path = "../unsupported/common.rs"] +#[allow(unused)] +mod common; +pub use common::*; pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind { use std_io::ErrorKind::*; @@ -86,20 +77,6 @@ pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind { } } -// This enum is used as the storage for a bunch of types which can't actually -// exist. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] -pub enum Void {} - -pub unsafe fn strlen(mut s: *const c_char) -> usize { - let mut n = 0; - while *s != 0 { - n += 1; - s = s.offset(1); - } - return n; -} - pub fn abort_internal() -> ! { unsafe { libc::abort() } } diff --git a/src/libstd/sys/wasm/memchr.rs b/src/libstd/sys/wasm/memchr.rs deleted file mode 100644 index 9967482197eb3..0000000000000 --- a/src/libstd/sys/wasm/memchr.rs +++ /dev/null @@ -1 +0,0 @@ -pub use core::slice::memchr::{memchr, memrchr}; diff --git a/src/libstd/sys/wasm/mod.rs b/src/libstd/sys/wasm/mod.rs index 6939596e52d78..3de5890404357 100644 --- a/src/libstd/sys/wasm/mod.rs +++ b/src/libstd/sys/wasm/mod.rs @@ -14,25 +14,35 @@ //! compiling for wasm. That way it's a compile time error for something that's //! guaranteed to be a runtime error! -use crate::os::raw::c_char; - pub mod alloc; pub mod args; +#[path = "../unsupported/cmath.rs"] pub mod cmath; pub mod env; +#[path = "../unsupported/fs.rs"] pub mod fs; +#[path = "../unsupported/io.rs"] pub mod io; -pub mod memchr; +#[path = "../unsupported/net.rs"] pub mod net; +#[path = "../unsupported/os.rs"] pub mod os; +#[path = "../unsupported/path.rs"] pub mod path; +#[path = "../unsupported/pipe.rs"] pub mod pipe; +#[path = "../unsupported/process.rs"] pub mod process; +#[path = "../unsupported/stack_overflow.rs"] pub mod stack_overflow; +#[path = "../unsupported/stdio.rs"] pub mod stdio; pub mod thread; +#[path = "../unsupported/thread_local_dtor.rs"] pub mod thread_local_dtor; +#[path = "../unsupported/thread_local_key.rs"] pub mod thread_local_key; +#[path = "../unsupported/time.rs"] pub mod time; pub use crate::sys_common::os_str_bytes as os_str; @@ -46,50 +56,15 @@ cfg_if::cfg_if! { #[path = "rwlock_atomics.rs"] pub mod rwlock; } else { + #[path = "../unsupported/condvar.rs"] pub mod condvar; + #[path = "../unsupported/mutex.rs"] pub mod mutex; + #[path = "../unsupported/rwlock.rs"] pub mod rwlock; } } -#[cfg(not(test))] -pub fn init() {} - -pub fn unsupported() -> crate::io::Result { - Err(unsupported_err()) -} - -pub fn unsupported_err() -> crate::io::Error { - crate::io::Error::new(crate::io::ErrorKind::Other, "operation not supported on wasm yet") -} - -pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind { - crate::io::ErrorKind::Other -} - -// This enum is used as the storage for a bunch of types which can't actually -// exist. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] -pub enum Void {} - -pub unsafe fn strlen(mut s: *const c_char) -> usize { - let mut n = 0; - while *s != 0 { - n += 1; - s = s.offset(1); - } - return n; -} - -pub fn abort_internal() -> ! { - unsafe { crate::arch::wasm32::unreachable() } -} - -// We don't have randomness yet, but I totally used a random number generator to -// generate these numbers. -// -// More seriously though this is just for DOS protection in hash maps. It's ok -// if we don't do that on wasm just yet. -pub fn hashmap_random_keys() -> (u64, u64) { - (1, 2) -} +#[path = "../unsupported/common.rs"] +mod common; +pub use common::*; diff --git a/src/libstd/sys_common/mod.rs b/src/libstd/sys_common/mod.rs index e57bb267cbd0f..c3e6a5570e6ae 100644 --- a/src/libstd/sys_common/mod.rs +++ b/src/libstd/sys_common/mod.rs @@ -57,6 +57,7 @@ pub mod mutex; target_os = "cloudabi", target_os = "hermit", target_arch = "wasm32", + feature = "restricted-std", all(target_vendor = "fortanix", target_env = "sgx")))] pub mod os_str_bytes; pub mod poison; @@ -74,6 +75,7 @@ cfg_if::cfg_if! { if #[cfg(any(target_os = "cloudabi", target_os = "l4re", target_os = "hermit", + feature = "restricted-std", all(target_arch = "wasm32", not(target_os = "emscripten")), all(target_vendor = "fortanix", target_env = "sgx")))] { pub use crate::sys::net; diff --git a/src/libstd/sys_common/mutex.rs b/src/libstd/sys_common/mutex.rs index 899fc6a723530..e66d8994147e1 100644 --- a/src/libstd/sys_common/mutex.rs +++ b/src/libstd/sys_common/mutex.rs @@ -17,6 +17,7 @@ impl Mutex { /// Also, until `init` is called, behavior is undefined if this /// mutex is ever used reentrantly, i.e., `raw_lock` or `try_lock` /// are called by the thread currently holding the lock. + #[rustc_const_stable(feature = "const_sys_mutex_new", since = "1.0.0")] pub const fn new() -> Mutex { Mutex(imp::Mutex::new()) } diff --git a/src/libunwind/lib.rs b/src/libunwind/lib.rs index cc025da1af555..c705ed31fd5a4 100644 --- a/src/libunwind/lib.rs +++ b/src/libunwind/lib.rs @@ -10,7 +10,16 @@ cfg_if::cfg_if! { if #[cfg(target_env = "msvc")] { // no extra unwinder support needed - } else if #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] { + } else if #[cfg( + any( + all(target_arch = "wasm32", not(target_os = "emscripten")), + target_os = "none", + target_os = "hermit", + target_os = "uefi", + target_os = "cuda", + target_os = "l4re", + ))] + { // no unwinder on the system! } else { mod libunwind; diff --git a/src/tools/rustc-std-workspace-std/lib.rs b/src/tools/rustc-std-workspace-std/lib.rs index f40d09cafbb47..1e955c61ac85f 100644 --- a/src/tools/rustc-std-workspace-std/lib.rs +++ b/src/tools/rustc-std-workspace-std/lib.rs @@ -1 +1,2 @@ +#![feature(restricted_std)] pub use std::*; From cee9f05c2d369cda1d8487fe6e475a2ae990b9f8 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 6 Jul 2020 20:25:44 -0700 Subject: [PATCH 5/7] Tweak formatting. --- src/libpanic_abort/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libpanic_abort/lib.rs b/src/libpanic_abort/lib.rs index 497f40cafd3ed..95f3966228a2f 100644 --- a/src/libpanic_abort/lib.rs +++ b/src/libpanic_abort/lib.rs @@ -46,7 +46,8 @@ pub unsafe extern "C" fn __rust_start_panic(_payload: usize) -> u32 { libc::abort(); } } else if #[cfg(any(target_os = "hermit", - all(target_vendor = "fortanix", target_env = "sgx")))] { + all(target_vendor = "fortanix", target_env = "sgx") + ))] { unsafe fn abort() -> ! { // call std::sys::abort_internal extern "C" { From 0eb293ddb72ce080b3d0a6bdf643bd645849fa9e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 6 Jul 2020 20:31:12 -0700 Subject: [PATCH 6/7] Use an allow-list of platforms that support std. Use a fall-through for no_std targets. --- src/libpanic_unwind/lib.rs | 20 ++++++++++++++++---- src/libstd/build.rs | 26 +++++++++++++++++++++++--- src/libunwind/lib.rs | 36 +++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/libpanic_unwind/lib.rs b/src/libpanic_unwind/lib.rs index 26b503cb7920b..430062d4ac44e 100644 --- a/src/libpanic_unwind/lib.rs +++ b/src/libpanic_unwind/lib.rs @@ -41,21 +41,33 @@ cfg_if::cfg_if! { if #[cfg(target_os = "emscripten")] { #[path = "emcc.rs"] mod real_imp; - } else if #[cfg(any(target_arch = "wasm32", target_os = "none"))] { - #[path = "dummy.rs"] - mod real_imp; } else if #[cfg(target_os = "hermit")] { #[path = "hermit.rs"] mod real_imp; } else if #[cfg(target_env = "msvc")] { #[path = "seh.rs"] mod real_imp; - } else { + } else if #[cfg(any( + all(target_family = "windows", target_env = "gnu"), + target_os = "cloudabi", + target_family = "unix", + all(target_vendor = "fortanix", target_env = "sgx"), + ))] { // Rust runtime's startup objects depend on these symbols, so make them public. #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))] pub use real_imp::eh_frame_registry::*; #[path = "gcc.rs"] mod real_imp; + } else { + // Targets that don't support unwinding. + // - arch=wasm32 + // - os=none ("bare metal" targets) + // - os=uefi + // - nvptx64-nvidia-cuda + // - avr-unknown-unknown + // - mipsel-sony-psp + #[path = "dummy.rs"] + mod real_imp; } } diff --git a/src/libstd/build.rs b/src/libstd/build.rs index 8317eedd53fff..eb2753d62457a 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -62,9 +62,29 @@ fn main() { } println!("cargo:rustc-link-lib=c"); println!("cargo:rustc-link-lib=compiler_rt"); - } - println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap()); - if target.contains("-none") || target.contains("nvptx") { + } else if (target.contains("sgx") && target.contains("fortanix")) + || target.contains("hermit") + || target.contains("l4re") + || target.contains("redox") + || target.contains("haiku") + || target.contains("vxworks") + || target.contains("wasm32") + || target.contains("asmjs") + { + // These platforms don't have any special requirements. + } else { + // This is for Cargo's build-std support, to mark std as unstable for + // typically no_std platforms. + // This covers: + // - os=none ("bare metal" targets) + // - mipsel-sony-psp + // - nvptx64-nvidia-cuda + // - avr-unknown-unknown + // - tvos (aarch64-apple-tvos, x86_64-apple-tvos) + // - uefi (x86_64-unknown-uefi, i686-unknown-uefi) + // - JSON targets + // - Any new targets that have not been explicitly added above. println!("cargo:rustc-cfg=feature=\"restricted-std\""); } + println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap()); } diff --git a/src/libunwind/lib.rs b/src/libunwind/lib.rs index c705ed31fd5a4..c4d10ab177be9 100644 --- a/src/libunwind/lib.rs +++ b/src/libunwind/lib.rs @@ -9,21 +9,31 @@ cfg_if::cfg_if! { if #[cfg(target_env = "msvc")] { - // no extra unwinder support needed - } else if #[cfg( - any( - all(target_arch = "wasm32", not(target_os = "emscripten")), - target_os = "none", - target_os = "hermit", - target_os = "uefi", - target_os = "cuda", - target_os = "l4re", - ))] - { - // no unwinder on the system! - } else { + // Windows MSVC no extra unwinder support needed + } else if #[cfg(any( + target_os = "l4re", + target_os = "none", + ))] { + // These "unix" family members do not have unwinder. + // Note this also matches x86_64-linux-kernel. + } else if #[cfg(any( + unix, + windows, + target_os = "cloudabi", + all(target_vendor = "fortanix", target_env = "sgx"), + ))] { mod libunwind; pub use libunwind::*; + } else { + // no unwinder on the system! + // - wasm32 (not emscripten, which is "unix" family) + // - os=none ("bare metal" targets) + // - os=hermit + // - os=uefi + // - os=cuda + // - nvptx64-nvidia-cuda + // - mipsel-sony-psp + // - Any new targets not listed above. } } From 3d44d3ccfd0a17c6d45bc504be182b47efe1018d Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 7 Jul 2020 10:51:45 -0700 Subject: [PATCH 7/7] Simplify os_str_bytes cfg expression. --- src/libstd/sys_common/mod.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/libstd/sys_common/mod.rs b/src/libstd/sys_common/mod.rs index c3e6a5570e6ae..840f9093e00f1 100644 --- a/src/libstd/sys_common/mod.rs +++ b/src/libstd/sys_common/mod.rs @@ -51,14 +51,9 @@ pub mod condvar; pub mod fs; pub mod io; pub mod mutex; -#[cfg(any(doc, // see `mod os`, docs are generated for multiple platforms - unix, - target_os = "redox", - target_os = "cloudabi", - target_os = "hermit", - target_arch = "wasm32", - feature = "restricted-std", - all(target_vendor = "fortanix", target_env = "sgx")))] +// `doc` is required because `sys/mod.rs` imports `unix/ext/mod.rs` on Windows +// when generating documentation. +#[cfg(any(doc, not(windows)))] pub mod os_str_bytes; pub mod poison; pub mod process;