Skip to content

Commit 4ee4d73

Browse files
authored
Ensure asserts can be optimized away (#4306)
1 parent 6c0c21d commit 4ee4d73

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

esp-sync/src/raw.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub trait RawLock {
2727
/// A lock that disables interrupts.
2828
pub struct SingleCoreInterruptLock;
2929

30+
// Reserved bits in the PS register, these must be written as 0.
31+
#[cfg(all(xtensa, debug_assertions))]
32+
const RESERVED_MASK: u32 = 0b1111_1111_1111_1000_1111_0000_0000_0000;
33+
3034
impl RawLock for SingleCoreInterruptLock {
3135
#[inline]
3236
unsafe fn enter(&self) -> RestoreState {
@@ -38,6 +42,8 @@ impl RawLock for SingleCoreInterruptLock {
3842
} else if #[cfg(xtensa)] {
3943
let token: u32;
4044
unsafe { core::arch::asm!("rsil {0}, 5", out(reg) token); }
45+
#[cfg(debug_assertions)]
46+
let token = token & !RESERVED_MASK;
4147
} else {
4248
compile_error!("Unsupported architecture")
4349
}
@@ -67,21 +73,18 @@ impl RawLock for SingleCoreInterruptLock {
6773
}
6874
} else if #[cfg(xtensa)] {
6975
#[cfg(debug_assertions)]
70-
{
71-
// Reserved bits in the PS register, these must be written as 0.
72-
const RESERVED_MASK: u32 = 0b1111_1111_1111_1000_1111_0000_0000_0000;
73-
if token & RESERVED_MASK != 0 {
74-
// We could do this transformation in fmt.rs automatically, but experiments
75-
// show this is only worth it in terms of binary size for code inlined into many places.
76-
#[cold]
77-
#[inline(never)]
78-
fn __assert_failed() {
79-
panic!("Reserved bits in PS register must be written as 0");
80-
}
81-
82-
__assert_failed();
76+
if token & RESERVED_MASK != 0 {
77+
// We could do this transformation in fmt.rs automatically, but experiments
78+
// show this is only worth it in terms of binary size for code inlined into many places.
79+
#[cold]
80+
#[inline(never)]
81+
fn __assert_failed() {
82+
panic!("Reserved bits in PS register must be written as 0");
8383
}
84+
85+
__assert_failed();
8486
}
87+
8588
unsafe {
8689
core::arch::asm!(
8790
"wsr.ps {0}",

0 commit comments

Comments
 (0)