Skip to content

Commit

Permalink
flipperzero: Enable Miri to start running the tests
Browse files Browse the repository at this point in the history
It doesn't *successfully* run the tests because Miri doesn't yet support
FFI, but these changes enable the following command to actually start:

    MIRI_NO_STD=1 cargo miri test --target thumbv7em-none-eabihf

Part of #100.
  • Loading branch information
str4d committed Apr 18, 2024
1 parent 6f8f5f5 commit 8c7c40e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
6 changes: 5 additions & 1 deletion crates/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Equivalent to [target.thumbv7em-none-eabihf] but also disabing the runner for Miri.
# Until target_abi is stable, this also permits thumbv7em-none-eabi.
[target.'cfg(all(target_arch = "arm", target_feature = "thumb2", target_feature = "v7", target_feature = "dsp", target_os = "none", not(miri)))']
runner = "python3 ../cargo-runner.py"

[target.thumbv7em-none-eabihf]
linker = "./fap-lld.py"
runner = "python3 ../cargo-runner.py"
rustflags = [
# CPU is Cortex-M4 (STM32WB55)
"-C", "target-cpu=cortex-m4",
Expand Down
3 changes: 2 additions & 1 deletion crates/flipperzero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
//!

#![no_std]
#![cfg_attr(test, no_main)]
#![cfg_attr(all(test, not(miri)), no_main)]
#![cfg_attr(all(test, miri), feature(start))]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(rustdoc::broken_intra_doc_links)]

Expand Down
1 change: 1 addition & 0 deletions crates/rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ macro_rules! entry {
($path:path) => {
// Force the section to `.text` instead of `.text.main`.
// lld seems not to automatically rename `.rel.text.main` properly.
#[cfg(not(miri))]
#[export_name = "main"]
pub unsafe fn __main(args: *mut u8) -> i32 {
// type check the entry function
Expand Down
17 changes: 10 additions & 7 deletions crates/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

// Features that identify thumbv7em-none-eabihf.
// Until target_abi is stable, this also permits thumbv7em-none-eabi.
#[cfg(not(all(
target_arch = "arm",
target_feature = "thumb2",
target_feature = "v7",
target_feature = "dsp",
target_os = "none",
//target_abi = "eabihf",
#[cfg(not(any(
all(
target_arch = "arm",
target_feature = "thumb2",
target_feature = "v7",
target_feature = "dsp",
target_os = "none",
//target_abi = "eabihf",
),
miri
)))]
core::compile_error!("This crate requires `--target thumbv7em-none-eabihf`");

Expand Down
15 changes: 14 additions & 1 deletion crates/test/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,27 @@ fn tests_runner_impl(args: TokenStream) -> parse::Result<TokenStream> {
}

// Test runner entry point
fn main(args: Option<&::core::ffi::CStr>) -> i32 {
pub(super) fn main(args: Option<&::core::ffi::CStr>) -> i32 {
let args = ::flipperzero_test::__macro_support::Args::parse(args);
match ::flipperzero_test::__macro_support::run_tests(test_count(), test_list(), args) {
Ok(()) => 0,
Err(e) => e,
}
}
}

#[cfg(all(test, miri))]
#[start]
fn main(argc: isize, argv: *const *const u8) -> isize {
// TODO: Is there any benefit to Miri in hooking up the binary arguments to
// the test runner?
let ret = __test_runner::main(None);

// Clean up app state.
::flipperzero_rt::__macro_support::__wait_for_thread_completion();

ret.try_into().unwrap_or(isize::MAX)
}
)
.into())
}
Expand Down

0 comments on commit 8c7c40e

Please sign in to comment.