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"
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
39 #include "ntdll_misc.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43 /* Exception record for handling exceptions happening inside exception handlers */
46 EXCEPTION_REGISTRATION_RECORD frame;
47 EXCEPTION_REGISTRATION_RECORD *prevFrame;
53 PVECTORED_EXCEPTION_HANDLER func;
56 static struct list vectored_handlers = LIST_INIT(vectored_handlers);
58 static CRITICAL_SECTION vectored_handlers_section;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
61 0, 0, &vectored_handlers_section,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { 0, (DWORD)(__FILE__ ": vectored_handlers_section") }
65 static CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
68 # define GET_IP(context) ((LPVOID)(context)->Eip)
69 #elif defined(__sparc__)
70 # define GET_IP(context) ((LPVOID)(context)->pc)
71 #elif defined(__powerpc__)
72 # define GET_IP(context) ((LPVOID)(context)->Iar)
74 # error You must define GET_IP for this CPU
78 /*******************************************************************
81 * Handler for exceptions happening inside a handler.
83 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
84 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
86 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
87 return ExceptionContinueSearch;
88 /* We shouldn't get here so we store faulty frame in dispatcher */
89 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
90 return ExceptionNestedException;
94 /*******************************************************************
97 * Handler for exceptions happening inside an unwind handler.
99 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
100 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
102 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
103 return ExceptionContinueSearch;
104 /* We shouldn't get here so we store faulty frame in dispatcher */
105 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
106 return ExceptionCollidedUnwind;
110 /*******************************************************************
113 * Call an exception handler, setting up an exception frame to catch exceptions
114 * happening during the handler execution.
115 * Please do not change the first 4 parameters order in any way - some exceptions handlers
116 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
118 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
119 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
120 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
122 EXC_NESTED_FRAME newframe;
125 newframe.frame.Handler = nested_handler;
126 newframe.prevFrame = frame;
127 __wine_push_frame( &newframe.frame );
128 TRACE( "calling handler at %p code=%lx flags=%lx\n",
129 handler, record->ExceptionCode, record->ExceptionFlags );
130 ret = handler( record, frame, context, dispatcher );
131 TRACE( "handler returned %lx\n", ret );
132 __wine_pop_frame( &newframe.frame );
137 /**********************************************************************
140 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
142 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
147 if (!NtCurrentTeb()->Peb->BeingDebugged) return 0; /* no debugger present */
149 SERVER_START_REQ( queue_exception_event )
151 req->first = first_chance;
152 wine_server_add_data( req, context, sizeof(*context) );
153 wine_server_add_data( req, rec, sizeof(*rec) );
154 if (!wine_server_call( req )) handle = reply->handle;
157 if (!handle) return 0;
159 /* No need to wait on the handle since the process gets suspended
160 * once the event is passed to the debugger, so when we get back
161 * here the event has been continued already.
163 SERVER_START_REQ( get_exception_status )
165 req->handle = handle;
166 wine_server_set_reply( req, context, sizeof(*context) );
167 wine_server_call( req );
176 /**********************************************************************
177 * call_vectored_handlers
179 * Call the vectored handlers chain.
181 static LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
184 LONG ret = EXCEPTION_CONTINUE_SEARCH;
185 EXCEPTION_POINTERS except_ptrs;
187 except_ptrs.ExceptionRecord = rec;
188 except_ptrs.ContextRecord = context;
190 RtlEnterCriticalSection( &vectored_handlers_section );
191 LIST_FOR_EACH( ptr, &vectored_handlers )
193 VECTORED_HANDLER *handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
194 ret = handler->func( &except_ptrs );
195 if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
197 RtlLeaveCriticalSection( &vectored_handlers_section );
202 /*******************************************************************
203 * EXC_DefaultHandling
205 * Default handling for exceptions. Called when we didn't find a suitable handler.
207 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
209 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
211 if (rec->ExceptionFlags & EH_STACK_INVALID)
212 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
213 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
214 ERR("Process attempted to continue execution after noncontinuable exception.\n");
216 ERR("Unhandled exception code %lx flags %lx addr %p\n",
217 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
218 NtTerminateProcess( NtCurrentProcess(), 1 );
222 /***********************************************************************
223 * RtlRaiseException (NTDLL.@)
225 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
227 PEXCEPTION_REGISTRATION_RECORD frame, dispatch, nested_frame;
228 EXCEPTION_RECORD newrec;
231 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
232 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
233 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
234 FIXME( "call to unimplemented function %s.%s\n",
235 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
237 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
239 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
241 frame = NtCurrentTeb()->Tib.ExceptionList;
243 while (frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL)
245 /* Check frame address */
246 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
247 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
250 rec->ExceptionFlags |= EH_STACK_INVALID;
255 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
256 if (frame == nested_frame)
258 /* no longer nested */
260 rec->ExceptionFlags &= ~EH_NESTED_CALL;
265 case ExceptionContinueExecution:
266 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
267 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
268 newrec.ExceptionFlags = EH_NONCONTINUABLE;
269 newrec.ExceptionRecord = rec;
270 newrec.NumberParameters = 0;
271 RtlRaiseException( &newrec ); /* never returns */
273 case ExceptionContinueSearch:
275 case ExceptionNestedException:
276 if (nested_frame < dispatch) nested_frame = dispatch;
277 rec->ExceptionFlags |= EH_NESTED_CALL;
280 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
281 newrec.ExceptionFlags = EH_NONCONTINUABLE;
282 newrec.ExceptionRecord = rec;
283 newrec.NumberParameters = 0;
284 RtlRaiseException( &newrec ); /* never returns */
289 EXC_DefaultHandling( rec, context );
292 #ifdef DEFINE_REGS_ENTRYPOINT
293 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, EXC_RtlRaiseException, 4, 4 );
295 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
298 memset( &context, 0, sizeof(context) );
299 EXC_RtlRaiseException( rec, &context );
304 /*******************************************************************
305 * RtlUnwind (NTDLL.@)
307 void WINAPI EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD pEndFrame, PVOID unusedEip,
308 PEXCEPTION_RECORD pRecord, PVOID returnEax, CONTEXT *context )
310 EXCEPTION_RECORD record, newrec;
311 PEXCEPTION_REGISTRATION_RECORD frame, dispatch;
314 context->Eax = (DWORD)returnEax;
317 /* build an exception record, if we do not have one */
320 record.ExceptionCode = STATUS_UNWIND;
321 record.ExceptionFlags = 0;
322 record.ExceptionRecord = NULL;
323 record.ExceptionAddress = GET_IP(context);
324 record.NumberParameters = 0;
328 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
330 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
332 /* get chain of exception frames */
333 frame = NtCurrentTeb()->Tib.ExceptionList;
334 while ((frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL) && (frame != pEndFrame))
336 /* Check frame address */
337 if (pEndFrame && (frame > pEndFrame))
339 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
340 newrec.ExceptionFlags = EH_NONCONTINUABLE;
341 newrec.ExceptionRecord = pRecord;
342 newrec.NumberParameters = 0;
343 RtlRaiseException( &newrec ); /* never returns */
345 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
346 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
349 newrec.ExceptionCode = STATUS_BAD_STACK;
350 newrec.ExceptionFlags = EH_NONCONTINUABLE;
351 newrec.ExceptionRecord = pRecord;
352 newrec.NumberParameters = 0;
353 RtlRaiseException( &newrec ); /* never returns */
357 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
358 frame->Handler, EXC_UnwindHandler ))
360 case ExceptionContinueSearch:
362 case ExceptionCollidedUnwind:
366 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
367 newrec.ExceptionFlags = EH_NONCONTINUABLE;
368 newrec.ExceptionRecord = pRecord;
369 newrec.NumberParameters = 0;
370 RtlRaiseException( &newrec ); /* never returns */
373 frame = __wine_pop_frame( frame );
377 #ifdef DEFINE_REGS_ENTRYPOINT
378 DEFINE_REGS_ENTRYPOINT( RtlUnwind, EXC_RtlUnwind, 16, 16 );
380 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
381 PEXCEPTION_RECORD pRecord, PVOID returnEax )
384 memset( &context, 0, sizeof(context) );
385 EXC_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
390 /*******************************************************************
391 * NtRaiseException (NTDLL.@)
393 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
394 BOOL first, CONTEXT *context )
396 EXC_RtlRaiseException( rec, ctx );
400 #ifdef DEFINE_REGS_ENTRYPOINT
401 DEFINE_REGS_ENTRYPOINT( NtRaiseException, EXC_NtRaiseException, 12, 12 );
403 void WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
406 memset( &context, 0, sizeof(context) );
407 EXC_NtRaiseException( rec, ctx, first, &context );
412 /***********************************************************************
413 * RtlRaiseStatus (NTDLL.@)
415 * Raise an exception with ExceptionCode = status
417 void WINAPI RtlRaiseStatus( NTSTATUS status )
419 EXCEPTION_RECORD ExceptionRec;
421 ExceptionRec.ExceptionCode = status;
422 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
423 ExceptionRec.ExceptionRecord = NULL;
424 ExceptionRec.NumberParameters = 0;
425 RtlRaiseException( &ExceptionRec );
429 /*******************************************************************
430 * RtlAddVectoredExceptionHandler (NTDLL.@)
432 PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
434 VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
437 handler->func = func;
438 RtlEnterCriticalSection( &vectored_handlers_section );
439 if (first) list_add_head( &vectored_handlers, &handler->entry );
440 else list_add_tail( &vectored_handlers, &handler->entry );
441 RtlLeaveCriticalSection( &vectored_handlers_section );
447 /*******************************************************************
448 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
450 ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
455 RtlEnterCriticalSection( &vectored_handlers_section );
456 LIST_FOR_EACH( ptr, &vectored_handlers )
458 if (ptr == &((VECTORED_HANDLER *)handler)->entry)
465 RtlLeaveCriticalSection( &vectored_handlers_section );
466 if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
471 /*************************************************************
472 * __wine_exception_handler (NTDLL.@)
474 * Exception handler for exception blocks declared in Wine code.
476 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
477 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
479 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
481 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
482 return ExceptionContinueSearch;
483 if (wine_frame->u.filter)
485 EXCEPTION_POINTERS ptrs;
486 ptrs.ExceptionRecord = record;
487 ptrs.ContextRecord = context;
488 switch(wine_frame->u.filter( &ptrs ))
490 case EXCEPTION_CONTINUE_SEARCH:
491 return ExceptionContinueSearch;
492 case EXCEPTION_CONTINUE_EXECUTION:
493 return ExceptionContinueExecution;
494 case EXCEPTION_EXECUTE_HANDLER:
497 MESSAGE( "Invalid return value from exception filter\n" );
501 /* hack to make GetExceptionCode() work in handler */
502 wine_frame->ExceptionCode = record->ExceptionCode;
503 wine_frame->ExceptionRecord = wine_frame;
505 RtlUnwind( frame, 0, record, 0 );
506 __wine_pop_frame( frame );
507 siglongjmp( wine_frame->jmp, 1 );
511 /*************************************************************
512 * __wine_finally_handler (NTDLL.@)
514 * Exception handler for try/finally blocks declared in Wine code.
516 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
517 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
519 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
521 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
522 wine_frame->u.finally_func( FALSE );
524 return ExceptionContinueSearch;