On Sun, Jan 21, 2024 at 01:19:24PM -0500, Kent Overstreet wrote:
> On Sun, Jan 21, 2024 at 09:11:24AM -0700, Thomas Bertschinger wrote:
> > I made a script to compare the size and alignment of bcachefs structs
> > in C vs. in Rust generated by the patched, lossy bindgen. All sizes
> > were the same, but the following types had different alignment:
> > 
> That right there is really good news. If we can add that script to the
> tests in bcachefs-tools, we'll already be in better shape than we were.
> 
> I wonder if it would be possible to upstream that check into bindgen.

I did this with an awk script that grabs the structs from the bcachefs
C headers and outputs code in Rust and C to dump the sizes and
alignments. I then manually added calls to the generated functions into
the bcachefs utility and diffed the output. I'll try to get this into a
form more suitable for an automated test, and hopefully submit a patch
to bcachefs-tools soon...

Enhancing bindgen to do something like this automatically also sounds
like a good idea but it will take me longer to figure out how to do
that. I think it could also be good to incorporate explicit member
offset checks with this. Handling that with an awk script or similar
seems challenging to me with how bindgen mangles bitfield names.

> Also - you tracked down the difference between microsoft and gcc
> __attribute__((align)), let me try to recap (and tell me if I get it
> wrong): with gcc, __align() works like any naturally aligned type, but
> Microsoft's version overrides __packed on a containing structure.
> 
> I think there's a strong argument to be made that Microsoft is the weird
> one here and Rustc should just provide the gcc behaviour when mixing
> packed and align...

I don't have access to a Microsoft environment to test anything, so
everything that I think I know about Microsoft semantics is based on what
I've read on various rust-lang threads, like [1][2]

With that disclaimer, from what I can tell it seems like Microsoft's
semantics are strictly less expressive than gcc's. I think gcc can
represent any struct that Microsoft can (adding padding by hand
may be necessary in some cases, but it's always possible), whereas
there are structures representable in gcc that cannot be represented
with the Microsoft semantics as presented in the cited rust-lang
threads. (Given that I haven't tested the MS compiler, I could be
totally wrong about this.)

[1] https://github.com/rust-lang/rust/issues/59154#issuecomment-476408300
[2] https://github.com/rust-lang/rust/issues/33158

- Thomas Bertschinger

Reply via email to