First public contribution.
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\common\win32\seh.cpp
20 // Fill in the blank types for TWin32SEHTrap
21 #define __WIN32_SEH_TYPES_KNOWN__
22 #define __UnknownWindowsType1 EXCEPTION_RECORD
23 #define __UnknownWindowsType2 CONTEXT
25 // Pretend we're tools to avoid clashes with Win32 headers
27 #define __IN_SEH_CPP__
29 #include <e32cmn_private.h>
34 GLREF_C void Panic(TCdtPanic);
36 // magic value denoting the end of the SEH handler list
37 static const TWin32SEHTrap* const KFencePost = (TWin32SEHTrap*)-1;
40 // Class TWin32SEHTrap
43 #ifdef __KERNEL_MODE__
45 extern DWORD CallFinalSEHHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext)
47 // Get the final SEH entry on the chain
48 TWin32SEHTrap* finalHandler = TWin32SEHTrap::IterateForFinal();
50 // Call the handler - ignoring return value
51 (void)(*finalHandler->ExceptionHandler())(aException, finalHandler, aContext);
53 // Explicitly tell Win32 the exception has been handled
54 return ExceptionContinueExecution;
57 TWin32SEHTrap* TWin32SEHTrap::IterateForFinal()
59 TWin32SEHTrap* p = (TWin32SEHTrap*)Tib()->ExceptionList;
61 // Iterate through the SEH chain to find the final SEH record that we wish to skip to
62 for (; p && p!=KFencePost && p->iPrevExceptionRegistrationRecord!=KFencePost; p=p->iPrevExceptionRegistrationRecord)
67 TWin32SEHExceptionHandler* TWin32SEHTrap::ExceptionHandler()
69 return iExceptionHandler;
72 #else // !__KERNEL_MODE__
76 extern "C" void trap_check(TWin32SEHTrap* a)
78 TWin32SEHTrap* p = a->iPrevExceptionRegistrationRecord;
79 if (p && p!=KFencePost && a->iExceptionHandler == p->iExceptionHandler && a->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler)
80 Exec::PushTrapFrame((TTrap*)p);
82 Exec::PushTrapFrame(0);
85 extern "C" void untrap_check()
87 // search back for consecutive TWin32SEHTrap and remember the second one
88 TWin32SEHTrap* p = (TWin32SEHTrap*)Tib()->ExceptionList;
91 if (p && p!=KFencePost)
95 q = p->iPrevExceptionRegistrationRecord;
96 if (!q || q==KFencePost)
98 if (p->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler && q->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler)
106 Exec::PushTrapFrame((TTrap*)s);
109 // Use assembler to ensure no extra SEH frame is created by the compiler
110 UEXPORT_C __NAKED__ void TWin32SEHTrap::Trap()
121 extern "C" void panic_chain_corrupt()
123 Panic(EWin32SEHChainCorrupt);
126 // Use assembler to ensure no extra SEH frame is created by the compiler
127 UEXPORT_C __NAKED__ void TWin32SEHTrap::UnTrap()
137 _asm call untrap_check
141 _asm jmp panic_chain_corrupt
144 UEXPORT_C TWin32SEHTrap::TWin32SEHTrap()
145 : iPrevExceptionRegistrationRecord(NULL),
146 iExceptionHandler(&ExceptionHandler)
150 // Handler called whilst Win32 is walking the SEH chain
151 DWORD TWin32SEHTrap::ExceptionHandler(EXCEPTION_RECORD* aException, TWin32SEHTrap* /*aRegistrationRecord*/, CONTEXT* aContext)
153 if (aException->ExceptionCode != EXCEPTION_MSCPP)
155 return Emulator::Win32SEHException(aException, aContext);
159 return ExceptionContinueSearch;
163 #if defined(__LEAVE_EQUALS_THROW__) && defined(__WINS__)
164 extern "C" TWin32SEHTrap* pop_trap_frame()
166 return (TWin32SEHTrap*)Exec::PopTrapFrame();
169 extern "C" void leave_end()
175 EXPORT_C __NAKED__ TInt XLeaveException::GetReason() const
178 _asm call pop_trap_frame
180 _asm jz no_nested_trap
182 // eax points to TWin32SEHTrap to be restored
183 // if current exception record is above eax on the stack just restore eax
185 _asm jbe nested_trap_error
188 _asm ja nested_trap_insert_in_middle
189 _asm je no_nested_trap // we haven't been unwound after all
191 // check we eventually reach current exception record from eax
193 _asm mov edx, [eax+4] // &TWin32SEHTrap::ExceptionHandler
197 _asm jz nested_trap_error
198 _asm cmp ecx, 0ffffffffh
199 _asm jz nested_trap_error
201 _asm jz nested_trap_check_ok
202 _asm cmp edx, [ecx+4] // all intervening entries should be TWin32SEHTrap
203 _asm jz nested_trap_check
204 _asm jmp nested_trap_error
206 // other SEH handlers have been added after we were unwound so we need to insert eax 'in the middle'
207 nested_trap_insert_in_middle:
209 _asm je no_nested_trap // eax is still in the chain
210 _asm jb nested_trap_insert_in_middle_found
212 _asm jmp nested_trap_insert_in_middle
214 nested_trap_insert_in_middle_found:
215 _asm mov ecx, [edx] // first SEH above eax on stack
216 _asm cmp ecx, 0ffffffffh
217 _asm je nested_trap_error // reached end of SEH list
220 // ECX should be reachable from EAX
222 nested_trap_insert_in_middle_check:
225 _asm je nested_trap_insert_in_middle_ok
226 _asm cmp edx, 0ffffffffh
227 _asm je nested_trap_error // reached end of SEH list
229 _asm jnz nested_trap_insert_in_middle_check
230 _asm jmp nested_trap_error
232 nested_trap_insert_in_middle_ok:
234 _asm mov [edx], eax // insert eax back into chain
235 _asm jmp no_nested_trap
237 nested_trap_check_ok:
238 _asm mov fs:[0], eax // reinstall nested trap SEH handlers
241 _asm call untrap_check // check for other nested TRAPs
244 _asm mov eax, [ecx]XLeaveException.iR
248 _asm jmp panic_chain_corrupt
250 #endif // defined(__LEAVE_EQUALS_THROW__) && defined(__WINS__)
252 #endif //__KERNEL_MODE__