From a7b1bf959bbe64bc93e759eeff58cc2a12917c7a Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:15:20 +0200 Subject: [PATCH] Add panic handler using semihosting to vm app --- vm/src/main.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/vm/src/main.rs b/vm/src/main.rs index 38652a3..fdc8238 100644 --- a/vm/src/main.rs +++ b/vm/src/main.rs @@ -17,6 +17,7 @@ #![no_std] #![no_main] +#![feature(panic_info_message)] mod app_ui; mod handlers; @@ -34,8 +35,6 @@ use ledger_device_sdk::io::{ApduHeader, Comm, Event, Reply, StatusWords}; #[cfg(not(any(target_os = "stax", target_os = "flex")))] use ledger_device_sdk::ui::gadgets::display_pending_review; -ledger_device_sdk::set_panic!(ledger_device_sdk::exiting_panic); - // Required for using String, Vec, format!... extern crate alloc; @@ -61,6 +60,31 @@ macro_rules! println { }); } +// Print panic message to the console. Uses the `print!` macro defined above, +// therefore it only works when running on Speculos. +fn handle_panic(info: &core::panic::PanicInfo) -> ! { + let message = if let Some(location) = info.location() { + alloc::format!( + "Panic occurred in file '{}' at line {}: {:?}", + location.file(), + location.line(), + info.message().unwrap_or(&format_args!("no message")) + ) + } else { + alloc::format!( + "Panic occurred: {}", + info.message().unwrap_or(&format_args!("no message")) + ) + }; + println!("{}", message); + + let mut comm = ledger_device_sdk::io::Comm::new(); + comm.reply(ledger_device_sdk::io::StatusWords::Panic); + ledger_device_sdk::exit_app(0x01); +} + +ledger_device_sdk::set_panic!(handle_panic); + // Application status words. #[repr(u16)] pub enum AppSW {