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