Faster serial speed cases for non Linux systems.
[wine] / dlls / msvcrt / except.c
1 /*
2  * msvcrt.dll exception handling
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  *
20  * NOTES:
21  *
22  * See http://www.microsoft.com/msj/0197/exception/exception.htm,
23  * but don't believe all of it.
24  *
25  * FIXME: Incomplete support for nested exceptions/try block cleanup.
26  */
27 #include "config.h"
28
29 #include "ntddk.h"
30 #include "wine/exception.h"
31 #include "thread.h"
32 #include "msvcrt.h"
33
34 #include "msvcrt/setjmp.h"
35 #include "msvcrt/excpt.h"
36
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41
42 typedef void (*MSVCRT_sig_handler_func)(void);
43
44 /* VC++ extensions to Win32 SEH */
45 typedef struct _SCOPETABLE
46 {
47   DWORD previousTryLevel;
48   int (*lpfnFilter)(PEXCEPTION_POINTERS);
49   int (*lpfnHandler)(void);
50 } SCOPETABLE, *PSCOPETABLE;
51
52 typedef struct _MSVCRT_EXCEPTION_FRAME
53 {
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;
62
63 #define TRYLEVEL_END 0xffffffff /* End of trylevel list */
64
65 #if defined(__GNUC__) && defined(__i386__)
66
67 inline static void call_finally_block( void *code_block, void *base_ptr )
68 {
69     __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
70                           : : "a" (code_block), "g" (base_ptr));
71 }
72
73 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
74                                    struct __EXCEPTION_FRAME* frame,
75                                    PCONTEXT context WINE_UNUSED,
76                                    struct __EXCEPTION_FRAME** dispatch)
77 {
78   if (rec->ExceptionFlags & 0x6)
79     return ExceptionContinueSearch;
80   *dispatch = frame;
81   return ExceptionCollidedUnwind;
82 }
83 #endif
84
85
86 /*********************************************************************
87  *              _XcptFilter (MSVCRT.@)
88  */
89 int _XcptFilter(int ex, PEXCEPTION_POINTERS ptr)
90 {
91   FIXME("(%d,%p)semi-stub\n", ex, ptr);
92   return UnhandledExceptionFilter(ptr);
93 }
94
95 /*********************************************************************
96  *              _EH_prolog (MSVCRT.@)
97  */
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
111
112 /*******************************************************************
113  *              _global_unwind2 (MSVCRT.@)
114  */
115 void _global_unwind2(PEXCEPTION_FRAME frame)
116 {
117     TRACE("(%p)\n",frame);
118     RtlUnwind( frame, 0, 0, 0 );
119 }
120
121 /*******************************************************************
122  *              _local_unwind2 (MSVCRT.@)
123  */
124 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame,
125                     DWORD trylevel)
126 {
127   MSVCRT_EXCEPTION_FRAME *curframe = frame;
128   DWORD curtrylevel = 0xfe;
129   EXCEPTION_FRAME reg;
130
131   TRACE("(%p,%ld,%ld)\n",frame, frame->trylevel, trylevel);
132
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);
137
138   while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
139   {
140     curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
141     curframe = frame;
142     curframe->trylevel = curtrylevel;
143     if (!frame->scopetable[curtrylevel].lpfnFilter)
144     {
145       ERR("__try block cleanup not implemented - expect crash!\n");
146       /* FIXME: Remove current frame, set ebp, call
147        * frame->scopetable[curtrylevel].lpfnHandler()
148        */
149     }
150   }
151   __wine_pop_frame(&reg);
152   TRACE("unwound OK\n");
153 }
154
155 /*********************************************************************
156  *              _except_handler2 (MSVCRT.@)
157  */
158 int _except_handler2(PEXCEPTION_RECORD rec,
159                      PEXCEPTION_FRAME frame,
160                      PCONTEXT context,
161                      PEXCEPTION_FRAME* dispatcher)
162 {
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;
167 }
168
169 /*********************************************************************
170  *              _except_handler3 (MSVCRT.@)
171  */
172 int _except_handler3(PEXCEPTION_RECORD rec,
173                      MSVCRT_EXCEPTION_FRAME* frame,
174                      PCONTEXT context, void* dispatcher)
175 {
176 #if defined(__GNUC__) && defined(__i386__)
177   long retval, trylevel;
178   EXCEPTION_POINTERS exceptPtrs;
179   PSCOPETABLE pScopeTable;
180
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);
184
185   __asm__ __volatile__ ("cld");
186
187   if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
188   {
189     /* Unwinding the current frame */
190      _local_unwind2(frame, TRYLEVEL_END);
191     return ExceptionContinueSearch;
192   }
193   else
194   {
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;
201
202     while (trylevel != TRYLEVEL_END)
203     {
204       if (pScopeTable[trylevel].lpfnFilter)
205       {
206         TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
207
208         retval = pScopeTable[trylevel].lpfnFilter(&exceptPtrs);
209
210         TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
211               "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
212               "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
213
214         if (retval == EXCEPTION_CONTINUE_EXECUTION)
215           return ExceptionContinueExecution;
216
217         if (retval == EXCEPTION_EXECUTE_HANDLER)
218         {
219           /* Unwind all higher frames, this one will handle the exception */
220           _global_unwind2((PEXCEPTION_FRAME)frame);
221           _local_unwind2(frame, trylevel);
222
223           /* Set our trylevel to the enclosing block, and call the __finally
224            * code, which won't return
225            */
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");
230        }
231       }
232       trylevel = pScopeTable->previousTryLevel;
233     }
234   }
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;
241 }
242
243 /*********************************************************************
244  *              _abnormal_termination (MSVCRT.@)
245  */
246 int _abnormal_termination(void)
247 {
248   FIXME("(void)stub\n");
249   return 0;
250 }
251
252 /*
253  * setjmp/longjmp implementation
254  */
255
256 #ifdef __i386__
257 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
258 typedef void (*MSVCRT_unwind_function)(const void*);
259
260 /*
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.
264  */
265
266 /*******************************************************************
267  *              _setjmp (MSVCRT.@)
268  */
269 void _MSVCRT__setjmp(_JUMP_BUFFER *jmp, CONTEXT86* context)
270 {
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;
285 }
286
287 /*******************************************************************
288  *              _setjmp3 (MSVCRT.@)
289  */
290 void _MSVCRT__setjmp3(_JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
291 {
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)
303     {
304         jmp->TryLevel = TRYLEVEL_END;
305     }
306     else
307     {
308         void **args = ((void**)context->Esp)+2;
309
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)
314         {
315             size_t size = (nb_args - 2) * sizeof(DWORD);
316             memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
317         }
318     }
319     TRACE("returning 0\n");
320     context->Eax = 0;
321 }
322
323 /*********************************************************************
324  *              longjmp (MSVCRT.@)
325  */
326 void _MSVCRT_longjmp(_JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
327 {
328     unsigned long cur_frame = 0;
329
330     TRACE("(%p,%d)\n", jmp, retval);
331
332     cur_frame=(unsigned long)NtCurrentTeb()->except;
333     TRACE("cur_frame=%lx\n",cur_frame);
334
335     if (cur_frame != jmp->Registration)
336         _global_unwind2((PEXCEPTION_FRAME)jmp->Registration);
337
338     if (jmp->Registration)
339     {
340         if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
341             jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
342         {
343             MSVCRT_unwind_function unwind_func;
344
345             unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
346             unwind_func(jmp);
347         }
348         else
349             _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
350                            jmp->TryLevel);
351     }
352
353     if (!retval)
354         retval = 1;
355
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;
364 }
365 #endif /* i386 */
366
367 /*********************************************************************
368  *              signal (MSVCRT.@)
369  */
370 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
371 {
372   FIXME("(%d %p):stub\n", sig, func);
373   return (void*)-1;
374 }
375
376 /*********************************************************************
377  *              __CxxFrameHandler (MSVCRT.@)
378  */
379 DWORD __CxxFrameHandler(PEXCEPTION_RECORD rec, struct __EXCEPTION_FRAME* frame,
380                         PCONTEXT context, struct __EXCEPTION_FRAME** dispatch)
381 {
382     FIXME("(%p,%p,%p,%p):stub?\n",rec,frame,context,dispatch);
383
384     /* Copied from MSVCRT_nested_handler, I hope this is more 
385      * or less the right thing to do
386      */
387     if (rec->ExceptionFlags & 0x6)
388         return ExceptionContinueSearch;
389     *dispatch = frame;
390     return ExceptionCollidedUnwind;
391 }