Revamped kernel panic functions with significant improvements

This commit is contained in:
EnderIce2
2024-02-28 06:31:02 +02:00
parent ddad5ca38a
commit 5fd8d3b3a5
23 changed files with 2057 additions and 3573 deletions

View File

@ -21,9 +21,12 @@
#include "../kernel.h"
/* EXTERNC */ __weak uintptr_t __stack_chk_guard = 0;
extern __noreturn void HandleStackSmashing();
extern __noreturn void HandleBufferOverflow();
EXTERNC __weak __no_stack_protector uintptr_t __stack_chk_guard_init(void)
/* EXTERNC */ uintptr_t __stack_chk_guard = 0;
EXTERNC __no_stack_protector uintptr_t __stack_chk_guard_init(void)
{
int MaxRetries = 0;
#if UINTPTR_MAX == UINT32_MAX
@ -52,55 +55,26 @@ EXTERNC __constructor __no_stack_protector void __guard_setup(void)
debug("Stack guard value: %ld", __stack_chk_guard);
}
EXTERNC __weak __noreturn __no_stack_protector void __stack_chk_fail(void)
EXTERNC __noreturn __no_stack_protector void __stack_chk_fail(void)
{
TaskingPanic();
for (short i = 0; i < 10; i++)
error("Stack smashing detected!");
debug("Current stack check guard value: %#lx", __stack_chk_guard);
KPrint("\eFF0000Stack smashing detected!");
void *Stack = nullptr;
#if defined(a86)
#if defined(a64)
asmv("movq %%rsp, %0"
: "=r"(Stack));
#elif defined(a32)
asmv("movl %%esp, %0"
: "=r"(Stack));
#endif
#elif defined(aa64)
asmv("mov %%sp, %0"
: "=r"(Stack));
#endif
error("Stack address: %#lx", Stack);
if (DebuggerIsAttached)
#ifdef a86
asmv("int $0x3");
#elif defined(aa64)
asmv("brk #0");
#endif
CPU::Stop();
HandleStackSmashing();
}
// https://github.com/gcc-mirror/gcc/blob/master/libssp/ssp.c
EXTERNC __weak __noreturn __no_stack_protector void __chk_fail(void)
EXTERNC __noreturn nsa void __chk_fail(void)
{
TaskingPanic();
for (short i = 0; i < 10; i++)
error("Buffer overflow detected!");
KPrint("\eFF0000Buffer overflow detected!");
#if defined(a86)
while (1)
asmv("cli; hlt");
#elif defined(aa64)
asmv("wfe");
#endif
HandleBufferOverflow();
}