Author: andrew
Date: Tue Nov  6 17:32:07 2018
New Revision: 340191
URL: https://svnweb.freebsd.org/changeset/base/340191

Log:
  Port the NetBSD ubsan runtime to the FreeBSD kernel.
  
  This allows us to build the ubsan code added in r340189 into the kernel
  with the KUBSAN option. This will report when undefined behaviour is
  detected in the currently running kernel.
  
  As it can be large, the kernel is 65MB on arm64, loader may not be able to
  load the kernel on all architectures so is disabled by default for now.
  
  Sponsored by: DARPA, AFRL

Modified:
  head/sys/conf/files
  head/sys/conf/kern.post.mk
  head/sys/conf/kern.pre.mk
  head/sys/conf/kmod.mk
  head/sys/conf/options
  head/sys/kern/kern_ubsan.c   (contents, props changed)

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files Tue Nov  6 17:31:09 2018        (r340190)
+++ head/sys/conf/files Tue Nov  6 17:32:07 2018        (r340191)
@@ -3823,6 +3823,7 @@ kern/kern_thread.c                standard
 kern/kern_time.c               standard
 kern/kern_timeout.c            standard
 kern/kern_tslog.c              optional tslog
+kern/kern_ubsan.c              optional kubsan
 kern/kern_umtx.c               standard
 kern/kern_uuid.c               standard
 kern/kern_xxx.c                        standard

Modified: head/sys/conf/kern.post.mk
==============================================================================
--- head/sys/conf/kern.post.mk  Tue Nov  6 17:31:09 2018        (r340190)
+++ head/sys/conf/kern.post.mk  Tue Nov  6 17:32:07 2018        (r340191)
@@ -28,6 +28,10 @@ MKMODULESENV+=       WITH_CTF="${WITH_CTF}"
 MKMODULESENV+= WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}"
 .endif
 
+.if defined(SAN_CFLAGS)
+MKMODULESENV+= SAN_CFLAGS="${SAN_CFLAGS}"
+.endif
+
 # Allow overriding the kernel debug directory, so kernel and user debug may be
 # installed in different directories. Setting it to "" restores the historical
 # behavior of installing debug files in the kernel directory.

Modified: head/sys/conf/kern.pre.mk
==============================================================================
--- head/sys/conf/kern.pre.mk   Tue Nov  6 17:31:09 2018        (r340190)
+++ head/sys/conf/kern.pre.mk   Tue Nov  6 17:32:07 2018        (r340191)
@@ -113,6 +113,12 @@ PROF=              -pg
 .endif
 DEFINED_PROF=  ${PROF}
 
+KUBSAN_ENABLED!=       grep KUBSAN opt_global.h || true ; echo
+.if !empty(KUBSAN_ENABLED)
+SAN_CFLAGS+=   -fsanitize=undefined
+.endif
+CFLAGS+=       ${SAN_CFLAGS}
+
 # Put configuration-specific C flags last (except for ${PROF}) so that they
 # can override the others.
 CFLAGS+=       ${CONF_CFLAGS}

Modified: head/sys/conf/kmod.mk
==============================================================================
--- head/sys/conf/kmod.mk       Tue Nov  6 17:31:09 2018        (r340190)
+++ head/sys/conf/kmod.mk       Tue Nov  6 17:32:07 2018        (r340191)
@@ -377,6 +377,9 @@ ${_src}:
 .endfor
 .endif
 
+# Add the sanitizer C flags
+CFLAGS+=       ${SAN_CFLAGS}
+
 # Respect configuration-specific C flags.
 CFLAGS+=       ${ARCH_FLAGS} ${CONF_CFLAGS}
 

Modified: head/sys/conf/options
==============================================================================
--- head/sys/conf/options       Tue Nov  6 17:31:09 2018        (r340190)
+++ head/sys/conf/options       Tue Nov  6 17:32:07 2018        (r340191)
@@ -232,6 +232,9 @@ UMTX_CHAINS opt_global.h
 VERBOSE_SYSINIT
 ZSTDIO         opt_zstdio.h
 
+# Sanitizers
+KUBSAN         opt_global.h
+
 # POSIX kernel options
 P1003_1B_MQUEUE                        opt_posix.h
 P1003_1B_SEMAPHORES            opt_posix.h

Modified: head/sys/kern/kern_ubsan.c
==============================================================================
--- head/sys/kern/kern_ubsan.c  Tue Nov  6 17:31:09 2018        (r340190)
+++ head/sys/kern/kern_ubsan.c  Tue Nov  6 17:32:07 2018        (r340191)
@@ -37,17 +37,29 @@
  */
 
 #include <sys/cdefs.h>
+#ifdef __FreeBSD__
+__FBSDID("$FreeBSD$");
+#else
 #if defined(_KERNEL)
 __KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
 #else
 __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
 #endif
+#endif
 
 #if defined(_KERNEL)
 #include <sys/param.h>
 #include <sys/types.h>
-#include <sys/stdarg.h>
-#define ASSERT(x) KASSERT(x)
+#include <sys/limits.h>
+#include <sys/systm.h>
+#include <machine/_inttypes.h>
+#include <machine/stdarg.h>
+#define        ASSERT(x) KASSERT(x, ("%s: " __STRING(x) " failed", __func__))
+#define        __arraycount(x) nitems(x)
+#define        ISSET(x, y)     ((x) & (y))
+#define        __BIT(x)        ((uintmax_t)1 << (uintmax_t)(x))
+#define        __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ 
(__mask))
+#define        __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / 
__LOWEST_SET_BIT(__mask))
 #else
 #if defined(_LIBC)
 #include "namespace.h"
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to