1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::{ops, mem, ptr, sync::atomic};

// Holds a string and zeros it when we're done.
pub struct ZeroOnDrop {
    inner: Inner
}

impl ZeroOnDrop {
    pub fn new() -> Self {
        ZeroOnDrop {
            inner: Inner(String::new())
        }
    }

    pub fn into_inner(mut self) -> String {
        mem::replace(&mut self.inner.0, String::new())
    }
}

impl ops::Deref for ZeroOnDrop {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.inner.0
    }
}

impl ops::DerefMut for ZeroOnDrop {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner.0
    }
}

struct Inner(String);

impl Drop for Inner {
    fn drop(&mut self) {
        self.zero_memory();
    }
}

impl Inner {
    /// Sets all bytes of a String to 0
    fn zero_memory(&mut self) {
        let default = u8::default();

        for c in unsafe { self.0.as_bytes_mut() } {
            unsafe { ptr::write_volatile(c, default) };
        }

        atomic::fence(atomic::Ordering::SeqCst);
        atomic::compiler_fence(atomic::Ordering::SeqCst);
    }
}