Skip to content

Opaque structs not generating with right size #3177

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
leon-xd opened this issue Mar 22, 2025 · 1 comment
Open

Opaque structs not generating with right size #3177

leon-xd opened this issue Mar 22, 2025 · 1 comment

Comments

@leon-xd
Copy link

leon-xd commented Mar 22, 2025

Hello bindgen team! I am having some issues with bindings generated from Windows USB headers defined in ucxusbdevice.h. Here is a link to a minimal repro I've been able to generate: https://github.com/leon-xd/rust-repros/tree/main/crates/bindgen-size-overflow. I copied relevant definitions and made a new header file that is able to reproduce the issue.

There are four structs that are not generated with the right definition. It seems like they are generated as opaque, but do not have the correct size. In switching from bindgen 0.69.* to 0.71.0 we were able to detect this issue with the introduction of compile time error checking.

A (preferably reduced) C/C++ header file that reproduces the issue

The bindgen flags used to reproduce the issue with the header file

The expected bindgen output

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _USBDEVICE_ABORTIO {
    pub _address: _USBDEVICE_MGMT_HEADER,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of _USBDEVICE_ABORTIO"][::std::mem::size_of::<_USBDEVICE_ABORTIO>() - 24usize];
    ["Alignment of _USBDEVICE_ABORTIO"][::std::mem::align_of::<_USBDEVICE_ABORTIO>() - 8usize];
};

The actual bindgen output

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _USBDEVICE_ABORTIO {
    pub _address: u8,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of _USBDEVICE_ABORTIO"][::std::mem::size_of::<_USBDEVICE_ABORTIO>() - 24usize];
    ["Alignment of _USBDEVICE_ABORTIO"][::std::mem::align_of::<_USBDEVICE_ABORTIO>() - 8usize];
};

The debugging logs generated when running bindgen on this testcase

  • The output of cargo build -vvv is here: logs.txt
@wmmc88
Copy link

wmmc88 commented Mar 23, 2025

It looks like MSVC (and Clang w/ -ms-extensions) treat USBDEVICE_MGMT_HEADER as an anonymous struct member. I think that bindgen is failing to recognize that, and is then assuming the type is ZeroSized which is why it generates that _address member (I think).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants