Skip to content

Miri Experiment #3297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ jobs:
toolchain: stable
components: rust-src

# MIRI is only available in nightly
- uses: dtolnay/rust-toolchain@v1
with:
toolchain: nightly
components: miri

- uses: Swatinem/rust-cache@v2
with:
prefix-key: "ci-${{ matrix.device.soc }}"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exclude = [
"extras/esp-wifishark",
"extras/ieee802154-sniffer",
"hil-test",
"miri-test",
"qa-test",
"xtensa-lx",
"xtensa-lx-rt",
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ mod peripheral_macros {
#[doc = r"Return a reference to the register block"]
#[inline(always)]
#[instability::unstable]
pub const fn regs<'a>() -> &'a <pac::$base as core::ops::Deref>::Target {
pub fn regs<'a>() -> &'a <pac::$base as core::ops::Deref>::Target {
unsafe { &*Self::PTR }
}

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3663,7 +3663,7 @@ macro_rules! spi_instance {
#[inline(always)]
fn info(&self) -> &'static Info {
static INFO: Info = Info {
register_block: crate::peripherals::[<SPI $num>]::regs(),
register_block: crate::peripherals::[<SPI $num>]::ptr(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this compiles? 👀

peripheral: crate::system::Peripheral::[<Spi $num>],
interrupt: crate::peripherals::Interrupt::[<SPI $num>],
sclk: OutputSignal::$sclk,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/spi/slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ macro_rules! spi_instance {
#[inline(always)]
fn info(&self) -> &'static Info {
static INFO: Info = Info {
register_block: crate::peripherals::[<SPI $num>]::regs(),
register_block: crate::peripherals::[<SPI $num>]::ptr(),
peripheral: crate::system::Peripheral::[<Spi $num>],
sclk: InputSignal::$sclk,
mosi: InputSignal::$mosi,
Expand Down
29 changes: 29 additions & 0 deletions miri-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "miri-test"
version = "0.0.0"
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false

[dependencies]
esp-hal = { path = "../esp-hal", features = ["unstable"] }

[features]
unstable = []
#esp32 = ["esp-hal/esp32", ]
esp32c2 = ["esp-hal/esp32c2", ]
esp32c3 = ["esp-hal/esp32c3", ]
esp32c6 = ["esp-hal/esp32c6", ]
esp32h2 = ["esp-hal/esp32h2", ]
#esp32s2 = ["esp-hal/esp32s2", ]
#esp32s3 = ["esp-hal/esp32s3", ]

[profile.dev]
panic = "abort"

[profile.release]
debug = 2
debug-assertions = true
lto = "fat"
codegen-units = 1
panic = "abort"
13 changes: 13 additions & 0 deletions miri-test/src/bin/duration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]
#![no_main]

use miri_test as _;

miri_test::miri_init!();

fn main() {
let one_hour = esp_hal::time::Duration::from_hours(1);

assert_eq!(one_hour.as_minutes(), 60);
assert_eq!(one_hour.as_secs(), 3600);
}
50 changes: 50 additions & 0 deletions miri-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![no_std]
#![allow(internal_features)]
#![feature(core_intrinsics)]

// see https://github.com/rust-lang/miri/blob/master/tests/utils/miri_extern.rs
extern "Rust" {
pub fn miri_write_to_stdout(bytes: &[u8]);
}

#[panic_handler]
fn panic<'a, 'b>(pi: &'a core::panic::PanicInfo<'b>) -> ! {
println!("{:?}", pi);
core::intrinsics::abort();
}

#[macro_export]
macro_rules! miri_init {
() => {
#[cfg(miri)]
#[no_mangle]
fn miri_start(argc: isize, argv: *const *const u8) -> isize {
main();
0
}

#[cfg(not(miri))]
pub fn _main() {
main();
}
};
}

#[macro_export]
macro_rules! println {
($($arg:tt)*) => {{
{
use core::fmt::Write;
writeln!($crate::Printer, $($arg)*).ok();
}
}};
}

pub struct Printer;

impl core::fmt::Write for Printer {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
unsafe { miri_write_to_stdout(s.as_bytes()) };
Ok(())
}
}
3 changes: 2 additions & 1 deletion xtask/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ impl CargoArgsBuilder {
args.push(format!("+{toolchain}"));
}

args.push(self.subcommand.clone());
let sub_commands: Vec<String> = self.subcommand.split(' ').into_iter().map(|v| v.to_string()).collect();
args.extend_from_slice(&sub_commands);

if let Some(ref target) = self.target {
args.push(format!("--target={target}"));
Expand Down
10 changes: 9 additions & 1 deletion xtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum Package {
EspWifi,
Examples,
HilTest,
MiriTest,
QaTest,
XtensaLx,
XtensaLxRt,
Expand Down Expand Up @@ -86,7 +87,7 @@ impl Package {

/// Should documentation be built for the package?
pub fn is_published(&self) -> bool {
!matches!(self, Package::Examples | Package::HilTest | Package::QaTest)
!matches!(self, Package::Examples | Package::HilTest | Package::QaTest | Package::MiriTest)
}

/// Build on host
Expand Down Expand Up @@ -114,6 +115,7 @@ pub fn execute_app(
action: CargoAction,
repeat: usize,
debug: bool,
use_miri: bool,
) -> Result<()> {
let package = app.example_path().strip_prefix(package_path)?;
log::info!("Building example '{}' for '{}'", package.display(), chip);
Expand All @@ -137,6 +139,10 @@ pub fn execute_app(
.target(target)
.features(&features);

if use_miri {
builder = builder.toolchain(if chip.is_xtensa() { "esp" } else { "nightly" });
}

let bin_arg = if package.starts_with("src/bin") {
format!("--bin={}", app.binary_name())
} else if package.starts_with("tests") {
Expand All @@ -150,6 +156,8 @@ pub fn execute_app(
"build"
} else if package.starts_with("tests") {
"test"
} else if use_miri {
"miri run"
} else {
"run"
};
Expand Down
102 changes: 98 additions & 4 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use strum::IntoEnumIterator;
use xtask::{
cargo::{CargoAction, CargoArgsBuilder},
firmware::Metadata,
target_triple,
Package,
Version,
target_triple, Package, Version,
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -48,6 +46,8 @@ enum Cli {
RunDocTests(ExampleArgs),
/// Run the given example for the specified chip.
RunExample(ExampleArgs),
/// Run the binary example for the specified chip via Miri.
RunMiri(ExampleArgs),
/// Run all applicable tests or the specified test for a specified chip.
RunTests(TestArgs),
/// Run all ELFs in a folder.
Expand Down Expand Up @@ -242,6 +242,7 @@ fn main() -> Result<()> {
Cli::RunDocTests(args) => run_doc_tests(&workspace, args),
Cli::RunElfs(args) => run_elfs(args),
Cli::RunExample(args) => examples(&workspace, args, CargoAction::Run),
Cli::RunMiri(args) => miri(&workspace, args),
Cli::RunTests(args) => tests(&workspace, args, CargoAction::Run),
Cli::Ci(args) => run_ci_checks(&workspace, args),
Cli::TagReleases(args) => tag_releases(&workspace, args),
Expand Down Expand Up @@ -298,6 +299,80 @@ fn examples(workspace: &Path, mut args: ExampleArgs, action: CargoAction) -> Res
}
}

fn miri(workspace: &Path, args: ExampleArgs) -> Result<()> {
// Ensure that the package/chip combination provided are valid:
xtask::validate_package_chip(&args.package, &args.chip)?;

if args.package != Package::MiriTest {
panic!("find out how to do this!");
//return Err(anyhow::Error::from("Only `miri-test` is supported here".into()));
}

// Absolute path of the package's root:
let package_path = xtask::windows_safe_path(&workspace.join(args.package.to_string()));

let example_path = match args.package {
Package::Examples | Package::QaTest | Package::MiriTest => {
package_path.join("src").join("bin")
}
Package::HilTest => package_path.join("tests"),
_ => package_path.join("examples"),
};

// Load all examples which support the specified chip and parse their metadata:
let mut examples = xtask::firmware::load(&example_path)?
.iter()
.filter_map(|example| {
if example.supports_chip(args.chip) {
Some(example.clone())
} else {
None
}
})
.collect::<Vec<_>>();

// Sort all examples by name:
examples.sort_by_key(|a| a.binary_name());

run_miri(args, examples, &package_path)
}

fn run_miri(args: ExampleArgs, examples: Vec<Metadata>, package_path: &Path) -> Result<()> {
// for now just return if the target is Xtensa - we don't have Miri for Xtensa
if args.chip.is_xtensa() {
return Ok(());
}

// Determine the appropriate build target for the given package and chip:
let target = target_triple(args.package, &args.chip)?;

let mut found_one = false;
for example in examples
.iter()
.filter(|ex| (args.example.is_none() && ex.supports_chip(args.chip)) || ex.matches(&args.example))
{
found_one = true;
xtask::execute_app(
package_path,
args.chip,
target,
example,
CargoAction::Run,
1,
args.debug,
true,
)?;
}

ensure!(
found_one,
"Example not found or unsupported for {}",
args.chip
);

Ok(())
}

fn build_examples(
args: ExampleArgs,
examples: Vec<Metadata>,
Expand All @@ -322,6 +397,7 @@ fn build_examples(
CargoAction::Build(out_path.clone()),
1,
args.debug,
false,
)?;
}
Ok(())
Expand All @@ -339,6 +415,7 @@ fn build_examples(
CargoAction::Build(out_path.clone()),
1,
args.debug,
false,
)
})
}
Expand All @@ -361,6 +438,7 @@ fn run_example(args: ExampleArgs, examples: Vec<Metadata>, package_path: &Path)
CargoAction::Run,
1,
args.debug,
false,
)?;
}

Expand Down Expand Up @@ -423,6 +501,7 @@ fn run_examples(args: ExampleArgs, examples: Vec<Metadata>, package_path: &Path)
CargoAction::Run,
1,
args.debug,
false,
)
.is_err()
{
Expand Down Expand Up @@ -473,6 +552,7 @@ fn tests(workspace: &Path, args: TestArgs, action: CargoAction) -> Result<()> {
action.clone(),
args.repeat.unwrap_or(1),
false,
false,
)?;
}
Ok(())
Expand All @@ -489,6 +569,7 @@ fn tests(workspace: &Path, args: TestArgs, action: CargoAction) -> Result<()> {
action.clone(),
args.repeat.unwrap_or(1),
false,
false,
)
.is_err()
{
Expand Down Expand Up @@ -819,7 +900,7 @@ fn lint_packages(workspace: &Path, args: LintPackagesArgs) -> Result<()> {

// We will *not* check the following packages with `clippy`; this
// may or may not change in the future:
Package::Examples | Package::HilTest | Package::QaTest => {}
Package::Examples | Package::HilTest | Package::QaTest | Package::MiriTest => {}

// By default, no `clippy` arguments are required:
_ => lint_package(chip, &path, &[], args.fix, package.build_on_host())?,
Expand Down Expand Up @@ -1124,6 +1205,19 @@ fn run_ci_checks(workspace: &Path, args: CiArgs) -> Result<()> {
.inspect_err(|_| failure = true)
.ok();

// Miri checks
miri(
workspace,
ExampleArgs {
package: Package::MiriTest,
chip: args.chip,
example: None,
debug: true,
},
)
.inspect_err(|_| failure = true)
.ok();

let completed_at = Instant::now();
log::info!("CI checks completed in {:?}", completed_at - started_at);

Expand Down
Loading