2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
15 #include "wine/exception.h"
16 #include "stackframe.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 /*******************************************************************
43 * Handler for exceptions happening inside a handler.
45 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
46 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
48 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
49 return ExceptionContinueSearch;
50 /* We shouldn't get here so we store faulty frame in dispatcher */
51 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
52 return ExceptionNestedException;
56 /*******************************************************************
59 * Handler for exceptions happening inside an unwind handler.
61 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
62 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
64 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
65 return ExceptionContinueSearch;
66 /* We shouldn't get here so we store faulty frame in dispatcher */
67 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
68 return ExceptionCollidedUnwind;
72 /*******************************************************************
75 * Call an exception handler, setting up an exception frame to catch exceptions
76 * happening during the handler execution.
77 * Please do not change the first 4 parameters order in any way - some exceptions handlers
78 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
80 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
81 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
82 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
84 EXC_NESTED_FRAME newframe;
87 newframe.frame.Handler = nested_handler;
88 newframe.prevFrame = frame;
89 __wine_push_frame( &newframe.frame );
90 TRACE( "calling handler at %p code=%lx flags=%lx\n",
91 handler, record->ExceptionCode, record->ExceptionFlags );
92 ret = handler( record, frame, context, dispatcher );
93 TRACE( "handler returned %lx\n", ret );
94 __wine_pop_frame( &newframe.frame );
99 /**********************************************************************
102 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
104 static inline int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
109 struct exception_event_request *req = server_alloc_req( sizeof(*req),
110 sizeof(*rec)+sizeof(*context) );
111 CONTEXT *context_ptr = server_data_ptr(req);
112 EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
113 req->first = first_chance;
115 *context_ptr = *context;
116 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *context = *context_ptr;
124 /*******************************************************************
125 * EXC_DefaultHandling
127 * Default handling for exceptions. Called when we didn't find a suitable handler.
129 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
131 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
133 if (rec->ExceptionFlags & EH_STACK_INVALID)
134 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
135 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
136 ERR("Process attempted to continue execution after noncontinuable exception.\n");
138 ERR("Unhandled exception code %lx flags %lx addr %p\n",
139 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
140 NtTerminateProcess( NtCurrentProcess(), 1 );
144 /***********************************************************************
145 * EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
147 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
148 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
150 PEXCEPTION_FRAME frame, dispatch, nested_frame;
151 EXCEPTION_RECORD newrec;
154 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
156 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
158 frame = NtCurrentTeb()->except;
160 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
162 /* Check frame address */
163 if (((void*)frame < NtCurrentTeb()->stack_low) ||
164 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
167 rec->ExceptionFlags |= EH_STACK_INVALID;
172 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
173 if (frame == nested_frame)
175 /* no longer nested */
177 rec->ExceptionFlags &= ~EH_NESTED_CALL;
182 case ExceptionContinueExecution:
183 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
184 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
185 newrec.ExceptionFlags = EH_NONCONTINUABLE;
186 newrec.ExceptionRecord = rec;
187 newrec.NumberParameters = 0;
188 RtlRaiseException( &newrec ); /* never returns */
190 case ExceptionContinueSearch:
192 case ExceptionNestedException:
193 if (nested_frame < dispatch) nested_frame = dispatch;
194 rec->ExceptionFlags |= EH_NESTED_CALL;
197 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
198 newrec.ExceptionFlags = EH_NONCONTINUABLE;
199 newrec.ExceptionRecord = rec;
200 newrec.NumberParameters = 0;
201 RtlRaiseException( &newrec ); /* never returns */
206 EXC_DefaultHandling( rec, context );
210 /*******************************************************************
211 * EXC_RtlUnwind / RtlUnwind (KERNEL32.590) (NTDLL.518)
213 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
214 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
215 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
216 PEXCEPTION_RECORD pRecord, DWORD returnEax,
219 EXCEPTION_RECORD record, newrec;
220 PEXCEPTION_FRAME frame, dispatch;
223 context->Eax = returnEax;
226 /* build an exception record, if we do not have one */
229 record.ExceptionCode = STATUS_UNWIND;
230 record.ExceptionFlags = 0;
231 record.ExceptionRecord = NULL;
232 record.ExceptionAddress = GET_IP(context);
233 record.NumberParameters = 0;
237 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
239 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
241 /* get chain of exception frames */
242 frame = NtCurrentTeb()->except;
243 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
245 /* Check frame address */
246 if (pEndFrame && (frame > pEndFrame))
248 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
249 newrec.ExceptionFlags = EH_NONCONTINUABLE;
250 newrec.ExceptionRecord = pRecord;
251 newrec.NumberParameters = 0;
252 RtlRaiseException( &newrec ); /* never returns */
254 if (((void*)frame < NtCurrentTeb()->stack_low) ||
255 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
258 newrec.ExceptionCode = STATUS_BAD_STACK;
259 newrec.ExceptionFlags = EH_NONCONTINUABLE;
260 newrec.ExceptionRecord = pRecord;
261 newrec.NumberParameters = 0;
262 RtlRaiseException( &newrec ); /* never returns */
266 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
267 frame->Handler, EXC_UnwindHandler ))
269 case ExceptionContinueSearch:
271 case ExceptionCollidedUnwind:
275 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
276 newrec.ExceptionFlags = EH_NONCONTINUABLE;
277 newrec.ExceptionRecord = pRecord;
278 newrec.NumberParameters = 0;
279 RtlRaiseException( &newrec ); /* never returns */
282 frame = __wine_pop_frame( frame );
287 /*******************************************************************
288 * EXC_NtRaiseException / NtRaiseException (NTDLL.175)
290 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
291 EXCEPTION_RECORD *, CONTEXT *, BOOL )
292 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
293 BOOL first, CONTEXT *context )
295 EXC_RtlRaiseException( rec, ctx );
300 /***********************************************************************
301 * RtlRaiseStatus (NTDLL.465)
303 * Raise an exception with ExceptionCode = status
305 void WINAPI RtlRaiseStatus( NTSTATUS status )
307 EXCEPTION_RECORD ExceptionRec;
309 ExceptionRec.ExceptionCode = status;
310 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
311 ExceptionRec.ExceptionRecord = NULL;
312 ExceptionRec.NumberParameters = 0;
313 RtlRaiseException( &ExceptionRec );
317 /*************************************************************
318 * __wine_exception_handler
320 * Exception handler for exception blocks declared in Wine code.
322 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
323 CONTEXT *context, LPVOID pdispatcher )
325 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
327 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
328 return ExceptionContinueSearch;
329 if (wine_frame->u.filter)
331 EXCEPTION_POINTERS ptrs;
332 ptrs.ExceptionRecord = record;
333 ptrs.ContextRecord = context;
334 switch(wine_frame->u.filter( &ptrs ))
336 case EXCEPTION_CONTINUE_SEARCH:
337 return ExceptionContinueSearch;
338 case EXCEPTION_CONTINUE_EXECUTION:
339 return ExceptionContinueExecution;
340 case EXCEPTION_EXECUTE_HANDLER:
343 MESSAGE( "Invalid return value from exception filter\n" );
347 /* hack to make GetExceptionCode() work in handler */
348 wine_frame->ExceptionCode = record->ExceptionCode;
349 wine_frame->ExceptionRecord = wine_frame;
351 RtlUnwind( frame, 0, record, 0 );
352 __wine_pop_frame( frame );
353 longjmp( wine_frame->jmp, 1 );
357 /*************************************************************
358 * __wine_finally_handler
360 * Exception handler for try/finally blocks declared in Wine code.
362 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
363 CONTEXT *context, LPVOID pdispatcher )
365 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
367 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
368 return ExceptionContinueSearch;
369 wine_frame->u.finally_func( FALSE );
370 return ExceptionContinueSearch;