Skip to contentSkip to conversation

Detect changed compiler dependencies for full rebuild

PATCH In review Kees Cook 3 participants 12 messages May 3, 2025 – May 8, 2025

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author maintainer
#0 lore raw

v3 cover Detect changed compiler dependencies for full rebuild

v3: move to include/generated, add touch helper
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Hi,

This is my attempt to introduce dependencies that track the various compiler behaviors that may globally change the build that aren't represented by either compiler flags nor the compiler version (CC_VERSION_TEXT). Namely, this is to detect when the contents of a file the compiler uses changes. We have 3 such situations currently in the tree:

- If any of the GCC plugins change, we need to rebuild everything that was built with them, as they may have changed their behavior and those behaviors may need to be synchronized across all translation units. (The most obvious of these is the randstruct GCC plugin, but is true for most of them.)

- If the randstruct seed itself changes (whether for GCC plugins or Clang), the entire tree needs to be rebuilt since the randomization of structures may change between compilation units if not.

- If the integer-wrap-ignore.scl file for Clang's integer wrapping sanitizer changes, a full rebuild is needed as the coverage for wrapping types may have changed, once again cause behavior differences between compilation units.

The current solution is to:
- Touch a .h file in include/generated that is updated when the specific dependencies change.
e.g.: randstruct_hash.h depends on randstruct.seed

- Add a conditional -D argument for each separate case e.g.: RANDSTRUCT_CFLAGS += -DRANDSTRUCT

- Include the .h file from compiler-version.h through an #ifdef for the define e.g.:
#ifdef RANDSTUCT
#include <generated/randstruct_hash.h>
#endif

This means that all targets gain the dependency (via fixdep), but only when the defines are active, which means they are trivially controlled by the existing CFLAGS removal mechanisms that are already being used to turn off each of the above features.

-Kees

Kees Cook (3):
gcc-plugins: Force full rebuild when plugins change
randstruct: Force full rebuild when seed changes
integer-wrap: Force full rebuild when .scl file changes

 include/linux/compiler-version.h | 10 ++++++++++
 include/linux/vermagic.h         |  1 -
 scripts/Makefile.gcc-plugins     |  2 +-
 scripts/Makefile.lib             | 18 ++++++++++++++++++
 scripts/Makefile.ubsan           |  1 +
 scripts/basic/Makefile           |  5 +++++
 scripts/gcc-plugins/Makefile     |  4 ++++
 7 files changed, 39 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/3 gcc-plugins: Force full rebuild when plugins change

There was no dependency between the plugins changing and the rest of the kernel being built. This could cause strange behaviors as instrumentation could vary between targets depending on when they were built.

Generate a new header file, gcc-plugins.h, any time the GCC plugins change. Include the header file in compiler-version.h when its associated feature name, GCC_PLUGINS, is defined. This will be picked up by fixdep and force rebuilds where needed.

Add a generic "touch" kbuild command, which will be used again in a following patch. Add a "normalize_path" string helper to make the "TOUCH" output less ugly.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h |  4 ++++
 scripts/Makefile.gcc-plugins     |  2 +-
 scripts/Makefile.lib             | 18 ++++++++++++++++++
 scripts/gcc-plugins/Makefile     |  4 ++++
 4 files changed, 27 insertions(+), 1 deletion(-)
include/linux/compiler-version.h +4 -0
index 573fa85b6c0c..74ea11563ce3 100644
@@ -12,3 +12,7 @@
  * and add dependency on include/config/CC_VERSION_TEXT, which is touched
  * by Kconfig when the version string from the compiler changes.
  */
+
+#ifdef GCC_PLUGINS
+#include <generated/gcc-plugins.h>
+#endif
scripts/Makefile.gcc-plugins +1 -1
index 5b8a8378ca8a..e50dc931be49 100644
@@ -38,7 +38,7 @@ export DISABLE_STACKLEAK_PLUGIN
 
 # All the plugin CFLAGS are collected here in case a build target needs to
 # filter them out of the KBUILD_CFLAGS.
-GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) -DGCC_PLUGINS
 export GCC_PLUGINS_CFLAGS
 
 # Add the flags to the build!
scripts/Makefile.lib +18 -0
index 2fe73cda0bdd..6fc2a82ee3bb 100644
@@ -296,6 +296,19 @@ $(foreach m, $1, \
 	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
 endef
 
+# Remove ".." and "." from a path, without using "realpath"
+# Usage:
+#   $(call normalize_path,path/to/../file)
+define normalize_path
+$(strip $(eval elements :=) \
+$(foreach elem,$(subst /, ,$1), \
+	$(if $(filter-out .,$(elem)), \
+	     $(if $(filter ..,$(elem)), \
+		  $(eval elements := $(wordlist 2,$(words $(elements)),x $(elements))), \
+		  $(eval elements := $(elements) $(elem))))) \
+$(subst $(space),/,$(elements)))
+endef
+
 # Build commands
 # ===========================================================================
 # These are shared by some Makefile.* files.
@@ -343,6 +356,11 @@ quiet_cmd_copy = COPY    $@
 $(obj)/%: $(src)/%_shipped
 	$(call cmd,copy)
 
+# Touch a file
+# ===========================================================================
+quiet_cmd_touch = TOUCH   $(call normalize_path,$@)
+      cmd_touch = touch $@
+
 # Commands useful for building a boot image
 # ===========================================================================
 #
scripts/gcc-plugins/Makefile +4 -0
index 320afd3cf8e8..05b14aba41ef 100644
@@ -66,3 +66,7 @@ quiet_cmd_plugin_cxx_o_c = HOSTCXX $@
 
 $(plugin-objs): $(obj)/%.o: $(src)/%.c FORCE
 	$(call if_changed_dep,plugin_cxx_o_c)
+
+$(obj)/../../include/generated/gcc-plugins.h: $(plugin-single) $(plugin-multi) FORCE
+	$(call if_changed,touch)
+always-y += ../../include/generated/gcc-plugins.h
Signature · 1 line
2.34.1
Tested-by Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] gcc-plugins: Force full rebuild when plugins change

Quoted 32 lines · Kees Cook

There was no dependency between the plugins changing and the rest of the kernel being built. This could cause strange behaviors as instrumentation could vary between targets depending on when they were built.

Generate a new header file, gcc-plugins.h, any time the GCC plugins change. Include the header file in compiler-version.h when its associated feature name, GCC_PLUGINS, is defined. This will be picked up by fixdep and force rebuilds where needed.

Add a generic "touch" kbuild command, which will be used again in a following patch. Add a "normalize_path" string helper to make the "TOUCH" output less ugly.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h |  4 ++++
 scripts/Makefile.gcc-plugins     |  2 +-
 scripts/Makefile.lib             | 18 ++++++++++++++++++
 scripts/gcc-plugins/Makefile     |  4 ++++
 4 files changed, 27 insertions(+), 1 deletion(-)
include/linux/compiler-version.h +0 -0
index 573fa85b6c0c..74ea11563ce3 100644

include/linux/compiler-version.h line 16

Quoted 5 lines
include/linux/compiler-version.h +2 -0
@@ -12,3 +12,7 @@
  * and add dependency on include/config/CC_VERSION_TEXT, which is touched
  * by Kconfig when the version string from the compiler changes.
  */
+
+#ifdef GCC_PLUGINS

Out of curiousity: Why can't we use CONFIG_GCC_PLUGINS here?

Quoted 25 lines
include/linux/compiler-version.h +2 -0
+#include <generated/gcc-plugins.h>
+#endif
scripts/Makefile.gcc-plugins +1 -1
index 5b8a8378ca8a..e50dc931be49 100644
@@ -38,7 +38,7 @@ export DISABLE_STACKLEAK_PLUGIN
 
 # All the plugin CFLAGS are collected here in case a build target needs to
 # filter them out of the KBUILD_CFLAGS.
-GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) -DGCC_PLUGINS
 export GCC_PLUGINS_CFLAGS
 
 # Add the flags to the build!
