2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/exception.h"
31 #include "stackframe.h"
33 #include "wine/server.h"
34 #include "wine/debug.h"
35 #include "msvcrt/excpt.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(seh);
39 /* Exception record for handling exceptions happening inside exception handlers */
42 EXCEPTION_FRAME frame;
43 EXCEPTION_FRAME *prevFrame;
47 # define GET_IP(context) ((LPVOID)(context)->Eip)
50 # define GET_IP(context) ((LPVOID)(context)->pc)
53 # error You must define GET_IP for this CPU
56 extern void SIGNAL_Unblock(void);
58 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
59 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
60 PEXCEPTION_RECORD, DWORD, PCONTEXT );
61 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
64 /*******************************************************************
67 * Handler for exceptions happening inside a handler.
69 static DWORD EXC_RaiseHandler( 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 ExceptionNestedException;
80 /*******************************************************************
83 * Handler for exceptions happening inside an unwind handler.
85 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
86 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
88 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
89 return ExceptionContinueSearch;
90 /* We shouldn't get here so we store faulty frame in dispatcher */
91 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
92 return ExceptionCollidedUnwind;
96 /*******************************************************************
99 * Call an exception handler, setting up an exception frame to catch exceptions
100 * happening during the handler execution.
101 * Please do not change the first 4 parameters order in any way - some exceptions handlers
102 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
104 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
105 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
106 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
108 EXC_NESTED_FRAME newframe;
111 newframe.frame.Handler = nested_handler;
112 newframe.prevFrame = frame;
113 __wine_push_frame( &newframe.frame );
114 TRACE( "calling handler at %p code=%lx flags=%lx\n",
115 handler, record->ExceptionCode, record->ExceptionFlags );
116 ret = handler( record, frame, context, dispatcher );
117 TRACE( "handler returned %lx\n", ret );
118 __wine_pop_frame( &newframe.frame );
123 /**********************************************************************
126 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
128 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
133 SERVER_START_REQ( queue_exception_event )
135 req->first = first_chance;
136 wine_server_add_data( req, context, sizeof(*context) );
137 wine_server_add_data( req, rec, sizeof(*rec) );
138 if (!wine_server_call( req )) handle = reply->handle;
141 if (!handle) return 0; /* no debugger present or other error */
143 /* No need to wait on the handle since the process gets suspended
144 * once the event is passed to the debugger, so when we get back
145 * here the event has been continued already.
147 SERVER_START_REQ( get_exception_status )
149 req->handle = handle;
150 wine_server_set_reply( req, context, sizeof(*context) );
151 wine_server_call( req );
160 /*******************************************************************
161 * EXC_DefaultHandling
163 * Default handling for exceptions. Called when we didn't find a suitable handler.
165 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
167 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
169 if (rec->ExceptionFlags & EH_STACK_INVALID)
170 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
171 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
172 ERR("Process attempted to continue execution after noncontinuable exception.\n");
174 ERR("Unhandled exception code %lx flags %lx addr %p\n",
175 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
176 NtTerminateProcess( NtCurrentProcess(), 1 );
180 /***********************************************************************
181 * RtlRaiseException (NTDLL.@)
183 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
184 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
186 PEXCEPTION_FRAME frame, dispatch, nested_frame;
187 EXCEPTION_RECORD newrec;
190 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
191 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
192 if (rec->ExceptionCode == EXCEPTION_WINE_STUB) TRACE(" stub=%s\n", (char*)rec->ExceptionInformation[1]);
194 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
196 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
198 frame = NtCurrentTeb()->except;
200 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
202 /* Check frame address */
203 if (((void*)frame < NtCurrentTeb()->stack_low) ||
204 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
207 rec->ExceptionFlags |= EH_STACK_INVALID;
212 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
213 if (frame == nested_frame)
215 /* no longer nested */
217 rec->ExceptionFlags &= ~EH_NESTED_CALL;
222 case ExceptionContinueExecution:
223 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
224 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
225 newrec.ExceptionFlags = EH_NONCONTINUABLE;
226 newrec.ExceptionRecord = rec;
227 newrec.NumberParameters = 0;
228 RtlRaiseException( &newrec ); /* never returns */
230 case ExceptionContinueSearch:
232 case ExceptionNestedException:
233 if (nested_frame < dispatch) nested_frame = dispatch;
234 rec->ExceptionFlags |= EH_NESTED_CALL;
237 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
238 newrec.ExceptionFlags = EH_NONCONTINUABLE;
239 newrec.ExceptionRecord = rec;
240 newrec.NumberParameters = 0;
241 RtlRaiseException( &newrec ); /* never returns */
246 EXC_DefaultHandling( rec, context );
250 /*******************************************************************
251 * RtlUnwind (NTDLL.@)
253 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
254 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD );
255 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
256 PEXCEPTION_RECORD pRecord, DWORD returnEax,
259 EXCEPTION_RECORD record, newrec;
260 PEXCEPTION_FRAME frame, dispatch;
263 context->Eax = returnEax;
266 /* build an exception record, if we do not have one */
269 record.ExceptionCode = STATUS_UNWIND;
270 record.ExceptionFlags = 0;
271 record.ExceptionRecord = NULL;
272 record.ExceptionAddress = GET_IP(context);
273 record.NumberParameters = 0;
277 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
279 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
281 /* get chain of exception frames */
282 frame = NtCurrentTeb()->except;
283 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
285 /* Check frame address */
286 if (pEndFrame && (frame > pEndFrame))
288 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
289 newrec.ExceptionFlags = EH_NONCONTINUABLE;
290 newrec.ExceptionRecord = pRecord;
291 newrec.NumberParameters = 0;
292 RtlRaiseException( &newrec ); /* never returns */
294 if (((void*)frame < NtCurrentTeb()->stack_low) ||
295 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
298 newrec.ExceptionCode = STATUS_BAD_STACK;
299 newrec.ExceptionFlags = EH_NONCONTINUABLE;
300 newrec.ExceptionRecord = pRecord;
301 newrec.NumberParameters = 0;
302 RtlRaiseException( &newrec ); /* never returns */
306 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
307 frame->Handler, EXC_UnwindHandler ))
309 case ExceptionContinueSearch:
311 case ExceptionCollidedUnwind:
315 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
316 newrec.ExceptionFlags = EH_NONCONTINUABLE;
317 newrec.ExceptionRecord = pRecord;
318 newrec.NumberParameters = 0;
319 RtlRaiseException( &newrec ); /* never returns */
322 frame = __wine_pop_frame( frame );
327 /*******************************************************************
328 * NtRaiseException (NTDLL.@)
330 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
331 EXCEPTION_RECORD *, CONTEXT *, BOOL );
332 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
333 BOOL first, CONTEXT *context )
335 EXC_RtlRaiseException( rec, ctx );
340 /***********************************************************************
341 * RtlRaiseStatus (NTDLL.@)
343 * Raise an exception with ExceptionCode = status
345 void WINAPI RtlRaiseStatus( NTSTATUS status )
347 EXCEPTION_RECORD ExceptionRec;
349 ExceptionRec.ExceptionCode = status;
350 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
351 ExceptionRec.ExceptionRecord = NULL;
352 ExceptionRec.NumberParameters = 0;
353 RtlRaiseException( &ExceptionRec );
357 /*************************************************************
358 * __wine_exception_handler (NTDLL.@)
360 * Exception handler for exception blocks declared in Wine code.
362 DWORD __wine_exception_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 | EH_NESTED_CALL))
368 return ExceptionContinueSearch;
369 if (wine_frame->u.filter)
371 EXCEPTION_POINTERS ptrs;
372 ptrs.ExceptionRecord = record;
373 ptrs.ContextRecord = context;
374 switch(wine_frame->u.filter( &ptrs ))
376 case EXCEPTION_CONTINUE_SEARCH:
377 return ExceptionContinueSearch;
378 case EXCEPTION_CONTINUE_EXECUTION:
379 return ExceptionContinueExecution;
380 case EXCEPTION_EXECUTE_HANDLER:
383 MESSAGE( "Invalid return value from exception filter\n" );
387 /* hack to make GetExceptionCode() work in handler */
388 wine_frame->ExceptionCode = record->ExceptionCode;
389 wine_frame->ExceptionRecord = wine_frame;
391 RtlUnwind( frame, 0, record, 0 );
392 __wine_pop_frame( frame );
393 longjmp( wine_frame->jmp, 1 );
397 /*************************************************************
398 * __wine_finally_handler (NTDLL.@)
400 * Exception handler for try/finally blocks declared in Wine code.
402 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
403 CONTEXT *context, LPVOID pdispatcher )
405 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
407 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
408 return ExceptionContinueSearch;
409 wine_frame->u.finally_func( FALSE );
410 return ExceptionContinueSearch;