Skip to contentSkip to conversation

build: force gcc to initialize padding bits

PATCH Superseded Stephen Hemminger 4 participants 8 messages Jan 23, 2025 – Feb 11, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 build: force gcc to initialize padding bits

With GCC 15, the compiler has changed the default behavior when initialization is used for aggregate variables. The new default is to follow the standard (C23) and not initialize everything by default. This breaks assumptions in some drivers and can be lead to other bugs.

Use the new zero initialization flag to force the old behavior of initializing everything to zero.

---
 config/meson.build | 3 +++
 1 file changed, 3 insertions(+)
config/meson.build +3 -0
index 6aaad6d8a4..5c8b5a15f5 100644
@@ -330,6 +330,9 @@ warning_flags = [
         # globally disabled warnings
         '-Wno-packed-not-aligned',
         '-Wno-missing-field-initializers',
+
+        # guarantee that everything is zero when using initialization
+        '-fzero-init-padding-bits=all',
 ]
 
 if not dpdk_conf.get('RTE_ARCH_64')
Signature · 1 line
2.45.2
Acked-by on v1 1/1
#1 lore raw

config/meson.build warning_flags = [

Quoted 22 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 23 January 2025 18.21

With GCC 15, the compiler has changed the default behavior when initialization is used for aggregate variables. The new default is to follow the standard (C23) and not initialize everything by default. This breaks assumptions in some drivers and can be lead to other bugs.

Use the new zero initialization flag to force the old behavior of initializing everything to zero.

---
 config/meson.build | 3 +++
 1 file changed, 3 insertions(+)
config/meson.build +0 -0
index 6aaad6d8a4..5c8b5a15f5 100644
@@ -330,6 +330,9 @@ warning_flags = [

Is warning_flags the right location for this?
Alternatively, should warning_flags be renamed?

config/meson.build warning_flags = [ · line 334

Quoted 5 lines
config/meson.build +2 -0
         # globally disabled warnings
         '-Wno-packed-not-aligned',
         '-Wno-missing-field-initializers',
+
+        # guarantee that everything is zero when using initialization

Maybe add ", like in the C23 standard" to the comment.

Quoted 6 lines
config/meson.build +1 -1
+        '-fzero-init-padding-bits=all',
 ]
 
 if not dpdk_conf.get('RTE_ARCH_64')
--

2.45.2

I have read up on -fzero-init-padding-bits, and this is the correct solution.

With or without suggested changes:

#2 lore raw
Quoted 17 lines · Morten Brørup
Quoted 17 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 23 January 2025 18.21

With GCC 15, the compiler has changed the default behavior when initialization is used for aggregate variables. The new default is to follow the standard (C23) and not initialize everything by default. This breaks assumptions in some drivers and can be lead to other bugs.

Use the new zero initialization flag to force the old behavior of initializing everything to zero.

---
 config/meson.build | 3 +++
 1 file changed, 3 insertions(+)
Quoted 9 lines

config/meson.build warning_flags = [

Quoted 5 lines
config/meson.build +0 -0
index 6aaad6d8a4..5c8b5a15f5 100644
@@ -330,6 +330,9 @@ warning_flags = [

Is warning_flags the right location for this?
Alternatively, should warning_flags be renamed?

+1 to renaming warning flags, to e.g. "global_cflags"?

Quoted 8 lines

config/meson.build warning_flags = [ · line 334

Quoted 5 lines
config/meson.build +2 -0
         # globally disabled warnings
         '-Wno-packed-not-aligned',
         '-Wno-missing-field-initializers',
+
+        # guarantee that everything is zero when using initialization

Maybe add ", like in the C23 standard" to the comment.

Quoted 9 lines
Quoted 6 lines
config/meson.build +1 -1
+        '-fzero-init-padding-bits=all',
 ]
 
 if not dpdk_conf.get('RTE_ARCH_64')
--

2.45.2

I have read up on -fzero-init-padding-bits, and this is the correct solution.

Does this flag give us additional guarantees of padding being zero-initialized that were there before? From my reading of the gcc doc[1], "..padding-bits=union" corresponds to the old behaviour, right?

This also means we will have different padding behaviour on clang and gcc, since clang (at least v18 on my board) doesn't support this flag. Do we see any issues with that?

/Bruce

[1] https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html

Quoted 3 lines

With or without suggested changes:

#3 lore raw
Quoted 11 lines · On Fri, 24 Jan 2025 09:38:20 +0000 Bruce Richardson…
Quoted 1 line

Does this flag give us additional guarantees of padding being zero-initialized that were there before? From my reading of the gcc doc[1], "..padding-bits=union" corresponds to the old behaviour, right?

This also means we will have different padding behaviour on clang and gcc, since clang (at least v18 on my board) doesn't support this flag. Do we see any issues with that?

/Bruce

I chose the setting based on some email with the Linux kernel hardening project and their choice.

Clang decided to fix and just do the right thing.

https://github.com/llvm/llvm-project/commit/7a086e1b2dc05f54afae3591614feede727601fa

#4 lore raw
Quoted 15 lines · Stephen Hemminger
Quoted 11 lines · On Fri, 24 Jan 2025 09:38:20 +0000 Bruce Richardson…
Quoted 1 line

Does this flag give us additional guarantees of padding being zero-initialized that were there before? From my reading of the gcc doc[1], "..padding-bits=union" corresponds to the old behaviour, right?

This also means we will have different padding behaviour on clang and gcc, since clang (at least v18 on my board) doesn't support this flag. Do we see any issues with that?

/Bruce

Quoted 7 lines

I chose the setting based on some email with the Linux kernel hardening project and their choice.

Clang decided to fix and just do the right thing.

https://github.com/llvm/llvm-project/commit/7a086e1b2dc05f54afae3591614feede727601fa

Excellent. Agree on initializing everything being "the right thing".

#5 lore raw

PATCH v2 1/1 build: force gcc to initialize padding bits

In reply to Stephen Hemminger

With GCC 15, the compiler has changed the default behavior when initialization is used for aggregate variables. The new default is to follow the standard (C23) and not initialize everything by default. This breaks assumptions in some drivers and can be lead to other bugs. Use the new zero initialization flag to force of all fields to zero.

Note: other compilers always initialize to zero in these cases. Only GCC seems to have decided to start initializing less as a silly attempt at optimization.

---
 config/meson.build | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
config/meson.build +7 -4
index 6aaad6d8a4..b6b3558e11 100644
@@ -310,7 +310,7 @@ endif
 # enable extra warnings and disable any unwanted warnings
 # -Wall is added by default at warning level 1, and -Wextra
 # at warning level 2 (DPDK default)
-warning_flags = [
+global_cflags = [
         # additional warnings in alphabetical order
         '-Wcast-qual',
         '-Wdeprecated',
@@ -330,19 +330,22 @@ warning_flags = [
         # globally disabled warnings
         '-Wno-packed-not-aligned',
         '-Wno-missing-field-initializers',
+
+        # guarantee that all non-initialized parts of structure/union are zero
+        '-fzero-init-padding-bits=all',
 ]
 
 if not dpdk_conf.get('RTE_ARCH_64')
 # for 32-bit, don't warn about casting a 32-bit pointer to 64-bit int - it's fine!!
-    warning_flags += '-Wno-pointer-to-int-cast'
+    global_cflags += '-Wno-pointer-to-int-cast'
 endif
 if cc.get_id() == 'intel'
     warning_ids = [181, 188, 2203, 2279, 2557, 3179, 3656]
     foreach i:warning_ids
-        warning_flags += '-diag-disable=@0@'.format(i)
+        global_cflags += '-diag-disable=@0@'.format(i)
     endforeach
 endif
-foreach arg: warning_flags
+foreach arg: global_cflags
     if cc.has_argument(arg)
         add_project_arguments(arg, language: 'c')
     endif
Signature · 1 line
2.45.2
Acked-by on v2 1/1
#6 lore raw
Quoted 7 lines · Stephen Hemminger

With GCC 15, the compiler has changed the default behavior when initialization is used for aggregate variables. The new default is to follow the standard (C23) and not initialize everything by default. This breaks assumptions in some drivers and can be lead to other bugs. Use the new zero initialization flag to force of all fields to zero.

Quoted 6 lines

Note: other compilers always initialize to zero in these cases. Only GCC seems to have decided to start initializing less as a silly attempt at optimization.

#7 lore raw
In reply to Stephen Hemminger
Quoted 8 lines · Stephen Hemminger

With GCC 15, the compiler has changed the default behavior when initialization is used for aggregate variables. The new default is to follow the standard (C23) and not initialize everything by default. This breaks assumptions in some drivers and can be lead to other bugs. Use the new zero initialization flag to force of all fields to zero.

Quoted 6 lines

Note: other compilers always initialize to zero in these cases. Only GCC seems to have decided to start initializing less as a silly attempt at optimization.

Applied, thanks.

Signature · 1 line
David Marchand