-
Notifications
You must be signed in to change notification settings - Fork 424
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
fill_via_chunks: use safe code via chunks_exact_mut on BE #1180
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9684ebf
fill_via_chunks: use safe code via chunks_exact_mut on BE
dhardy 9972046
fill_via_chunks: make a generic function
dhardy 93ade1a
fill_via_chunks: better value names
dhardy 19b7a76
fill_via_chunks: make ToLe an unsafe trait
dhardy 34a8f13
Replace ToLe with Observable, including as_byte_slice method
dhardy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this means the trait has to be unsafe. How would a safe implementation violating this contract look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An implementation of
ToLe
for any type that could have padding (e.g. anyrepr(Rust)
struct), which may be uninitialized, would be unsound becausefill_via_chunks
is safe and could expose the uninitialized data on a little-endian target.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ralith an unsound implementation of
ToLe
cannot be implemented withoutunsafe
, can it? A trait isunsafe
to indicate that due to the way the trait is used, soundness requires that the impl maintain certain invariants (on top of the standard invariants of safe Rust), which I don't see to be the case here. If usage ofToLe
is sound as long as the implementation is sound, it's a regular trait.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An unsound impl is trivial:
The key is that
fill_via_chunks
doesn't useto_le_bytes
on little-endian targets, it just relies on the unsafe guarantee made by the trait.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ralith
Code dependent on
to_le_bytes
does not use anyunsafe
, so it does not make any assumptions aboutToLe
which may cause UB. Your impl will simply panic onchunk.copy_from_slice(src[i].to_le_bytes().as_ref());
, since chunk size will not be equal to thesrc
array. You simply can not expose the padding bytes without usingunsafe
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reiterate, on little-endian targets,
fill_via_chunks
does not invoketo_le_bytes
ever, instead usingunsafe
to access theToLe
implementer's representation directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Ralith. There's a subtlety here in that the trait is serving dual roles as a (unsafe) marker trait, and a regular trait. @dhardy maybe we could use a comment that specifically mentions that memcpy is sometimes used in lieu of the given implementation, as that does open a can of worms. Worms that are certainly worth that much performance gain, but worms nonetheless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't put quite so much effort into this trait since it is supposed to change again in the next breaking release, but sure. Perhaps it should be renamed
Observable
with extended documentation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new method to the trait, moving the usage of
unsafe
into the trait. Benchmarks are unaffected.