Skip to content

Commit

Permalink
Auto merge of #972 - gnzlbg:packed, r=alexcrichton
Browse files Browse the repository at this point in the history
Fix undefined-behavior on MacOSX structs in stdbuilds

Some MacOSX structs have an incorrect layout that results in undefined behavior. This is because on `x86_64` the MacOSX kernel headers define these using `#pragma pack 4`.

This PR fixes their layout using `repr(packed(4))` . Since it is only available on nightly, it is only enabled for stdbuilds .
  • Loading branch information
bors committed Apr 17, 2018
2 parents 735a5a9 + 77837a0 commit 69769fb
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ env:
secure: "e2/3QjgRN9atOuSHp22TrYG7QVKcYUWY48Hi9b60w+r1+BhPkTseIJLte7WefRhdXtqpjjUJTooKDhnurFOeHaCT+nmBgiv+FPU893sBl4bhesY4m0vgUJVbNZcs6lTImYekWVb+aqjGdgV/XAgCw7c3kPmrZV0MzGDWL64Xaps="
matrix:
include:
# 1.0.0 compat
# 1.13.0 compat
- env: TARGET=x86_64-unknown-linux-gnu NO_ADD=1
rust: 1.0.0
rust: 1.13.0
script: rm -f Cargo.lock && cargo build
install:

Expand Down
4 changes: 3 additions & 1 deletion libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ fn main() {
// which is absent in glibc, has to be defined.
"__timeval" if linux => true,

// The alignment of this is 4 on 64-bit OSX...
// Fixed on stdbuild with repr(packed(4))
// Once repr_packed stabilizes we can fix this unconditionally
// and remove this check.
"kevent" | "shmid_ds" if apple && x86_64 => true,

// This is actually a union, not a struct
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

// Attributes needed when building as part of the standard library
#![cfg_attr(feature = "stdbuild", feature(no_std, staged_api, custom_attribute, cfg_target_vendor))]
#![cfg_attr(feature = "stdbuild", feature(link_cfg))]
#![cfg_attr(feature = "stdbuild", feature(link_cfg, repr_packed))]
#![cfg_attr(feature = "stdbuild", no_std)]
#![cfg_attr(feature = "stdbuild", staged_api)]
#![cfg_attr(feature = "stdbuild", allow(warnings))]
Expand Down
4 changes: 2 additions & 2 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ s! {
pub f_reserved: [::uint32_t; 8],
}

// FIXME: this should have align 4 but it's got align 8 on 64-bit
#[cfg_attr(feature = "stdbuild", repr(packed(4)))]
pub struct kevent {
pub ident: ::uintptr_t,
pub filter: ::int16_t,
Expand Down Expand Up @@ -524,7 +524,7 @@ s! {
pub _key: ::key_t,
}

// FIXME: this should have align 4 but it's got align 8 on 64-bit
#[cfg_attr(feature = "stdbuild", repr(packed(4)))]
pub struct shmid_ds {
pub shm_perm: ipc_perm,
pub shm_segsz: ::size_t,
Expand Down

0 comments on commit 69769fb

Please sign in to comment.