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

Transmuting &mut MaybeUninit::zeroed() to &mut [u8] is unsound #2319

Open
1 of 2 tasks
kupiakos opened this issue Feb 7, 2025 · 0 comments
Open
1 of 2 tasks

Transmuting &mut MaybeUninit::zeroed() to &mut [u8] is unsound #2319

kupiakos opened this issue Feb 7, 2025 · 0 comments

Comments

@kupiakos
Copy link
Contributor

kupiakos commented Feb 7, 2025

Progress


These days, MaybeUninit::zeroed is pretty clear that you cannot expect all padding bytes to be zero in the output value (same for mem::zeroed). Zerocopy mentions this, but it doesn't seem fully internalized by the library:

  • FromZeros::zero contradicts this, stating that it's similar to *self = Self::new_zeroed() and that the difference is it doesn't semantically drop anything. This isn't the only difference! zero is capable of fully zeroing all of the bytes of an object until it is moved into, while *self = Self::new_zeroed() doesn't guarantee you can then soundly access as a byte slice unless that Self: IntoBytes.
  • FromZeros::new_zeroed doesn't mention the same caveat as MaybeUninit::zeroed at all even though it applies.
  • The current implementation of FromBytes::read_from_io is unsound, since there may still be padding bytes for !IntoBytes types from new_zeroed. It should be constructing a MaybeUninit::uninit() then zeroing its bytes before casting to &mut [u8].

I'll send a PR fixing the unsoundness issue. I'm also interested in improving the documentation to be more consistent and to provide clear working alternatives to documented unsound operations.

@kupiakos kupiakos changed the title Transmuting MaybeUninit::zeroed() to &mut [u8] is unsound Transmuting &mut MaybeUninit::zeroed() to &mut [u8] is unsound Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 7, 2025
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 8, 2025
See google#2319. Includes a Miri test confirming the previous unsoundness.
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 8, 2025
See google#2319. Backport of google#2320.
Includes a Miri test confirming the previous unsoundness.
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 8, 2025
See google#2319. Backport of google#2320.
Includes a Miri test confirming the previous unsoundness.
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 8, 2025
See google#2319. Includes a Miri test confirming the previous unsoundness.
kupiakos added a commit to kupiakos/zerocopy that referenced this issue Feb 17, 2025
See google#2319. Includes a Miri test confirming the previous unsoundness.
joshlf pushed a commit to kupiakos/zerocopy that referenced this issue Feb 19, 2025
See google#2319. Includes a Miri test confirming the previous unsoundness.

gherrit-pr-id: Iede94c196c710c74d970c93935f1539e07446e50
joshlf pushed a commit to kupiakos/zerocopy that referenced this issue Feb 19, 2025
See google#2319. Includes a Miri test confirming the previous unsoundness.

gherrit-pr-id: Iede94c196c710c74d970c93935f1539e07446e50
google-pr-creation-bot pushed a commit to google-pr-creation-bot/zerocopy that referenced this issue Feb 19, 2025
See google#2319. Includes a Miri test confirming the previous unsoundness.

gherrit-pr-id: Iede94c196c710c74d970c93935f1539e07446e50
github-merge-queue bot pushed a commit that referenced this issue Feb 19, 2025
See #2319. Includes a Miri test confirming the previous unsoundness.

gherrit-pr-id: Iede94c196c710c74d970c93935f1539e07446e50
github-merge-queue bot pushed a commit that referenced this issue Feb 19, 2025
See #2319. Includes a Miri test confirming the previous unsoundness.

gherrit-pr-id: Iede94c196c710c74d970c93935f1539e07446e50

Co-authored-by: Alyssa Haroldsen <[email protected]>
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

1 participant