Skip to content

Commit 98e2eb0

Browse files
committed
fix panicking
1 parent d846877 commit 98e2eb0

File tree

3 files changed

+301
-56
lines changed

3 files changed

+301
-56
lines changed

flc-asm/src/lib.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ macro_rules! never_exit {
5151
};
5252
}
5353

54-
/// A panic handler that never exits, even in cases of fault-injection attacks.
55-
// In debug mode, don't inline in order to allow setting breakpoints.
56-
#[cfg_attr(debug_assertions, inline(never))]
5754
#[panic_handler]
55+
fn panic_handler(_: &PanicInfo) -> ! {
56+
never_exit!()
57+
}
58+
59+
/// A "panic" function that is guaranteed to be in RAM
5860
#[link_section = ".analogsucks"]
59-
fn panic_handler(_info: &PanicInfo) -> ! {
61+
fn panic() -> ! {
6062
never_exit!()
6163
}
6264

@@ -75,19 +77,19 @@ struct FlashController<'gcr, 'icc> {
7577
icc: &'icc ICC0,
7678
}
7779

78-
/// Checks whether the given address range (exclusive) is within flash space.
79-
///
80-
/// # Panics
81-
/// - Panics if the given address range is not contained within flash range.
80+
/// Checks whether the given address range (exclusive) is within flash space, returning `false` if there
81+
/// is an error.
8282
#[inline(always)]
83-
const fn check_address_bounds(address_range: core::ops::Range<u32>) {
83+
#[must_use]
84+
const fn check_address_bounds(address_range: core::ops::Range<u32>) -> bool {
8485
if !(FLASH_MEM_BASE <= address_range.start
8586
&& address_range.start < FLASH_MEM_BASE + FLASH_MEM_SIZE
8687
&& FLASH_MEM_BASE < address_range.end
8788
&& address_range.end <= FLASH_MEM_BASE + FLASH_MEM_SIZE)
8889
{
89-
panic!();
90+
return false;
9091
}
92+
return true;
9193
}
9294

9395
impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
@@ -209,9 +211,9 @@ impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
209211
const PAGE2: u32 = FLASH_MEM_BASE + FLASH_PAGE_SIZE;
210212
// SAFETY: `FLASH_MEM_BASE` points to a valid, aligned word within flash space.
211213
const {
212-
check_address_bounds(PAGE1..PAGE1 + 4);
214+
assert!(check_address_bounds(PAGE1..PAGE1 + 4));
213215
assert!(PAGE1 % 4 == 0);
214-
check_address_bounds(PAGE2..PAGE2 + 4);
216+
assert!(check_address_bounds(PAGE2..PAGE2 + 4));
215217
assert!(PAGE2 % 4 == 0);
216218
}
217219
unsafe { core::hint::black_box(read32(PAGE1 as *const u32)) };
@@ -268,9 +270,11 @@ impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
268270
/// - `address` must be aligned to 128 bits
269271
#[inline(always)]
270272
unsafe fn write128(&self, address: u32, data: &[u32; 4], sys_clk_freq: u32) {
271-
check_address_bounds(address..address + 16);
273+
if !check_address_bounds(address..address + 16) {
274+
panic()
275+
}
272276
if address % size_of::<[u32; 4]>() as u32 != 0 {
273-
panic!();
277+
panic();
274278
}
275279

276280
// SAFETY: the caller must guarantee that `sys_clk_freq` is valid per this function's
@@ -305,8 +309,11 @@ impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
305309
/// - If `sys_clk_freq` is not a multiple of 1 MHz, this function panics.
306310
/// - This function also panics when the `address` does not point inside of a page
307311
/// contained in flash space.
312+
#[inline(always)]
308313
unsafe fn page_erase(&self, address: u32, sys_clk_freq: u32) {
309-
check_address_bounds(address..address + 1);
314+
if !check_address_bounds(address..address + 1) {
315+
panic()
316+
}
310317
// SAFETY: the caller must guarantee that `sys_clk_freq` is valid per this function's
311318
// safety comment.
312319
unsafe {
@@ -329,9 +336,11 @@ impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
329336
#[link_section = ".analogsucks"]
330337
pub unsafe extern "C" fn read32(address: *const u32) -> u32 {
331338
if !address.is_aligned() {
332-
panic!();
339+
panic();
340+
}
341+
if !check_address_bounds(address as u32..(address as u32 + 4)) {
342+
panic();
333343
}
334-
check_address_bounds(address as u32..(address as u32 + 4));
335344
// SAFETY: the caller must guarantee that `address` is aligned and is within
336345
// flash memory.
337346
unsafe { read_volatile(address) }

0 commit comments

Comments
 (0)