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