From 06faae8f82044d09c1cb4d136a7f673ab4cbd9fc Mon Sep 17 00:00:00 2001 From: cepetr Date: Tue, 11 Jun 2024 14:59:16 +0200 Subject: [PATCH] refactor(core/rust): replace panic! by fatal_error! [no changelog] --- core/embed/rust/src/lib.rs | 2 +- core/embed/rust/src/micropython/iter.rs | 2 +- core/embed/rust/src/trezorhal/fatal_error.rs | 18 +++++++++--------- core/embed/rust/src/trezorhal/translations.rs | 2 +- core/embed/rust/src/ui/animation.rs | 2 +- core/embed/rust/src/ui/component/base.rs | 8 ++++---- core/embed/rust/src/ui/component/placed.rs | 4 ++-- .../rust/src/ui/component/text/paragraphs.rs | 2 +- core/embed/rust/src/ui/display/font.rs | 2 +- core/embed/rust/src/ui/layout/obj.rs | 2 +- core/embed/rust/src/ui/model_mercury/layout.rs | 2 +- .../ui/model_tr/component/hold_to_confirm.rs | 2 +- core/embed/rust/src/ui/model_tt/layout.rs | 2 +- core/embed/rust/src/ui/shape/bar.rs | 2 +- core/embed/rust/src/ui/shape/circle.rs | 2 +- core/embed/rust/src/ui/shape/qrcode.rs | 2 +- core/embed/rust/src/ui/util.rs | 4 +--- 17 files changed, 29 insertions(+), 31 deletions(-) diff --git a/core/embed/rust/src/lib.rs b/core/embed/rust/src/lib.rs index c51044faa..c9d1b2260 100644 --- a/core/embed/rust/src/lib.rs +++ b/core/embed/rust/src/lib.rs @@ -73,7 +73,7 @@ fn panic(_info: &core::panic::PanicInfo) -> ! { // raises a Hard Fault on hardware. // // Otherwise, use `unwrap!` macro from trezorhal. - fatal_error!("", "rs"); + fatal_error!("rs"); } #[cfg(not(target_arch = "arm"))] diff --git a/core/embed/rust/src/micropython/iter.rs b/core/embed/rust/src/micropython/iter.rs index afbfcf9ed..fbc322046 100644 --- a/core/embed/rust/src/micropython/iter.rs +++ b/core/embed/rust/src/micropython/iter.rs @@ -87,7 +87,7 @@ impl<'a> Iterator for Iter<'a> { self.iter_buf.caught_exception = exc; None } - Err(_) => panic!("unexpected error"), + Err(_) => fatal_error!("Unexpected error"), Ok(item) if item == Obj::const_stop_iteration() => { self.finished = true; None diff --git a/core/embed/rust/src/trezorhal/fatal_error.rs b/core/embed/rust/src/trezorhal/fatal_error.rs index 8f66c6bc4..3d8ca31b3 100644 --- a/core/embed/rust/src/trezorhal/fatal_error.rs +++ b/core/embed/rust/src/trezorhal/fatal_error.rs @@ -108,19 +108,19 @@ macro_rules! unwrap { macro_rules! ensure { ($what:expr, $error:expr) => { if !($what) { - fatal_error!(stringify!($what), $error); + crate::trezorhal::fatal_error::__fatal_error( + stringify!($what), + $error, + file!(), + line!(), + function_name!(), + ); } }; } macro_rules! fatal_error { - ($expr:expr, $msg:expr) => {{ - crate::trezorhal::fatal_error::__fatal_error( - stringify!($expr), - $msg, - file!(), - line!(), - function_name!(), - ); + ($msg:expr) => {{ + crate::trezorhal::fatal_error::__fatal_error("", $msg, file!(), line!(), function_name!()); }}; } diff --git a/core/embed/rust/src/trezorhal/translations.rs b/core/embed/rust/src/trezorhal/translations.rs index 7761087dd..edac8f054 100644 --- a/core/embed/rust/src/trezorhal/translations.rs +++ b/core/embed/rust/src/trezorhal/translations.rs @@ -7,7 +7,7 @@ pub unsafe fn get_blob<'a>() -> &'a [u8] { let mut len: u32 = 0; let ptr = unsafe { ffi::translations_read(&mut len, 0) }; if ptr.is_null() { - fatal_error!("Translations read failed", ""); + fatal_error!("Translations read failed"); } // SAFETY: The pointer is always valid. unsafe { core::slice::from_raw_parts(ptr, len as usize) } diff --git a/core/embed/rust/src/ui/animation.rs b/core/embed/rust/src/ui/animation.rs index f72424207..af0709c84 100644 --- a/core/embed/rust/src/ui/animation.rs +++ b/core/embed/rust/src/ui/animation.rs @@ -57,7 +57,7 @@ impl Animation { } else { // Duration is too large to be added to an `Instant`. #[cfg(feature = "ui_debug")] - panic!("offset is too large"); + fatal_error!("Offset is too large"); } } diff --git a/core/embed/rust/src/ui/component/base.rs b/core/embed/rust/src/ui/component/base.rs index 16929b8cd..3104180c7 100644 --- a/core/embed/rust/src/ui/component/base.rs +++ b/core/embed/rust/src/ui/component/base.rs @@ -226,7 +226,7 @@ impl Root { if let Some(ref mut c) = self.inner { c } else { - fatal_error!("deallocated", "Root object is deallocated") + fatal_error!("Root object is deallocated") } } @@ -234,7 +234,7 @@ impl Root { if let Some(ref c) = self.inner { c } else { - fatal_error!("deallocated", "Root object is deallocated") + fatal_error!("Root object is deallocated") } } @@ -459,7 +459,7 @@ where // Messages raised during a `RequestPaint` dispatch are not propagated, let's // make sure we don't do that. #[cfg(feature = "ui_debug")] - panic!("cannot raise messages during RequestPaint"); + fatal_error!("Cannot raise messages during RequestPaint"); } // Make sure to at least a propagate the paint flag upwards (in case there are // no `Child` instances in `self`, paint would not get automatically requested @@ -662,7 +662,7 @@ impl EventCtx { // The timer queue is full, this would be a development error in the layout // layer. Let's panic in the debug env. #[cfg(feature = "ui_debug")] - panic!("timer queue is full"); + fatal_error!("Timer queue is full"); } } diff --git a/core/embed/rust/src/ui/component/placed.rs b/core/embed/rust/src/ui/component/placed.rs index 18fcc1e18..d3901203f 100644 --- a/core/embed/rust/src/ui/component/placed.rs +++ b/core/embed/rust/src/ui/component/placed.rs @@ -163,7 +163,7 @@ where let mut border = self.border; let area = match self.align.0 { Alignment::Start => bounds.split_left(self.size.x).0, - Alignment::Center => panic!("alignment not supported"), + Alignment::Center => fatal_error!("Alignment not supported"), Alignment::End => { border.x = -border.x; bounds.split_right(self.size.x).1 @@ -171,7 +171,7 @@ where }; let area = match self.align.1 { Alignment::Start => area.split_top(self.size.y).0, - Alignment::Center => panic!("alignment not supported"), + Alignment::Center => fatal_error!("Alignment not supported"), Alignment::End => { border.y = -border.y; area.split_bottom(self.size.y).1 diff --git a/core/embed/rust/src/ui/component/text/paragraphs.rs b/core/embed/rust/src/ui/component/text/paragraphs.rs index 34ae7a9e0..9e3b54677 100644 --- a/core/embed/rust/src/ui/component/text/paragraphs.rs +++ b/core/embed/rust/src/ui/component/text/paragraphs.rs @@ -766,7 +766,7 @@ impl<'a, const N: usize> VecExt<'a> for Vec, N> { } if self.push(paragraph).is_err() { #[cfg(feature = "ui_debug")] - panic!("paragraph list is full"); + fatal_error!("Paragraph list is full"); } self } diff --git a/core/embed/rust/src/ui/display/font.rs b/core/embed/rust/src/ui/display/font.rs index 5c65d9385..2814f7fa3 100644 --- a/core/embed/rust/src/ui/display/font.rs +++ b/core/embed/rust/src/ui/display/font.rs @@ -47,7 +47,7 @@ impl Glyph { 2 => (width * height + 3) / 4, // packed bits 4 => (width + 1) / 2 * height, // row aligned to bytes 8 => width * height, - _ => panic!(), + _ => fatal_error!("Unsupported font bpp"), }; Glyph { diff --git a/core/embed/rust/src/ui/layout/obj.rs b/core/embed/rust/src/ui/layout/obj.rs index 315744e61..f01f1a876 100644 --- a/core/embed/rust/src/ui/layout/obj.rs +++ b/core/embed/rust/src/ui/layout/obj.rs @@ -485,7 +485,7 @@ extern "C" fn ui_layout_request_complete_repaint(this: Obj) -> Obj { // Messages raised during a `RequestPaint` dispatch are not propagated, let's // make sure we don't do that. #[cfg(feature = "ui_debug")] - panic!("cannot raise messages during RequestPaint"); + fatal_error!("Cannot raise messages during RequestPaint"); }; this.obj_request_clear(); Ok(Obj::const_none()) diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index d0641fa2c..ad12ca051 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -133,7 +133,7 @@ where if let Some(word) = self.mnemonic() { word.try_into() } else { - panic!("invalid mnemonic") + fatal_error!("Invalid mnemonic") } } MnemonicKeyboardMsg::Previous => "".try_into(), diff --git a/core/embed/rust/src/ui/model_tr/component/hold_to_confirm.rs b/core/embed/rust/src/ui/model_tr/component/hold_to_confirm.rs index d0afb1e2c..5cd104cb3 100644 --- a/core/embed/rust/src/ui/model_tr/component/hold_to_confirm.rs +++ b/core/embed/rust/src/ui/model_tr/component/hold_to_confirm.rs @@ -49,7 +49,7 @@ impl HoldToConfirm { ButtonContent::Text(text) => { Self::text(pos, text, LoaderStyleSheet::default_loader(), duration) } - ButtonContent::Icon(_) => panic!("Icon is not supported"), + ButtonContent::Icon(_) => fatal_error!("Icon is not supported"), } } diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 8e638bb3d..538cc91b9 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -162,7 +162,7 @@ where if let Some(word) = self.mnemonic() { word.try_into() } else { - panic!("invalid mnemonic") + fatal_error!("Invalid mnemonic") } } MnemonicKeyboardMsg::Previous => "".try_into(), diff --git a/core/embed/rust/src/ui/shape/bar.rs b/core/embed/rust/src/ui/shape/bar.rs index 97f5d8dc6..238d33c35 100644 --- a/core/embed/rust/src/ui/shape/bar.rs +++ b/core/embed/rust/src/ui/shape/bar.rs @@ -75,7 +75,7 @@ impl Shape<'_> for Bar { // is not supported. If we needed it, we would have to // introduce a new function in RgbCanvas. - // TODO: panic! in unsupported scenarious + // TODO: fatal_error! in unsupported scenarious let th = match self.fg_color { Some(_) => self.thickness, diff --git a/core/embed/rust/src/ui/shape/circle.rs b/core/embed/rust/src/ui/shape/circle.rs index af2558466..c4cc309fe 100644 --- a/core/embed/rust/src/ui/shape/circle.rs +++ b/core/embed/rust/src/ui/shape/circle.rs @@ -91,7 +91,7 @@ impl Shape<'_> for Circle { // is not supported. If we needed it, we would have to // introduce RgbCanvas::draw_ring() function. - // TODO: panic! in unsupported scenarious + // TODO: fatal_error! in unsupported scenarious let th = match self.fg_color { Some(_) => self.thickness, None => 0, diff --git a/core/embed/rust/src/ui/shape/qrcode.rs b/core/embed/rust/src/ui/shape/qrcode.rs index 491dae033..1a0249e2d 100644 --- a/core/embed/rust/src/ui/shape/qrcode.rs +++ b/core/embed/rust/src/ui/shape/qrcode.rs @@ -30,7 +30,7 @@ pub struct QrImage { impl QrImage { pub fn new(area: Rect, qrcode: &QrCode) -> Self { if area.width() < qrcode.size() as i16 || area.height() < qrcode.size() as i16 { - panic!("Too small area"); + fatal_error!("Too small area"); } let mut result = QrImage { diff --git a/core/embed/rust/src/ui/util.rs b/core/embed/rust/src/ui/util.rs index e27b94c1f..6f5ede8a7 100644 --- a/core/embed/rust/src/ui/util.rs +++ b/core/embed/rust/src/ui/util.rs @@ -22,9 +22,7 @@ impl ResultExt for Result { fn assert_if_debugging_ui(self, #[allow(unused)] message: &str) { #[cfg(feature = "ui_debug")] if self.is_err() { - print!("Panic from assert_if_debugging_ui: "); - println!(message); - panic!("{}", message); + fatal_error!(message); } } }