scripts/Makefile.lib +2 -0
index 2fe73cda0bdd..6fc2a82ee3bb 100644
@@ -296,6 +296,19 @@ $(foreach m, $1, \
 	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
 endef
 
+# Remove ".." and "." from a path, without using "realpath"
+# Usage:

scripts/Makefile.lib $(foreach m, $1, \ · line 310

Quoted 10 lines
scripts/Makefile.lib +10 -0
+#   $(call normalize_path,path/to/../file)
+define normalize_path
+$(strip $(eval elements :=) \
+$(foreach elem,$(subst /, ,$1), \
+	$(if $(filter-out .,$(elem)), \
+	     $(if $(filter ..,$(elem)), \
+		  $(eval elements := $(wordlist 2,$(words $(elements)),x $(elements))), \
+		  $(eval elements := $(elements) $(elem))))) \
+$(subst $(space),/,$(elements)))
+endef

Nice :)

Quoted 31 lines
scripts/Makefile.lib +6 -0
+
 # Build commands
 # ===========================================================================
 # These are shared by some Makefile.* files.
@@ -343,6 +356,11 @@ quiet_cmd_copy = COPY    $@
 $(obj)/%: $(src)/%_shipped
 	$(call cmd,copy)
 
+# Touch a file
+# ===========================================================================
+quiet_cmd_touch = TOUCH   $(call normalize_path,$@)
+      cmd_touch = touch $@
+
 # Commands useful for building a boot image
 # ===========================================================================
 #
scripts/gcc-plugins/Makefile +4 -0
index 320afd3cf8e8..05b14aba41ef 100644
@@ -66,3 +66,7 @@ quiet_cmd_plugin_cxx_o_c = HOSTCXX $@
 
 $(plugin-objs): $(obj)/%.o: $(src)/%.c FORCE
 	$(call if_changed_dep,plugin_cxx_o_c)
+
+$(obj)/../../include/generated/gcc-plugins.h: $(plugin-single) $(plugin-multi) FORCE
+	$(call if_changed,touch)
+always-y += ../../include/generated/gcc-plugins.h
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v3 1/3] gcc-plugins: Force full rebuild when plugins change

Quoted 29 lines · Nicolas Schier
Quoted 27 lines · Kees Cook

There was no dependency between the plugins changing and the rest of the kernel being built. This could cause strange behaviors as instrumentation could vary between targets depending on when they were built.

Generate a new header file, gcc-plugins.h, any time the GCC plugins change. Include the header file in compiler-version.h when its associated feature name, GCC_PLUGINS, is defined. This will be picked up by fixdep and force rebuilds where needed.

Add a generic "touch" kbuild command, which will be used again in a following patch. Add a "normalize_path" string helper to make the "TOUCH" output less ugly.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h |  4 ++++
 scripts/Makefile.gcc-plugins     |  2 +-
 scripts/Makefile.lib             | 18 ++++++++++++++++++
 scripts/gcc-plugins/Makefile     |  4 ++++
 4 files changed, 27 insertions(+), 1 deletion(-)
Quoted 12 lines

include/linux/compiler-version.h line 16

Quoted 10 lines
include/linux/compiler-version.h +2 -0
index 573fa85b6c0c..74ea11563ce3 100644
@@ -12,3 +12,7 @@
  * and add dependency on include/config/CC_VERSION_TEXT, which is touched
  * by Kconfig when the version string from the compiler changes.
  */
+
+#ifdef GCC_PLUGINS

Out of curiousity: Why can't we use CONFIG_GCC_PLUGINS here?

... because compiler-version.h is included before kconfig.h (which includes autoconf.h). Sorry for the noise.

author maintainer
#4 lore raw

PATCH v3 2/3 randstruct: Force full rebuild when seed changes

In reply to Kees Cook

While the randstruct GCC plugin was being rebuilt if the randstruct seed changed, Clang builds did not notice the change. This could result in differing struct layouts in a target depending on when it was built.

