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