public static void Throw_NullReferenceException(uint address) { HaltReason = "Null reference exception. Instruction: 0x "; FillString(address, 48, HaltReason); bool BCPOEnabled = BasicConsole.PrimaryOutputEnabled; BasicConsole.PrimaryOutputEnabled = true; BasicConsole.WriteLine(HaltReason); BasicConsole.DelayOutput(10); BasicConsole.PrimaryOutputEnabled = BCPOEnabled; FOS_System.Exception ex = new FOS_System.Exceptions.NullReferenceException(); ex.InstructionAddress = address; Throw(ex); }
public static unsafe void AddExceptionHandlerInfo( void *handlerPtr, void *filterPtr) { if (State == null) { BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! ExceptionMethods.State is null."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); } State->depth++; //if (filterPtr != null) //{ // BasicConsole.WriteLine("Enter try-catch block"); //} //else //{ // BasicConsole.WriteLine("Enter try-finally block"); //} AddExceptionHandlerInfo_EntryStackState *BasePtr = (AddExceptionHandlerInfo_EntryStackState *)BasePointer; uint LocalsSize = (uint)BasePtr - (uint)StackPointer; // Create space for setting up handler info StackPointer -= sizeof(ExceptionHandlerInfo); // Setup handler info ExceptionHandlerInfo *ExHndlrPtr = (ExceptionHandlerInfo *)StackPointer; ExHndlrPtr->EBP = BasePtr->EBP; // EBP + 8 (for ret addr, ebp) + 8 (for args) - sizeof(ExceptionHandlerInfo) ExHndlrPtr->ESP = (uint)BasePtr + 8 + 8 - (uint)sizeof(ExceptionHandlerInfo); ExHndlrPtr->FilterAddress = (byte *)filterPtr; ExHndlrPtr->HandlerAddress = (byte *)handlerPtr; ExHndlrPtr->PrevHandlerPtr = State->CurrentHandlerPtr; ExHndlrPtr->InHandler = 0; ExHndlrPtr->ExPending = 0; ExHndlrPtr->Ex = null; State->CurrentHandlerPtr = (ExceptionHandlerInfo *)((byte *)ExHndlrPtr + (LocalsSize + 12)); StackPointer -= 8; // For duplicate (empty) args StackPointer -= 8; // For duplicate ebp, ret addr // Setup the duplicate stack data // - Nothing to do for args - duplicate values don't matter // - Copy over ebp and return address uint *DuplicateValsStackPointer = (uint *)StackPointer; *DuplicateValsStackPointer = BasePtr->EBP; *(DuplicateValsStackPointer + 1) = BasePtr->RetAddr; ShiftStack((byte *)ExHndlrPtr + sizeof(ExceptionHandlerInfo) - 4, LocalsSize + 12); // Shift stack pointer to correct position - eliminates "empty space" of duplicates StackPointer += 16; // MethodEnd will: // - Add size of locals to esp // - Pop EBP // - Ret to ret address // Caller will: // - Add size of args to esp // Which should leave the stack at the bottom of the (shifted up) ex handler info }
/// <summary> /// Throws a page fault exception. /// </summary> /// <param name="errorCode">The error code associated with the page fault.</param> /// <param name="address">The address which caused the fault.</param> /// <remarks> /// Used by CPU interrupts to handle the creation of the exception object and calling Throw. /// </remarks> public static void Throw_PageFaultException(uint eip, uint errorCode, uint address) { if (ThePageFaultHandler != null) { ThePageFaultHandler(eip, errorCode, address); } else { BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Page fault exception!"); BasicConsole.DelayOutput(10); HaltReason = "Page fault exception. Address: 0x , errorCode: 0x , eip: 0x "; uint y = address; int offset = 40; #region Address while (offset > 32) { uint rem = y & 0xFu; switch (rem) { case 0: HaltReason[offset] = '0'; break; case 1: HaltReason[offset] = '1'; break; case 2: HaltReason[offset] = '2'; break; case 3: HaltReason[offset] = '3'; break; case 4: HaltReason[offset] = '4'; break; case 5: HaltReason[offset] = '5'; break; case 6: HaltReason[offset] = '6'; break; case 7: HaltReason[offset] = '7'; break; case 8: HaltReason[offset] = '8'; break; case 9: HaltReason[offset] = '9'; break; case 10: HaltReason[offset] = 'A'; break; case 11: HaltReason[offset] = 'B'; break; case 12: HaltReason[offset] = 'C'; break; case 13: HaltReason[offset] = 'D'; break; case 14: HaltReason[offset] = 'E'; break; case 15: HaltReason[offset] = 'F'; break; } y >>= 4; offset--; } #endregion y = errorCode; offset = 63; #region Error Code while (offset > 55) { uint rem = y & 0xFu; switch (rem) { case 0: HaltReason[offset] = '0'; break; case 1: HaltReason[offset] = '1'; break; case 2: HaltReason[offset] = '2'; break; case 3: HaltReason[offset] = '3'; break; case 4: HaltReason[offset] = '4'; break; case 5: HaltReason[offset] = '5'; break; case 6: HaltReason[offset] = '6'; break; case 7: HaltReason[offset] = '7'; break; case 8: HaltReason[offset] = '8'; break; case 9: HaltReason[offset] = '9'; break; case 10: HaltReason[offset] = 'A'; break; case 11: HaltReason[offset] = 'B'; break; case 12: HaltReason[offset] = 'C'; break; case 13: HaltReason[offset] = 'D'; break; case 14: HaltReason[offset] = 'E'; break; case 15: HaltReason[offset] = 'F'; break; } y >>= 4; offset--; } #endregion y = eip; offset = 80; #region EIP while (offset > 72) { uint rem = y & 0xFu; switch (rem) { case 0: HaltReason[offset] = '0'; break; case 1: HaltReason[offset] = '1'; break; case 2: HaltReason[offset] = '2'; break; case 3: HaltReason[offset] = '3'; break; case 4: HaltReason[offset] = '4'; break; case 5: HaltReason[offset] = '5'; break; case 6: HaltReason[offset] = '6'; break; case 7: HaltReason[offset] = '7'; break; case 8: HaltReason[offset] = '8'; break; case 9: HaltReason[offset] = '9'; break; case 10: HaltReason[offset] = 'A'; break; case 11: HaltReason[offset] = 'B'; break; case 12: HaltReason[offset] = 'C'; break; case 13: HaltReason[offset] = 'D'; break; case 14: HaltReason[offset] = 'E'; break; case 15: HaltReason[offset] = 'F'; break; } y >>= 4; offset--; } #endregion BasicConsole.WriteLine(HaltReason); BasicConsole.SetTextColour(BasicConsole.default_colour); Throw(new FOS_System.Exceptions.PageFaultException(errorCode, address)); } }
public static unsafe void HandleEndFinally() { if (State == null || State->CurrentHandlerPtr == null) { // If we get to here, it's an unhandled exception if (State == null) { HaltReason = "Cannot end finally in null state!"; } else if (State->CurrentHandlerPtr == null) { HaltReason = "Cannot end finally on null handler!"; } else { HaltReason = "Cannot end finally for unexpected reason!"; } BasicConsole.WriteLine(HaltReason); BasicConsole.DelayOutput(5); // Try to cause fault *((byte *)0x800000000) = 0; } // Leaving a "finally" critical section cleanly // We need to handle 2 cases: // Case 1 : Pending exception // Case 2 : No pending exception //BasicConsole.WriteLine("Handle end finally"); if (State->CurrentHandlerPtr->ExPending != 0) { // Case 1 : Pending exception HandleException(); } else { // Case 2 : No pending exception State->CurrentHandlerPtr->InHandler = 0; uint EBP = State->CurrentHandlerPtr->EBP; uint ESP = State->CurrentHandlerPtr->ESP; byte *retAddr = State->CurrentHandlerPtr->HandlerAddress;//(byte*)*((uint*)(BasePointer + 4)); //BasicConsole.Write("Continue ptr (from HandlerAddress): "); //BasicConsole.WriteLine((uint)State->CurrentHandlerPtr->HandlerAddress); //BasicConsole.Write("Actual continue addr (from EBP): "); //BasicConsole.WriteLine(*((uint*)(BasePointer + 4))); State->CurrentHandlerPtr = State->CurrentHandlerPtr->PrevHandlerPtr; State->depth--; State->history[State->history_pos++] = (uint)State->CurrentHandlerPtr->HandlerAddress; if (State->history_pos > 31) { State->history_pos = 0; } ArbitaryReturn(EBP, ESP + (uint)sizeof(ExceptionHandlerInfo), retAddr); } }
public static unsafe void HandleLeave(void *continuePtr) { if (State == null || State->CurrentHandlerPtr == null) { // If we get to here, it's an unhandled exception HaltReason = ""; if (State == null) { HaltReason = "Cannot leave on null handler! Address: 0x - Null state"; } else if (State->CurrentHandlerPtr == null) { HaltReason = "Cannot leave on null handler! Address: 0x - Null handler"; } else { HaltReason = "Cannot leave on null handler! Address: 0x - Unexpected reason"; } uint y = *((uint *)(BasePointer + 4)); int offset = 48; #region Address while (offset > 40) { uint rem = y & 0xFu; switch (rem) { case 0: HaltReason[offset] = '0'; break; case 1: HaltReason[offset] = '1'; break; case 2: HaltReason[offset] = '2'; break; case 3: HaltReason[offset] = '3'; break; case 4: HaltReason[offset] = '4'; break; case 5: HaltReason[offset] = '5'; break; case 6: HaltReason[offset] = '6'; break; case 7: HaltReason[offset] = '7'; break; case 8: HaltReason[offset] = '8'; break; case 9: HaltReason[offset] = '9'; break; case 10: HaltReason[offset] = 'A'; break; case 11: HaltReason[offset] = 'B'; break; case 12: HaltReason[offset] = 'C'; break; case 13: HaltReason[offset] = 'D'; break; case 14: HaltReason[offset] = 'E'; break; case 15: HaltReason[offset] = 'F'; break; } y >>= 4; offset--; } #endregion BasicConsole.WriteLine(HaltReason); if (State != null) { if (State->depth > 0) { BasicConsole.WriteLine(" -- Positive depth"); } else if (State->depth == 0) { BasicConsole.WriteLine(" -- Zero depth"); } else if (State->depth < 0) { BasicConsole.WriteLine(" -- Negative depth"); } int pos = State->history_pos; do { BasicConsole.Write(State->history[pos]); BasicConsole.Write(" "); pos--; if (pos == -1) { pos = 31; } }while (pos != State->history_pos); } BasicConsole.DelayOutput(5); // Try to cause fault *((byte *)0x800000000) = 0; } // Leaving a critical section cleanly // We need to handle 2 cases: // Case 1 : Leaving "try" or "catch" of a try-catch // Case 2 : Leaving the "try" of a try-finally if ((uint)State->CurrentHandlerPtr->FilterAddress != 0x0u) { // Case 1 : Leaving "try" or "catch" of a try-catch //BasicConsole.WriteLine("Leave try or catch of try-catch"); if (State->CurrentHandlerPtr->Ex != null) { FOS_System.GC.DecrementRefCount((FOS_System.Object)Utilities.ObjectUtilities.GetObject(State->CurrentHandlerPtr->Ex)); State->CurrentHandlerPtr->Ex = null; } State->CurrentHandlerPtr->InHandler = 0; uint EBP = State->CurrentHandlerPtr->EBP; uint ESP = State->CurrentHandlerPtr->ESP; State->CurrentHandlerPtr = State->CurrentHandlerPtr->PrevHandlerPtr; State->depth--; State->history[State->history_pos++] = (uint)State->CurrentHandlerPtr->HandlerAddress; if (State->history_pos > 31) { State->history_pos = 0; } ArbitaryReturn(EBP, ESP + (uint)sizeof(ExceptionHandlerInfo), (byte *)continuePtr); } else { // Case 2 : Leaving the "try" of a try-finally State->CurrentHandlerPtr->InHandler = 1; byte *handlerAddress = State->CurrentHandlerPtr->HandlerAddress; //BasicConsole.WriteLine("Leave try of try-finally"); //BasicConsole.Write("Handler address: "); //BasicConsole.WriteLine((uint)handlerAddress); //BasicConsole.Write("Continue ptr: "); //BasicConsole.WriteLine((uint)continuePtr); State->CurrentHandlerPtr->HandlerAddress = (byte *)continuePtr; ArbitaryReturn(State->CurrentHandlerPtr->EBP, State->CurrentHandlerPtr->ESP, handlerAddress); } }