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