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