From 2129bdb22bab7dc5a9d23a31c23f38e847511a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:17:44 +0000 Subject: =?UTF-8?q?Revert=20assert=20with=20printf=20functionality,=20requ?= =?UTF-8?q?ires=20malloc=20=F0=9F=98=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arch/x86_64/CMakeLists.txt | 1 + .../include/arch/exception_handling/assert.hpp | 44 ++-------------------- arch/x86_64/src/exception_handling/abort.cpp | 1 - arch/x86_64/src/exception_handling/assert.cpp | 13 +++++++ arch/x86_64/src/memory/multiboot/reader.cpp | 15 +++----- 5 files changed, 22 insertions(+), 52 deletions(-) create mode 100644 arch/x86_64/src/exception_handling/assert.cpp diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 645660a..8658ed7 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -57,6 +57,7 @@ target_sources("_memory" PRIVATE #]============================================================================] target_sources("_exception" PRIVATE + "src/exception_handling/assert.cpp" "src/exception_handling/abort.cpp" "src/exception_handling/panic.cpp" ) diff --git a/arch/x86_64/include/arch/exception_handling/assert.hpp b/arch/x86_64/include/arch/exception_handling/assert.hpp index 152e653..bfc205c 100644 --- a/arch/x86_64/include/arch/exception_handling/assert.hpp +++ b/arch/x86_64/include/arch/exception_handling/assert.hpp @@ -3,54 +3,16 @@ #include "arch/exception_handling/panic.hpp" -#include - namespace teachos::arch::exception_handling { - namespace - { - char constexpr FAILED_MESSAGE[] = "Invalid arguments passed to format specifiers (%) in exception_handling::assert"; - } - /** * @brief Assert a condition to be true, if not do not continue * execution of the code and print the formatted message to screen. * - * @param condition Condition we want to be true. - * @param format Formatting message that the given arguments will be inserted into. - * @param ...args Arguments that will be formatted and inserted into the resulting string, replacing their respective - * specifiers. Uses the printf specifiers see https://cplusplus.com/reference/cstdio/printf/ for more information. + * @param condition Condition we want to be true or else halt execution. + * @param message Message that should be printed before halting the execution if the condition is not met. */ - template - auto assert(bool condition, char const * format, Args const &... args) -> void - { - if (condition) - { - return; - } - else if constexpr (sizeof...(args) == 0) - { - panic("Assertion Violation: ", format); - } - - // Result is what would have been written if the passed buffer would have been large enough not counting null - // character, or if an error occured while creating the string a negative number is returned instead. To ensure this - // will not crash the system when creating an array with negative size we assert beforehand with a clear error - // message. - int const size = snprintf(nullptr, 0U, format, args...) + 1U; - if (size < 0) - { - panic("Assertion Violation: ", FAILED_MESSAGE); - } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wvla" - char arguments[size] = {}; -#pragma GCC diagnostic pop - int const written_characters = snprintf(arguments, size, format, args...); - // Written characters is expected to be one less, because of the null termination character. - bool const result = (written_characters == (size - 1)); - panic("Assertion Violation: ", result ? arguments : FAILED_MESSAGE); - } + auto assert(bool condition, char const * message) -> void; } // namespace teachos::arch::exception_handling #endif diff --git a/arch/x86_64/src/exception_handling/abort.cpp b/arch/x86_64/src/exception_handling/abort.cpp index 77576b1..e331d34 100644 --- a/arch/x86_64/src/exception_handling/abort.cpp +++ b/arch/x86_64/src/exception_handling/abort.cpp @@ -12,5 +12,4 @@ namespace teachos::arch::exception_handling * currently implement, @p ::abort gets overridden to simply panic. */ extern "C" auto abort() -> void { panic("Terminate was called, possibly due to an unhandled exception"); } - } // namespace teachos::arch::exception_handling diff --git a/arch/x86_64/src/exception_handling/assert.cpp b/arch/x86_64/src/exception_handling/assert.cpp new file mode 100644 index 0000000..b36f52d --- /dev/null +++ b/arch/x86_64/src/exception_handling/assert.cpp @@ -0,0 +1,13 @@ +#include "arch/exception_handling/assert.hpp" + +namespace teachos::arch::exception_handling +{ + auto assert(bool condition, char const * message) -> void + { + if (condition) + { + return; + } + panic("Assertion Violation: ", message); + } +} // namespace teachos::arch::exception_handling diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 4e78a06..545e517 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -20,8 +20,7 @@ namespace teachos::arch::memory::multiboot auto expected_entry_size = mminfo->entry_size; constexpr auto actual_entry_size = sizeof(memory_area); exception_handling::assert(expected_entry_size == actual_entry_size, - "[Multiboot Reader] Expected memory area entry size (%u) but got (%u)", - expected_entry_size, actual_entry_size); + "[Multiboot Reader] Unexpected memory area entry size"); auto total_size = mminfo->info.size; auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; @@ -37,16 +36,14 @@ namespace teachos::arch::memory::multiboot auto expected_entry_size = symbol->entry_size; constexpr auto actual_entry_size = sizeof(elf_section_header); exception_handling::assert(expected_entry_size == actual_entry_size, - "[Multiboot Reader] Expected elf section header entry size (%u) but got (%u)", - expected_entry_size, actual_entry_size); + "[Multiboot Reader] Unexpected elf section header entry size"); auto expected_total_size = symbol->info.size; auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); auto actual_total_size = actual_total_entry_size + actual_total_section_size; exception_handling::assert(expected_total_size == actual_total_size, - "[Multiboot Reader] Expected elf symbols section header total size (%u) but got (%u)", - expected_total_size, actual_total_size); + "[Multiboot Reader] Unexpected elf symbols section header total size"); auto begin = reinterpret_cast(&symbol->end); auto end = begin + symbol->number_of_sections; @@ -87,12 +84,10 @@ namespace teachos::arch::memory::multiboot exception_handling::assert( symbol_table_section_count == 1U, - "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got (%u)", - symbol_table_section_count); + "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got more"); exception_handling::assert( dynamic_section_count <= 1U, - "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got (%u)", - dynamic_section_count); + "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); } } // namespace -- cgit v1.2.3