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