2 * msvcrt.dll exception handling
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See http://www.microsoft.com/msj/0197/exception/exception.htm,
23 * but don't believe all of it.
25 * FIXME: Incomplete support for nested exceptions/try block cleanup.
29 #include "wine/port.h"
32 #include "wine/exception.h"
36 #include "msvcrt/setjmp.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
44 typedef void (*MSVCRT_sig_handler_func)(void);
46 /* VC++ extensions to Win32 SEH */
47 typedef struct _SCOPETABLE
50 int (*lpfnFilter)(PEXCEPTION_POINTERS);
51 int (*lpfnHandler)(void);
52 } SCOPETABLE, *PSCOPETABLE;
54 typedef struct _MSVCRT_EXCEPTION_FRAME
56 EXCEPTION_FRAME *prev;
57 void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_FRAME,
58 PCONTEXT, PEXCEPTION_RECORD);
59 PSCOPETABLE scopetable;
62 PEXCEPTION_POINTERS xpointers;
63 } MSVCRT_EXCEPTION_FRAME;
65 #define TRYLEVEL_END (-1) /* End of trylevel list */
67 #if defined(__GNUC__) && defined(__i386__)
68 inline static void call_finally_block( void *code_block, void *base_ptr )
70 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
71 : : "a" (code_block), "g" (base_ptr));
74 inline static DWORD call_filter( void *func, void *arg, void *ebp )
77 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
79 : "0" (func), "g" (ebp), "g" (arg)
80 : "ecx", "edx", "memory" );
85 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
86 struct __EXCEPTION_FRAME* frame,
87 PCONTEXT context WINE_UNUSED,
88 struct __EXCEPTION_FRAME** dispatch)
90 if (rec->ExceptionFlags & 0x6)
91 return ExceptionContinueSearch;
93 return ExceptionCollidedUnwind;
97 /*********************************************************************
98 * _XcptFilter (MSVCRT.@)
100 int _XcptFilter(int ex, PEXCEPTION_POINTERS ptr)
102 FIXME("(%d,%p)semi-stub\n", ex, ptr);
103 return UnhandledExceptionFilter(ptr);
106 /*********************************************************************
107 * _EH_prolog (MSVCRT.@)
110 /* Provided for VC++ binary compatability only */
111 __ASM_GLOBAL_FUNC(_EH_prolog,
115 "movl %esp, %fs:0\n\t"
116 "movl 12(%esp), %eax\n\t"
117 "movl %ebp, 12(%esp)\n\t"
118 "leal 12(%esp), %ebp\n\t"
123 /*******************************************************************
124 * _global_unwind2 (MSVCRT.@)
126 void _global_unwind2(PEXCEPTION_FRAME frame)
128 TRACE("(%p)\n",frame);
129 RtlUnwind( frame, 0, 0, 0 );
132 /*******************************************************************
133 * _local_unwind2 (MSVCRT.@)
135 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
137 MSVCRT_EXCEPTION_FRAME *curframe = frame;
140 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
142 /* Register a handler in case of a nested exception */
143 reg.Handler = (PEXCEPTION_HANDLER)MSVCRT_nested_handler;
144 reg.Prev = NtCurrentTeb()->except;
145 __wine_push_frame(®);
147 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
149 int curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
151 curframe->trylevel = curtrylevel;
152 if (!frame->scopetable[curtrylevel].lpfnFilter)
154 ERR("__try block cleanup not implemented - expect crash!\n");
155 /* FIXME: Remove current frame, set ebp, call
156 * frame->scopetable[curtrylevel].lpfnHandler()
160 __wine_pop_frame(®);
161 TRACE("unwound OK\n");
164 /*********************************************************************
165 * _except_handler2 (MSVCRT.@)
167 int _except_handler2(PEXCEPTION_RECORD rec,
168 PEXCEPTION_FRAME frame,
170 PEXCEPTION_FRAME* dispatcher)
172 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
173 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
174 frame->Handler, context, dispatcher);
175 return ExceptionContinueSearch;
178 /*********************************************************************
179 * _except_handler3 (MSVCRT.@)
181 int _except_handler3(PEXCEPTION_RECORD rec,
182 MSVCRT_EXCEPTION_FRAME* frame,
183 PCONTEXT context, void* dispatcher)
185 #if defined(__GNUC__) && defined(__i386__)
188 EXCEPTION_POINTERS exceptPtrs;
189 PSCOPETABLE pScopeTable;
191 TRACE("exception %lx flags=%lx at %p handler=%p %p %p semi-stub\n",
192 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
193 frame->handler, context, dispatcher);
195 __asm__ __volatile__ ("cld");
197 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
199 /* Unwinding the current frame */
200 _local_unwind2(frame, TRYLEVEL_END);
201 return ExceptionContinueSearch;
205 /* Hunting for handler */
206 exceptPtrs.ExceptionRecord = rec;
207 exceptPtrs.ContextRecord = context;
208 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
209 trylevel = frame->trylevel;
210 pScopeTable = frame->scopetable;
212 while (trylevel != TRYLEVEL_END)
214 if (pScopeTable[trylevel].lpfnFilter)
216 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
218 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
220 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
221 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
222 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
224 if (retval == EXCEPTION_CONTINUE_EXECUTION)
225 return ExceptionContinueExecution;
227 if (retval == EXCEPTION_EXECUTE_HANDLER)
229 /* Unwind all higher frames, this one will handle the exception */
230 _global_unwind2((PEXCEPTION_FRAME)frame);
231 _local_unwind2(frame, trylevel);
233 /* Set our trylevel to the enclosing block, and call the __finally
234 * code, which won't return
236 frame->trylevel = pScopeTable->previousTryLevel;
237 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
238 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
239 ERR("Returned from __finally block - expect crash!\n");
242 trylevel = pScopeTable->previousTryLevel;
246 TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
247 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
248 frame->handler, context, dispatcher);
250 return ExceptionContinueSearch;
253 /*********************************************************************
254 * _abnormal_termination (MSVCRT.@)
256 int _abnormal_termination(void)
258 FIXME("(void)stub\n");
263 * setjmp/longjmp implementation
267 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
268 typedef void (*MSVCRT_unwind_function)(const void*);
271 * The signatures of the setjmp/longjmp functions do not match that
272 * declared in the setjmp header so they don't follow the regular naming
273 * convention to avoid conflicts.
276 /*******************************************************************
279 void _MSVCRT__setjmp(_JUMP_BUFFER *jmp, CONTEXT86* context)
282 jmp->Ebp = context->Ebp;
283 jmp->Ebx = context->Ebx;
284 jmp->Edi = context->Edi;
285 jmp->Esi = context->Esi;
286 jmp->Esp = context->Esp;
287 jmp->Eip = context->Eip;
288 jmp->Registration = (unsigned long)NtCurrentTeb()->except;
289 if (jmp->Registration == TRYLEVEL_END)
290 jmp->TryLevel = TRYLEVEL_END;
292 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
293 TRACE("returning 0\n");
297 /*******************************************************************
298 * _setjmp3 (MSVCRT.@)
300 void _MSVCRT__setjmp3(_JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
302 TRACE("(%p,%d)\n",jmp,nb_args);
303 jmp->Ebp = context->Ebp;
304 jmp->Ebx = context->Ebx;
305 jmp->Edi = context->Edi;
306 jmp->Esi = context->Esi;
307 jmp->Esp = context->Esp;
308 jmp->Eip = context->Eip;
309 jmp->Cookie = MSVCRT_JMP_MAGIC;
311 jmp->Registration = (unsigned long)NtCurrentTeb()->except;
312 if (jmp->Registration == TRYLEVEL_END)
314 jmp->TryLevel = TRYLEVEL_END;
318 void **args = ((void**)context->Esp)+2;
320 if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
321 if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
322 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
325 size_t size = (nb_args - 2) * sizeof(DWORD);
326 memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
329 TRACE("returning 0\n");
333 /*********************************************************************
336 void _MSVCRT_longjmp(_JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
338 unsigned long cur_frame = 0;
340 TRACE("(%p,%d)\n", jmp, retval);
342 cur_frame=(unsigned long)NtCurrentTeb()->except;
343 TRACE("cur_frame=%lx\n",cur_frame);
345 if (cur_frame != jmp->Registration)
346 _global_unwind2((PEXCEPTION_FRAME)jmp->Registration);
348 if (jmp->Registration)
350 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
351 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
353 MSVCRT_unwind_function unwind_func;
355 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
359 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
366 TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
367 context->Ebp = jmp->Ebp;
368 context->Ebx = jmp->Ebx;
369 context->Edi = jmp->Edi;
370 context->Esi = jmp->Esi;
371 context->Esp = jmp->Esp;
372 context->Eip = jmp->Eip;
373 context->Eax = retval;
376 /*********************************************************************
377 * _seh_longjmp_unwind (MSVCRT.@)
379 void __stdcall _seh_longjmp_unwind(_JUMP_BUFFER *jmp)
381 _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
385 /*********************************************************************
388 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
390 FIXME("(%d %p):stub\n", sig, func);