Skip to content

Commit 83ad711

Browse files
committed
Make std::hash::RandomState not be align(8) to work around TLS issues on 95
At least on Windows 95 RTM, the TLS variables are only aligned to a 4 byte boundary. Unclear if this affects other systems, but NT4 is fine.
1 parent 60c9a88 commit 83ad711

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

library/std/src/hash/random.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ impl RandomState {
6565
// iteration order allows a form of DOS attack. To counter that we
6666
// increment one of the seeds on every RandomState creation, giving
6767
// every corresponding HashMap a different iteration order.
68-
thread_local!(static KEYS: Cell<(u64, u64)> = {
68+
thread_local!(static KEYS: Cell<[u8; 16]> = {
6969
Cell::new(hashmap_random_keys())
7070
});
7171

7272
KEYS.with(|keys| {
73-
let (k0, k1) = keys.get();
74-
keys.set((k0.wrapping_add(1), k1));
73+
let [n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15] = keys.get();
74+
let k0 = u64::from_ne_bytes([n0, n1, n2, n3, n4, n5, n6, n7]);
75+
let k1 = u64::from_ne_bytes([n8, n9, n10, n11, n12, n13, n14, n15]);
76+
let [n0, n1, n2, n3, n4, n5, n6, n7] = k0.wrapping_add(1).to_ne_bytes();
77+
keys.set([n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15]);
7578
RandomState { k0, k1 }
7679
})
7780
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ cfg_if::cfg_if! {
8989
all(target_family = "wasm", target_os = "unknown"),
9090
target_os = "xous",
9191
)))]
92-
pub fn hashmap_random_keys() -> (u64, u64) {
92+
pub fn hashmap_random_keys() -> [u8; 16] {
9393
let mut buf = [0; 16];
9494
fill_bytes(&mut buf);
95-
let k1 = u64::from_ne_bytes(buf[..8].try_into().unwrap());
96-
let k2 = u64::from_ne_bytes(buf[8..].try_into().unwrap());
97-
(k1, k2)
95+
buf
9896
}

0 commit comments

Comments
 (0)