Skip to content

Commit e8fa876

Browse files
committed
zeroize: remove internal use of optimization_barrier
We replaced our previous use of compiler fences with `optimization_barrier` under the assumption it would be a zero-cost abstraction, but per #1504 that is not the case as it caused a performance regression. The use of either of these was effectively redundant and a belt-and-suspenders defense as volatile writes alone are already sufficient to guarantee zeroization will not be removed by the compiler. This removes the use of `optimization_barrier` to restore the previous performance. Closes #1504.
1 parent a8897c9 commit e8fa876

3 files changed

Lines changed: 1 addition & 12 deletions

File tree

‎zeroize/src/aarch64.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ macro_rules! impl_zeroize_for_simd_register {
1111
#[inline]
1212
fn zeroize(&mut self) {
1313
volatile_write(self, unsafe { core::mem::zeroed() });
14-
optimization_barrier(self);
1514
}
1615
}
1716
)+

‎zeroize/src/lib.rs‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ where
273273
{
274274
fn zeroize(&mut self) {
275275
volatile_write(self, Z::default());
276-
optimization_barrier(self);
277276
}
278277
}
279278

@@ -307,7 +306,6 @@ macro_rules! impl_zeroize_for_non_zero {
307306
None => unreachable!(),
308307
};
309308
volatile_write(self, ONE);
310-
optimization_barrier(self);
311309
}
312310
}
313311
)+
@@ -401,8 +399,6 @@ where
401399
// already done semantically. Any value which needed to be dropped will have been
402400
// done so by take().
403401
unsafe { ptr::write_volatile(self, None) }
404-
405-
optimization_barrier(self);
406402
}
407403
}
408404

@@ -415,10 +411,8 @@ impl<Z> ZeroizeOnDrop for Option<Z> where Z: ZeroizeOnDrop {}
415411
/// [`MaybeUninit`] removes all invariants.
416412
impl<Z> Zeroize for MaybeUninit<Z> {
417413
fn zeroize(&mut self) {
418-
// Safety:
419-
// `MaybeUninit` is valid for any byte pattern, including zeros.
414+
// SAFETY: `MaybeUninit` is valid for any byte pattern, including zeros.
420415
unsafe { ptr::write_volatile(self, MaybeUninit::zeroed()) }
421-
optimization_barrier(self);
422416
}
423417
}
424418

@@ -444,7 +438,6 @@ impl<Z> Zeroize for [MaybeUninit<Z>] {
444438
// and 0 is a valid value for `MaybeUninit<Z>`
445439
// The memory of the slice should not wrap around the address space.
446440
unsafe { volatile_set(ptr, MaybeUninit::zeroed(), size) }
447-
optimization_barrier(self);
448441
}
449442
}
450443

@@ -470,7 +463,6 @@ where
470463
// `self.len()` is also not larger than an `isize`, because of the assertion above.
471464
// The memory of the slice should not wrap around the address space.
472465
unsafe { volatile_set(self.as_mut_ptr(), Z::default(), self.len()) };
473-
optimization_barrier(self);
474466
}
475467
}
476468

@@ -826,7 +818,6 @@ pub unsafe fn zeroize_flat_type<F: Sized>(data: *mut F) {
826818
unsafe {
827819
volatile_set(data.cast::<u8>(), 0, size);
828820
}
829-
optimization_barrier(&data);
830821
}
831822

832823
/// Internal module used as support for `AssertZeroizeOnDrop`.

‎zeroize/src/x86.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ macro_rules! impl_zeroize_for_simd_register {
1414
#[inline]
1515
fn zeroize(&mut self) {
1616
volatile_write(self, unsafe { core::mem::zeroed() });
17-
optimization_barrier(self);
1817
}
1918
}
2019
)*

0 commit comments

Comments
 (0)