Skip to content

Fix union bitfield storage and layout - #3531

Merged
emilio merged 3 commits into
rust-lang:mainfrom
1ndahous3:fix-union-bitfield-layout
Nov 23, 2026
Merged

emilio merged 3 commits into
rust-lang:mainfrom
1ndahous3:fix-union-bitfield-layout

Conversation

@1ndahous3

Copy link
Copy Markdown
Contributor

Fixes #3184.

Overlapping union bitfields can produce undersized storage when a narrower member follows a wider one. Native unions can also be smaller than their C layout when bitfields occupy fewer bytes than their allocation unit.

This is not MSVC-specific: on the GNU ABI, alignment can preserve the correct union size while bitfield accessors still fail because the underlying storage is too small.

This also surfaced in a real-world project using native Windows structures from PHNT (x64/MSVC, generated size vs. C size):

Track the maximum bitfield extent and add a full-size, byte-aligned padding member where needed, preserving Clang-provided offsets and existing alignment handling.

Regression tests cover #3184, varying bit widths, packed layouts, and C/Rust accessor and by-value ABI round trips.

@emilio emilio left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks reasonable, some questions about the tests tho.

View changes since this review

@@ -0,0 +1,3 @@
// bindgen-flags: --raw-line '#![cfg(target_env = "msvc")]' -- --target=x86_64-pc-windows-msvc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're not running these on msvc at all right now...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, CI compares the MSVC-targeted output against the snapshot, but doesn't compile its layout assertions. I've kept this fixture for MSVC-specific codegen coverage.

@@ -0,0 +1,11 @@
// bindgen-flags: --bindgen-wrapper-union 'WithBindgenWrapper' --no-copy 'NonCopyType' --raw-line '#![cfg(target_env = "msvc")]' -- --target=x86_64-pc-windows-msvc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to restrict these / run these in msvc? I guess it doesn't hurt to test the MSVC layout?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the restriction wasn't necessary. Removed the forced MSVC target and cfg, and regenerated the expectation. The separate MSVC layout fixture remains.


// Bitfields can occupy fewer bytes than their C allocation unit. Union
// members overlap, so this field must cover the full size, not the gap.
Some(self.padding_field(Layout::new(layout.size, 1)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we're relying on the alignment being correct already, seems fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, alignment is handled by the existing requires_explicit_align path. The padding field only ensures sufficient storage.

@emilio
emilio added this pull request to the merge queue Oct 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 28, 2026
@1ndahous3

Copy link
Copy Markdown
Contributor Author

@emilio FYI automatic merge was canceled (unrelated CI job error)

thestinger pushed a commit to GrapheneOS/platform_external_rust_android-crates-io that referenced this pull request Nov 21, 2026
Upstream: c799f9e73b9041d43033dfffcaeeec1c1ba6b444
Source: rust-lang/rust-bindgen#3531
@emilio
emilio added this pull request to the merge queue Nov 23, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 23, 2026
@emilio
emilio added this pull request to the merge queue Nov 23, 2026
Merged via the queue into rust-lang:main with commit a2f8322 Nov 23, 2026
68 checks passed
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

Successfully merging this pull request may close these issues.

Wrong code generated for union with bitfields

2 participants