-
Notifications
You must be signed in to change notification settings - Fork 149
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
Merged
+105
−20
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f03a14
zeroize: replace `atomic_fence` with `optimization_barrier`
newpavlov fd684b4
move `optimization_barrier` into a separate module
newpavlov 3ccf121
Harden `optimization_barrier` for non-`asm!` targets
newpavlov 4202bd8
tweak docs
newpavlov dc55d3f
Mark `optimization_barrier` as `pub(crate)`
newpavlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(crate) 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", | ||
| ) | ||
| ))] | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
cfg!work here so you could just do if/else rather than duplicating the architecture list?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will not work since it would result in compilation of the
asm!macro on targets which haven't stabilized it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we would use
cfg-if, but it's probably not worth to add a dependency just for it.Hopefully, one day we will get a macro for this as part of
core.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cfg_selectis currently undergoing stabilization :)