Skip to content

Commit

Permalink
BACKPORT: kcov: test compiler capability in Kconfig and correct depen…
Browse files Browse the repository at this point in the history
…dency

Work around missing cc-option support in Kconfig by checking required
compiler flags in Makefile.

(Upstream commit 5aadfde.)

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>
Change-Id: Iad9110eb7b6ecef6dfcec38cf483699c1b85af01
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Bug: 147413187
  • Loading branch information
masahir0y authored and adelva1984 committed Jan 13, 2020
1 parent 0f0fe8e commit c093b58
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
14 changes: 10 additions & 4 deletions lib/Kconfig.debug
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,17 @@ 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.

# Upstream uses $(cc-option, -fsanitize-coverage=trace-pc), which requires
# cc-option support. Here we instead check CC in scripts/Makefile.kcov.
config CC_HAS_SANCOV_TRACE_PC
def_bool ARCH_HAS_KCOV

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).
Expand All @@ -756,10 +761,11 @@ config KCOV

For more details, see Documentation/dev-tools/kcov.rst.

# Upstream uses $(cc-option, -fsanitize-coverage=trace-cmp), which requires
# cc-option support. Here we instead check CC in scripts/Makefile.kcov.
config KCOV_ENABLE_COMPARISONS
bool "Enable comparison operands collection by KCOV"
depends on KCOV
default n
help
KCOV also exposes operands of every comparison in the instrumented
code along with operand sizes and PCs of the comparison instructions.
Expand All @@ -769,7 +775,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
Expand Down
8 changes: 2 additions & 6 deletions scripts/Makefile.gcc-plugins
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
24 changes: 21 additions & 3 deletions scripts/Makefile.kcov
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
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,)

ifeq ($(call cc-option, -fsanitize-coverage=trace-pc -Werror),)
ifneq ($(CONFIG_COMPILE_TEST),y)
$(error Cannot use CONFIG_KCOV: \
-fsanitize-coverage=trace-pc is not supported by compiler)
endif
endif

ifdef CONFIG_KCOV_ENABLE_COMPARISONS
ifeq ($(call cc-option, -fsanitize-coverage=trace-cmp -Werror),)
ifneq ($(CONFIG_COMPILE_TEST),y)
$(error Cannot use CONFIG_KCOV_ENABLE_COMPARISONS: \
-fsanitize-coverage=trace-cmp is not supported by compiler)
endif
endif
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
4 changes: 0 additions & 4 deletions scripts/gcc-plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c093b58

Please sign in to comment.