Include the existing generated header file in compiler-version.h when its associated feature name, RANDSTRUCT, is defined. This will be picked up by fixdep and force rebuilds where needed.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Petr Pavlu <[email protected]>
Cc: Sebastian Andrzej Siewior <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h | 3 +++
 include/linux/vermagic.h         | 1 -
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/compiler-version.h +3 -0
index 74ea11563ce3..69b29b400ce2 100644
@@ -16,3 +16,6 @@
 #ifdef GCC_PLUGINS
 #include <generated/gcc-plugins.h>
 #endif
+#ifdef RANDSTRUCT
+#include <generated/randstruct_hash.h>
+#endif
include/linux/vermagic.h +0 -1
index 939ceabcaf06..335c360d4f9b 100644
@@ -33,7 +33,6 @@
 #define MODULE_VERMAGIC_MODVERSIONS ""
 #endif
 #ifdef RANDSTRUCT
-#include <generated/randstruct_hash.h>
 #define MODULE_RANDSTRUCT "RANDSTRUCT_" RANDSTRUCT_HASHED_SEED
 #else
 #define MODULE_RANDSTRUCT
Signature · 1 line
2.34.1
Reviewed-by Tested-by on v3 2/3
#5 lore raw

Re: [PATCH v3 2/3] randstruct: Force full rebuild when seed changes

Quoted 47 lines · Kees Cook

While the randstruct GCC plugin was being rebuilt if the randstruct seed changed, Clang builds did not notice the change. This could result in differing struct layouts in a target depending on when it was built.

Include the existing generated header file in compiler-version.h when its associated feature name, RANDSTRUCT, is defined. This will be picked up by fixdep and force rebuilds where needed.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Petr Pavlu <[email protected]>
Cc: Sebastian Andrzej Siewior <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h | 3 +++
 include/linux/vermagic.h         | 1 -
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/compiler-version.h +3 -0
index 74ea11563ce3..69b29b400ce2 100644
@@ -16,3 +16,6 @@
 #ifdef GCC_PLUGINS
 #include <generated/gcc-plugins.h>
 #endif
+#ifdef RANDSTRUCT
+#include <generated/randstruct_hash.h>
+#endif
include/linux/vermagic.h +0 -1
index 939ceabcaf06..335c360d4f9b 100644
@@ -33,7 +33,6 @@
 #define MODULE_VERMAGIC_MODVERSIONS ""
 #endif
 #ifdef RANDSTRUCT
-#include <generated/randstruct_hash.h>
 #define MODULE_RANDSTRUCT "RANDSTRUCT_" RANDSTRUCT_HASHED_SEED
 #else
 #define MODULE_RANDSTRUCT
Signature · 1 line
2.34.1
author maintainer
#6 lore raw

PATCH v3 3/3 integer-wrap: Force full rebuild when .scl file changes

In reply to Kees Cook

Since the integer wrapping sanitizer's behavior depends on its associated .scl file, we must force a full rebuild if the file changes. If not, instrumentation may differ between targets based on when they were built.

Generate a new header file, integer-wrap.h, any time the Clang .scl file changes. Include the header file in compiler-version.h when its associated feature name, INTEGER_WRAP, is defined. This will be picked up by fixdep and force rebuilds where needed.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Justin Stitt <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h | 3 +++
 scripts/Makefile.ubsan           | 1 +
 scripts/basic/Makefile           | 5 +++++
 3 files changed, 9 insertions(+)
include/linux/compiler-version.h +3 -0
index 69b29b400ce2..187e749f9e79 100644
@@ -19,3 +19,6 @@
 #ifdef RANDSTRUCT
 #include <generated/randstruct_hash.h>
 #endif
