Added the DFCS_{HOT,TRANSPARENT} definitions.
[wine/gsoc_dplay.git] / dlls / msvcrt / except.c
blob5cf36fcba7bd48c3a7e8e3dbf3c3a7dd033cc14b
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.
27 #include "config.h"
29 #include "ntddk.h"
30 #include "wine/exception.h"
31 #include "thread.h"
32 #include "msvcrt.h"
34 #include "msvcrt/setjmp.h"
35 #include "msvcrt/excpt.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42 typedef void (*MSVCRT_sig_handler_func)(void);
44 /* VC++ extensions to Win32 SEH */
45 typedef struct _SCOPETABLE
47 DWORD previousTryLevel;
48 int (*lpfnFilter)(PEXCEPTION_POINTERS);
49 int (*lpfnHandler)(void);
50 } SCOPETABLE, *PSCOPETABLE;
52 typedef struct _MSVCRT_EXCEPTION_FRAME
54 EXCEPTION_FRAME *prev;
55 void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_FRAME,
56 PCONTEXT, PEXCEPTION_RECORD);
57 PSCOPETABLE scopetable;
58 DWORD trylevel;
59 int _ebp;
60 PEXCEPTION_POINTERS xpointers;
61 } MSVCRT_EXCEPTION_FRAME;
63 #define TRYLEVEL_END 0xffffffff /* End of trylevel list */
65 #if defined(__GNUC__) && defined(__i386__)
67 inline static void call_finally_block( void *code_block, void *base_ptr )
69 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
70 : : "a" (code_block), "g" (base_ptr));
73 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
74 struct __EXCEPTION_FRAME* frame,
75 PCONTEXT context WINE_UNUSED,
76 struct __EXCEPTION_FRAME** dispatch)
78 if (rec->ExceptionFlags & 0x6)
79 return ExceptionContinueSearch;
80 *dispatch = frame;
81 return ExceptionCollidedUnwind;
83 #endif
86 /*********************************************************************
87 * _XcptFilter (MSVCRT.@)
89 int _XcptFilter(int ex, PEXCEPTION_POINTERS ptr)
91 FIXME("(%d,%p)semi-stub\n", ex, ptr);
92 return UnhandledExceptionFilter(ptr);
95 /*********************************************************************
96 * _EH_prolog (MSVCRT.@)
98 #ifdef __i386__
99 /* Provided for VC++ binary compatability only */
100 __ASM_GLOBAL_FUNC(_EH_prolog,
101 "pushl $0xff\n\t"
102 "pushl %eax\n\t"
103 "pushl %fs:0\n\t"
104 "movl %esp, %fs:0\n\t"
105 "movl 12(%esp), %eax\n\t"
106 "movl %ebp, 12(%esp)\n\t"
107 "leal 12(%esp), %ebp\n\t"
108 "pushl %eax\n\t"
109 "ret");
110 #endif
112 /*******************************************************************
113 * _global_unwind2 (MSVCRT.@)
115 void _global_unwind2(PEXCEPTION_FRAME frame)
117 TRACE("(%p)\n",frame);
118 RtlUnwind( frame, 0, 0, 0 );
121 /*******************************************************************
122 * _local_unwind2 (MSVCRT.@)
124 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame,
125 DWORD trylevel)
127 MSVCRT_EXCEPTION_FRAME *curframe = frame;
128 DWORD curtrylevel = 0xfe;
129 EXCEPTION_FRAME reg;
131 TRACE("(%p,%ld,%ld)\n",frame, frame->trylevel, trylevel);
133 /* Register a handler in case of a nested exception */
134 reg.Handler = (PEXCEPTION_HANDLER)MSVCRT_nested_handler;
135 reg.Prev = NtCurrentTeb()->except;
136 __wine_push_frame(&reg);
138 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
140 curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
141 curframe = frame;
142 curframe->trylevel = curtrylevel;
143 if (!frame->scopetable[curtrylevel].lpfnFilter)
145 ERR("__try block cleanup not implemented - expect crash!\n");
146 /* FIXME: Remove current frame, set ebp, call
147 * frame->scopetable[curtrylevel].lpfnHandler()
151 __wine_pop_frame(&reg);
152 TRACE("unwound OK\n");
155 /*********************************************************************
156 * _except_handler2 (MSVCRT.@)
158 int _except_handler2(PEXCEPTION_RECORD rec,
159 PEXCEPTION_FRAME frame,
160 PCONTEXT context,
161 PEXCEPTION_FRAME* dispatcher)
163 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
164 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
165 frame->Handler, context, dispatcher);
166 return ExceptionContinueSearch;
169 /*********************************************************************
170 * _except_handler3 (MSVCRT.@)
172 int _except_handler3(PEXCEPTION_RECORD rec,
173 MSVCRT_EXCEPTION_FRAME* frame,
174 PCONTEXT context, void* dispatcher)
176 #if defined(__GNUC__) && defined(__i386__)
177 long retval, trylevel;
178 EXCEPTION_POINTERS exceptPtrs;
179 PSCOPETABLE pScopeTable;
181 TRACE("exception %lx flags=%lx at %p handler=%p %p %p semi-stub\n",
182 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
183 frame->handler, context, dispatcher);
185 __asm__ __volatile__ ("cld");
187 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
189 /* Unwinding the current frame */
190 _local_unwind2(frame, TRYLEVEL_END);
191 return ExceptionContinueSearch;
193 else
195 /* Hunting for handler */
196 exceptPtrs.ExceptionRecord = rec;
197 exceptPtrs.ContextRecord = context;
198 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
199 trylevel = frame->trylevel;
200 pScopeTable = frame->scopetable;
202 while (trylevel != TRYLEVEL_END)
204 if (pScopeTable[trylevel].lpfnFilter)
206 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
208 retval = pScopeTable[trylevel].lpfnFilter(&exceptPtrs);
210 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
211 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
212 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
214 if (retval == EXCEPTION_CONTINUE_EXECUTION)
215 return ExceptionContinueExecution;
217 if (retval == EXCEPTION_EXECUTE_HANDLER)
219 /* Unwind all higher frames, this one will handle the exception */
220 _global_unwind2((PEXCEPTION_FRAME)frame);
221 _local_unwind2(frame, trylevel);
223 /* Set our trylevel to the enclosing block, and call the __finally
224 * code, which won't return
226 frame->trylevel = pScopeTable->previousTryLevel;
227 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
228 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
229 ERR("Returned from __finally block - expect crash!\n");
232 trylevel = pScopeTable->previousTryLevel;
235 #else
236 TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
237 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
238 frame->handler, context, dispatcher);
239 #endif
240 return ExceptionContinueSearch;
243 /*********************************************************************
244 * _abnormal_termination (MSVCRT.@)
246 int _abnormal_termination(void)
248 FIXME("(void)stub\n");
249 return 0;
253 * setjmp/longjmp implementation
256 #ifdef __i386__
257 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
258 typedef void (*MSVCRT_unwind_function)(const void*);
261 * The signatures of the setjmp/longjmp functions do not match that
262 * declared in the setjmp header so they don't follow the regular naming
263 * convention to avoid conflicts.
266 /*******************************************************************
267 * _setjmp (MSVCRT.@)
269 void _MSVCRT__setjmp(_JUMP_BUFFER *jmp, CONTEXT86* context)
271 TRACE("(%p)\n",jmp);
272 jmp->Ebp = context->Ebp;
273 jmp->Ebx = context->Ebx;
274 jmp->Edi = context->Edi;
275 jmp->Esi = context->Esi;
276 jmp->Esp = context->Esp;
277 jmp->Eip = context->Eip;
278 jmp->Registration = (unsigned long)NtCurrentTeb()->except;
279 if (jmp->Registration == TRYLEVEL_END)
280 jmp->TryLevel = TRYLEVEL_END;
281 else
282 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
283 TRACE("returning 0\n");
284 context->Eax=0;
287 /*******************************************************************
288 * _setjmp3 (MSVCRT.@)
290 void _MSVCRT__setjmp3(_JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
292 TRACE("(%p,%d)\n",jmp,nb_args);
293 jmp->Ebp = context->Ebp;
294 jmp->Ebx = context->Ebx;
295 jmp->Edi = context->Edi;
296 jmp->Esi = context->Esi;
297 jmp->Esp = context->Esp;
298 jmp->Eip = context->Eip;
299 jmp->Cookie = MSVCRT_JMP_MAGIC;
300 jmp->UnwindFunc = 0;
301 jmp->Registration = (unsigned long)NtCurrentTeb()->except;
302 if (jmp->Registration == TRYLEVEL_END)
304 jmp->TryLevel = TRYLEVEL_END;
306 else
308 void **args = ((void**)context->Esp)+2;
310 if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
311 if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
312 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
313 if (nb_args > 2)
315 size_t size = (nb_args - 2) * sizeof(DWORD);
316 memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
319 TRACE("returning 0\n");
320 context->Eax = 0;
323 /*********************************************************************
324 * longjmp (MSVCRT.@)
326 void _MSVCRT_longjmp(_JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
328 unsigned long cur_frame = 0;
330 TRACE("(%p,%d)\n", jmp, retval);
332 cur_frame=(unsigned long)NtCurrentTeb()->except;
333 TRACE("cur_frame=%lx\n",cur_frame);
335 if (cur_frame != jmp->Registration)
336 _global_unwind2((PEXCEPTION_FRAME)jmp->Registration);
338 if (jmp->Registration)
340 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
341 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
343 MSVCRT_unwind_function unwind_func;
345 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
346 unwind_func(jmp);
348 else
349 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
350 jmp->TryLevel);
353 if (!retval)
354 retval = 1;
356 TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
357 context->Ebp = jmp->Ebp;
358 context->Ebx = jmp->Ebx;
359 context->Edi = jmp->Edi;
360 context->Esi = jmp->Esi;
361 context->Esp = jmp->Esp;
362 context->Eip = jmp->Eip;
363 context->Eax = retval;
365 #endif /* i386 */
367 /*********************************************************************
368 * signal (MSVCRT.@)
370 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
372 FIXME("(%d %p):stub\n", sig, func);
373 return (void*)-1;
376 /*********************************************************************
377 * __CxxFrameHandler (MSVCRT.@)
379 DWORD __CxxFrameHandler(PEXCEPTION_RECORD rec, struct __EXCEPTION_FRAME* frame,
380 PCONTEXT context, struct __EXCEPTION_FRAME** dispatch)
382 FIXME("(%p,%p,%p,%p):stub?\n",rec,frame,context,dispatch);
384 /* Copied from MSVCRT_nested_handler, I hope this is more
385 * or less the right thing to do
387 if (rec->ExceptionFlags & 0x6)
388 return ExceptionContinueSearch;
389 *dispatch = frame;
390 return ExceptionCollidedUnwind;