Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions zeroize/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.9.1 (UNRELEASED)
### Fixed
- False positive failures in proxy allocator test ([#1538])

[#1538]: https://github.com/RustCrypto/utils/pull/1538

## 1.9.0 (2026-06-12)
### Added
- `Zeroizing` is now `repr(transparent)` ([#1253])
Expand Down
55 changes: 37 additions & 18 deletions zeroize/tests/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
//! Tests for `Zeroize` impls on heap-allocated data structures

#![allow(clippy::std_instead_of_core, clippy::undocumented_unsafe_blocks)]

use std::alloc::{GlobalAlloc, Layout, System};
use core::{
alloc::{GlobalAlloc, Layout},
ptr,
sync::atomic::{AtomicPtr, Ordering::Relaxed},
};
use zeroize::Zeroize;

// Allocator that ensures that deallocated data is zeroized.
use std::alloc::System;

static REG_PTR: AtomicPtr<u8> = AtomicPtr::new(ptr::null_mut());

// Allocator that ensures that allocation registered in `REG_PTR` is zeroized.
struct ProxyAllocator;

#[allow(clippy::undocumented_unsafe_blocks)]
unsafe impl GlobalAlloc for ProxyAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe { System.alloc(layout) }
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
if layout.size() == 160 {
if ptr == REG_PTR.load(Relaxed) {
for i in 0..layout.size() {
let b = unsafe { core::ptr::read(ptr.add(i)) };
let b = unsafe { ptr::read(ptr.add(i)) };
assert_eq!(b, 0);
}
REG_PTR.store(ptr::null_mut(), Relaxed);
}

unsafe { System.dealloc(ptr, layout) }
Expand All @@ -32,7 +40,11 @@ struct SecretBox<S: Zeroize>(Box<S>);

impl<S: Zeroize> SecretBox<S> {
fn new(val: S) -> Self {
Self(Box::new(val))
let mut b = Box::new(val);
// TODO(MSRV-1.98): use `Box::as_mut_ptr`
let p: *mut S = &raw mut *b;
REG_PTR.store(p.cast(), Relaxed);
Self(b)
}
}

Expand All @@ -42,19 +54,15 @@ impl<S: Zeroize> Drop for SecretBox<S> {
}
}

#[test]
fn secret_box_alloc_test() {
let b1 = SecretBox::new([u128::MAX; 10]);
core::hint::black_box(&b1);
let b2 = SecretBox::new([u8::MAX; 160]);
core::hint::black_box(&b2);
}

struct ObserveSecretBox<S: Default>(Box<S>);

impl<S: Default> ObserveSecretBox<S> {
fn new(val: S) -> Self {
Self(Box::new(val))
let mut b = Box::new(val);
// TODO(MSRV-1.98): use `Box::as_mut_ptr`
let p: *mut S = &raw mut *b;
REG_PTR.store(p.cast(), Relaxed);
Self(b)
}
}

Expand All @@ -66,9 +74,20 @@ impl<S: Default> Drop for ObserveSecretBox<S> {
}

#[test]
fn observe_secret_box_alloc_test() {
let b1 = ObserveSecretBox::new([u128::MAX; 10]);
fn proxy_alloc_test() {
let b1 = SecretBox::new([u128::MAX; 10]);
core::hint::black_box(&b1);
drop(b1);

let b2 = SecretBox::new([u8::MAX; 160]);
core::hint::black_box(&b2);
drop(b2);

let b3 = ObserveSecretBox::new([u128::MAX; 10]);
core::hint::black_box(&b3);
drop(b3);

let b4 = SecretBox::new([u8::MAX; 160]);
core::hint::black_box(&b4);
drop(b4);
}