From 688ba4b4a4141ed19e307d35bf5d36932102ef47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 18 Dec 2020 12:56:24 +0300 Subject: [PATCH 1/6] Use getrandom for generating HashMap seed --- Cargo.lock | 3 + library/std/Cargo.toml | 5 + library/std/src/collections/hash/map.rs | 20 +- library/std/src/sys/hermit/mod.rs | 5 - library/std/src/sys/sgx/mod.rs | 18 -- library/std/src/sys/unix/mod.rs | 2 - library/std/src/sys/unix/rand.rs | 239 ---------------------- library/std/src/sys/unsupported/common.rs | 4 - library/std/src/sys/vxworks/mod.rs | 2 - library/std/src/sys/vxworks/rand.rs | 36 ---- library/std/src/sys/wasi/mod.rs | 10 - library/std/src/sys/windows/c.rs | 5 - library/std/src/sys/windows/mod.rs | 16 -- library/std/src/sys/windows/pipe.rs | 6 +- library/std/src/sys/windows/rand.rs | 33 --- src/tools/tidy/src/pal.rs | 2 + 16 files changed, 28 insertions(+), 378 deletions(-) delete mode 100644 library/std/src/sys/unix/rand.rs delete mode 100644 library/std/src/sys/vxworks/rand.rs delete mode 100644 library/std/src/sys/windows/rand.rs diff --git a/Cargo.lock b/Cargo.lock index 9fef7fc1e3528..df901203146a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1284,7 +1284,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" dependencies = [ "cfg-if 0.1.10", + "compiler_builtins", "libc", + "rustc-std-workspace-core", "wasi", ] @@ -4692,6 +4694,7 @@ dependencies = [ "core", "dlmalloc", "fortanix-sgx-abi", + "getrandom 0.2.0", "hashbrown", "hermit-abi", "libc", diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml index 71d00b5c08735..f22856f00ff8b 100644 --- a/library/std/Cargo.toml +++ b/library/std/Cargo.toml @@ -21,6 +21,11 @@ compiler_builtins = { version = "0.1.35" } profiler_builtins = { path = "../profiler_builtins", optional = true } unwind = { path = "../unwind" } hashbrown = { version = "0.9.0", default-features = false, features = ['rustc-dep-of-std'] } +# RDRAND feature is needed for SGX targets, but will work on other x86(-64) targets +# unsupported in `getrandom` by default +# FIXME: remove the Hermit part after its support will be added to `getrandom` +[target.'cfg(not(any(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")), all(target_arch = "aarch64", target_os = "hermit")))'.dependencies] +getrandom = { version = "0.2.0", features = ['rustc-dep-of-std', 'rdrand'] } # Dependencies of the `backtrace` crate addr2line = { version = "0.14.0", optional = true, default-features = false } diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 27d90e6613748..5d4b95f4af6ca 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -13,7 +13,6 @@ use crate::fmt::{self, Debug}; use crate::hash::{BuildHasher, Hash, Hasher, SipHasher13}; use crate::iter::{FromIterator, FusedIterator}; use crate::ops::Index; -use crate::sys; /// A hash map implemented with quadratic probing and SIMD lookup. /// @@ -2783,13 +2782,24 @@ impl RandomState { // iteration order allows a form of DOS attack. To counter that we // increment one of the seeds on every RandomState creation, giving // every corresponding HashMap a different iteration order. - thread_local!(static KEYS: Cell<(u64, u64)> = { - Cell::new(sys::hashmap_random_keys()) + thread_local!(static KEYS: Cell<[u64; 2]> = { + let mut buf = [0u8; 16]; + // Use a constant seed on `wasm32-unknown-unknown` and Hermit targets. + // FIXME: remove the Hermit part after its support will be added to `getrandom` + // TODO: replace the WASM part with `cfg(target = "..")`: + // https://github.com/rust-lang/rust/issues/63217 + #[cfg(not(any( + all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"), + all(target_arch = "aarch64", target_os = "hermit"), + )))] + getrandom::getrandom(&mut buf).expect("failed to get system entropy"); + let n = u128::from_ne_bytes(buf); + Cell::new([n as u64, (n >> 64) as u64]) }); KEYS.with(|keys| { - let (k0, k1) = keys.get(); - keys.set((k0.wrapping_add(1), k1)); + let [k0, k1] = keys.get(); + keys.set([k0.wrapping_add(1), k1]); RandomState { k0, k1 } }) } diff --git a/library/std/src/sys/hermit/mod.rs b/library/std/src/sys/hermit/mod.rs index f185635b7a0a6..3436976b1d604 100644 --- a/library/std/src/sys/hermit/mod.rs +++ b/library/std/src/sys/hermit/mod.rs @@ -83,11 +83,6 @@ pub fn abort_internal() -> ! { } } -// FIXME: just a workaround to test the system -pub fn hashmap_random_keys() -> (u64, u64) { - (1, 2) -} - // This function is needed by the panic runtime. The symbol is named in // pre-link args for the target specification, so keep that in sync. #[cfg(not(test))] diff --git a/library/std/src/sys/sgx/mod.rs b/library/std/src/sys/sgx/mod.rs index b10bed621dbad..6eea53c2e9a20 100644 --- a/library/std/src/sys/sgx/mod.rs +++ b/library/std/src/sys/sgx/mod.rs @@ -142,24 +142,6 @@ pub extern "C" fn __rust_abort() { abort_internal(); } -pub mod rand { - pub fn rdrand64() -> u64 { - unsafe { - let mut ret: u64 = 0; - for _ in 0..10 { - if crate::arch::x86_64::_rdrand64_step(&mut ret) == 1 { - return ret; - } - } - rtabort!("Failed to obtain random data"); - } - } -} - -pub fn hashmap_random_keys() -> (u64, u64) { - (self::rand::rdrand64(), self::rand::rdrand64()) -} - pub use crate::sys_common::{AsInner, FromInner, IntoInner}; pub trait TryIntoInner: Sized { diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index f8a5ee8996911..a15abe42d881f 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -34,7 +34,6 @@ pub use crate::os::redox as platform; #[cfg(all(not(doc), target_os = "solaris"))] pub use crate::os::solaris as platform; -pub use self::rand::hashmap_random_keys; pub use libc::strlen; #[macro_use] @@ -65,7 +64,6 @@ pub mod os; pub mod path; pub mod pipe; pub mod process; -pub mod rand; pub mod rwlock; pub mod stack_overflow; pub mod stdio; diff --git a/library/std/src/sys/unix/rand.rs b/library/std/src/sys/unix/rand.rs deleted file mode 100644 index 38ddb41700c4b..0000000000000 --- a/library/std/src/sys/unix/rand.rs +++ /dev/null @@ -1,239 +0,0 @@ -use crate::mem; -use crate::slice; - -pub fn hashmap_random_keys() -> (u64, u64) { - let mut v = (0, 0); - unsafe { - let view = slice::from_raw_parts_mut(&mut v as *mut _ as *mut u8, mem::size_of_val(&v)); - imp::fill_bytes(view); - } - v -} - -#[cfg(all( - unix, - not(target_os = "macos"), - not(target_os = "ios"), - not(target_os = "openbsd"), - not(target_os = "freebsd"), - not(target_os = "netbsd"), - not(target_os = "fuchsia"), - not(target_os = "redox") -))] -mod imp { - use crate::fs::File; - use crate::io::Read; - - #[cfg(any(target_os = "linux", target_os = "android"))] - fn getrandom(buf: &mut [u8]) -> libc::ssize_t { - // A weak symbol allows interposition, e.g. for perf measurements that want to - // disable randomness for consistency. Otherwise, we'll try a raw syscall. - // (`getrandom` was added in glibc 2.25, musl 1.1.20, android API level 28) - syscall! { - fn getrandom( - buffer: *mut libc::c_void, - length: libc::size_t, - flags: libc::c_uint - ) -> libc::ssize_t - } - - unsafe { getrandom(buf.as_mut_ptr().cast(), buf.len(), libc::GRND_NONBLOCK) } - } - - #[cfg(not(any(target_os = "linux", target_os = "android")))] - fn getrandom_fill_bytes(_buf: &mut [u8]) -> bool { - false - } - - #[cfg(any(target_os = "linux", target_os = "android"))] - fn getrandom_fill_bytes(v: &mut [u8]) -> bool { - use crate::sync::atomic::{AtomicBool, Ordering}; - use crate::sys::os::errno; - - static GETRANDOM_UNAVAILABLE: AtomicBool = AtomicBool::new(false); - if GETRANDOM_UNAVAILABLE.load(Ordering::Relaxed) { - return false; - } - - let mut read = 0; - while read < v.len() { - let result = getrandom(&mut v[read..]); - if result == -1 { - let err = errno() as libc::c_int; - if err == libc::EINTR { - continue; - } else if err == libc::ENOSYS || err == libc::EPERM { - // Fall back to reading /dev/urandom if `getrandom` is not - // supported on the current kernel. - // - // Also fall back in case it is disabled by something like - // seccomp or inside of virtual machines. - GETRANDOM_UNAVAILABLE.store(true, Ordering::Relaxed); - return false; - } else if err == libc::EAGAIN { - return false; - } else { - panic!("unexpected getrandom error: {}", err); - } - } else { - read += result as usize; - } - } - true - } - - pub fn fill_bytes(v: &mut [u8]) { - // getrandom_fill_bytes here can fail if getrandom() returns EAGAIN, - // meaning it would have blocked because the non-blocking pool (urandom) - // has not initialized in the kernel yet due to a lack of entropy. The - // fallback we do here is to avoid blocking applications which could - // depend on this call without ever knowing they do and don't have a - // work around. The PRNG of /dev/urandom will still be used but over a - // possibly predictable entropy pool. - if getrandom_fill_bytes(v) { - return; - } - - // getrandom failed because it is permanently or temporarily (because - // of missing entropy) unavailable. Open /dev/urandom, read from it, - // and close it again. - let mut file = File::open("/dev/urandom").expect("failed to open /dev/urandom"); - file.read_exact(v).expect("failed to read /dev/urandom") - } -} - -#[cfg(target_os = "macos")] -mod imp { - use crate::fs::File; - use crate::io::Read; - use crate::sys::os::errno; - use libc::{c_int, c_void, size_t}; - - fn getentropy_fill_bytes(v: &mut [u8]) -> bool { - weak!(fn getentropy(*mut c_void, size_t) -> c_int); - - getentropy - .get() - .map(|f| { - // getentropy(2) permits a maximum buffer size of 256 bytes - for s in v.chunks_mut(256) { - let ret = unsafe { f(s.as_mut_ptr() as *mut c_void, s.len()) }; - if ret == -1 { - panic!("unexpected getentropy error: {}", errno()); - } - } - true - }) - .unwrap_or(false) - } - - pub fn fill_bytes(v: &mut [u8]) { - if getentropy_fill_bytes(v) { - return; - } - - // for older macos which doesn't support getentropy - let mut file = File::open("/dev/urandom").expect("failed to open /dev/urandom"); - file.read_exact(v).expect("failed to read /dev/urandom") - } -} - -#[cfg(target_os = "openbsd")] -mod imp { - use crate::sys::os::errno; - - pub fn fill_bytes(v: &mut [u8]) { - // getentropy(2) permits a maximum buffer size of 256 bytes - for s in v.chunks_mut(256) { - let ret = unsafe { libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len()) }; - if ret == -1 { - panic!("unexpected getentropy error: {}", errno()); - } - } - } -} - -// On iOS and MacOS `SecRandomCopyBytes` calls `CCRandomCopyBytes` with -// `kCCRandomDefault`. `CCRandomCopyBytes` manages a CSPRNG which is seeded -// from `/dev/random` and which runs on its own thread accessed via GCD. -// This seems needlessly heavyweight for the purposes of generating two u64s -// once per thread in `hashmap_random_keys`. Therefore `SecRandomCopyBytes` is -// only used on iOS where direct access to `/dev/urandom` is blocked by the -// sandbox. -#[cfg(target_os = "ios")] -mod imp { - use crate::io; - use crate::ptr; - use libc::{c_int, size_t}; - - enum SecRandom {} - - #[allow(non_upper_case_globals)] - const kSecRandomDefault: *const SecRandom = ptr::null(); - - extern "C" { - fn SecRandomCopyBytes(rnd: *const SecRandom, count: size_t, bytes: *mut u8) -> c_int; - } - - pub fn fill_bytes(v: &mut [u8]) { - let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, v.len(), v.as_mut_ptr()) }; - if ret == -1 { - panic!("couldn't generate random bytes: {}", io::Error::last_os_error()); - } - } -} - -#[cfg(any(target_os = "freebsd", target_os = "netbsd"))] -mod imp { - use crate::ptr; - - pub fn fill_bytes(v: &mut [u8]) { - let mib = [libc::CTL_KERN, libc::KERN_ARND]; - // kern.arandom permits a maximum buffer size of 256 bytes - for s in v.chunks_mut(256) { - let mut s_len = s.len(); - let ret = unsafe { - libc::sysctl( - mib.as_ptr(), - mib.len() as libc::c_uint, - s.as_mut_ptr() as *mut _, - &mut s_len, - ptr::null(), - 0, - ) - }; - if ret == -1 || s_len != s.len() { - panic!( - "kern.arandom sysctl failed! (returned {}, s.len() {}, oldlenp {})", - ret, - s.len(), - s_len - ); - } - } - } -} - -#[cfg(target_os = "fuchsia")] -mod imp { - #[link(name = "zircon")] - extern "C" { - fn zx_cprng_draw(buffer: *mut u8, len: usize); - } - - pub fn fill_bytes(v: &mut [u8]) { - unsafe { zx_cprng_draw(v.as_mut_ptr(), v.len()) } - } -} - -#[cfg(target_os = "redox")] -mod imp { - use crate::fs::File; - use crate::io::Read; - - pub fn fill_bytes(v: &mut [u8]) { - // Open rand:, read from it, and close it again. - let mut file = File::open("rand:").expect("failed to open rand:"); - file.read_exact(v).expect("failed to read rand:") - } -} diff --git a/library/std/src/sys/unsupported/common.rs b/library/std/src/sys/unsupported/common.rs index 2cdd9c4d19e6e..8f9fe233ea6fc 100644 --- a/library/std/src/sys/unsupported/common.rs +++ b/library/std/src/sys/unsupported/common.rs @@ -29,10 +29,6 @@ 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)] diff --git a/library/std/src/sys/vxworks/mod.rs b/library/std/src/sys/vxworks/mod.rs index c20edaa1a4778..a4f28f52bc4e0 100644 --- a/library/std/src/sys/vxworks/mod.rs +++ b/library/std/src/sys/vxworks/mod.rs @@ -3,7 +3,6 @@ use crate::io::ErrorKind; -pub use self::rand::hashmap_random_keys; pub use crate::os::vxworks as platform; pub use libc::strlen; @@ -41,7 +40,6 @@ pub mod path; #[path = "../unix/pipe.rs"] pub mod pipe; pub mod process; -pub mod rand; #[path = "../unix/rwlock.rs"] pub mod rwlock; #[path = "../unix/stack_overflow.rs"] diff --git a/library/std/src/sys/vxworks/rand.rs b/library/std/src/sys/vxworks/rand.rs deleted file mode 100644 index 3a1ff5fd3b9c6..0000000000000 --- a/library/std/src/sys/vxworks/rand.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::mem; -use crate::slice; - -pub fn hashmap_random_keys() -> (u64, u64) { - let mut v = (0, 0); - unsafe { - let view = slice::from_raw_parts_mut(&mut v as *mut _ as *mut u8, mem::size_of_val(&v)); - imp::fill_bytes(view); - } - return v; -} - -mod imp { - use crate::io; - use core::sync::atomic::{AtomicBool, Ordering::Relaxed}; - - pub fn fill_bytes(v: &mut [u8]) { - static RNG_INIT: AtomicBool = AtomicBool::new(false); - while !RNG_INIT.load(Relaxed) { - let ret = unsafe { libc::randSecure() }; - if ret < 0 { - panic!("couldn't generate random bytes: {}", io::Error::last_os_error()); - } else if ret > 0 { - RNG_INIT.store(true, Relaxed); - break; - } - unsafe { libc::usleep(10) }; - } - let ret = unsafe { - libc::randABytes(v.as_mut_ptr() as *mut libc::c_uchar, v.len() as libc::c_int) - }; - if ret < 0 { - panic!("couldn't generate random bytes: {}", io::Error::last_os_error()); - } - } -} diff --git a/library/std/src/sys/wasi/mod.rs b/library/std/src/sys/wasi/mod.rs index a0a37ef8316a8..0debf3de17f48 100644 --- a/library/std/src/sys/wasi/mod.rs +++ b/library/std/src/sys/wasi/mod.rs @@ -86,16 +86,6 @@ pub fn abort_internal() -> ! { unsafe { libc::abort() } } -pub fn hashmap_random_keys() -> (u64, u64) { - let mut ret = (0u64, 0u64); - unsafe { - let base = &mut ret as *mut (u64, u64) as *mut u8; - let len = mem::size_of_val(&ret); - wasi::random_get(base, len).expect("random_get failure"); - } - return ret; -} - fn err2io(err: wasi::Error) -> std_io::Error { std_io::Error::from_raw_os_error(err.raw_error().into()) } diff --git a/library/std/src/sys/windows/c.rs b/library/std/src/sys/windows/c.rs index 2b1bc92dc84ae..ce851e177e371 100644 --- a/library/std/src/sys/windows/c.rs +++ b/library/std/src/sys/windows/c.rs @@ -689,9 +689,6 @@ if #[cfg(not(target_vendor = "uwp"))] { pub const TOKEN_READ: DWORD = 0x20008; extern "system" { - #[link_name = "SystemFunction036"] - pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN; - pub fn ReadConsoleW(hConsoleInput: HANDLE, lpBuffer: LPVOID, nNumberOfCharsToRead: DWORD, @@ -749,8 +746,6 @@ if #[cfg(target_vendor = "uwp")] { fileInfoClass: FILE_INFO_BY_HANDLE_CLASS, lpFileInformation: LPVOID, dwBufferSize: DWORD) -> BOOL; - pub fn BCryptGenRandom(hAlgorithm: LPVOID, pBuffer: *mut u8, - cbBuffer: ULONG, dwFlags: ULONG) -> LONG; } } } diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index fcbff59dec007..1ee0633db9f32 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -6,7 +6,6 @@ use crate::os::windows::ffi::{OsStrExt, OsStringExt}; use crate::path::PathBuf; use crate::time::Duration; -pub use self::rand::hashmap_random_keys; pub use libc::strlen; #[macro_use] @@ -30,7 +29,6 @@ pub mod os_str; pub mod path; pub mod pipe; pub mod process; -pub mod rand; pub mod rwlock; pub mod thread; pub mod thread_local_dtor; @@ -271,17 +269,3 @@ pub fn abort_internal() -> ! { } crate::intrinsics::abort(); } - -cfg_if::cfg_if! { - if #[cfg(target_vendor = "uwp")] { - #[link(name = "ws2_32")] - // For BCryptGenRandom - #[link(name = "bcrypt")] - extern "C" {} - } else { - #[link(name = "advapi32")] - #[link(name = "ws2_32")] - #[link(name = "userenv")] - extern "C" {} - } -} diff --git a/library/std/src/sys/windows/pipe.rs b/library/std/src/sys/windows/pipe.rs index 104a8db46596e..14cd15bc8bcfe 100644 --- a/library/std/src/sys/windows/pipe.rs +++ b/library/std/src/sys/windows/pipe.rs @@ -11,7 +11,6 @@ use crate::sync::atomic::Ordering::SeqCst; use crate::sys::c; use crate::sys::fs::{File, OpenOptions}; use crate::sys::handle::Handle; -use crate::sys::hashmap_random_keys; //////////////////////////////////////////////////////////////////////////////// // Anonymous pipes @@ -161,8 +160,9 @@ fn random_number() -> usize { if N.load(SeqCst) != 0 { return N.fetch_add(1, SeqCst); } - - N.store(hashmap_random_keys().0 as usize, SeqCst); + let mut buf = [0u8; 8]; + getrandom::getrandom(&mut buf).expect("OS RNG failure"); + N.store(u64::from_ne_bytes(buf) as usize, SeqCst); } } diff --git a/library/std/src/sys/windows/rand.rs b/library/std/src/sys/windows/rand.rs deleted file mode 100644 index 87ea416bf675a..0000000000000 --- a/library/std/src/sys/windows/rand.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::io; -use crate::mem; -use crate::sys::c; - -#[cfg(not(target_vendor = "uwp"))] -pub fn hashmap_random_keys() -> (u64, u64) { - let mut v = (0, 0); - let ret = - unsafe { c::RtlGenRandom(&mut v as *mut _ as *mut u8, mem::size_of_val(&v) as c::ULONG) }; - if ret == 0 { - panic!("couldn't generate random bytes: {}", io::Error::last_os_error()); - } - v -} - -#[cfg(target_vendor = "uwp")] -pub fn hashmap_random_keys() -> (u64, u64) { - use crate::ptr; - - let mut v = (0, 0); - let ret = unsafe { - c::BCryptGenRandom( - ptr::null_mut(), - &mut v as *mut _ as *mut u8, - mem::size_of_val(&v) as c::ULONG, - c::BCRYPT_USE_SYSTEM_PREFERRED_RNG, - ) - }; - if ret != 0 { - panic!("couldn't generate random bytes: {}", io::Error::last_os_error()); - } - return v; -} diff --git a/src/tools/tidy/src/pal.rs b/src/tools/tidy/src/pal.rs index 1dba6b73b9361..66346d4822526 100644 --- a/src/tools/tidy/src/pal.rs +++ b/src/tools/tidy/src/pal.rs @@ -86,6 +86,8 @@ const EXCEPTION_PATHS: &[&str] = &[ "src/librustdoc", "src/librustc_ast", "src/bootstrap", + // constant hash map seed for `wasm32-unknown-unknown` + "src/libstd/collections/hash/map.rs", ]; pub fn check(path: &Path, bad: &mut bool) { From 7d0be778bd83781e39686f2daafd8911dd1e5da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 18 Dec 2020 13:03:07 +0300 Subject: [PATCH 2/6] fix cfg --- library/std/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml index f22856f00ff8b..7b54ab4cfd253 100644 --- a/library/std/Cargo.toml +++ b/library/std/Cargo.toml @@ -24,7 +24,7 @@ hashbrown = { version = "0.9.0", default-features = false, features = ['rustc-de # RDRAND feature is needed for SGX targets, but will work on other x86(-64) targets # unsupported in `getrandom` by default # FIXME: remove the Hermit part after its support will be added to `getrandom` -[target.'cfg(not(any(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")), all(target_arch = "aarch64", target_os = "hermit")))'.dependencies] +[target.'cfg(not(any(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"), all(target_arch = "aarch64", target_os = "hermit"))))'.dependencies] getrandom = { version = "0.2.0", features = ['rustc-dep-of-std', 'rdrand'] } # Dependencies of the `backtrace` crate From 636bbf4cfb3b53c4f8d3735d11813878bd65cfff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 18 Dec 2020 13:23:40 +0300 Subject: [PATCH 3/6] replace TODO with FIXME --- library/std/src/collections/hash/map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 5d4b95f4af6ca..efa94def0f5a5 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -2786,7 +2786,7 @@ impl RandomState { let mut buf = [0u8; 16]; // Use a constant seed on `wasm32-unknown-unknown` and Hermit targets. // FIXME: remove the Hermit part after its support will be added to `getrandom` - // TODO: replace the WASM part with `cfg(target = "..")`: + // FIXME: replace the WASM part with `cfg(target = "..")`: // https://github.com/rust-lang/rust/issues/63217 #[cfg(not(any( all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"), From 0117047fc4f682416d1aded1f307626aa5c6016b Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Fri, 8 Jan 2021 20:52:24 +0300 Subject: [PATCH 4/6] Change u64 to usize as per @josephlr's proposal --- library/std/src/sys/windows/pipe.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/windows/pipe.rs b/library/std/src/sys/windows/pipe.rs index 14cd15bc8bcfe..520862e39ade9 100644 --- a/library/std/src/sys/windows/pipe.rs +++ b/library/std/src/sys/windows/pipe.rs @@ -160,9 +160,9 @@ fn random_number() -> usize { if N.load(SeqCst) != 0 { return N.fetch_add(1, SeqCst); } - let mut buf = [0u8; 8]; + let mut buf = [0; mem::size_of::()]; getrandom::getrandom(&mut buf).expect("OS RNG failure"); - N.store(u64::from_ne_bytes(buf) as usize, SeqCst); + N.store(usize::from_ne_bytes(buf), SeqCst); } } From 528fb0d7b02195ae77d45a9487d6b9a602952e3b Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Fri, 8 Jan 2021 20:55:26 +0300 Subject: [PATCH 5/6] fix path --- src/tools/tidy/src/pal.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tools/tidy/src/pal.rs b/src/tools/tidy/src/pal.rs index 66346d4822526..dcd357cb945fa 100644 --- a/src/tools/tidy/src/pal.rs +++ b/src/tools/tidy/src/pal.rs @@ -53,6 +53,7 @@ const EXCEPTION_PATHS: &[&str] = &[ "library/std/src/path.rs", "library/std/src/f32.rs", "library/std/src/f64.rs", + "library/std/src/collections/hash/map.rs", // constant hash map seed for `wasm32-unknown-unknown` // Integration test for platform-specific run-time feature detection: "library/std/tests/run-time-detect.rs", "library/std/src/net/test.rs", @@ -86,8 +87,6 @@ const EXCEPTION_PATHS: &[&str] = &[ "src/librustdoc", "src/librustc_ast", "src/bootstrap", - // constant hash map seed for `wasm32-unknown-unknown` - "src/libstd/collections/hash/map.rs", ]; pub fn check(path: &Path, bad: &mut bool) { From 582566d513ee4e1e33c9f6f98d913e6a025b6283 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Sat, 16 Jan 2021 22:18:22 +0000 Subject: [PATCH 6/6] link RFC 2991 instead of the feature request issue --- library/std/src/collections/hash/map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index efa94def0f5a5..38b01af8bd526 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -2787,7 +2787,7 @@ impl RandomState { // Use a constant seed on `wasm32-unknown-unknown` and Hermit targets. // FIXME: remove the Hermit part after its support will be added to `getrandom` // FIXME: replace the WASM part with `cfg(target = "..")`: - // https://github.com/rust-lang/rust/issues/63217 + // https://github.com/rust-lang/rfcs/pull/2991 #[cfg(not(any( all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"), all(target_arch = "aarch64", target_os = "hermit"),