From 81c4dcadf142b3d428230c6faa194596be8f3017 Mon Sep 17 00:00:00 2001 From: Nicolas Van Bossuyt Date: Sun, 3 Nov 2019 13:17:55 +0100 Subject: [PATCH] Kernel: Prevent kprintf() from asserting in Console::the() (#718) This triggered a stack overflow because ubsan can call kprintf() at any time, even before Console is initialized. --- Kernel/Console.cpp | 5 +++++ Kernel/Console.h | 1 + Kernel/kprintf.cpp | 9 ++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Kernel/Console.cpp b/Kernel/Console.cpp index 91ddfd56cd5..c4054189420 100644 --- a/Kernel/Console.cpp +++ b/Kernel/Console.cpp @@ -13,6 +13,11 @@ Console& Console::the() return *s_the; } +bool Console::is_initialized() +{ + return s_the != nullptr; +} + Console::Console() : CharacterDevice(5, 1) { diff --git a/Kernel/Console.h b/Kernel/Console.h index be43380bc8d..a9c89f5df7b 100644 --- a/Kernel/Console.h +++ b/Kernel/Console.h @@ -14,6 +14,7 @@ class Console final : public CharacterDevice { AK_MAKE_ETERNAL public: static Console& the(); + static bool is_initialized(); Console(); virtual ~Console() override; diff --git a/Kernel/kprintf.cpp b/Kernel/kprintf.cpp index 69dffbcf7e8..333929d1a03 100644 --- a/Kernel/kprintf.cpp +++ b/Kernel/kprintf.cpp @@ -70,7 +70,14 @@ static void console_putch(char*&, char ch) { if (serial_debug) serial_putch(ch); - Console::the().put_char(ch); + + // It would be bad to reach the assert in Console()::the() and do a stack overflow + + if (Console::is_initialized()) { + Console::the().put_char(ch); + } else { + IO::out8(0xe9, ch); + } } int kprintf(const char* fmt, ...)