Rust-bindgen: repr(C,packed(4)) type generated that transitively contains repr(align(4)), causing a build error

Created on 2 May 2019  路  3Comments  路  Source: rust-lang/rust-bindgen

So I've got some sdl2 bindings of my own that currently binds _only_ SDL.h, and I tried to add the SDL_syswm.h file to the bindings. However, when I did the following rust ended up being generated:

#[repr(C, packed(4))]
#[derive(Copy, Clone)]
pub struct _IMAGE_TLS_DIRECTORY64 {
  pub StartAddressOfRawData: ULONGLONG,
  pub EndAddressOfRawData: ULONGLONG,
  pub AddressOfIndex: ULONGLONG,
  pub AddressOfCallBacks: ULONGLONG,
  pub SizeOfZeroFill: DWORD,
  pub __bindgen_anon_1: _IMAGE_TLS_DIRECTORY64__bindgen_ty_1,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union _IMAGE_TLS_DIRECTORY64__bindgen_ty_1 {
  pub Characteristics: DWORD,
  pub __bindgen_anon_1: _IMAGE_TLS_DIRECTORY64__bindgen_ty_1__bindgen_ty_1,
  _bindgen_union_align: u32,
}
#[repr(C)]
#[repr(align(4))]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct _IMAGE_TLS_DIRECTORY64__bindgen_ty_1__bindgen_ty_1 {
  pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize], u32>,
}

Unfortunately this is actually an illegal setup

D:\dev\fermium>cargo build
   Compiling fermium v0.0.7 (D:\dev\fermium)
error[E0588]: packed type cannot transitively contain a `[repr(align)]` type
     --> D:\dev\fermium\target\debug\build\fermium-3d250ea95a1b04df\out/bindings.rs:62015:1
      |
