Made exception_event_request non-blocking, and added
[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 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
41 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID, 
42                            PEXCEPTION_RECORD, DWORD, PCONTEXT );
43 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
44                                   BOOL, PCONTEXT );
45
46 /*******************************************************************
47  *         EXC_RaiseHandler
48  *
49  * Handler for exceptions happening inside a handler.
50  */
51 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
52                                CONTEXT *context, EXCEPTION_FRAME **dispatcher )
53 {
54     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
55         return ExceptionContinueSearch;
56     /* We shouldn't get here so we store faulty frame in dispatcher */
57     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
58     return ExceptionNestedException;
59 }
60
61
62 /*******************************************************************
63  *         EXC_UnwindHandler
64  *
65  * Handler for exceptions happening inside an unwind handler.
66  */
67 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
68                                 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
69 {
70     if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
71         return ExceptionContinueSearch;
72     /* We shouldn't get here so we store faulty frame in dispatcher */
73     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
74     return ExceptionCollidedUnwind;
75 }
76
77
78 /*******************************************************************
79  *         EXC_CallHandler
80  *
81  * Call an exception handler, setting up an exception frame to catch exceptions
82  * happening during the handler execution.
83  * Please do not change the first 4 parameters order in any way - some exceptions handlers
84  * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
85  */
86 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
87                               CONTEXT *context, EXCEPTION_FRAME **dispatcher, 
88                               PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
89 {
90     EXC_NESTED_FRAME newframe;
91     DWORD ret;
92
93     newframe.frame.Handler = nested_handler;
94     newframe.prevFrame     = frame;
95     __wine_push_frame( &newframe.frame );
96     TRACE( "calling handler at %p code=%lx flags=%lx\n",
97            handler, record->ExceptionCode, record->ExceptionFlags );
98     ret = handler( record, frame, context, dispatcher );
99     TRACE( "handler returned %lx\n", ret );
100     __wine_pop_frame( &newframe.frame );
101     return ret;
102 }
103
104
105 /**********************************************************************
106  *           send_debug_event
107  *
108  * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
109  */
110 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
111 {
112     int ret;
113     HANDLE handle = 0;
114
115     SERVER_START_REQ
116     {
117         struct queue_exception_event_request *req = server_alloc_req( sizeof(*req),
118                                                                       sizeof(*rec)+sizeof(*context) );
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_noerr( REQ_QUEUE_EXCEPTION_EVENT )) handle = req->handle;
125     }
126     SERVER_END_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_REQ
134     {
135         struct get_exception_status_request *req = server_alloc_req( sizeof(*req), sizeof(*context) );
136         req->handle = handle;
137         if (!server_call_noerr( REQ_GET_EXCEPTION_STATUS ))
138             *context = *(CONTEXT *)server_data_ptr(req);
139         ret = req->status;
140     }
141     SERVER_END_REQ;
142     NtClose( handle );
143     return ret;
144 }
145
146
147 /*******************************************************************
148  *         EXC_DefaultHandling
149  *
150  * Default handling for exceptions. Called when we didn't find a suitable handler.
151  */
152 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
153 {
154     if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
155
156     if (rec->ExceptionFlags & EH_STACK_INVALID)
157         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
158     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
159         ERR("Process attempted to continue execution after noncontinuable exception.\n");
160     else
161         ERR("Unhandled exception code %lx flags %lx addr %p\n",
162             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
163     NtTerminateProcess( NtCurrentProcess(), 1 );
164 }
165
166
167 /***********************************************************************
168  *            EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
169  */
170 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
171 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
172 {
173     PEXCEPTION_FRAME frame, dispatch, nested_frame;
174     EXCEPTION_RECORD newrec;
175     DWORD res;
176
177     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
178
179     if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
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 }