-
Notifications
You must be signed in to change notification settings - Fork 150
zeroize: replace atomic_fence with optimization_barrier
#1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
1f03a14
fd684b4
3ccf121
4202bd8
dc55d3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /// Observe the referenced data and prevent the compiler from removing previous writes to it. | ||
| /// | ||
| /// This function acts like [`core::hint::black_box`] but takes a reference and | ||
| /// does not return the passed value. | ||
| /// | ||
| /// It's implemented using the [`core::arch::asm!`] macro on target arches where `asm!` is stable, | ||
| /// i.e. `aarch64`, `arm`, `arm64ec`, `loongarch64`, `riscv32`, `riscv64`, `s390x`, `x86`, and | ||
| /// `x86_64`. | ||
| /// | ||
| /// On all other targets it's implemented using [`core::hint::black_box`] and custom `black_box` | ||
| /// implemented using `#[inline(never)]` and `read_volatile`. | ||
| /// | ||
| /// # Examples | ||
| /// ```ignore | ||
| /// use core::num::NonZeroU32; | ||
| /// use zeroize::{ZeroizeOnDrop, zeroize_flat_type}; | ||
| /// | ||
| /// struct DataToZeroize { | ||
| /// buf: [u8; 32], | ||
| /// pos: NonZeroU32, | ||
| /// } | ||
| /// | ||
| /// struct SomeMoreFlatData(u64); | ||
| /// | ||
| /// impl Drop for DataToZeroize { | ||
| /// fn drop(&mut self) { | ||
| /// self.buf = [0u8; 32]; | ||
| /// self.pos = NonZeroU32::new(32).unwrap(); | ||
| /// zeroize::optimization_barrier(self); | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// impl zeroize::ZeroizeOnDrop for DataToZeroize {} | ||
| /// | ||
| /// let mut data = DataToZeroize { | ||
| /// buf: [3u8; 32], | ||
| /// pos: NonZeroU32::new(32).unwrap(), | ||
| /// }; | ||
| /// | ||
| /// // data gets zeroized when dropped | ||
| /// ``` | ||
| pub fn optimization_barrier<T: ?Sized>(val: &T) { | ||
| #[cfg(all( | ||
| not(miri), | ||
| any( | ||
| target_arch = "aarch64", | ||
| target_arch = "arm", | ||
| target_arch = "arm64ec", | ||
| target_arch = "loongarch64", | ||
| target_arch = "riscv32", | ||
| target_arch = "riscv64", | ||
| target_arch = "s390x", | ||
| target_arch = "x86", | ||
| target_arch = "x86_64", | ||
| ) | ||
| ))] | ||
|
Comment on lines
+43
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will not work since it would result in compilation of the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, we would use Hopefully, one day we will get a macro for this as part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| unsafe { | ||
| core::arch::asm!( | ||
| "# {}", | ||
| in(reg) val as *const T as *const (), | ||
| options(readonly, preserves_flags, nostack), | ||
| ); | ||
| } | ||
| #[cfg(not(all( | ||
| not(miri), | ||
| any( | ||
| target_arch = "aarch64", | ||
| target_arch = "arm", | ||
| target_arch = "arm64ec", | ||
| target_arch = "loongarch64", | ||
| target_arch = "riscv32", | ||
| target_arch = "riscv64", | ||
| target_arch = "s390x", | ||
| target_arch = "x86", | ||
| target_arch = "x86_64", | ||
| ) | ||
| )))] | ||
| { | ||
| /// Custom version of `core::hint::black_box` implemented using | ||
| /// `#[inline(never)]` and `read_volatile`. | ||
| #[inline(never)] | ||
| fn custom_black_box(p: *const u8) { | ||
| let _ = unsafe { core::ptr::read_volatile(p) }; | ||
| } | ||
|
|
||
| core::hint::black_box(val); | ||
| if size_of_val(val) > 0 { | ||
| custom_black_box(val as *const T as *const u8); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.