From cf4670fe0b1049863ed3150a6ffb0b80dad151b2 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Tue, 14 Sep 2021 14:29:27 -0400 Subject: [PATCH] kcov: Integrate with KMSAN - kern_kcov.c needs to be compiled with -fsanitize=kernel-memory when KMSAN is configured since it calls into various other subsystems. - Disable address and memory sanitizers in kcov(4)'s coverage sanitizer callbacks, as they do not provide useful checking. Moreover, with KMSAN we may otherwise get false positives since the caller (coverage sanitizer runtime) is not instrumented. - Disable KASAN and KMSAN interceptors in subr_coverage.c, as they do not provide any benefit but do introduce overhead when fuzzing. Sponsored by: The FreeBSD Foundation --- sys/conf/files | 2 +- sys/kern/kern_kcov.c | 7 ++++--- sys/kern/subr_coverage.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sys/conf/files b/sys/conf/files index a5690ec1df7..eb0c489b783 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -3826,7 +3826,7 @@ kern/kern_idle.c standard kern/kern_intr.c standard kern/kern_jail.c standard kern/kern_kcov.c optional kcov \ - compile-with "${NORMAL_C:N-fsanitize*}" + compile-with "${NORMAL_C:N-fsanitize*} ${NORMAL_C:M-fsanitize=kernel-memory}" kern/kern_khelp.c standard kern/kern_kthread.c standard kern/kern_ktr.c optional ktr diff --git a/sys/kern/kern_kcov.c b/sys/kern/kern_kcov.c index 23e0da4cdb7..7a11f800c7c 100644 --- a/sys/kern/kern_kcov.c +++ b/sys/kern/kern_kcov.c @@ -35,7 +35,8 @@ * $FreeBSD$ */ -#ifdef KCSAN +/* Interceptors are required for KMSAN. */ +#if defined(KASAN) || defined(KCSAN) #define SAN_RUNTIME #endif @@ -191,7 +192,7 @@ get_kinfo(struct thread *td) return (info); } -static void +static void __nosanitizeaddress __nosanitizememory trace_pc(uintptr_t ret) { struct thread *td; @@ -223,7 +224,7 @@ trace_pc(uintptr_t ret) buf[0] = index + 1; } -static bool +static bool __nosanitizeaddress __nosanitizememory trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uint64_t ret) { struct thread *td; diff --git a/sys/kern/subr_coverage.c b/sys/kern/subr_coverage.c index 9a719bcaeca..28467af6a8d 100644 --- a/sys/kern/subr_coverage.c +++ b/sys/kern/subr_coverage.c @@ -35,7 +35,7 @@ * $FreeBSD$ */ -#ifdef KCSAN +#ifdef SAN_NEEDS_INTERCEPTORS #define SAN_RUNTIME #endif