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