Skip to content

alignment of integer field holding bitfield values is not correct #111

Closed
@heycam

Description

@heycam

The use of a single integer-typed field to represent contiguous bitfield members results in layout that doesn't match the layout generated by the corresponding C++ struct. For example with:

struct A
{
  uint8_t x;
  unsigned b1 : 1;
  unsigned b2 : 1;
  unsigned b3 : 1;
  unsigned b4 : 1;
  unsigned b5 : 1;
  unsigned b6 : 1;
  unsigned b7 : 1;
  unsigned b8 : 1;
  unsigned b9 : 1;
  unsigned b10 : 1;
  uint8_t y;
};
...
printf("offsetof(A, y) = %d\n", (int) offsetof(A, y));

I get an output of 3 when compiled with clang, but rust-bindgen generates:

#[repr(C)]
#[derive(Debug, Copy)]
pub struct A {
    pub x: u8,
    pub _bitfield_1: u16,
    pub y: u8,
}

which places y at offset 4.

Packing and alignment of bitfields is probably implementation dependent, but we should generate the same layout that clang does. It might just be a matter of generating a sequence of u8s to hold the contiguous bitfields.

cc @emilio

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions