Skip to content
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

Signature of bkpt() function #119

Closed
therealprof opened this issue Oct 26, 2018 · 7 comments
Closed

Signature of bkpt() function #119

therealprof opened this issue Oct 26, 2018 · 7 comments

Comments

@therealprof
Copy link
Contributor

I was just looking at ways to cheaply and clippy happy "terminate" an #[entry] function and was looking at the cortex_m::asm:bkpt() function noticing that it has a signature of pub fn bkpt(). My expectation would have been that this function has a signature of pub fn bkpt() -> ! instead which would qualify it as a terminating statement in #[entry].

Is there a reason for bkpt() not returning never?

@adamgreig
Copy link
Member

After the breakpoint is triggered, the debugger can just continue and execution continues after the function, right?

@therealprof
Copy link
Contributor Author

Or something completely different can happen because the debugger could completely rewrite the state, or the MCU could go into a Fault state because no debugger is attached. I thought that is exactly what the never type is representing?

@adamgreig
Copy link
Member

But the code after the bkpt() call could be run (and quite often will be!), whereas I thought ! tells the compiler nothing after here will execute; I believe I've seen it generate trap instructions after a ! function to ensure just that (but haven't tested specifically...).

@therealprof
Copy link
Contributor Author

I thought the never type was used to express "no value" or uncertainty of program flow and bpkt() would qualify.

Do you have any other idea how to produce a never value for #[entry] that does not produce overhead (like panic!()) or is frowned upon by clippy (like loop {})?

@therealprof
Copy link
Contributor Author

This related issue just in: rust-embedded/cortex-m-rt#139

@adamgreig
Copy link
Member

The never type ! as a function return type means "never returns" which isn't true of bkpt(). I usually do something like loop { cortex_m::asm::wfi() } or nop().

Re that c-m-rt issue, that's sort of correct behaviour as far as "It seems that all divergent functions (fn(..) -> !) are given the noreturn attribute" goes. Annoying that trashing LR screws with debugging of course...

@therealprof
Copy link
Contributor Author

I'm still not convinced bpkt() doesn't count as a diverging function because it is not guaranteed that program flow will ever continue after it; it's totally up to the debugger (if connected) to decide whether and how to continue after that point.

However reading the book about the never type I actually found a good, cheap and clippy happy solution to my problem:

loop {
     continue;
}

adamgreig pushed a commit that referenced this issue Jan 12, 2022
119: make all exception handlers extern "C" r=adamgreig a=japaric

They'll be called by the hardware and must comply to the stable C ABI.

Using the Rust ABI in this case was an oversight.

r? @rust-embedded/cortex-m (anyone)

Co-authored-by: Jorge Aparicio <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants