Skip to content

Bindgen fail to generate bindings for struct. #1314

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

Open
Catramen opened this issue May 17, 2018 · 6 comments
Open

Bindgen fail to generate bindings for struct. #1314

Catramen opened this issue May 17, 2018 · 6 comments

Comments

@Catramen
Copy link

Header:
#include <stdint.h>
typedef struct {
uint32_t f : 20;
uint8_t f1 : 4;
uint8_t f2 : 1;
uint8_t f3 : 1;
uint8_t f4 : 1;
uint8_t f5 : 5;
uint16_t f6;
uint8_t f7;
uint8_t f8;
uint8_t f9;
uint8_t f10;
uint8_t f11 : 2;
uint8_t f12 : 4;
uint16_t f13 : 10;
uint8_t f14;
uint32_t f15 : 19;
} S;

Bindgen command:
bindgen types.h -o t.rs
ERROR 2018-05-17T23:24:11Z: bindgen::codegen::struct_layout: Calculated wrong layout for S, too more 1 bytes

Bindgen version:
bindgen 0.36.1

@emilio
Copy link
Contributor

emilio commented May 18, 2018

Thanks for the report!

A bit reduced:

#include <stdint.h>
typedef struct {
  uint16_t f6;
  uint8_t f11 : 2;
  uint8_t f12 : 4;
  uint16_t f13 : 10;
  uint8_t f14;
  uint32_t f15 : 19;
} S;

@Catramen
Copy link
Author

The size of:
typedef struct {
uint16_t f6;
uint8_t f11 : 2;
uint8_t f12 : 4;
uint16_t f13 : 10;
uint8_t f14;
uint32_t f15 : 19;
} S;
is 8. Which means the last bitfield is not aligned to u32.
But bindgen generates:
pub struct S {
pub f6: u16,
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 2usize], u16>,
pub f14: u8,
pub _bitfield_2: __BindgenBitfieldUnit<[u8; 4usize], u32>,
pub __bindgen_align: [u32; 0usize],
}

We should have "pub _bitfield_2: __BindgenBitfieldUnit<[u8; 3usize], u8>,"
rather than "pub _bitfield_2: __BindgenBitfieldUnit<[u8; 4usize], u32>,"

@Catramen
Copy link
Author

In section 6.7.2.1 #10 of C99 spec (http://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf):
The alignment of the addressable storage unit is unspecified.
Guess we should comply with clang implementation here.

@Catramen
Copy link
Author

Clang implementation is here: (section Detailed Description)
https://clang.llvm.org/doxygen/structclang_1_1CodeGen_1_1CGBitFieldInfo.html

@Catramen
Copy link
Author

The following structs have different layouts
typedef struct {
uint8_t a;
uint32_t b : 15;
} S;
typedef struct {
uint8_t a;
uint16_t b : 15;
} S2;

But bindgen gives same struct.

@emilio emilio self-assigned this Jun 3, 2018
@emilio
Copy link
Contributor

emilio commented Jun 3, 2018

I took a look at this today, will submit a fix.

emilio added a commit to emilio/rust-bindgen that referenced this issue Jun 3, 2018
We should also align the allocation unit even if it's the first bitfield in the
allocation unit.

Fixes rust-lang#1314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants