From 803e35fd89dd4873996f2a189426e00ba9a7b6d1 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 16 Nov 2026 19:52:25 -0700 Subject: [PATCH] zeroize: extend `asm!` to slice `Zeroize` impl (Re-)adds `fill_slice`, but this time using `fill` followed by `optimization_barrier` on targets that support `asm!`, falling back to volatile writes for ones that don't. This reintroduces some annoying slice-to-pointer-to-slice juggling mainly because `optimization_barrier` has a slice-based API but also to preserve the byte zeroization fast path, particularly for cases that use a pointer cast from another type whose bit pattern is allowed to be all zeros. --- zeroize/src/bulk.rs | 48 +++++++++++++++++++++++++++++++-------------- zeroize/src/lib.rs | 2 +- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/zeroize/src/bulk.rs b/zeroize/src/bulk.rs index ace2ecc2..70c69c3a 100644 --- a/zeroize/src/bulk.rs +++ b/zeroize/src/bulk.rs @@ -1,8 +1,36 @@ -//! Bulk byte zeroization and volatile writes of a repeated `Copy` value. +//! Bulk operations over slices and pointers. -use crate::volatile_write; +use crate::{optimization_barrier, volatile_write}; use core::slice; +/// Whether this target supports an assembly optimization barrier. +const HAS_ASM: bool = cfg!(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", +)); + +/// Fill a slice using an assembly optimization barrier or portable volatile writes. +#[inline] +pub(crate) fn fill_slice(dst: &mut [T], src: T) { + if size_of::() == 0 { + return; + } + + if HAS_ASM { + dst.fill(src); + optimization_barrier(dst); + } else { + volatile_fill_slice(dst, src); + } +} + /// Zero bytes which may be uninitialized, including padding. /// /// # Safety @@ -14,22 +42,12 @@ use core::slice; /// typed access or drop. #[inline] pub(crate) unsafe fn zeroize_byte_ptr(dst: *mut u8, len: usize) { - if cfg!(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", - )) { + if HAS_ASM { // SAFETY: the caller supplies exclusive access to `len` writable bytes. // `write_bytes` initializes every byte before constructing the slice. unsafe { core::ptr::write_bytes(dst, 0, len); - crate::optimization_barrier(slice::from_raw_parts(dst, len)); + optimization_barrier(slice::from_raw_parts(dst, len)); } } else { use core::mem::MaybeUninit; @@ -43,7 +61,7 @@ pub(crate) unsafe fn zeroize_byte_ptr(dst: *mut u8, len: usize) { /// Fill a slice with a repeated value, preserving its type and alignment. #[inline(always)] -pub(crate) fn volatile_fill_slice(dst: &mut [T], src: T) { +fn volatile_fill_slice(dst: &mut [T], src: T) { // Target 16-byte chunks match size_of::() { 0 => {} diff --git a/zeroize/src/lib.rs b/zeroize/src/lib.rs index 6be84015..de53f2b8 100644 --- a/zeroize/src/lib.rs +++ b/zeroize/src/lib.rs @@ -448,7 +448,7 @@ where Z: DefaultIsZeroes, { fn zeroize(&mut self) { - bulk::volatile_fill_slice(self, Z::default()); + bulk::fill_slice(self, Z::default()); } }