Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ compiler_builtins = { version = "0.1.16" }
profiler_builtins = { path = "../libprofiler_builtins", optional = true }
unwind = { path = "../libunwind" }
hashbrown = { version = "0.4.0", features = ['rustc-dep-of-std'] }
getrandom = "0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caveat: this won't work on bare WASM since getrandom currently requires a feature flag to enable either stdweb or wasm_bindgen. There was mention of making wasm_bindgen the default; we might want to do this first?

Copy link
Contributor Author

@newpavlov newpavlov Jun 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would prefer if a separate target will be introduced and wasm32-unknown-wunknown will stay assumptions-free regarding executor environment.

Copy link
Contributor Author

@newpavlov newpavlov Jun 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC without enabled stdweb or wasm_bindgen features outside of WASI and Emscripten it will use a dummy impl, so it will work in the same way as it does today by using a constant seed value.


[dependencies.backtrace]
version = "0.3.29"
Expand Down
13 changes: 9 additions & 4 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2445,13 +2445,18 @@ 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];
// we consciously ignore potential errors here
let _ = getrandom::getrandom(&mut buf);
// SAFETY: it's safe to transmute random 16 bytes to two `u64`s
let buf: [u64; 2] = unsafe { core::mem::transmute(buf) };
Cell::new(buf)
});

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: k0, k1: k1 }
})
}
Expand Down
8 changes: 0 additions & 8 deletions src/libstd/sys/cloudabi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,3 @@ pub unsafe fn abort_internal() -> ! {
}

pub use libc::strlen;

pub fn hashmap_random_keys() -> (u64, u64) {
unsafe {
let mut v = mem::uninitialized();
libc::arc4random_buf(&mut v as *mut _ as *mut libc::c_void, mem::size_of_val(&v));
v
}
}
2 changes: 0 additions & 2 deletions src/libstd/sys/redox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::io::ErrorKind;

pub use libc::strlen;
pub use self::rand::hashmap_random_keys;

#[path = "../unix/alloc.rs"]
pub mod alloc;
Expand All @@ -23,7 +22,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;
Expand Down
3 changes: 0 additions & 3 deletions src/libstd/sys/redox/rand.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/libstd/sys/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,6 @@ pub unsafe extern "C" fn __rust_abort() {
abort_internal();
}

pub fn hashmap_random_keys() -> (u64, u64) {
fn rdrand64() -> u64 {
unsafe {
let mut ret: u64 = crate::mem::uninitialized();
for _ in 0..10 {
if crate::arch::x86_64::_rdrand64_step(&mut ret) == 1 {
return ret;
}
}
rtabort!("Failed to obtain random data");
}
}
(rdrand64(), rdrand64())
}

pub use crate::sys_common::{AsInner, FromInner, IntoInner};

pub trait TryIntoInner<Inner>: Sized {
Expand Down
2 changes: 0 additions & 2 deletions src/libstd/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::io::ErrorKind;
#[cfg(all(not(rustdoc), target_os = "l4re"))] pub use crate::os::linux as platform;
#[cfg(all(not(rustdoc), target_os = "hermit"))] pub use crate::os::hermit as platform;

pub use self::rand::hashmap_random_keys;
pub use libc::strlen;

#[macro_use]
Expand Down Expand Up @@ -47,7 +46,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 thread;
Expand Down
176 changes: 0 additions & 176 deletions src/libstd/sys/unix/rand.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/libstd/sys/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ pub unsafe fn abort_internal() -> ! {
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 libc::c_void;
let len = mem::size_of_val(&ret);
cvt_wasi(libc::__wasi_random_get(base, len)).unwrap();
}
return ret
}

#[doc(hidden)]
pub trait IsMinusOne {
fn is_minus_one(&self) -> bool;
Expand Down
9 changes: 0 additions & 9 deletions src/libstd/sys/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ pub unsafe fn abort_internal() -> ! {
ExitSysCall::perform(1)
}

// 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)
}

// Implement a minimal set of system calls to enable basic IO
pub enum SysCallIndex {
Read = 0,
Expand Down
2 changes: 0 additions & 2 deletions src/libstd/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::path::PathBuf;
use crate::time::Duration;

pub use libc::strlen;
pub use self::rand::hashmap_random_keys;

#[macro_use] pub mod compat;

Expand All @@ -31,7 +30,6 @@ pub mod os_str;
pub mod path;
pub mod pipe;
pub mod process;
pub mod rand;
pub mod rwlock;
pub mod stack_overflow;
pub mod thread;
Expand Down
6 changes: 4 additions & 2 deletions src/libstd/sys/windows/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::sync::atomic::AtomicUsize;
use crate::sys::c;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::handle::Handle;
use crate::sys::hashmap_random_keys;

////////////////////////////////////////////////////////////////////////////////
// Anonymous pipes
Expand Down Expand Up @@ -154,7 +153,10 @@ fn random_number() -> usize {
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");
let n = u64::from_ne_bytes(buf) as usize;
N.store(n, SeqCst);
}
}

Expand Down
16 changes: 0 additions & 16 deletions src/libstd/sys/windows/rand.rs

This file was deleted.