1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LIB_UBSAN_H 2 #ifndef _LIB_UBSAN_H 3 #define _LIB_UBSAN_H 3 #define _LIB_UBSAN_H 4 4 5 /* 5 /* 6 * ABI defined by Clang's UBSAN enum Sanitizer 6 * ABI defined by Clang's UBSAN enum SanitizerHandler: 7 * https://github.com/llvm/llvm-project/blob/r 7 * https://github.com/llvm/llvm-project/blob/release/16.x/clang/lib/CodeGen/CodeGenFunction.h#L113 8 */ 8 */ 9 enum ubsan_checks { 9 enum ubsan_checks { 10 ubsan_add_overflow, 10 ubsan_add_overflow, 11 ubsan_builtin_unreachable, 11 ubsan_builtin_unreachable, 12 ubsan_cfi_check_fail, 12 ubsan_cfi_check_fail, 13 ubsan_divrem_overflow, 13 ubsan_divrem_overflow, 14 ubsan_dynamic_type_cache_miss, 14 ubsan_dynamic_type_cache_miss, 15 ubsan_float_cast_overflow, 15 ubsan_float_cast_overflow, 16 ubsan_function_type_mismatch, 16 ubsan_function_type_mismatch, 17 ubsan_implicit_conversion, 17 ubsan_implicit_conversion, 18 ubsan_invalid_builtin, 18 ubsan_invalid_builtin, 19 ubsan_invalid_objc_cast, 19 ubsan_invalid_objc_cast, 20 ubsan_load_invalid_value, 20 ubsan_load_invalid_value, 21 ubsan_missing_return, 21 ubsan_missing_return, 22 ubsan_mul_overflow, 22 ubsan_mul_overflow, 23 ubsan_negate_overflow, 23 ubsan_negate_overflow, 24 ubsan_nullability_arg, 24 ubsan_nullability_arg, 25 ubsan_nullability_return, 25 ubsan_nullability_return, 26 ubsan_nonnull_arg, 26 ubsan_nonnull_arg, 27 ubsan_nonnull_return, 27 ubsan_nonnull_return, 28 ubsan_out_of_bounds, 28 ubsan_out_of_bounds, 29 ubsan_pointer_overflow, 29 ubsan_pointer_overflow, 30 ubsan_shift_out_of_bounds, 30 ubsan_shift_out_of_bounds, 31 ubsan_sub_overflow, 31 ubsan_sub_overflow, 32 ubsan_type_mismatch, 32 ubsan_type_mismatch, 33 ubsan_alignment_assumption, 33 ubsan_alignment_assumption, 34 ubsan_vla_bound_not_positive, 34 ubsan_vla_bound_not_positive, 35 }; 35 }; 36 36 37 enum { 37 enum { 38 type_kind_int = 0, 38 type_kind_int = 0, 39 type_kind_float = 1, 39 type_kind_float = 1, 40 type_unknown = 0xffff 40 type_unknown = 0xffff 41 }; 41 }; 42 42 43 struct type_descriptor { 43 struct type_descriptor { 44 u16 type_kind; 44 u16 type_kind; 45 u16 type_info; 45 u16 type_info; 46 char type_name[]; 46 char type_name[]; 47 }; 47 }; 48 48 49 struct source_location { 49 struct source_location { 50 const char *file_name; 50 const char *file_name; 51 union { 51 union { 52 unsigned long reported; 52 unsigned long reported; 53 struct { 53 struct { 54 u32 line; 54 u32 line; 55 u32 column; 55 u32 column; 56 }; 56 }; 57 }; 57 }; 58 }; 58 }; 59 59 60 struct overflow_data { 60 struct overflow_data { 61 struct source_location location; 61 struct source_location location; 62 struct type_descriptor *type; 62 struct type_descriptor *type; 63 }; 63 }; 64 64 65 struct type_mismatch_data { 65 struct type_mismatch_data { 66 struct source_location location; 66 struct source_location location; 67 struct type_descriptor *type; 67 struct type_descriptor *type; 68 unsigned long alignment; 68 unsigned long alignment; 69 unsigned char type_check_kind; 69 unsigned char type_check_kind; 70 }; 70 }; 71 71 72 struct type_mismatch_data_v1 { 72 struct type_mismatch_data_v1 { 73 struct source_location location; 73 struct source_location location; 74 struct type_descriptor *type; 74 struct type_descriptor *type; 75 unsigned char log_alignment; 75 unsigned char log_alignment; 76 unsigned char type_check_kind; 76 unsigned char type_check_kind; 77 }; 77 }; 78 78 79 struct type_mismatch_data_common { 79 struct type_mismatch_data_common { 80 struct source_location *location; 80 struct source_location *location; 81 struct type_descriptor *type; 81 struct type_descriptor *type; 82 unsigned long alignment; 82 unsigned long alignment; 83 unsigned char type_check_kind; 83 unsigned char type_check_kind; 84 }; 84 }; 85 85 86 struct nonnull_arg_data { 86 struct nonnull_arg_data { 87 struct source_location location; 87 struct source_location location; 88 struct source_location attr_location; 88 struct source_location attr_location; 89 int arg_index; 89 int arg_index; 90 }; 90 }; 91 91 92 struct out_of_bounds_data { 92 struct out_of_bounds_data { 93 struct source_location location; 93 struct source_location location; 94 struct type_descriptor *array_type; 94 struct type_descriptor *array_type; 95 struct type_descriptor *index_type; 95 struct type_descriptor *index_type; 96 }; 96 }; 97 97 98 struct shift_out_of_bounds_data { 98 struct shift_out_of_bounds_data { 99 struct source_location location; 99 struct source_location location; 100 struct type_descriptor *lhs_type; 100 struct type_descriptor *lhs_type; 101 struct type_descriptor *rhs_type; 101 struct type_descriptor *rhs_type; 102 }; 102 }; 103 103 104 struct unreachable_data { 104 struct unreachable_data { 105 struct source_location location; 105 struct source_location location; 106 }; 106 }; 107 107 108 struct invalid_value_data { 108 struct invalid_value_data { 109 struct source_location location; 109 struct source_location location; 110 struct type_descriptor *type; 110 struct type_descriptor *type; 111 }; 111 }; 112 112 113 struct alignment_assumption_data { 113 struct alignment_assumption_data { 114 struct source_location location; 114 struct source_location location; 115 struct source_location assumption_loca 115 struct source_location assumption_location; 116 struct type_descriptor *type; 116 struct type_descriptor *type; 117 }; 117 }; 118 118 119 #if defined(CONFIG_ARCH_SUPPORTS_INT128) 119 #if defined(CONFIG_ARCH_SUPPORTS_INT128) 120 typedef __int128 s_max; 120 typedef __int128 s_max; 121 typedef unsigned __int128 u_max; 121 typedef unsigned __int128 u_max; 122 #else 122 #else 123 typedef s64 s_max; 123 typedef s64 s_max; 124 typedef u64 u_max; 124 typedef u64 u_max; 125 #endif 125 #endif 126 126 127 /* 127 /* 128 * When generating Runtime Calls, Clang doesn' 128 * When generating Runtime Calls, Clang doesn't respect the -mregparm=3 129 * option used on i386: https://github.com/llv 129 * option used on i386: https://github.com/llvm/llvm-project/issues/89670 130 * Fix this for earlier Clang versions by forc 130 * Fix this for earlier Clang versions by forcing the calling convention 131 * to use non-register arguments. 131 * to use non-register arguments. 132 */ 132 */ 133 #if defined(CONFIG_X86_32) && \ 133 #if defined(CONFIG_X86_32) && \ 134 defined(CONFIG_CC_IS_CLANG) && CONFIG_CLAN 134 defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION < 190000 135 # define ubsan_linkage asmlinkage 135 # define ubsan_linkage asmlinkage 136 #else 136 #else 137 # define ubsan_linkage 137 # define ubsan_linkage 138 #endif 138 #endif 139 139 140 void ubsan_linkage __ubsan_handle_add_overflow 140 void ubsan_linkage __ubsan_handle_add_overflow(void *data, void *lhs, void *rhs); 141 void ubsan_linkage __ubsan_handle_sub_overflow 141 void ubsan_linkage __ubsan_handle_sub_overflow(void *data, void *lhs, void *rhs); 142 void ubsan_linkage __ubsan_handle_mul_overflow 142 void ubsan_linkage __ubsan_handle_mul_overflow(void *data, void *lhs, void *rhs); 143 void ubsan_linkage __ubsan_handle_negate_overf 143 void ubsan_linkage __ubsan_handle_negate_overflow(void *_data, void *old_val); 144 void ubsan_linkage __ubsan_handle_divrem_overf 144 void ubsan_linkage __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs); 145 void ubsan_linkage __ubsan_handle_type_mismatc 145 void ubsan_linkage __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr); 146 void ubsan_linkage __ubsan_handle_type_mismatc 146 void ubsan_linkage __ubsan_handle_type_mismatch_v1(void *_data, void *ptr); 147 void ubsan_linkage __ubsan_handle_out_of_bound 147 void ubsan_linkage __ubsan_handle_out_of_bounds(void *_data, void *index); 148 void ubsan_linkage __ubsan_handle_shift_out_of 148 void ubsan_linkage __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs); 149 void ubsan_linkage __ubsan_handle_builtin_unre 149 void ubsan_linkage __ubsan_handle_builtin_unreachable(void *_data); 150 void ubsan_linkage __ubsan_handle_load_invalid 150 void ubsan_linkage __ubsan_handle_load_invalid_value(void *_data, void *val); 151 void ubsan_linkage __ubsan_handle_alignment_as 151 void ubsan_linkage __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, 152 152 unsigned long align, 153 153 unsigned long offset); 154 154 155 #endif 155 #endif 156 156
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.