Skip to content

Commit 7f1c63a

Browse files
committed
Improvements
1 parent 38baefc commit 7f1c63a

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

esp-hal/src/debugger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn debugger_connected() -> bool {
2828
///
2929
/// Breakpoint 0 is used.
3030
///
31-
/// # Safety:
31+
/// # Safety
3232
/// The address must be word aligned.
3333
pub unsafe fn set_stack_watchpoint(addr: usize) {
3434
assert!(addr.is_multiple_of(4));

esp-hal/src/exception_handler/mod.rs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ pub(crate) fn breakpoint_interrupt(context: &TrapFrame) {
2525
);
2626
}
2727

28+
#[cfg(stack_guard_monitoring)]
2829
if dbgcause & 4 != 0 && dbgcause & 0b1111_0000_0000 == 0 {
2930
panic!(
3031
"\n\nDetected a write to the stack guard value on {:?}\n{:?}",
3132
crate::system::Cpu::current(),
3233
context
3334
);
34-
} else {
35-
panic!(
36-
"\n\nBreakpoint on {:?}\n{:?}\nDebug cause: {}",
37-
crate::system::Cpu::current(),
38-
context,
39-
dbgcause
40-
);
4135
}
36+
37+
panic!(
38+
"\n\nBreakpoint on {:?}\n{:?}\nDebug cause: {}",
39+
crate::system::Cpu::current(),
40+
context,
41+
dbgcause
42+
);
4243
}
4344

4445
#[cfg(riscv)]
@@ -52,42 +53,52 @@ unsafe extern "C" fn ExceptionHandler(context: &TrapFrame) -> ! {
5253
static mut __stack_chk_guard: u32;
5354
}
5455

55-
let guard_addr = core::ptr::addr_of_mut!(__stack_chk_guard) as *mut _ as usize;
56-
5756
if code == 14 {
5857
panic!(
5958
"Stack overflow detected at 0x{:x}, possibly called by 0x{:x}",
6059
mepc, context.ra
6160
);
62-
} else if code == 3 && mtval == guard_addr {
63-
panic!(
64-
"Detected a write to the stack guard value at 0x{:x}, possibly called by 0x{:x}",
65-
mepc, context.ra
66-
)
67-
} else {
68-
let code = match code {
69-
0 => "Instruction address misaligned",
70-
1 => "Instruction access fault",
71-
2 => "Illegal instruction",
72-
3 => "Breakpoint",
73-
4 => "Load address misaligned",
74-
5 => "Load access fault",
75-
6 => "Store/AMO address misaligned",
76-
7 => "Store/AMO access fault",
77-
8 => "Environment call from U-mode",
78-
9 => "Environment call from S-mode",
79-
10 => "Reserved",
80-
11 => "Environment call from M-mode",
81-
12 => "Instruction page fault",
82-
13 => "Load page fault",
83-
14 => "Reserved",
84-
15 => "Store/AMO page fault",
85-
_ => "UNKNOWN",
86-
};
61+
}
8762

88-
panic!(
89-
"Exception '{}' mepc=0x{:08x}, mtval=0x{:08x}\n{:?}",
90-
code, mepc, mtval, context
91-
);
63+
#[cfg(stack_guard_monitoring)]
64+
if code == 3 {
65+
let guard_addr = core::ptr::addr_of_mut!(__stack_chk_guard) as *mut _ as usize;
66+
67+
if mtval == guard_addr {
68+
panic!(
69+
"Detected a write to the main stack's guard value at 0x{:x}, possibly called by 0x{:x}",
70+
mepc, context.ra
71+
)
72+
} else {
73+
panic!(
74+
"Breakpoint exception at 0x{:08x}, mtval=0x{:08x}\n{:?}",
75+
mepc, mtval, context
76+
);
77+
}
9278
}
79+
80+
let code = match code {
81+
0 => "Instruction address misaligned",
82+
1 => "Instruction access fault",
83+
2 => "Illegal instruction",
84+
3 => "Breakpoint",
85+
4 => "Load address misaligned",
86+
5 => "Load access fault",
87+
6 => "Store/AMO address misaligned",
88+
7 => "Store/AMO access fault",
89+
8 => "Environment call from U-mode",
90+
9 => "Environment call from S-mode",
91+
10 => "Reserved",
92+
11 => "Environment call from M-mode",
93+
12 => "Instruction page fault",
94+
13 => "Load page fault",
95+
14 => "Reserved",
96+
15 => "Store/AMO page fault",
97+
_ => "UNKNOWN",
98+
};
99+
100+
panic!(
101+
"Exception '{}' mepc=0x{:08x}, mtval=0x{:08x}\n{:?}",
102+
code, mepc, mtval, context
103+
);
93104
}

esp-hal/src/soc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn setup_stack_guard() {
247247
}
248248
}
249249

250-
#[cfg(feature = "rt")]
250+
#[cfg(all(feature = "rt", stack_guard_monitoring))]
251251
pub(crate) fn enable_main_stack_guard_monitoring() {
252252
unsafe {
253253
unsafe extern "C" {

0 commit comments

Comments
 (0)