Skip to content

Commit 686b027

Browse files
authored
Merge pull request #123 from flipperzero-rs/basic-miri-support
flipperzero: Enable Miri to start running the tests
2 parents a2d5a4b + 92f86c3 commit 686b027

File tree

9 files changed

+42
-16
lines changed

9 files changed

+42
-16
lines changed

crates/.cargo/config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# Equivalent to [target.thumbv7em-none-eabihf] but also disabing the runner for Miri.
2+
[target.'cfg(all(target_arch = "arm", target_feature = "thumb2", target_feature = "v7", target_feature = "dsp", target_os = "none", target_abi = "eabihf", not(miri)))']
3+
runner = "python3 ../cargo-runner.py"
4+
15
[target.thumbv7em-none-eabihf]
26
linker = "./fap-lld.py"
3-
runner = "python3 ../cargo-runner.py"
47
rustflags = [
58
# CPU is Cortex-M4 (STM32WB55)
69
"-C", "target-cpu=cortex-m4",

crates/flipperzero/src/furi/thread.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ pub fn sleep(duration: core::time::Duration) {
206206
/// A unique identifier for a running thread.
207207
#[cfg(feature = "alloc")]
208208
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
209+
#[allow(dead_code)]
209210
pub struct ThreadId(sys::FuriThreadId);
210211

211212
/// A handle to a thread.

crates/flipperzero/src/gpio/i2c.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! I2C interface for the Flipper Zero.
22
33
use core::fmt;
4+
use core::ptr::addr_of_mut;
45

56
use flipperzero_sys as sys;
67

@@ -99,9 +100,12 @@ impl Bus {
99100
///
100101
/// Blocks indefinitely until the bus is available.
101102
pub fn acquire(self) -> BusHandle {
103+
// SAFETY: We block until we acquire a handle to the selected bus, so nothing else
104+
// will be using it while we have a raw pointer to it. We don't convert this to a
105+
// `&'static mut` reference because this will be disallowed in Rust 2024 edition.
102106
BusHandle::acquire(match self.0 {
103-
BusKind::Internal => unsafe { &mut sys::furi_hal_i2c_handle_power },
104-
BusKind::External => unsafe { &mut sys::furi_hal_i2c_handle_external },
107+
BusKind::Internal => unsafe { addr_of_mut!(sys::furi_hal_i2c_handle_power) },
108+
BusKind::External => unsafe { addr_of_mut!(sys::furi_hal_i2c_handle_external) },
105109
})
106110
}
107111

@@ -115,7 +119,7 @@ impl Bus {
115119

116120
/// A handle to an I2C bus on the Flipper Zero.
117121
pub struct BusHandle {
118-
handle: &'static mut sys::FuriHalI2cBusHandle,
122+
handle: *mut sys::FuriHalI2cBusHandle,
119123
}
120124

121125
impl Drop for BusHandle {
@@ -128,7 +132,7 @@ impl BusHandle {
128132
/// Acquires a handle to the given I2C bus.
129133
///
130134
/// Blocks indefinitely until the Flipper Zero bus is locally available.
131-
fn acquire(handle: &'static mut sys::FuriHalI2cBusHandle) -> Self {
135+
fn acquire(handle: *mut sys::FuriHalI2cBusHandle) -> Self {
132136
unsafe { sys::furi_hal_i2c_acquire(handle) };
133137
Self { handle }
134138
}

crates/flipperzero/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
//!
66
77
#![no_std]
8-
#![cfg_attr(test, no_main)]
8+
#![cfg_attr(all(test, not(miri)), no_main)]
9+
#![cfg_attr(all(test, miri), feature(start))]
910
#![cfg_attr(docsrs, feature(doc_cfg))]
1011
#![deny(rustdoc::broken_intra_doc_links)]
1112

crates/flipperzero/src/storage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl OpenOptions {
146146
}
147147

148148
/// Basic, unbuffered file handle
149+
#[allow(dead_code)]
149150
pub struct File(NonNull<sys::File>, UnsafeRecord<sys::Storage>);
150151

151152
impl File {

crates/rt/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ macro_rules! entry {
3434
($path:path) => {
3535
// Force the section to `.text` instead of `.text.main`.
3636
// lld seems not to automatically rename `.rel.text.main` properly.
37+
#[cfg(not(miri))]
3738
#[export_name = "main"]
3839
pub unsafe fn __main(args: *mut u8) -> i32 {
3940
// type check the entry function

crates/rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
2-
channel = "nightly-2023-12-09"
2+
channel = "nightly-2024-04-16"
33
targets = [ "thumbv7em-none-eabihf" ]
44
components = [ "clippy", "rustfmt" ]

crates/sys/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
#![deny(rustdoc::broken_intra_doc_links)]
55

66
// Features that identify thumbv7em-none-eabihf.
7-
// Until target_abi is stable, this also permits thumbv7em-none-eabi.
8-
#[cfg(not(all(
9-
target_arch = "arm",
10-
target_feature = "thumb2",
11-
target_feature = "v7",
12-
target_feature = "dsp",
13-
target_os = "none",
14-
//target_abi = "eabihf",
7+
#[cfg(not(any(
8+
all(
9+
target_arch = "arm",
10+
target_feature = "thumb2",
11+
target_feature = "v7",
12+
target_feature = "dsp",
13+
target_os = "none",
14+
target_abi = "eabihf",
15+
),
16+
miri
1517
)))]
1618
core::compile_error!("This crate requires `--target thumbv7em-none-eabihf`");
1719

crates/test/macros/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,27 @@ fn tests_runner_impl(args: TokenStream) -> parse::Result<TokenStream> {
131131
}
132132

133133
// Test runner entry point
134-
fn main(args: Option<&::core::ffi::CStr>) -> i32 {
134+
pub(super) fn main(args: Option<&::core::ffi::CStr>) -> i32 {
135135
let args = ::flipperzero_test::__macro_support::Args::parse(args);
136136
match ::flipperzero_test::__macro_support::run_tests(test_count(), test_list(), args) {
137137
Ok(()) => 0,
138138
Err(e) => e,
139139
}
140140
}
141141
}
142+
143+
#[cfg(all(test, miri))]
144+
#[start]
145+
fn main(argc: isize, argv: *const *const u8) -> isize {
146+
// TODO: Is there any benefit to Miri in hooking up the binary arguments to
147+
// the test runner?
148+
let ret = __test_runner::main(None);
149+
150+
// Clean up app state.
151+
::flipperzero_rt::__macro_support::__wait_for_thread_completion();
152+
153+
ret.try_into().unwrap_or(isize::MAX)
154+
}
142155
)
143156
.into())
144157
}

0 commit comments

Comments
 (0)