+#ifdef INTEGER_WRAP
+#include <generated/integer-wrap.h>
+#endif
scripts/Makefile.ubsan +1 -0
index 9e35198edbf0..653f7117819c 100644
@@ -15,6 +15,7 @@ ubsan-cflags-$(CONFIG_UBSAN_TRAP)		+= $(call cc-option,-fsanitize-trap=undefined
 export CFLAGS_UBSAN := $(ubsan-cflags-y)
 
 ubsan-integer-wrap-cflags-$(CONFIG_UBSAN_INTEGER_WRAP)     +=	\
+	-DINTEGER_WRAP						\
 	-fsanitize-undefined-ignore-overflow-pattern=all	\
 	-fsanitize=signed-integer-overflow			\
 	-fsanitize=unsigned-integer-overflow			\
scripts/basic/Makefile +5 -0
index dd289a6725ac..fb8e2c38fbc7 100644
@@ -14,3 +14,8 @@ cmd_create_randstruct_seed = \
 $(obj)/randstruct.seed: $(gen-randstruct-seed) FORCE
 	$(call if_changed,create_randstruct_seed)
 always-$(CONFIG_RANDSTRUCT) += randstruct.seed
+
+# integer-wrap: if the .scl file changes, we need to do a full rebuild.
+$(obj)/../../include/generated/integer-wrap.h: $(srctree)/scripts/integer-wrap-ignore.scl FORCE
+	$(call if_changed,touch)
+always-$(CONFIG_UBSAN_INTEGER_WRAP) += ../../include/generated/integer-wrap.h
Signature · 1 line
2.34.1
Acked-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] integer-wrap: Force full rebuild when .scl file changes

Quoted 56 lines · Kees Cook

Since the integer wrapping sanitizer's behavior depends on its associated .scl file, we must force a full rebuild if the file changes. If not, instrumentation may differ between targets based on when they were built.

Generate a new header file, integer-wrap.h, any time the Clang .scl file changes. Include the header file in compiler-version.h when its associated feature name, INTEGER_WRAP, is defined. This will be picked up by fixdep and force rebuilds where needed.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Justin Stitt <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h | 3 +++
 scripts/Makefile.ubsan           | 1 +
 scripts/basic/Makefile           | 5 +++++
 3 files changed, 9 insertions(+)
include/linux/compiler-version.h +3 -0
index 69b29b400ce2..187e749f9e79 100644
@@ -19,3 +19,6 @@
 #ifdef RANDSTRUCT
 #include <generated/randstruct_hash.h>
 #endif
