Fixed some issues found by winapi_check.
[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
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include "winternl.h"
32 #include "wine/exception.h"
33 #include "thread.h"
34 #include "msvcrt.h"
35
36 #include "msvcrt/setjmp.h"
37 #include "excpt.h"
38
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
43
44 typedef void (*MSVCRT_sig_handler_func)(void);
45
46 /* VC++ extensions to Win32 SEH */
47 typedef struct _SCOPETABLE
48 {
49   int previousTryLevel;
50   int (*lpfnFilter)(PEXCEPTION_POINTERS);
51   int (*lpfnHandler)(void);
52 } SCOPETABLE, *PSCOPETABLE;
53
54 typedef struct _MSVCRT_EXCEPTION_FRAME
55 {
56   EXCEPTION_FRAME *prev;
57   void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_FRAME,
58                   PCONTEXT, PEXCEPTION_RECORD);
59   PSCOPETABLE scopetable;
60   int trylevel;
61   int _ebp;
62   PEXCEPTION_POINTERS xpointers;
63 } MSVCRT_EXCEPTION_FRAME;
64
65 #define TRYLEVEL_END (-1) /* End of trylevel list */
66
67 #if defined(__GNUC__) && defined(__i386__)
68 inline static void call_finally_block( void *code_block, void *base_ptr )
69 {
70     __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
71                           : : "a" (code_block), "g" (base_ptr));
72 }
73
74 inline static DWORD call_filter( void *func, void *arg, void *ebp )
75 {
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;
82 }
83 #endif
84
85 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
86                                    struct __EXCEPTION_FRAME* frame,
87                                    PCONTEXT context WINE_UNUSED,
88                                    struct __EXCEPTION_FRAME** dispatch)
89 {
90   if (rec->ExceptionFlags & 0x6)
91     return ExceptionContinueSearch;
92   *dispatch = frame;
93   return ExceptionCollidedUnwind;
94 }
95
96
97 /*********************************************************************
98  *              _XcptFilter (MSVCRT.@)
99  */
100 int _XcptFilter(int ex, PEXCEPTION_POINTERS ptr)
101 {
102   FIXME("(%d,%p)semi-stub\n", ex, ptr);
103   return UnhandledExceptionFilter(ptr);
104 }
105
106 /*********************************************************************
107  *              _EH_prolog (MSVCRT.@)
108  */
109 #ifdef __i386__
110 /* Provided for VC++ binary compatability 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
122
123 /*******************************************************************
124  *              _global_unwind2 (MSVCRT.@)
125  */
126 void _global_unwind2(PEXCEPTION_FRAME frame)
127 {
128     TRACE("(%p)\n",frame);
129     RtlUnwind( frame, 0, 0, 0 );
130 }
131
132 /*******************************************************************
133  *              _local_unwind2 (MSVCRT.@)
134  */
135 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
136 {
137   MSVCRT_EXCEPTION_FRAME *curframe = frame;
138   EXCEPTION_FRAME reg;
139
140   TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
141
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(&reg);
146
147   while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
148   {
149     int curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
150     curframe = frame;
151     curframe->trylevel = curtrylevel;
152     if (!frame->scopetable[curtrylevel].lpfnFilter)
153     {
154       ERR("__try block cleanup not implemented - expect crash!\n");
155       /* FIXME: Remove current frame, set ebp, call
156        * frame->scopetable[curtrylevel].lpfnHandler()
157        */
158     }
159   }
160   __wine_pop_frame(&reg);
161   TRACE("unwound OK\n");
162 }
163
164 /*********************************************************************
165  *              _except_handler2 (MSVCRT.@)
166  */
167 int _except_handler2(PEXCEPTION_RECORD rec,
168                      PEXCEPTION_FRAME frame,
169                      PCONTEXT context,
170                      PEXCEPTION_FRAME* dispatcher)
171 {
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;
176 }
177
178 /*********************************************************************
179  *              _except_handler3 (MSVCRT.@)
180  */
181 int _except_handler3(PEXCEPTION_RECORD rec,
182                      MSVCRT_EXCEPTION_FRAME* frame,
183                      PCONTEXT context, void* dispatcher)
184 {
185 #if defined(__GNUC__) && defined(__i386__)
186   long retval;
187   int trylevel;
188   EXCEPTION_POINTERS exceptPtrs;
189   PSCOPETABLE pScopeTable;
190
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);
194
195   __asm__ __volatile__ ("cld");
196
197   if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
198   {
199     /* Unwinding the current frame */
200      _local_unwind2(frame, TRYLEVEL_END);
201     return ExceptionContinueSearch;
202   }
203   else
204   {
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;
211
212     while (trylevel != TRYLEVEL_END)
213     {
214       if (pScopeTable[trylevel].lpfnFilter)
215       {
216         TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
217
218         retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
219
220         TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
221               "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
222               "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
223
224         if (retval == EXCEPTION_CONTINUE_EXECUTION)
225           return ExceptionContinueExecution;
226
227         if (retval == EXCEPTION_EXECUTE_HANDLER)
228         {
229           /* Unwind all higher frames, this one will handle the exception */
230           _global_unwind2((PEXCEPTION_FRAME)frame);
231           _local_unwind2(frame, trylevel);
232
233           /* Set our trylevel to the enclosing block, and call the __finally
234            * code, which won't return
235            */
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");
240        }
241       }
242       trylevel = pScopeTable->previousTryLevel;
243     }
244   }
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;
251 }
252
253 /*********************************************************************
254  *              _abnormal_termination (MSVCRT.@)
255  */
256 int _abnormal_termination(void)
257 {
258   FIXME("(void)stub\n");
259   return 0;
260 }
261
262 /*
263  * setjmp/longjmp implementation
264  */
265
266 #ifdef __i386__
267 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
268 typedef void (*MSVCRT_unwind_function)(const void*);
269
270 /*
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.
274  */
275
276 /*******************************************************************
277  *              _setjmp (MSVCRT.@)
278  */
279 void _MSVCRT__setjmp(_JUMP_BUFFER *jmp, CONTEXT86* context)
280 {
281     TRACE("(%p)\n",jmp);
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;
291     else
292         jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
293     TRACE("returning 0\n");
294     context->Eax=0;
295 }
296
297 /*******************************************************************
298  *              _setjmp3 (MSVCRT.@)
299  */
300 void _MSVCRT__setjmp3(_JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
301 {
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;
310     jmp->UnwindFunc = 0;
311     jmp->Registration = (unsigned long)NtCurrentTeb()->except;
312     if (jmp->Registration == TRYLEVEL_END)
313     {
314         jmp->TryLevel = TRYLEVEL_END;
315     }
316     else
317     {
318         void **args = ((void**)context->Esp)+2;
319
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;
323         if (nb_args > 2)
324         {
325             size_t size = (nb_args - 2) * sizeof(DWORD);
326             memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
327         }
328     }
329     TRACE("returning 0\n");
330     context->Eax = 0;
331 }
332
333 /*********************************************************************
334  *              longjmp (MSVCRT.@)
335  */
336 void _MSVCRT_longjmp(_JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
337 {
338     unsigned long cur_frame = 0;
339
340     TRACE("(%p,%d)\n", jmp, retval);
341
342     cur_frame=(unsigned long)NtCurrentTeb()->except;
343     TRACE("cur_frame=%lx\n",cur_frame);
344
345     if (cur_frame != jmp->Registration)
346         _global_unwind2((PEXCEPTION_FRAME)jmp->Registration);
347
348     if (jmp->Registration)
349     {
350         if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
351             jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
352         {
353             MSVCRT_unwind_function unwind_func;
354
355             unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
356             unwind_func(jmp);
357         }
358         else
359             _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
360                            jmp->TryLevel);
361     }
362
363     if (!retval)
364         retval = 1;
365
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;
374 }
375
376 /*********************************************************************
377  *              _seh_longjmp_unwind (MSVCRT.@)
378  */
379 void __stdcall _seh_longjmp_unwind(_JUMP_BUFFER *jmp)
380 {
381     _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
382 }
383 #endif /* i386 */
384
385 /*********************************************************************
386  *              signal (MSVCRT.@)
387  */
388 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
389 {
390   FIXME("(%d %p):stub\n", sig, func);
391   return (void*)-1;
392 }