Skip to content

Commit 6a0432a

Browse files
Merge #202
202: Prevent unnecessary bounds check in SCB::{get_priority, set_priority} r=therealprof a=qwerty19106 SystemHandler.index() gives true index for SCB.shpr array. But now it produce unnecessary bounds check. For example, SCB::set_priority: ``` 48: sym.cortex_m::peripheral::scb::__impl_cortex_m::peripheral::SCB_::set_priority::h622cd21bcc3321be (); 0x08000376 push {r7, lr} 0x08000378 mov r7, sp 0x0800037a bl cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5 ; sym.cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5 0x0800037e subs r0, 4 0x08000380 uxtb r0, r0 0x08000382 cmp r0, 0xb ; 11 0x08000384 itttt ls 0x08000386 movw r1, 0xed18 0x0800038a movt r1, 0xe000 0x0800038e movs r2, 0xff 0x08000390 strb r2, [r0, r1] 0x08000392 it ls 0x08000394 pop {r7, pc} 0x08000396 movw r2, 0x1dc0 0x0800039a movs r1, 0xc 0x0800039c movt r2, 0x800 0x080003a0 bl core::panicking::panic_bounds_check::h5181f47ae17a04a5 ; sym.core::panicking::panic_bounds_check::h5181f47ae17a04a5 0x080003a4 trap ``` This PR fix it. Co-authored-by: Роман Кривенков <[email protected]>
2 parents 12f11d2 + 9c57405 commit 6a0432a

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/peripheral/scb.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,13 +949,23 @@ impl SCB {
949949
#[cfg(not(armv6m))]
950950
{
951951
// NOTE(unsafe) atomic read with no side effects
952-
unsafe { (*Self::ptr()).shpr[usize::from(index - 4)].read() }
952+
953+
// NOTE(unsafe): Index is bounded to [4,15] by SystemHandler design.
954+
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
955+
let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from(index - 4))};
956+
957+
priority_ref.read()
953958
}
954959

955960
#[cfg(armv6m)]
956961
{
957962
// NOTE(unsafe) atomic read with no side effects
958-
let shpr = unsafe { (*Self::ptr()).shpr[usize::from((index - 8) / 4)].read() };
963+
964+
// NOTE(unsafe): Index is bounded to [11,15] by SystemHandler design.
965+
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
966+
let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4))};
967+
968+
let shpr = priority_ref.read();
959969
let prio = (shpr >> (8 * (index % 4))) & 0x0000_00ff;
960970
prio as u8
961971
}
@@ -979,12 +989,20 @@ impl SCB {
979989

980990
#[cfg(not(armv6m))]
981991
{
982-
self.shpr[usize::from(index - 4)].write(prio)
992+
// NOTE(unsafe): Index is bounded to [4,15] by SystemHandler design.
993+
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
994+
let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from(index - 4));
995+
996+
priority_ref.write(prio)
983997
}
984998

985999
#[cfg(armv6m)]
9861000
{
987-
self.shpr[usize::from((index - 8) / 4)].modify(|value| {
1001+
// NOTE(unsafe): Index is bounded to [11,15] by SystemHandler design.
1002+
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
1003+
let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4));
1004+
1005+
priority_ref.modify(|value| {
9881006
let shift = 8 * (index % 4);
9891007
let mask = 0x0000_00ff << shift;
9901008
let prio = u32::from(prio) << shift;

0 commit comments

Comments
 (0)