Max OS 0.3
Loading...
Searching...
No Matches
ubsan.cpp
Go to the documentation of this file.
1
9#include <runtime/ubsan.h>
10#include <common/logger.h>
11
12using namespace MaxOS;
13using namespace MaxOS::runtime;
14
15
16UBSanHandler::UBSanHandler() = default;
17
18UBSanHandler::~UBSanHandler() = default;
19
25void UBSanHandler::handle(source_location_t location, const char* msg) {
26
27 // Print the location
28 ASSERT(false, "UBSAN ERROR AT %s:%d:%d - %s \n", location.file, location.line, location.column, msg);
29}
30
38
39 // Print the error
40 string msg;
41 if (ptr == 0)
42 msg = "null pointer access";
43 else if (info->alignment != 0 && UBSAN_ALIGNED(ptr, info->alignment))
44 msg = StringBuilder() << "misaligned address 0x" << ptr;
45 else
46 msg = StringBuilder() << TYPE_CHECK_KINDS[info->type_check_kind] << " address 0x" << ptr << " with insufficient space for an object of type " << info->type->name;
47
48 // Print the location
49 handle(info->location, msg.c_str());
50}
51
59
60 // Print the error
61 string msg;
62 if (ptr == 0)
63 msg = "null pointer access";
64 else if (info->log_alignment != 0 && UBSAN_ALIGNED(ptr, (1 << (info->log_alignment))))
65 msg = StringBuilder() << "misaligned address 0x" << ptr;
66 else
67 msg = StringBuilder() << TYPE_CHECK_KINDS[info->type_check_kind] << " address 0x" << ptr << " with insufficient space for an object of type " << info->type->name;
68
69 // Print the location
70 handle(info->location, msg.c_str());
71}
72
79extern "C" void __ubsan_handle_type_mismatch(type_mismatch_info_t* info, uintptr_t ptr) {
80
82}
83
90extern "C" void __ubsan_handle_type_mismatch_v1(type_mismatch_info_v1_t* info, uintptr_t ptr) {
91
93}
94
101
102 UBSanHandler::handle(info->location, "Pointer overflow");
103
104}
105
112
113 UBSanHandler::handle(info->location, "Subtraction overflow");
114
115}
116
123
124 UBSanHandler::handle(info->location, "Shift out of bounds");
125}
126
133
134 UBSanHandler::handle(info->location, "Array out of bounds");
135
136}
137
144
145 UBSanHandler::handle(info->location, "Addition overflow");
146
147}
148
155
156 UBSanHandler::handle(info->location, "Division overflow");
157
158}
159
166
167 UBSanHandler::handle(info->location, "Negation overflow");
168
169}
170
177
178 UBSanHandler::handle(info->location, "Unreachable code");
179
180}
181
188
189 UBSanHandler::handle(info->location, "Multiplication overflow");
190
191}
192
199
200 UBSanHandler::handle(info->location, "Load of invalid value");
201
202}
203
210
211 UBSanHandler::handle(info->location, "Missing return");
212}
213
220
221 UBSanHandler::handle(info->location, "VLA bound not positive");
222}
static void handle(source_location_t location, const char *msg)
Handles the UBSan error (currently only printing the location and panicking)
Definition ubsan.cpp:25
static void print_type_mismatch(type_mismatch_info_t *info, uintptr_t ptr)
Prints the type mismatch error for UBSan.
Definition ubsan.cpp:37
static void print_type_mismatch_v1(type_mismatch_info_v1_t *info, uintptr_t ptr)
Prints the type mismatch error for UBSan v1.
Definition ubsan.cpp:58
Defines a Logger class for logging messages with different severity levels to multiple output streams...
Information about an invalid value error.
Definition ubsan.h:147
source_location_t location
The location of the invalid value.
Definition ubsan.h:149
Information that an error that only has a location.
Definition ubsan.h:134
source_location_t location
The location of the error.
Definition ubsan.h:136
Information about an out of bounds error.
Definition ubsan.h:119
source_location_t location
The location of the out of bounds access.
Definition ubsan.h:121
Information about an overflow error.
Definition ubsan.h:90
source_location_t location
The location of the overflow.
Definition ubsan.h:92
Information about a shift out of bounds error.
Definition ubsan.h:104
source_location_t location
The location of the shift out of bounds.
Definition ubsan.h:106
The location in the source code where the undefined behaviour occurred.
Definition ubsan.h:28
uint32_t line
The line number in the source file.
Definition ubsan.h:31
uint32_t column
The column number in the source file.
Definition ubsan.h:32
const char * file
The name of the source file.
Definition ubsan.h:30
char name[]
The name of the type.
Definition ubsan.h:47
Information about a type mismatch error (version 1)
Definition ubsan.h:74
source_location_t location
The location of the type mismatch.
Definition ubsan.h:76
unsigned char log_alignment
The log2 of the required alignment.
Definition ubsan.h:78
unsigned char type_check_kind
The kind of type check that failed.
Definition ubsan.h:79
type_descriptor_t * type
The type that was expected.
Definition ubsan.h:77
Information about a type mismatch error.
Definition ubsan.h:58
source_location_t location
The location of the type mismatch.
Definition ubsan.h:60
uint8_t type_check_kind
The kind of type check that failed.
Definition ubsan.h:63
uintptr_t alignment
The required alignment.
Definition ubsan.h:62
type_descriptor_t * type
The type that was expected.
Definition ubsan.h:61
Information about a VLA bound not positive error.
Definition ubsan.h:161
source_location_t location
The location of the VLA bound not positive.
Definition ubsan.h:163
void __ubsan_handle_divrem_overflow(overflow_info_t *info)
Triggered when dividing or taking the remainder results in an overflow (eg dividing INT_MIN by -1)
Definition ubsan.cpp:154
void __ubsan_handle_out_of_bounds(out_of_bounds_info_t *info)
Triggered when accessing an array at an index larger than its size (or negative)
Definition ubsan.cpp:132
void __ubsan_handle_builtin_unreachable(location_only_info_t *info)
Triggered when reaching code that should be unreachable.
Definition ubsan.cpp:176
void __ubsan_handle_mul_overflow(overflow_info_t *info)
Triggered when multiplying beyond the maximum value for the type.
Definition ubsan.cpp:187
void __ubsan_handle_sub_overflow(overflow_info_t *info)
Triggered when subtracting below the minimum value for the type.
Definition ubsan.cpp:111
void __ubsan_handle_vla_bound_not_positive(vla_bound_not_positive_info_t *info)
Triggered when a variable length array (VLA) is declared with a non-positive bound.
Definition ubsan.cpp:219
void __ubsan_handle_type_mismatch_v1(type_mismatch_info_v1_t *info, uintptr_t ptr)
Triggered when memory is accessed with an unexpected type (UBSan v1 - includes more metadata)
Definition ubsan.cpp:90
void __ubsan_handle_pointer_overflow(overflow_info_t *info)
Triggered when adding/subbing to a memory address is outside the expected bounds.
Definition ubsan.cpp:100
void __ubsan_handle_shift_out_of_bounds(shift_out_of_bounds_info_t *info)
Triggered when shifting beyond the capacity of the type.
Definition ubsan.cpp:122
void __ubsan_handle_load_invalid_value(invalid_value_info_t *info)
Triggered when loading a value that is invalid for its type (eg corrupted or uninitialised)
Definition ubsan.cpp:198
void __ubsan_handle_add_overflow(overflow_info_t *info)
Triggered when adding beyond the maximum value for the type.
Definition ubsan.cpp:143
void __ubsan_handle_missing_return(location_only_info_t *info)
Triggered when a function that is expected to return a value does not.
Definition ubsan.cpp:209
void __ubsan_handle_type_mismatch(type_mismatch_info_t *info, uintptr_t ptr)
Triggered when memory is accessed with an unexpected type.
Definition ubsan.cpp:79
void __ubsan_handle_negate_overflow(overflow_info_t *info)
Triggered when negating (taking -x) when there is no corresponding positive/negative value.
Definition ubsan.cpp:165
Defines structures and a handler for Undefined Behavior Sanitizer (UBSan) runtime errors.
const char * TYPE_CHECK_KINDS[]
List of type check errors.
Definition ubsan.h:169
#define UBSAN_ALIGNED(value, alignment)
Check if a value is aligned to the given alignment.
Definition ubsan.h:17