Skip to contentSkip to conversation

kbuild: Enable GCC diagnostic context for value-tracking warnings

PATCH Superseded Kees Cook 3 participants 5 messages Nov 19, 2025 – Nov 21, 2025 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 kbuild: Enable GCC diagnostic context for value-tracking warnings

Enable GCC 16's coming "-fdiagnostics-show-context=2" option[1] to provide enhanced diagnostic information for value-tracking warnings, which displays the control flow chain leading to the diagnostic. This covers our existing use of -Wrestrict and -Wstringop-overread, and gets us closer to enabling -Warray-bounds, -Wstringop-overflow, and -Wstringop-truncation.

The context depth of 2 provides the immediate decision path that led to the problematic code location, showing conditional checks and branch decisions that caused the warning. This will help us understand why GCC's value-tracking analysis triggered the warning and makes it easier to determine whether warnings are legitimate issues or false positives.

For example, an array bounds warning will now show the conditional statements (like "if (i >= 4)") that established the out-of-bounds access range, directly connecting the control flow to the warning location. This is particularly valuable when GCC's interprocedural analysis can generate warnings that are difficult to understand without seeing the inferred control flow.

---
Cc: Gustavo A. R. Silva <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: Miguel Ojeda <[email protected]>
Cc: <[email protected]>

---
 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index d14824792227..d97452441cd0 100644
@@ -940,6 +940,9 @@ KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
 # for the randomize_kstack_offset feature. Disable it for all compilers.
 KBUILD_CFLAGS	+= $(call cc-option, -fno-stack-clash-protection)
 
+# Get details on warnings generated due to GCC value tracking.
+KBUILD_CFLAGS	+= $(call cc-option, -fdiagnostics-show-context=2)
+
 # Clear used registers at func exit (to reduce data lifetime and ROP gadgets).
 ifdef CONFIG_ZERO_CALL_USED_REGS
 KBUILD_CFLAGS	+= -fzero-call-used-regs=used-gpr
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Kees Cook

Enable GCC 16's coming "-fdiagnostics-show-context=2" option[1] to provide enhanced diagnostic information for value-tracking warnings, which displays the control flow chain leading to the diagnostic. This covers our existing use of -Wrestrict and -Wstringop-overread, and gets us closer to enabling -Warray-bounds, -Wstringop-overflow, and -Wstringop-truncation.

The context depth of 2 provides the immediate decision path that led to the problematic code location, showing conditional checks and branch decisions that caused the warning. This will help us understand why GCC's value-tracking analysis triggered the warning and makes it easier to determine whether warnings are legitimate issues or false positives.

Would we ever want a depth more than 2? In other words, should this be customizable in case there is a warning that needs more context?

Quoted 6 lines

For example, an array bounds warning will now show the conditional statements (like "if (i >= 4)") that established the out-of-bounds access range, directly connecting the control flow to the warning location. This is particularly valuable when GCC's interprocedural analysis can generate warnings that are difficult to understand without seeing the inferred control flow.

Not that it is that different from what you describe here but having an actual example of the insight that this gives using a problematic case from the past (such as one that resulted in these various warnings getting disabled) might be useful for future travellers.

Quoted 1 line

I have a small preference for using links that the project controls, i.e.

https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=6faa3cfe60ff9769d1bebfffdd2c7325217d7389

but I am guessing that mirror is not going anywhere and we have the hash regardless so consider it a nit.

Quoted 2 lines

---

Should we take this via Kbuild or do you want to take it via the hardening tree?

Quoted 21 lines
---
 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index d14824792227..d97452441cd0 100644
@@ -940,6 +940,9 @@ KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
 # for the randomize_kstack_offset feature. Disable it for all compilers.
 KBUILD_CFLAGS	+= $(call cc-option, -fno-stack-clash-protection)
 
+# Get details on warnings generated due to GCC value tracking.
+KBUILD_CFLAGS	+= $(call cc-option, -fdiagnostics-show-context=2)
+
 # Clear used registers at func exit (to reduce data lifetime and ROP gadgets).
 ifdef CONFIG_ZERO_CALL_USED_REGS
 KBUILD_CFLAGS	+= -fzero-call-used-regs=used-gpr
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 7 lines · Nathan Chancellor
Quoted 6 lines · Kees Cook

