Skip to content
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

Cannot include structs with bitfields in linux-api #3270

Open
stevenengler opened this issue Jan 3, 2024 · 0 comments
Open

Cannot include structs with bitfields in linux-api #3270

stevenengler opened this issue Jan 3, 2024 · 0 comments
Labels
Component: Build Build/install tools and dependencies Type: Enhancement New functionality or improved design

Comments

@stevenengler
Copy link
Contributor

If you try to add a Linux struct containing bitfields to linux-api, it will cause cbindgen to fail.

For example if you try to add tcp_info which contains:

struct tcp_info {
    ...
    __u8 tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;

bindgen will generate rust bindings like:

#[repr(C)]
pub struct linux_tcp_info {
    ...
    pub l_bitfield_1: linux___BindgenBitfieldUnit<[u8; 2usize]>,
    ...
}
#[repr(C)]
pub struct linux___BindgenBitfieldUnit<Storage> {
    storage: Storage,
}

The linux-api build script then generates C bindings for this rust code, which fails with:

thread 'main' panicked at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cbindgen-0.26.0/src/bindgen/mangle.rs:132:17:
  not implemented: Unable to mangle generic parameter Array(Primitive(Integer { zeroable: true, signed: false, kind: B8 }), Value("2")) for 'linux___BindgenBitfieldUnit'

So cbindgen can't create C bindings for this structure. We also cannot exclude this structure from cbindgen using the "exclude" option since it appears that this mangling occurs before types are excluded: https://github.com/mozilla/cbindgen/blob/6bfc2176187a6fc6fba6315323b0296112330294/src/bindgen/library.rs#L69-L72.

I'm not sure what the best option is. We could either wait until we no longer need to build C bindings for linux-api, but this would require moving more of the shim to rust. We could also split the rust bindings into two files, and only generate C bindings for one of the files, so this could be messy. We could also just manually write rust bindings for tcp_info if this is the only struct containing bitfields that we need to deal with.

@stevenengler stevenengler added Type: Enhancement New functionality or improved design Component: Build Build/install tools and dependencies labels Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Build Build/install tools and dependencies Type: Enhancement New functionality or improved design
Projects
None yet
Development

No branches or pull requests

1 participant