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

Deriving uniffi::Record together with ZeroizeOnDrop does not compile. Solvable? #2080

Closed
Sajjon opened this issue Apr 23, 2024 · 5 comments
Closed

Comments

@Sajjon
Copy link
Contributor

Sajjon commented Apr 23, 2024

I've recently tried to add Zeroize on my UniFFI records / enums, like so:

#[derive(
    Zeroize,
    ZeroizeOnDrop,
    Clone,
    uniffi::Record,
)]
pub struct Mnemonic {
    pub words: Vec<BIP39Word>, // BIP39Word: Zeroize, Clone, uniffi::Record
}

Results in compilation error:

cannot move out of type `Mnemonic`, which implements the `Drop` trait
move occurs because `obj.words` has type `Vec<BIP39Word>`, which does not implement the `Copy` trait

Hmm.... is this even solvable?

@jplatte
Copy link
Collaborator

jplatte commented Apr 23, 2024

UniFFI could certainly generate code that works without destructuring, but for zeroize in particular I think you'd be defeating the purpose of that derive since UniFFI would be creating copies of the bytes that don't get zeroized on drop.

@Sajjon
Copy link
Contributor Author

Sajjon commented Apr 23, 2024

but for zeroize in particular I think you'd be defeating the purpose of that derive since UniFFI would be creating copies of the bytes that don't get zeroized on drop.

Right that is what I feared, but maybe some optimization could be done in "that realm", e.g. if UniFFI could check if the Source or Target implements Zeroize and could use it on any copies, so that only the the resulting Target / Source contains those bytes, but any "ephemeral copies" are zeroed out during the conversion?

@jplatte
Copy link
Collaborator

jplatte commented Apr 24, 2024

It's impossible for a macro to check whether a trait is implemented, trait solving happens after macro expansion (trait solving depends on the output of all macros in a given source tree incl. dependencies).

It would be possible to introduce #[uniffi(zeroize)] as a helper attribute though. My hunch is that there's probably no appetite for it by the maintainers, but maybe you're lucky 😅

@Sajjon
Copy link
Contributor Author

Sajjon commented Apr 24, 2024

It's impossible for a macro to check whether a trait is implemented, trait solving happens after macro expansion (trait solving depends on the output of all macros in a given source tree incl. dependencies).

Right that makes sense, thanks for explaining @jplatte ! I'm still fairly new to Rust (started in December last year) and have only beeng doing macros since February.

It would be possible to introduce #[uniffi(zeroize)] as a helper attribute though

Yes that would really be amazing! I might try to give a PR a go later on!

@mhammond
Copy link
Member

I'd suggest you start with a justification first - I can only speak for myself, but @jplatte is correct regarding appetite - I doubt we want to support every cool-sounding crate out there. In this specific case, the hurdle to overcome is already noted above - uniffi makes many copies of records with the expectation that many of them live in the foreign code - in these scenarios, zeroize seems, frankly, pointless.

@mhammond mhammond closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants