xen: hook up UBSAN with CONFIG_UBSAN
authorWei Liu <wei.liu2@citrix.com>
Mon, 9 Oct 2017 13:54:58 +0000 (14:54 +0100)
committerWei Liu <wei.liu2@citrix.com>
Mon, 9 Oct 2017 16:05:00 +0000 (17:05 +0100)
Make the following changes:

1. Introduce CONFIG_UBSAN and other auxiliary options.
2. Introduce Build system rune to filter objects.
3. Make ubsan.c build.

Currently only x86 is supported. All init.o's are filtered out because
of limitation in the build system. There is no user of noubsan-y yet
but it is worth keeping to ease future development.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
xen/Kconfig
xen/Kconfig.debug
xen/Rules.mk
xen/arch/x86/Kconfig
xen/common/Kconfig
xen/common/Makefile
xen/common/ubsan/ubsan.c

index 65d491d7762fd06d24f6f865b44f1372bb840c4f..ea7229ad1f956546fbe19fb3a5ea33fcd2ee9b0f 100644 (file)
@@ -38,4 +38,10 @@ config LTO
 
          If unsure, say N.
 
+#
+# For architectures that know their compiler __int128 support is sound
+#
+config ARCH_SUPPORTS_INT128
+       bool
+
 source "Kconfig.debug"
index 195d5041478ab356ad74634ee69c5b82416fd7ff..3329c75bfdbd19eb41fea2ca63ad20f57abbb503 100644 (file)
@@ -121,6 +121,16 @@ config SCRUB_DEBUG
          Verify that pages that need to be scrubbed before being allocated to
          a guest are indeed scrubbed.
 
+config UBSAN
+       bool "Undefined behaviour sanitizer"
+       depends on HAS_UBSAN
+       ---help---
+         Enable undefined behaviour sanitizer. It uses compiler to insert code
+         snippets so that undefined behaviours in C are detected during runtime.
+         This requires a UBSAN capable compiler and it is a debug only feature.
+
+         If unsure, say N here.
+
 endif # DEBUG || EXPERT
 
 endmenu
index cafc67b86e9f530e117f0259e3f76abbc310984c..2659f8a4d163a3d20eef0b0e826774b7733eaa9e 100644 (file)
@@ -119,6 +119,10 @@ ifeq ($(CONFIG_GCOV),y)
 $(filter-out %.init.o $(nogcov-y),$(obj-y) $(obj-bin-y) $(extra-y)): CFLAGS += -fprofile-arcs -ftest-coverage
 endif
 
+ifeq ($(CONFIG_UBSAN),y)
+$(filter-out %.init.o $(noubsan-y),$(obj-y) $(obj-bin-y) $(extra-y)): CFLAGS += -fsanitize=undefined
+endif
+
 ifeq ($(CONFIG_LTO),y)
 CFLAGS += -flto
 LDFLAGS-$(clang) += -plugin LLVMgold.so
index 30c2769684060b95c5ebb6777a26dc594d85c276..64955dc017987d96c1396aa079be164f1f02aa57 100644 (file)
@@ -5,6 +5,7 @@ config X86
        def_bool y
        select ACPI
        select ACPI_LEGACY_TABLES_LOOKUP
+       select ARCH_SUPPORTS_INT128
        select COMPAT
        select CORE_PARKING
        select HAS_ALTERNATIVE
@@ -21,6 +22,7 @@ config X86
        select HAS_PASSTHROUGH
        select HAS_PCI
        select HAS_PDX
+       select HAS_UBSAN
        select NUMA
        select VGA
 
index e9bb849298f40f91598b1b3296df7fafa10de186..103ef44cb5edb60ad2e0f07b659126947ec6f5ba 100644 (file)
@@ -32,6 +32,9 @@ config HAS_MEM_SHARING
 config HAS_PDX
        bool
 
+config HAS_UBSAN
+       bool
+
 config HAS_KEXEC
        bool
 
index 39e26145462b5b778f7c60c2455a1cb236fda327..66cc2c899528ee9880dc6dfb049ca8a3614a1a5c 100644 (file)
@@ -75,6 +75,7 @@ tmem-$(CONFIG_COMPAT) += compat/tmem_xen.o
 obj-$(CONFIG_TMEM) += $(tmem-y)
 
 subdir-$(CONFIG_GCOV) += gcov
+subdir-$(CONFIG_UBSAN) += ubsan
 
 subdir-y += libelf
 subdir-$(CONFIG_HAS_DEVICE_TREE) += libfdt
index 685b4de0d63edca014369d915d294fcb03835d16..fbe568562a875a64098e2c608a689811b718612d 100644 (file)
  *
  */
 
-#include <linux/bitops.h>
-#include <linux/bug.h>
-#include <linux/ctype.h>
-#include <linux/init.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
-#include <linux/sched.h>
+#include <xen/spinlock.h>
+#include <xen/percpu.h>
+
+#define __noreturn    noreturn
+#define pr_err(...) printk(XENLOG_ERR __VA_ARGS__)
+struct xen_ubsan { int in_ubsan; };
+static DEFINE_PER_CPU(struct xen_ubsan[1], in_ubsan);
+#undef current
+#define current this_cpu(in_ubsan)
+#define dump_stack dump_execution_state
+#define u64 long long unsigned int
+#define s64 long long int
 
 #include "ubsan.h"