diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-12-20 17:07:40 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-12-20 21:29:47 +0100 |
commit | 669dc1b628b16da33cc382142c53f6377120a3b8 (patch) | |
tree | f08354425b49284f14abf02db51e817fc52b2d06 /src | |
parent | 98841b32ed51e4474377712aa636251d9a4f7215 (diff) |
comp: Fix bitfields to allow underaligned fields after them to take padding space.
Fixes #1947.
There are two separate issues here: First, the change in comp.rs ensures
that we don't round up the amount of storage to the alignment of the
bitfield. That generates the "expected" output in #1947
(`__BindgenBitfieldUnit<[u8; 3], u16>`).
But that's still not enough to fix that test-case because
__BindgenBitfieldUnit would be aligned and have padding, and Rust won't
put the extra field in the padding.
In order to ensure the bitfield starts at the right alignment, but that
Rust can put stuff in the extra field, we need to make a breaking change
and split the generated fields in two: One preceding that guarantees
alignment, and the actual storage, bit-aligned.
This keeps the existing behavior while fixing that test-case.
Diffstat (limited to 'src')
-rwxr-xr-x | src/codegen/bitfield_unit.rs | 9 | ||||
-rw-r--r-- | src/codegen/bitfield_unit_tests.rs | 49 | ||||
-rw-r--r-- | src/codegen/helpers.rs | 9 | ||||
-rw-r--r-- | src/codegen/mod.rs | 15 | ||||
-rw-r--r-- | src/ir/comp.rs | 2 |
5 files changed, 26 insertions, 58 deletions
diff --git a/src/codegen/bitfield_unit.rs b/src/codegen/bitfield_unit.rs index a5a8ac77..73ec2bd6 100755 --- a/src/codegen/bitfield_unit.rs +++ b/src/codegen/bitfield_unit.rs @@ -1,18 +1,17 @@ #[repr(C)] #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct __BindgenBitfieldUnit<Storage, Align> { +pub struct __BindgenBitfieldUnit<Storage> { storage: Storage, - align: [Align; 0], } -impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> { +impl<Storage> __BindgenBitfieldUnit<Storage> { #[inline] pub const fn new(storage: Storage) -> Self { - Self { storage, align: [] } + Self { storage } } } -impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> +impl<Storage> __BindgenBitfieldUnit<Storage> where Storage: AsRef<[u8]> + AsMut<[u8]>, { diff --git a/src/codegen/bitfield_unit_tests.rs b/src/codegen/bitfield_unit_tests.rs index 3a9239c2..e143e4ea 100644 --- a/src/codegen/bitfield_unit_tests.rs +++ b/src/codegen/bitfield_unit_tests.rs @@ -22,12 +22,10 @@ //! ``` use super::bitfield_unit::__BindgenBitfieldUnit; -use std::mem; #[test] fn bitfield_unit_get_bit() { - let unit = - __BindgenBitfieldUnit::<[u8; 2], u64>::new([0b10011101, 0b00011101]); + let unit = __BindgenBitfieldUnit::<[u8; 2]>::new([0b10011101, 0b00011101]); let mut bits = vec![]; for i in 0..16 { @@ -50,7 +48,7 @@ fn bitfield_unit_get_bit() { #[test] fn bitfield_unit_set_bit() { let mut unit = - __BindgenBitfieldUnit::<[u8; 2], u64>::new([0b00000000, 0b00000000]); + __BindgenBitfieldUnit::<[u8; 2]>::new([0b00000000, 0b00000000]); for i in 0..16 { if i % 3 == 0 { @@ -63,7 +61,7 @@ fn bitfield_unit_set_bit() { } let mut unit = - __BindgenBitfieldUnit::<[u8; 2], u64>::new([0b11111111, 0b11111111]); + __BindgenBitfieldUnit::<[u8; 2]>::new([0b11111111, 0b11111111]); for i in 0..16 { if i % 3 == 0 { @@ -76,43 +74,6 @@ fn bitfield_unit_set_bit() { } } -#[test] -fn bitfield_unit_align() { - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u8>>(), - mem::align_of::<u8>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u16>>(), - mem::align_of::<u16>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u32>>(), - mem::align_of::<u32>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 1], u64>>(), - mem::align_of::<u64>() - ); - - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u8>>(), - mem::align_of::<u8>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u16>>(), - mem::align_of::<u16>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u32>>(), - mem::align_of::<u32>() - ); - assert_eq!( - mem::align_of::<__BindgenBitfieldUnit<[u8; 8], u64>>(), - mem::align_of::<u64>() - ); -} - macro_rules! bitfield_unit_get { ( $( @@ -123,7 +84,7 @@ macro_rules! bitfield_unit_get { fn bitfield_unit_get() { $({ let expected = $expected; - let unit = __BindgenBitfieldUnit::<_, u64>::new($storage); + let unit = __BindgenBitfieldUnit::<_>::new($storage); let actual = unit.get($start, $len); println!(); @@ -223,7 +184,7 @@ macro_rules! bitfield_unit_set { #[test] fn bitfield_unit_set() { $( - let mut unit = __BindgenBitfieldUnit::<[u8; 4], u64>::new([0, 0, 0, 0]); + let mut unit = __BindgenBitfieldUnit::<[u8; 4]>::new([0, 0, 0, 0]); unit.set($start, $len, $val); let actual = unit.get(0, 32); diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs index b6825f5e..205995bc 100644 --- a/src/codegen/helpers.rs +++ b/src/codegen/helpers.rs @@ -120,16 +120,9 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> TokenStream { tokens.append_all(quote! { root:: }); } - let align = match layout.align { - n if n >= 8 => quote! { u64 }, - 4 => quote! { u32 }, - 2 => quote! { u16 }, - _ => quote! { u8 }, - }; - let size = layout.size; tokens.append_all(quote! { - __BindgenBitfieldUnit<[u8; #size], #align> + __BindgenBitfieldUnit<[u8; #size]> }); tokens diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0d93c491..194a461c 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1437,6 +1437,21 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit { } }; + { + let align_field_name = format!("_bitfield_align_{}", self.nth()); + let align_field_ident = ctx.rust_ident(&align_field_name); + let align_ty = match self.layout().align { + n if n >= 8 => quote! { u64 }, + 4 => quote! { u32 }, + 2 => quote! { u16 }, + _ => quote! { u8 }, + }; + let align_field = quote! { + pub #align_field_ident: [#align_ty; 0], + }; + fields.extend(Some(align_field)); + } + let unit_field_name = format!("_bitfield_{}", self.nth()); let unit_field_ident = ctx.rust_ident(&unit_field_name); diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 645f6251..a0ca925c 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -575,7 +575,7 @@ where } else { bytes_from_bits_pow2(unit_align_in_bits) }; - let size = align_to(unit_size_in_bits, align * 8) / 8; + let size = align_to(unit_size_in_bits, 8) / 8; let layout = Layout::new(size, align); fields.extend(Some(Field::Bitfields(BitfieldUnit { nth: *bitfield_unit_count, |