miri: ```no_std``` issues with implementing ```panic_handler```

I’m trying to interpet a very simple no_std example before going for any bigger problems. Problem appears to be that none of the panic_handler crates are working with Miri.

The command I’m using to run the code is: MIRI_NO_STD=1 cargo miri run

Code:

#![no_main]
#![no_std]

//insert individual panic methods here

use cortex_m_rt;

#[cortex_m_rt::entry]
fn main() -> ! {
   loop {}
}

First off, both use panic_halt as _; and use panic_abort as _; create the following issue:

error: unwinding panics are not supported without std
 |
 = help: using nightly cargo, use -Zbuild-std with panic="abort" to avoid unwinding
 = note: since the core library is usually precompiled with panic="unwind", rebuilding your crate with panic="abort" may not be enough to fix the problem

That’s something that seems to be an issue with cargo build in general and not just Miri specific. I don’t quite understand why abort and halt wouldn’t work for no_std environments though considering these panic handlers were made for this express purpose.

So I tried going for use panic_semihosting as _; and encountered an even weirder problem where the crates aren’t recognized on build but only when I run cargo miri. If I run cargo run everything works without a hitch. Error:

error[E0463]: can't find crate for `panic_semihosting`
 --> embeddedMiri/src/main.rs:4:5
  |
4 | use panic_semihosting as _;
  |     ^^^^^^^^^^^^^^^^^ can't find crate
  |
  = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `#[panic_handler]` function required, but not found

error: unwinding panics are not supported without std
  |
  = help: using nightly cargo, use -Zbuild-std with panic="abort" to avoid unwinding
  = note: since the core library is usually precompiled with panic="unwind", rebuilding your crate with panic="abort" may not be enough to fix the problem

It seems that Miri fails to somehow link the cargo crates when building. I’ve also tried building with extern crate but then the problem of the panic handler being missing consists and all examples of panic handlers using the #[panic_handler] flag I found invoke the unwinding panics error.

Thank you in advance for the help!

About this issue

  • Original URL
  • State: closed
  • Created 2 months ago
  • Comments: 15 (11 by maintainers)

Most upvoted comments

Strangely, if I make that program panic it seems to use the default panic handler from std. I don’t know where this comes from. But cargo run behaves the same so it’s not a Miri thing…

Ah, lol, figured it out…

#![cfg(all(target_arch = "arm", target_os = "none"))]

That crate is empty when built for any other target. Which means it does not have the no_std attribute. Which means it pulls in std, and therefore the std panic handler.