Enable GCC 16's coming "-fdiagnostics-show-context=2" option[1] to provide enhanced diagnostic information for value-tracking warnings, which displays the control flow chain leading to the diagnostic. This covers our existing use of -Wrestrict and -Wstringop-overread, and gets us closer to enabling -Warray-bounds, -Wstringop-overflow, and -Wstringop-truncation.

Quoted 8 lines
Quoted 5 lines

The context depth of 2 provides the immediate decision path that led to the problematic code location, showing conditional checks and branch decisions that caused the warning. This will help us understand why GCC's value-tracking analysis triggered the warning and makes it easier to determine whether warnings are legitimate issues or false positives.

Would we ever want a depth more than 2? In other words, should this be customizable in case there is a warning that needs more context?

Honestly, I'm not sure yet. I think if we find it to be true, we can add it then. So far, everything I've found works with =1, but I went with =2 just to be conservative. (And I did build time comparisons -- there is no measurable difference between off, 1, or 2.)

Quoted 11 lines
Quoted 6 lines

For example, an array bounds warning will now show the conditional statements (like "if (i >= 4)") that established the out-of-bounds access range, directly connecting the control flow to the warning location. This is particularly valuable when GCC's interprocedural analysis can generate warnings that are difficult to understand without seeing the inferred control flow.

Not that it is that different from what you describe here but having an actual example of the insight that this gives using a problematic case from the past (such as one that resulted in these various warnings getting disabled) might be useful for future travellers.

Yeah, I can list some of the prior fixes.

Quoted 9 lines
Quoted 1 line

I have a small preference for using links that the project controls, i.e.

https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=6faa3cfe60ff9769d1bebfffdd2c7325217d7389

but I am guessing that mirror is not going anywhere and we have the hash regardless so consider it a nit.

I regularly have gcc.gnu.org time out for me, so I've been using github for trees and references, but I can change this.

Quoted 5 lines
Quoted 3 lines

---

Thanks!

Quoted 2 lines

Should we take this via Kbuild or do you want to take it via the hardening tree?

I figured I'd take it via the hardening tree, but I have no strong rationale for that. ;)

-Kees

Signature · 1 line
Kees Cook
Reviewed-by on v1 1/1
#3 lore raw
In reply to Kees Cook
Quoted 7 lines · Kees Cook

Enable GCC 16's coming "-fdiagnostics-show-context=2" option[1] to provide enhanced diagnostic information for value-tracking warnings, which displays the control flow chain leading to the diagnostic. This covers our

existing use of -Wrestrict and -Wstringop-overread, and gets us closer to enabling -Warray-bounds, -Wstringop-overflow, and -Wstringop-truncation.

I am probably missing some context, but in what sense gets us closer? Do you mean it will make it easier to understand those when we enable them? i.e. we still can't enable them until the minimum is upgraded, right?

Apart from that, it looks like a nice improvement on the output from the PR examples -- I didn't test it, but the patch itself looks fine of course:

Thanks!

Cheers,
Miguel

author
#4 lore raw
Quoted 6 lines · Miguel Ojeda
Quoted 4 lines · Kees Cook

Enable GCC 16's coming "-fdiagnostics-show-context=2" option[1] to provide enhanced diagnostic information for value-tracking warnings, which displays the control flow chain leading to the diagnostic. This covers our

Quoted 7 lines
Quoted 2 lines

existing use of -Wrestrict and -Wstringop-overread, and gets us closer to enabling -Warray-bounds, -Wstringop-overflow, and -Wstringop-truncation.

I am probably missing some context, but in what sense gets us closer? Do you mean it will make it easier to understand those when we enable them? i.e. we still can't enable them until the minimum is upgraded, right?

Right -- we've been tracking down -Warray-bounds warnings for several years now and it has been quite difficult to determine which are "real" and which are false positives. This option makes the "real" cases much more obvious.

I will improve the commit log.

Quoted 5 lines

Apart from that, it looks like a nice improvement on the output from the PR examples -- I didn't test it, but the patch itself looks fine of course:

Thanks!

Signature · 1 line
Kees Cook