blob: 0f2e8945bc746511ecaa6d63e46d985cb3f7d8ca [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * UBSAN error reporting functions
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/bitops.h>
14#include <linux/bug.h>
15#include <linux/ctype.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/sched.h>
20
21#include "ubsan.h"
22
23#include "../kernel/sched/sched.h"
24#include "../drivers/misc/mediatek/include/mt-plat/aee.h"
25
26const char *type_check_kinds[] = {
27 "load of",
28 "store to",
29 "reference binding to",
30 "member access within",
31 "member call on",
32 "constructor call on",
33 "downcast of",
34 "downcast of"
35};
36
37#define REPORTED_BIT 31
38
39#if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
40#define COLUMN_MASK (~(1U << REPORTED_BIT))
41#define LINE_MASK (~0U)
42#else
43#define COLUMN_MASK (~0U)
44#define LINE_MASK (~(1U << REPORTED_BIT))
45#endif
46
47#define VALUE_LENGTH 40
48
49static bool was_reported(struct source_location *location)
50{
51 return test_and_set_bit(REPORTED_BIT, &location->reported);
52}
53
54static void print_source_location(const char *prefix,
55 struct source_location *loc)
56{
57 pr_err("%s %s:%d:%d\n", prefix, loc->file_name,
58 loc->line & LINE_MASK, loc->column & COLUMN_MASK);
59}
60
61static bool suppress_report(struct source_location *loc)
62{
63 return current->in_ubsan || was_reported(loc);
64}
65
66static bool type_is_int(struct type_descriptor *type)
67{
68 return type->type_kind == type_kind_int;
69}
70
71static bool type_is_signed(struct type_descriptor *type)
72{
73 WARN_ON(!type_is_int(type));
74 return type->type_info & 1;
75}
76
77static unsigned type_bit_width(struct type_descriptor *type)
78{
79 return 1 << (type->type_info >> 1);
80}
81
82static bool is_inline_int(struct type_descriptor *type)
83{
84 unsigned inline_bits = sizeof(unsigned long)*8;
85 unsigned bits = type_bit_width(type);
86
87 WARN_ON(!type_is_int(type));
88
89 return bits <= inline_bits;
90}
91
92static s_max get_signed_val(struct type_descriptor *type, void *val)
93{
94 if (is_inline_int(type)) {
95 unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
96 unsigned long ulong_val = (unsigned long)val;
97
98 return ((s_max)ulong_val) << extra_bits >> extra_bits;
99 }
100
101 if (type_bit_width(type) == 64)
102 return *(s64 *)val;
103
104 return *(s_max *)val;
105}
106
107static bool val_is_negative(struct type_descriptor *type, void *val)
108{
109 return type_is_signed(type) && get_signed_val(type, val) < 0;
110}
111
112static u_max get_unsigned_val(struct type_descriptor *type, void *val)
113{
114 if (is_inline_int(type))
115 return (unsigned long)val;
116
117 if (type_bit_width(type) == 64)
118 return *(u64 *)val;
119
120 return *(u_max *)val;
121}
122
123static void val_to_string(char *str, size_t size, struct type_descriptor *type,
124 void *value)
125{
126 if (type_is_int(type)) {
127 if (type_bit_width(type) == 128) {
128#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
129 u_max val = get_unsigned_val(type, value);
130
131 scnprintf(str, size, "0x%08x%08x%08x%08x",
132 (u32)(val >> 96),
133 (u32)(val >> 64),
134 (u32)(val >> 32),
135 (u32)(val));
136#else
137 WARN_ON(1);
138#endif
139 } else if (type_is_signed(type)) {
140 scnprintf(str, size, "%lld",
141 (s64)get_signed_val(type, value));
142 } else {
143 scnprintf(str, size, "%llu",
144 (u64)get_unsigned_val(type, value));
145 }
146 }
147}
148
149static DEFINE_SPINLOCK(report_lock);
150
151static void ubsan_prologue(struct source_location *location,
152 unsigned long *flags)
153{
154 current->in_ubsan++;
155 spin_lock_irqsave(&report_lock, *flags);
156
157 pr_err("========================================"
158 "========================================\n");
159 print_source_location("UBSAN: Undefined behaviour in", location);
160}
161
162static void ubsan_epilogue(unsigned long *flags)
163{
164 int cpu;
165 struct rq *rq;
166
167 dump_stack();
168 pr_err("========================================"
169 "========================================\n");
170 spin_unlock_irqrestore(&report_lock, *flags);
171 current->in_ubsan--;
172
173 cpu = raw_smp_processor_id();
174 rq = cpu_rq(cpu);
175 if (!raw_spin_is_locked(&rq->lock)) {
176 /* AEE Kernel API Dump for UBSan */
177 aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DEFAULT,
178 "UBSan error",
179 "[UBSan report]");
180 } else {
181 BUG();
182 }
183
184}
185
186static void handle_overflow(struct overflow_data *data, void *lhs,
187 void *rhs, char op)
188{
189
190 struct type_descriptor *type = data->type;
191 unsigned long flags;
192 char lhs_val_str[VALUE_LENGTH];
193 char rhs_val_str[VALUE_LENGTH];
194
195 if (suppress_report(&data->location))
196 return;
197
198 ubsan_prologue(&data->location, &flags);
199
200 val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
201 val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
202 pr_err("%s integer overflow:\n",
203 type_is_signed(type) ? "signed" : "unsigned");
204 pr_err("%s %c %s cannot be represented in type %s\n",
205 lhs_val_str,
206 op,
207 rhs_val_str,
208 type->type_name);
209
210 ubsan_epilogue(&flags);
211}
212
213void __ubsan_handle_add_overflow(struct overflow_data *data,
214 void *lhs, void *rhs)
215{
216
217 handle_overflow(data, lhs, rhs, '+');
218}
219EXPORT_SYMBOL(__ubsan_handle_add_overflow);
220
221void __ubsan_handle_sub_overflow(struct overflow_data *data,
222 void *lhs, void *rhs)
223{
224 handle_overflow(data, lhs, rhs, '-');
225}
226EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
227
228void __ubsan_handle_mul_overflow(struct overflow_data *data,
229 void *lhs, void *rhs)
230{
231 handle_overflow(data, lhs, rhs, '*');
232}
233EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
234
235void __ubsan_handle_negate_overflow(struct overflow_data *data,
236 void *old_val)
237{
238 unsigned long flags;
239 char old_val_str[VALUE_LENGTH];
240
241 if (suppress_report(&data->location))
242 return;
243
244 ubsan_prologue(&data->location, &flags);
245
246 val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
247
248 pr_err("negation of %s cannot be represented in type %s:\n",
249 old_val_str, data->type->type_name);
250
251 ubsan_epilogue(&flags);
252}
253EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
254
255
256void __ubsan_handle_divrem_overflow(struct overflow_data *data,
257 void *lhs, void *rhs)
258{
259 unsigned long flags;
260 char rhs_val_str[VALUE_LENGTH];
261
262 if (suppress_report(&data->location))
263 return;
264
265 ubsan_prologue(&data->location, &flags);
266
267 val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
268
269 if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
270 pr_err("division of %s by -1 cannot be represented in type %s\n",
271 rhs_val_str, data->type->type_name);
272 else
273 pr_err("division by zero\n");
274
275 ubsan_epilogue(&flags);
276}
277EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
278
279static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
280{
281 unsigned long flags;
282
283 if (suppress_report(data->location))
284 return;
285
286 ubsan_prologue(data->location, &flags);
287
288 pr_err("%s null pointer of type %s\n",
289 type_check_kinds[data->type_check_kind],
290 data->type->type_name);
291
292 ubsan_epilogue(&flags);
293}
294
295static void handle_misaligned_access(struct type_mismatch_data_common *data,
296 unsigned long ptr)
297{
298 unsigned long flags;
299
300 if (suppress_report(data->location))
301 return;
302
303 ubsan_prologue(data->location, &flags);
304
305 pr_err("%s misaligned address %p for type %s\n",
306 type_check_kinds[data->type_check_kind],
307 (void *)ptr, data->type->type_name);
308 pr_err("which requires %ld byte alignment\n", data->alignment);
309
310 ubsan_epilogue(&flags);
311}
312
313static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
314 unsigned long ptr)
315{
316 unsigned long flags;
317
318 if (suppress_report(data->location))
319 return;
320
321 ubsan_prologue(data->location, &flags);
322 pr_err("%s address %p with insufficient space\n",
323 type_check_kinds[data->type_check_kind],
324 (void *) ptr);
325 pr_err("for an object of type %s\n", data->type->type_name);
326 ubsan_epilogue(&flags);
327}
328
329static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
330 unsigned long ptr)
331{
332
333 if (!ptr)
334 handle_null_ptr_deref(data);
335 else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
336 handle_misaligned_access(data, ptr);
337 else
338 handle_object_size_mismatch(data, ptr);
339}
340
341void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
342 void *ptr)
343{
344 struct type_mismatch_data_common common_data = {
345 .location = &data->location,
346 .type = data->type,
347 .alignment = data->alignment,
348 .type_check_kind = data->type_check_kind
349 };
350
351 ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
352}
353EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
354
355void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
356 void *ptr)
357{
358
359 struct type_mismatch_data_common common_data = {
360 .location = &data->location,
361 .type = data->type,
362 .alignment = 1UL << data->log_alignment,
363 .type_check_kind = data->type_check_kind
364 };
365
366 ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
367}
368EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
369
370void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
371 void *bound)
372{
373 unsigned long flags;
374 char bound_str[VALUE_LENGTH];
375
376 if (suppress_report(&data->location))
377 return;
378
379 ubsan_prologue(&data->location, &flags);
380
381 val_to_string(bound_str, sizeof(bound_str), data->type, bound);
382 pr_err("variable length array bound value %s <= 0\n", bound_str);
383
384 ubsan_epilogue(&flags);
385}
386EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
387
388void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data, void *index)
389{
390 unsigned long flags;
391 char index_str[VALUE_LENGTH];
392
393 if (suppress_report(&data->location))
394 return;
395
396 ubsan_prologue(&data->location, &flags);
397
398 val_to_string(index_str, sizeof(index_str), data->index_type, index);
399 pr_err("index %s is out of range for type %s\n", index_str,
400 data->array_type->type_name);
401 ubsan_epilogue(&flags);
402}
403EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
404
405void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
406 void *lhs, void *rhs)
407{
408 unsigned long flags;
409 struct type_descriptor *rhs_type = data->rhs_type;
410 struct type_descriptor *lhs_type = data->lhs_type;
411 char rhs_str[VALUE_LENGTH];
412 char lhs_str[VALUE_LENGTH];
413
414 if (suppress_report(&data->location))
415 return;
416
417 ubsan_prologue(&data->location, &flags);
418
419 val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
420 val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
421
422 if (val_is_negative(rhs_type, rhs))
423 pr_err("shift exponent %s is negative\n", rhs_str);
424
425 else if (get_unsigned_val(rhs_type, rhs) >=
426 type_bit_width(lhs_type))
427 pr_err("shift exponent %s is too large for %u-bit type %s\n",
428 rhs_str,
429 type_bit_width(lhs_type),
430 lhs_type->type_name);
431 else if (val_is_negative(lhs_type, lhs))
432 pr_err("left shift of negative value %s\n",
433 lhs_str);
434 else
435 pr_err("left shift of %s by %s places cannot be"
436 " represented in type %s\n",
437 lhs_str, rhs_str,
438 lhs_type->type_name);
439
440 ubsan_epilogue(&flags);
441}
442EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
443
444
445void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
446{
447 unsigned long flags;
448
449 ubsan_prologue(&data->location, &flags);
450 pr_err("calling __builtin_unreachable()\n");
451 ubsan_epilogue(&flags);
452 panic("can't return from __builtin_unreachable()");
453}
454EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
455
456void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
457 void *val)
458{
459 unsigned long flags;
460 char val_str[VALUE_LENGTH];
461
462 if (suppress_report(&data->location))
463 return;
464
465 ubsan_prologue(&data->location, &flags);
466
467 val_to_string(val_str, sizeof(val_str), data->type, val);
468
469 pr_err("load of value %s is not a valid value for type %s\n",
470 val_str, data->type->type_name);
471
472 ubsan_epilogue(&flags);
473}
474EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);