Skip to content

Fix "dereferencing a null pointer" in C layout tests #2203

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

Merged
merged 1 commit into from
May 17, 2022
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
4 changes: 3 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,9 @@ impl CodeGenerator for CompInfo {
quote! {
assert_eq!(
unsafe {
&(*(::#prefix::ptr::null::<#canonical_ident>())).#field_name as *const _ as usize
let uninit = ::#prefix::mem::MaybeUninit::<#canonical_ident>::uninit();
let ptr = uninit.as_ptr();
::#prefix::ptr::addr_of!((*ptr).#field_name) as usize - ptr as usize

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using offset_from() might work here (as #field_offset will be a literal you don't need to worry about usize/isize conversions)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset_from() assumes that the two pointers have the same type, which is not the case in this case, and if we were to cast to *const u8, I would rather just directly use usize.

},
#field_offset,
concat!("Offset of field: ", stringify!(#canonical_ident), "::", stringify!(#field_name))
Expand Down
74 changes: 48 additions & 26 deletions tests/expectations/tests/16-byte-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.dport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dport) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -58,9 +60,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.sport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sport) as usize - ptr as usize
},
2usize,
concat!(
Expand All @@ -85,8 +89,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple__bindgen_ty_1>())).sctp_tag
as *const _ as usize
let uninit =
::std::mem::MaybeUninit::<rte_ipv4_tuple__bindgen_ty_1>::uninit(
);
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sctp_tag) as usize - ptr as usize
},
0usize,
concat!(
Expand Down Expand Up @@ -120,8 +127,9 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple>())).src_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv4_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).src_addr) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -133,8 +141,9 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple>())).dst_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv4_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dst_addr) as usize - ptr as usize
},
4usize,
concat!(
Expand Down Expand Up @@ -193,9 +202,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.dport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dport) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -207,9 +218,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.sport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sport) as usize - ptr as usize
},
2usize,
concat!(
Expand All @@ -234,8 +247,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple__bindgen_ty_1>())).sctp_tag
as *const _ as usize
let uninit =
::std::mem::MaybeUninit::<rte_ipv6_tuple__bindgen_ty_1>::uninit(
);
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sctp_tag) as usize - ptr as usize
},
0usize,
concat!(
Expand Down Expand Up @@ -269,8 +285,9 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple>())).src_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv6_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).src_addr) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -282,8 +299,9 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple>())).dst_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv6_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dst_addr) as usize - ptr as usize
},
16usize,
concat!(
Expand Down Expand Up @@ -324,7 +342,9 @@ fn bindgen_test_layout_rte_thash_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_thash_tuple>())).v4 as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<rte_thash_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).v4) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -336,7 +356,9 @@ fn bindgen_test_layout_rte_thash_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_thash_tuple>())).v6 as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<rte_thash_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).v6) as usize - ptr as usize
},
0usize,
concat!(
Expand Down
74 changes: 48 additions & 26 deletions tests/expectations/tests/16-byte-alignment_1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.dport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dport) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -103,9 +105,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.sport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sport) as usize - ptr as usize
},
2usize,
concat!(
Expand Down Expand Up @@ -135,8 +139,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple__bindgen_ty_1>())).sctp_tag
as *const _ as usize
let uninit =
::std::mem::MaybeUninit::<rte_ipv4_tuple__bindgen_ty_1>::uninit(
);
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sctp_tag) as usize - ptr as usize
},
0usize,
concat!(
Expand Down Expand Up @@ -166,8 +173,9 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple>())).src_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv4_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).src_addr) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -179,8 +187,9 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv4_tuple>())).dst_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv4_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dst_addr) as usize - ptr as usize
},
4usize,
concat!(
Expand Down Expand Up @@ -237,9 +246,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.dport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dport) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -251,9 +262,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>(
)))
.sport as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sport) as usize - ptr as usize
},
2usize,
concat!(
Expand Down Expand Up @@ -283,8 +296,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple__bindgen_ty_1>())).sctp_tag
as *const _ as usize
let uninit =
::std::mem::MaybeUninit::<rte_ipv6_tuple__bindgen_ty_1>::uninit(
);
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).sctp_tag) as usize - ptr as usize
},
0usize,
concat!(
Expand Down Expand Up @@ -314,8 +330,9 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple>())).src_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv6_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).src_addr) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -327,8 +344,9 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_ipv6_tuple>())).dst_addr as *const _
as usize
let uninit = ::std::mem::MaybeUninit::<rte_ipv6_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).dst_addr) as usize - ptr as usize
},
16usize,
concat!(
Expand Down Expand Up @@ -360,7 +378,9 @@ fn bindgen_test_layout_rte_thash_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_thash_tuple>())).v4 as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<rte_thash_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).v4) as usize - ptr as usize
},
0usize,
concat!(
Expand All @@ -372,7 +392,9 @@ fn bindgen_test_layout_rte_thash_tuple() {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<rte_thash_tuple>())).v6 as *const _ as usize
let uninit = ::std::mem::MaybeUninit::<rte_thash_tuple>::uninit();
let ptr = uninit.as_ptr();
::std::ptr::addr_of!((*ptr).v6) as usize - ptr as usize
},
0usize,
concat!(
Expand Down
Loading