zeroize/barrier.rs
1/// Observe the referenced data and prevent the compiler from removing previous writes to it.
2///
3/// This function acts like [`core::hint::black_box`] but takes a reference and
4/// does not return the passed value.
5///
6/// It's implemented using the [`core::arch::asm!`] macro on target arches where `asm!` is stable,
7/// i.e. `aarch64`, `arm`, `arm64ec`, `loongarch64`, `riscv32`, `riscv64`, `s390x`, `x86`, and
8/// `x86_64`.
9///
10/// On all other targets it's implemented using [`core::hint::black_box`].
11///
12/// # Examples
13/// ```
14/// use core::num::NonZeroU32;
15/// use zeroize::{ZeroizeOnDrop, zeroize_flat_type};
16///
17/// # type ThirdPartyType = u32;
18///
19/// struct DataToZeroize {
20/// buf: [u8; 32],
21/// // `ThirdPartyType` can be a type with private fields
22/// // defined in a third-party crate and which does not implement
23/// // `Zeroize` or zeroization on drop.
24/// data: ThirdPartyType,
25/// pos: NonZeroU32,
26/// }
27///
28/// struct SomeMoreFlatData(u64);
29///
30/// impl Drop for DataToZeroize {
31/// fn drop(&mut self) {
32/// self.buf = [0u8; 32];
33/// self.data = ThirdPartyType::default();
34/// self.pos = NonZeroU32::new(32).unwrap();
35/// zeroize::optimization_barrier(self);
36/// }
37/// }
38///
39/// impl zeroize::ZeroizeOnDrop for DataToZeroize {}
40///
41/// let mut data = DataToZeroize {
42/// buf: [3u8; 32],
43/// data: ThirdPartyType::default(),
44/// pos: NonZeroU32::new(32).unwrap(),
45/// };
46///
47/// // data gets zeroized when dropped
48/// ```
49///
50/// Note that erasure of `ThirdPartyType` demonstrated above can be fragile if it contains
51/// `MaybeUninit` or `union` data. It also does not perform erasure of types like `Box` or `Vec`.
52pub fn optimization_barrier<T: ?Sized>(val: &T) {
53 #[cfg(all(
54 not(miri),
55 any(
56 target_arch = "aarch64",
57 target_arch = "arm",
58 target_arch = "arm64ec",
59 target_arch = "loongarch64",
60 target_arch = "riscv32",
61 target_arch = "riscv64",
62 target_arch = "s390x",
63 target_arch = "x86",
64 target_arch = "x86_64",
65 )
66 ))]
67 unsafe {
68 core::arch::asm!(
69 "# {}",
70 in(reg) core::ptr::from_ref::<T>(val).cast::<()>(),
71 options(readonly, preserves_flags, nostack),
72 );
73 }
74 #[cfg(not(all(
75 not(miri),
76 any(
77 target_arch = "aarch64",
78 target_arch = "arm",
79 target_arch = "arm64ec",
80 target_arch = "loongarch64",
81 target_arch = "riscv32",
82 target_arch = "riscv64",
83 target_arch = "s390x",
84 target_arch = "x86",
85 target_arch = "x86_64",
86 )
87 )))]
88 core::hint::black_box(val);
89}