2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
15 #include "wine/exception.h"
16 #include "stackframe.h"
18 #include "wine/server.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(seh);
23 /* Exception record for handling exceptions happening inside exception handlers */
26 EXCEPTION_FRAME frame;
27 EXCEPTION_FRAME *prevFrame;
31 # define GET_IP(context) ((LPVOID)(context)->Eip)
34 # define GET_IP(context) ((LPVOID)(context)->pc)
37 # error You must define GET_IP for this CPU
40 extern void SIGNAL_Unblock(void);
42 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
43 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
44 PEXCEPTION_RECORD, DWORD, PCONTEXT );
45 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
48 /*******************************************************************
51 * Handler for exceptions happening inside a handler.
53 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
54 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
56 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
57 return ExceptionContinueSearch;
58 /* We shouldn't get here so we store faulty frame in dispatcher */
59 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
60 return ExceptionNestedException;
64 /*******************************************************************
67 * Handler for exceptions happening inside an unwind handler.
69 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
70 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
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 ExceptionCollidedUnwind;
80 /*******************************************************************
83 * Call an exception handler, setting up an exception frame to catch exceptions
84 * happening during the handler execution.
85 * Please do not change the first 4 parameters order in any way - some exceptions handlers
86 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
88 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
89 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
90 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
92 EXC_NESTED_FRAME newframe;
95 newframe.frame.Handler = nested_handler;
96 newframe.prevFrame = frame;
97 __wine_push_frame( &newframe.frame );
98 TRACE( "calling handler at %p code=%lx flags=%lx\n",
99 handler, record->ExceptionCode, record->ExceptionFlags );
100 ret = handler( record, frame, context, dispatcher );
101 TRACE( "handler returned %lx\n", ret );
102 __wine_pop_frame( &newframe.frame );
107 /**********************************************************************
110 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
112 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
117 SERVER_START_VAR_REQ( queue_exception_event, sizeof(*rec)+sizeof(*context) )
119 CONTEXT *context_ptr = server_data_ptr(req);
120 EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
121 req->first = first_chance;
123 *context_ptr = *context;
124 if (!SERVER_CALL()) handle = req->handle;
127 if (!handle) return 0; /* no debugger present or other error */
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.
133 SERVER_START_VAR_REQ( get_exception_status, sizeof(*context) )
135 req->handle = handle;
136 if (!SERVER_CALL()) *context = *(CONTEXT *)server_data_ptr(req);
145 /*******************************************************************
146 * EXC_DefaultHandling
148 * Default handling for exceptions. Called when we didn't find a suitable handler.
150 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
152 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
154 if (rec->ExceptionFlags & EH_STACK_INVALID)
155 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
156 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
157 ERR("Process attempted to continue execution after noncontinuable exception.\n");
159 ERR("Unhandled exception code %lx flags %lx addr %p\n",
160 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
161 NtTerminateProcess( NtCurrentProcess(), 1 );
165 /***********************************************************************
166 * RtlRaiseException (NTDLL.@)
168 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
169 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
171 PEXCEPTION_FRAME frame, dispatch, nested_frame;
172 EXCEPTION_RECORD newrec;
175 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
177 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
179 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
181 frame = NtCurrentTeb()->except;
183 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
185 /* Check frame address */
186 if (((void*)frame < NtCurrentTeb()->stack_low) ||
187 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
190 rec->ExceptionFlags |= EH_STACK_INVALID;
195 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
196 if (frame == nested_frame)
198 /* no longer nested */
200 rec->ExceptionFlags &= ~EH_NESTED_CALL;
205 case ExceptionContinueExecution:
206 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
207 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
208 newrec.ExceptionFlags = EH_NONCONTINUABLE;
209 newrec.ExceptionRecord = rec;
210 newrec.NumberParameters = 0;
211 RtlRaiseException( &newrec ); /* never returns */
213 case ExceptionContinueSearch:
215 case ExceptionNestedException:
216 if (nested_frame < dispatch) nested_frame = dispatch;
217 rec->ExceptionFlags |= EH_NESTED_CALL;
220 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
221 newrec.ExceptionFlags = EH_NONCONTINUABLE;
222 newrec.ExceptionRecord = rec;
223 newrec.NumberParameters = 0;
224 RtlRaiseException( &newrec ); /* never returns */
229 EXC_DefaultHandling( rec, context );
233 /*******************************************************************
234 * RtlUnwind (NTDLL.@)
236 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
237 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
238 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
239 PEXCEPTION_RECORD pRecord, DWORD returnEax,
242 EXCEPTION_RECORD record, newrec;
243 PEXCEPTION_FRAME frame, dispatch;
246 context->Eax = returnEax;
249 /* build an exception record, if we do not have one */
252 record.ExceptionCode = STATUS_UNWIND;
253 record.ExceptionFlags = 0;
254 record.ExceptionRecord = NULL;
255 record.ExceptionAddress = GET_IP(context);
256 record.NumberParameters = 0;
260 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
262 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
264 /* get chain of exception frames */
265 frame = NtCurrentTeb()->except;
266 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
268 /* Check frame address */
269 if (pEndFrame && (frame > pEndFrame))
271 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
272 newrec.ExceptionFlags = EH_NONCONTINUABLE;
273 newrec.ExceptionRecord = pRecord;
274 newrec.NumberParameters = 0;
275 RtlRaiseException( &newrec ); /* never returns */
277 if (((void*)frame < NtCurrentTeb()->stack_low) ||
278 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
281 newrec.ExceptionCode = STATUS_BAD_STACK;
282 newrec.ExceptionFlags = EH_NONCONTINUABLE;
283 newrec.ExceptionRecord = pRecord;
284 newrec.NumberParameters = 0;
285 RtlRaiseException( &newrec ); /* never returns */
289 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
290 frame->Handler, EXC_UnwindHandler ))
292 case ExceptionContinueSearch:
294 case ExceptionCollidedUnwind:
298 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
299 newrec.ExceptionFlags = EH_NONCONTINUABLE;
300 newrec.ExceptionRecord = pRecord;
301 newrec.NumberParameters = 0;
302 RtlRaiseException( &newrec ); /* never returns */
305 frame = __wine_pop_frame( frame );
310 /*******************************************************************
311 * NtRaiseException (NTDLL.@)
313 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
314 EXCEPTION_RECORD *, CONTEXT *, BOOL )
315 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
316 BOOL first, CONTEXT *context )
318 EXC_RtlRaiseException( rec, ctx );
323 /***********************************************************************
324 * RtlRaiseStatus (NTDLL.@)
326 * Raise an exception with ExceptionCode = status
328 void WINAPI RtlRaiseStatus( NTSTATUS status )
330 EXCEPTION_RECORD ExceptionRec;
332 ExceptionRec.ExceptionCode = status;
333 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
334 ExceptionRec.ExceptionRecord = NULL;
335 ExceptionRec.NumberParameters = 0;
336 RtlRaiseException( &ExceptionRec );
340 /*************************************************************
341 * __wine_exception_handler (NTDLL.@)
343 * Exception handler for exception blocks declared in Wine code.
345 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
346 CONTEXT *context, LPVOID pdispatcher )
348 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
350 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
351 return ExceptionContinueSearch;
352 if (wine_frame->u.filter)
354 EXCEPTION_POINTERS ptrs;
355 ptrs.ExceptionRecord = record;
356 ptrs.ContextRecord = context;
357 switch(wine_frame->u.filter( &ptrs ))
359 case EXCEPTION_CONTINUE_SEARCH:
360 return ExceptionContinueSearch;
361 case EXCEPTION_CONTINUE_EXECUTION:
362 return ExceptionContinueExecution;
363 case EXCEPTION_EXECUTE_HANDLER:
366 MESSAGE( "Invalid return value from exception filter\n" );
370 /* hack to make GetExceptionCode() work in handler */
371 wine_frame->ExceptionCode = record->ExceptionCode;
372 wine_frame->ExceptionRecord = wine_frame;
374 RtlUnwind( frame, 0, record, 0 );
375 __wine_pop_frame( frame );
376 longjmp( wine_frame->jmp, 1 );
380 /*************************************************************
381 * __wine_finally_handler (NTDLL.@)
383 * Exception handler for try/finally blocks declared in Wine code.
385 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
386 CONTEXT *context, LPVOID pdispatcher )
388 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
390 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
391 return ExceptionContinueSearch;
392 wine_frame->u.finally_func( FALSE );
393 return ExceptionContinueSearch;