Added missing prototypes to avoid compile warnings on Solaris.
[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 inline int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
111 {
112     int ret;
113     SERVER_START_REQ
114     {
115         struct exception_event_request *req = server_alloc_req( sizeof(*req),
116                                                                 sizeof(*rec)+sizeof(*context) );
117         CONTEXT *context_ptr = server_data_ptr(req);
118         EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
119         req->first   = first_chance;
120         *rec_ptr     = *rec;
121         *context_ptr = *context;
122         if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *context = *context_ptr;
123         ret = req->status;
124     }
125     SERVER_END_REQ;
126     return ret;
127 }
128
129
130 /*******************************************************************
131  *         EXC_DefaultHandling
132  *
133  * Default handling for exceptions. Called when we didn't find a suitable handler.
134  */
135 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
136 {
137     if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
138
139     if (rec->ExceptionFlags & EH_STACK_INVALID)
140         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
141     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
142         ERR("Process attempted to continue execution after noncontinuable exception.\n");
143     else
144         ERR("Unhandled exception code %lx flags %lx addr %p\n",
145             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
146     NtTerminateProcess( NtCurrentProcess(), 1 );
147 }
148
149
150 /***********************************************************************
151  *            EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
152  */
153 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
154 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
155 {
156     PEXCEPTION_FRAME frame, dispatch, nested_frame;
157     EXCEPTION_RECORD newrec;
158     DWORD res;
159
160     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
161
162     if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
163
164     frame = NtCurrentTeb()->except;
165     nested_frame = NULL;
166     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
167     {
168         /* Check frame address */
169         if (((void*)frame < NtCurrentTeb()->stack_low) ||
170             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
171             (int)frame & 3)
172         {
173             rec->ExceptionFlags |= EH_STACK_INVALID;
174             break;
175         }
176
177         /* Call handler */
178         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
179         if (frame == nested_frame)
180         {
181             /* no longer nested */
182             nested_frame = NULL;
183             rec->ExceptionFlags &= ~EH_NESTED_CALL;
184         }
185
186         switch(res)
187         {
188         case ExceptionContinueExecution:
189             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
190             newrec.ExceptionCode    = STATUS_NONCONTINUABLE_EXCEPTION;
191             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
192             newrec.ExceptionRecord  = rec;
193             newrec.NumberParameters = 0;
194             RtlRaiseException( &newrec );  /* never returns */
195             break;
196         case ExceptionContinueSearch:
197             break;
198         case ExceptionNestedException:
199             if (nested_frame < dispatch) nested_frame = dispatch;
200             rec->ExceptionFlags |= EH_NESTED_CALL;
201             break;
202         default:
203             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
204             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
205             newrec.ExceptionRecord  = rec;
206             newrec.NumberParameters = 0;
207             RtlRaiseException( &newrec );  /* never returns */
208             break;
209         }
210         frame = frame->Prev;
211     }
212     EXC_DefaultHandling( rec, context );
213 }
214
215
216 /*******************************************************************
217  *         EXC_RtlUnwind / RtlUnwind  (KERNEL32.590) (NTDLL.518)
218  */
219 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind, 
220                           PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
221 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip, 
222                            PEXCEPTION_RECORD pRecord, DWORD returnEax,
223                            CONTEXT *context )
224 {
225     EXCEPTION_RECORD record, newrec;
226     PEXCEPTION_FRAME frame, dispatch;
227
228 #ifdef __i386__
229     context->Eax = returnEax;
230 #endif
231
232     /* build an exception record, if we do not have one */
233     if (!pRecord)
234     {
235         record.ExceptionCode    = STATUS_UNWIND;
236         record.ExceptionFlags   = 0;
237         record.ExceptionRecord  = NULL;
238         record.ExceptionAddress = GET_IP(context); 
239         record.NumberParameters = 0;
240         pRecord = &record;
241     }
242
243     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
244
245     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
246
247     /* get chain of exception frames */
248     frame = NtCurrentTeb()->except;
249     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
250     {
251         /* Check frame address */
252         if (pEndFrame && (frame > pEndFrame))
253         {
254             newrec.ExceptionCode    = STATUS_INVALID_UNWIND_TARGET;
255             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
256             newrec.ExceptionRecord  = pRecord;
257             newrec.NumberParameters = 0;
258             RtlRaiseException( &newrec );  /* never returns */
259         }
260         if (((void*)frame < NtCurrentTeb()->stack_low) ||
261             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
262             (int)frame & 3)
263         {
264             newrec.ExceptionCode    = STATUS_BAD_STACK;
265             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
266             newrec.ExceptionRecord  = pRecord;
267             newrec.NumberParameters = 0;
268             RtlRaiseException( &newrec );  /* never returns */
269         }
270
271         /* Call handler */
272         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
273                                 frame->Handler, EXC_UnwindHandler ))
274         {
275         case ExceptionContinueSearch:
276             break;
277         case ExceptionCollidedUnwind:
278             frame = dispatch;
279             break;
280         default:
281             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
282             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
283             newrec.ExceptionRecord  = pRecord;
284             newrec.NumberParameters = 0;
285             RtlRaiseException( &newrec );  /* never returns */
286             break;
287         }
288         frame = __wine_pop_frame( frame );
289     }
290 }
291
292
293 /*******************************************************************
294  *            EXC_NtRaiseException / NtRaiseException  (NTDLL.175)
295  */
296 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
297                           EXCEPTION_RECORD *, CONTEXT *, BOOL )
298 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
299                                   BOOL first, CONTEXT *context )
300 {
301     EXC_RtlRaiseException( rec, ctx );
302     *context = *ctx;
303 }
304
305
306 /***********************************************************************
307  *            RtlRaiseStatus  (NTDLL.465)
308  *
309  * Raise an exception with ExceptionCode = status
310  */
311 void WINAPI RtlRaiseStatus( NTSTATUS status )
312 {
313     EXCEPTION_RECORD ExceptionRec;
314
315     ExceptionRec.ExceptionCode    = status;
316     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
317     ExceptionRec.ExceptionRecord  = NULL;
318     ExceptionRec.NumberParameters = 0;
319     RtlRaiseException( &ExceptionRec );
320 }
321
322
323 /*************************************************************
324  *            __wine_exception_handler
325  *
326  * Exception handler for exception blocks declared in Wine code.
327  */
328 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
329                                 CONTEXT *context, LPVOID pdispatcher )
330 {
331     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
332
333     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
334         return ExceptionContinueSearch;
335     if (wine_frame->u.filter)
336     {
337         EXCEPTION_POINTERS ptrs;
338         ptrs.ExceptionRecord = record;
339         ptrs.ContextRecord = context;
340         switch(wine_frame->u.filter( &ptrs ))
341         {
342         case EXCEPTION_CONTINUE_SEARCH:
343             return ExceptionContinueSearch;
344         case EXCEPTION_CONTINUE_EXECUTION:
345             return ExceptionContinueExecution;
346         case EXCEPTION_EXECUTE_HANDLER:
347             break;
348         default:
349             MESSAGE( "Invalid return value from exception filter\n" );
350             assert( FALSE );
351         }
352     }
353     /* hack to make GetExceptionCode() work in handler */
354     wine_frame->ExceptionCode   = record->ExceptionCode;
355     wine_frame->ExceptionRecord = wine_frame;
356
357     RtlUnwind( frame, 0, record, 0 );
358     __wine_pop_frame( frame );
359     longjmp( wine_frame->jmp, 1 );
360 }
361
362
363 /*************************************************************
364  *            __wine_finally_handler
365  *
366  * Exception handler for try/finally blocks declared in Wine code.
367  */
368 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
369                               CONTEXT *context, LPVOID pdispatcher )
370 {
371     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
372
373     if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
374         return ExceptionContinueSearch;
375     wine_frame->u.finally_func( FALSE );
376     return ExceptionContinueSearch;
377 }