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

Fix unsafe unaligned loads in test. #39682

Merged
merged 2 commits into from
Feb 10, 2017
Merged

Fix unsafe unaligned loads in test. #39682

merged 2 commits into from
Feb 10, 2017

Conversation

solson
Copy link
Member

@solson solson commented Feb 9, 2017

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:

#[repr(packed)]
#[derive(PartialEq, Debug)]
struct Packed {
    a: u8,
    b: u64,
}

It expands to:

    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

    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),
            },
        }
    }

@eddyb
Copy link
Member

eddyb commented Feb 9, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Feb 9, 2017

📌 Commit 2589f4a has been approved by eddyb

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),
            },
        }
    }
```
bors added a commit that referenced this pull request Feb 9, 2017
Rollup of 6 pull requests

- Successful merges: #39604, #39619, #39670, #39678, #39682, #39683
- Failed merges:
@bors bors merged commit 2589f4a into rust-lang:master Feb 10, 2017
@solson solson deleted the fix-unaligned-read branch February 10, 2017 00:48
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

Successfully merging this pull request may close these issues.

3 participants