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

Divide by zero in struct layout #1137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,9 +1787,13 @@ impl CodeGenerator for CompInfo {
let field_name = ctx.rust_ident(name);

Some(quote! {
assert_eq!(unsafe { &(*(0 as *const #canonical_ident)).#field_name as *const _ as usize },
#field_offset,
concat!("Alignment of field: ", stringify!(#canonical_ident), "::", stringify!(#field_name)));
assert_eq!(
unsafe {
&(*(0 as *const #canonical_ident)).#field_name as *const _ as usize
},
#field_offset,
concat!("Offset of field: ", stringify!(#canonical_ident), "::", stringify!(#field_name))
);
})
})
})
Expand Down
7 changes: 5 additions & 2 deletions src/codegen/struct_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,12 @@ impl<'a> StructLayoutTracker<'a> {
new_field_layout
);

// Avoid divide-by-zero errors if align is 0.
let align = cmp::max(1, layout.align);

if self.last_field_was_bitfield &&
new_field_layout.align <= layout.size % layout.align &&
new_field_layout.size <= layout.size % layout.align
new_field_layout.align <= layout.size % align &&
new_field_layout.size <= layout.size % align
{
// The new field will be coalesced into some of the remaining bits.
//
Expand Down
24 changes: 12 additions & 12 deletions tests/expectations/tests/16-byte-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
},
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(dport)
Expand All @@ -60,7 +60,7 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
},
2usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(sport)
Expand All @@ -83,7 +83,7 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
unsafe { &(*(0 as *const rte_ipv4_tuple__bindgen_ty_1)).sctp_tag as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple__bindgen_ty_1),
"::",
stringify!(sctp_tag)
Expand Down Expand Up @@ -111,7 +111,7 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
unsafe { &(*(0 as *const rte_ipv4_tuple)).src_addr as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple),
"::",
stringify!(src_addr)
Expand All @@ -121,7 +121,7 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
unsafe { &(*(0 as *const rte_ipv4_tuple)).dst_addr as *const _ as usize },
4usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple),
"::",
stringify!(dst_addr)
Expand Down Expand Up @@ -177,7 +177,7 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
},
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(dport)
Expand All @@ -189,7 +189,7 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
},
2usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(sport)
Expand All @@ -212,7 +212,7 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
unsafe { &(*(0 as *const rte_ipv6_tuple__bindgen_ty_1)).sctp_tag as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple__bindgen_ty_1),
"::",
stringify!(sctp_tag)
Expand Down Expand Up @@ -240,7 +240,7 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
unsafe { &(*(0 as *const rte_ipv6_tuple)).src_addr as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple),
"::",
stringify!(src_addr)
Expand All @@ -250,7 +250,7 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
unsafe { &(*(0 as *const rte_ipv6_tuple)).dst_addr as *const _ as usize },
16usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple),
"::",
stringify!(dst_addr)
Expand Down Expand Up @@ -280,7 +280,7 @@ fn bindgen_test_layout_rte_thash_tuple() {
unsafe { &(*(0 as *const rte_thash_tuple)).v4 as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_thash_tuple),
"::",
stringify!(v4)
Expand All @@ -290,7 +290,7 @@ fn bindgen_test_layout_rte_thash_tuple() {
unsafe { &(*(0 as *const rte_thash_tuple)).v6 as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_thash_tuple),
"::",
stringify!(v6)
Expand Down
24 changes: 12 additions & 12 deletions tests/expectations/tests/16-byte-alignment_1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
},
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(dport)
Expand All @@ -103,7 +103,7 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
},
2usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(sport)
Expand Down Expand Up @@ -131,7 +131,7 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
unsafe { &(*(0 as *const rte_ipv4_tuple__bindgen_ty_1)).sctp_tag as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple__bindgen_ty_1),
"::",
stringify!(sctp_tag)
Expand Down Expand Up @@ -159,7 +159,7 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
unsafe { &(*(0 as *const rte_ipv4_tuple)).src_addr as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple),
"::",
stringify!(src_addr)
Expand All @@ -169,7 +169,7 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
unsafe { &(*(0 as *const rte_ipv4_tuple)).dst_addr as *const _ as usize },
4usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv4_tuple),
"::",
stringify!(dst_addr)
Expand Down Expand Up @@ -225,7 +225,7 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
},
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(dport)
Expand All @@ -237,7 +237,7 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
},
2usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1),
"::",
stringify!(sport)
Expand Down Expand Up @@ -265,7 +265,7 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
unsafe { &(*(0 as *const rte_ipv6_tuple__bindgen_ty_1)).sctp_tag as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple__bindgen_ty_1),
"::",
stringify!(sctp_tag)
Expand Down Expand Up @@ -293,7 +293,7 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
unsafe { &(*(0 as *const rte_ipv6_tuple)).src_addr as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple),
"::",
stringify!(src_addr)
Expand All @@ -303,7 +303,7 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
unsafe { &(*(0 as *const rte_ipv6_tuple)).dst_addr as *const _ as usize },
16usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_ipv6_tuple),
"::",
stringify!(dst_addr)
Expand Down Expand Up @@ -333,7 +333,7 @@ fn bindgen_test_layout_rte_thash_tuple() {
unsafe { &(*(0 as *const rte_thash_tuple)).v4 as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_thash_tuple),
"::",
stringify!(v4)
Expand All @@ -343,7 +343,7 @@ fn bindgen_test_layout_rte_thash_tuple() {
unsafe { &(*(0 as *const rte_thash_tuple)).v6 as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(rte_thash_tuple),
"::",
stringify!(v6)
Expand Down
28 changes: 14 additions & 14 deletions tests/expectations/tests/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn bindgen_test_layout_SomeAccessors() {
unsafe { &(*(0 as *const SomeAccessors)).mNoAccessor as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(SomeAccessors),
"::",
stringify!(mNoAccessor)
Expand All @@ -41,7 +41,7 @@ fn bindgen_test_layout_SomeAccessors() {
unsafe { &(*(0 as *const SomeAccessors)).mBothAccessors as *const _ as usize },
4usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(SomeAccessors),
"::",
stringify!(mBothAccessors)
Expand All @@ -51,7 +51,7 @@ fn bindgen_test_layout_SomeAccessors() {
unsafe { &(*(0 as *const SomeAccessors)).mUnsafeAccessors as *const _ as usize },
8usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(SomeAccessors),
"::",
stringify!(mUnsafeAccessors)
Expand All @@ -61,7 +61,7 @@ fn bindgen_test_layout_SomeAccessors() {
unsafe { &(*(0 as *const SomeAccessors)).mImmutableAccessor as *const _ as usize },
12usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(SomeAccessors),
"::",
stringify!(mImmutableAccessor)
Expand Down Expand Up @@ -113,7 +113,7 @@ fn bindgen_test_layout_AllAccessors() {
unsafe { &(*(0 as *const AllAccessors)).mBothAccessors as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(AllAccessors),
"::",
stringify!(mBothAccessors)
Expand All @@ -123,7 +123,7 @@ fn bindgen_test_layout_AllAccessors() {
unsafe { &(*(0 as *const AllAccessors)).mAlsoBothAccessors as *const _ as usize },
4usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(AllAccessors),
"::",
stringify!(mAlsoBothAccessors)
Expand Down Expand Up @@ -171,7 +171,7 @@ fn bindgen_test_layout_AllUnsafeAccessors() {
unsafe { &(*(0 as *const AllUnsafeAccessors)).mBothAccessors as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(AllUnsafeAccessors),
"::",
stringify!(mBothAccessors)
Expand All @@ -181,7 +181,7 @@ fn bindgen_test_layout_AllUnsafeAccessors() {
unsafe { &(*(0 as *const AllUnsafeAccessors)).mAlsoBothAccessors as *const _ as usize },
4usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(AllUnsafeAccessors),
"::",
stringify!(mAlsoBothAccessors)
Expand Down Expand Up @@ -234,7 +234,7 @@ fn bindgen_test_layout_ContradictAccessors() {
unsafe { &(*(0 as *const ContradictAccessors)).mBothAccessors as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(ContradictAccessors),
"::",
stringify!(mBothAccessors)
Expand All @@ -244,7 +244,7 @@ fn bindgen_test_layout_ContradictAccessors() {
unsafe { &(*(0 as *const ContradictAccessors)).mNoAccessors as *const _ as usize },
4usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(ContradictAccessors),
"::",
stringify!(mNoAccessors)
Expand All @@ -254,7 +254,7 @@ fn bindgen_test_layout_ContradictAccessors() {
unsafe { &(*(0 as *const ContradictAccessors)).mUnsafeAccessors as *const _ as usize },
8usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(ContradictAccessors),
"::",
stringify!(mUnsafeAccessors)
Expand All @@ -264,7 +264,7 @@ fn bindgen_test_layout_ContradictAccessors() {
unsafe { &(*(0 as *const ContradictAccessors)).mImmutableAccessor as *const _ as usize },
12usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(ContradictAccessors),
"::",
stringify!(mImmutableAccessor)
Expand Down Expand Up @@ -315,7 +315,7 @@ fn bindgen_test_layout_Replaced() {
unsafe { &(*(0 as *const Replaced)).mAccessor as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(Replaced),
"::",
stringify!(mAccessor)
Expand Down Expand Up @@ -354,7 +354,7 @@ fn bindgen_test_layout_Wrapper() {
unsafe { &(*(0 as *const Wrapper)).mReplaced as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(Wrapper),
"::",
stringify!(mReplaced)
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/annotation_hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn bindgen_test_layout_NotAnnotated() {
unsafe { &(*(0 as *const NotAnnotated)).f as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
"Offset of field: ",
stringify!(NotAnnotated),
"::",
stringify!(f)
Expand Down
Loading