From 1ecf0eab9181cad646f5859c78c45ff5568a5f7d Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 22 Mar 2021 23:21:52 +0000 Subject: [PATCH] added clflush after explicit_bzero on x86 nightly builds --- Cargo.toml | 3 +++ build.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 2 ++ src/stream.rs | 8 ++++++++ 4 files changed, 37 insertions(+) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index ba99f99..9647fc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,6 @@ base64 = "0.13" getrandom = "0.2" openssl = "0.10" smallvec = {version = "1.6", features=["union"], optional = true} + +[build-dependencies] +rustc_version = "0.2" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..6399463 --- /dev/null +++ b/build.rs @@ -0,0 +1,24 @@ + +extern crate rustc_version; +use rustc_version::{version, version_meta, Channel}; + +fn main() { + // Assert we haven't travelled back in time + assert!(version().unwrap().major >= 1); + + // Set cfg flags depending on release channel + match version_meta().unwrap().channel { + Channel::Stable => { + println!("cargo:rustc-cfg=stable"); + } + Channel::Beta => { + println!("cargo:rustc-cfg=beta"); + } + Channel::Nightly => { + println!("cargo:rustc-cfg=nightly"); + } + Channel::Dev => { + println!("cargo:rustc-cfg=dev"); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 710995f..21cae37 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(nightly, feature(asm))] + #![allow(dead_code)] //extern crate test; diff --git a/src/stream.rs b/src/stream.rs index d29eb45..09581f1 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -128,6 +128,14 @@ where W: Write } unsafe { explicit_bzero(self.buffer.as_mut_ptr() as *mut c_void, self.buffer.len()); + + #[cfg(nightly)] + if cfg!(target_arch = "x86_64") || cfg!(target_arch = "x86"){ + asm!( + "clflush [{}]", + in(reg) self.buffer.as_mut_ptr() + ); + } } return; }