aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMasahiro Yamada <yamada.masahiro@socionext.com>2018-05-28 05:22:04 -0400
committerMasahiro Yamada <yamada.masahiro@socionext.com>2018-06-10 20:14:08 -0400
commit5aadfdeb8de001ca04d500586e3b033404c28617 (patch)
treea430e73a414b972a4a4cb71ba98fad5af960254d
parent6a61b70b43c9c4cbc7314bf6c8b5ba8b0d6e1e7b (diff)
kcov: test compiler capability in Kconfig and correct dependency
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>
-rw-r--r--Makefile2
-rw-r--r--lib/Kconfig.debug11
-rw-r--r--scripts/Makefile.gcc-plugins8
-rw-r--r--scripts/Makefile.kcov10
-rw-r--r--scripts/gcc-plugins/Makefile4
5 files changed, 16 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index ca9d98b4a71b..73f0bb2c7a98 100644
--- a/Makefile
+++ b/Makefile
@@ -601,7 +601,7 @@ all: vmlinux
601CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ 601CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \
602 $(call cc-option,-fno-tree-loop-im) \ 602 $(call cc-option,-fno-tree-loop-im) \
603 $(call cc-disable-warning,maybe-uninitialized,) 603 $(call cc-disable-warning,maybe-uninitialized,)
604export CFLAGS_GCOV CFLAGS_KCOV 604export CFLAGS_GCOV
605 605
606# The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default 606# The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
607# values of the respective KBUILD_* variables 607# values of the respective KBUILD_* variables
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index eb885942eb0f..d543c65ce0eb 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -736,12 +736,15 @@ config ARCH_HAS_KCOV
736 only for x86_64. KCOV requires testing on other archs, and most likely 736 only for x86_64. KCOV requires testing on other archs, and most likely
737 disabling of instrumentation for some early boot code. 737 disabling of instrumentation for some early boot code.
738 738
739config CC_HAS_SANCOV_TRACE_PC
740 def_bool $(cc-option,-fsanitize-coverage=trace-pc)
741
739config KCOV 742config KCOV
740 bool "Code coverage for fuzzing" 743 bool "Code coverage for fuzzing"
741 depends on ARCH_HAS_KCOV 744 depends on ARCH_HAS_KCOV
745 depends on CC_HAS_SANCOV_TRACE_PC || GCC_PLUGINS
742 select DEBUG_FS 746 select DEBUG_FS
743 select GCC_PLUGINS if !COMPILE_TEST 747 select GCC_PLUGIN_SANCOV if !CC_HAS_SANCOV_TRACE_PC
744 select GCC_PLUGIN_SANCOV if !COMPILE_TEST
745 help 748 help
746 KCOV exposes kernel code coverage information in a form suitable 749 KCOV exposes kernel code coverage information in a form suitable
747 for coverage-guided fuzzing (randomized testing). 750 for coverage-guided fuzzing (randomized testing).
@@ -755,7 +758,7 @@ config KCOV
755config KCOV_ENABLE_COMPARISONS 758config KCOV_ENABLE_COMPARISONS
756 bool "Enable comparison operands collection by KCOV" 759 bool "Enable comparison operands collection by KCOV"
757 depends on KCOV 760 depends on KCOV
758 default n 761 depends on $(cc-option,-fsanitize-coverage=trace-cmp)
759 help 762 help
760 KCOV also exposes operands of every comparison in the instrumented 763 KCOV also exposes operands of every comparison in the instrumented
761 code along with operand sizes and PCs of the comparison instructions. 764 code along with operand sizes and PCs of the comparison instructions.
@@ -765,7 +768,7 @@ config KCOV_ENABLE_COMPARISONS
765config KCOV_INSTRUMENT_ALL 768config KCOV_INSTRUMENT_ALL
766 bool "Instrument all code by default" 769 bool "Instrument all code by default"
767 depends on KCOV 770 depends on KCOV
768 default y if KCOV 771 default y
769 help 772 help
770 If you are doing generic system call fuzzing (like e.g. syzkaller), 773 If you are doing generic system call fuzzing (like e.g. syzkaller),
771 then you will want to instrument the whole kernel and you should 774 then you will want to instrument the whole kernel and you should
diff --git a/scripts/Makefile.gcc-plugins b/scripts/Makefile.gcc-plugins
index 7f5c86246138..708c8f6a5717 100644
--- a/scripts/Makefile.gcc-plugins
+++ b/scripts/Makefile.gcc-plugins
@@ -14,16 +14,12 @@ ifdef CONFIG_GCC_PLUGINS
14 endif 14 endif
15 15
16 ifdef CONFIG_GCC_PLUGIN_SANCOV 16 ifdef CONFIG_GCC_PLUGIN_SANCOV
17 ifeq ($(strip $(CFLAGS_KCOV)),)
18 # It is needed because of the gcc-plugin.sh and gcc version checks. 17 # It is needed because of the gcc-plugin.sh and gcc version checks.
19 gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so 18 gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so
20 19
21 ifneq ($(PLUGINCC),) 20 ifeq ($(PLUGINCC),)
22 CFLAGS_KCOV := $(SANCOV_PLUGIN)
23 else
24 $(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler) 21 $(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler)
25 endif 22 endif
26 endif
27 endif 23 endif
28 24
29 gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += structleak_plugin.so 25 gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += structleak_plugin.so
@@ -38,7 +34,7 @@ ifdef CONFIG_GCC_PLUGINS
38 GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) 34 GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
39 35
40 export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR 36 export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
41 export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN 37 export DISABLE_LATENT_ENTROPY_PLUGIN
42 38
43 ifneq ($(PLUGINCC),) 39 ifneq ($(PLUGINCC),)
44 # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication. 40 # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.
diff --git a/scripts/Makefile.kcov b/scripts/Makefile.kcov
index 5cc72037e423..3d61c4bfcbee 100644
--- a/scripts/Makefile.kcov
+++ b/scripts/Makefile.kcov
@@ -1,7 +1,9 @@
1ifdef CONFIG_KCOV 1ifdef CONFIG_KCOV
2CFLAGS_KCOV := $(call cc-option,-fsanitize-coverage=trace-pc,) 2
3ifeq ($(CONFIG_KCOV_ENABLE_COMPARISONS),y) 3kcov-flags-$(CONFIG_CC_HAS_SANCOV_TRACE_PC) += -fsanitize-coverage=trace-pc
4CFLAGS_KCOV += $(call cc-option,-fsanitize-coverage=trace-cmp,) 4kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -fsanitize-coverage=trace-cmp
5endif 5kcov-flags-$(CONFIG_GCC_PLUGIN_SANCOV) += -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so
6
7export CFLAGS_KCOV := $(kcov-flags-y)
6 8
7endif 9endif
diff --git a/scripts/gcc-plugins/Makefile b/scripts/gcc-plugins/Makefile
index e2ff425f4c7e..ea465799ced5 100644
--- a/scripts/gcc-plugins/Makefile
+++ b/scripts/gcc-plugins/Makefile
@@ -13,10 +13,6 @@ else
13 export HOST_EXTRACXXFLAGS 13 export HOST_EXTRACXXFLAGS
14endif 14endif
15 15
16ifneq ($(CFLAGS_KCOV), $(SANCOV_PLUGIN))
17 GCC_PLUGIN := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGIN))
18endif
19
20export HOSTLIBS 16export HOSTLIBS
21 17
22$(obj)/randomize_layout_plugin.o: $(objtree)/$(obj)/randomize_layout_seed.h 18$(obj)/randomize_layout_plugin.o: $(objtree)/$(obj)/randomize_layout_seed.h