rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Invalid c struct bitfield generation in some cases

Open Strackeror opened this issue 1 year ago • 2 comments

Discussed in https://github.com/rust-lang/rust-bindgen/discussions/3104

So, i'm trying to run bindgen from a pokemon decomp project, and having issues with some structs not passing the sanity checks.

I've pinpointed it to an alignment issue on some structs with bitfields, here is a minimal reproduction:

struct Test
{
    char a;
    char b:4;
    char c:4; 

    short x:6;
    short y:10;
};

clang/gcc make that struct fit into 4 bytes, but bindgen adds a full byte of padding between c and x

I've experimented quickly and stepped through the code for handling bitfields in bindgen/ir/comp.rs and I'm pretty sure it's an issue with bindgen's handling. I think it's because bitfield groups are constructed as if they are already starting aligned, but I'm an informed layman at best.

Strackeror avatar Feb 01 '25 16:02 Strackeror

Yeah this looks like a bug. Unfortunately our bitfield generation is kinda reverse-engineered from LLVM, it seems we're getting that case wrong. Fixing and adding it as a test would be great.

emilio avatar Feb 01 '25 20:02 emilio

This might be a dupe of https://github.com/rust-lang/rust-bindgen/issues/1377

emilio avatar Feb 01 '25 20:02 emilio