2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
16 #include "wine/exception.h"
17 #include "stackframe.h"
19 #include "wine/server.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(seh);
24 /* Exception record for handling exceptions happening inside exception handlers */
27 EXCEPTION_FRAME frame;
28 EXCEPTION_FRAME *prevFrame;
32 # define GET_IP(context) ((LPVOID)(context)->Eip)
35 # define GET_IP(context) ((LPVOID)(context)->pc)
38 # error You must define GET_IP for this CPU
41 extern void SIGNAL_Unblock(void);
43 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
44 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
45 PEXCEPTION_RECORD, DWORD, PCONTEXT );
46 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
49 /*******************************************************************
52 * Handler for exceptions happening inside a handler.
54 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
55 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
57 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
58 return ExceptionContinueSearch;
59 /* We shouldn't get here so we store faulty frame in dispatcher */
60 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
61 return ExceptionNestedException;
65 /*******************************************************************
68 * Handler for exceptions happening inside an unwind handler.
70 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
71 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
73 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
74 return ExceptionContinueSearch;
75 /* We shouldn't get here so we store faulty frame in dispatcher */
76 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
77 return ExceptionCollidedUnwind;
81 /*******************************************************************
84 * Call an exception handler, setting up an exception frame to catch exceptions
85 * happening during the handler execution.
86 * Please do not change the first 4 parameters order in any way - some exceptions handlers
87 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
89 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
90 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
91 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
93 EXC_NESTED_FRAME newframe;
96 newframe.frame.Handler = nested_handler;
97 newframe.prevFrame = frame;
98 __wine_push_frame( &newframe.frame );
99 TRACE( "calling handler at %p code=%lx flags=%lx\n",
100 handler, record->ExceptionCode, record->ExceptionFlags );
101 ret = handler( record, frame, context, dispatcher );
102 TRACE( "handler returned %lx\n", ret );
103 __wine_pop_frame( &newframe.frame );
108 /**********************************************************************
111 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
113 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
118 SERVER_START_VAR_REQ( queue_exception_event, sizeof(*rec)+sizeof(*context) )
120 CONTEXT *context_ptr = server_data_ptr(req);
121 EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
122 req->first = first_chance;
124 *context_ptr = *context;
125 if (!SERVER_CALL()) handle = req->handle;
128 if (!handle) return 0; /* no debugger present or other error */
130 /* No need to wait on the handle since the process gets suspended
131 * once the event is passed to the debugger, so when we get back
132 * here the event has been continued already.
134 SERVER_START_VAR_REQ( get_exception_status, sizeof(*context) )
136 req->handle = handle;
137 if (!SERVER_CALL()) *context = *(CONTEXT *)server_data_ptr(req);
146 /*******************************************************************
147 * EXC_DefaultHandling
149 * Default handling for exceptions. Called when we didn't find a suitable handler.
151 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
153 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
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");
160 ERR("Unhandled exception code %lx flags %lx addr %p\n",
161 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
162 NtTerminateProcess( NtCurrentProcess(), 1 );
166 /***********************************************************************
167 * RtlRaiseException (NTDLL.@)
169 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
170 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
172 PEXCEPTION_FRAME frame, dispatch, nested_frame;
173 EXCEPTION_RECORD newrec;
176 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
178 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
180 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
182 frame = NtCurrentTeb()->except;
184 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
186 /* Check frame address */
187 if (((void*)frame < NtCurrentTeb()->stack_low) ||
188 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
191 rec->ExceptionFlags |= EH_STACK_INVALID;
196 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
197 if (frame == nested_frame)
199 /* no longer nested */
201 rec->ExceptionFlags &= ~EH_NESTED_CALL;
206 case ExceptionContinueExecution:
207 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
208 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
209 newrec.ExceptionFlags = EH_NONCONTINUABLE;
210 newrec.ExceptionRecord = rec;
211 newrec.NumberParameters = 0;
212 RtlRaiseException( &newrec ); /* never returns */
214 case ExceptionContinueSearch:
216 case ExceptionNestedException:
217 if (nested_frame < dispatch) nested_frame = dispatch;
218 rec->ExceptionFlags |= EH_NESTED_CALL;
221 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
222 newrec.ExceptionFlags = EH_NONCONTINUABLE;
223 newrec.ExceptionRecord = rec;
224 newrec.NumberParameters = 0;
225 RtlRaiseException( &newrec ); /* never returns */
230 EXC_DefaultHandling( rec, context );
234 /*******************************************************************
235 * RtlUnwind (NTDLL.@)
237 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
238 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD );
239 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
240 PEXCEPTION_RECORD pRecord, DWORD returnEax,
243 EXCEPTION_RECORD record, newrec;
244 PEXCEPTION_FRAME frame, dispatch;
247 context->Eax = returnEax;
250 /* build an exception record, if we do not have one */
253 record.ExceptionCode = STATUS_UNWIND;
254 record.ExceptionFlags = 0;
255 record.ExceptionRecord = NULL;
256 record.ExceptionAddress = GET_IP(context);
257 record.NumberParameters = 0;
261 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
263 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
265 /* get chain of exception frames */
266 frame = NtCurrentTeb()->except;
267 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
269 /* Check frame address */
270 if (pEndFrame && (frame > pEndFrame))
272 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
273 newrec.ExceptionFlags = EH_NONCONTINUABLE;
274 newrec.ExceptionRecord = pRecord;
275 newrec.NumberParameters = 0;
276 RtlRaiseException( &newrec ); /* never returns */
278 if (((void*)frame < NtCurrentTeb()->stack_low) ||
279 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
282 newrec.ExceptionCode = STATUS_BAD_STACK;
283 newrec.ExceptionFlags = EH_NONCONTINUABLE;
284 newrec.ExceptionRecord = pRecord;
285 newrec.NumberParameters = 0;
286 RtlRaiseException( &newrec ); /* never returns */
290 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
291 frame->Handler, EXC_UnwindHandler ))
293 case ExceptionContinueSearch:
295 case ExceptionCollidedUnwind:
299 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
300 newrec.ExceptionFlags = EH_NONCONTINUABLE;
301 newrec.ExceptionRecord = pRecord;
302 newrec.NumberParameters = 0;
303 RtlRaiseException( &newrec ); /* never returns */
306 frame = __wine_pop_frame( frame );
311 /*******************************************************************
312 * NtRaiseException (NTDLL.@)
314 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
315 EXCEPTION_RECORD *, CONTEXT *, BOOL );
316 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
317 BOOL first, CONTEXT *context )
319 EXC_RtlRaiseException( rec, ctx );
324 /***********************************************************************
325 * RtlRaiseStatus (NTDLL.@)
327 * Raise an exception with ExceptionCode = status
329 void WINAPI RtlRaiseStatus( NTSTATUS status )
331 EXCEPTION_RECORD ExceptionRec;
333 ExceptionRec.ExceptionCode = status;
334 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
335 ExceptionRec.ExceptionRecord = NULL;
336 ExceptionRec.NumberParameters = 0;
337 RtlRaiseException( &ExceptionRec );
341 /*************************************************************
342 * __wine_exception_handler (NTDLL.@)
344 * Exception handler for exception blocks declared in Wine code.
346 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
347 CONTEXT *context, LPVOID pdispatcher )
349 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
351 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
352 return ExceptionContinueSearch;
353 if (wine_frame->u.filter)
355 EXCEPTION_POINTERS ptrs;
356 ptrs.ExceptionRecord = record;
357 ptrs.ContextRecord = context;
358 switch(wine_frame->u.filter( &ptrs ))
360 case EXCEPTION_CONTINUE_SEARCH:
361 return ExceptionContinueSearch;
362 case EXCEPTION_CONTINUE_EXECUTION:
363 return ExceptionContinueExecution;
364 case EXCEPTION_EXECUTE_HANDLER:
367 MESSAGE( "Invalid return value from exception filter\n" );
371 /* hack to make GetExceptionCode() work in handler */
372 wine_frame->ExceptionCode = record->ExceptionCode;
373 wine_frame->ExceptionRecord = wine_frame;
375 RtlUnwind( frame, 0, record, 0 );
376 __wine_pop_frame( frame );
377 longjmp( wine_frame->jmp, 1 );
381 /*************************************************************
382 * __wine_finally_handler (NTDLL.@)
384 * Exception handler for try/finally blocks declared in Wine code.
386 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
387 CONTEXT *context, LPVOID pdispatcher )
389 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
391 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
392 return ExceptionContinueSearch;
393 wine_frame->u.finally_func( FALSE );
394 return ExceptionContinueSearch;