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