62015 | / pub struct _IMAGE_TLS_DIRECTORY64 {
62016 | |   pub StartAddressOfRawData: ULONGLONG,
62017 | |   pub EndAddressOfRawData: ULONGLONG,
62018 | |   pub AddressOfIndex: ULONGLONG,
...     |
62021 | |   pub __bindgen_anon_1: _IMAGE_TLS_DIRECTORY64__bindgen_ty_1,
62022 | | }
      | |_^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0588`.
error: Could not compile `fermium`.

And so I come here, and I'm not sure how to move forward.

Is this a bug in bindgen not computing the alignment setup properly? Should I just blacklist the type from being generated? Something else?

Most helpful comment

For those, who face this issue, here is the list of the blacklisted types and functions, which worked for me:

...
            .blacklist_type("LPMONITORINFOEXA?W?")
            .blacklist_type("LPTOP_LEVEL_EXCEPTION_FILTER")
            .blacklist_type("MONITORINFOEXA?W?")
            .blacklist_type("PEXCEPTION_FILTER")
            .blacklist_type("PEXCEPTION_ROUTINE")
            .blacklist_type("PSLIST_HEADER")
            .blacklist_type("PTOP_LEVEL_EXCEPTION_FILTER")
            .blacklist_type("PVECTORED_EXCEPTION_HANDLER")
            .blacklist_type("_?L?P?CONTEXT")
            .blacklist_type("_?L?P?EXCEPTION_POINTERS")
            .blacklist_type("_?P?DISPATCHER_CONTEXT")
            .blacklist_type("_?P?EXCEPTION_REGISTRATION_RECORD")
            .blacklist_type("_?P?IMAGE_TLS_DIRECTORY.*")
            .blacklist_type("_?P?NT_TIB")
            .blacklist_type("tagMONITORINFOEXA")
            .blacklist_type("tagMONITORINFOEXW")
            .blacklist_function("AddVectoredContinueHandler")
            .blacklist_function("AddVectoredExceptionHandler")
            .blacklist_function("CopyContext")
            .blacklist_function("GetThreadContext")
            .blacklist_function("GetXStateFeaturesMask")
            .blacklist_function("InitializeContext")
            .blacklist_function("InitializeContext2")
            .blacklist_function("InitializeSListHead")
            .blacklist_function("InterlockedFlushSList")
            .blacklist_function("InterlockedPopEntrySList")
            .blacklist_function("InterlockedPushEntrySList")
            .blacklist_function("InterlockedPushListSListEx")
            .blacklist_function("LocateXStateFeature")
            .blacklist_function("QueryDepthSList")
            .blacklist_function("RaiseFailFastException")
            .blacklist_function("RtlCaptureContext")
            .blacklist_function("RtlCaptureContext2")
            .blacklist_function("RtlFirstEntrySList")
            .blacklist_function("RtlInitializeSListHead")
            .blacklist_function("RtlInterlockedFlushSList")
            .blacklist_function("RtlInterlockedPopEntrySList")
            .blacklist_function("RtlInterlockedPushEntrySList")
            .blacklist_function("RtlInterlockedPushListSListEx")
            .blacklist_function("RtlQueryDepthSList")
            .blacklist_function("RtlRestoreContext")
            .blacklist_function("RtlUnwindEx")
            .blacklist_function("RtlVirtualUnwind")
            .blacklist_function("SetThreadContext")
            .blacklist_function("SetUnhandledExceptionFilter")
            .blacklist_function("SetXStateFeaturesMask")
            .blacklist_function("UnhandledExceptionFilter")
            .blacklist_function("__C_specific_handler")
...

All 3 comments

What's the C code for those? Looks like they come from some windows headers... This needs rust compiler work, otherwise there isn't going to be a sane way of getting this right. https://github.com/rust-lang/rust/issues/59154 tracks that.

This is basically #1538 (so closing as a dupe of that). Depending on use-case, workaround can be either flag that struct as opaque (with opaque_type) or blacklist it if it's not used at all.

Hi, I had the same issue compiling windows.h.
If I compile my hyper-v-sys crate without function whitelisting, it fails with this error:
error_bindgen

I can show the generated bindings also, but I guess it's an issue with the Rust compiler, as you said.

Thanks.

For those, who face this issue, here is the list of the blacklisted types and functions, which worked for me:

...
            .blacklist_type("LPMONITORINFOEXA?W?")
            .blacklist_type("LPTOP_LEVEL_EXCEPTION_FILTER")
            .blacklist_type("MONITORINFOEXA?W?")
            .blacklist_type("PEXCEPTION_FILTER")
            .blacklist_type("PEXCEPTION_ROUTINE")
            .blacklist_type("PSLIST_HEADER")
            .blacklist_type("PTOP_LEVEL_EXCEPTION_FILTER")
            .blacklist_type("PVECTORED_EXCEPTION_HANDLER")
            .blacklist_type("_?L?P?CONTEXT")
            .blacklist_type("_?L?P?EXCEPTION_POINTERS")
            .blacklist_type("_?P?DISPATCHER_CONTEXT")
            .blacklist_type("_?P?EXCEPTION_REGISTRATION_RECORD")
            .blacklist_type("_?P?IMAGE_TLS_DIRECTORY.*")
            .blacklist_type("_?P?NT_TIB")
            .blacklist_type("tagMONITORINFOEXA")
            .blacklist_type("tagMONITORINFOEXW")
            .blacklist_function("AddVectoredContinueHandler")
            .blacklist_function("AddVectoredExceptionHandler")
            .blacklist_function("CopyContext")
            .blacklist_function("GetThreadContext")
            .blacklist_function("GetXStateFeaturesMask")
            .blacklist_function("InitializeContext")
            .blacklist_function("InitializeContext2")
            .blacklist_function("InitializeSListHead")
            .blacklist_function("InterlockedFlushSList")
            .blacklist_function("InterlockedPopEntrySList")
            .blacklist_function("InterlockedPushEntrySList")
            .blacklist_function("InterlockedPushListSListEx")
            .blacklist_function("LocateXStateFeature")
            .blacklist_function("QueryDepthSList")
            .blacklist_function("RaiseFailFastException")
            .blacklist_function("RtlCaptureContext")
            .blacklist_function("RtlCaptureContext2")
            .blacklist_function("RtlFirstEntrySList")
            .blacklist_function("RtlInitializeSListHead")
            .blacklist_function("RtlInterlockedFlushSList")
            .blacklist_function("RtlInterlockedPopEntrySList")
            .blacklist_function("RtlInterlockedPushEntrySList")
            .blacklist_function("RtlInterlockedPushListSListEx")
            .blacklist_function("RtlQueryDepthSList")
            .blacklist_function("RtlRestoreContext")
            .blacklist_function("RtlUnwindEx")
            .blacklist_function("RtlVirtualUnwind")
            .blacklist_function("SetThreadContext")
            .blacklist_function("SetUnhandledExceptionFilter")
            .blacklist_function("SetXStateFeaturesMask")
            .blacklist_function("UnhandledExceptionFilter")
            .blacklist_function("__C_specific_handler")
...
Was this page helpful?
0 / 5 - 0 ratings