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
This commit is contained in:
Andrew Turner 2018-11-06 17:32:07 +00:00
parent 0645126fae
commit 4ea56599e8
6 changed files with 31 additions and 2 deletions

View File

@ -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

View File

@ -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.

View File

@ -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}

View File

@ -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}

View File

@ -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

View File

@ -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"