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 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
41 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
42 PEXCEPTION_RECORD, DWORD, PCONTEXT );
43 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
46 /*******************************************************************
49 * Handler for exceptions happening inside a handler.
51 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
52 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
54 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
55 return ExceptionContinueSearch;
56 /* We shouldn't get here so we store faulty frame in dispatcher */
57 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
58 return ExceptionNestedException;
62 /*******************************************************************
65 * Handler for exceptions happening inside an unwind handler.
67 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
68 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
70 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
71 return ExceptionContinueSearch;
72 /* We shouldn't get here so we store faulty frame in dispatcher */
73 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
74 return ExceptionCollidedUnwind;
78 /*******************************************************************
81 * Call an exception handler, setting up an exception frame to catch exceptions
82 * happening during the handler execution.
83 * Please do not change the first 4 parameters order in any way - some exceptions handlers
84 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
86 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
87 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
88 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
90 EXC_NESTED_FRAME newframe;
93 newframe.frame.Handler = nested_handler;
94 newframe.prevFrame = frame;
95 __wine_push_frame( &newframe.frame );
96 TRACE( "calling handler at %p code=%lx flags=%lx\n",
97 handler, record->ExceptionCode, record->ExceptionFlags );
98 ret = handler( record, frame, context, dispatcher );
99 TRACE( "handler returned %lx\n", ret );
100 __wine_pop_frame( &newframe.frame );
105 /**********************************************************************
108 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
110 static inline int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
115 struct exception_event_request *req = server_alloc_req( sizeof(*req),
116 sizeof(*rec)+sizeof(*context) );
117 CONTEXT *context_ptr = server_data_ptr(req);
118 EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
119 req->first = first_chance;
121 *context_ptr = *context;
122 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *context = *context_ptr;
130 /*******************************************************************
131 * EXC_DefaultHandling
133 * Default handling for exceptions. Called when we didn't find a suitable handler.
135 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
137 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
139 if (rec->ExceptionFlags & EH_STACK_INVALID)
140 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
141 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
142 ERR("Process attempted to continue execution after noncontinuable exception.\n");
144 ERR("Unhandled exception code %lx flags %lx addr %p\n",
145 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
146 NtTerminateProcess( NtCurrentProcess(), 1 );
150 /***********************************************************************
151 * EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
153 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
154 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
156 PEXCEPTION_FRAME frame, dispatch, nested_frame;
157 EXCEPTION_RECORD newrec;
160 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
162 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
164 frame = NtCurrentTeb()->except;
166 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
168 /* Check frame address */
169 if (((void*)frame < NtCurrentTeb()->stack_low) ||
170 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
173 rec->ExceptionFlags |= EH_STACK_INVALID;
178 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
179 if (frame == nested_frame)
181 /* no longer nested */
183 rec->ExceptionFlags &= ~EH_NESTED_CALL;
188 case ExceptionContinueExecution:
189 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
190 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
191 newrec.ExceptionFlags = EH_NONCONTINUABLE;
192 newrec.ExceptionRecord = rec;
193 newrec.NumberParameters = 0;
194 RtlRaiseException( &newrec ); /* never returns */
196 case ExceptionContinueSearch:
198 case ExceptionNestedException:
199 if (nested_frame < dispatch) nested_frame = dispatch;
200 rec->ExceptionFlags |= EH_NESTED_CALL;
203 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
204 newrec.ExceptionFlags = EH_NONCONTINUABLE;
205 newrec.ExceptionRecord = rec;
206 newrec.NumberParameters = 0;
207 RtlRaiseException( &newrec ); /* never returns */
212 EXC_DefaultHandling( rec, context );
216 /*******************************************************************
217 * EXC_RtlUnwind / RtlUnwind (KERNEL32.590) (NTDLL.518)
219 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
220 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
221 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
222 PEXCEPTION_RECORD pRecord, DWORD returnEax,
225 EXCEPTION_RECORD record, newrec;
226 PEXCEPTION_FRAME frame, dispatch;
229 context->Eax = returnEax;
232 /* build an exception record, if we do not have one */
235 record.ExceptionCode = STATUS_UNWIND;
236 record.ExceptionFlags = 0;
237 record.ExceptionRecord = NULL;
238 record.ExceptionAddress = GET_IP(context);
239 record.NumberParameters = 0;
243 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
245 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
247 /* get chain of exception frames */
248 frame = NtCurrentTeb()->except;
249 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
251 /* Check frame address */
252 if (pEndFrame && (frame > pEndFrame))
254 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
255 newrec.ExceptionFlags = EH_NONCONTINUABLE;
256 newrec.ExceptionRecord = pRecord;
257 newrec.NumberParameters = 0;
258 RtlRaiseException( &newrec ); /* never returns */
260 if (((void*)frame < NtCurrentTeb()->stack_low) ||
261 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
264 newrec.ExceptionCode = STATUS_BAD_STACK;
265 newrec.ExceptionFlags = EH_NONCONTINUABLE;
266 newrec.ExceptionRecord = pRecord;
267 newrec.NumberParameters = 0;
268 RtlRaiseException( &newrec ); /* never returns */
272 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
273 frame->Handler, EXC_UnwindHandler ))
275 case ExceptionContinueSearch:
277 case ExceptionCollidedUnwind:
281 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
282 newrec.ExceptionFlags = EH_NONCONTINUABLE;
283 newrec.ExceptionRecord = pRecord;
284 newrec.NumberParameters = 0;
285 RtlRaiseException( &newrec ); /* never returns */
288 frame = __wine_pop_frame( frame );
293 /*******************************************************************
294 * EXC_NtRaiseException / NtRaiseException (NTDLL.175)
296 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
297 EXCEPTION_RECORD *, CONTEXT *, BOOL )
298 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
299 BOOL first, CONTEXT *context )
301 EXC_RtlRaiseException( rec, ctx );
306 /***********************************************************************
307 * RtlRaiseStatus (NTDLL.465)
309 * Raise an exception with ExceptionCode = status
311 void WINAPI RtlRaiseStatus( NTSTATUS status )
313 EXCEPTION_RECORD ExceptionRec;
315 ExceptionRec.ExceptionCode = status;
316 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
317 ExceptionRec.ExceptionRecord = NULL;
318 ExceptionRec.NumberParameters = 0;
319 RtlRaiseException( &ExceptionRec );
323 /*************************************************************
324 * __wine_exception_handler
326 * Exception handler for exception blocks declared in Wine code.
328 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
329 CONTEXT *context, LPVOID pdispatcher )
331 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
333 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
334 return ExceptionContinueSearch;
335 if (wine_frame->u.filter)
337 EXCEPTION_POINTERS ptrs;
338 ptrs.ExceptionRecord = record;
339 ptrs.ContextRecord = context;
340 switch(wine_frame->u.filter( &ptrs ))
342 case EXCEPTION_CONTINUE_SEARCH:
343 return ExceptionContinueSearch;
344 case EXCEPTION_CONTINUE_EXECUTION:
345 return ExceptionContinueExecution;
346 case EXCEPTION_EXECUTE_HANDLER:
349 MESSAGE( "Invalid return value from exception filter\n" );
353 /* hack to make GetExceptionCode() work in handler */
354 wine_frame->ExceptionCode = record->ExceptionCode;
355 wine_frame->ExceptionRecord = wine_frame;
357 RtlUnwind( frame, 0, record, 0 );
358 __wine_pop_frame( frame );
359 longjmp( wine_frame->jmp, 1 );
363 /*************************************************************
364 * __wine_finally_handler
366 * Exception handler for try/finally blocks declared in Wine code.
368 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
369 CONTEXT *context, LPVOID pdispatcher )
371 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
373 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
374 return ExceptionContinueSearch;
375 wine_frame->u.finally_func( FALSE );
376 return ExceptionContinueSearch;