Skip to content

Commit 7603eb1

Browse files
committed
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.
1 parent 8c71eed commit 7603eb1

27 files changed

+81
-8
lines changed

src/codegen/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1612,14 +1612,20 @@ impl CodeGenerator for CompInfo {
16121612
);
16131613
}
16141614

1615-
if is_union && !self.can_be_rust_union(ctx) {
1615+
if is_union {
16161616
let layout = layout.expect("Unable to get layout information?");
16171617
let ty = BlobTyBuilder::new(layout).build();
1618-
let field = StructFieldBuilder::named("bindgen_union_field")
1619-
.pub_()
1620-
.build_ty(ty);
1618+
1619+
let field = if self.can_be_rust_union(ctx) {
1620+
StructFieldBuilder::named("_bindgen_union_align")
1621+
.build_ty(ty)
1622+
} else {
1623+
struct_layout.saw_union(layout);
16211624

1622-
struct_layout.saw_union(layout);
1625+
StructFieldBuilder::named("bindgen_union_field")
1626+
.pub_()
1627+
.build_ty(ty)
1628+
};
16231629

16241630
fields.push(field);
16251631
}

tests/expectations/tests/16-byte-alignment.rs

+30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct rte_ipv4_tuple {
1616
pub union rte_ipv4_tuple__bindgen_ty_1 {
1717
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
1818
pub sctp_tag: u32,
19+
_bindgen_union_align: u32,
1920
}
2021
#[repr(C)]
2122
#[derive(Debug, Default, Copy, Hash)]
@@ -108,6 +109,7 @@ pub struct rte_ipv6_tuple {
108109
pub union rte_ipv6_tuple__bindgen_ty_1 {
109110
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
110111
pub sctp_tag: u32,
112+
_bindgen_union_align: u32,
111113
}
112114
#[repr(C)]
113115
#[derive(Debug, Default, Copy, Hash)]
@@ -188,3 +190,31 @@ impl Clone for rte_ipv6_tuple {
188190
impl Default for rte_ipv6_tuple {
189191
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
190192
}
193+
#[repr(C)]
194+
#[derive(Copy)]
195+
pub union rte_thash_tuple {
196+
pub v4: rte_ipv4_tuple,
197+
pub v6: rte_ipv6_tuple,
198+
_bindgen_union_align: [u8; 48usize],
199+
}
200+
#[test]
201+
fn bindgen_test_layout_rte_thash_tuple() {
202+
assert_eq!(::std::mem::size_of::<rte_thash_tuple>() , 48usize , concat ! (
203+
"Size of: " , stringify ! ( rte_thash_tuple ) ));
204+
assert_eq! (unsafe {
205+
& ( * ( 0 as * const rte_thash_tuple ) ) . v4 as * const _ as
206+
usize } , 0usize , concat ! (
207+
"Alignment of field: " , stringify ! ( rte_thash_tuple ) ,
208+
"::" , stringify ! ( v4 ) ));
209+
assert_eq! (unsafe {
210+
& ( * ( 0 as * const rte_thash_tuple ) ) . v6 as * const _ as
211+
usize } , 0usize , concat ! (
212+
"Alignment of field: " , stringify ! ( rte_thash_tuple ) ,
213+
"::" , stringify ! ( v6 ) ));
214+
}
215+
impl Clone for rte_thash_tuple {
216+
fn clone(&self) -> Self { *self }
217+
}
218+
impl Default for rte_thash_tuple {
219+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
220+
}

tests/expectations/tests/anon_struct_in_union.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct s {
1313
#[derive(Copy)]
1414
pub union s__bindgen_ty_1 {
1515
pub field: s__bindgen_ty_1_inner,
16+
_bindgen_union_align: u32,
1617
}
1718
#[repr(C)]
1819
#[derive(Debug, Default, Copy, Hash)]

tests/expectations/tests/anon_union.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct TErrorResult_DOMExceptionInfo {
3030
pub union TErrorResult__bindgen_ty_1 {
3131
pub mMessage: *mut TErrorResult_Message,
3232
pub mDOMExceptionInfo: *mut TErrorResult_DOMExceptionInfo,
33+
_bindgen_union_align: u64,
3334
}
3435
impl Default for TErrorResult__bindgen_ty_1 {
3536
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/class.rs

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ impl Default for IncompleteArrayNonCopiable {
180180
pub union Union {
181181
pub d: f32,
182182
pub i: ::std::os::raw::c_int,
183+
_bindgen_union_align: u32,
183184
}
184185
#[test]
185186
fn bindgen_test_layout_Union() {

tests/expectations/tests/class_with_inner_struct.rs

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl Clone for A_Segment {
4141
#[derive(Copy)]
4242
pub union A__bindgen_ty_1 {
4343
pub f: ::std::os::raw::c_int,
44+
_bindgen_union_align: u32,
4445
}
4546
#[test]
4647
fn bindgen_test_layout_A__bindgen_ty_1() {
@@ -64,6 +65,7 @@ impl Default for A__bindgen_ty_1 {
6465
#[derive(Copy)]
6566
pub union A__bindgen_ty_2 {
6667
pub d: ::std::os::raw::c_int,
68+
_bindgen_union_align: u32,
6769
}
6870
#[test]
6971
fn bindgen_test_layout_A__bindgen_ty_2() {
@@ -169,6 +171,7 @@ pub struct C {
169171
pub union C__bindgen_ty_1 {
170172
pub mFunc: C__bindgen_ty_1__bindgen_ty_1,
171173
pub __bindgen_anon_1: C__bindgen_ty_1__bindgen_ty_2,
174+
_bindgen_union_align: [u32; 4usize],
172175
}
173176
#[repr(C)]
174177
#[derive(Debug, Default, Copy)]

tests/expectations/tests/issue-493.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct basic_string___short {
6363
pub union basic_string___short__bindgen_ty_1 {
6464
pub __size_: ::std::os::raw::c_uchar,
6565
pub __lx: basic_string_value_type,
66+
_bindgen_union_align: u8,
6667
}
6768
impl Default for basic_string___short__bindgen_ty_1 {
6869
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/jsval_layout_opaque.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub union jsval_layout {
8181
pub asPtr: *mut ::std::os::raw::c_void,
8282
pub asWord: usize,
8383
pub asUIntPtr: usize,
84+
_bindgen_union_align: u64,
8485
}
8586
#[repr(C)]
8687
#[derive(Debug, Copy, Hash)]
@@ -199,6 +200,7 @@ pub union jsval_layout__bindgen_ty_2__bindgen_ty_1 {
199200
pub i32: i32,
200201
pub u32: u32,
201202
pub why: JSWhyMagic,
203+
_bindgen_union_align: u32,
202204
}
203205
#[test]
204206
fn bindgen_test_layout_jsval_layout__bindgen_ty_2__bindgen_ty_1() {

tests/expectations/tests/layout_eth_conf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,7 @@ pub union rte_eth_conf__bindgen_ty_2 {
15291529
pub vmdq_dcb_tx_conf: rte_eth_vmdq_dcb_tx_conf,
15301530
pub dcb_tx_conf: rte_eth_dcb_tx_conf,
15311531
pub vmdq_tx_conf: rte_eth_vmdq_tx_conf,
1532+
_bindgen_union_align: [u32; 3usize],
15321533
}
15331534
#[test]
15341535
fn bindgen_test_layout_rte_eth_conf__bindgen_ty_2() {

tests/expectations/tests/layout_mbuf.rs

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub union rte_mbuf__bindgen_ty_1 {
9292
pub refcnt_atomic: rte_atomic16_t,
9393
/// < Non-atomically accessed refcnt
9494
pub refcnt: u16,
95+
_bindgen_union_align: u16,
9596
}
9697
#[test]
9798
fn bindgen_test_layout_rte_mbuf__bindgen_ty_1() {
@@ -124,6 +125,7 @@ pub union rte_mbuf__bindgen_ty_2 {
124125
/// < L2/L3/L4 and tunnel information.
125126
pub packet_type: u32,
126127
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_2__bindgen_ty_1,
128+
_bindgen_union_align: u32,
127129
}
128130
#[repr(C)]
129131
#[derive(Debug, Default, Copy, Hash)]
@@ -462,6 +464,7 @@ pub union rte_mbuf__bindgen_ty_3 {
462464
pub sched: rte_mbuf__bindgen_ty_3__bindgen_ty_2,
463465
/// < User defined tags. See rte_distributor_process()
464466
pub usr: u32,
467+
_bindgen_union_align: [u32; 2usize],
465468
}
466469
#[repr(C)]
467470
#[derive(Copy)]
@@ -474,6 +477,7 @@ pub struct rte_mbuf__bindgen_ty_3__bindgen_ty_1 {
474477
pub union rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1 {
475478
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
476479
pub lo: u32,
480+
_bindgen_union_align: u32,
477481
}
478482
#[repr(C)]
479483
#[derive(Debug, Default, Copy, Hash)]
@@ -639,6 +643,7 @@ pub union rte_mbuf__bindgen_ty_4 {
639643
pub userdata: *mut ::std::os::raw::c_void,
640644
/// < Allow 8-byte userdata on 32-bit
641645
pub udata64: u64,
646+
_bindgen_union_align: u64,
642647
}
643648
#[test]
644649
fn bindgen_test_layout_rte_mbuf__bindgen_ty_4() {
@@ -671,6 +676,7 @@ pub union rte_mbuf__bindgen_ty_5 {
671676
/// < combined for easy fetch
672677
pub tx_offload: u64,
673678
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_5__bindgen_ty_1,
679+
_bindgen_union_align: u64,
674680
}
675681
#[repr(C)]
676682
#[derive(Debug, Default, Copy, Hash)]

tests/expectations/tests/struct_with_anon_union.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct foo {
1414
pub union foo__bindgen_ty_1 {
1515
pub a: ::std::os::raw::c_uint,
1616
pub b: ::std::os::raw::c_ushort,
17+
_bindgen_union_align: u32,
1718
}
1819
#[test]
1920
fn bindgen_test_layout_foo__bindgen_ty_1() {

tests/expectations/tests/struct_with_anon_unnamed_union.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct foo {
1414
pub union foo__bindgen_ty_1 {
1515
pub a: ::std::os::raw::c_uint,
1616
pub b: ::std::os::raw::c_ushort,
17+
_bindgen_union_align: u32,
1718
}
1819
#[test]
1920
fn bindgen_test_layout_foo__bindgen_ty_1() {

tests/expectations/tests/struct_with_nesting.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub union foo__bindgen_ty_1 {
1616
pub b: ::std::os::raw::c_uint,
1717
pub __bindgen_anon_1: foo__bindgen_ty_1__bindgen_ty_1,
1818
pub __bindgen_anon_2: foo__bindgen_ty_1__bindgen_ty_2,
19+
_bindgen_union_align: u32,
1920
}
2021
#[repr(C)]
2122
#[derive(Debug, Default, Copy, Hash)]

tests/expectations/tests/typeref.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct mozilla_StyleShapeSource {
6868
pub union mozilla_StyleShapeSource__bindgen_ty_1 {
6969
pub mPosition: *mut mozilla_Position,
7070
pub mFragmentOrURL: *mut mozilla_FragmentOrURL,
71+
_bindgen_union_align: u64,
7172
}
7273
impl Default for mozilla_StyleShapeSource__bindgen_ty_1 {
7374
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/union-in-ns.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod root {
1212
#[derive(Copy)]
1313
pub union bar {
1414
pub baz: ::std::os::raw::c_int,
15+
_bindgen_union_align: u32,
1516
}
1617
#[test]
1718
fn bindgen_test_layout_bar() {

tests/expectations/tests/union_dtor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
pub union UnionWithDtor {
99
pub mFoo: ::std::os::raw::c_int,
1010
pub mBar: *mut ::std::os::raw::c_void,
11+
_bindgen_union_align: u64,
1112
}
1213
#[test]
1314
fn bindgen_test_layout_UnionWithDtor() {

tests/expectations/tests/union_fields.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub union nsStyleUnion {
1010
pub mInt: ::std::os::raw::c_int,
1111
pub mFloat: f32,
1212
pub mPointer: *mut ::std::os::raw::c_void,
13+
_bindgen_union_align: u64,
1314
}
1415
#[test]
1516
fn bindgen_test_layout_nsStyleUnion() {

tests/expectations/tests/union_template.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct NastyStruct {
1414
pub union NastyStruct__bindgen_ty_1 {
1515
pub mFoo: *mut ::std::os::raw::c_void,
1616
pub mDummy: ::std::os::raw::c_ulong,
17+
_bindgen_union_align: u64,
1718
}
1819
impl Default for NastyStruct__bindgen_ty_1 {
1920
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
@@ -22,6 +23,7 @@ impl Default for NastyStruct__bindgen_ty_1 {
2223
pub union NastyStruct__bindgen_ty_2 {
2324
pub wat: ::std::os::raw::c_short,
2425
pub wut: *mut ::std::os::raw::c_int,
26+
_bindgen_union_align: u64,
2527
}
2628
impl Default for NastyStruct__bindgen_ty_2 {
2729
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
@@ -33,6 +35,7 @@ impl Default for NastyStruct {
3335
pub union Whatever {
3436
pub mTPtr: *mut ::std::os::raw::c_void,
3537
pub mInt: ::std::os::raw::c_int,
38+
_bindgen_union_align: u64,
3639
}
3740
impl Default for Whatever {
3841
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/union_with_anon_struct.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[derive(Copy)]
99
pub union foo {
1010
pub bar: foo__bindgen_ty_1,
11+
_bindgen_union_align: [u32; 2usize],
1112
}
1213
#[repr(C)]
1314
#[derive(Debug, Default, Copy, Hash)]

tests/expectations/tests/union_with_anon_struct_bitfield.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pub union foo {
1010
pub a: ::std::os::raw::c_int,
1111
pub __bindgen_anon_1: foo__bindgen_ty_1,
12+
_bindgen_union_align: u32,
1213
}
1314
#[repr(C)]
1415
#[derive(Debug, Default, Copy, Hash)]

tests/expectations/tests/union_with_anon_union.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
#[derive(Copy)]
99
pub union foo {
1010
pub bar: foo__bindgen_ty_1,
11+
_bindgen_union_align: u32,
1112
}
1213
#[repr(C)]
1314
#[derive(Copy)]
1415
pub union foo__bindgen_ty_1 {
1516
pub a: ::std::os::raw::c_uint,
1617
pub b: ::std::os::raw::c_ushort,
18+
_bindgen_union_align: u32,
1719
}
1820
#[test]
1921
fn bindgen_test_layout_foo__bindgen_ty_1() {

tests/expectations/tests/union_with_anon_unnamed_struct.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pub union pixel {
1010
pub rgba: ::std::os::raw::c_uint,
1111
pub __bindgen_anon_1: pixel__bindgen_ty_1,
12+
_bindgen_union_align: u32,
1213
}
1314
#[repr(C)]
1415
#[derive(Debug, Default, Copy, Hash)]

tests/expectations/tests/union_with_anon_unnamed_union.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
pub union foo {
1010
pub a: ::std::os::raw::c_uint,
1111
pub __bindgen_anon_1: foo__bindgen_ty_1,
12+
_bindgen_union_align: u32,
1213
}
1314
#[repr(C)]
1415
#[derive(Copy)]
1516
pub union foo__bindgen_ty_1 {
1617
pub b: ::std::os::raw::c_ushort,
1718
pub c: ::std::os::raw::c_uchar,
19+
_bindgen_union_align: u16,
1820
}
1921
#[test]
2022
fn bindgen_test_layout_foo__bindgen_ty_1() {

tests/expectations/tests/union_with_big_member.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pub union WithBigArray {
1010
pub a: ::std::os::raw::c_int,
1111
pub b: [::std::os::raw::c_int; 33usize],
12+
_bindgen_union_align: [u32; 33usize],
1213
}
1314
#[test]
1415
fn bindgen_test_layout_WithBigArray() {
@@ -38,6 +39,7 @@ impl Default for WithBigArray {
3839
pub union WithBigArray2 {
3940
pub a: ::std::os::raw::c_int,
4041
pub b: [::std::os::raw::c_char; 33usize],
42+
_bindgen_union_align: [u32; 9usize],
4143
}
4244
#[test]
4345
fn bindgen_test_layout_WithBigArray2() {
@@ -67,6 +69,7 @@ impl Default for WithBigArray2 {
6769
pub union WithBigMember {
6870
pub a: ::std::os::raw::c_int,
6971
pub b: WithBigArray,
72+
_bindgen_union_align: [u32; 33usize],
7073
}
7174
#[test]
7275
fn bindgen_test_layout_WithBigMember() {

tests/expectations/tests/union_with_nesting.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pub union foo {
1010
pub a: ::std::os::raw::c_uint,
1111
pub __bindgen_anon_1: foo__bindgen_ty_1,
12+
_bindgen_union_align: u32,
1213
}
1314
#[repr(C)]
1415
#[derive(Copy)]
@@ -21,6 +22,7 @@ pub struct foo__bindgen_ty_1 {
2122
pub union foo__bindgen_ty_1__bindgen_ty_1 {
2223
pub b1: ::std::os::raw::c_ushort,
2324
pub b2: ::std::os::raw::c_ushort,
25+
_bindgen_union_align: u16,
2426
}
2527
#[test]
2628
fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_1() {
@@ -56,6 +58,7 @@ impl Default for foo__bindgen_ty_1__bindgen_ty_1 {
5658
pub union foo__bindgen_ty_1__bindgen_ty_2 {
5759
pub c1: ::std::os::raw::c_ushort,
5860
pub c2: ::std::os::raw::c_ushort,
61+
_bindgen_union_align: u16,
5962
}
6063
#[test]
6164
fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_2() {

tests/expectations/tests/use-core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl Default for foo {
4545
pub union _bindgen_ty_1 {
4646
pub bar: ::std::os::raw::c_int,
4747
pub baz: ::std::os::raw::c_long,
48+
_bindgen_union_align: u64,
4849
}
4950
#[test]
5051
fn bindgen_test_layout__bindgen_ty_1() {

0 commit comments

Comments
 (0)