From 7105073c91f2a4f876119b4d716c72eabbd0934f Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Tue, 17 May 2022 05:44:34 +0900 Subject: [PATCH] shared hash seed in process --- common/src/hash.rs | 5 +++-- vm/src/vm/mod.rs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index c3f13e74d..ba3d6048a 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -54,8 +54,9 @@ impl HashSecret { pub fn new(seed: u32) -> Self { let mut buf = [0u8; 16]; lcg_urandom(seed, &mut buf); - let k0 = u64::from_le_bytes(buf[..8].try_into().unwrap()); - let k1 = u64::from_le_bytes(buf[8..].try_into().unwrap()); + let (left, right) = buf.split_at(8); + let k0 = u64::from_le_bytes(left.try_into().unwrap()); + let k1 = u64::from_le_bytes(right.try_into().unwrap()); Self { k0, k1 } } } diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index c3e0723ce..f96ad7f44 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -88,6 +88,12 @@ pub struct PyGlobalState { pub codec_registry: CodecsRegistry, } +pub fn process_hash_secret_seed() -> u32 { + use once_cell::sync::OnceCell; + static SEED: OnceCell = OnceCell::new(); + *SEED.get_or_init(rand::random) +} + impl VirtualMachine { /// Create a new `VirtualMachine` structure. fn new(settings: Settings, ctx: PyRc) -> VirtualMachine { @@ -120,10 +126,11 @@ impl VirtualMachine { let module_inits = stdlib::get_module_inits(); - let hash_secret = match settings.hash_seed { - Some(seed) => HashSecret::new(seed), - None => rand::random(), + let seed = match settings.hash_seed { + Some(seed) => seed, + None => process_hash_secret_seed(), }; + let hash_secret = HashSecret::new(seed); let codec_registry = CodecsRegistry::new(&ctx);