2 * msvcrt.dll exception handling
4 * Copyright 2000 Jon Griffiths
6 * See http://www.microsoft.com/msj/0197/exception/exception.htm,
7 * but don't believe all of it.
9 * FIXME: Incomplete support for nested exceptions/try block cleanup.
14 #include "wine/exception.h"
18 #include "msvcrt/setjmp.h"
21 DEFAULT_DEBUG_CHANNEL(msvcrt);
23 typedef void (*MSVCRT_sig_handler_func)(void);
25 /* VC++ extensions to Win32 SEH */
26 typedef struct _SCOPETABLE
28 DWORD previousTryLevel;
29 int (*lpfnFilter)(PEXCEPTION_POINTERS);
30 int (*lpfnHandler)(void);
31 } SCOPETABLE, *PSCOPETABLE;
33 typedef struct _MSVCRT_EXCEPTION_FRAME
35 EXCEPTION_FRAME *prev;
36 void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_FRAME,
37 PCONTEXT, PEXCEPTION_RECORD);
38 PSCOPETABLE scopetable;
41 PEXCEPTION_POINTERS xpointers;
42 } MSVCRT_EXCEPTION_FRAME;
44 #define TRYLEVEL_END 0xffffffff /* End of trylevel list */
46 #if defined(__GNUC__) && defined(__i386__)
48 inline static void call_finally_block( void *code_block, void *base_ptr )
50 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
51 : : "a" (code_block), "g" (base_ptr));
54 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
55 struct __EXCEPTION_FRAME* frame,
56 PCONTEXT context WINE_UNUSED,
57 struct __EXCEPTION_FRAME** dispatch)
59 if (rec->ExceptionFlags & 0x6)
60 return ExceptionContinueSearch;
62 return ExceptionCollidedUnwind;
67 /*********************************************************************
68 * _XcptFilter (MSVCRT.@)
70 int _XcptFilter(int ex, PEXCEPTION_POINTERS ptr)
72 FIXME("(%d,%p)semi-stub\n", ex, ptr);
73 return UnhandledExceptionFilter(ptr);
76 /*********************************************************************
77 * _EH_prolog (MSVCRT.@)
80 /* Provided for VC++ binary compatability only */
81 __ASM_GLOBAL_FUNC(_EH_prolog,
85 "movl %esp, %fs:0\n\t"
86 "movl 12(%esp), %eax\n\t"
87 "movl %ebp, 12(%esp)\n\t"
88 "leal 12(%esp), %ebp\n\t"
93 /*******************************************************************
94 * _global_unwind2 (MSVCRT.@)
96 void _global_unwind2(PEXCEPTION_FRAME frame)
98 TRACE("(%p)\n",frame);
99 RtlUnwind( frame, 0, 0, 0 );
102 /*******************************************************************
103 * _local_unwind2 (MSVCRT.@)
105 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame,
108 MSVCRT_EXCEPTION_FRAME *curframe = frame;
109 DWORD curtrylevel = 0xfe;
112 TRACE("(%p,%ld,%ld)\n",frame, frame->trylevel, trylevel);
114 /* Register a handler in case of a nested exception */
115 reg.Handler = (PEXCEPTION_HANDLER)MSVCRT_nested_handler;
116 reg.Prev = NtCurrentTeb()->except;
117 __wine_push_frame(®);
119 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
121 curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
123 curframe->trylevel = curtrylevel;
124 if (!frame->scopetable[curtrylevel].lpfnFilter)
126 ERR("__try block cleanup not implemented - expect crash!\n");
127 /* FIXME: Remove current frame, set ebp, call
128 * frame->scopetable[curtrylevel].lpfnHandler()
132 __wine_pop_frame(®);
133 TRACE("unwound OK\n");
136 /*********************************************************************
137 * _except_handler2 (MSVCRT.@)
139 int _except_handler2(PEXCEPTION_RECORD rec,
140 PEXCEPTION_FRAME frame,
142 PEXCEPTION_FRAME* dispatcher)
144 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
145 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
146 frame->Handler, context, dispatcher);
147 return ExceptionContinueSearch;
150 /*********************************************************************
151 * _except_handler3 (MSVCRT.@)
153 int _except_handler3(PEXCEPTION_RECORD rec,
154 MSVCRT_EXCEPTION_FRAME* frame,
155 PCONTEXT context, void* dispatcher)
157 #if defined(__GNUC__) && defined(__i386__)
158 long retval, trylevel;
159 EXCEPTION_POINTERS exceptPtrs;
160 PSCOPETABLE pScopeTable;
162 TRACE("exception %lx flags=%lx at %p handler=%p %p %p semi-stub\n",
163 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
164 frame->handler, context, dispatcher);
166 __asm__ __volatile__ ("cld");
168 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
170 /* Unwinding the current frame */
171 _local_unwind2(frame, TRYLEVEL_END);
172 return ExceptionContinueSearch;
176 /* Hunting for handler */
177 exceptPtrs.ExceptionRecord = rec;
178 exceptPtrs.ContextRecord = context;
179 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
180 trylevel = frame->trylevel;
181 pScopeTable = frame->scopetable;
183 while (trylevel != TRYLEVEL_END)
185 if (pScopeTable[trylevel].lpfnFilter)
187 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
189 retval = pScopeTable[trylevel].lpfnFilter(&exceptPtrs);
191 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
192 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
193 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
195 if (retval == EXCEPTION_CONTINUE_EXECUTION)
196 return ExceptionContinueExecution;
198 if (retval == EXCEPTION_EXECUTE_HANDLER)
200 /* Unwind all higher frames, this one will handle the exception */
201 _global_unwind2((PEXCEPTION_FRAME)frame);
202 _local_unwind2(frame, trylevel);
204 /* Set our trylevel to the enclosing block, and call the __finally
205 * code, which won't return
207 frame->trylevel = pScopeTable->previousTryLevel;
208 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
209 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
210 ERR("Returned from __finally block - expect crash!\n");
213 trylevel = pScopeTable->previousTryLevel;
217 TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
218 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
219 frame->handler, context, dispatcher);
221 return ExceptionContinueSearch;
224 /*********************************************************************
225 * _abnormal_termination (MSVCRT.@)
227 int _abnormal_termination(void)
229 FIXME("(void)stub\n");
234 * setjmp/longjmp implementation
238 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
239 typedef void (*MSVCRT_unwind_function)(const void*);
242 * The signatures of the setjmp/longjmp functions do not match that
243 * declared in the setjmp header so they don't follow the regular naming
244 * convention to avoid conflicts.
247 /*******************************************************************
250 void _MSVCRT__setjmp(_JUMP_BUFFER *jmp, CONTEXT86* context)
253 jmp->Ebp = context->Ebp;
254 jmp->Ebx = context->Ebx;
255 jmp->Edi = context->Edi;
256 jmp->Esi = context->Esi;
257 jmp->Esp = context->Esp;
258 jmp->Eip = context->Eip;
259 jmp->Registration = (unsigned long)NtCurrentTeb()->except;
260 if (jmp->Registration == TRYLEVEL_END)
261 jmp->TryLevel = TRYLEVEL_END;
263 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
264 TRACE("returning 0\n");
268 /*******************************************************************
269 * _setjmp3 (MSVCRT.@)
271 void _MSVCRT__setjmp3(_JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
273 TRACE("(%p,%d)\n",jmp,nb_args);
274 jmp->Ebp = context->Ebp;
275 jmp->Ebx = context->Ebx;
276 jmp->Edi = context->Edi;
277 jmp->Esi = context->Esi;
278 jmp->Esp = context->Esp;
279 jmp->Eip = context->Eip;
280 jmp->Cookie = MSVCRT_JMP_MAGIC;
282 jmp->Registration = (unsigned long)NtCurrentTeb()->except;
283 if (jmp->Registration == TRYLEVEL_END)
285 jmp->TryLevel = TRYLEVEL_END;
289 void **args = ((void**)context->Esp)+2;
291 if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
292 if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
293 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
296 size_t size = (nb_args - 2) * sizeof(DWORD);
297 memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
300 TRACE("returning 0\n");
304 /*********************************************************************
307 void _MSVCRT_longjmp(_JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
309 unsigned long cur_frame = 0;
311 TRACE("(%p,%d)\n", jmp, retval);
313 cur_frame=(unsigned long)NtCurrentTeb()->except;
314 TRACE("cur_frame=%lx\n",cur_frame);
316 if (cur_frame != jmp->Registration)
317 _global_unwind2((PEXCEPTION_FRAME)jmp->Registration);
319 if (jmp->Registration)
321 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
322 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
324 MSVCRT_unwind_function unwind_func;
326 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
330 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
337 TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
338 context->Ebp = jmp->Ebp;
339 context->Ebx = jmp->Ebx;
340 context->Edi = jmp->Edi;
341 context->Esi = jmp->Esi;
342 context->Esp = jmp->Esp;
343 context->Eip = jmp->Eip;
344 context->Eax = retval;
348 /*********************************************************************
351 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
353 FIXME("(%d %p):stub\n", sig, func);
357 /*********************************************************************
358 * __CxxFrameHandler (MSVCRT.@)
360 DWORD __CxxFrameHandler(PEXCEPTION_RECORD rec, struct __EXCEPTION_FRAME* frame,
361 PCONTEXT context, struct __EXCEPTION_FRAME** dispatch)
363 FIXME("(%p,%p,%p,%p):stub?\n",rec,frame,context,dispatch);
365 /* Copied from MSVCRT_nested_handler, I hope this is more
366 * or less the right thing to do
368 if (rec->ExceptionFlags & 0x6)
369 return ExceptionContinueSearch;
371 return ExceptionCollidedUnwind;