Skip to content

Commit 7105073

Browse files
committed
shared hash seed in process
1 parent 42c7c79 commit 7105073

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

common/src/hash.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ impl HashSecret {
5454
pub fn new(seed: u32) -> Self {
5555
let mut buf = [0u8; 16];
5656
lcg_urandom(seed, &mut buf);
57-
let k0 = u64::from_le_bytes(buf[..8].try_into().unwrap());
58-
let k1 = u64::from_le_bytes(buf[8..].try_into().unwrap());
57+
let (left, right) = buf.split_at(8);
58+
let k0 = u64::from_le_bytes(left.try_into().unwrap());
59+
let k1 = u64::from_le_bytes(right.try_into().unwrap());
5960
Self { k0, k1 }
6061
}
6162
}

vm/src/vm/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ pub struct PyGlobalState {
8888
pub codec_registry: CodecsRegistry,
8989
}
9090

91+
pub fn process_hash_secret_seed() -> u32 {
92+
use once_cell::sync::OnceCell;
93+
static SEED: OnceCell<u32> = OnceCell::new();
94+
*SEED.get_or_init(rand::random)
95+
}
96+
9197
impl VirtualMachine {
9298
/// Create a new `VirtualMachine` structure.
9399
fn new(settings: Settings, ctx: PyRc<Context>) -> VirtualMachine {
@@ -120,10 +126,11 @@ impl VirtualMachine {
120126

121127
let module_inits = stdlib::get_module_inits();
122128

123-
let hash_secret = match settings.hash_seed {
124-
Some(seed) => HashSecret::new(seed),
125-
None => rand::random(),
129+
let seed = match settings.hash_seed {
130+
Some(seed) => seed,
131+
None => process_hash_secret_seed(),
126132
};
133+
let hash_secret = HashSecret::new(seed);
127134

128135
let codec_registry = CodecsRegistry::new(&ctx);
129136

0 commit comments

Comments
 (0)