From eb0c1c437768bd6dbdc91fca72b328b98d3e1d7b Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 10 Jan 2022 17:13:00 +0800 Subject: [PATCH] fix some hosted mode brokeness that came with pinning crates the rand api was downgraded to be in line with the ed25519 crate which removed some "modern" conveniences. Small price to pay for less code to review. --- kernel/src/arch/hosted/mod.rs | 5 +++-- kernel/src/arch/hosted/rand.rs | 3 ++- services/pddb/src/tests.rs | 11 +++++++---- services/susres/src/lib.rs | 2 +- services/trng/src/main.rs | 7 +++++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index 4c150f03e..54f30daea 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -67,13 +67,14 @@ static LOCAL_RNG_STATE: AtomicU64 = AtomicU64::new(2); #[cfg(not(test))] fn generate_pid_key() -> [u8; 16] { - use ::rand::prelude::*; use rand_chacha::ChaCha8Rng; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::rand_core::RngCore; let mut process_key = [0u8; 16]; let mut rng = ChaCha8Rng::seed_from_u64(LOCAL_RNG_STATE.load(Ordering::SeqCst) + xous_kernel::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst)); for b in process_key.iter_mut() { - *b = rng.gen(); + *b = rng.next_u32() as u8; } LOCAL_RNG_STATE.store(rng.next_u64(), Ordering::SeqCst); process_key diff --git a/kernel/src/arch/hosted/rand.rs b/kernel/src/arch/hosted/rand.rs index ab2feac1c..67adfa90a 100644 --- a/kernel/src/arch/hosted/rand.rs +++ b/kernel/src/arch/hosted/rand.rs @@ -5,8 +5,9 @@ use core::sync::atomic::Ordering; use crate::arch::hosted::LOCAL_RNG_STATE; pub fn get_u32() -> u32 { - use ::rand::prelude::*; use rand_chacha::ChaCha8Rng; + use rand_chacha::rand_core::RngCore; + use rand_chacha::rand_core::SeedableRng; let mut rng = ChaCha8Rng::seed_from_u64(LOCAL_RNG_STATE.load(Ordering::SeqCst)); let r = rng.next_u32(); diff --git a/services/pddb/src/tests.rs b/services/pddb/src/tests.rs index ad1af22d7..86a58f45b 100644 --- a/services/pddb/src/tests.rs +++ b/services/pddb/src/tests.rs @@ -1,5 +1,6 @@ -use rand::prelude::*; use rand_chacha::ChaCha8Rng; +use rand_chacha::rand_core::RngCore; +use rand_chacha::rand_core::SeedableRng; use crate::*; use core::sync::atomic::{AtomicU64, Ordering}; use std::collections::{BTreeSet, HashSet}; @@ -13,10 +14,12 @@ static RNG_LOCAL_STATE: AtomicU64 = AtomicU64::new(3); fn gen_key(dictname: &str, keynum: usize, lower_size_bound: usize, upper_size_bound: usize) -> (String, Vec::) { let mut rng = ChaCha8Rng::seed_from_u64(RNG_LOCAL_STATE.load(Ordering::SeqCst) + xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst)); // we want roughly half our keys to be in the small bin, and half in the large bin - let keylen = if rng.gen_bool(0.5) { - rng.gen_range(lower_size_bound..VPAGE_SIZE) + let keylen = if rng.next_u32() < (u32::MAX / 2) { + ((rng.next_u64() as usize) % (VPAGE_SIZE - lower_size_bound)) + lower_size_bound + //rng.gen_range(lower_size_bound..VPAGE_SIZE) // older API used to minimize crate count } else { - rng.gen_range(VPAGE_SIZE..upper_size_bound) + ((rng.next_u64() as usize) % (upper_size_bound - VPAGE_SIZE)) + VPAGE_SIZE + //rng.gen_range(VPAGE_SIZE..upper_size_bound) // older API used to minimize crate count }; // record the owning dictionary name & length with the key. This isn't mandatory for a key name, // but it helps the test checking program check things. diff --git a/services/susres/src/lib.rs b/services/susres/src/lib.rs index 9b543ae1c..b0e613739 100644 --- a/services/susres/src/lib.rs +++ b/services/susres/src/lib.rs @@ -47,7 +47,7 @@ impl Susres { // different and have a lot of overhead; it seems like the system goes into a form of deadlock // during boot when all the hosted mode servers try to connect. This isn't an issue on real hardware. #[cfg(not(any(target_os = "none", target_os = "xous")))] - pub fn new(xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { + pub fn new(_ordering: Option, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); Ok(Susres { conn: 0, diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index 4f5bea31d..cb24b1da4 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -467,8 +467,9 @@ mod implementation { // a stub to try to avoid breaking hosted mode for as long as possible. #[cfg(not(any(target_os = "none", target_os = "xous")))] mod implementation { - use rand::prelude::*; use rand_chacha::ChaCha8Rng; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::rand_core::RngCore; use crate::api::{HealthTests, TrngBuf, TrngErrors}; pub struct Trng { @@ -502,7 +503,9 @@ mod implementation { } self.msgcount += 1; let mut data = [0; 1024]; - self.rng.fill(&mut data); + for d in data.iter_mut() { + *d = self.rng.next_u32(); + } TrngBuf { data, len,