Fix unsafe unaligned loads in test.#39682
Merged
bors merged 2 commits intorust-lang:masterfrom Feb 10, 2017
solson:fix-unaligned-read
Merged
Fix unsafe unaligned loads in test.#39682bors merged 2 commits intorust-lang:masterfrom solson:fix-unaligned-read
bors merged 2 commits intorust-lang:masterfrom
solson:fix-unaligned-read
Conversation
Member
|
@bors r+ |
Collaborator
|
📌 Commit 2589f4a has been approved by |
frewsxcv
added a commit
to frewsxcv/rust
that referenced
this pull request
Feb 9, 2017
Fix unsafe unaligned loads in test. r? @eddyb cc @Aatch @nikomatsakis The `#[derive(PartialEq, Debug)]` impls on a packed struct contain undefined behaviour. Both generated impls take references to unaligned fields, which will fail to compile once we correctly treat that as unsafe (see rust-lang#27060). This UB was found by running the test under [Miri](https://github.com/solson/miri/) which rejects these unsafe unaligned loads. 😄 Here's a simpler example: ```rust struct Packed { a: u8, b: u64, } ``` It expands to: ```rust fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match *self { Packed { a: ref __self_0_0, b: ref __self_0_1 } => { // BAD: these patterns are unsafe let mut builder = __arg_0.debug_struct("Packed"); let _ = builder.field("a", &&(*__self_0_0)); let _ = builder.field("b", &&(*__self_0_1)); builder.finish() } } } ``` and ```rust fn eq(&self, __arg_0: &Packed) -> bool { match *__arg_0 { Packed { a: ref __self_1_0, b: ref __self_1_1 } => // BAD: these patterns are unsafe match *self { Packed { a: ref __self_0_0, b: ref __self_0_1 } => // BAD: these patterns are unsafe true && (*__self_0_0) == (*__self_1_0) && (*__self_0_1) == (*__self_1_1), }, } } ```
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
r? @eddyb
cc @Aatch @nikomatsakis
The
#[derive(PartialEq, Debug)]impls on a packed struct contain undefined behaviour. Both generated impls take references to unaligned fields, which will fail to compile once we correctly treat that as unsafe (see #27060).This UB was found by running the test under Miri which rejects these unsafe unaligned loads. 😄
Here's a simpler example:
It expands to:
and