retrofit the seed into an atomicu64 so we can set it elsewhere

This commit is contained in:
bunnie 2021-12-27 02:49:08 +08:00
parent 809dbbdbc2
commit dd14dfc4c6
7 changed files with 14 additions and 11 deletions

View file

@ -65,7 +65,7 @@ pub fn set_send_addr(send_addr: Sender<SocketAddr>) {
#[cfg(not(test))] #[cfg(not(test))]
use core::sync::atomic::{AtomicU64, Ordering}; use core::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(test))] #[cfg(not(test))]
static LOCAL_RNG_STATE: AtomicU64 = AtomicU64::new(xous_kernel::TESTING_RNG_SEED + 2); static LOCAL_RNG_STATE: AtomicU64 = AtomicU64::new(2);
#[cfg(not(test))] #[cfg(not(test))]
fn generate_pid_key() -> [u8; 16] { fn generate_pid_key() -> [u8; 16] {
@ -73,7 +73,7 @@ fn generate_pid_key() -> [u8; 16] {
use rand_chacha::ChaCha8Rng; use rand_chacha::ChaCha8Rng;
let mut process_key = [0u8; 16]; let mut process_key = [0u8; 16];
let mut rng = ChaCha8Rng::seed_from_u64(LOCAL_RNG_STATE.load(Ordering::SeqCst)); 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() { for b in process_key.iter_mut() {
*b = rng.gen(); *b = rng.gen();
} }

View file

@ -17,7 +17,7 @@ static GLOBAL: &StatsAlloc<std::alloc::System> = &INSTRUMENTED_SYSTEM;
const SERVER_SPEC: &str = "127.0.0.1:0"; const SERVER_SPEC: &str = "127.0.0.1:0";
use core::sync::atomic::{AtomicU64, Ordering}; use core::sync::atomic::{AtomicU64, Ordering};
static RNG_LOCAL_STATE: AtomicU64 = AtomicU64::new(xous::TESTING_RNG_SEED + 1); static RNG_LOCAL_STATE: AtomicU64 = AtomicU64::new(1);
fn start_kernel(server_spec: &str) -> JoinHandle<()> { fn start_kernel(server_spec: &str) -> JoinHandle<()> {
assert!( assert!(
@ -32,7 +32,7 @@ fn start_kernel(server_spec: &str) -> JoinHandle<()> {
use rand::prelude::*; use rand::prelude::*;
use rand_chacha::ChaCha8Rng; use rand_chacha::ChaCha8Rng;
let mut pid1_key = [0u8; 16]; let mut pid1_key = [0u8; 16];
let mut rng = ChaCha8Rng::seed_from_u64(RNG_LOCAL_STATE.load(Ordering::SeqCst)); let mut rng = ChaCha8Rng::seed_from_u64(RNG_LOCAL_STATE.load(Ordering::SeqCst) + xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst));
for b in pid1_key.iter_mut() { for b in pid1_key.iter_mut() {
*b = rng.gen(); *b = rng.gen();
} }

View file

@ -437,7 +437,7 @@ fn xmain() -> ! {
const EXTRA_BASIS_PW: &'static str = "some password blah blah"; const EXTRA_BASIS_PW: &'static str = "some password blah blah";
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
log::info!("Seed for this run: {}", xous::TESTING_RNG_SEED); log::info!("Seed for this run: {}", xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst));
pddb_os.test_reset(); pddb_os.test_reset();
log::info!("Creating `basecase1e`"); log::info!("Creating `basecase1e`");

View file

@ -7,10 +7,10 @@ use std::collections::BTreeSet;
const UPPER_BOUND: usize = 9000; const UPPER_BOUND: usize = 9000;
const LOWER_BOUND: usize = 12; // needs to be big enough to compute murmur3 hash + hold checksum const LOWER_BOUND: usize = 12; // needs to be big enough to compute murmur3 hash + hold checksum
static RNG_LOCAL_STATE: AtomicU64 = AtomicU64::new(xous::TESTING_RNG_SEED + 3); 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::<u8>) { fn gen_key(dictname: &str, keynum: usize, lower_size_bound: usize, upper_size_bound: usize) -> (String, Vec::<u8>) {
let mut rng = ChaCha8Rng::seed_from_u64(RNG_LOCAL_STATE.load(Ordering::SeqCst)); 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 // 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) { let keylen = if rng.gen_bool(0.5) {
rng.gen_range(lower_size_bound..VPAGE_SIZE) rng.gen_range(lower_size_bound..VPAGE_SIZE)

View file

@ -328,7 +328,7 @@ impl Drop for Spinor {
use core::sync::atomic::AtomicU64; use core::sync::atomic::AtomicU64;
#[cfg(test)] #[cfg(test)]
static TEST_RNG_STATE: AtomicU64 = AtomicU64::new(xous::TESTING_RNG_SEED + 4); static TEST_RNG_STATE: AtomicU64 = AtomicU64::new(4);
// run with `cargo test -- --nocapture --test-threads=1`: // run with `cargo test -- --nocapture --test-threads=1`:
// --nocapture to see the print output (while debugging) // --nocapture to see the print output (while debugging)
@ -419,7 +419,7 @@ mod tests {
fn flash_fill_rand() { fn flash_fill_rand() {
use rand::prelude::*; use rand::prelude::*;
use rand_chacha::ChaCha8Rng; use rand_chacha::ChaCha8Rng;
let mut rng = ChaCha8Rng::seed_from_u64(TEST_RNG_STATE.load(Ordering::SeqCst));; let mut rng = ChaCha8Rng::seed_from_u64(TEST_RNG_STATE.load(Ordering::SeqCst) + xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst));
for byte in EMU_FLASH.lock().unwrap().iter_mut() { for byte in EMU_FLASH.lock().unwrap().iter_mut() {
*byte = rng.gen::<u8>(); *byte = rng.gen::<u8>();
} }

View file

@ -480,7 +480,7 @@ mod implementation {
impl Trng { impl Trng {
pub fn new(_xns: &xous_names::XousNames) -> Trng { pub fn new(_xns: &xous_names::XousNames) -> Trng {
Trng { Trng {
rng: ChaCha8Rng::seed_from_u64(xous::TESTING_RNG_SEED), rng: ChaCha8Rng::seed_from_u64(xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst)),
seed: 0x1afe_cafe, seed: 0x1afe_cafe,
msgcount: 0, msgcount: 0,
} }

View file

@ -40,7 +40,10 @@ pub const PDDB_LOC: u32 = 0x0100_0000; // PDDB start
pub const PDDB_LEN: u32 = EC_REGION_LOC - PDDB_LOC; pub const PDDB_LEN: u32 = EC_REGION_LOC - PDDB_LOC;
#[cfg(all(any(windows, unix), not(feature = "processes-as-threads")))] #[cfg(all(any(windows, unix), not(feature = "processes-as-threads")))]
pub const TESTING_RNG_SEED: u64 = 8; // 0, 1, 2, 3, 4, 7, 8 "works"; 5 "oom"; 6 "key space oom" use core::sync::atomic::AtomicU64;
#[cfg(all(any(windows, unix), not(feature = "processes-as-threads")))]
pub const TESTING_RNG_SEED: AtomicU64 = AtomicU64::new(0); // 0, 1, 2, 3, 4, 7, 8 "works"; 5 "oom"; 6 "key space oom"
pub mod exceptions; pub mod exceptions;
pub use exceptions::*; pub use exceptions::*;