Skip to content

Commit 412de3c

Browse files
committed
generate test case for memory layout
1 parent 754f6e6 commit 412de3c

File tree

64 files changed

+610
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+610
-0
lines changed

src/codegen/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1285,11 +1285,40 @@ impl CodeGenerator for CompInfo {
12851285
::$prefix::mem::align_of::<$ident>());
12861286
let size = layout.size;
12871287
let align = layout.align;
1288+
1289+
let check_field_offset = if item.is_opaque(ctx) ||
1290+
self.base_members().iter().filter(|base| ctx.resolve_type(base.ty).has_vtable(ctx)).count() > 1 {
1291+
None
1292+
} else {
1293+
let type_name = aster::AstBuilder::new().id(&canonical_name);
1294+
1295+
let asserts = self.fields()
1296+
.iter()
1297+
.filter(|field| field.bitfield().is_none())
1298+
.flat_map(|field|
1299+
{
1300+
if let Some(name) = field.name() {
1301+
let field_offset = field.offset() / 8;
1302+
let field_name = aster::AstBuilder::new().id(ctx.rust_mangle(name).into_owned());
1303+
1304+
quote_item!(ctx.ext_cx(),
1305+
assert_eq!($field_offset, unsafe { &(*(0 as *const $type_name)).$field_name as *const _ as usize });
1306+
)
1307+
} else {
1308+
None
1309+
}
1310+
}).collect::<Vec<P<ast::Item>>>();
1311+
1312+
Some(asserts)
1313+
};
1314+
12881315
let item = quote_item!(ctx.ext_cx(),
12891316
#[test]
12901317
fn $fn_name() {
12911318
assert_eq!($size_of_expr, $size);
12921319
assert_eq!($align_of_expr, $align);
1320+
1321+
$check_field_offset
12931322
})
12941323
.unwrap();
12951324
result.push(item);

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

