sl@0: // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\common\win32\seh.cpp sl@0: // sl@0: // sl@0: sl@0: #include "seh.h" sl@0: sl@0: // Fill in the blank types for TWin32SEHTrap sl@0: #define __WIN32_SEH_TYPES_KNOWN__ sl@0: #define __UnknownWindowsType1 EXCEPTION_RECORD sl@0: #define __UnknownWindowsType2 CONTEXT sl@0: sl@0: // Pretend we're tools to avoid clashes with Win32 headers sl@0: #define __TOOLS__ sl@0: #define __IN_SEH_CPP__ sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #include sl@0: GLREF_C void Panic(TCdtPanic); sl@0: sl@0: // magic value denoting the end of the SEH handler list sl@0: static const TWin32SEHTrap* const KFencePost = (TWin32SEHTrap*)-1; sl@0: sl@0: // sl@0: // Class TWin32SEHTrap sl@0: // sl@0: sl@0: #ifdef __KERNEL_MODE__ sl@0: sl@0: extern DWORD CallFinalSEHHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext) sl@0: { sl@0: // Get the final SEH entry on the chain sl@0: TWin32SEHTrap* finalHandler = TWin32SEHTrap::IterateForFinal(); sl@0: sl@0: // Call the handler - ignoring return value sl@0: (void)(*finalHandler->ExceptionHandler())(aException, finalHandler, aContext); sl@0: sl@0: // Explicitly tell Win32 the exception has been handled sl@0: return ExceptionContinueExecution; sl@0: } sl@0: sl@0: TWin32SEHTrap* TWin32SEHTrap::IterateForFinal() sl@0: { sl@0: TWin32SEHTrap* p = (TWin32SEHTrap*)Tib()->ExceptionList; sl@0: sl@0: // Iterate through the SEH chain to find the final SEH record that we wish to skip to sl@0: for (; p && p!=KFencePost && p->iPrevExceptionRegistrationRecord!=KFencePost; p=p->iPrevExceptionRegistrationRecord) sl@0: {} sl@0: return p; sl@0: } sl@0: sl@0: TWin32SEHExceptionHandler* TWin32SEHTrap::ExceptionHandler() sl@0: { sl@0: return iExceptionHandler; sl@0: } sl@0: sl@0: #else // !__KERNEL_MODE__ sl@0: #include sl@0: sl@0: sl@0: extern "C" void trap_check(TWin32SEHTrap* a) sl@0: { sl@0: TWin32SEHTrap* p = a->iPrevExceptionRegistrationRecord; sl@0: if (p && p!=KFencePost && a->iExceptionHandler == p->iExceptionHandler && a->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler) sl@0: Exec::PushTrapFrame((TTrap*)p); sl@0: else sl@0: Exec::PushTrapFrame(0); sl@0: } sl@0: sl@0: extern "C" void untrap_check() sl@0: { sl@0: // search back for consecutive TWin32SEHTrap and remember the second one sl@0: TWin32SEHTrap* p = (TWin32SEHTrap*)Tib()->ExceptionList; sl@0: TWin32SEHTrap* q = 0; sl@0: TWin32SEHTrap* s = 0; sl@0: if (p && p!=KFencePost) sl@0: { sl@0: for(;;) sl@0: { sl@0: q = p->iPrevExceptionRegistrationRecord; sl@0: if (!q || q==KFencePost) sl@0: break; sl@0: if (p->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler && q->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler) sl@0: { sl@0: s = q; sl@0: break; sl@0: } sl@0: p = q; sl@0: } sl@0: } sl@0: Exec::PushTrapFrame((TTrap*)s); sl@0: } sl@0: sl@0: // Use assembler to ensure no extra SEH frame is created by the compiler sl@0: UEXPORT_C __NAKED__ void TWin32SEHTrap::Trap() sl@0: { sl@0: _asm mov eax, fs:[0] sl@0: _asm mov [ecx], eax sl@0: _asm mov fs:[0], ecx sl@0: _asm push ecx sl@0: _asm call trap_check sl@0: _asm pop ecx sl@0: _asm ret sl@0: } sl@0: sl@0: extern "C" void panic_chain_corrupt() sl@0: { sl@0: Panic(EWin32SEHChainCorrupt); sl@0: } sl@0: sl@0: // Use assembler to ensure no extra SEH frame is created by the compiler sl@0: UEXPORT_C __NAKED__ void TWin32SEHTrap::UnTrap() sl@0: { sl@0: _asm mov eax, fs:[0] sl@0: _asm cmp eax, ecx sl@0: _asm ja untrap_0 sl@0: _asm jb untrap_error sl@0: _asm mov eax, [ecx] sl@0: _asm mov fs:[0], eax sl@0: _asm xor eax, eax sl@0: _asm mov [ecx], eax sl@0: _asm call untrap_check sl@0: untrap_0: sl@0: _asm ret sl@0: untrap_error: sl@0: _asm jmp panic_chain_corrupt sl@0: } sl@0: sl@0: UEXPORT_C TWin32SEHTrap::TWin32SEHTrap() sl@0: : iPrevExceptionRegistrationRecord(NULL), sl@0: iExceptionHandler(&ExceptionHandler) sl@0: { sl@0: } sl@0: sl@0: // Handler called whilst Win32 is walking the SEH chain sl@0: DWORD TWin32SEHTrap::ExceptionHandler(EXCEPTION_RECORD* aException, TWin32SEHTrap* /*aRegistrationRecord*/, CONTEXT* aContext) sl@0: { sl@0: if (aException->ExceptionCode != EXCEPTION_MSCPP) sl@0: { sl@0: return Emulator::Win32SEHException(aException, aContext); sl@0: } sl@0: else sl@0: { sl@0: return ExceptionContinueSearch; sl@0: } sl@0: } sl@0: sl@0: #if defined(__LEAVE_EQUALS_THROW__) && defined(__WINS__) sl@0: extern "C" TWin32SEHTrap* pop_trap_frame() sl@0: { sl@0: return (TWin32SEHTrap*)Exec::PopTrapFrame(); sl@0: } sl@0: sl@0: extern "C" void leave_end() sl@0: { sl@0: Exec::LeaveEnd(); sl@0: } sl@0: sl@0: sl@0: EXPORT_C __NAKED__ TInt XLeaveException::GetReason() const sl@0: { sl@0: _asm push ecx sl@0: _asm call pop_trap_frame sl@0: _asm test eax, eax sl@0: _asm jz no_nested_trap sl@0: sl@0: // eax points to TWin32SEHTrap to be restored sl@0: // if current exception record is above eax on the stack just restore eax sl@0: _asm cmp eax, esp sl@0: _asm jbe nested_trap_error sl@0: _asm mov edx, fs:[0] sl@0: _asm cmp eax, edx sl@0: _asm ja nested_trap_insert_in_middle sl@0: _asm je no_nested_trap // we haven't been unwound after all sl@0: sl@0: // check we eventually reach current exception record from eax sl@0: _asm mov ecx, eax sl@0: _asm mov edx, [eax+4] // &TWin32SEHTrap::ExceptionHandler sl@0: nested_trap_check: sl@0: _asm mov ecx, [ecx] sl@0: _asm cmp ecx, 0 sl@0: _asm jz nested_trap_error sl@0: _asm cmp ecx, 0ffffffffh sl@0: _asm jz nested_trap_error sl@0: _asm cmp ecx, fs:[0] sl@0: _asm jz nested_trap_check_ok sl@0: _asm cmp edx, [ecx+4] // all intervening entries should be TWin32SEHTrap sl@0: _asm jz nested_trap_check sl@0: _asm jmp nested_trap_error sl@0: sl@0: // other SEH handlers have been added after we were unwound so we need to insert eax 'in the middle' sl@0: nested_trap_insert_in_middle: sl@0: _asm cmp eax, [edx] sl@0: _asm je no_nested_trap // eax is still in the chain sl@0: _asm jb nested_trap_insert_in_middle_found sl@0: _asm mov edx, [edx] sl@0: _asm jmp nested_trap_insert_in_middle sl@0: sl@0: nested_trap_insert_in_middle_found: sl@0: _asm mov ecx, [edx] // first SEH above eax on stack sl@0: _asm cmp ecx, 0ffffffffh sl@0: _asm je nested_trap_error // reached end of SEH list sl@0: _asm push edx sl@0: sl@0: // ECX should be reachable from EAX sl@0: _asm mov edx, eax sl@0: nested_trap_insert_in_middle_check: sl@0: _asm mov edx, [edx] sl@0: _asm cmp ecx, edx sl@0: _asm je nested_trap_insert_in_middle_ok sl@0: _asm cmp edx, 0ffffffffh sl@0: _asm je nested_trap_error // reached end of SEH list sl@0: _asm test edx, edx sl@0: _asm jnz nested_trap_insert_in_middle_check sl@0: _asm jmp nested_trap_error sl@0: sl@0: nested_trap_insert_in_middle_ok: sl@0: _asm pop edx sl@0: _asm mov [edx], eax // insert eax back into chain sl@0: _asm jmp no_nested_trap sl@0: sl@0: nested_trap_check_ok: sl@0: _asm mov fs:[0], eax // reinstall nested trap SEH handlers sl@0: sl@0: no_nested_trap: sl@0: _asm call untrap_check // check for other nested TRAPs sl@0: _asm call leave_end sl@0: _asm pop ecx sl@0: _asm mov eax, [ecx]XLeaveException.iR sl@0: _asm ret sl@0: sl@0: nested_trap_error: sl@0: _asm jmp panic_chain_corrupt sl@0: } sl@0: #endif // defined(__LEAVE_EQUALS_THROW__) && defined(__WINS__) sl@0: sl@0: #endif //__KERNEL_MODE__