Skip to contentSkip to conversation

Add support for using the Undefined Behaviour Sanitizer

PATCH In review Wei Liu 4 participants 11 messages Oct 9, 2017

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover Add support for using the Undefined Behaviour Sanitizer

Andrew Cooper (2):
xen/ubsan: Import ubsan implementation from Linux 4.13 xen/ubsan: Implement __ubsan_handle_nonnull_arg()

Wei Liu (1):
xen: hook up UBSAN with CONFIG_UBSAN

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Julien Grall <[email protected]>
 xen/Kconfig               |   6 +
 xen/Kconfig.debug         |  10 +
 xen/Rules.mk              |   4 +
 xen/arch/x86/Kconfig      |   2 +
 xen/common/Kconfig        |   3 +
 xen/common/Makefile       |   1 +
 xen/common/ubsan/Makefile |   1 +
 xen/common/ubsan/ubsan.c  | 480 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/common/ubsan/ubsan.h  |  84 ++++++++
 9 files changed, 591 insertions(+)
 create mode 100644 xen/common/ubsan/Makefile
 create mode 100644 xen/common/ubsan/ubsan.c
 create mode 100644 xen/common/ubsan/ubsan.h
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#1 lore raw

PATCH v2 1/3 xen/ubsan: Import ubsan implementation from Linux 4.13

From: Andrew Cooper <[email protected]>

A future change will adjust it to compile in Xen.

---
 xen/common/ubsan/Makefile |   1 +
 xen/common/ubsan/ubsan.c  | 456 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/common/ubsan/ubsan.h  |  84 +++++++++
 3 files changed, 541 insertions(+)
 create mode 100644 xen/common/ubsan/Makefile
 create mode 100644 xen/common/ubsan/ubsan.c
 create mode 100644 xen/common/ubsan/ubsan.h
xen/common/ubsan/Makefile new file +1 -0
new file mode 100644
index 0000000000..e6b85ea187
@@ -0,0 +1 @@
+obj-y += ubsan.o
xen/common/ubsan/ubsan.c new file +456 -0

456 lines not on this page · raw message

xen/common/ubsan/ubsan.h new file +84 -0

84 lines not on this page · raw message

Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#2 lore raw

PATCH v2 2/3 xen/ubsan: Implement __ubsan_handle_nonnull_arg()

In reply to Wei Liu

From: Andrew Cooper <[email protected]>

This hook appears to be missing from the Linux ubsan implemention. This patch is a forward port of https://lkml.org/lkml/2014/10/20/182

---
 xen/common/ubsan/ubsan.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
xen/common/ubsan/ubsan.c +19 -0
index fb0409df1b..685b4de0d6 100644
@@ -328,6 +328,25 @@ void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
 }
 EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
 
