From 9f53cf93522aad21eb8b65c03fe44310e93d99ad Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 23 Mar 2021 02:43:55 +0000 Subject: [PATCH] remove nonstandard glibc dependancy on nightly builds --- README.md | 4 ++-- src/stream.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2db2493..93d6907 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ Alternatively, run `./test.sh` after building to test the release build's correc To enable explicit buffer clearing, compile with the option `--features explicit_clear`. The `explicit_clear` feature forces any temporary work buffers to be zeroed out in memory when the corresponding stream is flushed itself. -It requires the nonstandard glibc extension `explicit_bzero(void*, size_t)` to build. -On x86 targets with the Rust nightly toolchain installed, this will also force a cache flush of the corresponding memory address. +Unless being built with the Rust nightly toolchain, it requires the nonstandard glibc extension `explicit_bzero(void*, size_t)` to build. +On x86 targets with the Rust nightly toolchain installed, it will also force a cache flush of the corresponding memory address when the stream is flushed. This is *usually not needed*, and can cause a slowdown; but it prevents any lingering data being left in the buffer. The unit test `remainder()` checks the process' memory map for leftover data in the working buffer when testing with this feature enabled. It is still unlikely data will remain even without this feature, depending on your system; you should only use it if you are very paranoid. diff --git a/src/stream.rs b/src/stream.rs index df96f91..153d4e2 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -102,6 +102,15 @@ where W: Write #[cfg(feature="explicit_clear")] { use std::ffi::c_void; + + #[cfg(nightly)] + #[inline(never)] unsafe fn explicit_bzero(p: *mut c_void, s: usize) + { + std::ptr::write_bytes(p, 0, s); + + asm!(""); + } + #[cfg(not(nightly))] extern "C" { fn explicit_bzero(_: *mut c_void, _:usize); }