+34
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
5454
, 4usize);
5555
assert_eq!(::std::mem::align_of::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>()
5656
, 2usize);
57+
assert_eq! (0usize , unsafe {
58+
& (
59+
* ( 0 as * const rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 )
60+
) . dport as * const _ as usize });
61+
assert_eq! (2usize , unsafe {
62+
& (
63+
* ( 0 as * const rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 )
64+
) . sport as * const _ as usize });
5765
}
5866
impl Clone for rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
5967
fn clone(&self) -> Self { *self }
@@ -64,6 +72,9 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
6472
4usize);
6573
assert_eq!(::std::mem::align_of::<rte_ipv4_tuple__bindgen_ty_1>() ,
6674
4usize);
75+
assert_eq! (0usize , unsafe {
76+
& ( * ( 0 as * const rte_ipv4_tuple__bindgen_ty_1 ) ) .
77+
sctp_tag as * const _ as usize });
6778
}
6879
impl Clone for rte_ipv4_tuple__bindgen_ty_1 {
6980
fn clone(&self) -> Self { *self }
@@ -72,6 +83,12 @@ impl Clone for rte_ipv4_tuple__bindgen_ty_1 {
7283
fn bindgen_test_layout_rte_ipv4_tuple() {
7384
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple>() , 12usize);
7485
assert_eq!(::std::mem::align_of::<rte_ipv4_tuple>() , 4usize);
86+
assert_eq! (0usize , unsafe {
87+
& ( * ( 0 as * const rte_ipv4_tuple ) ) . src_addr as * const
88+
_ as usize });
89+
assert_eq! (4usize , unsafe {
90+
& ( * ( 0 as * const rte_ipv4_tuple ) ) . dst_addr as * const
91+
_ as usize });
7592
}
7693
impl Clone for rte_ipv4_tuple {
7794
fn clone(&self) -> Self { *self }
@@ -102,6 +119,14 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
102119
, 4usize);
103120
assert_eq!(::std::mem::align_of::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>()
104121
, 2usize);
122+
assert_eq! (0usize , unsafe {
123+
& (
124+
* ( 0 as * const rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 )
125+
) . dport as * const _ as usize });
126+
assert_eq! (2usize , unsafe {
127+
& (
128+
* ( 0 as * const rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 )
129+
) . sport as * const _ as usize });
105130
}
106131
impl Clone for rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
107132
fn clone(&self) -> Self { *self }
@@ -112,6 +137,9 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
112137
4usize);
113138
assert_eq!(::std::mem::align_of::<rte_ipv6_tuple__bindgen_ty_1>() ,
114139
4usize);
140+
assert_eq! (0usize , unsafe {
141+
& ( * ( 0 as * const rte_ipv6_tuple__bindgen_ty_1 ) ) .
142+
sctp_tag as * const _ as usize });
115143
}
116144
impl Clone for rte_ipv6_tuple__bindgen_ty_1 {
117145
fn clone(&self) -> Self { *self }
@@ -120,6 +148,12 @@ impl Clone for rte_ipv6_tuple__bindgen_ty_1 {
120148
fn bindgen_test_layout_rte_ipv6_tuple() {
121149
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple>() , 36usize);
122150
assert_eq!(::std::mem::align_of::<rte_ipv6_tuple>() , 4usize);
151+
assert_eq! (0usize , unsafe {
152+
& ( * ( 0 as * const rte_ipv6_tuple ) ) . src_addr as * const
153+
_ as usize });
154+
assert_eq! (16usize , unsafe {
155+
& ( * ( 0 as * const rte_ipv6_tuple ) ) . dst_addr as * const
156+
_ as usize });
123157
}
124158
impl Clone for rte_ipv6_tuple {
125159
fn clone(&self) -> Self { *self }

tests/expectations/tests/accessors.rs

+42
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ pub struct SomeAccessors {
1919
fn bindgen_test_layout_SomeAccessors() {
2020
assert_eq!(::std::mem::size_of::<SomeAccessors>() , 16usize);
2121
assert_eq!(::std::mem::align_of::<SomeAccessors>() , 4usize);
22+
assert_eq! (0usize , unsafe {
23+
& ( * ( 0 as * const SomeAccessors ) ) . mNoAccessor as *
24+
const _ as usize });
25+
assert_eq! (4usize , unsafe {
26+
& ( * ( 0 as * const SomeAccessors ) ) . mBothAccessors as *
27+
const _ as usize });
28+
assert_eq! (8usize , unsafe {
29+
& ( * ( 0 as * const SomeAccessors ) ) . mUnsafeAccessors as *
30+
const _ as usize });
31+
assert_eq! (12usize , unsafe {
32+
& ( * ( 0 as * const SomeAccessors ) ) . mImmutableAccessor as
33+
* const _ as usize });
2234
}
2335
impl Clone for SomeAccessors {
2436
fn clone(&self) -> Self { *self }
@@ -57,6 +69,12 @@ pub struct AllAccessors {
5769
fn bindgen_test_layout_AllAccessors() {
5870
assert_eq!(::std::mem::size_of::<AllAccessors>() , 8usize);
5971
assert_eq!(::std::mem::align_of::<AllAccessors>() , 4usize);
72+
assert_eq! (0usize , unsafe {
73+
& ( * ( 0 as * const AllAccessors ) ) . mBothAccessors as *
74+
const _ as usize });
75+
assert_eq! (4usize , unsafe {
76+
& ( * ( 0 as * const AllAccessors ) ) . mAlsoBothAccessors as
77+
* const _ as usize });
6078
}
6179
impl Clone for AllAccessors {
6280
fn clone(&self) -> Self { *self }
@@ -91,6 +109,12 @@ pub struct AllUnsafeAccessors {
91109
fn bindgen_test_layout_AllUnsafeAccessors() {
92110
assert_eq!(::std::mem::size_of::<AllUnsafeAccessors>() , 8usize);
93111
assert_eq!(::std::mem::align_of::<AllUnsafeAccessors>() , 4usize);
112+
assert_eq! (0usize , unsafe {
113+
& ( * ( 0 as * const AllUnsafeAccessors ) ) . mBothAccessors
114+
as * const _ as usize });
115+
assert_eq! (4usize , unsafe {
116+
& ( * ( 0 as * const AllUnsafeAccessors ) ) .
117+
mAlsoBothAccessors as * const _ as usize });
94118
}
95119
impl Clone for AllUnsafeAccessors {
96120
fn clone(&self) -> Self { *self }
@@ -131,6 +155,18 @@ pub struct ContradictAccessors {
131155
fn bindgen_test_layout_ContradictAccessors() {
132156
assert_eq!(::std::mem::size_of::<ContradictAccessors>() , 16usize);
133157
assert_eq!(::std::mem::align_of::<ContradictAccessors>() , 4usize);
158+
assert_eq! (0usize , unsafe {
159+
& ( * ( 0 as * const ContradictAccessors ) ) . mBothAccessors
160+
as * const _ as usize });
161+
assert_eq! (4usize , unsafe {
162+
& ( * ( 0 as * const ContradictAccessors ) ) . mNoAccessors as
163+
* const _ as usize });
164+
assert_eq! (8usize , unsafe {
165+
& ( * ( 0 as * const ContradictAccessors ) ) .
166+
mUnsafeAccessors as * const _ as usize });
167+
assert_eq! (12usize , unsafe {
168+
& ( * ( 0 as * const ContradictAccessors ) ) .
169+
mImmutableAccessor as * const _ as usize });
134170
}
135171
impl Clone for ContradictAccessors {
136172
fn clone(&self) -> Self { *self }
@@ -168,6 +204,9 @@ pub struct Replaced {
168204
fn bindgen_test_layout_Replaced() {
169205
assert_eq!(::std::mem::size_of::<Replaced>() , 4usize);
170206
assert_eq!(::std::mem::align_of::<Replaced>() , 4usize);
207+
assert_eq! (0usize , unsafe {
208+
& ( * ( 0 as * const Replaced ) ) . mAccessor as * const _ as
209+
usize });
171210
}
172211
impl Clone for Replaced {
173212
fn clone(&self) -> Self { *self }
@@ -190,6 +229,9 @@ pub struct Wrapper {
190229
fn bindgen_test_layout_Wrapper() {
191230
assert_eq!(::std::mem::size_of::<Wrapper>() , 4usize);
192231
assert_eq!(::std::mem::align_of::<Wrapper>() , 4usize);
232+
assert_eq! (0usize , unsafe {
233+
& ( * ( 0 as * const Wrapper ) ) . mReplaced as * const _ as
234+
usize });
193235
}
194236
impl Clone for Wrapper {
195237
fn clone(&self) -> Self { *self }

tests/expectations/tests/annotation_hide.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub struct NotAnnotated {
2929
fn bindgen_test_layout_NotAnnotated() {
3030
assert_eq!(::std::mem::size_of::<NotAnnotated>() , 4usize);
3131
assert_eq!(::std::mem::align_of::<NotAnnotated>() , 4usize);
32+
assert_eq! (0usize , unsafe {
33+
& ( * ( 0 as * const NotAnnotated ) ) . f as * const _ as
34+
usize });
3235
}
3336
impl Clone for NotAnnotated {
3437
fn clone(&self) -> Self { *self }

tests/expectations/tests/anon_enum.rs

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub enum Test__bindgen_ty_1 { T_NONE = 0, }
1818
fn bindgen_test_layout_Test() {
1919
assert_eq!(::std::mem::size_of::<Test>() , 8usize);
2020
assert_eq!(::std::mem::align_of::<Test>() , 4usize);
21+
assert_eq! (0usize , unsafe {
22+
& ( * ( 0 as * const Test ) ) . foo as * const _ as usize });
23+
assert_eq! (4usize , unsafe {
24+
& ( * ( 0 as * const Test ) ) . bar as * const _ as usize });
2125
}
2226
impl Clone for Test {
2327
fn clone(&self) -> Self { *self }

tests/expectations/tests/class_no_members.rs

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub struct whatever_child_with_member {
3939
fn bindgen_test_layout_whatever_child_with_member() {
4040
assert_eq!(::std::mem::size_of::<whatever_child_with_member>() , 4usize);
4141
assert_eq!(::std::mem::align_of::<whatever_child_with_member>() , 4usize);
42+
assert_eq! (0usize , unsafe {
43+
& ( * ( 0 as * const whatever_child_with_member ) ) . m_member
44+
as * const _ as usize });
4245
}
4346
impl Clone for whatever_child_with_member {
4447
fn clone(&self) -> Self { *self }

tests/expectations/tests/class_use_as.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub struct whatever {
1616
fn bindgen_test_layout_whatever() {
1717
assert_eq!(::std::mem::size_of::<whatever>() , 4usize);
1818
assert_eq!(::std::mem::align_of::<whatever>() , 4usize);
19+
assert_eq! (0usize , unsafe {
20+
& ( * ( 0 as * const whatever ) ) . replacement as * const _
21+
as usize });
1922
}
2023
impl Clone for whatever {
2124
fn clone(&self) -> Self { *self }
@@ -29,6 +32,9 @@ pub struct container {
2932
fn bindgen_test_layout_container() {
3033
assert_eq!(::std::mem::size_of::<container>() , 4usize);
3134
assert_eq!(::std::mem::align_of::<container>() , 4usize);
35+
assert_eq! (0usize , unsafe {
36+
& ( * ( 0 as * const container ) ) . c as * const _ as usize
37+
});
3238
}
3339
impl Clone for container {
3440
fn clone(&self) -> Self { *self }

tests/expectations/tests/class_with_inner_struct.rs

+54
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ pub struct A_Segment {
4545
fn bindgen_test_layout_A_Segment() {
4646
assert_eq!(::std::mem::size_of::<A_Segment>() , 8usize);
4747
assert_eq!(::std::mem::align_of::<A_Segment>() , 4usize);
48+
assert_eq! (0usize , unsafe {
49+
& ( * ( 0 as * const A_Segment ) ) . begin as * const _ as
50+
usize });
51+
assert_eq! (4usize , unsafe {
52+
& ( * ( 0 as * const A_Segment ) ) . end as * const _ as usize
53+
});
4854
}
4955
impl Clone for A_Segment {
5056
fn clone(&self) -> Self { *self }
@@ -59,6 +65,9 @@ pub struct A__bindgen_ty_1 {
5965
fn bindgen_test_layout_A__bindgen_ty_1() {
6066
assert_eq!(::std::mem::size_of::<A__bindgen_ty_1>() , 4usize);
6167
assert_eq!(::std::mem::align_of::<A__bindgen_ty_1>() , 4usize);
68+
assert_eq! (0usize , unsafe {
69+
& ( * ( 0 as * const A__bindgen_ty_1 ) ) . f as * const _ as
70+
usize });
6271
}
6372
impl Clone for A__bindgen_ty_1 {
6473
fn clone(&self) -> Self { *self }
@@ -73,6 +82,9 @@ pub struct A__bindgen_ty_2 {
7382
fn bindgen_test_layout_A__bindgen_ty_2() {
7483
assert_eq!(::std::mem::size_of::<A__bindgen_ty_2>() , 4usize);
7584
assert_eq!(::std::mem::align_of::<A__bindgen_ty_2>() , 4usize);
85+
assert_eq! (0usize , unsafe {
86+
& ( * ( 0 as * const A__bindgen_ty_2 ) ) . d as * const _ as
87+
usize });
7688
}
7789
impl Clone for A__bindgen_ty_2 {
7890
fn clone(&self) -> Self { *self }
@@ -81,6 +93,11 @@ impl Clone for A__bindgen_ty_2 {
8193
fn bindgen_test_layout_A() {
8294
assert_eq!(::std::mem::size_of::<A>() , 12usize);
8395
assert_eq!(::std::mem::align_of::<A>() , 4usize);
96+
assert_eq! (0usize , unsafe {
97+
& ( * ( 0 as * const A ) ) . c as * const _ as usize });
98+
assert_eq! (4usize , unsafe {
99+
& ( * ( 0 as * const A ) ) . named_union as * const _ as usize
100+
});
84101
}
85102
impl Clone for A {
86103
fn clone(&self) -> Self { *self }
@@ -100,6 +117,12 @@ pub struct B_Segment {
100117
fn bindgen_test_layout_B_Segment() {
101118
assert_eq!(::std::mem::size_of::<B_Segment>() , 8usize);
102119
assert_eq!(::std::mem::align_of::<B_Segment>() , 4usize);
120+
assert_eq! (0usize , unsafe {
121+
& ( * ( 0 as * const B_Segment ) ) . begin as * const _ as
122+
usize });
123+
assert_eq! (4usize , unsafe {
124+
& ( * ( 0 as * const B_Segment ) ) . end as * const _ as usize
125+
});
103126
}
104127
impl Clone for B_Segment {
105128
fn clone(&self) -> Self { *self }
@@ -108,6 +131,8 @@ impl Clone for B_Segment {
108131
fn bindgen_test_layout_B() {
109132
assert_eq!(::std::mem::size_of::<B>() , 4usize);
110133
assert_eq!(::std::mem::align_of::<B>() , 4usize);
134+
assert_eq! (0usize , unsafe {
135+
& ( * ( 0 as * const B ) ) . d as * const _ as usize });
111136
}
112137
impl Clone for B {
113138
fn clone(&self) -> Self { *self }
@@ -147,6 +172,18 @@ fn bindgen_test_layout_C__bindgen_ty_1__bindgen_ty_1() {
147172
16usize);
148173
assert_eq!(::std::mem::align_of::<C__bindgen_ty_1__bindgen_ty_1>() ,
149174
4usize);
175+
assert_eq! (0usize , unsafe {
176+
& ( * ( 0 as * const C__bindgen_ty_1__bindgen_ty_1 ) ) . mX1
177+
as * const _ as usize });
178+
assert_eq! (4usize , unsafe {
179+
& ( * ( 0 as * const C__bindgen_ty_1__bindgen_ty_1 ) ) . mY1
180+
as * const _ as usize });
181+
assert_eq! (8usize , unsafe {
182+
& ( * ( 0 as * const C__bindgen_ty_1__bindgen_ty_1 ) ) . mX2
183+
as * const _ as usize });
184+
assert_eq! (12usize , unsafe {
185+
& ( * ( 0 as * const C__bindgen_ty_1__bindgen_ty_1 ) ) . mY2
186+
as * const _ as usize });
150187
}
151188
impl Clone for C__bindgen_ty_1__bindgen_ty_1 {
152189
fn clone(&self) -> Self { *self }
@@ -163,6 +200,12 @@ fn bindgen_test_layout_C__bindgen_ty_1__bindgen_ty_2() {
163200
8usize);
164201
assert_eq!(::std::mem::align_of::<C__bindgen_ty_1__bindgen_ty_2>() ,
165202
4usize);
203+
assert_eq! (0usize , unsafe {
204+
& ( * ( 0 as * const C__bindgen_ty_1__bindgen_ty_2 ) ) .
205+
mStepSyntax as * const _ as usize });
206+
assert_eq! (4usize , unsafe {
207+
& ( * ( 0 as * const C__bindgen_ty_1__bindgen_ty_2 ) ) .
208+
mSteps as * const _ as usize });
166209
}
167210
impl Clone for C__bindgen_ty_1__bindgen_ty_2 {
168211
fn clone(&self) -> Self { *self }
@@ -171,6 +214,9 @@ impl Clone for C__bindgen_ty_1__bindgen_ty_2 {
171214
fn bindgen_test_layout_C__bindgen_ty_1() {
172215
assert_eq!(::std::mem::size_of::<C__bindgen_ty_1>() , 16usize);
173216
assert_eq!(::std::mem::align_of::<C__bindgen_ty_1>() , 4usize);
217+
assert_eq! (0usize , unsafe {
218+
& ( * ( 0 as * const C__bindgen_ty_1 ) ) . mFunc as * const _
219+
as usize });
174220
}
175221
impl Clone for C__bindgen_ty_1 {
176222
fn clone(&self) -> Self { *self }
@@ -185,6 +231,12 @@ pub struct C_Segment {
185231
fn bindgen_test_layout_C_Segment() {
186232
assert_eq!(::std::mem::size_of::<C_Segment>() , 8usize);
187233
assert_eq!(::std::mem::align_of::<C_Segment>() , 4usize);
234+
assert_eq! (0usize , unsafe {
235+
& ( * ( 0 as * const C_Segment ) ) . begin as * const _ as
236+
usize });
237+
assert_eq! (4usize , unsafe {
238+
& ( * ( 0 as * const C_Segment ) ) . end as * const _ as usize
239+
});
188240
}
189241
impl Clone for C_Segment {
190242
fn clone(&self) -> Self { *self }
@@ -193,6 +245,8 @@ impl Clone for C_Segment {
193245
fn bindgen_test_layout_C() {
194246
assert_eq!(::std::mem::size_of::<C>() , 20usize);
195247
assert_eq!(::std::mem::align_of::<C>() , 4usize);
248+
assert_eq! (0usize , unsafe {
249+
& ( * ( 0 as * const C ) ) . d as * const _ as usize });
196250
}
197251
impl Clone for C {
198252
fn clone(&self) -> Self { *self }

0 commit comments

Comments
 (0)