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
23 #include "wine/port.h"
31 #include "wine/exception.h"
32 #include "stackframe.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40 /* Exception record for handling exceptions happening inside exception handlers */
43 EXCEPTION_FRAME frame;
44 EXCEPTION_FRAME *prevFrame;
48 # define GET_IP(context) ((LPVOID)(context)->Eip)
49 #elif defined(__sparc__)
50 # define GET_IP(context) ((LPVOID)(context)->pc)
51 #elif defined(__powerpc__)
52 # define GET_IP(context) ((LPVOID)(context)->Iar)
54 # error You must define GET_IP for this CPU
57 extern void SIGNAL_Unblock(void);
59 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
60 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
61 PEXCEPTION_RECORD, DWORD, PCONTEXT );
62 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
65 /*******************************************************************
68 * Handler for exceptions happening inside a handler.
70 static DWORD EXC_RaiseHandler( 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 ExceptionNestedException;
81 /*******************************************************************
84 * Handler for exceptions happening inside an unwind handler.
86 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
87 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
89 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
90 return ExceptionContinueSearch;
91 /* We shouldn't get here so we store faulty frame in dispatcher */
92 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
93 return ExceptionCollidedUnwind;
97 /*******************************************************************
100 * Call an exception handler, setting up an exception frame to catch exceptions
101 * happening during the handler execution.
102 * Please do not change the first 4 parameters order in any way - some exceptions handlers
103 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
105 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
106 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
107 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
109 EXC_NESTED_FRAME newframe;
112 newframe.frame.Handler = nested_handler;
113 newframe.prevFrame = frame;
114 __wine_push_frame( &newframe.frame );
115 TRACE( "calling handler at %p code=%lx flags=%lx\n",
116 handler, record->ExceptionCode, record->ExceptionFlags );
117 ret = handler( record, frame, context, dispatcher );
118 TRACE( "handler returned %lx\n", ret );
119 __wine_pop_frame( &newframe.frame );
124 /**********************************************************************
127 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
129 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
134 SERVER_START_REQ( queue_exception_event )
136 req->first = first_chance;
137 wine_server_add_data( req, context, sizeof(*context) );
138 wine_server_add_data( req, rec, sizeof(*rec) );
139 if (!wine_server_call( req )) handle = reply->handle;
142 if (!handle) return 0; /* no debugger present or other error */
144 /* No need to wait on the handle since the process gets suspended
145 * once the event is passed to the debugger, so when we get back
146 * here the event has been continued already.
148 SERVER_START_REQ( get_exception_status )
150 req->handle = handle;
151 wine_server_set_reply( req, context, sizeof(*context) );
152 wine_server_call( req );
161 /*******************************************************************
162 * EXC_DefaultHandling
164 * Default handling for exceptions. Called when we didn't find a suitable handler.
166 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
168 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
170 if (rec->ExceptionFlags & EH_STACK_INVALID)
171 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
172 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
173 ERR("Process attempted to continue execution after noncontinuable exception.\n");
175 ERR("Unhandled exception code %lx flags %lx addr %p\n",
176 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
177 NtTerminateProcess( NtCurrentProcess(), 1 );
181 /***********************************************************************
182 * RtlRaiseException (NTDLL.@)
184 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
185 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
187 PEXCEPTION_FRAME frame, dispatch, nested_frame;
188 EXCEPTION_RECORD newrec;
191 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
192 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
193 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
194 FIXME( "call to unimplemented function %s.%s\n",
195 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
197 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
199 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
201 frame = NtCurrentTeb()->except;
203 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
205 /* Check frame address */
206 if (((void*)frame < NtCurrentTeb()->stack_low) ||
207 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
210 rec->ExceptionFlags |= EH_STACK_INVALID;
215 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
216 if (frame == nested_frame)
218 /* no longer nested */
220 rec->ExceptionFlags &= ~EH_NESTED_CALL;
225 case ExceptionContinueExecution:
226 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
227 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
228 newrec.ExceptionFlags = EH_NONCONTINUABLE;
229 newrec.ExceptionRecord = rec;
230 newrec.NumberParameters = 0;
231 RtlRaiseException( &newrec ); /* never returns */
233 case ExceptionContinueSearch:
235 case ExceptionNestedException:
236 if (nested_frame < dispatch) nested_frame = dispatch;
237 rec->ExceptionFlags |= EH_NESTED_CALL;
240 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
241 newrec.ExceptionFlags = EH_NONCONTINUABLE;
242 newrec.ExceptionRecord = rec;
243 newrec.NumberParameters = 0;
244 RtlRaiseException( &newrec ); /* never returns */
249 EXC_DefaultHandling( rec, context );
253 /*******************************************************************
254 * RtlUnwind (NTDLL.@)
256 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
257 PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
258 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
259 PEXCEPTION_RECORD pRecord, DWORD returnEax,
262 EXCEPTION_RECORD record, newrec;
263 PEXCEPTION_FRAME frame, dispatch;
266 context->Eax = returnEax;
269 /* build an exception record, if we do not have one */
272 record.ExceptionCode = STATUS_UNWIND;
273 record.ExceptionFlags = 0;
274 record.ExceptionRecord = NULL;
275 record.ExceptionAddress = GET_IP(context);
276 record.NumberParameters = 0;
280 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
282 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
284 /* get chain of exception frames */
285 frame = NtCurrentTeb()->except;
286 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
288 /* Check frame address */
289 if (pEndFrame && (frame > pEndFrame))
291 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
292 newrec.ExceptionFlags = EH_NONCONTINUABLE;
293 newrec.ExceptionRecord = pRecord;
294 newrec.NumberParameters = 0;
295 RtlRaiseException( &newrec ); /* never returns */
297 if (((void*)frame < NtCurrentTeb()->stack_low) ||
298 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
301 newrec.ExceptionCode = STATUS_BAD_STACK;
302 newrec.ExceptionFlags = EH_NONCONTINUABLE;
303 newrec.ExceptionRecord = pRecord;
304 newrec.NumberParameters = 0;
305 RtlRaiseException( &newrec ); /* never returns */
309 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
310 frame->Handler, EXC_UnwindHandler ))
312 case ExceptionContinueSearch:
314 case ExceptionCollidedUnwind:
318 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
319 newrec.ExceptionFlags = EH_NONCONTINUABLE;
320 newrec.ExceptionRecord = pRecord;
321 newrec.NumberParameters = 0;
322 RtlRaiseException( &newrec ); /* never returns */
325 frame = __wine_pop_frame( frame );
330 /*******************************************************************
331 * NtRaiseException (NTDLL.@)
333 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
334 EXCEPTION_RECORD *, CONTEXT *, BOOL );
335 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
336 BOOL first, CONTEXT *context )
338 EXC_RtlRaiseException( rec, ctx );
343 /***********************************************************************
344 * RtlRaiseStatus (NTDLL.@)
346 * Raise an exception with ExceptionCode = status
348 void WINAPI RtlRaiseStatus( NTSTATUS status )
350 EXCEPTION_RECORD ExceptionRec;
352 ExceptionRec.ExceptionCode = status;
353 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
354 ExceptionRec.ExceptionRecord = NULL;
355 ExceptionRec.NumberParameters = 0;
356 RtlRaiseException( &ExceptionRec );
360 /*************************************************************
361 * __wine_exception_handler (NTDLL.@)
363 * Exception handler for exception blocks declared in Wine code.
365 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
366 CONTEXT *context, LPVOID pdispatcher )
368 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
370 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
371 return ExceptionContinueSearch;
372 if (wine_frame->u.filter)
374 EXCEPTION_POINTERS ptrs;
375 ptrs.ExceptionRecord = record;
376 ptrs.ContextRecord = context;
377 switch(wine_frame->u.filter( &ptrs ))
379 case EXCEPTION_CONTINUE_SEARCH:
380 return ExceptionContinueSearch;
381 case EXCEPTION_CONTINUE_EXECUTION:
382 return ExceptionContinueExecution;
383 case EXCEPTION_EXECUTE_HANDLER:
386 MESSAGE( "Invalid return value from exception filter\n" );
390 /* hack to make GetExceptionCode() work in handler */
391 wine_frame->ExceptionCode = record->ExceptionCode;
392 wine_frame->ExceptionRecord = wine_frame;
394 RtlUnwind( frame, 0, record, 0 );
395 __wine_pop_frame( frame );
396 longjmp( wine_frame->jmp, 1 );
400 /*************************************************************
401 * __wine_finally_handler (NTDLL.@)
403 * Exception handler for try/finally blocks declared in Wine code.
405 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
406 CONTEXT *context, LPVOID pdispatcher )
408 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
410 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
411 wine_frame->u.finally_func( FALSE );
413 return ExceptionContinueSearch;
417 /*************************************************************
418 * __wine_callto16_handler
420 * Handler for exceptions occuring in 16-bit code.
422 DWORD __wine_callto16_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
423 CONTEXT *context, LPVOID pdispatcher )
425 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
427 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
428 STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
429 NtCurrentTeb()->cur_stack = frame32->frame16;
432 return ExceptionContinueSearch;