From 0341a6783a64f7e12e5c058b3ba4f7faee58caf2 Mon Sep 17 00:00:00 2001 From: William Durand Date: Tue, 30 Jan 2024 08:47:12 +0100 Subject: [PATCH] libc: further simplify ubsan --- include/libc/ubsan.h | 28 ++----------------- src/libc/ubsan.c | 65 ++++++++------------------------------------ 2 files changed, 14 insertions(+), 79 deletions(-) diff --git a/include/libc/ubsan.h b/include/libc/ubsan.h index 2094b79d..e0ebcd6d 100644 --- a/include/libc/ubsan.h +++ b/include/libc/ubsan.h @@ -7,13 +7,6 @@ #include -typedef struct ubsan_type -{ - uint16_t kind; - uint16_t info; - char name[]; -} ubsan_type_t; - typedef struct ubsan_source_location { const char* file; @@ -21,31 +14,14 @@ typedef struct ubsan_source_location uint32_t column; } ubsan_source_location_t; -typedef struct ubsan_mismatch_data -{ - ubsan_source_location_t location; - ubsan_type_t* type; - uintptr_t alignment; - uint8_t kind; -} ubsan_mismatch_data_t; - -typedef struct ubsan_mismatch_v1_data -{ - ubsan_source_location_t location; - ubsan_type_t* type; - uint8_t log_alignment; - uint8_t kind; -} ubsan_mismatch_v1_data_t; - -void __ubsan_handle_type_mismatch(ubsan_mismatch_data_t* data, uintptr_t ptr); +void __ubsan_handle_type_mismatch(ubsan_source_location_t* location); // This function is suffixed with _v1 because Clang and GCC 8 slightly changed // ABI for 'type mismatch' errors, so compilers now use this function. // // See: // https://patches.linaro.org/project/lkml/patch/20180208154636.21320-1-mark.rutland@arm.com/ -void __ubsan_handle_type_mismatch_v1(ubsan_mismatch_v1_data_t* data, - uintptr_t ptr); +void __ubsan_handle_type_mismatch_v1(ubsan_source_location_t* location); void __ubsan_handle_add_overflow(ubsan_source_location_t* location); diff --git a/src/libc/ubsan.c b/src/libc/ubsan.c index c1fc2f13..a9cc1a20 100644 --- a/src/libc/ubsan.c +++ b/src/libc/ubsan.c @@ -3,6 +3,8 @@ #include #include +// Based on: https://wiki.osdev.org/Undefined_Behavior_Sanitization + #ifdef __is_libk #include @@ -12,57 +14,14 @@ void ubsan_panic_at(ubsan_source_location_t* location, const char* error); -static const char* kinds[] = { - "load", - "store", - "reference binding", - "member access", - "member call", - "constructor call", - "downcast", - "downcast", - "upcast", - "cast to virtual base", -}; - -void __ubsan_handle_type_mismatch(ubsan_mismatch_data_t* data, uintptr_t ptr) -{ - const char* error = "type mismatch (insufficient size)"; - - if (!ptr) { - error = "null pointer access"; - } else if (data->alignment && (ptr & (data->alignment - 1))) { - error = "unaligned access"; - } else { -#ifdef __is_libk - DEBUG("ubsan: kind=%s ptr=%p type=%s", - kinds[data->kind], - (void*)ptr, - data->type->name); - // In non-debug mode, this variable is not used. - UNUSED(kinds); -#else - printf("ubsan: kind=%s ptr=%p type=%s\n", - kinds[data->kind], - (void*)ptr, - data->type->name); -#endif - } - - ubsan_panic_at(&data->location, error); +void __ubsan_handle_type_mismatch(ubsan_source_location_t* location) +{ + ubsan_panic_at(location, "type mismatch"); } -void __ubsan_handle_type_mismatch_v1(ubsan_mismatch_v1_data_t* data, - uintptr_t ptr) +void __ubsan_handle_type_mismatch_v1(ubsan_source_location_t* location) { - ubsan_mismatch_data_t old_data = { - .location = data->location, - .type = data->type, - .alignment = 1UL << data->log_alignment, - .kind = data->kind, - }; - - __ubsan_handle_type_mismatch(&old_data, ptr); + ubsan_panic_at(location, "type mismatch v1"); } void __ubsan_handle_add_overflow(ubsan_source_location_t* location) @@ -82,12 +41,12 @@ void __ubsan_handle_mul_overflow(ubsan_source_location_t* location) void __ubsan_handle_negate_overflow(ubsan_source_location_t* location) { - ubsan_panic_at(location, "negation overflow"); + ubsan_panic_at(location, "negate overflow"); } void __ubsan_handle_divrem_overflow(ubsan_source_location_t* location) { - ubsan_panic_at(location, "negation overflow"); + ubsan_panic_at(location, "divrem overflow"); } void __ubsan_handle_shift_out_of_bounds(ubsan_source_location_t* location) @@ -97,12 +56,12 @@ void __ubsan_handle_shift_out_of_bounds(ubsan_source_location_t* location) void __ubsan_handle_out_of_bounds(ubsan_source_location_t* location) { - ubsan_panic_at(location, "shift out of bounds"); + ubsan_panic_at(location, "out of bounds"); } void __ubsan_handle_load_invalid_value(ubsan_source_location_t* location) { - ubsan_panic_at(location, "invalid value load"); + ubsan_panic_at(location, "load invalid value"); } void __ubsan_handle_float_cast_overflow(ubsan_source_location_t* location) @@ -122,7 +81,7 @@ void __ubsan_handle_vla_bound_not_positive(ubsan_source_location_t* location) void __ubsan_handle_invalid_builtin(ubsan_source_location_t* location) { - ubsan_panic_at(location, "invalid built-in"); + ubsan_panic_at(location, "invalid builtin"); } void __ubsan_handle_function_type_mismatch(ubsan_source_location_t* location)