https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129078
Bug ID: 129078
Summary: -fzero-init-padding-bits=all: C++ aggregate
initialization with non-constant initializers leaves
padding bits uninitialized
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: ---
Target: x86_64-pc-linux-gnu
With -fzero-init-padding-bits=all, a C++ aggregate initialized with
non-constant expressions does not get its inter-member or tail padding bits
zeroed: neither a whole-object clear nor any store to the padding bytes is
emitted. This occurs at all optimization levels, including -O0, and under
-std=c++17, -std=c++20, and -std=c++23.
In contrast, initializing the exact same aggregate type with constant
expressions, or omitting trailing members from the initializer list, zeroes the
padding as documented.
Testcase (self-contained, no headers required, clean under -Wall -Wextra):
----------------------------------------------------------------------
struct S2 { int m3; };
struct T { double m1; S2 n4; };
struct T5 { S2 n; double m1; };
volatile double v1 = 2.5;
volatile int v2 = 3;
__attribute__((noinline)) static void dirty(void) {
volatile unsigned char b[8192];
for (unsigned i = 0; i < sizeof b; i++)
b[i] = (unsigned char)(i * 181 + 0x5A);
}
__attribute__((noinline)) static void show(const char *tag, const void *p,
unsigned long n) {
__builtin_printf("%s ", tag);
const unsigned char *q = (const unsigned char *)p;
for (unsigned long i = 0; i < n; i++)
__builtin_printf("%02x", q[i]);
__builtin_printf("\n");
}
__attribute__((noinline)) static void t1(void) { T x = {v1, {v2}}; show("t1",
&x, sizeof x); }
__attribute__((noinline)) static void t2(void) { T x = {2.5, {3}}; show("t2",
&x, sizeof x); }
__attribute__((noinline)) static void t4(void) { T5 y = {{v2}, v1}; show("t4",
&y, sizeof y); }
int main() {
dirty(); t1();
dirty(); t2();
dirty(); t4();
return 0;
}
----------------------------------------------------------------------
Actual vs. expected output on x86-64:
$ g++ -std=c++17 -Wall -Wextra -O2 -fzero-init-padding-bits=all zp5_repro.cpp
&& ./a.out
t1 000000000000044003000000863bf0a5
t2 00000000000004400300000000000000
t4 03000000de9348fd0000000000000440
In t1, bytes 12..15 (tail padding of T) contain stale stack data (863bf0a5).
In t2 (constant initializer), bytes 12..15 are zeroed (00000000).
In t4, bytes 4..7 (inter-member padding between n and m1) contain stale stack
data (de9348fd).
$ g++ -std=c++17 -Wall -Wextra -O0 -fzero-init-padding-bits=all zp5_repro.cpp
&& ./a.out
t1 00000000000004400300000000200000
t2 00000000000004400300000000000000
t4 03000000000004400000000000000440
At -O0 with the option, bytes 12..15 of t1 (00200000) and bytes 4..7 of t4
(00000440) are not written at all and retain prior stack residue.
The same behavior is observed at -O1, -O3, and -Os, as well as under -std=c++20
and -std=c++23.
Running either the -O0 or the -O2 build under Valgrind (valgrind -q
--error-exitcode=77 ./a.out) exits with code 77 (Use of uninitialised value of
size 8).
Compiler version (g++ -v):
Using built-in specs.
COLLECT_GCC=/home/ko1sh1/resource/gcc-16.2/bin/g++
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:
1. Generated assembly for t1 at -O0 with -fzero-init-padding-bits=all:
subq $16, %rsp
movsd v1(%rip), %xmm0
movsd %xmm0, -16(%rbp)
movl v2(%rip), %eax
movl %eax, -8(%rbp)
There is no clear of the 16-byte object and no store to bytes 12..15
(-4(%rbp)).
2. In GCC 16.2.0, flag_zero_init_padding_bits is only checked in three places
in gcc/expr.cc (:7285, :7366, :7380), notably inside categorize_ctor_elements_1
(which causes store_constructor to clear the entire object first).
gcc/cp/init.cc has no reference to flag_zero_init_padding_bits. When a C++
aggregate initializer contains non-constant expressions, it is expanded member
by member rather than through that constructor-clearing path, so no padding
clear is emitted.
Variants tested (g++ 16.2, -std=c++17, -fzero-init-padding-bits=all):
- T x = {v1, {v2}}; -> tail padding uninitialized
- T x = {2.5, {3}}; or {2.5, S2{3}}; -> zeroed
- T x = {v1}; or {v1, {}}; -> zeroed
- T5 x = {{v2}, v1}; -> inter-member padding uninitialized
- struct T4 { char a; S2 n; char c; }; T4 x = {1, {v2}, 3}; -> zeroed
- struct U { char a; int b; char c; }; U x = {1, 2, 3}; -> zeroed
- T arr[2] = {{v1, {v2}}, {v1, {v2}}}; -> uninitialized in both elements
Security / hardening impact:
The documented contract of -fzero-init-padding-bits=all explicitly guarantees
zero initialization of inter-member and tail padding in aggregates to prevent
stale stack contents from leaking when objects are copied bytewise or
serialized across boundaries. Because aggregate initialization with runtime
expressions is overwhelmingly the common case in real-world C++ code, silently
omitting the clear across all optimization levels (including unoptimized -O0
debug builds) makes the option ineffective for those initializers and leaves
prior stack contents exposed in the padding bytes.
Additional notes:
- Also observed in generated assembly on g++ 15.1, 15.3, 16.1, 16.2, and trunk
(Compiler Explorer).
- Searches on Bugzilla (all statuses) for "padding bits" and
"zero-init-padding-bits" found no existing report linking C++ aggregate
initialization with non-constant initializers to this option.