From eac170afb456022d019cc090ce1c2e97c8631186 Mon Sep 17 00:00:00 2001 From: zzhu Date: Mon, 6 Aug 2018 15:56:06 -0500 Subject: [PATCH] Fix incomplete array alignment in struct --- src/codegen/mod.rs | 12 ++- .../tests/struct_with_incomplete_array.rs | 80 +++++++++++++++++++ .../headers/struct_with_incomplete_array.hpp | 3 + 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 tests/expectations/tests/struct_with_incomplete_array.rs create mode 100644 tests/headers/struct_with_incomplete_array.hpp diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 8c113f5a82..57b73c4e94 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -3658,17 +3658,23 @@ mod utils { let incomplete_array_decl = quote! { #[repr(C)] #[derive(Default)] - pub struct __IncompleteArrayField( - ::#prefix::marker::PhantomData); + pub struct __IncompleteArrayField{ + _alignment: [*const (); 0], + _marker: ::#prefix::marker::PhantomData, + } }; let incomplete_array_impl = quote! { impl __IncompleteArrayField { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::#prefix::marker::PhantomData) + __IncompleteArrayField { + _marker: ::#prefix::marker::PhantomData, + _alignment: Default::default(), + } } + #[inline] pub unsafe fn as_ptr(&self) -> *const T { ::#prefix::mem::transmute(self) diff --git a/tests/expectations/tests/struct_with_incomplete_array.rs b/tests/expectations/tests/struct_with_incomplete_array.rs new file mode 100644 index 0000000000..861bf871e3 --- /dev/null +++ b/tests/expectations/tests/struct_with_incomplete_array.rs @@ -0,0 +1,80 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField { + _alignment: [*const (); 0], + _marker: ::std::marker::PhantomData, +} +impl __IncompleteArrayField { + #[inline] + pub fn new() -> Self { + __IncompleteArrayField { + _marker: ::std::marker::PhantomData, + _alignment: Default::default(), + } + } + #[inline] + pub unsafe fn as_ptr(&self) -> *const T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_mut_ptr(&mut self) -> *mut T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + ::std::slice::from_raw_parts(self.as_ptr(), len) + } + #[inline] + pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + } +} +impl ::std::fmt::Debug for __IncompleteArrayField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +impl ::std::clone::Clone for __IncompleteArrayField { + #[inline] + fn clone(&self) -> Self { + Self::new() + } +} +impl ::std::marker::Copy for __IncompleteArrayField {} +#[repr(C)] +#[derive(Debug, Default)] +pub struct bpf_raw_tracepoint_args { + pub args: __IncompleteArrayField<::std::os::raw::c_ulonglong>, +} +#[test] +fn bindgen_test_layout_bpf_raw_tracepoint_args() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(bpf_raw_tracepoint_args)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(bpf_raw_tracepoint_args)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).args as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(bpf_raw_tracepoint_args), + "::", + stringify!(args) + ) + ); +} diff --git a/tests/headers/struct_with_incomplete_array.hpp b/tests/headers/struct_with_incomplete_array.hpp new file mode 100644 index 0000000000..fb2fa27169 --- /dev/null +++ b/tests/headers/struct_with_incomplete_array.hpp @@ -0,0 +1,3 @@ +struct bpf_raw_tracepoint_args { + unsigned long long args[0]; +};