Repository navigation
C front-end: fix unnamed bit-field alignment on x86-64 Linux - #9216
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #9216 +/- ##
========================================
Coverage 81.29% 81.29%
========================================
Files 1725 1725
Lines 191641 191655 +14
Branches 73 73
========================================
+ Hits 155786 155809 +23
+ Misses 35855 35846 -9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Many thanks for this. May I ask for the following: |
|
The rule is in the System V x86-64 psABI, section 3.1.2 (Bit-Fields): "Unnamed bit-fields' types do not affect the alignment of a structure or union." (source, page 20 of the current 1.0 PDF), and GCC 13.3 and Clang 18.1 on x86-64 Linux give exactly the sizes, alignments and offsets the test expects. The new commit applies clang-format to the changed lines, runs the test with the default target instead of forcing — ChipLab |
The System V AMD64 ABI excludes unnamed bit-field types from aggregate
alignment, section 3.1.2, Bit-Fields: "Unnamed bit-fields' types do not
affect the alignment of a structure or union." They still occupy space,
and zero-width fields still align the next member. Both forms could
previously raise aggregate alignment and add incorrect tail padding.
Exclude unnamed bit-fields from aggregate alignment and tail padding for
this target. For structs and unions that contain an unnamed bit-field,
also ignore synthetic padding when calculating alignment, since a
zero-width field can produce padding wider than the required alignment.
Ignoring padding components on all targets would also change unrelated
layouts: for example, union { char c[5]; int i; } would go from 8-byte
to 4-byte alignment on every architecture.
Add a regression covering both forms, unions, nested aggregates and a
named-field control. CBMC preprocesses with the host compiler, whose
predefined macros remain visible when --arch selects another target, so
the assertions are guarded with #if to only apply when CBMC models
x86-64 Linux (and the guard also excludes __aarch64__). The test passes
with the default target and with --arch i386 and --arch arm64.
Co-authored-by: Kiro <[email protected]>
alignment() took padding members into account when computing the
alignment of a struct or union. Padding members are derived from the
other members, and they only seemed harmless for structs because the
padding of a struct is always smaller than the alignment of the member
that caused it. For unions, however, add_padding adds a single padding
member that spans the entire union, and alignment() then used the size
of the union as its alignment. With GCC, Clang and Visual Studio alike,
union { char c[5]; int i; } has alignment 4, and a struct containing a
char followed by such a union has size 12. CBMC computed alignment 8 and
size 16. It even computed alignment 6, which is not a power of two, for
union { char c[5]; short s; }.
Skip padding members unconditionally. This also makes the special case
for aggregates that contain an unnamed bit-field unnecessary.
Co-authored-by: Kiro <[email protected]>
The rule that unnamed bit-fields do not affect the alignment of a structure or union is not specific to x86-64 Linux. The i386 psABI has the same sentence in its section 3.1.2, and GCC and Clang apply it on all targets other than those using the Microsoft ABI or AAPCS/AAPCS64 (Arm, other than on Apple platforms). Comparing against Clang's layouts for x86-64 and i386 Linux, x32, x86-64 and Arm macOS, x86-64 FreeBSD, RISC-V, PowerPC (32 and 64 bit), MIPS, s390x, SPARC64 and LoongArch Linux, CBMC (with the previous commits) got the layout of unnamed bit-fields wrong on all of these except for x86-64 Linux and x86-64 FreeBSD. On Arm Linux, FreeBSD and NetBSD CBMC already matched, as it did with Visual Studio for unnamed bit-fields of non-zero width. Make the predicate select the targets that deviate from the rule, and use a single helper, contributes_to_aggregate_alignment, at all three places that compute the alignment of an aggregate, so that they cannot diverge. Replace the regression test: it used to rely on the host's predefined macros, which do not follow --arch, so that it checked nothing on hosts other than x86-64 Linux. The new tests are header-free .i files that are type-checked against a fixed target, with expected layouts taken from Clang: positive cases for x86-64 and i386 Linux, RISC-V Linux and Arm macOS, and negative controls for Arm Linux (AAPCS and AAPCS64) and Visual Studio. Also add the out-of-bounds example from diffblue#9304, which CBMC did not report as the unnamed bit-field made the array appear twice its actual size. Fixes diffblue#9304 Co-authored-by: Kiro <[email protected]>
a829f98 to
4853dbe
Compare
On x86-64 Linux, CBMC gives this structure size 4 and alignment 4:
The System V AMD64 ABI excludes unnamed bit-field types from aggregate alignment, so the expected size is 2 and alignment is 1. The current psABI says it in section 3.1.2, Bit-Fields: "Unnamed bit-fields' types do not affect the alignment of a structure or union." (source). An unnamed zero-width
unsigned intbetween twocharmembers likewise gives size 8 instead of 5.We exclude unnamed bit-fields from aggregate alignment and tail padding, while preserving separator placement. The i386 psABI has the same rule in its section 3.1.2, and GCC and Clang follow it on all targets except those using the Microsoft ABI or AAPCS/AAPCS64 (Arm, other than on Apple platforms), so the change applies to all of those targets. Padding members no longer contribute to the alignment of a struct or union on any target, which also fixes the alignment of unions such as
union { char c[5]; int i; }(4, not 8). The regression tests are header-free and type-checked against fixed targets, with expected layouts taken from Clang and negative controls for Arm Linux and Visual Studio. They include the out-of-bounds write reported in #9304. The second and third commits were added during review.Fixes #9304
— ChipLab