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

rust: handle panics to show meaningful error #1336

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

Beerosagos
Copy link
Collaborator

In b09c333 we added the panic_immediate_abort to the rust cargo flags. This caused all the panics previously handled by the handler defined in lib.rs to just show the hard fault error message on the screen of the bitbox.

This adds the manual handling of some of those panics, to provide useful debug messages.

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

The pattern with empty_loop tag and print/busyloop is repeated many times. It would be good to put this into a function like Abort() and just call that.

print_debug!() should probably be renamed if it is not used for debugging but for production messages.

@Beerosagos Beerosagos force-pushed the handle-panics branch 2 times, most recently from 7b5e826 to cbf9051 Compare December 9, 2024 13:43
@Beerosagos
Copy link
Collaborator Author

Updated! PTAL 🙏

/// displays the input error message on the screen and enters
/// an infinite loop.
#[allow(clippy::empty_loop)]
pub fn abort(err: &str) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think here -> ! would fit as a return type? Right @NickeZ?

https://doc.rust-lang.org/reference/types/never.html

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, if you make it return !, you can probably remove the clippy lint exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, but clippy still complaints

Comment on lines 425 to 430
pub fn with_lock_animation<F: Fn()>(f: F) {
pub fn lock_animation_start() {
unsafe { bitbox02_sys::lock_animation_start() };
f();
}

pub fn lock_animation_stop() {
unsafe { bitbox02_sys::lock_animation_stop() };
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine, but if you wanted to keep the with_lock_animation() wrapper, you could have the callback function return the error and do

    unsafe { bitbox02::ui::lock_animation_start(); }
    let result = f();
    unsafe { bitbox02::ui::lock_animation_stop(); }
    result

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried it in the beginning, but it wasn't working well as I was handling the error inside f(), and the error message was overlapped by the unlock animation. Now it should work properly 👍

In b09c333 we added the `panic_immediate_abort` to the rust cargo flags.
This caused all the panics previously handled by the handler defined in
lib.rs to just show the `hard fault` error message on the screen of the
bitbox.

This adds the manual handling of some of those panics, to provide useful
debug messages.
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

utACK

@Beerosagos Beerosagos merged commit 402201e into BitBoxSwiss:master Dec 16, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

3 participants