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 Dec 27, 2023
1 parent b532880 commit 033173a
Show file tree
Hide file tree
Showing 5 changed files with 30 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 @@ -32,6 +32,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
13 changes: 12 additions & 1 deletion crates/test/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn tests_runner_impl(args: TokenStream) -> parse::Result<TokenStream> {
}

// Test runner entry point
fn main(args: *mut u8) -> i32 {
pub(super) fn main(args: *mut u8) -> i32 {
// SAFETY: Flipper Zero passes arguments to FAPs as a C string.
let args = unsafe { ::flipperzero_test::__macro_support::Args::parse(args) };
match ::flipperzero_test::__macro_support::run_tests(test_count(), test_list(), args) {
Expand All @@ -140,6 +140,17 @@ fn tests_runner_impl(args: TokenStream) -> parse::Result<TokenStream> {
}
}
}

#[cfg(all(test, miri))]
#[start]
fn main(argc: isize, argv: *const *const u8) -> isize {
let ret = __test_runner::main(::core::ptr::null_mut());

// 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 033173a

Please sign in to comment.