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

Using implicit padding on C FFI prevents Rust from writing / copying those bits #1324

Open
gnzlbg opened this issue Apr 20, 2019 · 2 comments
Labels
E-medium E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate.
Milestone

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 20, 2019

@sunfishcode pointed out here (#1321 (comment)) that when we use explicit padding on C FFI:

struct Foo {
   _pad: u32;
  pub field: u16
}

Rust will insert copies, etc. for the padding bits, and when users write code like:

let foo = MaybeUninit::<Foo>::zeroed().assume_init();

those fields will be zeroed.

However, if we were to use

#[repr(align(4))] 
struct Foo {
    pub field: u16,
}

instead, this would not be the case.

We don't have that many types using repr(align) and repr(packed) explicitly, so we should manually check those against the C headers, and verify that they only use these attributes when C does so as well.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Apr 20, 2019

Either way, one way to make Rust code "forward-compatible" with fixes that would require adding / removing private fields would be to add a private ZST to the type. That would prevent people from assuming that all field types will always be public, which is what leads to breaking changes when it comes to padding / alignment.

@tgross35 tgross35 added the E-medium E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. label Aug 29, 2024
@tgross35 tgross35 added this to the 1.0 milestone Aug 29, 2024
@tgross35
Copy link
Contributor

#[non_exhaustive] would work here too and probably be more clear #4080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-medium E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate.
Projects
None yet
Development

No branches or pull requests

2 participants