https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128921

            Bug ID: 128921
           Summary: x86-64 ABI: packed struct with an unaligned
                    bit-field-only struct member (or _BitInt(33) member)
                    passed and returned in registers, not in memory
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ABI, wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

Testcase 1 (C++; the C form is the same with `struct B s;` and an initializer
`{ 7, { -5 } }`):

extern "C" int printf(const char *, ...);
struct B { int b : 5; };
#pragma pack(push, 1)
struct S { char a; B s; };
#pragma pack(pop)
#ifndef CALLER
void f(S s) { printf("%d %d\n", s.a, s.s.b); }
#else
void f(S);
#endif
#ifndef CALLEE
int main() { S s; s.a = 7; s.s.b = -5; f(s); }
#endif

Caller and callee in separate objects (-O0), expected "7 -5":

| caller \ callee | gcc | clang |
|---|---|---|
| gcc | 7 -5 | 0 0 |
| clang | 1 0 | 7 -5 |

With `struct B { int b; }` (no bit-field) all four combinations give 7 -5: GCC
passes that S in memory. With the bit-field, GCC passes S in %rdi (and returns
it in %rax); Clang uses byval / sret memory.

psABI 3.2.3: "If the size of an object is larger than eight eightbytes, or it
contains unaligned fields, it has class MEMORY." The member s (alignment 4) is
at byte 1, so it is unaligned.

The return value has the same problem: for `S g(void)` returning {7, {-5}}, GCC
returns S in %rax, Clang through a hidden pointer (sret); mixing them gives "0
0" (gcc caller, clang callee) or garbage (clang caller, gcc callee).

Testcase 2 (C, _BitInt; also needs packed):

int printf(const char *, ...);
typedef struct __attribute__((packed)) {
  char a;
  unsigned _BitInt(33) b;
} S;
#ifndef CALLER
void named(S s)
{
  printf("n %llu %llu\n", (unsigned long long)s.a,
         (unsigned long long)s.b);
}
#else
void named(S);
#endif
#ifndef CALLEE
int main(void)
{
  S s = { .a = 3, .b = 1 };
  named(s);
  return 0;
}
#endif

Expected "n 3 1". gcc caller + clang callee: "n 0 0"; clang caller + gcc
callee: "n 1 0". With `unsigned long long b` all four combinations agree
(memory).

Cause: classify_argument (config/i386/i386.cc). Misalignment is only checked at
scalar leaves (`if (bit_offset % mode_alignment) return 0;`). Two paths skip
that check:

1. Testcase 1: the RECORD_TYPE loop recurses into struct B, whose only field is
a bit-field. "Bitfields are always classified as integer. Handle them early,
since later code would consider them to be misaligned integers" gives INTEGER
without any alignment check, so TYPE_ALIGN (B) = 32 at bit offset 8 is never
compared.
2. Testcase 2: words = CEIL (bytes + (bit_offset % 64) / 8, UNITS_PER_WORD)
includes the bit offset, so the 33-bit field at bit offset 8 has words = 2, and
`BITINT_TYPE_P (type) && words > 1` (meant for _BitInt(N > 64)) sends it to
`case BITINT_TYPE:`, which returns two INTEGER classes.

A check in the RECORD_TYPE (and ARRAY_TYPE) loop that returns 0 (MEMORY) when a
non-bit-field field's bit position is not a multiple of TYPE_ALIGN of its type
would catch both (TYPE_ALIGN of _BitInt(33) is 64). For testcase 2 alone,
testing the _BitInt size without the bit offset (int_size_in_bytes (type) > 8)
would also work.

Fails in all releases we tested back to 7.5 for testcase 1 and 14.3 (the first
release with _BitInt) for testcase 2, so there is no good version to bisect
from.

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to