diff --git a/source/arm9/console.c b/source/arm9/console.c index 3deaed8..e1343d8 100644 --- a/source/arm9/console.c +++ b/source/arm9/console.c @@ -232,6 +232,8 @@ chr = *(tmp++); i++; count++; + + if((u32) tmp > 0x8100000) panic(); // ptr poisoned. if ( chr == 0x1b && *tmp == '[' ) { bool escaping = true; @@ -243,6 +245,9 @@ chr = *(tmp++); i++; count++; escapelen++; int parameter, assigned, consumed; + + if((u32) tmp > 0x8100000) panic(); // ptr poisoned. + if((u32) escapeseq > 0x8100000) panic(); // ptr poisoned. // make sure parameters are positive values and delimited by semicolon if((chr >= '0' && chr <= '9') || chr == ';') @@ -374,9 +379,12 @@ parameter = 0; if (escapelen == 1) { consumed = 1; - } else if (strchr(escapeseq,';')) { - sscanf(escapeseq,"%d;%n", ¶meter, &consumed); } else { + if((u32) escapeseq > 0x8100000) panic(); // ptr poisoned. + if (strchr(escapeseq,';')) { + sscanf(escapeseq,"%d;%n", ¶meter, &consumed); + } + else sscanf(escapeseq,"%dm%n", ¶meter, &consumed); } diff --git a/source/arm9/debug.c b/source/arm9/debug.c index e50d211..5a8eb82 100644 --- a/source/arm9/debug.c +++ b/source/arm9/debug.c @@ -4,11 +4,26 @@ #include "mem_map.h" #include "console.h" +static bool exceptionTriggered = false; + +static u32 debugHash = 0; + void NORETURN guruMeditation(u8 type, u32 *excStack, u32 *stackPointer) { const char *typeStr[3] = {"Undefined instruction", "Prefetch abort", "Data abort"}; u32 realPc, instSize = 4; + bool codeChanged = false; + if(exceptionTriggered) // inception :( + while(1); + + exceptionTriggered = true; + + // verify text and rodata + u32 prevHash = debugHash; + hashCodeRoData(); + if(prevHash != debugHash) + codeChanged = true; consoleInit(0, NULL); @@ -44,6 +59,8 @@ stackPointer += 2; } } + + if(codeChanged) printf("Attention: RO section data changed!!"); // avoid fs corruptions unmount_fs(); @@ -54,6 +71,8 @@ void NORETURN panic() { + consoleInit(0, NULL); + printf("\x1b[41m\x1b[0J\x1b[9CGPANIC!!!\n"); unmount_fs(); @@ -62,3 +81,16 @@ while(1); } +void hashCodeRoData() +{ + extern u32 *__text_start; + extern u32 *__exidx_start; + + u32 *start = __text_start; + u32 *end = __exidx_start; + + debugHash = 0; + + for(u32 *ptr = start; ptr < end; ptr++) + debugHash += *ptr; +}