Skip to content

Commit 67bba5a

Browse files
authored
zeroize: mark optimization_barrier as public (#1261)
The function was added in #1252.
1 parent 2a35660 commit 67bba5a

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

zeroize/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## 1.9.0 (unreleased)
8+
### Added
9+
- `optimization_barrier` function ([#1261])
10+
811
### Changed
912
- Edition changed to 2024 and MSRV bumped to 1.85 ([#1149])
1013

1114
[#1149]: https://github.com/RustCrypto/utils/pull/1149
15+
[#1261]: https://github.com/RustCrypto/utils/pull/1261
1216

1317
## 1.8.2 (2025-09-29)
1418
### Changed

zeroize/src/barrier.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
/// implemented using `#[inline(never)]` and `read_volatile`.
1212
///
1313
/// # Examples
14-
/// ```ignore
14+
/// ```
1515
/// use core::num::NonZeroU32;
1616
/// use zeroize::{ZeroizeOnDrop, zeroize_flat_type};
1717
///
18+
/// # type ThirdPartyType = u32;
19+
///
1820
/// struct DataToZeroize {
1921
/// buf: [u8; 32],
22+
/// // `ThirdPartyType` can be a type with private fields
23+
/// // defined in a third-party crate and which does not implement
24+
/// // `Zeroize` or zeroization on drop.
25+
/// data: ThirdPartyType,
2026
/// pos: NonZeroU32,
2127
/// }
2228
///
@@ -25,6 +31,7 @@
2531
/// impl Drop for DataToZeroize {
2632
/// fn drop(&mut self) {
2733
/// self.buf = [0u8; 32];
34+
/// self.data = ThirdPartyType::default();
2835
/// self.pos = NonZeroU32::new(32).unwrap();
2936
/// zeroize::optimization_barrier(self);
3037
/// }
@@ -34,12 +41,16 @@
3441
///
3542
/// let mut data = DataToZeroize {
3643
/// buf: [3u8; 32],
44+
/// data: ThirdPartyType::default(),
3745
/// pos: NonZeroU32::new(32).unwrap(),
3846
/// };
3947
///
4048
/// // data gets zeroized when dropped
4149
/// ```
42-
pub(crate) fn optimization_barrier<T: ?Sized>(val: &T) {
50+
///
51+
/// Note that erasure of `ThirdPartyType` demonstrated above can be fragile if it contains
52+
/// `MaybeUninit` or `union` data. It also does not perform erasure of types like `Box` or `Vec`.
53+
pub fn optimization_barrier<T: ?Sized>(val: &T) {
4354
#[cfg(all(
4455
not(miri),
4556
any(

zeroize/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ mod aarch64;
251251
mod x86;
252252

253253
mod barrier;
254-
use barrier::optimization_barrier;
254+
pub use barrier::optimization_barrier;
255255

256256
use core::{
257257
marker::{PhantomData, PhantomPinned},

zeroize/tests/alloc.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,32 @@ impl<S: Zeroize> Drop for SecretBox<S> {
4242
}
4343

4444
#[test]
45-
fn proxy_alloc_test() {
45+
fn secret_box_alloc_test() {
4646
let b1 = SecretBox::new([u128::MAX; 10]);
4747
core::hint::black_box(&b1);
4848
let b2 = SecretBox::new([u8::MAX; 160]);
4949
core::hint::black_box(&b2);
5050
}
51+
52+
struct ObserveSecretBox<S: Default>(Box<S>);
53+
54+
impl<S: Default> ObserveSecretBox<S> {
55+
fn new(val: S) -> Self {
56+
Self(Box::new(val))
57+
}
58+
}
59+
60+
impl<S: Default> Drop for ObserveSecretBox<S> {
61+
fn drop(&mut self) {
62+
*self.0 = Default::default();
63+
zeroize::optimization_barrier(&self);
64+
}
65+
}
66+
67+
#[test]
68+
fn observe_secret_box_alloc_test() {
69+
let b1 = ObserveSecretBox::new([u128::MAX; 10]);
70+
core::hint::black_box(&b1);
71+
let b2 = SecretBox::new([u8::MAX; 160]);
72+
core::hint::black_box(&b2);
73+
}

0 commit comments

Comments
 (0)