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

Accesses to fields with packed type fail alignment check #352

Closed
RalfJung opened this issue Sep 27, 2017 · 1 comment · Fixed by #431
Closed

Accesses to fields with packed type fail alignment check #352

RalfJung opened this issue Sep 27, 2017 · 1 comment · Fixed by #431

Comments

@RalfJung
Copy link
Member

RalfJung commented Sep 27, 2017

The following function fails to execute in miri, raising an alignment error:

fn test_inner_packed() {
    // Even if just the inner struct is packed, accesses to the outer field can get unaligned
    #[repr(packed)]
    #[derive(Clone,Copy)]
    struct Inner(u32);
    struct Outer(u8, Inner);

    let o = Outer(0, Inner(42)); // error: tried to access memory with alignment 1, but alignment 4 is required
    let _x = o.1;
    let _y = (o.1).0;
}

The problem is that the 2nd field of Outer is unaligned because it's type is repr(packed). This came as a surprise to me, I thought repr(packed) would only influence the layout within this type, not the layout of other types that just involve this type.

How far does this go? For example, if I have a local of type Inner, do I even still have alignment guarantees for that local?

(It is amazing how much time I spend thinking about alignment problems in miri, and how many problems there are. Why is this all so complicated?!?)

@RalfJung
Copy link
Member Author

The problem is that the 2nd field of Outer is unaligned because it's type is repr(packed). This came as a surprise to me, I thought repr(packed) would only influence the layout within this type, not the layout of other types that just involve this type.

In the mean time, I learned that repr(packed) actually has two effects -- it changes the layout inside the struct, and it changes the alignment of the struct to 1. So this somehow makes more sense to me now.

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 a pull request may close this issue.

1 participant