+#ifdef INTEGER_WRAP
+#include <generated/integer-wrap.h>
+#endif
scripts/Makefile.ubsan +1 -0
index 9e35198edbf0..653f7117819c 100644
@@ -15,6 +15,7 @@ ubsan-cflags-$(CONFIG_UBSAN_TRAP)             += $(call cc-option,-fsanitize-trap=undefined
 export CFLAGS_UBSAN := $(ubsan-cflags-y)
 
 ubsan-integer-wrap-cflags-$(CONFIG_UBSAN_INTEGER_WRAP)     +=  \
+       -DINTEGER_WRAP                                          \
        -fsanitize-undefined-ignore-overflow-pattern=all        \
        -fsanitize=signed-integer-overflow                      \
        -fsanitize=unsigned-integer-overflow                    \
scripts/basic/Makefile +0 -0
index dd289a6725ac..fb8e2c38fbc7 100644

scripts/basic/Makefile cmd_create_randstruct_seed = \ · line 21

Quoted 8 lines
scripts/basic/Makefile +5 -0
@@ -14,3 +14,8 @@ cmd_create_randstruct_seed = \
 $(obj)/randstruct.seed: $(gen-randstruct-seed) FORCE
        $(call if_changed,create_randstruct_seed)
 always-$(CONFIG_RANDSTRUCT) += randstruct.seed
+
+# integer-wrap: if the .scl file changes, we need to do a full rebuild.
+$(obj)/../../include/generated/integer-wrap.h: $(srctree)/scripts/integer-wrap-ignore.scl FORCE
+       $(call if_changed,touch)
+always-$(CONFIG_UBSAN_INTEGER_WRAP) += ../../include/generated/integer-wrap.h

I'm not sure how this fake header stuff works to ensure builds deps are tracked properly but we do need scl files to be considered as part of complete builds, so:

Quoted 3 lines

--
2.34.1

#8 lore raw

Re: [PATCH v3 3/3] integer-wrap: Force full rebuild when .scl file changes

Quoted 62 lines · Justin Stitt
Quoted 60 lines · Kees Cook

Since the integer wrapping sanitizer's behavior depends on its associated .scl file, we must force a full rebuild if the file changes. If not, instrumentation may differ between targets based on when they were built.

Generate a new header file, integer-wrap.h, any time the Clang .scl file changes. Include the header file in compiler-version.h when its associated feature name, INTEGER_WRAP, is defined. This will be picked up by fixdep and force rebuilds where needed.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Justin Stitt <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h | 3 +++
 scripts/Makefile.ubsan           | 1 +
 scripts/basic/Makefile           | 5 +++++
 3 files changed, 9 insertions(+)
include/linux/compiler-version.h +3 -0
index 69b29b400ce2..187e749f9e79 100644
@@ -19,3 +19,6 @@
 #ifdef RANDSTRUCT
 #include <generated/randstruct_hash.h>
 #endif
+#ifdef INTEGER_WRAP
+#include <generated/integer-wrap.h>
+#endif
scripts/Makefile.ubsan +1 -0
index 9e35198edbf0..653f7117819c 100644
@@ -15,6 +15,7 @@ ubsan-cflags-$(CONFIG_UBSAN_TRAP)             += $(call cc-option,-fsanitize-trap=undefined
 export CFLAGS_UBSAN := $(ubsan-cflags-y)
 
 ubsan-integer-wrap-cflags-$(CONFIG_UBSAN_INTEGER_WRAP)     +=  \
+       -DINTEGER_WRAP                                          \
        -fsanitize-undefined-ignore-overflow-pattern=all        \
        -fsanitize=signed-integer-overflow                      \
        -fsanitize=unsigned-integer-overflow                    \
scripts/basic/Makefile +1 -0
index dd289a6725ac..fb8e2c38fbc7 100644
@@ -14,3 +14,8 @@ cmd_create_randstruct_seed = \
 $(obj)/randstruct.seed: $(gen-randstruct-seed) FORCE
        $(call if_changed,create_randstruct_seed)
 always-$(CONFIG_RANDSTRUCT) += randstruct.seed
+
Quoted 8 lines

scripts/basic/Makefile cmd_create_randstruct_seed = \ · line 21

Quoted 4 lines
scripts/basic/Makefile +4 -0
+# integer-wrap: if the .scl file changes, we need to do a full rebuild.
+$(obj)/../../include/generated/integer-wrap.h: $(srctree)/scripts/integer-wrap-ignore.scl FORCE
+       $(call if_changed,touch)
+always-$(CONFIG_UBSAN_INTEGER_WRAP) += ../../include/generated/integer-wrap.h

I'm not sure how this fake header stuff works to ensure builds deps are tracked properly but we do need scl files to be considered as part of complete builds, so:

As in, I'm sure it works but have personally never written or reviewed a Makefile+generated header snippet like that before :)

Quoted 6 lines
Quoted 3 lines

--
2.34.1

Reviewed-by on v3 3/3
#9 lore raw

Re: [PATCH v3 3/3] integer-wrap: Force full rebuild when .scl file changes

In reply to Kees Cook
Quoted 66 lines · Kees Cook

Since the integer wrapping sanitizer's behavior depends on its associated .scl file, we must force a full rebuild if the file changes. If not, instrumentation may differ between targets based on when they were built.

Generate a new header file, integer-wrap.h, any time the Clang .scl file changes. Include the header file in compiler-version.h when its associated feature name, INTEGER_WRAP, is defined. This will be picked up by fixdep and force rebuilds where needed.

---
Cc: Masahiro Yamada <[email protected]>
Cc: Justin Stitt <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 include/linux/compiler-version.h | 3 +++
 scripts/Makefile.ubsan           | 1 +
 scripts/basic/Makefile           | 5 +++++
 3 files changed, 9 insertions(+)
include/linux/compiler-version.h +3 -0
index 69b29b400ce2..187e749f9e79 100644
@@ -19,3 +19,6 @@
 #ifdef RANDSTRUCT
 #include <generated/randstruct_hash.h>
 #endif
+#ifdef INTEGER_WRAP
+#include <generated/integer-wrap.h>
+#endif
scripts/Makefile.ubsan +1 -0
index 9e35198edbf0..653f7117819c 100644
@@ -15,6 +15,7 @@ ubsan-cflags-$(CONFIG_UBSAN_TRAP)		+= $(call cc-option,-fsanitize-trap=undefined
 export CFLAGS_UBSAN := $(ubsan-cflags-y)
 
 ubsan-integer-wrap-cflags-$(CONFIG_UBSAN_INTEGER_WRAP)     +=	\
+	-DINTEGER_WRAP						\
 	-fsanitize-undefined-ignore-overflow-pattern=all	\
 	-fsanitize=signed-integer-overflow			\
 	-fsanitize=unsigned-integer-overflow			\
scripts/basic/Makefile +5 -0
index dd289a6725ac..fb8e2c38fbc7 100644
@@ -14,3 +14,8 @@ cmd_create_randstruct_seed = \
 $(obj)/randstruct.seed: $(gen-randstruct-seed) FORCE
 	$(call if_changed,create_randstruct_seed)
 always-$(CONFIG_RANDSTRUCT) += randstruct.seed
+
+# integer-wrap: if the .scl file changes, we need to do a full rebuild.
+$(obj)/../../include/generated/integer-wrap.h: $(srctree)/scripts/integer-wrap-ignore.scl FORCE
+	$(call if_changed,touch)
+always-$(CONFIG_UBSAN_INTEGER_WRAP) += ../../include/generated/integer-wrap.h
Signature · 1 line
2.34.1
#10 lore raw
In reply to Kees Cook
Quoted 19 lines · Kees Cook

v3: move to include/generated, add touch helper
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Hi,

This is my attempt to introduce dependencies that track the various compiler behaviors that may globally change the build that aren't represented by either compiler flags nor the compiler version (CC_VERSION_TEXT). Namely, this is to detect when the contents of a file the compiler uses changes. We have 3 such situations currently in the tree:

- If any of the GCC plugins change, we need to rebuild everything that was built with them, as they may have changed their behavior and those behaviors may need to be synchronized across all translation units. (The most obvious of these is the randstruct GCC plugin, but is true for most of them.)

Quoted 8 lines

- If the randstruct seed itself changes (whether for GCC plugins or Clang), the entire tree needs to be rebuilt since the randomization of structures may change between compilation units if not.

- If the integer-wrap-ignore.scl file for Clang's integer wrapping sanitizer changes, a full rebuild is needed as the coverage for wrapping types may have changed, once again cause behavior differences between compilation units.

I am unsure if it is too much detail, but I'd like to see some of these infos in include/linux/compiler-version.h, too.

Kind regards,
Nicolas

author maintainer
#11 lore raw
Quoted 25 lines · Nicolas Schier
Quoted 23 lines · Kees Cook

v3: move to include/generated, add touch helper
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Hi,

This is my attempt to introduce dependencies that track the various compiler behaviors that may globally change the build that aren't represented by either compiler flags nor the compiler version (CC_VERSION_TEXT). Namely, this is to detect when the contents of a file the compiler uses changes. We have 3 such situations currently in the tree:

- If any of the GCC plugins change, we need to rebuild everything that was built with them, as they may have changed their behavior and those behaviors may need to be synchronized across all translation units. (The most obvious of these is the randstruct GCC plugin, but is true for most of them.)

- If the randstruct seed itself changes (whether for GCC plugins or Clang), the entire tree needs to be rebuilt since the randomization of structures may change between compilation units if not.

Quoted 7 lines
Quoted 4 lines

- If the integer-wrap-ignore.scl file for Clang's integer wrapping sanitizer changes, a full rebuild is needed as the coverage for wrapping types may have changed, once again cause behavior differences between compilation units.

I am unsure if it is too much detail, but I'd like to see some of these infos in include/linux/compiler-version.h, too.

Yeah, that's a good idea. No reason to make people dig for the commit logs, etc -- it should be immediately discoverable. I've updated the patches to include the (slight rephrased) text above.

Thanks!

Signature · 1 line
Kees Cook