+void __ubsan_handle_nonnull_arg(struct nonnull_arg_data *data)
+{
+	unsigned long flags;
+
+	if (suppress_report(&data->location))
+		return;
+
+	ubsan_prologue(&data->location, &flags);
+
+	pr_err("null pointer passed as argument %d, declared with nonnull attribute\n",
+	       data->arg_index);
+
+	if (location_is_valid(&data->attr_location))
+		print_source_location("nonnull attribute declared in ",
+				      &data->attr_location);
+
+	ubsan_epilogue(&flags);
+}
+
 void __ubsan_handle_nonnull_return(struct nonnull_return_data *data)
 {
 	unsigned long flags;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#3 lore raw

PATCH v2 3/3 xen: hook up UBSAN with CONFIG_UBSAN

In reply to Wei Liu

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.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/Kconfig              |  6 ++++++
 xen/Kconfig.debug        | 10 ++++++++++
 xen/Rules.mk             |  4 ++++
 xen/arch/x86/Kconfig     |  2 ++
 xen/common/Kconfig       |  3 +++
 xen/common/Makefile      |  1 +
 xen/common/ubsan/ubsan.c | 19 ++++++++++++-------
 7 files changed, 38 insertions(+), 7 deletions(-)
xen/Kconfig +6 -0
index 65d491d776..ea7229ad1f 100644
@@ -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"
xen/Kconfig.debug +10 -0
index 195d504147..3329c75bfd 100644
@@ -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
xen/Rules.mk +4 -0
index cafc67b86e..2659f8a4d1 100644
@@ -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
xen/arch/x86/Kconfig +2 -0
index 30c2769684..64955dc017 100644
@@ -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
 
xen/common/Kconfig +3 -0
index e9bb849298..103ef44cb5 100644
@@ -32,6 +32,9 @@ config HAS_MEM_SHARING
 config HAS_PDX
 	bool
 
+config HAS_UBSAN
+	bool
+
 config HAS_KEXEC
 	bool
 
xen/common/Makefile +1 -0
index 39e2614546..66cc2c8995 100644
@@ -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
xen/common/ubsan/ubsan.c +12 -7
index 685b4de0d6..fbe568562a 100644
@@ -10,13 +10,18 @@
  *
  */
 
-#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"
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#4 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

xen/arch/x86/Kconfig

Quoted 4 lines · Wei Liu
xen/arch/x86/Kconfig +0 -0
index 30c2769684..64955dc017 100644

Based on Julien's testing, shouldn't ARM64 also select HAS_UBSAN? Otherwise, LGTM.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#5 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

Quoted 8 lines · Andrew Cooper

xen/arch/x86/Kconfig

Quoted 4 lines · Wei Liu
xen/arch/x86/Kconfig +0 -0
index 30c2769684..64955dc017 100644

Based on Julien's testing, shouldn't ARM64 also select HAS_UBSAN? Otherwise, LGTM.

IIRC there is an issue with the size of the image being larger than 2MB.

I will let Julien decide. I can't test ARM support anyway.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

xen/arch/x86/Kconfig

Quoted 4 lines · Wei Liu
xen/arch/x86/Kconfig +0 -0
index 30c2769684..64955dc017 100644

Based on Julien's testing, shouldn't ARM64 also select HAS_UBSAN?

I tested it with some patches on top. Currently, the size of Xen Arm is limited to 2MB. That limit will be exploded when building with UBSAN.

So for now, I will leave that unselectable for both Arm32 and Arm64.

Cheers,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

In reply to Wei Liu
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 09.10.17 at 16:11, <[email protected]> wrote:
xen/common/ubsan/ubsan.c +2 -7
@@ -10,13 +10,18 @@
  *
  */
 
-#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>

xen/common/ubsan/ubsan.c line 24

Quoted 10 lines
xen/common/ubsan/ubsan.c +10 -0
+
+#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

Wasn't it agreed to make these uint64_t and int64_t?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#8 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.10.17 at 16:11, <[email protected]> wrote:
xen/common/ubsan/ubsan.c +5 -7
@@ -10,13 +10,18 @@
  *
  */
 
-#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__)
Quoted 8 lines

xen/common/ubsan/ubsan.c line 24

Quoted 7 lines
xen/common/ubsan/ubsan.c +7 -0
+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

Wasn't it agreed to make these uint64_t and int64_t?

Sadly that doesn't work, because of the explicit casts used in the sprintf().

Depending on how much we tolerate modifying the Linux code, these can be removed if we change to using appropriate PRIx64 formatters instead. However, I would prefer to avoid changing the Linux code if possible.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#9 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

Quoted 20 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.10.17 at 16:11, <[email protected]> wrote:
xen/common/ubsan/ubsan.c +5 -7
@@ -10,13 +10,18 @@
  *
  */
 
-#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__)
Quoted 9 lines
Quoted 8 lines

xen/common/ubsan/ubsan.c line 24

Quoted 7 lines
xen/common/ubsan/ubsan.c +7 -0
+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

Wasn't it agreed to make these uint64_t and int64_t?

No, not yet.

Quoted 6 lines

Sadly that doesn't work, because of the explicit casts used in the sprintf().

Depending on how much we tolerate modifying the Linux code, these can be removed if we change to using appropriate PRIx64 formatters instead. However, I would prefer to avoid changing the Linux code if possible.

+1 for this.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Acked-by
#10 lore raw

Re: [PATCH v2 3/3] xen: hook up UBSAN with CONFIG_UBSAN

In reply to Andrew Cooper
Quoted 30 lines
Quoted 1 line
Quoted 1 line · On 09.10.17 at 16:38, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.10.17 at 16:11, <[email protected]> wrote:
xen/common/ubsan/ubsan.c +5 -7
@@ -10,13 +10,18 @@
  *
  */
 
-#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__)
Quoted 8 lines

xen/common/ubsan/ubsan.c line 24

Quoted 7 lines
xen/common/ubsan/ubsan.c +7 -0
+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

Wasn't it agreed to make these uint64_t and int64_t?

Quoted 6 lines

Sadly that doesn't work, because of the explicit casts used in the sprintf().

Depending on how much we tolerate modifying the Linux code, these can be removed if we change to using appropriate PRIx64 formatters instead. However, I would prefer to avoid changing the Linux code if possible.

Ah, yes, that's good enough a reason. Together with Julien's reply then

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel