getrandom/utils/sanitizer.rs
1use core::mem::MaybeUninit;
2
3/// Unpoisons `buf` if MSAN support is enabled.
4///
5/// Most backends do not need to unpoison their output. Rust language- and
6/// library- provided functionality unpoisons automatically. Similarly, libc
7/// either natively supports MSAN and/or MSAN hooks libc-provided functions
8/// to unpoison outputs on success. Only when all of these things are
9/// bypassed do we need to do it ourselves.
10///
11/// The call to unpoison should be done as close to the write as possible.
12/// For example, if the backend partially fills the output buffer in chunks,
13/// each chunk should be unpoisoned individually. This way, the correctness of
14/// the chunking logic can be validated (in part) using MSAN.
15pub unsafe fn unpoison(buf: &mut [MaybeUninit<u8>]) {
16 cfg_if! {
17 if #[cfg(getrandom_msan)] {
18 unsafe extern "C" {
19 fn __msan_unpoison(a: *mut core::ffi::c_void, size: usize);
20 }
21 let a = buf.as_mut_ptr().cast();
22 let size = buf.len();
23 unsafe { __msan_unpoison(a, size) };
24 } else {
25 let _ = buf;
26 }
27 }
28}