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

            Bug ID: 129076
           Summary: -fzero-init-padding-bits=all: GIMPLE DSE removes
                    zero-initialization of trailing padding in x87 long
                    double member
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 220255624 at seu dot edu.cn
  Target Milestone: ---

With -fzero-init-padding-bits=all, an aggregate containing padding is
initialized by clearing the whole object and then storing the member values. At
-O1 and above, the GIMPLE dead store elimination pass (047t.dse1) treats a
store to a long double member as overwriting its full 16-byte TYPE_SIZE (128
bits) and narrows the preceding clear store to omit those bytes. However, on
x86-64 the x87 fstp store only writes 10 bytes (80 bits), leaving the 6
trailing internal padding bytes (offsets 26..31) uninitialized with stale stack
contents.

At -O0, or at -O2 with -fno-tree-dse, all padding bytes are properly zeroed.

Testcase (self-contained, no headers required, clean under -Wall -Wextra):

----------------------------------------------------------------------
struct S {
    char a;
    long double c;
};

volatile long double vv = 4.25L;

__attribute__((noinline)) static void dirty(void) {
    volatile unsigned char b[4096];
    for (unsigned i = 0; i < sizeof b; i++)
        b[i] = (unsigned char)(i * 181 + 0x5A);
}

__attribute__((noinline)) static void t(void) {
    struct S x = {1, vv};
    unsigned char *q = (unsigned char *)&x;
    for (int i = 0; i < (int)sizeof x; i++)
        __builtin_printf("%02x", q[i]);
    __builtin_printf("\n");
}

int main(void) {
    dirty();
    t();
    return 0;
}
----------------------------------------------------------------------

Actual vs. expected output on x86-64:

$ gcc -std=c23 -Wall -Wextra -O0 -fzero-init-padding-bits=all zp1_repro.c &&
./a.out
0100000000000000000000000000000000000000000000880140000000000000

$ gcc -std=c23 -Wall -Wextra -O1 -fzero-init-padding-bits=all zp1_repro.c &&
./a.out
0100000000000000000000000000000000000000000000880140cc8136eba055

$ gcc -std=c23 -Wall -Wextra -O2 -fzero-init-padding-bits=all zp1_repro.c &&
./a.out
01000000000000000000000000000000000000000000008801401cd1863bf0a5

(-O3 and -Os exhibit the same uninitialized bytes at offsets 26..31.)

$ gcc -std=c23 -Wall -Wextra -O2 -fno-tree-dse -fzero-init-padding-bits=all
zp1_repro.c && ./a.out
0100000000000000000000000000000000000000000000880140000000000000

Running under Valgrind (valgrind -q --error-exitcode=77 ./a.out):
  -O0 build: exits cleanly with code 0.
  -O2 build: exits with code 77 (Use of uninitialised value of size 8).

Compiler version (gcc -v):

Using built-in specs.
COLLECT_GCC=/home/ko1sh1/resource/gcc-16.2/bin/gcc
COLLECT_LTO_WRAPPER=/home/ko1sh1/resource/gcc-16.2/libexec/gcc/x86_64-pc-linux-gnu/16.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/home/ko1sh1/resource/gcc-16.2
--disable-multilib --enable-languages=c,c++ --disable-bootstrap
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.2.0 (GCC)

Diagnosis and optimizer behavior:

1. On x86-64, long double occupies 16 bytes (TYPE_SIZE = 128) with 80 value
bits (TYPE_PRECISION = 80). Offsets 26..31 hold no value representation bits,
are cleared by __builtin_clear_padding for the same type, and are explicitly
cleared by GCC itself at -O0 under -fzero-init-padding-bits=all.

2. With -fdump-tree-dse1, after dse1 the 32-byte aggregate clear is narrowed to
only the first 16 bytes:

  MEM <char[16]> [(struct S *)&x] = {};
  x.a = 1;
  vv.0_1 ={v} vv;
  x.c = vv.0_1;

In gcc/tree-ssa-dse.cc (initialize_ao_ref_for_dse, calling ao_ref_init (write,
lhs)), the write footprint for x.c = vv.0_1 is taken from the lvalue's
TYPE_SIZE (16 bytes) instead of the actual XFmode store width (10 bytes),
causing dse_optimize to falsely treat offsets 16..31 as completely overwritten.

3. Generated assembly at -O2 (-S -masm=intel):

  pxor    xmm0, xmm0
  sub     rsp, 32
  fld     TBYTE PTR vv[rip]
  movaps  XMMWORD PTR [rsp], xmm0
  mov     rbx, rsp
  mov     BYTE PTR [rsp], 1
  fstp    TBYTE PTR [rsp+16]

fstp writes only 10 bytes (offsets 16..25); offsets 26..31 are never written.

Security / hardening impact:

The purpose of -fzero-init-padding-bits=all is to prevent stale stack contents
from leaking through padding bytes when an aggregate is copied or written out
as raw bytes (e.g., serialization, IPC, or cross-boundary copies). Because DSE
removes the clear at -O1 and above, prior stack contents survive in offsets
26..31 and are exposed if the object is copied bytewise, silently defeating the
guarantee the option is intended to provide.

Additional notes:

- Constant initializer variant (struct S x = {1, 4.25L};): even with -O2
-fno-tree-dse, bytes 26..31 remain dirty on x86-64 because the RTL DSE pass
(303r.dse1) subsequently removes the second 16-byte clear store based on the
16-byte XFmode store footprint.
- _Complex long double member with runtime initializer at -O2 (x86-64): leaves
12 padding bytes uninitialized (6 bytes after each part).
- With -m32 (i386, where long double is 12 bytes at offset 4): at
-O1/-O2/-O3/-Os with -fzero-init-padding-bits=all, offsets 14..15 contain stale
stack data; at -O0 or with -fno-tree-dse, all padding bytes are zero.
- Also observed in generated assembly across GCC 15.1, 15.3, 16.1, 16.2, and
trunk (Compiler Explorer).

Reply via email to