Function parameters don't need WINE_UNUSED.
[wine.git] / dlls / msvcrt / except.c
blobcf5eaba6906ff29ec873163ef99e80b637130d4c
1 /*
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
20 * NOTES:
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.
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdarg.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winternl.h"
37 #include "wine/exception.h"
38 #include "msvcrt.h"
39 #include "excpt.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
49 int previousTryLevel;
50 int (*lpfnFilter)(PEXCEPTION_POINTERS);
51 int (*lpfnHandler)(void);
52 } SCOPETABLE, *PSCOPETABLE;
54 typedef struct _MSVCRT_EXCEPTION_FRAME
56 EXCEPTION_REGISTRATION_RECORD *prev;
57 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
58 PCONTEXT, PEXCEPTION_RECORD);
59 PSCOPETABLE scopetable;
60 int trylevel;
61 int _ebp;
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 )
76 DWORD ret;
77 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
78 : "=a" (ret)
79 : "0" (func), "g" (ebp), "g" (arg)
80 : "ecx", "edx", "memory" );
81 return ret;
83 #endif
85 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
86 EXCEPTION_REGISTRATION_RECORD* frame,
87 PCONTEXT context,
88 EXCEPTION_REGISTRATION_RECORD** dispatch)
90 if (rec->ExceptionFlags & 0x6)
91 return ExceptionContinueSearch;
92 *dispatch = frame;
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.@)
109 #ifdef __i386__
110 /* Provided for VC++ binary compatibility only */
111 __ASM_GLOBAL_FUNC(_EH_prolog,
112 "pushl $-1\n\t"
113 "pushl %eax\n\t"
114 "pushl %fs:0\n\t"
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"
119 "pushl %eax\n\t"
120 "ret");
121 #endif
123 /*******************************************************************
124 * _global_unwind2 (MSVCRT.@)
126 void _global_unwind2(EXCEPTION_REGISTRATION_RECORD* 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;
138 EXCEPTION_REGISTRATION_RECORD reg;
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()->Tib.ExceptionList;
145 __wine_push_frame(&reg);
147 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
149 int curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
150 curframe = frame;
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(&reg);
161 TRACE("unwound OK\n");
164 /*********************************************************************
165 * _except_handler2 (MSVCRT.@)
167 int _except_handler2(PEXCEPTION_RECORD rec,
168 EXCEPTION_REGISTRATION_RECORD* frame,
169 PCONTEXT context,
170 EXCEPTION_REGISTRATION_RECORD** 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__)
186 long retval;
187 int trylevel;
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;
203 else
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((EXCEPTION_REGISTRATION_RECORD*)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;
245 #else
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);
249 #endif
250 return ExceptionContinueSearch;
253 /*********************************************************************
254 * _abnormal_termination (MSVCRT.@)
256 int _abnormal_termination(void)
258 FIXME("(void)stub\n");
259 return 0;
263 * setjmp/longjmp implementation
266 #ifdef __i386__
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 /*******************************************************************
277 * _setjmp (MSVCRT.@)
279 DEFINE_REGS_ENTRYPOINT( MSVCRT__setjmp, _MSVCRT__setjmp, 4, 0 );
280 void _MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp, CONTEXT86* context)
282 TRACE("(%p)\n",jmp);
283 jmp->Ebp = context->Ebp;
284 jmp->Ebx = context->Ebx;
285 jmp->Edi = context->Edi;
286 jmp->Esi = context->Esi;
287 jmp->Esp = context->Esp;
288 jmp->Eip = context->Eip;
289 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
290 if (jmp->Registration == TRYLEVEL_END)
291 jmp->TryLevel = TRYLEVEL_END;
292 else
293 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
294 TRACE("returning 0\n");
295 context->Eax=0;
298 /*******************************************************************
299 * _setjmp3 (MSVCRT.@)
301 DEFINE_REGS_ENTRYPOINT( MSVCRT__setjmp3, _MSVCRT__setjmp3, 8, 0 );
302 void _MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
304 TRACE("(%p,%d)\n",jmp,nb_args);
305 jmp->Ebp = context->Ebp;
306 jmp->Ebx = context->Ebx;
307 jmp->Edi = context->Edi;
308 jmp->Esi = context->Esi;
309 jmp->Esp = context->Esp;
310 jmp->Eip = context->Eip;
311 jmp->Cookie = MSVCRT_JMP_MAGIC;
312 jmp->UnwindFunc = 0;
313 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
314 if (jmp->Registration == TRYLEVEL_END)
316 jmp->TryLevel = TRYLEVEL_END;
318 else
320 void **args = ((void**)context->Esp)+2;
322 if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
323 if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
324 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
325 if (nb_args > 2)
327 size_t size = (nb_args - 2) * sizeof(DWORD);
328 memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
331 TRACE("returning 0\n");
332 context->Eax = 0;
335 /*********************************************************************
336 * longjmp (MSVCRT.@)
338 DEFINE_REGS_ENTRYPOINT( MSVCRT_longjmp, _MSVCRT_longjmp, 8, 0 );
339 void _MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
341 unsigned long cur_frame = 0;
343 TRACE("(%p,%d)\n", jmp, retval);
345 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
346 TRACE("cur_frame=%lx\n",cur_frame);
348 if (cur_frame != jmp->Registration)
349 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
351 if (jmp->Registration)
353 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
354 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
356 MSVCRT_unwind_function unwind_func;
358 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
359 unwind_func(jmp);
361 else
362 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
363 jmp->TryLevel);
366 if (!retval)
367 retval = 1;
369 TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
370 context->Ebp = jmp->Ebp;
371 context->Ebx = jmp->Ebx;
372 context->Edi = jmp->Edi;
373 context->Esi = jmp->Esi;
374 context->Esp = jmp->Esp;
375 context->Eip = jmp->Eip;
376 context->Eax = retval;
379 /*********************************************************************
380 * _seh_longjmp_unwind (MSVCRT.@)
382 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
384 _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
386 #endif /* i386 */
388 /*********************************************************************
389 * signal (MSVCRT.@)
391 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
393 FIXME("(%d %p):stub\n", sig, func);
394 return (void*)-1;