|
14 | 14 | #include <linux/types.h> |
15 | 15 | #include <linux/sched.h> |
16 | 16 | #include <linux/uaccess.h> |
| 17 | +#include <linux/ubsan.h> |
17 | 18 | #include <kunit/test-bug.h> |
18 | 19 |
|
19 | 20 | #include "ubsan.h" |
20 | 21 |
|
| 22 | +#ifdef CONFIG_UBSAN_TRAP |
| 23 | +/* |
| 24 | + * Only include matches for UBSAN checks that are actually compiled in. |
| 25 | + * The mappings of struct SanitizerKind (the -fsanitize=xxx args) to |
| 26 | + * enum SanitizerHandler (the traps) in Clang is in clang/lib/CodeGen/. |
| 27 | + */ |
| 28 | +const char *report_ubsan_failure(struct pt_regs *regs, u32 check_type) |
| 29 | +{ |
| 30 | + switch (check_type) { |
| 31 | +#ifdef CONFIG_UBSAN_BOUNDS |
| 32 | + /* |
| 33 | + * SanitizerKind::ArrayBounds and SanitizerKind::LocalBounds |
| 34 | + * emit SanitizerHandler::OutOfBounds. |
| 35 | + */ |
| 36 | + case ubsan_out_of_bounds: |
| 37 | + return "UBSAN: array index out of bounds"; |
| 38 | +#endif |
| 39 | +#ifdef CONFIG_UBSAN_SHIFT |
| 40 | + /* |
| 41 | + * SanitizerKind::ShiftBase and SanitizerKind::ShiftExponent |
| 42 | + * emit SanitizerHandler::ShiftOutOfBounds. |
| 43 | + */ |
| 44 | + case ubsan_shift_out_of_bounds: |
| 45 | + return "UBSAN: shift out of bounds"; |
| 46 | +#endif |
| 47 | +#ifdef CONFIG_UBSAN_DIV_ZERO |
| 48 | + /* |
| 49 | + * SanitizerKind::IntegerDivideByZero emits |
| 50 | + * SanitizerHandler::DivremOverflow. |
| 51 | + */ |
| 52 | + case ubsan_divrem_overflow: |
| 53 | + return "UBSAN: divide/remainder overflow"; |
| 54 | +#endif |
| 55 | +#ifdef CONFIG_UBSAN_UNREACHABLE |
| 56 | + /* |
| 57 | + * SanitizerKind::Unreachable emits |
| 58 | + * SanitizerHandler::BuiltinUnreachable. |
| 59 | + */ |
| 60 | + case ubsan_builtin_unreachable: |
| 61 | + return "UBSAN: unreachable code"; |
| 62 | +#endif |
| 63 | +#if defined(CONFIG_UBSAN_BOOL) || defined(CONFIG_UBSAN_ENUM) |
| 64 | + /* |
| 65 | + * SanitizerKind::Bool and SanitizerKind::Enum emit |
| 66 | + * SanitizerHandler::LoadInvalidValue. |
| 67 | + */ |
| 68 | + case ubsan_load_invalid_value: |
| 69 | + return "UBSAN: loading invalid value"; |
| 70 | +#endif |
| 71 | +#ifdef CONFIG_UBSAN_ALIGNMENT |
| 72 | + /* |
| 73 | + * SanitizerKind::Alignment emits SanitizerHandler::TypeMismatch |
| 74 | + * or SanitizerHandler::AlignmentAssumption. |
| 75 | + */ |
| 76 | + case ubsan_alignment_assumption: |
| 77 | + return "UBSAN: alignment assumption"; |
| 78 | + case ubsan_type_mismatch: |
| 79 | + return "UBSAN: type mismatch"; |
| 80 | +#endif |
| 81 | + default: |
| 82 | + return "UBSAN: unrecognized failure code"; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#else |
21 | 87 | static const char * const type_check_kinds[] = { |
22 | 88 | "load of", |
23 | 89 | "store to", |
@@ -384,3 +450,5 @@ void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, |
384 | 450 | ubsan_epilogue(); |
385 | 451 | } |
386 | 452 | EXPORT_SYMBOL(__ubsan_handle_alignment_assumption); |
| 453 | + |
| 454 | +#endif /* !CONFIG_UBSAN_TRAP */ |
0 commit comments