]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
kcov: test compiler capability in Kconfig and correct dependency
authorMasahiro Yamada <yamada.masahiro@socionext.com>
Mon, 28 May 2018 09:22:04 +0000 (18:22 +0900)
committerMasahiro Yamada <yamada.masahiro@socionext.com>
Mon, 11 Jun 2018 00:14:08 +0000 (09:14 +0900)
As Documentation/kbuild/kconfig-language.txt notes, 'select' should be
be used with care - it forces a lower limit of another symbol, ignoring
the dependency.  Currently, KCOV can select GCC_PLUGINS even if arch
does not select HAVE_GCC_PLUGINS.  This could cause the unmet direct
dependency.

Now that Kconfig can test compiler capability, let's handle this in a
more sophisticated way.

There are two ways to enable KCOV; use the compiler that natively
supports -fsanitize-coverage=trace-pc, or build the SANCOV plugin if
the compiler has ability to build GCC plugins.  Hence, the correct
dependency for KCOV is:

  depends on CC_HAS_SANCOV_TRACE_PC || GCC_PLUGINS

You do not need to build the SANCOV plugin if the compiler already
supports -fsanitize-coverage=trace-pc.  Hence, the select should be:

  select GCC_PLUGIN_SANCOV if !CC_HAS_SANCOV_TRACE_PC

With this, GCC_PLUGIN_SANCOV is selected only when necessary, so
scripts/Makefile.gcc-plugins can be cleaner.

I also cleaned up Kconfig and scripts/Makefile.kcov as well.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Makefile
lib/Kconfig.debug
scripts/Makefile.gcc-plugins
scripts/Makefile.kcov
scripts/gcc-plugins/Makefile

index ca9d98b4a71b4ecab23e03da4a3bac7f28fb070c..73f0bb2c7a984c595dc1ab6ac62b0438daf96367 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -601,7 +601,7 @@ all: vmlinux
 CFLAGS_GCOV    := -fprofile-arcs -ftest-coverage \
        $(call cc-option,-fno-tree-loop-im) \
        $(call cc-disable-warning,maybe-uninitialized,)
-export CFLAGS_GCOV CFLAGS_KCOV
+export CFLAGS_GCOV
 
 # The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
 # values of the respective KBUILD_* variables
index eb885942eb0f1e6f39fdc1fa93596395cdc3313f..d543c65ce0eb2a5181f2796ae119807e0c0f7e2d 100644 (file)
@@ -736,12 +736,15 @@ config ARCH_HAS_KCOV
          only for x86_64. KCOV requires testing on other archs, and most likely
          disabling of instrumentation for some early boot code.
 
+config CC_HAS_SANCOV_TRACE_PC
+       def_bool $(cc-option,-fsanitize-coverage=trace-pc)
+
 config KCOV
        bool "Code coverage for fuzzing"
        depends on ARCH_HAS_KCOV
+       depends on CC_HAS_SANCOV_TRACE_PC || GCC_PLUGINS
        select DEBUG_FS
-       select GCC_PLUGINS if !COMPILE_TEST
-       select GCC_PLUGIN_SANCOV if !COMPILE_TEST
+       select GCC_PLUGIN_SANCOV if !CC_HAS_SANCOV_TRACE_PC
        help
          KCOV exposes kernel code coverage information in a form suitable
          for coverage-guided fuzzing (randomized testing).
@@ -755,7 +758,7 @@ config KCOV
 config KCOV_ENABLE_COMPARISONS
        bool "Enable comparison operands collection by KCOV"
        depends on KCOV
-       default n
+       depends on $(cc-option,-fsanitize-coverage=trace-cmp)
        help
          KCOV also exposes operands of every comparison in the instrumented
          code along with operand sizes and PCs of the comparison instructions.
@@ -765,7 +768,7 @@ config KCOV_ENABLE_COMPARISONS
 config KCOV_INSTRUMENT_ALL
        bool "Instrument all code by default"
        depends on KCOV
-       default y if KCOV
+       default y
        help
          If you are doing generic system call fuzzing (like e.g. syzkaller),
          then you will want to instrument the whole kernel and you should
index 7f5c862461383c0d067f9ab017dd56b8cde390f4..708c8f6a57173ad3cf9f00f5bfd1c92fc177efa3 100644 (file)
@@ -14,16 +14,12 @@ ifdef CONFIG_GCC_PLUGINS
   endif
 
   ifdef CONFIG_GCC_PLUGIN_SANCOV
-    ifeq ($(strip $(CFLAGS_KCOV)),)
       # It is needed because of the gcc-plugin.sh and gcc version checks.
       gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV)           += sancov_plugin.so
 
-      ifneq ($(PLUGINCC),)
-        CFLAGS_KCOV := $(SANCOV_PLUGIN)
-      else
+      ifeq ($(PLUGINCC),)
         $(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler)
       endif
-    endif
   endif
 
   gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK)   += structleak_plugin.so
@@ -38,7 +34,7 @@ ifdef CONFIG_GCC_PLUGINS
   GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
 
   export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
-  export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN
+  export DISABLE_LATENT_ENTROPY_PLUGIN
 
   ifneq ($(PLUGINCC),)
     # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.
index 5cc72037e423459820d060bdeedaca44a54b8975..3d61c4bfcbee48d3129d4eed56fcebd8af267dea 100644 (file)
@@ -1,7 +1,9 @@
 ifdef CONFIG_KCOV
-CFLAGS_KCOV    := $(call cc-option,-fsanitize-coverage=trace-pc,)
-ifeq ($(CONFIG_KCOV_ENABLE_COMPARISONS),y)
-CFLAGS_KCOV += $(call cc-option,-fsanitize-coverage=trace-cmp,)
-endif
+
+kcov-flags-$(CONFIG_CC_HAS_SANCOV_TRACE_PC)    += -fsanitize-coverage=trace-pc
+kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS)   += -fsanitize-coverage=trace-cmp
+kcov-flags-$(CONFIG_GCC_PLUGIN_SANCOV)         += -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so
+
+export CFLAGS_KCOV := $(kcov-flags-y)
 
 endif
index e2ff425f4c7ea6958beddb3571ed9ac72a3b9ea8..ea465799ced590a3f9653500642b71d81a6d353e 100644 (file)
@@ -13,10 +13,6 @@ else
   export HOST_EXTRACXXFLAGS
 endif
 
-ifneq ($(CFLAGS_KCOV), $(SANCOV_PLUGIN))
-  GCC_PLUGIN := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGIN))
-endif
-
 export HOSTLIBS
 
 $(obj)/randomize_layout_plugin.o: $(objtree)/$(obj)/randomize_layout_seed.h