Skip to content

Commit

Permalink
Merge pull request #33 from cryspen/jonas/platform-abstraction
Browse files Browse the repository at this point in the history
Testutil platform abstraction
  • Loading branch information
jschneider-bensch authored Nov 11, 2024
2 parents 6e645d5 + 899ad5e commit f2cff84
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 59 deletions.
29 changes: 17 additions & 12 deletions libcrux-iot-testutil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
mod events;
mod logger;

pub mod platform;

pub use events::*;
pub use logger::defmt_logger::DefmtInfoLogger;
pub use logger::vec_logger::VecLogger;
Expand Down Expand Up @@ -65,7 +67,7 @@ where
E: Display,
{
/// Returns whether the test configuration is set to skip the test case.
pub fn is_skipped(&self, config: &TestConfig) -> bool {
pub fn is_skipped<P: platform::Platform>(&self, config: &TestConfig<P>) -> bool {
let all_tests_should_run = config.only_names.is_empty();
let test_is_selected = config.only_names.contains(&self.name);

Expand Down Expand Up @@ -97,15 +99,15 @@ where

/// If the configuration permits, runs the test case as a benchmark. Logs the
/// appropriate messages, and returns the error returned by the test.
pub fn benchmark(&self, logger: &mut L, run_id: u32, state: &S) -> Result<u32, E> {
pub fn benchmark<P: platform::Platform>(&self, config: &TestConfig<P>, logger: &mut L, run_id: u32, state: &S) -> Result<u32, E> {
logger.log_event(TestUtilEvent::Benchmark(BenchmarkEvent::Run {
name: String::from(self.name),
run_id,
}));

let cycles_start = cortex_m::peripheral::DWT::cycle_count();
let cycles_start = config.platform.cycle_count();
let result = core::hint::black_box((self.test_fn)(logger, state));
let cycles_end = cortex_m::peripheral::DWT::cycle_count();
let cycles_end = config.platform.cycle_count();
let cycles = cycles_end - cycles_start;

match &result {
Expand Down Expand Up @@ -146,10 +148,10 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> {
/// returns the first error, wrapped in [`ErrorReport::EarlyAbort`].
/// Otherwise it runs all tests and, if errors were encountered, returns the
/// vector of all errors wrapped in [`ErrorReport::Combined`].
pub fn run(
pub fn run<P: platform::Platform>(
&self,
logger: &mut L,
config: &TestConfig<'a>,
config: &TestConfig<'a, P>,
state: &S,
) -> Result<(), ErrorReport<E>>
where
Expand Down Expand Up @@ -191,15 +193,16 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> {
/// set, it returns the first error, wrapped in [`ErrorReport::EarlyAbort`].
/// Otherwise it runs all tests and, if errors were encountered, returns the
/// vector of all errors wrapped in [`ErrorReport::Combined`].
pub fn benchmark(
pub fn benchmark<P: platform::Platform>(
&self,
logger: &mut L,
config: &TestConfig<'a>,
config: &TestConfig<'a, P>,
state: &S,
) -> Result<Vec<(&'a str, u32, u32)>, ErrorReport<E>>
where
E: Display + Debug,
{
config.platform.init();
logger.log_event(TestUtilEvent::Launch(LaunchEvent {
core_freq: config.core_freq,
name: self.name.to_string(),
Expand All @@ -218,7 +221,7 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> {
}

for run_id in 0..config.benchmark_runs {
let result = test_case.benchmark(logger, run_id, state);
let result = test_case.benchmark(config,logger, run_id, state);

if config.early_abort {
cycle_infos.push((
Expand Down Expand Up @@ -246,7 +249,8 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> {
#[derive(Debug)]
/// The test config contains information on the system and settings that control
/// how the tests are run.
pub struct TestConfig<'a> {
pub struct TestConfig<'a, P: platform::Platform> {
pub platform: P,
/// The core frequency used when benchmarking, so we can reconstruct the actual time passed
/// of running the benchmark.
pub core_freq: u32,
Expand Down Expand Up @@ -283,9 +287,9 @@ impl<E: Display> Display for ErrorReport<E> {
}
}

impl<'a> TestConfig<'a> {
impl<'a, P: platform::Platform> TestConfig<'a, P> {
/// Parses a config string.
pub fn parse_config(core_freq: u32, config: &'a str) -> Result<Self, String> {
pub fn parse_config(platform: P, core_freq: u32, config: &'a str) -> Result<Self, String> {
let mut early_abort = false;
let mut only_names = vec![];
let mut benchmark_runs = 5;
Expand Down Expand Up @@ -320,6 +324,7 @@ impl<'a> TestConfig<'a> {
}

Ok(Self {
platform,
core_freq,
only_names,
early_abort,
Expand Down
25 changes: 25 additions & 0 deletions libcrux-iot-testutil/src/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// A trait capturing platform specific details
pub trait Platform {
/// Initialize cycle counters and other peripherals.
fn init(&self);

/// Return a cycle count, assuming the cycle counter has been initialized.
fn cycle_count(&self) -> u32;
}

/// The Cortex-M specific implementation details.
pub struct CortexM;

impl Platform for CortexM {

fn init(&self) {
use cortex_m::peripheral::Peripherals;
let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

fn cycle_count(&self) -> u32 {
cortex_m::peripheral::DWT::cycle_count()
}
}
1 change: 1 addition & 0 deletions libcrux-iot-testutil/tests/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn run_tests_no_early_abort() {
let test_suite = TestSuite::new("suite for testing", &cases);

let test_config = TestConfig {
platform: platform::CortexM,
core_freq: 25_000_000,
only_names: vec!["test_pass", "test_log", "test_error"],
early_abort: false,
Expand Down
6 changes: 1 addition & 5 deletions libcrux-nrf52810/src/bin/mlkem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;
use libcrux_nrf52810 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -21,14 +20,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
6 changes: 1 addition & 5 deletions libcrux-nrf52832/src/bin/mldsa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;
use libcrux_nrf52832 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -22,14 +21,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
6 changes: 1 addition & 5 deletions libcrux-nrf52832/src/bin/mlkem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;
use libcrux_nrf52832 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -21,14 +20,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
10 changes: 3 additions & 7 deletions libcrux-nrf52840/src/bin/mldsa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;
use libcrux_nrf52840 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -15,21 +14,18 @@ static HEAP: Heap = Heap::empty();
#[cortex_m_rt::entry]
fn main() -> ! {
use libcrux_iot_testutil::*;

// Initialize the allocator BEFORE you use it
{
use core::mem::MaybeUninit;
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
6 changes: 1 addition & 5 deletions libcrux-nrf52840/src/bin/mlkem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;
use libcrux_nrf52840 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -21,14 +20,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
6 changes: 1 addition & 5 deletions libcrux-nrf5340/src/bin/mldsa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;
use libcrux_nrf5340 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -22,14 +21,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
7 changes: 2 additions & 5 deletions libcrux-nrf5340/src/bin/mlkem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_main]
#![no_std]

use cortex_m::peripheral::Peripherals;

use libcrux_nrf5340 as board; // global logger + panicking-behavior + memory layout

extern crate alloc;
Expand All @@ -21,14 +21,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
5 changes: 1 addition & 4 deletions libcrux-nucleo-l4r5zi/src/bin/mldsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}
// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
5 changes: 1 addition & 4 deletions libcrux-nucleo-l4r5zi/src/bin/mlkem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ fn main() -> ! {
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }

let mut peripherals = Peripherals::take().unwrap();
peripherals.DCB.enable_trace();
peripherals.DWT.enable_cycle_counter();
}

// set up the test config
let test_config = TestConfig {
platform: platform::CortexM,
core_freq: board::COREFREQ,
only_names: alloc::vec![],
early_abort: false,
Expand Down
2 changes: 1 addition & 1 deletion libcrux-testbench/src/mldsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn bench_verify<L: EventLogger>(_l: &mut L, state: &MLDSABenchState) -> Result<(
Ok(())
}

pub fn run_benchmarks(test_config: TestConfig) {
pub fn run_benchmarks<P: platform::Platform>(test_config: TestConfig<P>) {
// set up the test suite
let test_cases = [
TestCase::new("bench_keygen", bench_keygen),
Expand Down
2 changes: 1 addition & 1 deletion libcrux-testbench/src/mlkem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn bench_decaps<L: EventLogger>(_l: &mut L, state: &MlKemBenchState) -> Result<(
Ok(())
}

pub fn run_benchmarks(test_config: TestConfig) {
pub fn run_benchmarks<P: platform::Platform>(test_config: TestConfig<P>) {
// set up the test suite
let test_cases = [
TestCase::new("bench_keygen", bench_keygen),
Expand Down

0 comments on commit f2cff84

Please sign in to comment.