git: 4ae699122810 - main - dtrace: Add WITH_DTRACE_ASAN

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 27 Jul 2023 20:13:01 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=4ae6991228105eb34989c870194ae7b0a1e23be4

commit 4ae6991228105eb34989c870194ae7b0a1e23be4
Author:     Domagoj Stolfa <ds815@cam.ac.uk>
AuthorDate: 2023-07-27 18:27:42 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-07-27 20:07:34 +0000

    dtrace: Add WITH_DTRACE_ASAN
    
    This option is a blanket for all the DTrace-related software. The option
    when enabled passes in -fsanitize=address -fsanitize=undeifned, enabling
    ASAN and UBSAN in the following components:
    
     - libdtrace
     - dtrace(1)
     - lockstat(1)
     - plockstat(1)
    
    The option defaults to "no" and is intended as a developer aid.
    
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D41157
---
 cddl/lib/libdtrace/Makefile          | 7 +++++++
 cddl/usr.sbin/dtrace/Makefile        | 5 +++++
 cddl/usr.sbin/lockstat/Makefile      | 7 +++++++
 cddl/usr.sbin/plockstat/Makefile     | 7 +++++++
 share/man/man5/src.conf.5            | 5 +++++
 share/mk/src.opts.mk                 | 1 +
 tools/build/options/WITH_DTRACE_ASAN | 4 ++++
 7 files changed, 36 insertions(+)

diff --git a/cddl/lib/libdtrace/Makefile b/cddl/lib/libdtrace/Makefile
index 23cc8d2db574..a6a7b90d3d39 100644
--- a/cddl/lib/libdtrace/Makefile
+++ b/cddl/lib/libdtrace/Makefile
@@ -1,5 +1,7 @@
 # $FreeBSD$
 
+.include <src.opts.mk>
+
 .PATH: ${SRCTOP}/cddl/contrib/opensolaris/lib/libdtrace/common
 .PATH: ${SRCTOP}/cddl/contrib/opensolaris/lib/libgen/common
 
@@ -138,6 +140,11 @@ DSRCS+=		regs_aarch64.d
 
 YFLAGS+=-d
 
+.if ${MK_DTRACE_ASAN} != "no"
+CFLAGS+=	-fsanitize=address -fsanitize=undefined
+LDFLAGS+=	-fsanitize=address -fsanitize=undefined
+.endif
+
 LIBADD=	ctf elf proc pthread rtld_db
 
 CLEANFILES=	dt_errtags.c dt_names.c
diff --git a/cddl/usr.sbin/dtrace/Makefile b/cddl/usr.sbin/dtrace/Makefile
index d8510e62fb29..fcc0668f1b1d 100644
--- a/cddl/usr.sbin/dtrace/Makefile
+++ b/cddl/usr.sbin/dtrace/Makefile
@@ -30,6 +30,11 @@ CFLAGS+= -DHAVE_ISSETUGID
 #CFLAGS+=	-DNEED_ERRLOC
 #YFLAGS+=	-d
 
+.if ${MK_DTRACE_ASAN} != "no"
+CFLAGS+=	-fsanitize=address -fsanitize=undefined
+LDFLAGS+=	-fsanitize=address -fsanitize=undefined
+.endif
+
 LIBADD=	dtrace ctf elf proc spl
 
 .if ${MK_DTRACE_TESTS} != "no"
diff --git a/cddl/usr.sbin/lockstat/Makefile b/cddl/usr.sbin/lockstat/Makefile
index a4bec42df367..72cedbdb20ed 100644
--- a/cddl/usr.sbin/lockstat/Makefile
+++ b/cddl/usr.sbin/lockstat/Makefile
@@ -1,5 +1,7 @@
 # $FreeBSD$
 
+.include <src.opts.mk>
+
 .PATH: ${SRCTOP}/cddl/contrib/opensolaris/cmd/lockstat
 
 PACKAGE=	dtrace
@@ -26,6 +28,11 @@ CFLAGS+=	-I${SRCTOP}/sys/cddl/compat/opensolaris \
 		-I${SRCTOP}/sys
 CFLAGS+= -DHAVE_ISSETUGID
 
+.if ${MK_DTRACE_ASAN} != "no"
+CFLAGS+=	-fsanitize=address -fsanitize=undefined
+LDFLAGS+=	-fsanitize=address -fsanitize=undefined
+.endif
+
 CFLAGS+=	-DNEED_ERRLOC -g
 
 #YFLAGS+=	-d
diff --git a/cddl/usr.sbin/plockstat/Makefile b/cddl/usr.sbin/plockstat/Makefile
index 22610870c846..41ef446c111b 100644
--- a/cddl/usr.sbin/plockstat/Makefile
+++ b/cddl/usr.sbin/plockstat/Makefile
@@ -1,5 +1,7 @@
 # $FreeBSD$
 
+.include <src.opts.mk>
+
 .PATH: ${SRCTOP}/cddl/contrib/opensolaris/cmd/plockstat
 
 PACKAGE=	dtrace
@@ -26,6 +28,11 @@ CFLAGS+=	-I${SRCTOP}/sys/cddl/compat/opensolaris \
 		-I${SRCTOP}/sys
 CFLAGS+= -DHAVE_ISSETUGID
 
+.if ${MK_DTRACE_ASAN} != "no"
+CFLAGS+=	-fsanitize=address -fsanitize=undefined
+LDFLAGS+=	-fsanitize=address -fsanitize=undefined
+.endif
+
 LIBADD=	dtrace proc
 
 .include <bsd.prog.mk>
diff --git a/share/man/man5/src.conf.5 b/share/man/man5/src.conf.5
index 0deb0216e027..69f0cc5feff9 100644
--- a/share/man/man5/src.conf.5
+++ b/share/man/man5/src.conf.5
@@ -594,6 +594,11 @@ When set, it enforces these options:
 .It
 .Va WITHOUT_CTF
 .El
+.It Va WITH_DTRACE_ASAN
+Compile userspace DTrace code (libdtrace, dtrace(1), lockstat(1), plockstat(1))
+with address and undefined behavior sanitizers.
+Requires that Clang be used as the base system compiler
+and that the runtime support library is available.
 .It Va WITH_DTRACE_TESTS
 Build and install the DTrace test suite in
 .Pa /usr/tests/cddl/usr.sbin/dtrace .
diff --git a/share/mk/src.opts.mk b/share/mk/src.opts.mk
index 899d620fb0c1..a799c1614fff 100644
--- a/share/mk/src.opts.mk
+++ b/share/mk/src.opts.mk
@@ -201,6 +201,7 @@ __DEFAULT_NO_OPTIONS = \
     CLANG_FORMAT \
     DETECT_TZ_CHANGES \
     DISK_IMAGE_TOOLS_BOOTSTRAP \
+    DTRACE_ASAN \
     DTRACE_TESTS \
     EXPERIMENTAL \
     HESIOD \
diff --git a/tools/build/options/WITH_DTRACE_ASAN b/tools/build/options/WITH_DTRACE_ASAN
new file mode 100644
index 000000000000..4240aee4b89b
--- /dev/null
+++ b/tools/build/options/WITH_DTRACE_ASAN
@@ -0,0 +1,4 @@
+Compile userspace DTrace code (libdtrace, dtrace(1), lockstat(1), plockstat(1))
+with address and undefined behavior sanitizers.
+Requires that Clang be used as the base system compiler
+and that the runtime support library is available.