Skip to content

Commit

Permalink
Clean up 'ring' test harness
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Jan 6, 2024
1 parent 88b3103 commit 7ecc2a6
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions crates/private/tests/root-task/ring-test-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#![feature(cfg_target_thread_local)]
#![feature(thread_local)]

use core::cell::RefCell;

use rand::rngs::SmallRng;
use rand::{RngCore, SeedableRng};

use sel4_newlib as _;
use sel4_root_task::root_task;
use sel4_test_harness::run_test_main;
Expand All @@ -24,35 +19,45 @@ const HEAP_SIZE: usize = 256 * 1024 * 1024;

#[root_task(heap_size = HEAP_SIZE)]
fn main(_bootinfo: &sel4::BootInfo) -> ! {
seed_insecure_dummy_rng(0);
init();
run_test_main();
sel4::BootInfo::init_thread_tcb().tcb_suspend().unwrap();
unreachable!()
}

#[cfg(not(target_thread_local))]
compile_error!("");
fn init() {
dummy_custom_getrandom::seed_dummy_custom_getrandom(0);
}

mod dummy_custom_getrandom {
use core::cell::RefCell;

#[thread_local]
static RNG: RefCell<Option<SmallRng>> = RefCell::new(None);
use rand::rngs::SmallRng;
use rand::{RngCore, SeedableRng};

pub fn seed_insecure_dummy_rng(seed: u64) {
assert!(RNG.replace(Some(SmallRng::seed_from_u64(seed))).is_none());
}
#[cfg(not(target_thread_local))]
compile_error!("");

#[thread_local]
static RNG: RefCell<Option<SmallRng>> = RefCell::new(None);

pub fn insecure_dummy_rng(buf: &mut [u8]) -> Result<(), getrandom::Error> {
if 1_u32.swap_bytes() == 0 {
panic!()
pub(crate) fn seed_dummy_custom_getrandom(seed: u64) {
assert!(RNG.replace(Some(SmallRng::seed_from_u64(seed))).is_none());
}
RNG.borrow_mut().as_mut().unwrap().fill_bytes(buf);
Ok(())
}

getrandom::register_custom_getrandom!(insecure_dummy_rng);
fn dummy_custom_getrandom(buf: &mut [u8]) -> Result<(), getrandom::Error> {
RNG.borrow_mut().as_mut().unwrap().fill_bytes(buf);
Ok(())
}

// https://github.com/rust-lang/compiler-builtins/pull/563
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
#[no_mangle]
pub extern "C" fn __bswapsi2(u: u32) -> u32 {
u.swap_bytes()
getrandom::register_custom_getrandom!(dummy_custom_getrandom);
}

mod compiler_builtins_supplement {
// https://github.com/rust-lang/compiler-builtins/pull/563
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
#[no_mangle]
pub extern "C" fn __bswapsi2(u: u32) -> u32 {
u.swap_bytes()
}
}

0 comments on commit 7ecc2a6

Please sign in to comment.