From cf855b71d2102e63b4a1b49b1b0505d6bf3ebb49 Mon Sep 17 00:00:00 2001 From: "Cliff L. Biffle" Date: Tue, 10 Dec 2024 11:57:18 -0800 Subject: [PATCH] kern: Make stack zap failure less catastrophic --- sys/kern/src/arch/arm_m.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/sys/kern/src/arch/arm_m.rs b/sys/kern/src/arch/arm_m.rs index eef3c9063..c8d70a0dd 100644 --- a/sys/kern/src/arch/arm_m.rs +++ b/sys/kern/src/arch/arm_m.rs @@ -309,15 +309,21 @@ pub fn reinitialize(task: &mut task::Task) { .iter() .find(|region| region.contains(initial_stack.saturating_sub(4))) { - let mut uslice: USlice = USlice::from_raw( + // If the slice doesn't fit in the region, this will fail. Should this + // occur, don't crash the entire system, since this is a diagnostic tool + // -- just skip filling the stack. + if let Ok(mut uslice) = USlice::::from_raw( region.base as usize, (initial_stack - frame_size - region.base as usize) >> 2, - ) - .unwrap_lite(); - - let zap = task.try_write(&mut uslice).unwrap_lite(); - for word in zap.iter_mut() { - *word = 0xbaddcafe; + ) { + // This one, we're unwrapping rather than tolerating failure. This + // is because try_write failing would indicate an invalid region + // descriptor for the task (read-only stack area) which would bite + // us later. + let zap = task.try_write(&mut uslice).unwrap_lite(); + for word in zap.iter_mut() { + *word = 0xbaddcafe; + } } }