2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
14 #include "wine/exception.h"
15 #include "stackframe.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(seh);
22 /* Exception record for handling exceptions happening inside exception handlers */
25 EXCEPTION_FRAME frame;
26 EXCEPTION_FRAME *prevFrame;
30 /*******************************************************************
33 * Handler for exceptions happening inside a handler.
35 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
36 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
38 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
39 return ExceptionContinueSearch;
40 /* We shouldn't get here so we store faulty frame in dispatcher */
41 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
42 return ExceptionNestedException;
46 /*******************************************************************
49 * Handler for exceptions happening inside an unwind handler.
51 static DWORD EXC_UnwindHandler( 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 ExceptionCollidedUnwind;
62 /*******************************************************************
65 * Call an exception handler, setting up an exception frame to catch exceptions
66 * happening during the handler execution.
67 * Please do not change the first 4 parameters order in any way - some exceptions handlers
68 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
70 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
71 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
72 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
74 EXC_NESTED_FRAME newframe;
77 newframe.frame.Handler = nested_handler;
78 newframe.prevFrame = frame;
79 __wine_push_frame( &newframe.frame );
80 TRACE( "calling handler at %p code=%lx flags=%lx\n",
81 handler, record->ExceptionCode, record->ExceptionFlags );
82 ret = handler( record, frame, context, dispatcher );
83 TRACE( "handler returned %lx\n", ret );
84 __wine_pop_frame( &newframe.frame );
89 /**********************************************************************
92 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
94 static inline int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
96 struct exception_event_request *req = get_req_buffer();
98 req->first = first_chance;
99 req->context = *context;
100 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *context = req->context;
105 /*******************************************************************
106 * EXC_DefaultHandling
108 * Default handling for exceptions. Called when we didn't find a suitable handler.
110 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
112 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
114 if (rec->ExceptionFlags & EH_STACK_INVALID)
115 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
116 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
117 ERR("Process attempted to continue execution after noncontinuable exception.\n");
119 ERR("Unhandled exception code %lx flags %lx addr %p\n",
120 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
121 NtTerminateProcess( NtCurrentProcess(), 1 );
125 /***********************************************************************
126 * EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
128 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
129 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
131 PEXCEPTION_FRAME frame, dispatch, nested_frame;
132 EXCEPTION_RECORD newrec;
135 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
137 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
139 frame = NtCurrentTeb()->except;
141 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
143 /* Check frame address */
144 if (((void*)frame < NtCurrentTeb()->stack_low) ||
145 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
148 rec->ExceptionFlags |= EH_STACK_INVALID;
153 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
154 if (frame == nested_frame)
156 /* no longer nested */
158 rec->ExceptionFlags &= ~EH_NESTED_CALL;
163 case ExceptionContinueExecution:
164 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
165 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
166 newrec.ExceptionFlags = EH_NONCONTINUABLE;
167 newrec.ExceptionRecord = rec;
168 newrec.NumberParameters = 0;
169 RtlRaiseException( &newrec ); /* never returns */
171 case ExceptionContinueSearch:
173 case ExceptionNestedException:
174 if (nested_frame < dispatch) nested_frame = dispatch;
175 rec->ExceptionFlags |= EH_NESTED_CALL;
178 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
179 newrec.ExceptionFlags = EH_NONCONTINUABLE;
180 newrec.ExceptionRecord = rec;
181 newrec.NumberParameters = 0;
182 RtlRaiseException( &newrec ); /* never returns */
187 EXC_DefaultHandling( rec, context );
191 /*******************************************************************
192 * EXC_RtlUnwind / RtlUnwind (KERNEL32.590) (NTDLL.518)
194 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
195 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
196 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
197 PEXCEPTION_RECORD pRecord, DWORD returnEax,
200 EXCEPTION_RECORD record, newrec;
201 PEXCEPTION_FRAME frame, dispatch;
204 context->Eax = returnEax;
207 /* build an exception record, if we do not have one */
210 record.ExceptionCode = STATUS_UNWIND;
211 record.ExceptionFlags = 0;
212 record.ExceptionRecord = NULL;
213 record.ExceptionAddress = GET_IP(context);
214 record.NumberParameters = 0;
218 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
220 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
222 /* get chain of exception frames */
223 frame = NtCurrentTeb()->except;
224 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
226 /* Check frame address */
227 if (pEndFrame && (frame > pEndFrame))
229 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
230 newrec.ExceptionFlags = EH_NONCONTINUABLE;
231 newrec.ExceptionRecord = pRecord;
232 newrec.NumberParameters = 0;
233 RtlRaiseException( &newrec ); /* never returns */
235 if (((void*)frame < NtCurrentTeb()->stack_low) ||
236 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
239 newrec.ExceptionCode = STATUS_BAD_STACK;
240 newrec.ExceptionFlags = EH_NONCONTINUABLE;
241 newrec.ExceptionRecord = pRecord;
242 newrec.NumberParameters = 0;
243 RtlRaiseException( &newrec ); /* never returns */
247 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
248 frame->Handler, EXC_UnwindHandler ))
250 case ExceptionContinueSearch:
252 case ExceptionCollidedUnwind:
256 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
257 newrec.ExceptionFlags = EH_NONCONTINUABLE;
258 newrec.ExceptionRecord = pRecord;
259 newrec.NumberParameters = 0;
260 RtlRaiseException( &newrec ); /* never returns */
263 frame = __wine_pop_frame( frame );
268 /*******************************************************************
269 * EXC_NtRaiseException / NtRaiseException (NTDLL.175)
271 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
272 EXCEPTION_RECORD *, CONTEXT *, BOOL )
273 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
274 BOOL first, CONTEXT *context )
276 EXC_RtlRaiseException( rec, ctx );
281 /***********************************************************************
282 * RtlRaiseStatus (NTDLL.465)
284 * Raise an exception with ExceptionCode = status
286 void WINAPI RtlRaiseStatus( NTSTATUS status )
288 EXCEPTION_RECORD ExceptionRec;
290 ExceptionRec.ExceptionCode = status;
291 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
292 ExceptionRec.ExceptionRecord = NULL;
293 ExceptionRec.NumberParameters = 0;
294 RtlRaiseException( &ExceptionRec );
298 /*************************************************************
299 * __wine_exception_handler
301 * Exception handler for exception blocks declared in Wine code.
303 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
304 CONTEXT *context, LPVOID pdispatcher )
306 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
308 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
309 return ExceptionContinueSearch;
310 if (wine_frame->u.filter)
312 EXCEPTION_POINTERS ptrs;
313 ptrs.ExceptionRecord = record;
314 ptrs.ContextRecord = context;
315 switch(wine_frame->u.filter( &ptrs ))
317 case EXCEPTION_CONTINUE_SEARCH:
318 return ExceptionContinueSearch;
319 case EXCEPTION_CONTINUE_EXECUTION:
320 return ExceptionContinueExecution;
321 case EXCEPTION_EXECUTE_HANDLER:
324 MESSAGE( "Invalid return value from exception filter\n" );
328 /* hack to make GetExceptionCode() work in handler */
329 wine_frame->ExceptionCode = record->ExceptionCode;
330 wine_frame->ExceptionRecord = wine_frame;
332 RtlUnwind( frame, 0, record, 0 );
333 __wine_pop_frame( frame );
334 longjmp( wine_frame->jmp, 1 );
338 /*************************************************************
339 * __wine_finally_handler
341 * Exception handler for try/finally blocks declared in Wine code.
343 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
344 CONTEXT *context, LPVOID pdispatcher )
346 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
348 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
349 return ExceptionContinueSearch;
350 wine_frame->u.finally_func( FALSE );
351 return ExceptionContinueSearch;