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