add volatile_clear() to xous_ipc::String for password fields

Call this on Drop() to ensure that the string is cleared.
Written using volatile pointers and compiler fences to ensure
the clear is not optimized out or re-ordered.
This commit is contained in:
bunnie 2021-07-26 18:12:13 +08:00
parent 53f35f9928
commit 42a1193522

View file

@ -20,6 +20,19 @@ impl<const N: usize> String<N> {
}
}
// use a volatile write to ensure a clear operation is not optimized out
// for ensuring that a string is cleared, e.g. at the exit of a function
pub fn volatile_clear(&mut self) {
let b = self.bytes.as_mut_ptr();
for i in 0..N {
unsafe{b.add(i).write_volatile(core::mem::zeroed());}
}
// Ensure the compiler doesn't re-order the clear.
// We use `SeqCst`, because `Acquire` only prevents later accesses from being reordered before
// *reads*, but this method only *writes* to the locations.
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
pub fn from_str<T>(src: T) -> String<N> where T: AsRef<str> {
let src = src.as_ref();
let mut s = Self::new();