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 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
58 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
59 PEXCEPTION_RECORD, DWORD, PCONTEXT );
60 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
63 /*******************************************************************
66 * Handler for exceptions happening inside a handler.
68 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
69 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
71 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
72 return ExceptionContinueSearch;
73 /* We shouldn't get here so we store faulty frame in dispatcher */
74 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
75 return ExceptionNestedException;
79 /*******************************************************************
82 * Handler for exceptions happening inside an unwind handler.
84 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
85 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
87 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
88 return ExceptionContinueSearch;
89 /* We shouldn't get here so we store faulty frame in dispatcher */
90 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
91 return ExceptionCollidedUnwind;
95 /*******************************************************************
98 * Call an exception handler, setting up an exception frame to catch exceptions
99 * happening during the handler execution.
100 * Please do not change the first 4 parameters order in any way - some exceptions handlers
101 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
103 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
104 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
105 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
107 EXC_NESTED_FRAME newframe;
110 newframe.frame.Handler = nested_handler;
111 newframe.prevFrame = frame;
112 __wine_push_frame( &newframe.frame );
113 TRACE( "calling handler at %p code=%lx flags=%lx\n",
114 handler, record->ExceptionCode, record->ExceptionFlags );
115 ret = handler( record, frame, context, dispatcher );
116 TRACE( "handler returned %lx\n", ret );
117 __wine_pop_frame( &newframe.frame );
122 /**********************************************************************
125 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
127 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
132 SERVER_START_REQ( queue_exception_event )
134 req->first = first_chance;
135 wine_server_add_data( req, context, sizeof(*context) );
136 wine_server_add_data( req, rec, sizeof(*rec) );
137 if (!wine_server_call( req )) handle = reply->handle;
140 if (!handle) return 0; /* no debugger present or other error */
142 /* No need to wait on the handle since the process gets suspended
143 * once the event is passed to the debugger, so when we get back
144 * here the event has been continued already.
146 SERVER_START_REQ( get_exception_status )
148 req->handle = handle;
149 wine_server_set_reply( req, context, sizeof(*context) );
150 wine_server_call( req );
159 /*******************************************************************
160 * EXC_DefaultHandling
162 * Default handling for exceptions. Called when we didn't find a suitable handler.
164 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
166 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
168 if (rec->ExceptionFlags & EH_STACK_INVALID)
169 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
170 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
171 ERR("Process attempted to continue execution after noncontinuable exception.\n");
173 ERR("Unhandled exception code %lx flags %lx addr %p\n",
174 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
175 NtTerminateProcess( NtCurrentProcess(), 1 );
179 /***********************************************************************
180 * RtlRaiseException (NTDLL.@)
182 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
183 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
185 PEXCEPTION_FRAME frame, dispatch, nested_frame;
186 EXCEPTION_RECORD newrec;
189 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
190 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
191 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
192 FIXME( "call to unimplemented function %s.%s\n",
193 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
195 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
197 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
199 frame = NtCurrentTeb()->except;
201 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
203 /* Check frame address */
204 if (((void*)frame < NtCurrentTeb()->stack_low) ||
205 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
208 rec->ExceptionFlags |= EH_STACK_INVALID;
213 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
214 if (frame == nested_frame)
216 /* no longer nested */
218 rec->ExceptionFlags &= ~EH_NESTED_CALL;
223 case ExceptionContinueExecution:
224 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
225 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
226 newrec.ExceptionFlags = EH_NONCONTINUABLE;
227 newrec.ExceptionRecord = rec;
228 newrec.NumberParameters = 0;
229 RtlRaiseException( &newrec ); /* never returns */
231 case ExceptionContinueSearch:
233 case ExceptionNestedException:
234 if (nested_frame < dispatch) nested_frame = dispatch;
235 rec->ExceptionFlags |= EH_NESTED_CALL;
238 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
239 newrec.ExceptionFlags = EH_NONCONTINUABLE;
240 newrec.ExceptionRecord = rec;
241 newrec.NumberParameters = 0;
242 RtlRaiseException( &newrec ); /* never returns */
247 EXC_DefaultHandling( rec, context );
251 /*******************************************************************
252 * RtlUnwind (NTDLL.@)
254 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
255 PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
256 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
257 PEXCEPTION_RECORD pRecord, DWORD returnEax,
260 EXCEPTION_RECORD record, newrec;
261 PEXCEPTION_FRAME frame, dispatch;
264 context->Eax = returnEax;
267 /* build an exception record, if we do not have one */
270 record.ExceptionCode = STATUS_UNWIND;
271 record.ExceptionFlags = 0;
272 record.ExceptionRecord = NULL;
273 record.ExceptionAddress = GET_IP(context);
274 record.NumberParameters = 0;
278 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
280 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
282 /* get chain of exception frames */
283 frame = NtCurrentTeb()->except;
284 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
286 /* Check frame address */
287 if (pEndFrame && (frame > pEndFrame))
289 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
290 newrec.ExceptionFlags = EH_NONCONTINUABLE;
291 newrec.ExceptionRecord = pRecord;
292 newrec.NumberParameters = 0;
293 RtlRaiseException( &newrec ); /* never returns */
295 if (((void*)frame < NtCurrentTeb()->stack_low) ||
296 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
299 newrec.ExceptionCode = STATUS_BAD_STACK;
300 newrec.ExceptionFlags = EH_NONCONTINUABLE;
301 newrec.ExceptionRecord = pRecord;
302 newrec.NumberParameters = 0;
303 RtlRaiseException( &newrec ); /* never returns */
307 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
308 frame->Handler, EXC_UnwindHandler ))
310 case ExceptionContinueSearch:
312 case ExceptionCollidedUnwind:
316 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
317 newrec.ExceptionFlags = EH_NONCONTINUABLE;
318 newrec.ExceptionRecord = pRecord;
319 newrec.NumberParameters = 0;
320 RtlRaiseException( &newrec ); /* never returns */
323 frame = __wine_pop_frame( frame );
328 /*******************************************************************
329 * NtRaiseException (NTDLL.@)
331 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
332 EXCEPTION_RECORD *, CONTEXT *, BOOL );
333 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
334 BOOL first, CONTEXT *context )
336 EXC_RtlRaiseException( rec, ctx );
341 /***********************************************************************
342 * RtlRaiseStatus (NTDLL.@)
344 * Raise an exception with ExceptionCode = status
346 void WINAPI RtlRaiseStatus( NTSTATUS status )
348 EXCEPTION_RECORD ExceptionRec;
350 ExceptionRec.ExceptionCode = status;
351 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
352 ExceptionRec.ExceptionRecord = NULL;
353 ExceptionRec.NumberParameters = 0;
354 RtlRaiseException( &ExceptionRec );
358 /*************************************************************
359 * __wine_exception_handler (NTDLL.@)
361 * Exception handler for exception blocks declared in Wine code.
363 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
364 CONTEXT *context, LPVOID pdispatcher )
366 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
368 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
369 return ExceptionContinueSearch;
370 if (wine_frame->u.filter)
372 EXCEPTION_POINTERS ptrs;
373 ptrs.ExceptionRecord = record;
374 ptrs.ContextRecord = context;
375 switch(wine_frame->u.filter( &ptrs ))
377 case EXCEPTION_CONTINUE_SEARCH:
378 return ExceptionContinueSearch;
379 case EXCEPTION_CONTINUE_EXECUTION:
380 return ExceptionContinueExecution;
381 case EXCEPTION_EXECUTE_HANDLER:
384 MESSAGE( "Invalid return value from exception filter\n" );
388 /* hack to make GetExceptionCode() work in handler */
389 wine_frame->ExceptionCode = record->ExceptionCode;
390 wine_frame->ExceptionRecord = wine_frame;
392 RtlUnwind( frame, 0, record, 0 );
393 __wine_pop_frame( frame );
394 longjmp( wine_frame->jmp, 1 );
398 /*************************************************************
399 * __wine_finally_handler (NTDLL.@)
401 * Exception handler for try/finally blocks declared in Wine code.
403 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
404 CONTEXT *context, LPVOID pdispatcher )
406 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
408 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
409 wine_frame->u.finally_func( FALSE );
411 return ExceptionContinueSearch;
415 /*************************************************************
416 * __wine_callto16_handler
418 * Handler for exceptions occuring in 16-bit code.
420 DWORD __wine_callto16_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
421 CONTEXT *context, LPVOID pdispatcher )
423 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
425 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
426 STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
427 NtCurrentTeb()->cur_stack = frame32->frame16;
430 return ExceptionContinueSearch;