1 // SPDX-License-Identifier: GPL-2.0-only
3 * UBSAN error reporting functions
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
9 #include <linux/bitops.h>
10 #include <linux/bug.h>
11 #include <linux/ctype.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/sched.h>
16 #include <linux/uaccess.h>
20 const char *type_check_kinds
[] = {
23 "reference binding to",
24 "member access within",
26 "constructor call on",
31 #define REPORTED_BIT 31
33 #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
34 #define COLUMN_MASK (~(1U << REPORTED_BIT))
35 #define LINE_MASK (~0U)
37 #define COLUMN_MASK (~0U)
38 #define LINE_MASK (~(1U << REPORTED_BIT))
41 #define VALUE_LENGTH 40
43 static bool was_reported(struct source_location
*location
)
45 return test_and_set_bit(REPORTED_BIT
, &location
->reported
);
48 static void print_source_location(const char *prefix
,
49 struct source_location
*loc
)
51 pr_err("%s %s:%d:%d\n", prefix
, loc
->file_name
,
52 loc
->line
& LINE_MASK
, loc
->column
& COLUMN_MASK
);
55 static bool suppress_report(struct source_location
*loc
)
57 return current
->in_ubsan
|| was_reported(loc
);
60 static bool type_is_int(struct type_descriptor
*type
)
62 return type
->type_kind
== type_kind_int
;
65 static bool type_is_signed(struct type_descriptor
*type
)
67 WARN_ON(!type_is_int(type
));
68 return type
->type_info
& 1;
71 static unsigned type_bit_width(struct type_descriptor
*type
)
73 return 1 << (type
->type_info
>> 1);
76 static bool is_inline_int(struct type_descriptor
*type
)
78 unsigned inline_bits
= sizeof(unsigned long)*8;
79 unsigned bits
= type_bit_width(type
);
81 WARN_ON(!type_is_int(type
));
83 return bits
<= inline_bits
;
86 static s_max
get_signed_val(struct type_descriptor
*type
, void *val
)
88 if (is_inline_int(type
)) {
89 unsigned extra_bits
= sizeof(s_max
)*8 - type_bit_width(type
);
90 unsigned long ulong_val
= (unsigned long)val
;
92 return ((s_max
)ulong_val
) << extra_bits
>> extra_bits
;
95 if (type_bit_width(type
) == 64)
101 static bool val_is_negative(struct type_descriptor
*type
, void *val
)
103 return type_is_signed(type
) && get_signed_val(type
, val
) < 0;
106 static u_max
get_unsigned_val(struct type_descriptor
*type
, void *val
)
108 if (is_inline_int(type
))
109 return (unsigned long)val
;
111 if (type_bit_width(type
) == 64)
114 return *(u_max
*)val
;
117 static void val_to_string(char *str
, size_t size
, struct type_descriptor
*type
,
120 if (type_is_int(type
)) {
121 if (type_bit_width(type
) == 128) {
122 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
123 u_max val
= get_unsigned_val(type
, value
);
125 scnprintf(str
, size
, "0x%08x%08x%08x%08x",
133 } else if (type_is_signed(type
)) {
134 scnprintf(str
, size
, "%lld",
135 (s64
)get_signed_val(type
, value
));
137 scnprintf(str
, size
, "%llu",
138 (u64
)get_unsigned_val(type
, value
));
143 static DEFINE_SPINLOCK(report_lock
);
145 static void ubsan_prologue(struct source_location
*location
,
146 unsigned long *flags
)
149 spin_lock_irqsave(&report_lock
, *flags
);
151 pr_err("========================================"
152 "========================================\n");
153 print_source_location("UBSAN: Undefined behaviour in", location
);
156 static void ubsan_epilogue(unsigned long *flags
)
159 pr_err("========================================"
160 "========================================\n");
161 spin_unlock_irqrestore(&report_lock
, *flags
);
165 static void handle_overflow(struct overflow_data
*data
, void *lhs
,
169 struct type_descriptor
*type
= data
->type
;
171 char lhs_val_str
[VALUE_LENGTH
];
172 char rhs_val_str
[VALUE_LENGTH
];
174 if (suppress_report(&data
->location
))
177 ubsan_prologue(&data
->location
, &flags
);
179 val_to_string(lhs_val_str
, sizeof(lhs_val_str
), type
, lhs
);
180 val_to_string(rhs_val_str
, sizeof(rhs_val_str
), type
, rhs
);
181 pr_err("%s integer overflow:\n",
182 type_is_signed(type
) ? "signed" : "unsigned");
183 pr_err("%s %c %s cannot be represented in type %s\n",
189 ubsan_epilogue(&flags
);
192 void __ubsan_handle_add_overflow(struct overflow_data
*data
,
193 void *lhs
, void *rhs
)
196 handle_overflow(data
, lhs
, rhs
, '+');
198 EXPORT_SYMBOL(__ubsan_handle_add_overflow
);
200 void __ubsan_handle_sub_overflow(struct overflow_data
*data
,
201 void *lhs
, void *rhs
)
203 handle_overflow(data
, lhs
, rhs
, '-');
205 EXPORT_SYMBOL(__ubsan_handle_sub_overflow
);
207 void __ubsan_handle_mul_overflow(struct overflow_data
*data
,
208 void *lhs
, void *rhs
)
210 handle_overflow(data
, lhs
, rhs
, '*');
212 EXPORT_SYMBOL(__ubsan_handle_mul_overflow
);
214 void __ubsan_handle_negate_overflow(struct overflow_data
*data
,
218 char old_val_str
[VALUE_LENGTH
];
220 if (suppress_report(&data
->location
))
223 ubsan_prologue(&data
->location
, &flags
);
225 val_to_string(old_val_str
, sizeof(old_val_str
), data
->type
, old_val
);
227 pr_err("negation of %s cannot be represented in type %s:\n",
228 old_val_str
, data
->type
->type_name
);
230 ubsan_epilogue(&flags
);
232 EXPORT_SYMBOL(__ubsan_handle_negate_overflow
);
235 void __ubsan_handle_divrem_overflow(struct overflow_data
*data
,
236 void *lhs
, void *rhs
)
239 char rhs_val_str
[VALUE_LENGTH
];
241 if (suppress_report(&data
->location
))
244 ubsan_prologue(&data
->location
, &flags
);
246 val_to_string(rhs_val_str
, sizeof(rhs_val_str
), data
->type
, rhs
);
248 if (type_is_signed(data
->type
) && get_signed_val(data
->type
, rhs
) == -1)
249 pr_err("division of %s by -1 cannot be represented in type %s\n",
250 rhs_val_str
, data
->type
->type_name
);
252 pr_err("division by zero\n");
254 ubsan_epilogue(&flags
);
256 EXPORT_SYMBOL(__ubsan_handle_divrem_overflow
);
258 static void handle_null_ptr_deref(struct type_mismatch_data_common
*data
)
262 if (suppress_report(data
->location
))
265 ubsan_prologue(data
->location
, &flags
);
267 pr_err("%s null pointer of type %s\n",
268 type_check_kinds
[data
->type_check_kind
],
269 data
->type
->type_name
);
271 ubsan_epilogue(&flags
);
274 static void handle_misaligned_access(struct type_mismatch_data_common
*data
,
279 if (suppress_report(data
->location
))
282 ubsan_prologue(data
->location
, &flags
);
284 pr_err("%s misaligned address %p for type %s\n",
285 type_check_kinds
[data
->type_check_kind
],
286 (void *)ptr
, data
->type
->type_name
);
287 pr_err("which requires %ld byte alignment\n", data
->alignment
);
289 ubsan_epilogue(&flags
);
292 static void handle_object_size_mismatch(struct type_mismatch_data_common
*data
,
297 if (suppress_report(data
->location
))
300 ubsan_prologue(data
->location
, &flags
);
301 pr_err("%s address %p with insufficient space\n",
302 type_check_kinds
[data
->type_check_kind
],
304 pr_err("for an object of type %s\n", data
->type
->type_name
);
305 ubsan_epilogue(&flags
);
308 static void ubsan_type_mismatch_common(struct type_mismatch_data_common
*data
,
311 unsigned long flags
= user_access_save();
314 handle_null_ptr_deref(data
);
315 else if (data
->alignment
&& !IS_ALIGNED(ptr
, data
->alignment
))
316 handle_misaligned_access(data
, ptr
);
318 handle_object_size_mismatch(data
, ptr
);
320 user_access_restore(flags
);
323 void __ubsan_handle_type_mismatch(struct type_mismatch_data
*data
,
326 struct type_mismatch_data_common common_data
= {
327 .location
= &data
->location
,
329 .alignment
= data
->alignment
,
330 .type_check_kind
= data
->type_check_kind
333 ubsan_type_mismatch_common(&common_data
, (unsigned long)ptr
);
335 EXPORT_SYMBOL(__ubsan_handle_type_mismatch
);
337 void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1
*data
,
341 struct type_mismatch_data_common common_data
= {
342 .location
= &data
->location
,
344 .alignment
= 1UL << data
->log_alignment
,
345 .type_check_kind
= data
->type_check_kind
348 ubsan_type_mismatch_common(&common_data
, (unsigned long)ptr
);
350 EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1
);
352 void __ubsan_handle_out_of_bounds(struct out_of_bounds_data
*data
, void *index
)
355 char index_str
[VALUE_LENGTH
];
357 if (suppress_report(&data
->location
))
360 ubsan_prologue(&data
->location
, &flags
);
362 val_to_string(index_str
, sizeof(index_str
), data
->index_type
, index
);
363 pr_err("index %s is out of range for type %s\n", index_str
,
364 data
->array_type
->type_name
);
365 ubsan_epilogue(&flags
);
367 EXPORT_SYMBOL(__ubsan_handle_out_of_bounds
);
369 void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data
*data
,
370 void *lhs
, void *rhs
)
373 struct type_descriptor
*rhs_type
= data
->rhs_type
;
374 struct type_descriptor
*lhs_type
= data
->lhs_type
;
375 char rhs_str
[VALUE_LENGTH
];
376 char lhs_str
[VALUE_LENGTH
];
378 if (suppress_report(&data
->location
))
381 ubsan_prologue(&data
->location
, &flags
);
383 val_to_string(rhs_str
, sizeof(rhs_str
), rhs_type
, rhs
);
384 val_to_string(lhs_str
, sizeof(lhs_str
), lhs_type
, lhs
);
386 if (val_is_negative(rhs_type
, rhs
))
387 pr_err("shift exponent %s is negative\n", rhs_str
);
389 else if (get_unsigned_val(rhs_type
, rhs
) >=
390 type_bit_width(lhs_type
))
391 pr_err("shift exponent %s is too large for %u-bit type %s\n",
393 type_bit_width(lhs_type
),
394 lhs_type
->type_name
);
395 else if (val_is_negative(lhs_type
, lhs
))
396 pr_err("left shift of negative value %s\n",
399 pr_err("left shift of %s by %s places cannot be"
400 " represented in type %s\n",
402 lhs_type
->type_name
);
404 ubsan_epilogue(&flags
);
406 EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds
);
409 void __ubsan_handle_builtin_unreachable(struct unreachable_data
*data
)
413 ubsan_prologue(&data
->location
, &flags
);
414 pr_err("calling __builtin_unreachable()\n");
415 ubsan_epilogue(&flags
);
416 panic("can't return from __builtin_unreachable()");
418 EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable
);
420 void __ubsan_handle_load_invalid_value(struct invalid_value_data
*data
,
424 char val_str
[VALUE_LENGTH
];
426 if (suppress_report(&data
->location
))
429 ubsan_prologue(&data
->location
, &flags
);
431 val_to_string(val_str
, sizeof(val_str
), data
->type
, val
);
433 pr_err("load of value %s is not a valid value for type %s\n",
434 val_str
, data
->type
->type_name
);
436 ubsan_epilogue(&flags
);
438 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value
);