Kernel: Stack trace dumping validates pointers before using them

This commit is contained in:
Bananymous 2023-08-31 21:36:23 +03:00
parent 3ed0a54847
commit 65501837b7
1 changed files with 16 additions and 6 deletions

View File

@ -1,5 +1,6 @@
#include <kernel/Debug.h> #include <kernel/Debug.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
#include <kernel/Memory/PageTable.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
#include <kernel/SpinLock.h> #include <kernel/SpinLock.h>
#include <kernel/Terminal/TTY.h> #include <kernel/Terminal/TTY.h>
@ -10,6 +11,8 @@ namespace Debug
void dump_stack_trace() void dump_stack_trace()
{ {
using namespace Kernel;
struct stackframe struct stackframe
{ {
stackframe* rbp; stackframe* rbp;
@ -24,26 +27,33 @@ namespace Debug
} }
uintptr_t first_rip = frame->rip; uintptr_t first_rip = frame->rip;
uintptr_t last_rip = 0; uintptr_t last_rip = 0;
bool first = true;
BAN::Formatter::print(Debug::putchar, "\e[36mStack trace:\r\n"); BAN::Formatter::print(Debug::putchar, "\e[36mStack trace:\r\n");
while (frame) while (frame)
{ {
BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->rip); if (PageTable::current().is_page_free((vaddr_t)frame & PAGE_ADDR_MASK))
frame = frame->rbp; {
derrorln(" {} not mapped", frame);
break;
}
if (frame && frame->rip == first_rip) BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->rip);
if (!first && frame->rip == first_rip)
{ {
derrorln("looping kernel panic :("); derrorln("looping kernel panic :(");
break; break;
} }
else if (!first && frame->rip == last_rip)
if (frame && frame->rip == last_rip)
{ {
derrorln("repeating stack strace"); derrorln("repeating stack trace");
break; break;
} }
last_rip = frame->rip; last_rip = frame->rip;
frame = frame->rbp;
first = false;
} }
BAN::Formatter::print(Debug::putchar, "\e[m"); BAN::Formatter::print(Debug::putchar, "\e[m");
} }