Skip to content

Commit 688ba4b

Browse files
committed
Use getrandom for generating HashMap seed
1 parent 8d006c0 commit 688ba4b

File tree

16 files changed

+28
-378
lines changed

16 files changed

+28
-378
lines changed

Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
12841284
checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4"
12851285
dependencies = [
12861286
"cfg-if 0.1.10",
1287+
"compiler_builtins",
12871288
"libc",
1289+
"rustc-std-workspace-core",
12881290
"wasi",
12891291
]
12901292

@@ -4692,6 +4694,7 @@ dependencies = [
46924694
"core",
46934695
"dlmalloc",
46944696
"fortanix-sgx-abi",
4697+
"getrandom 0.2.0",
46954698
"hashbrown",
46964699
"hermit-abi",
46974700
"libc",

library/std/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ compiler_builtins = { version = "0.1.35" }
2121
profiler_builtins = { path = "../profiler_builtins", optional = true }
2222
unwind = { path = "../unwind" }
2323
hashbrown = { version = "0.9.0", default-features = false, features = ['rustc-dep-of-std'] }
24+
# RDRAND feature is needed for SGX targets, but will work on other x86(-64) targets
25+
# unsupported in `getrandom` by default
26+
# FIXME: remove the Hermit part after its support will be added to `getrandom`
27+
[target.'cfg(not(any(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")), all(target_arch = "aarch64", target_os = "hermit")))'.dependencies]
28+
getrandom = { version = "0.2.0", features = ['rustc-dep-of-std', 'rdrand'] }
2429

2530
# Dependencies of the `backtrace` crate
2631
addr2line = { version = "0.14.0", optional = true, default-features = false }

library/std/src/collections/hash/map.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::fmt::{self, Debug};
1313
use crate::hash::{BuildHasher, Hash, Hasher, SipHasher13};
1414
use crate::iter::{FromIterator, FusedIterator};
1515
use crate::ops::Index;
16-
use crate::sys;
1716

1817
/// A hash map implemented with quadratic probing and SIMD lookup.
1918
///
@@ -2783,13 +2782,24 @@ impl RandomState {
27832782
// iteration order allows a form of DOS attack. To counter that we
27842783
// increment one of the seeds on every RandomState creation, giving
27852784
// every corresponding HashMap a different iteration order.
2786-
thread_local!(static KEYS: Cell<(u64, u64)> = {
2787-
Cell::new(sys::hashmap_random_keys())
2785+
thread_local!(static KEYS: Cell<[u64; 2]> = {
2786+
let mut buf = [0u8; 16];
2787+
// Use a constant seed on `wasm32-unknown-unknown` and Hermit targets.
2788+
// FIXME: remove the Hermit part after its support will be added to `getrandom`
2789+
// TODO: replace the WASM part with `cfg(target = "..")`:
2790+
// https://github.com/rust-lang/rust/issues/63217
2791+
#[cfg(not(any(
2792+
all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"),
2793+
all(target_arch = "aarch64", target_os = "hermit"),
2794+
)))]
2795+
getrandom::getrandom(&mut buf).expect("failed to get system entropy");
2796+
let n = u128::from_ne_bytes(buf);
2797+
Cell::new([n as u64, (n >> 64) as u64])
27882798
});
27892799

27902800
KEYS.with(|keys| {
2791-
let (k0, k1) = keys.get();
2792-
keys.set((k0.wrapping_add(1), k1));
2801+
let [k0, k1] = keys.get();
2802+
keys.set([k0.wrapping_add(1), k1]);
27932803
RandomState { k0, k1 }
27942804
})
27952805
}

library/std/src/sys/hermit/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ pub fn abort_internal() -> ! {
8383
}
8484
}
8585

86-
// FIXME: just a workaround to test the system
87-
pub fn hashmap_random_keys() -> (u64, u64) {
88-
(1, 2)
89-
}
90-
9186
// This function is needed by the panic runtime. The symbol is named in
9287
// pre-link args for the target specification, so keep that in sync.
9388
#[cfg(not(test))]

library/std/src/sys/sgx/mod.rs

-18
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,6 @@ pub extern "C" fn __rust_abort() {
142142
abort_internal();
143143
}
144144

145-
pub mod rand {
146-
pub fn rdrand64() -> u64 {
147-
unsafe {
148-
let mut ret: u64 = 0;
149-
for _ in 0..10 {
150-
if crate::arch::x86_64::_rdrand64_step(&mut ret) == 1 {
151-
return ret;
152-
}
153-
}
154-
rtabort!("Failed to obtain random data");
155-
}
156-
}
157-
}
158-
159-
pub fn hashmap_random_keys() -> (u64, u64) {
160-
(self::rand::rdrand64(), self::rand::rdrand64())
161-
}
162-
163145
pub use crate::sys_common::{AsInner, FromInner, IntoInner};
164146

165147
pub trait TryIntoInner<Inner>: Sized {

library/std/src/sys/unix/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub use crate::os::redox as platform;
3434
#[cfg(all(not(doc), target_os = "solaris"))]
3535
pub use crate::os::solaris as platform;
3636

37-
pub use self::rand::hashmap_random_keys;
3837
pub use libc::strlen;
3938

4039
#[macro_use]
@@ -65,7 +64,6 @@ pub mod os;
6564
pub mod path;
6665
pub mod pipe;
6766
pub mod process;
68-
pub mod rand;
6967
pub mod rwlock;
7068
pub mod stack_overflow;
7169
pub mod stdio;

library/std/src/sys/unix/rand.rs

-239
This file was deleted.

library/std/src/sys/unsupported/common.rs

-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ pub fn abort_internal() -> ! {
2929
core::intrinsics::abort();
3030
}
3131

32-
pub fn hashmap_random_keys() -> (u64, u64) {
33-
(1, 2)
34-
}
35-
3632
// This enum is used as the storage for a bunch of types which can't actually
3733
// exist.
3834
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]

library/std/src/sys/vxworks/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use crate::io::ErrorKind;
55

6-
pub use self::rand::hashmap_random_keys;
76
pub use crate::os::vxworks as platform;
87
pub use libc::strlen;
98

@@ -41,7 +40,6 @@ pub mod path;
4140
#[path = "../unix/pipe.rs"]
4241
pub mod pipe;
4342
pub mod process;
44-
pub mod rand;
4543
#[path = "../unix/rwlock.rs"]
4644
pub mod rwlock;
4745
#[path = "../unix/stack_overflow.rs"]

0 commit comments

Comments
 (0)