Do not insert dummy fields into bitfields in cases where this would cause misalignment. - #3431
Open
flowerhack wants to merge 1 commit into
Open
Do not insert dummy fields into bitfields in cases where this would cause misalignment.#3431flowerhack wants to merge 1 commit into
flowerhack wants to merge 1 commit into
Conversation
flowerhack
force-pushed
the
fix-bindgen-alignment
branch
from
August 12, 2026 22:46
f4ecf5c to
f3a08ef
Compare
cause misalignment.
Suppose you have the following C++ code:
```
// reproduce.h
struct __attribute__((aligned(8))) StructWithBitfieldAndDouble {
unsigned int bitfield: 32;
double standard_field;
};
// reproduce.cpp
#include "reproduce.h"
#include <iostream>
int main() {
std::cout << "cpp_size=" << sizeof(SomeStruct) << std::endl;
std::cout << "cpp_align=" << alignof(SomeStruct) << std::endl;
return 0;
}
```
As of right now, Bindgen will produce the following Rust code:
```
#[repr(C)]
#[repr(align(8))]
#[derive(Debug, Default, Copy, Clone)]
pub struct StructWithBitfieldAndDouble {
pub _bindgen_align: [u64; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8, 4usize]>,
pub standard_field: f64,
}
```
If Bindgen compiles this targeting a 32-bit architecture, the C++ struct
will be 16 bytes in size (as one would expect, based on the `aligned(8)`),
but the Rust struct will be only 12 bytes in size (because the alignment
of u64 is 4, so _bindgen_align, as a zero-length struct, ends up
occupying 0 bytes of space, and the struct's overall size will thus be
12).
This occurs because of the way dummy fields are added to bitfields as
per rust-lang#3247.
We should check that the target primitive's alignment is
greater-than-or-equal-to the explicitly-defined alignment in these
cases, and fall back to simply using the explicit alignment in those
cases.
flowerhack
force-pushed
the
fix-bindgen-alignment
branch
from
August 12, 2026 22:53
f3a08ef to
191f933
Compare
Author
|
r? @pvdrz Hi, I'm new! but I read the CONTRIBUTING.md and believe I'm following all the guidelines there; let me know if I missed anything. I didn't see any kind of LLM policy, but, in case there is one and I missed it:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Suppose you have the following C++ code:
As of right now, Bindgen will produce the following Rust code:
If Bindgen compiles this targeting a 32-bit architecture, the C++ struct will be 16 bytes in size (as one would expect, based on the
aligned(8)), but the Rust struct will be only 12 bytes in size (because the alignment of u64 is 4, so _bindgen_align, as a zero-length struct, ends up occupying 0 bytes of space, and the struct's overall size will thus be 12).This occurs because of the way dummy fields are added to bitfields as per #3247.
We should check that the target primitive's alignment is greater-than-or-equal-to the explicitly-defined alignment in these cases, and fall back to simply using the explicit alignment in those cases.