Skip to content

Commit

Permalink
Auto merge of #909 - bkchr:union_fix, r=emilio
Browse files Browse the repository at this point in the history
Fixes alignment errors with new Rust union type

This fix creates a new private field with the required aligned size. This new
private field ensures that the union has the required size.

This fixes: #908
  • Loading branch information
bors-servo authored Aug 14, 2017
2 parents 5d53f85 + 7603eb1 commit f9087f3
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,14 +1612,20 @@ impl CodeGenerator for CompInfo {
);
}

if is_union && !self.can_be_rust_union(ctx) {
if is_union {
let layout = layout.expect("Unable to get layout information?");
let ty = BlobTyBuilder::new(layout).build();
let field = StructFieldBuilder::named("bindgen_union_field")
.pub_()
.build_ty(ty);

let field = if self.can_be_rust_union(ctx) {
StructFieldBuilder::named("_bindgen_union_align")
.build_ty(ty)
} else {
struct_layout.saw_union(layout);

struct_layout.saw_union(layout);
StructFieldBuilder::named("bindgen_union_field")
.pub_()
.build_ty(ty)
};

fields.push(field);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/expectations/tests/16-byte-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct rte_ipv4_tuple {
pub union rte_ipv4_tuple__bindgen_ty_1 {
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
pub sctp_tag: u32,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down Expand Up @@ -108,6 +109,7 @@ pub struct rte_ipv6_tuple {
pub union rte_ipv6_tuple__bindgen_ty_1 {
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
pub sctp_tag: u32,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down Expand Up @@ -188,3 +190,31 @@ impl Clone for rte_ipv6_tuple {
impl Default for rte_ipv6_tuple {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
#[derive(Copy)]
pub union rte_thash_tuple {
pub v4: rte_ipv4_tuple,
pub v6: rte_ipv6_tuple,
_bindgen_union_align: [u8; 48usize],
}
#[test]
fn bindgen_test_layout_rte_thash_tuple() {
assert_eq!(::std::mem::size_of::<rte_thash_tuple>() , 48usize , concat ! (
"Size of: " , stringify ! ( rte_thash_tuple ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_thash_tuple ) ) . v4 as * const _ as
usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( rte_thash_tuple ) ,
"::" , stringify ! ( v4 ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_thash_tuple ) ) . v6 as * const _ as
usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( rte_thash_tuple ) ,
"::" , stringify ! ( v6 ) ));
}
impl Clone for rte_thash_tuple {
fn clone(&self) -> Self { *self }
}
impl Default for rte_thash_tuple {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
1 change: 1 addition & 0 deletions tests/expectations/tests/anon_struct_in_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct s {
#[derive(Copy)]
pub union s__bindgen_ty_1 {
pub field: s__bindgen_ty_1_inner,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/anon_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct TErrorResult_DOMExceptionInfo {
pub union TErrorResult__bindgen_ty_1 {
pub mMessage: *mut TErrorResult_Message,
pub mDOMExceptionInfo: *mut TErrorResult_DOMExceptionInfo,
_bindgen_union_align: u64,
}
impl Default for TErrorResult__bindgen_ty_1 {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Default for IncompleteArrayNonCopiable {
pub union Union {
pub d: f32,
pub i: ::std::os::raw::c_int,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_Union() {
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations/tests/class_with_inner_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Clone for A_Segment {
#[derive(Copy)]
pub union A__bindgen_ty_1 {
pub f: ::std::os::raw::c_int,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_A__bindgen_ty_1() {
Expand All @@ -64,6 +65,7 @@ impl Default for A__bindgen_ty_1 {
#[derive(Copy)]
pub union A__bindgen_ty_2 {
pub d: ::std::os::raw::c_int,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_A__bindgen_ty_2() {
Expand Down Expand Up @@ -169,6 +171,7 @@ pub struct C {
pub union C__bindgen_ty_1 {
pub mFunc: C__bindgen_ty_1__bindgen_ty_1,
pub __bindgen_anon_1: C__bindgen_ty_1__bindgen_ty_2,
_bindgen_union_align: [u32; 4usize],
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/issue-493.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct basic_string___short {
pub union basic_string___short__bindgen_ty_1 {
pub __size_: ::std::os::raw::c_uchar,
pub __lx: basic_string_value_type,
_bindgen_union_align: u8,
}
impl Default for basic_string___short__bindgen_ty_1 {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/jsval_layout_opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub union jsval_layout {
pub asPtr: *mut ::std::os::raw::c_void,
pub asWord: usize,
pub asUIntPtr: usize,
_bindgen_union_align: u64,
}
#[repr(C)]
#[derive(Debug, Copy, Hash)]
Expand Down Expand Up @@ -199,6 +200,7 @@ pub union jsval_layout__bindgen_ty_2__bindgen_ty_1 {
pub i32: i32,
pub u32: u32,
pub why: JSWhyMagic,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_jsval_layout__bindgen_ty_2__bindgen_ty_1() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/layout_eth_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,7 @@ pub union rte_eth_conf__bindgen_ty_2 {
pub vmdq_dcb_tx_conf: rte_eth_vmdq_dcb_tx_conf,
pub dcb_tx_conf: rte_eth_dcb_tx_conf,
pub vmdq_tx_conf: rte_eth_vmdq_tx_conf,
_bindgen_union_align: [u32; 3usize],
}
#[test]
fn bindgen_test_layout_rte_eth_conf__bindgen_ty_2() {
Expand Down
6 changes: 6 additions & 0 deletions tests/expectations/tests/layout_mbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub union rte_mbuf__bindgen_ty_1 {
pub refcnt_atomic: rte_atomic16_t,
/// < Non-atomically accessed refcnt
pub refcnt: u16,
_bindgen_union_align: u16,
}
#[test]
fn bindgen_test_layout_rte_mbuf__bindgen_ty_1() {
Expand Down Expand Up @@ -124,6 +125,7 @@ pub union rte_mbuf__bindgen_ty_2 {
/// < L2/L3/L4 and tunnel information.
pub packet_type: u32,
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_2__bindgen_ty_1,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down Expand Up @@ -462,6 +464,7 @@ pub union rte_mbuf__bindgen_ty_3 {
pub sched: rte_mbuf__bindgen_ty_3__bindgen_ty_2,
/// < User defined tags. See rte_distributor_process()
pub usr: u32,
_bindgen_union_align: [u32; 2usize],
}
#[repr(C)]
#[derive(Copy)]
Expand All @@ -474,6 +477,7 @@ pub struct rte_mbuf__bindgen_ty_3__bindgen_ty_1 {
pub union rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1 {
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
pub lo: u32,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down Expand Up @@ -639,6 +643,7 @@ pub union rte_mbuf__bindgen_ty_4 {
pub userdata: *mut ::std::os::raw::c_void,
/// < Allow 8-byte userdata on 32-bit
pub udata64: u64,
_bindgen_union_align: u64,
}
#[test]
fn bindgen_test_layout_rte_mbuf__bindgen_ty_4() {
Expand Down Expand Up @@ -671,6 +676,7 @@ pub union rte_mbuf__bindgen_ty_5 {
/// < combined for easy fetch
pub tx_offload: u64,
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_5__bindgen_ty_1,
_bindgen_union_align: u64,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/struct_with_anon_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct foo {
pub union foo__bindgen_ty_1 {
pub a: ::std::os::raw::c_uint,
pub b: ::std::os::raw::c_ushort,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/struct_with_anon_unnamed_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct foo {
pub union foo__bindgen_ty_1 {
pub a: ::std::os::raw::c_uint,
pub b: ::std::os::raw::c_ushort,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/struct_with_nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub union foo__bindgen_ty_1 {
pub b: ::std::os::raw::c_uint,
pub __bindgen_anon_1: foo__bindgen_ty_1__bindgen_ty_1,
pub __bindgen_anon_2: foo__bindgen_ty_1__bindgen_ty_2,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct mozilla_StyleShapeSource {
pub union mozilla_StyleShapeSource__bindgen_ty_1 {
pub mPosition: *mut mozilla_Position,
pub mFragmentOrURL: *mut mozilla_FragmentOrURL,
_bindgen_union_align: u64,
}
impl Default for mozilla_StyleShapeSource__bindgen_ty_1 {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/union-in-ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod root {
#[derive(Copy)]
pub union bar {
pub baz: ::std::os::raw::c_int,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_bar() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/union_dtor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
pub union UnionWithDtor {
pub mFoo: ::std::os::raw::c_int,
pub mBar: *mut ::std::os::raw::c_void,
_bindgen_union_align: u64,
}
#[test]
fn bindgen_test_layout_UnionWithDtor() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/union_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub union nsStyleUnion {
pub mInt: ::std::os::raw::c_int,
pub mFloat: f32,
pub mPointer: *mut ::std::os::raw::c_void,
_bindgen_union_align: u64,
}
#[test]
fn bindgen_test_layout_nsStyleUnion() {
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations/tests/union_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct NastyStruct {
pub union NastyStruct__bindgen_ty_1 {
pub mFoo: *mut ::std::os::raw::c_void,
pub mDummy: ::std::os::raw::c_ulong,
_bindgen_union_align: u64,
}
impl Default for NastyStruct__bindgen_ty_1 {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -22,6 +23,7 @@ impl Default for NastyStruct__bindgen_ty_1 {
pub union NastyStruct__bindgen_ty_2 {
pub wat: ::std::os::raw::c_short,
pub wut: *mut ::std::os::raw::c_int,
_bindgen_union_align: u64,
}
impl Default for NastyStruct__bindgen_ty_2 {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -33,6 +35,7 @@ impl Default for NastyStruct {
pub union Whatever {
pub mTPtr: *mut ::std::os::raw::c_void,
pub mInt: ::std::os::raw::c_int,
_bindgen_union_align: u64,
}
impl Default for Whatever {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/union_with_anon_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Copy)]
pub union foo {
pub bar: foo__bindgen_ty_1,
_bindgen_union_align: [u32; 2usize],
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub union foo {
pub a: ::std::os::raw::c_int,
pub __bindgen_anon_1: foo__bindgen_ty_1,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/union_with_anon_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#[derive(Copy)]
pub union foo {
pub bar: foo__bindgen_ty_1,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Copy)]
pub union foo__bindgen_ty_1 {
pub a: ::std::os::raw::c_uint,
pub b: ::std::os::raw::c_ushort,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/union_with_anon_unnamed_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub union pixel {
pub rgba: ::std::os::raw::c_uint,
pub __bindgen_anon_1: pixel__bindgen_ty_1,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash)]
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/union_with_anon_unnamed_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
pub union foo {
pub a: ::std::os::raw::c_uint,
pub __bindgen_anon_1: foo__bindgen_ty_1,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Copy)]
pub union foo__bindgen_ty_1 {
pub b: ::std::os::raw::c_ushort,
pub c: ::std::os::raw::c_uchar,
_bindgen_union_align: u16,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1() {
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations/tests/union_with_big_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub union WithBigArray {
pub a: ::std::os::raw::c_int,
pub b: [::std::os::raw::c_int; 33usize],
_bindgen_union_align: [u32; 33usize],
}
#[test]
fn bindgen_test_layout_WithBigArray() {
Expand Down Expand Up @@ -38,6 +39,7 @@ impl Default for WithBigArray {
pub union WithBigArray2 {
pub a: ::std::os::raw::c_int,
pub b: [::std::os::raw::c_char; 33usize],
_bindgen_union_align: [u32; 9usize],
}
#[test]
fn bindgen_test_layout_WithBigArray2() {
Expand Down Expand Up @@ -67,6 +69,7 @@ impl Default for WithBigArray2 {
pub union WithBigMember {
pub a: ::std::os::raw::c_int,
pub b: WithBigArray,
_bindgen_union_align: [u32; 33usize],
}
#[test]
fn bindgen_test_layout_WithBigMember() {
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations/tests/union_with_nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub union foo {
pub a: ::std::os::raw::c_uint,
pub __bindgen_anon_1: foo__bindgen_ty_1,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Copy)]
Expand All @@ -21,6 +22,7 @@ pub struct foo__bindgen_ty_1 {
pub union foo__bindgen_ty_1__bindgen_ty_1 {
pub b1: ::std::os::raw::c_ushort,
pub b2: ::std::os::raw::c_ushort,
_bindgen_union_align: u16,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_1() {
Expand Down Expand Up @@ -56,6 +58,7 @@ impl Default for foo__bindgen_ty_1__bindgen_ty_1 {
pub union foo__bindgen_ty_1__bindgen_ty_2 {
pub c1: ::std::os::raw::c_ushort,
pub c2: ::std::os::raw::c_ushort,
_bindgen_union_align: u16,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_2() {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/use-core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Default for foo {
pub union _bindgen_ty_1 {
pub bar: ::std::os::raw::c_int,
pub baz: ::std::os::raw::c_long,
_bindgen_union_align: u64,
}
#[test]
fn bindgen_test_layout__bindgen_ty_1() {
Expand Down
Loading

0 comments on commit f9087f3

Please sign in to comment.