MOS Source Code
Loading...
Searching...
No Matches
ubsan.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#define pr_fmt(fmt) "UBSAN: " fmt
4
6
7#include <mos/types.hpp>
8
9#undef mos_panic
10
12{
13 const char *file;
16};
17
19{
22 char name[];
23};
24
32
39
44
45static void log_location(source_location *location)
46{
47 pr_emerg(" in file %s:%u, column %u", location->file, location->line, location->column);
48}
49
51{
52 source_location *location = &type_mismatch->location;
53 pr_emerg();
54 if (pointer == 0)
55 pr_emerg("NULL pointer access");
56 else if (type_mismatch->alignment != 0 && is_aligned(pointer, type_mismatch->alignment))
57 pr_emerg("unaligned memory access"); // Most useful on architectures with stricter memory alignment requirements, like ARM.
58 else
59 pr_emerg("insufficient size");
60 pr_emerg("kind=%d, address %p, for object of type %s", type_mismatch->type_check_kind, (void *) pointer, type_mismatch->type->name);
61 log_location(location);
62}
63
65{
66 pr_emerg("pointer overflow, pointer=%p", (void *) pointer);
67 log_location(location);
68}
69
71{
72 __ubsan_handle_type_mismatch(type_mismatch, pointer);
73}
74
76{
77 pr_emerg("division overflow, left=%p, right=%p of type %s", (void *) left, (void *) right, type->name);
78 log_location(location);
79}
80
82{
83 pr_emerg("multiplication overflow, left=%p, right=%p of type %s", (void *) left, (void *) right, type->name);
84 log_location(location);
85}
86
88{
89 pr_emerg("addition overflow, left=%p, right=%p of type %s", (void *) left, (void *) right, type->name);
90 log_location(location);
91}
92
94{
95 pr_emerg("subtraction overflow, left=%p, right=%p of type %s", (void *) left, (void *) right, type->name);
96 log_location(location);
97}
98
100{
101 source_location *location = &out_of_bounds->location;
102 pr_emerg("out of bounds, array type %s, index type %s", out_of_bounds->array_type->name, out_of_bounds->index_type->name);
103 log_location(location);
104}
105
107{
108 pr_emerg("negate overflow, old value %p of type %s", (void *) old_value, type->name);
109 log_location(location);
110}
111
113{
114 __ubsan_handle_negate_overflow(location, type, old_value);
115}
116
118{
119 pr_emerg("load invalid value at %p of value %llu", data, value);
120}
121
123{
124 pr_emerg("shift out of bounds, lhs=(%s) %p, rhs=(%s) %p", lhs_type->name, (void *) lhs, rhs_type->name, (void *) rhs);
125 log_location(location);
126}
127
129{
130 pr_emerg("invalid builtin");
131 log_location(location);
132}
133
135{
136 pr_emerg("VLA bound not positive, bound=%p of type %s", (void *) bound, type->name);
137 log_location(location);
138}
139
141{
142 pr_emerg("builtin unreachable was reached");
143 log_location(&data->location);
144}
#define MOSAPI
Definition mos_global.h:112
#define is_aligned(ptr, alignment)
Definition mos_global.h:56
#define pr_emerg(fmt,...)
Definition printk.hpp:39
source_location location
Definition ubsan.cpp:35
type_descriptor * array_type
Definition ubsan.cpp:36
type_descriptor * index_type
Definition ubsan.cpp:37
const char * file
Definition ubsan.cpp:13
char name[]
Definition ubsan.cpp:22
source_location location
Definition ubsan.cpp:27
type_descriptor * type
Definition ubsan.cpp:28
source_location location
Definition ubsan.cpp:42
unsigned int u32
Definition types.h:17
unsigned short u16
Definition types.h:16
unsigned long ptr_t
Definition types.h:21
unsigned char u8
Definition types.h:15
MOSAPI void __ubsan_handle_sub_overflow(source_location *location, type_descriptor *type, ptr_t left, ptr_t right)
Definition ubsan.cpp:93
MOSAPI void __ubsan_handle_negate_overflow(source_location *location, type_descriptor *type, ptr_t old_value)
Definition ubsan.cpp:106
MOSAPI void __ubsan_handle_negate_overflow_v1(source_location *location, type_descriptor *type, ptr_t old_value)
Definition ubsan.cpp:112
MOSAPI void __ubsan_handle_mul_overflow(source_location *location, type_descriptor *type, ptr_t left, ptr_t right)
Definition ubsan.cpp:81
MOSAPI void __ubsan_handle_builtin_unreachable(unreachable_data *data)
Definition ubsan.cpp:140
MOSAPI void __ubsan_handle_type_mismatch(type_mismatch_info *type_mismatch, ptr_t pointer)
Definition ubsan.cpp:50
MOSAPI void __ubsan_handle_vla_bound_not_positive(source_location *location, type_descriptor *type, ptr_t bound)
Definition ubsan.cpp:134
MOSAPI void __ubsan_handle_invalid_builtin(source_location *location)
Definition ubsan.cpp:128
MOSAPI void __ubsan_handle_shift_out_of_bounds(source_location *location, type_descriptor *lhs_type, ptr_t lhs, type_descriptor *rhs_type, ptr_t rhs)
Definition ubsan.cpp:122
MOSAPI void __ubsan_handle_load_invalid_value(void *data, ptr_t value)
Definition ubsan.cpp:117
MOSAPI void __ubsan_handle_pointer_overflow(source_location *location, ptr_t pointer)
Definition ubsan.cpp:64
MOSAPI void __ubsan_handle_add_overflow(source_location *location, type_descriptor *type, ptr_t left, ptr_t right)
Definition ubsan.cpp:87
MOSAPI void __ubsan_handle_divrem_overflow(source_location *location, type_descriptor *type, ptr_t left, ptr_t right)
Definition ubsan.cpp:75
MOSAPI void __ubsan_handle_type_mismatch_v1(type_mismatch_info *type_mismatch, ptr_t pointer)
Definition ubsan.cpp:70
MOSAPI void __ubsan_handle_out_of_bounds(out_of_bounds_info *out_of_bounds)
Definition ubsan.cpp:99
static void log_location(source_location *location)
Definition ubsan.cpp:45