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"
30 #include "wine/exception.h"
31 #include "wine/server.h"
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(seh);
37 /* Exception record for handling exceptions happening inside exception handlers */
40 EXCEPTION_REGISTRATION_RECORD frame;
41 EXCEPTION_REGISTRATION_RECORD *prevFrame;
45 # define GET_IP(context) ((LPVOID)(context)->Eip)
46 #elif defined(__sparc__)
47 # define GET_IP(context) ((LPVOID)(context)->pc)
48 #elif defined(__powerpc__)
49 # define GET_IP(context) ((LPVOID)(context)->Iar)
51 # error You must define GET_IP for this CPU
54 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
55 void WINAPI EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD, LPVOID,
56 PEXCEPTION_RECORD, DWORD, PCONTEXT );
57 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
60 /*******************************************************************
63 * Handler for exceptions happening inside a handler.
65 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
66 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
68 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
69 return ExceptionContinueSearch;
70 /* We shouldn't get here so we store faulty frame in dispatcher */
71 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
72 return ExceptionNestedException;
76 /*******************************************************************
79 * Handler for exceptions happening inside an unwind handler.
81 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
82 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
84 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
85 return ExceptionContinueSearch;
86 /* We shouldn't get here so we store faulty frame in dispatcher */
87 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
88 return ExceptionCollidedUnwind;
92 /*******************************************************************
95 * Call an exception handler, setting up an exception frame to catch exceptions
96 * happening during the handler execution.
97 * Please do not change the first 4 parameters order in any way - some exceptions handlers
98 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
100 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
101 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
102 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
104 EXC_NESTED_FRAME newframe;
107 newframe.frame.Handler = nested_handler;
108 newframe.prevFrame = frame;
109 __wine_push_frame( &newframe.frame );
110 TRACE( "calling handler at %p code=%lx flags=%lx\n",
111 handler, record->ExceptionCode, record->ExceptionFlags );
112 ret = handler( record, frame, context, dispatcher );
113 TRACE( "handler returned %lx\n", ret );
114 __wine_pop_frame( &newframe.frame );
119 /**********************************************************************
122 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
124 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
129 SERVER_START_REQ( queue_exception_event )
131 req->first = first_chance;
132 wine_server_add_data( req, context, sizeof(*context) );
133 wine_server_add_data( req, rec, sizeof(*rec) );
134 if (!wine_server_call( req )) handle = reply->handle;
137 if (!handle) return 0; /* no debugger present or other error */
139 /* No need to wait on the handle since the process gets suspended
140 * once the event is passed to the debugger, so when we get back
141 * here the event has been continued already.
143 SERVER_START_REQ( get_exception_status )
145 req->handle = handle;
146 wine_server_set_reply( req, context, sizeof(*context) );
147 wine_server_call( req );
156 /*******************************************************************
157 * EXC_DefaultHandling
159 * Default handling for exceptions. Called when we didn't find a suitable handler.
161 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
163 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
165 if (rec->ExceptionFlags & EH_STACK_INVALID)
166 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
167 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
168 ERR("Process attempted to continue execution after noncontinuable exception.\n");
170 ERR("Unhandled exception code %lx flags %lx addr %p\n",
171 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
172 NtTerminateProcess( NtCurrentProcess(), 1 );
176 /***********************************************************************
177 * RtlRaiseException (NTDLL.@)
179 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
180 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
182 PEXCEPTION_REGISTRATION_RECORD frame, dispatch, nested_frame;
183 EXCEPTION_RECORD newrec;
186 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
187 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
188 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
189 FIXME( "call to unimplemented function %s.%s\n",
190 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
192 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
194 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
196 frame = NtCurrentTeb()->except;
198 while (frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL)
200 /* Check frame address */
201 if (((void*)frame < NtCurrentTeb()->stack_low) ||
202 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
205 rec->ExceptionFlags |= EH_STACK_INVALID;
210 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
211 if (frame == nested_frame)
213 /* no longer nested */
215 rec->ExceptionFlags &= ~EH_NESTED_CALL;
220 case ExceptionContinueExecution:
221 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
222 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
223 newrec.ExceptionFlags = EH_NONCONTINUABLE;
224 newrec.ExceptionRecord = rec;
225 newrec.NumberParameters = 0;
226 RtlRaiseException( &newrec ); /* never returns */
228 case ExceptionContinueSearch:
230 case ExceptionNestedException:
231 if (nested_frame < dispatch) nested_frame = dispatch;
232 rec->ExceptionFlags |= EH_NESTED_CALL;
235 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
236 newrec.ExceptionFlags = EH_NONCONTINUABLE;
237 newrec.ExceptionRecord = rec;
238 newrec.NumberParameters = 0;
239 RtlRaiseException( &newrec ); /* never returns */
244 EXC_DefaultHandling( rec, context );
248 /*******************************************************************
249 * RtlUnwind (NTDLL.@)
251 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
252 PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
253 void WINAPI EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD pEndFrame, LPVOID unusedEip,
254 PEXCEPTION_RECORD pRecord, DWORD returnEax,
257 EXCEPTION_RECORD record, newrec;
258 PEXCEPTION_REGISTRATION_RECORD frame, dispatch;
261 context->Eax = returnEax;
264 /* build an exception record, if we do not have one */
267 record.ExceptionCode = STATUS_UNWIND;
268 record.ExceptionFlags = 0;
269 record.ExceptionRecord = NULL;
270 record.ExceptionAddress = GET_IP(context);
271 record.NumberParameters = 0;
275 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
277 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
279 /* get chain of exception frames */
280 frame = NtCurrentTeb()->except;
281 while ((frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL) && (frame != pEndFrame))
283 /* Check frame address */
284 if (pEndFrame && (frame > pEndFrame))
286 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
287 newrec.ExceptionFlags = EH_NONCONTINUABLE;
288 newrec.ExceptionRecord = pRecord;
289 newrec.NumberParameters = 0;
290 RtlRaiseException( &newrec ); /* never returns */
292 if (((void*)frame < NtCurrentTeb()->stack_low) ||
293 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
296 newrec.ExceptionCode = STATUS_BAD_STACK;
297 newrec.ExceptionFlags = EH_NONCONTINUABLE;
298 newrec.ExceptionRecord = pRecord;
299 newrec.NumberParameters = 0;
300 RtlRaiseException( &newrec ); /* never returns */
304 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
305 frame->Handler, EXC_UnwindHandler ))
307 case ExceptionContinueSearch:
309 case ExceptionCollidedUnwind:
313 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
314 newrec.ExceptionFlags = EH_NONCONTINUABLE;
315 newrec.ExceptionRecord = pRecord;
316 newrec.NumberParameters = 0;
317 RtlRaiseException( &newrec ); /* never returns */
320 frame = __wine_pop_frame( frame );
325 /*******************************************************************
326 * NtRaiseException (NTDLL.@)
328 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
329 EXCEPTION_RECORD *, CONTEXT *, BOOL );
330 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
331 BOOL first, CONTEXT *context )
333 EXC_RtlRaiseException( rec, ctx );
338 /***********************************************************************
339 * RtlRaiseStatus (NTDLL.@)
341 * Raise an exception with ExceptionCode = status
343 void WINAPI RtlRaiseStatus( NTSTATUS status )
345 EXCEPTION_RECORD ExceptionRec;
347 ExceptionRec.ExceptionCode = status;
348 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
349 ExceptionRec.ExceptionRecord = NULL;
350 ExceptionRec.NumberParameters = 0;
351 RtlRaiseException( &ExceptionRec );
355 /*************************************************************
356 * __wine_exception_handler (NTDLL.@)
358 * Exception handler for exception blocks declared in Wine code.
360 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
361 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
363 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
365 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
366 return ExceptionContinueSearch;
367 if (wine_frame->u.filter)
369 EXCEPTION_POINTERS ptrs;
370 ptrs.ExceptionRecord = record;
371 ptrs.ContextRecord = context;
372 switch(wine_frame->u.filter( &ptrs ))
374 case EXCEPTION_CONTINUE_SEARCH:
375 return ExceptionContinueSearch;
376 case EXCEPTION_CONTINUE_EXECUTION:
377 return ExceptionContinueExecution;
378 case EXCEPTION_EXECUTE_HANDLER:
381 MESSAGE( "Invalid return value from exception filter\n" );
385 /* hack to make GetExceptionCode() work in handler */
386 wine_frame->ExceptionCode = record->ExceptionCode;
387 wine_frame->ExceptionRecord = wine_frame;
389 RtlUnwind( frame, 0, record, 0 );
390 __wine_pop_frame( frame );
391 longjmp( wine_frame->jmp, 1 );
395 /*************************************************************
396 * __wine_finally_handler (NTDLL.@)
398 * Exception handler for try/finally blocks declared in Wine code.
400 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
401 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
403 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
405 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
406 wine_frame->u.finally_func( FALSE );
408 return ExceptionContinueSearch;