git: 8b3c771fc62a - stable/13 - Add a KASAN option to the kernel build

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 01 Nov 2021 14:32:54 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=8b3c771fc62af7d0d9e5b36e835892924e44b144

commit 8b3c771fc62af7d0d9e5b36e835892924e44b144
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2021-04-13 20:29:47 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2021-11-01 13:56:14 +0000

    Add a KASAN option to the kernel build
    
    LLVM support for enabling KASAN has not yet landed so the option is not
    yet usable, but hopefully this will change soon.
    
    Reviewed by:    imp, andrew
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 01028c736cbcdba079967c787bee1551fc8439aa)
---
 sys/conf/kern.pre.mk | 10 ++++++++++
 sys/conf/options     |  1 +
 2 files changed, 11 insertions(+)

diff --git a/sys/conf/kern.pre.mk b/sys/conf/kern.pre.mk
index bb52d1b9fbc4..8cab54d0763a 100644
--- a/sys/conf/kern.pre.mk
+++ b/sys/conf/kern.pre.mk
@@ -107,6 +107,16 @@ PROF=		-pg
 .endif
 DEFINED_PROF=	${PROF}
 
+KASAN_ENABLED!=	grep KASAN opt_global.h || true ; echo
+.if !empty(KASAN_ENABLED)
+SAN_CFLAGS+=	-fsanitize=kernel-address \
+		-mllvm -asan-stack=true \
+		-mllvm -asan-instrument-dynamic-allocas=true \
+		-mllvm -asan-globals=true \
+		-mllvm -asan-use-after-scope=true \
+		-mllvm -asan-instrumentation-with-call-threshold=0
+.endif
+
 KCSAN_ENABLED!=	grep KCSAN opt_global.h || true ; echo
 .if !empty(KCSAN_ENABLED)
 SAN_CFLAGS+=	-fsanitize=thread
diff --git a/sys/conf/options b/sys/conf/options
index 121a23ed876c..774850158221 100644
--- a/sys/conf/options
+++ b/sys/conf/options
@@ -229,6 +229,7 @@ ZSTDIO		opt_zstdio.h
 
 # Sanitizers
 COVERAGE	opt_global.h
+KASAN		opt_global.h
 KCOV
 KCSAN		opt_global.h
 KUBSAN		opt_global.h