-
Notifications
You must be signed in to change notification settings - Fork 702
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
codegen: Avoid generating invalid Rust code when a struct is not properly aligned. #413
Merged
+211
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
libbindgen/tests/expectations/tests/16-byte-alignment.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
|
||
#![allow(non_snake_case)] | ||
|
||
|
||
#[repr(C)] | ||
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>); | ||
impl <T> __BindgenUnionField<T> { | ||
#[inline] | ||
pub fn new() -> Self { __BindgenUnionField(::std::marker::PhantomData) } | ||
#[inline] | ||
pub unsafe fn as_ref(&self) -> &T { ::std::mem::transmute(self) } | ||
#[inline] | ||
pub unsafe fn as_mut(&mut self) -> &mut T { ::std::mem::transmute(self) } | ||
} | ||
impl <T> ::std::default::Default for __BindgenUnionField<T> { | ||
#[inline] | ||
fn default() -> Self { Self::new() } | ||
} | ||
impl <T> ::std::clone::Clone for __BindgenUnionField<T> { | ||
#[inline] | ||
fn clone(&self) -> Self { Self::new() } | ||
} | ||
impl <T> ::std::marker::Copy for __BindgenUnionField<T> { } | ||
impl <T> ::std::fmt::Debug for __BindgenUnionField<T> { | ||
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { | ||
fmt.write_str("__BindgenUnionField") | ||
} | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct rte_ipv4_tuple { | ||
pub src_addr: u32, | ||
pub dst_addr: u32, | ||
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1, | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct rte_ipv4_tuple__bindgen_ty_1 { | ||
pub __bindgen_anon_1: __BindgenUnionField<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>, | ||
pub sctp_tag: __BindgenUnionField<u32>, | ||
pub bindgen_union_field: u32, | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 { | ||
pub dport: u16, | ||
pub sport: u16, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>() | ||
, 4usize); | ||
assert_eq!(::std::mem::align_of::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>() | ||
, 2usize); | ||
} | ||
impl Clone for rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[test] | ||
fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple__bindgen_ty_1>() , | ||
4usize); | ||
assert_eq!(::std::mem::align_of::<rte_ipv4_tuple__bindgen_ty_1>() , | ||
4usize); | ||
} | ||
impl Clone for rte_ipv4_tuple__bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[test] | ||
fn bindgen_test_layout_rte_ipv4_tuple() { | ||
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple>() , 12usize); | ||
assert_eq!(::std::mem::align_of::<rte_ipv4_tuple>() , 4usize); | ||
} | ||
impl Clone for rte_ipv4_tuple { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct rte_ipv6_tuple { | ||
pub src_addr: [u8; 16usize], | ||
pub dst_addr: [u8; 16usize], | ||
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1, | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct rte_ipv6_tuple__bindgen_ty_1 { | ||
pub __bindgen_anon_1: __BindgenUnionField<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>, | ||
pub sctp_tag: __BindgenUnionField<u32>, | ||
pub bindgen_union_field: u32, | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 { | ||
pub dport: u16, | ||
pub sport: u16, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>() | ||
, 4usize); | ||
assert_eq!(::std::mem::align_of::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>() | ||
, 2usize); | ||
} | ||
impl Clone for rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[test] | ||
fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple__bindgen_ty_1>() , | ||
4usize); | ||
assert_eq!(::std::mem::align_of::<rte_ipv6_tuple__bindgen_ty_1>() , | ||
4usize); | ||
} | ||
impl Clone for rte_ipv6_tuple__bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[test] | ||
fn bindgen_test_layout_rte_ipv6_tuple() { | ||
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple>() , 36usize); | ||
assert_eq!(::std::mem::align_of::<rte_ipv6_tuple>() , 4usize); | ||
} | ||
impl Clone for rte_ipv6_tuple { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[repr(C)] | ||
#[derive(Copy)] | ||
pub struct rte_thash_tuple { | ||
pub v4: __BindgenUnionField<rte_ipv4_tuple>, | ||
pub v6: __BindgenUnionField<rte_ipv6_tuple>, | ||
pub bindgen_union_field: [u8; 48usize], | ||
} | ||
impl Clone for rte_thash_tuple { | ||
fn clone(&self) -> Self { *self } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
typedef unsigned char uint8_t; | ||
typedef unsigned short uint16_t; | ||
typedef unsigned int uint32_t; | ||
|
||
struct rte_ipv4_tuple { | ||
uint32_t src_addr; | ||
uint32_t dst_addr; | ||
union { | ||
struct { | ||
uint16_t dport; | ||
uint16_t sport; | ||
}; | ||
uint32_t sctp_tag; | ||
}; | ||
}; | ||
|
||
struct rte_ipv6_tuple { | ||
uint8_t src_addr[16]; | ||
uint8_t dst_addr[16]; | ||
union { | ||
struct { | ||
uint16_t dport; | ||
uint16_t sport; | ||
}; | ||
uint32_t sctp_tag; | ||
}; | ||
}; | ||
|
||
union rte_thash_tuple { | ||
struct rte_ipv4_tuple v4; | ||
struct rte_ipv6_tuple v6; | ||
} __attribute__((aligned(16))); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stuff is great :)