Correctly use the returned value from GetTimeZoneInformation.
[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 <stdarg.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winternl.h"
37 #include "wine/exception.h"
38 #include "msvcrt.h"
39 #include "excpt.h"
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_REGISTRATION_RECORD *prev;
57   void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
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                                    EXCEPTION_REGISTRATION_RECORD* frame,
87                                    PCONTEXT context,
88                                    EXCEPTION_REGISTRATION_RECORD** 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 compatibility 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(EXCEPTION_REGISTRATION_RECORD* 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_REGISTRATION_RECORD 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()->Tib.ExceptionList;
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                      EXCEPTION_REGISTRATION_RECORD* frame,
169                      PCONTEXT context,
170                      EXCEPTION_REGISTRATION_RECORD** 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((EXCEPTION_REGISTRATION_RECORD*)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 DEFINE_REGS_ENTRYPOINT( MSVCRT__setjmp, _MSVCRT__setjmp, 4, 0 );
280 void _MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp, CONTEXT86* context)
281 {
282     TRACE("(%p)\n",jmp);
283     jmp->Ebp = context->Ebp;
284     jmp->Ebx = context->Ebx;
285     jmp->Edi = context->Edi;
286     jmp->Esi = context->Esi;
287     jmp->Esp = context->Esp;
288     jmp->Eip = context->Eip;
289     jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
290     if (jmp->Registration == TRYLEVEL_END)
291         jmp->TryLevel = TRYLEVEL_END;
292     else
293         jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
294     TRACE("returning 0\n");
295     context->Eax=0;
296 }
297
298 /*******************************************************************
299  *              _setjmp3 (MSVCRT.@)
300  */
301 DEFINE_REGS_ENTRYPOINT( MSVCRT__setjmp3, _MSVCRT__setjmp3, 8, 0 );
302 void _MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
303 {
304     TRACE("(%p,%d)\n",jmp,nb_args);
305     jmp->Ebp = context->Ebp;
306     jmp->Ebx = context->Ebx;
307     jmp->Edi = context->Edi;
308     jmp->Esi = context->Esi;
309     jmp->Esp = context->Esp;
310     jmp->Eip = context->Eip;
311     jmp->Cookie = MSVCRT_JMP_MAGIC;
312     jmp->UnwindFunc = 0;
313     jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
314     if (jmp->Registration == TRYLEVEL_END)
315     {
316         jmp->TryLevel = TRYLEVEL_END;
317     }
318     else
319     {
320         void **args = ((void**)context->Esp)+2;
321
322         if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
323         if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
324         else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
325         if (nb_args > 2)
326         {
327             size_t size = (nb_args - 2) * sizeof(DWORD);
328             memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
329         }
330     }
331     TRACE("returning 0\n");
332     context->Eax = 0;
333 }
334
335 /*********************************************************************
336  *              longjmp (MSVCRT.@)
337  */
338 DEFINE_REGS_ENTRYPOINT( MSVCRT_longjmp, _MSVCRT_longjmp, 8, 0 );
339 void _MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
340 {
341     unsigned long cur_frame = 0;
342
343     TRACE("(%p,%d)\n", jmp, retval);
344
345     cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
346     TRACE("cur_frame=%lx\n",cur_frame);
347
348     if (cur_frame != jmp->Registration)
349         _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
350
351     if (jmp->Registration)
352     {
353         if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
354             jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
355         {
356             MSVCRT_unwind_function unwind_func;
357
358             unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
359             unwind_func(jmp);
360         }
361         else
362             _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
363                            jmp->TryLevel);
364     }
365
366     if (!retval)
367         retval = 1;
368
369     TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
370     context->Ebp = jmp->Ebp;
371     context->Ebx = jmp->Ebx;
372     context->Edi = jmp->Edi;
373     context->Esi = jmp->Esi;
374     context->Esp = jmp->Esp;
375     context->Eip = jmp->Eip;
376     context->Eax = retval;
377 }
378
379 /*********************************************************************
380  *              _seh_longjmp_unwind (MSVCRT.@)
381  */
382 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
383 {
384     _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
385 }
386 #endif /* i386 */
387
388 /*********************************************************************
389  *              signal (MSVCRT.@)
390  */
391 void* MSVCRT_signal(int sig, MSVCRT_sig_handler_func func)
392 {
393   FIXME("(%d %p):stub\n", sig, func);
394   return (void*)-1;
395 }