From owner-svn-src-all@freebsd.org Tue Nov 6 17:32:10 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 01020112AE4D; Tue, 6 Nov 2018 17:32:10 +0000 (UTC) (envelope-from andrew@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 7E07882546; Tue, 6 Nov 2018 17:32:09 +0000 (UTC) (envelope-from andrew@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5D97B1CB6; Tue, 6 Nov 2018 17:32:09 +0000 (UTC) (envelope-from andrew@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id wA6HW9Sb068643; Tue, 6 Nov 2018 17:32:09 GMT (envelope-from andrew@FreeBSD.org) Received: (from andrew@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id wA6HW8af068637; Tue, 6 Nov 2018 17:32:08 GMT (envelope-from andrew@FreeBSD.org) Message-Id: <201811061732.wA6HW8af068637@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: andrew set sender to andrew@FreeBSD.org using -f From: Andrew Turner Date: Tue, 6 Nov 2018 17:32:08 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r340191 - in head/sys: conf kern X-SVN-Group: head X-SVN-Commit-Author: andrew X-SVN-Commit-Paths: in head/sys: conf kern X-SVN-Commit-Revision: 340191 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 7E07882546 X-Spamd-Result: default: False [-103.10 / 200.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; ALLOW_DOMAIN_WHITELIST(-100.00)[FreeBSD.org]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-0.99)[-0.995,0]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; DMARC_NA(0.00)[FreeBSD.org]; RCVD_COUNT_THREE(0.00)[4]; MX_GOOD(-0.01)[cached: mx1.FreeBSD.org]; NEURAL_HAM_SHORT(-1.00)[-0.998,0]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; RCVD_TLS_LAST(0.00)[] X-Rspamd-Server: mx1.freebsd.org X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Nov 2018 17:32:10 -0000 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 +#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 #include -#include -#define ASSERT(x) KASSERT(x) +#include +#include +#include +#include +#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"