Linux 6.9-rc1
[linux-2.6-microblaze.git] / lib / ubsan.c
index 36bd75e..5fc107f 100644 (file)
 #include <linux/types.h>
 #include <linux/sched.h>
 #include <linux/uaccess.h>
+#include <linux/ubsan.h>
 #include <kunit/test-bug.h>
 
 #include "ubsan.h"
 
+#ifdef CONFIG_UBSAN_TRAP
+/*
+ * Only include matches for UBSAN checks that are actually compiled in.
+ * The mappings of struct SanitizerKind (the -fsanitize=xxx args) to
+ * enum SanitizerHandler (the traps) in Clang is in clang/lib/CodeGen/.
+ */
+const char *report_ubsan_failure(struct pt_regs *regs, u32 check_type)
+{
+       switch (check_type) {
+#ifdef CONFIG_UBSAN_BOUNDS
+       /*
+        * SanitizerKind::ArrayBounds and SanitizerKind::LocalBounds
+        * emit SanitizerHandler::OutOfBounds.
+        */
+       case ubsan_out_of_bounds:
+               return "UBSAN: array index out of bounds";
+#endif
+#ifdef CONFIG_UBSAN_SHIFT
+       /*
+        * SanitizerKind::ShiftBase and SanitizerKind::ShiftExponent
+        * emit SanitizerHandler::ShiftOutOfBounds.
+        */
+       case ubsan_shift_out_of_bounds:
+               return "UBSAN: shift out of bounds";
+#endif
+#ifdef CONFIG_UBSAN_DIV_ZERO
+       /*
+        * SanitizerKind::IntegerDivideByZero emits
+        * SanitizerHandler::DivremOverflow.
+        */
+       case ubsan_divrem_overflow:
+               return "UBSAN: divide/remainder overflow";
+#endif
+#ifdef CONFIG_UBSAN_UNREACHABLE
+       /*
+        * SanitizerKind::Unreachable emits
+        * SanitizerHandler::BuiltinUnreachable.
+        */
+       case ubsan_builtin_unreachable:
+               return "UBSAN: unreachable code";
+#endif
+#if defined(CONFIG_UBSAN_BOOL) || defined(CONFIG_UBSAN_ENUM)
+       /*
+        * SanitizerKind::Bool and SanitizerKind::Enum emit
+        * SanitizerHandler::LoadInvalidValue.
+        */
+       case ubsan_load_invalid_value:
+               return "UBSAN: loading invalid value";
+#endif
+#ifdef CONFIG_UBSAN_ALIGNMENT
+       /*
+        * SanitizerKind::Alignment emits SanitizerHandler::TypeMismatch
+        * or SanitizerHandler::AlignmentAssumption.
+        */
+       case ubsan_alignment_assumption:
+               return "UBSAN: alignment assumption";
+       case ubsan_type_mismatch:
+               return "UBSAN: type mismatch";
+#endif
+       default:
+               return "UBSAN: unrecognized failure code";
+       }
+}
+
+#else
 static const char * const type_check_kinds[] = {
        "load of",
        "store to",
@@ -138,8 +204,8 @@ static void ubsan_prologue(struct source_location *loc, const char *reason)
 {
        current->in_ubsan++;
 
-       pr_err("========================================"
-               "========================================\n");
+       pr_warn(CUT_HERE);
+
        pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
                loc->line & LINE_MASK, loc->column & COLUMN_MASK);
 
@@ -149,15 +215,81 @@ static void ubsan_prologue(struct source_location *loc, const char *reason)
 static void ubsan_epilogue(void)
 {
        dump_stack();
-       pr_err("========================================"
-               "========================================\n");
+       pr_warn("---[ end trace ]---\n");
 
        current->in_ubsan--;
 
-       if (panic_on_warn)
-               panic("panic_on_warn set ...\n");
+       check_panic_on_warn("UBSAN");
 }
 
+static void handle_overflow(struct overflow_data *data, void *lhs,
+                       void *rhs, char op)
+{
+
+       struct type_descriptor *type = data->type;
+       char lhs_val_str[VALUE_LENGTH];
+       char rhs_val_str[VALUE_LENGTH];
+
+       if (suppress_report(&data->location))
+               return;
+
+       ubsan_prologue(&data->location, type_is_signed(type) ?
+                       "signed-integer-overflow" :
+                       "unsigned-integer-overflow");
+
+       val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
+       val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
+       pr_err("%s %c %s cannot be represented in type %s\n",
+               lhs_val_str,
+               op,
+               rhs_val_str,
+               type->type_name);
+
+       ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(void *data,
+                               void *lhs, void *rhs)
+{
+
+       handle_overflow(data, lhs, rhs, '+');
+}
+EXPORT_SYMBOL(__ubsan_handle_add_overflow);
+
+void __ubsan_handle_sub_overflow(void *data,
+                               void *lhs, void *rhs)
+{
+       handle_overflow(data, lhs, rhs, '-');
+}
+EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
+
+void __ubsan_handle_mul_overflow(void *data,
+                               void *lhs, void *rhs)
+{
+       handle_overflow(data, lhs, rhs, '*');
+}
+EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
+
+void __ubsan_handle_negate_overflow(void *_data, void *old_val)
+{
+       struct overflow_data *data = _data;
+       char old_val_str[VALUE_LENGTH];
+
+       if (suppress_report(&data->location))
+               return;
+
+       ubsan_prologue(&data->location, "negation-overflow");
+
+       val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
+
+       pr_err("negation of %s cannot be represented in type %s:\n",
+               old_val_str, data->type->type_name);
+
+       ubsan_epilogue();
+}
+EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
+
+
 void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
 {
        struct overflow_data *data = _data;
@@ -340,9 +472,10 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
 {
        struct invalid_value_data *data = _data;
        char val_str[VALUE_LENGTH];
+       unsigned long ua_flags = user_access_save();
 
        if (suppress_report(&data->location))
-               return;
+               goto out;
 
        ubsan_prologue(&data->location, "invalid-load");
 
@@ -352,12 +485,11 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
                val_str, data->type->type_name);
 
        ubsan_epilogue();
+out:
+       user_access_restore(ua_flags);
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
 
-void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
-                                        unsigned long align,
-                                        unsigned long offset);
 void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
                                         unsigned long align,
                                         unsigned long offset)
@@ -385,3 +517,5 @@ void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
        ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
+
+#endif /* !CONFIG_UBSAN_TRAP */