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", location.file, location.line, location.column, msg);
29}
30
38
39 // Print the error
40 string msg;
41 if (info->alignment != 0 && ubsan_aligned(ptr, info->alignment))
42 msg = "misaligned memory access";
43 else
44 msg = (StringBuilder)TYPE_CHECK_KINDS[info->type_check_kind] << " address 0x" << ptr << " with insufficient space for an object of type " << info->type->name;
45
46 // Print the location
47 handle(info->location, msg.c_str());
48}
49
57
58 // Print the error
59 string msg;
60 if (info->log_alignment != 0 && ubsan_aligned(ptr, (1 << (info->log_alignment))))
61 msg = "misaligned memory access";
62 else
63 msg= StringBuilder() << TYPE_CHECK_KINDS[info->type_check_kind] << " address 0x" << ptr << " with insufficient space for an object of type " << info->type->name;
64
65 // Print the location
66 handle(info->location, msg.c_str());
67}
68
75extern "C" void __ubsan_handle_type_mismatch(type_mismatch_info_t* info, uintptr_t ptr) {
76
78}
79
86extern "C" void __ubsan_handle_type_mismatch_v1(type_mismatch_info_v1_t* info, uintptr_t ptr) {
87
89}
90
97
98 UBSanHandler::handle(info->location, "Pointer overflow");
99
100}
101
108
109 UBSanHandler::handle(info->location, "Subtraction overflow");
110
111}
112
119
120 UBSanHandler::handle(info->location, "Shift out of bounds");
121}
122
129
130 UBSanHandler::handle(info->location, "Array out of bounds");
131
132}
133
140
141 UBSanHandler::handle(info->location, "Addition overflow");
142
143}
144
151
152 UBSanHandler::handle(info->location, "Division overflow");
153
154}
155
162
163 UBSanHandler::handle(info->location, "Negation overflow");
164
165}
166
173
174 UBSanHandler::handle(info->location, "Unreachable code");
175
176}
177
184
185 UBSanHandler::handle(info->location, "Multiplication overflow");
186
187}
188
195
196 UBSanHandler::handle(info->location, "Load of invalid value");
197
198}
199
206
207 UBSanHandler::handle(info->location, "Missing return");
208}
209
216
217 UBSanHandler::handle(info->location, "VLA bound not positive");
218}
Creates a string using a using a combination of parts with the '<<' operator. Simmilar to the logger.
Definition string.h:97
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:56
Defines a Logger class for logging messages with different severity levels to multiple output streams...
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100
Information about an invalid value error.
Definition ubsan.h:145
source_location_t location
The location of the invalid value.
Definition ubsan.h:147
Information that an error that only has a location.
Definition ubsan.h:132
source_location_t location
The location of the error.
Definition ubsan.h:134
Information about an out of bounds error.
Definition ubsan.h:117
source_location_t location
The location of the out of bounds access.
Definition ubsan.h:119
Information about an overflow error.
Definition ubsan.h:88
source_location_t location
The location of the overflow.
Definition ubsan.h:90
Information about a shift out of bounds error.
Definition ubsan.h:102
source_location_t location
The location of the shift out of bounds.
Definition ubsan.h:104
The location in the source code where the undefined behaviour occurred.
Definition ubsan.h:26
uint32_t line
The line number in the source file.
Definition ubsan.h:29
uint32_t column
The column number in the source file.
Definition ubsan.h:30
const char * file
The name of the source file.
Definition ubsan.h:28
char name[]
The name of the type.
Definition ubsan.h:45
Information about a type mismatch error (version 1)
Definition ubsan.h:72
source_location_t location
The location of the type mismatch.
Definition ubsan.h:74
unsigned char log_alignment
The log2 of the required alignment.
Definition ubsan.h:76
unsigned char type_check_kind
The kind of type check that failed.
Definition ubsan.h:77
type_descriptor_t * type
The type that was expected.
Definition ubsan.h:75
Information about a type mismatch error.
Definition ubsan.h:56
source_location_t location
The location of the type mismatch.
Definition ubsan.h:58
uint8_t type_check_kind
The kind of type check that failed.
Definition ubsan.h:61
uintptr_t alignment
The required alignment.
Definition ubsan.h:60
type_descriptor_t * type
The type that was expected.
Definition ubsan.h:59
Information about a VLA bound not positive error.
Definition ubsan.h:159
source_location_t location
The location of the VLA bound not positive.
Definition ubsan.h:161
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:150
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:128
void __ubsan_handle_builtin_unreachable(location_only_info_t *info)
Triggered when reaching code that should be unreachable.
Definition ubsan.cpp:172
void __ubsan_handle_mul_overflow(overflow_info_t *info)
Triggered when multiplying beyond the maximum value for the type.
Definition ubsan.cpp:183
void __ubsan_handle_sub_overflow(overflow_info_t *info)
Triggered when subtracting below the minimum value for the type.
Definition ubsan.cpp:107
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:215
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:86
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:96
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:118
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:194
void __ubsan_handle_add_overflow(overflow_info_t *info)
Triggered when adding beyond the maximum value for the type.
Definition ubsan.cpp:139
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:205
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:75
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:161
Defines structures and a handler for Undefined Behavior Sanitizer (UBSan) runtime errors.
#define ubsan_aligned(value, alignment)
Check if a value is aligned to the given alignment.
Definition ubsan.h:15
const char * TYPE_CHECK_KINDS[]
List of type check errors.
Definition ubsan.h:167