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

            Bug ID: 128751
           Summary: wrong code: x86-64 va_arg reads one slot too far after
                    an empty class with an unnamed bit-field passed on the
                    stack since r8-4853
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: 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

#include <cstdio>
#include <cstdarg>
struct E { unsigned : 8; };
long g(long a, long b, long c, long d, long f, long h, E e, ...) {
  va_list ap;
  va_start(ap, e);
  long x = va_arg(ap, long);
  va_end(ap);
  return x + a + b + c + d + f + h - 21;
}
int main() {
  printf("%ld\n", g(1, 2, 3, 4, 5, 6, E{}, 42L));
}

$ g++ -O0 t.cpp && ./a.out
140617609290114      (garbage; 0 on GCC 8.5 to 14.3; the C form prints 0)
$ g++ -O2 t.cpp && ./a.out
0
$ g++ -O2 -fabi-version=11 t.cpp && ./a.out
42

Expected 42: x is 42 and a + b + c + d + f + h - 21 is 0 (Clang gives 42; GCC
7.5 gives 42). Wrong at all optimization levels: the caller and the callee,
both compiled by GCC, disagree.

E has only an unnamed bit-field, so TYPE_EMPTY_P (E) is true (is_empty_type
skips DECL_PADDING_P fields). The six long arguments use all integer registers,
so E is a stack argument.

- Caller: E takes 0 bytes on the stack (calls.cc skips TYPE_EMPTY_P arguments;
locate_and_pad_parm gives size 0), so 42 is in the first stack slot.
- Callee: ix86_function_arg_advance computes words = CEIL (1, 8) = 1 for E, and
function_arg_advance_64 (config/i386/i386.cc) does cum->words += words, because
classify_argument gives the bit-field an INTEGER class and no register is left.
ix86_va_start then sets overflow_arg_area from crtl->args.info.words, one slot
after 42.

A plain `struct E {};` is correct (no class, no words added). With E in a
register (fewer named arguments) the result is also correct. The fix is
probably to add no words for a TYPE_EMPTY_P argument in function_arg_advance_64
(as ix86_function_arg_boundary already special-cases TYPE_EMPTY_P).

The empty-class passing came with r8-4853-g974aedcce8d (PR60336,
-fabi-version=12); 7.5 is correct and -fabi-version=11 gives 42.

The same happens in GNU C (struct with no named member):

#include <stdio.h>
#include <stdarg.h>
struct E { unsigned : 8; };
long g(long a, long b, long c, long d, long f, long h, struct E e, ...) {
  va_list ap;
  (void) e;
  va_start(ap, e);
  long x = va_arg(ap, long);
  va_end(ap);
  return x + a + b + c + d + f + h - 21;
}
int main(void) {
  struct E e = {};
  printf("%ld\n", g(1, 2, 3, 4, 5, 6, e, 42L));
}

-O0 -fsanitize=undefined,address with clang++ gives 42.

Cause: function_arg_advance_64 (config/i386/i386.cc) adds one word to
cum->words for E, although the generic argument layout (calls.cc,
locate_and_pad_parm) gives a TYPE_EMPTY_P argument 0 bytes. ix86_va_start
computes overflow_arg_area from crtl->args.info.words, so va_arg starts one
slot too far.

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

Possible fix: One possible fix is to add no words in ix86_function_arg_advance
/ function_arg_advance_64 for an argument with TYPE_EMPTY_P (arg.type), as
ix86_function_arg_boundary already special-cases TYPE_EMPTY_P. We are not sure
whether is_empty_type should instead treat a class with a non-zero-width
unnamed bit-field as not empty (the Itanium C++ ABI and Clang do).

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