Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroizeOnDrop auto-deref #700

Merged
merged 1 commit into from Jan 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions zeroize/derive/src/lib.rs
Expand Up @@ -69,11 +69,13 @@ fn derive_zeroize(mut s: synstructure::Structure<'_>) -> TokenStream {

/// Custom derive for `ZeroizeOnDrop`
fn derive_zeroize_on_drop(mut s: synstructure::Structure<'_>) -> TokenStream {
let zeroizers = generate_fields(&mut s);
let zeroizers = generate_fields(&mut s, quote! { zeroize_or_on_drop });

let drop_impl = s.gen_impl(quote! {
gen impl Drop for @Self {
fn drop(&mut self) {
use zeroize::__internal::AssertZeroize;
use zeroize::__internal::AssertZeroizeOnDrop;
match self {
#zeroizers
}
Expand Down Expand Up @@ -251,7 +253,7 @@ impl ZeroizeAttrs {
}
}

fn generate_fields(s: &mut synstructure::Structure<'_>) -> TokenStream {
fn generate_fields(s: &mut synstructure::Structure<'_>, method: TokenStream) -> TokenStream {
s.bind_with(|_| BindStyle::RefMut);

s.filter_variants(|vi| {
Expand All @@ -265,7 +267,7 @@ fn generate_fields(s: &mut synstructure::Structure<'_>) -> TokenStream {
result
})
.filter(|bi| filter_skip(&bi.ast().attrs, true))
.each(|bi| quote! { #bi.zeroize(); })
.each(|bi| quote! { #bi.#method(); })
}

fn filter_skip(attrs: &[Attribute], start: bool) -> bool {
Expand All @@ -291,7 +293,7 @@ fn filter_skip(attrs: &[Attribute], start: bool) -> bool {

/// Custom derive for `Zeroize` (without `Drop`)
fn derive_zeroize_without_drop(mut s: synstructure::Structure<'_>) -> TokenStream {
let zeroizers = generate_fields(&mut s);
let zeroizers = generate_fields(&mut s, quote! { zeroize });

s.bound_impl(
quote!(zeroize::Zeroize),
Expand Down Expand Up @@ -507,15 +509,17 @@ mod tests {
const _DERIVE_Drop_FOR_Z: () = {
impl Drop for Z {
fn drop(&mut self) {
use zeroize::__internal::AssertZeroize;
use zeroize::__internal::AssertZeroizeOnDrop;
match self {
Z {
a: ref mut __binding_0,
b: ref mut __binding_1,
c: ref mut __binding_2,
} => {
{ __binding_0.zeroize(); }
{ __binding_1.zeroize(); }
{ __binding_2.zeroize(); }
{ __binding_0.zeroize_or_on_drop(); }
{ __binding_1.zeroize_or_on_drop(); }
{ __binding_2.zeroize_or_on_drop(); }
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions zeroize/src/lib.rs
Expand Up @@ -263,6 +263,31 @@ pub trait Zeroize {
#[allow(drop_bounds)]
pub trait ZeroizeOnDrop: Drop {}

#[doc(hidden)]
pub mod __internal {
use super::*;

/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
pub trait AssertZeroizeOnDrop {
fn zeroize_or_on_drop(&mut self);
}

impl<T: ZeroizeOnDrop> AssertZeroizeOnDrop for &mut T {
fn zeroize_or_on_drop(&mut self) {}
}

/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
pub trait AssertZeroize {
fn zeroize_or_on_drop(&mut self);
}

impl<T: Zeroize> AssertZeroize for T {
fn zeroize_or_on_drop(&mut self) {
self.zeroize()
}
}
}

/// Marker trait for types whose `Default` is the desired zeroization result
pub trait DefaultIsZeroes: Copy + Default + Sized {}

Expand Down