Skip to content

Commit 5aadfde

Browse files
committed
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 <[email protected]> Reviewed-by: Kees Cook <[email protected]>
1 parent 6a61b70 commit 5aadfde

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ all: vmlinux
601601
CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \
602602
$(call cc-option,-fno-tree-loop-im) \
603603
$(call cc-disable-warning,maybe-uninitialized,)
604-
export CFLAGS_GCOV CFLAGS_KCOV
604+
export CFLAGS_GCOV
605605

606606
# The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
607607
# values of the respective KBUILD_* variables

lib/Kconfig.debug

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,12 +736,15 @@ config ARCH_HAS_KCOV
736736
only for x86_64. KCOV requires testing on other archs, and most likely
737737
disabling of instrumentation for some early boot code.
738738

739+
config CC_HAS_SANCOV_TRACE_PC
740+
def_bool $(cc-option,-fsanitize-coverage=trace-pc)
741+
739742
config KCOV
740743
bool "Code coverage for fuzzing"
741744
depends on ARCH_HAS_KCOV
745+
depends on CC_HAS_SANCOV_TRACE_PC || GCC_PLUGINS
742746
select DEBUG_FS
743-
select GCC_PLUGINS if !COMPILE_TEST
744-
select GCC_PLUGIN_SANCOV if !COMPILE_TEST
747+
select GCC_PLUGIN_SANCOV if !CC_HAS_SANCOV_TRACE_PC
745748
help
746749
KCOV exposes kernel code coverage information in a form suitable
747750
for coverage-guided fuzzing (randomized testing).
@@ -755,7 +758,7 @@ config KCOV
755758
config KCOV_ENABLE_COMPARISONS
756759
bool "Enable comparison operands collection by KCOV"
757760
depends on KCOV
758-
default n
761+
depends on $(cc-option,-fsanitize-coverage=trace-cmp)
759762
help
760763
KCOV also exposes operands of every comparison in the instrumented
761764
code along with operand sizes and PCs of the comparison instructions.
@@ -765,7 +768,7 @@ config KCOV_ENABLE_COMPARISONS
765768
config KCOV_INSTRUMENT_ALL
766769
bool "Instrument all code by default"
767770
depends on KCOV
768-
default y if KCOV
771+
default y
769772
help
770773
If you are doing generic system call fuzzing (like e.g. syzkaller),
771774
then you will want to instrument the whole kernel and you should

scripts/Makefile.gcc-plugins

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ ifdef CONFIG_GCC_PLUGINS
1414
endif
1515

1616
ifdef CONFIG_GCC_PLUGIN_SANCOV
17-
ifeq ($(strip $(CFLAGS_KCOV)),)
1817
# It is needed because of the gcc-plugin.sh and gcc version checks.
1918
gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so
2019

21-
ifneq ($(PLUGINCC),)
22-
CFLAGS_KCOV := $(SANCOV_PLUGIN)
23-
else
20+
ifeq ($(PLUGINCC),)
2421
$(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler)
2522
endif
26-
endif
2723
endif
2824

2925
gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += structleak_plugin.so
@@ -38,7 +34,7 @@ ifdef CONFIG_GCC_PLUGINS
3834
GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
3935

4036
export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
41-
export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN
37+
export DISABLE_LATENT_ENTROPY_PLUGIN
4238

4339
ifneq ($(PLUGINCC),)
4440
# SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.

scripts/Makefile.kcov

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
ifdef CONFIG_KCOV
2-
CFLAGS_KCOV := $(call cc-option,-fsanitize-coverage=trace-pc,)
3-
ifeq ($(CONFIG_KCOV_ENABLE_COMPARISONS),y)
4-
CFLAGS_KCOV += $(call cc-option,-fsanitize-coverage=trace-cmp,)
5-
endif
2+
3+
kcov-flags-$(CONFIG_CC_HAS_SANCOV_TRACE_PC) += -fsanitize-coverage=trace-pc
4+
kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -fsanitize-coverage=trace-cmp
5+
kcov-flags-$(CONFIG_GCC_PLUGIN_SANCOV) += -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so
6+
7+
export CFLAGS_KCOV := $(kcov-flags-y)
68

79
endif

scripts/gcc-plugins/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ else
1313
export HOST_EXTRACXXFLAGS
1414
endif
1515

16-
ifneq ($(CFLAGS_KCOV), $(SANCOV_PLUGIN))
17-
GCC_PLUGIN := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGIN))
18-
endif
19-
2016
export HOSTLIBS
2117

2218
$(obj)/randomize_layout_plugin.o: $(objtree)/$(obj)/randomize_layout_seed.h

0 commit comments

Comments
 (0)