lib: sbi: add UBSan support
PATCH v2 1/1 lib: sbi: add UBSan support
From: marcos <[email protected]>
UBSan (Undefined Behavior Sanitizer) is a tool implemented using compiler instrumentation at runtime that allows checking for statements whose output is not deterministic or defined by the C standard, incurring unexpected results in the execution of the program. Compiling and testing the OpenSBI firmware against UBSan will print a message in the console if any sentence performs any of these actions.
Support for this implementation involves two main components:
1.- The UBSan implementation hooks (derived from NetBSD), used by the
compiler to handle the check output.
2.- A test suite to verify correct operation at runtime.
Usage:
Users may compile OpenSBI with the UBSan instrumentation adding the next flag to the make command:
ENABLEUBSAN=y
Users may compile OpenSBI with the UBSan tests adding the next flag to the make command:
ENABLEUBSANTESTS=y
Note that the implementation of UBSan in the compilation of OpenSBI adds a certain overhead caused by the checks performed at runtime; therefore, it is only expected to be used in development builds, never in production. If ENABLEUBSAN is not set, tests won't be compiled even if the ENABLEUBSANTESTS flag is enabled.
--- Makefile | 14 + include/sbi/sbi_ubsan_test.h | 8 + lib/sbi/objects.mk | 6 + lib/sbi/sbi_init.c | 6 + lib/sbi/sbi_ubsan.c | 849 +++++++++++++++++ lib/sbi/sbi_ubsan_test.c | 147 +++ v2-0001-lib-sbi-add-UBSan-support.patch | 1145 +++++++++++++++++++++++ 7 files changed, 2175 insertions(+) create mode 100644 include/sbi/sbi_ubsan_test.h create mode 100644 lib/sbi/sbi_ubsan.c create mode 100644 lib/sbi/sbi_ubsan_test.c create mode 100644 v2-0001-lib-sbi-add-UBSan-support.patch
Makefile +14 -0
@@ -455,6 +455,20 @@ else CFLAGS += -O2 endif +ifeq ($(ENABLEUBSAN),y) +UBSAN_CC_FLAGS := -fsanitize=undefined +UBSAN_CC_FLAGS += -DUBSAN_ENABLED +ifeq ($(ENABLEUBSANTESTS),y) +UBSAN_CC_FLAGS += -DUBSAN_TESTS_ENABLED +endif +UBSAN_CC_FLAGS += -fno-sanitize=vptr +UBSAN_CC_FLAGS += -fno-sanitize=float-cast-overflow +UBSAN_CC_FLAGS += -fno-sanitize=float-divide-by-zero +UBSAN_CC_FLAGS += -fsanitize-recover=undefined +UBSAN_CC_FLAGS += -fsanitize=pointer-overflow +CFLAGS += $(UBSAN_CC_FLAGS) +endif + ifeq ($(V), 1) ELFFLAGS += -Wl,--print-gc-sections endif
include/sbi/sbi_ubsan_test.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifdef UBSAN_TESTS_ENABLED +#ifndef __SBI_UBSAN_TEST_H__ +#define __SBI_UBSAN_TEST_H__ + +void sbi_ubsan_test_suite (void ); + +#endif +#endif
lib/sbi/objects.mk +6 -0
@@ -99,6 +99,12 @@ libsbi-objs-y += sbi_tlb.o libsbi-objs-y += sbi_trap.o libsbi-objs-y += sbi_trap_ldst.o libsbi-objs-y += sbi_trap_v_ldst.o +ifeq ($(ENABLEUBSAN), y) +libsbi-objs-y += sbi_ubsan.o +ifeq ($(ENABLEUBSANTESTS), y) +libsbi-objs-y += sbi_ubsan_test.o +endif +endif libsbi-objs-y += sbi_unpriv.o libsbi-objs-y += sbi_expected_trap.o libsbi-objs-y += sbi_cppc.o
lib/sbi/sbi_init.c +6 -0
@@ -35,6 +35,8 @@#include <sbi/sbi_tlb.h> #include <sbi/sbi_version.h> #include <sbi/sbi_unit_test.h> +#include <sbi/sbi_ubsan_test.h> +#define BANNER \ " ____ _____ ____ _____\n" \
@@ -288,6 +290,10 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)sbi_double_trap_init (scratch); +#ifdef UBSAN_TESTS_ENABLED +sbi_ubsan_test_suite (); +#endif + rc =sbi_irqchip_init (scratch, true);if (rc) { sbi_printf("%s: irqchip init failed (error %d)\n",
lib/sbi/sbi_ubsan.c new file +849 -0
850 lines not on this page · raw message
lib/sbi/sbi_ubsan_test.c new file +147 -0
148 lines not on this page · raw message
v2-0001-lib-sbi-add-UBSan-support.patch new file +1145 -0
1,145 lines not on this page · raw message
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi