MCPcopy Create free account
hub / github.com/RustCrypto/utils / optimization_barrier

Function optimization_barrier

zeroize/src/barrier.rs:52–89  ·  view source on GitHub ↗

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`. O

(val: &T)

Source from the content-addressed store, hash-verified

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}

Callers 4

zeroize_stackFunction · 0.85
fill_sliceFunction · 0.85
zeroize_byte_ptrFunction · 0.85
dropMethod · 0.85

Calls

no outgoing calls

Tested by 1

dropMethod · 0.68