|
205 | 205 | //! result, our code should correctly handle it and return an error like
|
206 | 206 | //! [`Error::UNEXPECTED`].
|
207 | 207 | //!
|
| 208 | +//! ## Sanitizer support |
| 209 | +//! |
| 210 | +//! If your code uses `getrandom_uninit` and you use memory sanitizer |
| 211 | +//! (i.e. `-Zsanitizer=memory`), then you need to pass `getrandom_sanitize` |
| 212 | +//! configuration flag for `getrandom_uninit` to unpoison destination buffer. |
| 213 | +//! |
| 214 | +//! For example, it can be done like this (requires Nightly compiler): |
| 215 | +//! ```text |
| 216 | +//! RUSTFLAGS="-Zsanitizer=memory --cfg getrandom_sanitize" cargo test -Zbuild-std --target=x86_64-unknown-linux-gnu |
| 217 | +//! ``` |
| 218 | +//! |
208 | 219 | //! [1]: https://manned.org/getrandom.2
|
209 | 220 | //! [2]: https://manned.org/urandom.4
|
210 | 221 | //! [3]: https://www.unix.com/man-page/mojave/2/getentropy/
|
|
254 | 265 | #![no_std]
|
255 | 266 | #![warn(rust_2018_idioms, unused_lifetimes, missing_docs)]
|
256 | 267 | #![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
| 268 | +#![cfg_attr(getrandom_sanitize, feature(cfg_sanitize))] |
257 | 269 | #![deny(
|
258 | 270 | clippy::cast_lossless,
|
259 | 271 | clippy::cast_possible_truncation,
|
@@ -474,7 +486,20 @@ pub fn getrandom_uninit(dest: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error
|
474 | 486 | if !dest.is_empty() {
|
475 | 487 | imp::getrandom_inner(dest)?;
|
476 | 488 | }
|
| 489 | + |
| 490 | + #[cfg(getrandom_sanitize)] |
| 491 | + #[cfg(sanitize = "memory")] |
| 492 | + extern "C" { |
| 493 | + fn __msan_unpoison(a: *mut core::ffi::c_void, size: usize); |
| 494 | + } |
| 495 | + |
477 | 496 | // SAFETY: `dest` has been fully initialized by `imp::getrandom_inner`
|
478 | 497 | // since it returned `Ok`.
|
479 |
| - Ok(unsafe { slice_assume_init_mut(dest) }) |
| 498 | + Ok(unsafe { |
| 499 | + #[cfg(getrandom_sanitize)] |
| 500 | + #[cfg(sanitize = "memory")] |
| 501 | + __msan_unpoison(dest.as_mut_ptr().cast(), dest.len()); |
| 502 | + |
| 503 | + slice_assume_init_mut(dest) |
| 504 | + }) |
480 | 505 | }
|
0 commit comments