Various cosmetic changes.
[wine] / dlls / msvcrt / except.c
1 /*
2  * msvcrt.dll exception handling
3  *
4  * Copyright 2000 Jon Griffiths
5  *
6  * See http://www.microsoft.com/msj/0197/exception/exception.htm,
7  * but don't believe all of it.
8  *
9  * FIXME: Incomplete support for nested exceptions/try block cleanup.
10  */
11 #include "config.h"
12
13 #include "ntddk.h"
14 #include "wine/exception.h"
15 #include "thread.h"
16 #include "msvcrt.h"
17
18 #include "msvcrt/setjmp.h"
19 #include "msvcrt/excpt.h"
20
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
25
26 typedef void (*MSVCRT_sig_handler_func)(void);
27
28 /* VC++ extensions to Win32 SEH */
29 typedef struct _SCOPETABLE
30 {
31   DWORD previousTryLevel;
32   int (*lpfnFilter)(PEXCEPTION_POINTERS);
33   int (*lpfnHandler)(void);
34 } SCOPETABLE, *PSCOPETABLE;
35
36 typedef struct _MSVCRT_EXCEPTION_FRAME
37 {
38   EXCEPTION_FRAME *prev;
39   void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_FRAME,
40                   PCONTEXT, PEXCEPTION_RECORD);
41   PSCOPETABLE scopetable;
42   DWORD trylevel;
43   int _ebp;
44   PEXCEPTION_POINTERS xpointers;
45 } MSVCRT_EXCEPTION_FRAME;
46
47 #define TRYLEVEL_END 0xffffffff /* End of trylevel list */
48
49 #if defined(__GNUC__) && defined(__i386__)
50
51 inline static void call_finally_block( void *code_block, void *base_ptr )
52 {
53     __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
54                           : : "a" (code_block), "g" (base_ptr));
55 }
56
57 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
58                                    struct __EXCEPTION_FRAME* frame,
59                                    PCONTEXT context WINE_UNUSED,
60                                    struct __EXCEPTION_FRAME** dispatch)
61 {
62   if (rec->ExceptionFlags & 0x6)
63     return ExceptionContinueSearch;
64   *dispatch = frame;
65   return ExceptionCollidedUnwind;
66 }
67 #endif
68
69
70 /*********************************************************************
71  *              _XcptFilter (MSVCRT.@)
72  */
73 int _XcptFilter(int ex, PEXCEPTION_POINTERS ptr)
74 {
75   FIXME("(%d,%p)semi-stub\n", ex, ptr);
76   return UnhandledExceptionFilter(ptr);
77 }
78
79 /*********************************************************************
80  *              _EH_prolog (MSVCRT.@)
81  */
82 #ifdef __i386__
83 /* Provided for VC++ binary compatability only */
84 __ASM_GLOBAL_FUNC(_EH_prolog,
85                   "pushl $0xff\n\t"
86                   "pushl %eax\n\t"
87                   "pushl %fs:0\n\t"
88                   "movl  %esp, %fs:0\n\t"
89                   "movl  12(%esp), %eax\n\t"
90                   "movl  %ebp, 12(%esp)\n\t"
91                   "leal  12(%esp), %ebp\n\t"
92                   "pushl %eax\n\t"
93                   "ret");
94 #endif
95
96 /*******************************************************************
97  *              _global_unwind2 (MSVCRT.@)
98  */
99 void _global_unwind2(PEXCEPTION_FRAME frame)
100 {
101     TRACE("(%p)\n",frame);
102     RtlUnwind( frame, 0, 0, 0 );
103 }
104
105 /*******************************************************************
106  *              _local_unwind2 (MSVCRT.@)
107  */
108 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame,
109                     DWORD trylevel)
110 {
111   MSVCRT_EXCEPTION_FRAME *curframe = frame;
112   DWORD curtrylevel = 0xfe;
113   EXCEPTION_FRAME reg;
114
115   TRACE("(%p,%ld,%ld)\n",frame, frame->trylevel, trylevel);
116
117   /* Register a handler in case of a nested exception */
118   reg.Handler = (PEXCEPTION_HANDLER)MSVCRT_nested_handler;
119   reg.Prev = NtCurrentTeb()->except;
120   __wine_push_frame(&reg);
121
122   while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
123   {
124     curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
125     curframe = frame;
126     curframe->trylevel = curtrylevel;
127     if (!frame->scopetable[curtrylevel].lpfnFilter)
128     {
129       ERR("__try block cleanup not implemented - expect crash!\n");
130       /* FIXME: Remove current frame, set ebp, call
131        * frame->scopetable[curtrylevel].lpfnHandler()
132        */
133     }
134   }
135   __wine_pop_frame(&reg);
136   TRACE("unwound OK\n");
137 }
138
139 /*********************************************************************
140  *              _except_handler2 (MSVCRT.@)
141  */
142 int _except_handler2(PEXCEPTION_RECORD rec,
143                      PEXCEPTION_FRAME frame,
144                      PCONTEXT context,
145                      PEXCEPTION_FRAME* dispatcher)
146 {
147   FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
148         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
149         frame->Handler, context, dispatcher);
150   return ExceptionContinueSearch;
151 }
152
153 /*********************************************************************
154  *              _except_handler3 (MSVCRT.@)
155  */
156 int _except_handler3(PEXCEPTION_RECORD rec,
157                      MSVCRT_EXCEPTION_FRAME* frame,
158                      PCONTEXT context, void* dispatcher)
159 {
160 #if defined(__GNUC__) && defined(__i386__)
161   long retval, trylevel;
162   EXCEPTION_POINTERS exceptPtrs;
163   PSCOPETABLE pScopeTable;
164
165   TRACE("exception %lx flags=%lx at %p handler=%p %p %p semi-stub\n",
166         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
167         frame->handler, context, dispatcher);
168
169   __asm__ __volatile__ ("cld");
170
171   if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
172   {
173     /* Unwinding the current frame */
174      _local_unwind2(frame, TRYLEVEL_END);
175     return ExceptionContinueSearch;
176   }
177   else
178   {
179     /* Hunting for handler */
180     exceptPtrs.ExceptionRecord = rec;
181     exceptPtrs.ContextRecord = context;
182     *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
183     trylevel = frame->trylevel;
184     pScopeTable = frame->scopetable;
185
186     while (trylevel != TRYLEVEL_END)
187     {
188       if (pScopeTable[trylevel].lpfnFilter)
189       {
190         TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
191
192         retval = pScopeTable[trylevel].lpfnFilter(&exceptPtrs);
193
194         TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
195               "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
196               "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
197
198         if (retval == EXCEPTION_CONTINUE_EXECUTION)
199           return ExceptionContinueExecution;
200
201         if (retval == EXCEPTION_EXECUTE_HANDLER)
202         {
203           /* Unwind all higher frames, this one will handle the exception */
204           _global_unwind2((PEXCEPTION_FRAME)frame);
205           _local_unwind2(frame, trylevel);
206
207           /* Set our trylevel to the enclosing block, and call the __finally
208            * code, which won't return
209            */
210           frame->trylevel = pScopeTable->previousTryLevel;
211           TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
212           call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
213           ERR("Returned from __finally block - expect crash!\n");
214        }
215       }
216       trylevel = pScopeTable->previousTryLevel;
217     }
218   }
219 #else
220   TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
221         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
222         frame->handler, context, dispatcher);
223 #endif
224   return ExceptionContinueSearch;
225 }
226
227 /*********************************************************************
228  *              _abnormal_termination (MSVCRT.@)
229  */
230 int _abnormal_termination(void)
231 {
232   FIXME("(void)stub\n");
233   return 0;
234 }
235
236 /*
237  * setjmp/longjmp implementation
238  */
239
240 #ifdef __i386__
241 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
242 typedef void (*MSVCRT_unwind_function)(const void*);
243
244 /*
245  * The signatures of the setjmp/longjmp functions do not match that 
246  * declared in the setjmp header so they don't follow the regular naming 
247  * convention to avoid conflicts.
248  */
249
250 /*******************************************************************
251  *              _setjmp (MSVCRT.@)
252  */
253 void _MSVCRT__setjmp(_JUMP_BUFFER *jmp, CONTEXT86* context)
254 {
255     TRACE("(%p)\n",jmp);
256     jmp->Ebp = context->Ebp;
257     jmp->Ebx = context->Ebx;
258     jmp->Edi = context->Edi;
259     jmp->Esi = context->Esi;
260     jmp->Esp = context->Esp;
261     jmp->Eip = context->Eip;
262     jmp->Registration = (unsigned long)NtCurrentTeb()->except;
263     if (jmp->Registration == TRYLEVEL_END)
264         jmp->TryLevel = TRYLEVEL_END;
265     else
266         jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
267     TRACE("returning 0\n");
268     context->Eax=0;
269 }
270
271 /*******************************************************************
272  *              _setjmp3 (MSVCRT.@)
273  */
274 void _MSVCRT__setjmp3(_JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
275 {
276     TRACE("(%p,%d)\n",jmp,nb_args);
277     jmp->Ebp = context->Ebp;
278     jmp->Ebx = context->Ebx;
279     jmp->Edi = context->Edi;
280     jmp->Esi = context->Esi;
281     jmp->Esp = context->Esp;
282     jmp->Eip = context->Eip;
283     jmp->Cookie = MSVCRT_JMP_MAGIC;
284     jmp->UnwindFunc = 0;
285     jmp->Registration = (unsigned long)NtCurrentTeb()->except;
286     if (jmp->Registration == TRYLEVEL_END)
287     {
288         jmp->TryLevel = TRYLEVEL_END;
289     }
290     else
291     {
292         void **args = ((void**)context->Esp)+2;
293
294         if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
295         if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
296         else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
297         if (nb_args > 2)
298         {
299             size_t size = (nb_args - 2) * sizeof(DWORD);
300             memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
301         }
302     }
303     TRACE("returning 0\n");
304     context->Eax = 0;
305 }
306
307 /*********************************************************************
308  *              longjmp (MSVCRT.@)
309  */
310 void _MSVCRT_longjmp(_JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
311 {
312     unsigned long cur_frame = 0;
313
314     TRACE("(%p,%d)\n", jmp, retval);
315
316     cur_frame=(unsigned long)NtCurrentTeb()->except;
317     TRACE("cur_frame=%lx\n",cur_frame);
318
319     if (cur_frame != jmp->Registration)
320         _global_unwind2((PEXCEPTION_FRAME)jmp->Registration);
321
322     if (jmp->Registration)
323     {
324         if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
325             jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
326         {
327             MSVCRT_unwind_function unwind_func;
328
329             unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
330             unwind_func(jmp);
331         }
332         else
333             _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
334                            jmp->TryLevel);
335     }
336
337     if (!retval)
338         retval = 1;
339
340     TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
341     context->Ebp = jmp->Ebp;
342     context->Ebx = jmp->Ebx;
343     context->Edi = jmp->Edi;
344     context->Esi = jmp->Esi;
345     context->Esp = jmp->Esp;
346     context->Eip = jmp->Eip;
347     context->Eax = retval;
348 }
349 #endif /* i386 */
350
351 /*********************************************************************
352  *              signal (MSVCRT.@)
353  */
354 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
355 {
356   FIXME("(%d %p):stub\n", sig, func);
357   return (void*)-1;
358 }
359
360 /*********************************************************************
361  *              __CxxFrameHandler (MSVCRT.@)
362  */
363 DWORD __CxxFrameHandler(PEXCEPTION_RECORD rec, struct __EXCEPTION_FRAME* frame,
364                         PCONTEXT context, struct __EXCEPTION_FRAME** dispatch)
365 {
366     FIXME("(%p,%p,%p,%p):stub?\n",rec,frame,context,dispatch);
367
368     /* Copied from MSVCRT_nested_handler, I hope this is more 
369      * or less the right thing to do
370      */
371     if (rec->ExceptionFlags & 0x6)
372         return ExceptionContinueSearch;
373     *dispatch = frame;
374     return ExceptionCollidedUnwind;
375 }