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

Layout error with struct that includes __attribute__((aligned(8))) #2101

Open
asomers opened this issue Sep 20, 2021 · 0 comments
Open

Layout error with struct that includes __attribute__((aligned(8))) #2101

asomers opened this issue Sep 20, 2021 · 0 comments

Comments

@asomers
Copy link

asomers commented Sep 20, 2021

I encountered this error when trying to generate bindings for Fio.

Input C/C++ Header

typedef long a;
typedef b;
typedef a c;
typedef struct {
  union {
    c d
  };
} e;
struct thread_stat {
  b f;
  e g;
  c h __attribute__((aligned(8)))
} __attribute__((packed));

Bindgen Invocation

bindgen --whitelist-type=thread_stat isolated-test-case.h

Actual Results

---- ffi::bindgen_test_layout_thread_stat stdout ----
thread 'ffi::bindgen_test_layout_thread_stat' panicked at 'assertion failed: `(left == right)`
  left: `8`,
 right: `4`: Offset of field: thread_stat::g', src/ffi.rs:81:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    ffi::bindgen_test_layout_thread_stat

and/or

Expected Results

No errors from the layout tests

asomers added a commit to bfffs/bfffs that referenced this issue Apr 21, 2022
But bindgen is producing the wrong struct layout for thread_data.
It calculates the wrong offset for client_type.

Include the layout tests after all, to help catch bugs like this.

The layout bug was previously just an annoyance, but with fio-3.30 it
causes failures at runtime.

rust-lang/rust-bindgen#2101
asomers added a commit to bfffs/bfffs that referenced this issue Apr 21, 2022
bindgen generates incorrect code for C structs that use
`__attribute__((aligned(8)))`.  Work around that bug by changing some
fields's types manually, to give them a different alignment.  bfffs-fio
doesn't use those fields anyway.

This change may need to be reapplied after any future fio API updates.

rust-lang/rust-bindgen#2101
asomers added a commit to bfffs/bfffs that referenced this issue Apr 21, 2022
bindgen produces the wrong struct layout for thread_data.  It calculates
the wrong offset for client_type.  Workaround the bug by binding the
offending type as opaque.  In that case, bindgen calculates its size
correctly.

Include the layout tests after all, to help catch bugs like this.

The layout bug was previously just an annoyance, but with fio-3.30 it
causes failures at runtime.

rust-lang/rust-bindgen#2101
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

1 participant