kcov: rust: add flags for KCOV with Rust
Subsystems (4)
Alice Ryhl
reviewerPATCH v2 1/1 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.
---
I did not pick up the Tested-by due to the changes. I verified that it looks right under objdump, but I don't have a syzkaller setup I can try it with.
---
Changes in v2:
- Ignore core in KCOV.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- rust/Makefile | 1 + scripts/Makefile.kcov | 6 ++++++ scripts/Makefile.lib | 3 +++ 3 files changed, 10 insertions(+)
rust/Makefile +1 -0
@@ -492,6 +492,7 @@ $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) $(obj)/core.o: scripts/target.json endif +KCOV_INSTRUMENT_core.o := n $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
scripts/Makefile.kcov +6 -0
@@ -3,4 +3,10 @@ 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 +kcov-rflags-y += -Cpasses=sancov-module +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-level=3 +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-trace-pc +kcov-rflags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -Cllvm-args=-sanitizer-coverage-trace-compares + export CFLAGS_KCOV := $(kcov-flags-y) +export RUSTFLAGS_KCOV := $(kcov-rflags-y)
scripts/Makefile.lib +3 -0
@@ -169,6 +169,9 @@ ifeq ($(CONFIG_KCOV),y) _c_flags += $(if $(patsubst n%,, \ $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ $(CFLAGS_KCOV)) +_rust_flags += $(if $(patsubst n%,, \ + $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ + $(RUSTFLAGS_KCOV)) endif #
---
base-commit: 9c32cda43eb78f78c73aee4aa344b777714e259b
change-id: 20250430-rust-kcov-6c74fd0f1f06
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Quoted 13 lines · Alice Ryhl
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
Kconfigfeature detection. The coverage level is set to 3, as that is the level needed by trace-pc.We do not instrument
coresince when we fuzz the kernel, we are looking for bugs in the kernel, not the Rust stdlib.
Quoted 8 lines
Co-developed-by Matthew Maurer <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Reviewed-by Alexander Potapenko <[email protected]>Signed-off-by Alice Ryhl <[email protected]>---
I did not pick up the Tested-by due to the changes. I verified that it looks right under objdump, but I don't have a syzkaller setup I can try it with.
Thanks for incorporating the core.o change!
I've tested the v2 patch on my local setup and it works well.
Quoted 60 lines
---
Changes in v2:
- Ignorecorein KCOV.
- Link to v1: https://lore.kernel.org/r/[email protected]--- rust/Makefile | 1 + scripts/Makefile.kcov | 6 ++++++ scripts/Makefile.lib | 3 +++ 3 files changed, 10 insertions(+)rust/Makefile +1
-0@@ -492,6 +492,7 @@ $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) $(obj)/core.o: scripts/target.json endif +KCOV_INSTRUMENT_core.o := n $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'scripts/Makefile.kcov +6
-0@@ -3,4 +3,10 @@ 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 +kcov-rflags-y += -Cpasses=sancov-module +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-level=3 +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-trace-pc +kcov-rflags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -Cllvm-args=-sanitizer-coverage-trace-compares + export CFLAGS_KCOV := $(kcov-flags-y) +export RUSTFLAGS_KCOV := $(kcov-rflags-y)scripts/Makefile.lib +3
-1@@ -169,6 +169,9 @@ ifeq ($(CONFIG_KCOV),y) _c_flags += $(if $(patsubst n%,, \ $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ $(CFLAGS_KCOV)) +_rust_flags += $(if $(patsubst n%,, \ + $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ + $(RUSTFLAGS_KCOV)) endif # ---base-commit:
9c32cda43eb78f78c73aee4aa344b777714e259b
change-id: 20250430-rust-kcov-6c74fd0f1f06Best regards,
--
Alice Ryhl <[email protected]>
Miguel Ojeda
Acked-byQuoted 5 lines · Aleksandr Nogikh
Thanks for incorporating the core.o change!
I've tested the v2 patch on my local setup and it works well.Tested-by Aleksandr Nogikh <[email protected]>
Thanks for testing, very much appreciated.
Dmitry/Andrey: I guess you may want this to go through your tree (although I don't see a M: there), but if not, please let me know:
Cheers,
Miguel
Dmitry Vyukov
reviewerQuoted 6 lines · Miguel Ojeda
Quoted 4 lines · Aleksandr Nogikh
Thanks for incorporating the core.o change!
I've tested the v2 patch on my local setup and it works well.
Quoted 6 lines
Quoted 1 line
Tested-by Aleksandr Nogikh <[email protected]>Thanks for testing, very much appreciated.
Dmitry/Andrey: I guess you may want this to go through your tree (although I don't see a
M:there), but if not, please let me know:
KCOV does not have its own tree, it's merged via MM tree with MM maintainers effectively serving as final accepting maintainers.
Quoted 10 lines · Alice Ryhl
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
Kconfigfeature detection. The coverage level is set to 3, as that is the level needed by trace-pc.
Quoted 7 lines
We do not instrument
coresince when we fuzz the kernel, we are looking for bugs in the kernel, not the Rust stdlib.Co-developed-by Matthew Maurer <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Reviewed-by Alexander Potapenko <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Quoted 64 lines
---
I did not pick up the Tested-by due to the changes. I verified that it looks right under objdump, but I don't have a syzkaller setup I can try it with.
---
Changes in v2:
- Ignorecorein KCOV.
- Link to v1: https://lore.kernel.org/r/[email protected]--- rust/Makefile | 1 + scripts/Makefile.kcov | 6 ++++++ scripts/Makefile.lib | 3 +++ 3 files changed, 10 insertions(+)rust/Makefile +1
-0@@ -492,6 +492,7 @@ $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) $(obj)/core.o: scripts/target.json endif +KCOV_INSTRUMENT_core.o := n $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'scripts/Makefile.kcov +6
-0@@ -3,4 +3,10 @@ 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 +kcov-rflags-y += -Cpasses=sancov-module +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-level=3 +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-trace-pc +kcov-rflags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -Cllvm-args=-sanitizer-coverage-trace-compares + export CFLAGS_KCOV := $(kcov-flags-y) +export RUSTFLAGS_KCOV := $(kcov-rflags-y)scripts/Makefile.lib +3
-1@@ -169,6 +169,9 @@ ifeq ($(CONFIG_KCOV),y) _c_flags += $(if $(patsubst n%,, \ $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ $(CFLAGS_KCOV)) +_rust_flags += $(if $(patsubst n%,, \ + $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ + $(RUSTFLAGS_KCOV)) endif # ---base-commit:
9c32cda43eb78f78c73aee4aa344b777714e259b
change-id: 20250430-rust-kcov-6c74fd0f1f06Best regards,
--
Alice Ryhl <[email protected]>
+mm
Quoted 86 lines · Dmitry Vyukov
Quoted 10 lines · Alice Ryhl
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
Kconfigfeature detection. The coverage level is set to 3, as that is the level needed by trace-pc.Quoted 7 lines
We do not instrument
coresince when we fuzz the kernel, we are looking for bugs in the kernel, not the Rust stdlib.Co-developed-by Matthew Maurer <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Reviewed-by Alexander Potapenko <[email protected]>Signed-off-by Alice Ryhl <[email protected]>Reviewed-by Dmitry Vyukov <[email protected]>Quoted 64 lines
---
I did not pick up the Tested-by due to the changes. I verified that it looks right under objdump, but I don't have a syzkaller setup I can try it with.
---
Changes in v2:
- Ignorecorein KCOV.
- Link to v1: https://lore.kernel.org/r/[email protected]--- rust/Makefile | 1 + scripts/Makefile.kcov | 6 ++++++ scripts/Makefile.lib | 3 +++ 3 files changed, 10 insertions(+)rust/Makefile +1
-0@@ -492,6 +492,7 @@ $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) $(obj)/core.o: scripts/target.json endif +KCOV_INSTRUMENT_core.o := n $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'scripts/Makefile.kcov +6
-0@@ -3,4 +3,10 @@ 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 +kcov-rflags-y += -Cpasses=sancov-module +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-level=3 +kcov-rflags-y += -Cllvm-args=-sanitizer-coverage-trace-pc +kcov-rflags-$(CONFIG_KCOV_ENABLE_COMPARISONS) += -Cllvm-args=-sanitizer-coverage-trace-compares + export CFLAGS_KCOV := $(kcov-flags-y) +export RUSTFLAGS_KCOV := $(kcov-rflags-y)scripts/Makefile.lib +3
-1@@ -169,6 +169,9 @@ ifeq ($(CONFIG_KCOV),y) _c_flags += $(if $(patsubst n%,, \ $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ $(CFLAGS_KCOV)) +_rust_flags += $(if $(patsubst n%,, \ + $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \ + $(RUSTFLAGS_KCOV)) endif # ---base-commit:
9c32cda43eb78f78c73aee4aa344b777714e259b
change-id: 20250430-rust-kcov-6c74fd0f1f06Best regards,
--
Alice Ryhl <[email protected]>