Skip to content

Commit 3bf6717

Browse files
Darksonnakpm00
authored andcommitted
kcov: rust: add flags for KCOV with Rust
Rust code is currently not instrumented properly when KCOV is enabled. Thus, add the relevant flags to perform instrumentation correctly. This is necessary for efficient fuzzing of Rust code. The sanitizer-coverage features of LLVM have existed for long enough that they are available on any LLVM version supported by rustc, so we do not need any Kconfig feature detection. The coverage level is set to 3, as that is the level needed by trace-pc. We do not instrument `core` since when we fuzz the kernel, we are looking for bugs in the kernel, not the Rust stdlib. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Alice Ryhl <[email protected]> Co-developed-by: Matthew Maurer <[email protected]> Signed-off-by: Matthew Maurer <[email protected]> Reviewed-by: Alexander Potapenko <[email protected]> Tested-by: Aleksandr Nogikh <[email protected]> Acked-by: Miguel Ojeda <[email protected]> Cc: Andreas Hindborg <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Benno Lossin <[email protected]> Cc: Bill Wendling <[email protected]> Cc: Björn Roy Baron <[email protected]> Cc: Boqun Feng <[email protected]> Cc: Danilo Krummrich <[email protected]> Cc: Dmitriy Vyukov <[email protected]> Cc: Gary Guo <[email protected]> Cc: Justin Stitt <[email protected]> Cc: Masahiro Yamada <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Trevor Gross <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5a78977 commit 3bf6717

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
492492
ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
493493
$(obj)/core.o: scripts/target.json
494494
endif
495+
KCOV_INSTRUMENT_core.o := n
495496

496497
$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
497498
$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'

scripts/Makefile.kcov

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ kcov-flags-$(CONFIG_CC_HAS_SANCOV_TRACE_PC) += -fsanitize-coverage=trace-pc
33
kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -fsanitize-coverage=trace-cmp
44
kcov-flags-$(CONFIG_GCC_PLUGIN_SANCOV) += -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so
55

6+
kcov-rflags-y += -Cpasses=sancov-module
7+
kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-level=3
8+
kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-trace-pc
9+
kcov-rflags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -Cllvm-args=-sanitizer-coverage-trace-compares
10+
611
export CFLAGS_KCOV := $(kcov-flags-y)
12+
export RUSTFLAGS_KCOV := $(kcov-rflags-y)

scripts/Makefile.lib

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ ifeq ($(CONFIG_KCOV),y)
169169
_c_flags += $(if $(patsubst n%,, \
170170
$(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \
171171
$(CFLAGS_KCOV))
172+
_rust_flags += $(if $(patsubst n%,, \
173+
$(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \
174+
$(RUSTFLAGS_KCOV))
172175
endif
173176

174177
#

0 commit comments

Comments
 (0)