Block SIGINT and SIGALRM in signal handlers.
[wine] / dlls / ntdll / exception.c
1 /*
2  * NT exception handling routines
3  * 
4  * Copyright 1999 Turchanov Sergey
5  * Copyright 1999 Alexandre Julliard
6  */
7
8 #include <assert.h>
9 #include <signal.h>
10
11 #include "config.h"
12 #include "winnt.h"
13 #include "ntddk.h"
14 #include "global.h"
15 #include "wine/exception.h"
16 #include "stackframe.h"
17 #include "miscemu.h"
18 #include "server.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(seh);
22
23 /* Exception record for handling exceptions happening inside exception handlers */
24 typedef struct
25 {
26     EXCEPTION_FRAME frame;
27     EXCEPTION_FRAME *prevFrame;
28 } EXC_NESTED_FRAME;
29
30 #ifdef __i386__
31 # define GET_IP(context) ((LPVOID)(context)->Eip)
32 #endif
33 #ifdef __sparc__
34 # define GET_IP(context) ((LPVOID)(context)->pc)
35 #endif
36 #ifndef GET_IP
37 # error You must define GET_IP for this CPU
38 #endif
39
40 extern void SIGNAL_Unblock(void);
41
42 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
43 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID, 
44                            PEXCEPTION_RECORD, DWORD, PCONTEXT );
45 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
46                                   BOOL, PCONTEXT );
47
48 /*******************************************************************
49  *         EXC_RaiseHandler
50  *
51  * Handler for exceptions happening inside a handler.
52  */
53 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
54                                CONTEXT *context, EXCEPTION_FRAME **dispatcher )
55 {
56     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
57         return ExceptionContinueSearch;
58     /* We shouldn't get here so we store faulty frame in dispatcher */
59     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
60     return ExceptionNestedException;
61 }
62
63
64 /*******************************************************************
65  *         EXC_UnwindHandler
66  *
67  * Handler for exceptions happening inside an unwind handler.
68  */
69 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
70                                 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
71 {
72     if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
73         return ExceptionContinueSearch;
74     /* We shouldn't get here so we store faulty frame in dispatcher */
75     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
76     return ExceptionCollidedUnwind;
77 }
78
79
80 /*******************************************************************
81  *         EXC_CallHandler
82  *
83  * Call an exception handler, setting up an exception frame to catch exceptions
84  * happening during the handler execution.
85  * Please do not change the first 4 parameters order in any way - some exceptions handlers
86  * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
87  */
88 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
89                               CONTEXT *context, EXCEPTION_FRAME **dispatcher, 
90                               PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
91 {
92     EXC_NESTED_FRAME newframe;
93     DWORD ret;
94
95     newframe.frame.Handler = nested_handler;
96     newframe.prevFrame     = frame;
97     __wine_push_frame( &newframe.frame );
98     TRACE( "calling handler at %p code=%lx flags=%lx\n",
99            handler, record->ExceptionCode, record->ExceptionFlags );
100     ret = handler( record, frame, context, dispatcher );
101     TRACE( "handler returned %lx\n", ret );
102     __wine_pop_frame( &newframe.frame );
103     return ret;
104 }
105
106
107 /**********************************************************************
108  *           send_debug_event
109  *
110  * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
111  */
112 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
113 {
114     int ret;
115     HANDLE handle = 0;
116
117     SERVER_START_VAR_REQ( queue_exception_event, sizeof(*rec)+sizeof(*context) )
118     {
119         CONTEXT *context_ptr = server_data_ptr(req);
120         EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
121         req->first   = first_chance;
122         *rec_ptr     = *rec;
123         *context_ptr = *context;
124         if (!SERVER_CALL()) handle = req->handle;
125     }
126     SERVER_END_VAR_REQ;
127     if (!handle) return 0;  /* no debugger present or other error */
128
129     /* No need to wait on the handle since the process gets suspended
130      * once the event is passed to the debugger, so when we get back
131      * here the event has been continued already.
132      */
133     SERVER_START_VAR_REQ( get_exception_status, sizeof(*context) )
134     {
135         req->handle = handle;
136         if (!SERVER_CALL()) *context = *(CONTEXT *)server_data_ptr(req);
137         ret = req->status;
138     }
139     SERVER_END_VAR_REQ;
140     NtClose( handle );
141     return ret;
142 }
143
144
145 /*******************************************************************
146  *         EXC_DefaultHandling
147  *
148  * Default handling for exceptions. Called when we didn't find a suitable handler.
149  */
150 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
151 {
152     if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
153
154     if (rec->ExceptionFlags & EH_STACK_INVALID)
155         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
156     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
157         ERR("Process attempted to continue execution after noncontinuable exception.\n");
158     else
159         ERR("Unhandled exception code %lx flags %lx addr %p\n",
160             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
161     NtTerminateProcess( NtCurrentProcess(), 1 );
162 }
163
164
165 /***********************************************************************
166  *            EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
167  */
168 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
169 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
170 {
171     PEXCEPTION_FRAME frame, dispatch, nested_frame;
172     EXCEPTION_RECORD newrec;
173     DWORD res;
174
175     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
176
177     if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
178
179     SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
180
181     frame = NtCurrentTeb()->except;
182     nested_frame = NULL;
183     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
184     {
185         /* Check frame address */
186         if (((void*)frame < NtCurrentTeb()->stack_low) ||
187             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
188             (int)frame & 3)
189         {
190             rec->ExceptionFlags |= EH_STACK_INVALID;
191             break;
192         }
193
194         /* Call handler */
195         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
196         if (frame == nested_frame)
197         {
198             /* no longer nested */
199             nested_frame = NULL;
200             rec->ExceptionFlags &= ~EH_NESTED_CALL;
201         }
202
203         switch(res)
204         {
205         case ExceptionContinueExecution:
206             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
207             newrec.ExceptionCode    = STATUS_NONCONTINUABLE_EXCEPTION;
208             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
209             newrec.ExceptionRecord  = rec;
210             newrec.NumberParameters = 0;
211             RtlRaiseException( &newrec );  /* never returns */
212             break;
213         case ExceptionContinueSearch:
214             break;
215         case ExceptionNestedException:
216             if (nested_frame < dispatch) nested_frame = dispatch;
217             rec->ExceptionFlags |= EH_NESTED_CALL;
218             break;
219         default:
220             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
221             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
222             newrec.ExceptionRecord  = rec;
223             newrec.NumberParameters = 0;
224             RtlRaiseException( &newrec );  /* never returns */
225             break;
226         }
227         frame = frame->Prev;
228     }
229     EXC_DefaultHandling( rec, context );
230 }
231
232
233 /*******************************************************************
234  *         EXC_RtlUnwind / RtlUnwind  (KERNEL32.590) (NTDLL.518)
235  */
236 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind, 
237                           PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
238 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip, 
239                            PEXCEPTION_RECORD pRecord, DWORD returnEax,
240                            CONTEXT *context )
241 {
242     EXCEPTION_RECORD record, newrec;
243     PEXCEPTION_FRAME frame, dispatch;
244
245 #ifdef __i386__
246     context->Eax = returnEax;
247 #endif
248
249     /* build an exception record, if we do not have one */
250     if (!pRecord)
251     {
252         record.ExceptionCode    = STATUS_UNWIND;
253         record.ExceptionFlags   = 0;
254         record.ExceptionRecord  = NULL;
255         record.ExceptionAddress = GET_IP(context); 
256         record.NumberParameters = 0;
257         pRecord = &record;
258     }
259
260     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
261
262     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
263
264     /* get chain of exception frames */
265     frame = NtCurrentTeb()->except;
266     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
267     {
268         /* Check frame address */
269         if (pEndFrame && (frame > pEndFrame))
270         {
271             newrec.ExceptionCode    = STATUS_INVALID_UNWIND_TARGET;
272             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
273             newrec.ExceptionRecord  = pRecord;
274             newrec.NumberParameters = 0;
275             RtlRaiseException( &newrec );  /* never returns */
276         }
277         if (((void*)frame < NtCurrentTeb()->stack_low) ||
278             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
279             (int)frame & 3)
280         {
281             newrec.ExceptionCode    = STATUS_BAD_STACK;
282             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
283             newrec.ExceptionRecord  = pRecord;
284             newrec.NumberParameters = 0;
285             RtlRaiseException( &newrec );  /* never returns */
286         }
287
288         /* Call handler */
289         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
290                                 frame->Handler, EXC_UnwindHandler ))
291         {
292         case ExceptionContinueSearch:
293             break;
294         case ExceptionCollidedUnwind:
295             frame = dispatch;
296             break;
297         default:
298             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
299             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
300             newrec.ExceptionRecord  = pRecord;
301             newrec.NumberParameters = 0;
302             RtlRaiseException( &newrec );  /* never returns */
303             break;
304         }
305         frame = __wine_pop_frame( frame );
306     }
307 }
308
309
310 /*******************************************************************
311  *            EXC_NtRaiseException / NtRaiseException  (NTDLL.175)
312  */
313 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
314                           EXCEPTION_RECORD *, CONTEXT *, BOOL )
315 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
316                                   BOOL first, CONTEXT *context )
317 {
318     EXC_RtlRaiseException( rec, ctx );
319     *context = *ctx;
320 }
321
322
323 /***********************************************************************
324  *            RtlRaiseStatus  (NTDLL.465)
325  *
326  * Raise an exception with ExceptionCode = status
327  */
328 void WINAPI RtlRaiseStatus( NTSTATUS status )
329 {
330     EXCEPTION_RECORD ExceptionRec;
331
332     ExceptionRec.ExceptionCode    = status;
333     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
334     ExceptionRec.ExceptionRecord  = NULL;
335     ExceptionRec.NumberParameters = 0;
336     RtlRaiseException( &ExceptionRec );
337 }
338
339
340 /*************************************************************
341  *            __wine_exception_handler
342  *
343  * Exception handler for exception blocks declared in Wine code.
344  */
345 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
346                                 CONTEXT *context, LPVOID pdispatcher )
347 {
348     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
349
350     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
351         return ExceptionContinueSearch;
352     if (wine_frame->u.filter)
353     {
354         EXCEPTION_POINTERS ptrs;
355         ptrs.ExceptionRecord = record;
356         ptrs.ContextRecord = context;
357         switch(wine_frame->u.filter( &ptrs ))
358         {
359         case EXCEPTION_CONTINUE_SEARCH:
360             return ExceptionContinueSearch;
361         case EXCEPTION_CONTINUE_EXECUTION:
362             return ExceptionContinueExecution;
363         case EXCEPTION_EXECUTE_HANDLER:
364             break;
365         default:
366             MESSAGE( "Invalid return value from exception filter\n" );
367             assert( FALSE );
368         }
369     }
370     /* hack to make GetExceptionCode() work in handler */
371     wine_frame->ExceptionCode   = record->ExceptionCode;
372     wine_frame->ExceptionRecord = wine_frame;
373
374     RtlUnwind( frame, 0, record, 0 );
375     __wine_pop_frame( frame );
376     longjmp( wine_frame->jmp, 1 );
377 }
378
379
380 /*************************************************************
381  *            __wine_finally_handler
382  *
383  * Exception handler for try/finally blocks declared in Wine code.
384  */
385 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
386                               CONTEXT *context, LPVOID pdispatcher )
387 {
388     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
389
390     if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
391         return ExceptionContinueSearch;
392     wine_frame->u.finally_func( FALSE );
393     return ExceptionContinueSearch;
394 }