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 #define WIN32_NO_STATUS
33 #include "wine/exception.h"
34 #include "wine/server.h"
35 #include "wine/list.h"
36 #include "wine/debug.h"
38 #include "ntdll_misc.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
42 /* Exception record for handling exceptions happening inside exception handlers */
45 EXCEPTION_REGISTRATION_RECORD frame;
46 EXCEPTION_REGISTRATION_RECORD *prevFrame;
52 PVECTORED_EXCEPTION_HANDLER func;
55 static struct list vectored_handlers = LIST_INIT(vectored_handlers);
57 static RTL_CRITICAL_SECTION vectored_handlers_section;
58 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
60 0, 0, &vectored_handlers_section,
61 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
62 0, 0, { (DWORD_PTR)(__FILE__ ": vectored_handlers_section") }
64 static RTL_CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
67 # define GET_IP(context) ((LPVOID)(context)->Eip)
68 #elif defined(__sparc__)
69 # define GET_IP(context) ((LPVOID)(context)->pc)
70 #elif defined(__powerpc__)
71 # define GET_IP(context) ((LPVOID)(context)->Iar)
72 #elif defined(__ALPHA__)
73 # define GET_IP(context) ((LPVOID)(context)->Fir)
74 #elif defined(__x86_64__)
75 # define GET_IP(context) ((LPVOID)(context)->Rip)
77 # error You must define GET_IP for this CPU
81 /*******************************************************************
84 * Handler for exceptions happening inside a handler.
86 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
87 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **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 ExceptionNestedException;
97 /*******************************************************************
100 * Handler for exceptions happening inside an unwind handler.
102 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
103 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
105 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
106 return ExceptionContinueSearch;
107 /* We shouldn't get here so we store faulty frame in dispatcher */
108 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
109 return ExceptionCollidedUnwind;
113 /*******************************************************************
116 * Call an exception handler, setting up an exception frame to catch exceptions
117 * happening during the handler execution.
119 * For i386 this function is implemented in assembler in signal_i386.c.
122 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
123 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
124 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
126 EXC_NESTED_FRAME newframe;
129 newframe.frame.Handler = nested_handler;
130 newframe.prevFrame = frame;
131 __wine_push_frame( &newframe.frame );
132 TRACE( "calling handler at %p code=%lx flags=%lx\n",
133 handler, record->ExceptionCode, record->ExceptionFlags );
134 ret = handler( record, frame, context, dispatcher );
135 TRACE( "handler returned %lx\n", ret );
136 __wine_pop_frame( &newframe.frame );
140 /* in signal_i386.c */
141 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
142 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
143 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler);
146 /**********************************************************************
149 * Wait until the thread is no longer suspended.
151 void wait_suspend( CONTEXT *context )
153 LARGE_INTEGER timeout;
155 /* store the context we got at suspend time */
156 SERVER_START_REQ( set_thread_context )
158 req->handle = GetCurrentThread();
159 req->flags = CONTEXT_FULL;
161 wine_server_add_data( req, context, sizeof(*context) );
162 wine_server_call( req );
166 /* wait with 0 timeout, will only return once the thread is no longer suspended */
167 timeout.QuadPart = 0;
168 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout, 0 );
170 /* retrieve the new context */
171 SERVER_START_REQ( get_thread_context )
173 req->handle = GetCurrentThread();
174 req->flags = CONTEXT_FULL;
176 wine_server_set_reply( req, context, sizeof(*context) );
177 wine_server_call( req );
183 /**********************************************************************
186 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
188 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
193 if (!NtCurrentTeb()->Peb->BeingDebugged) return 0; /* no debugger present */
195 SERVER_START_REQ( queue_exception_event )
197 req->first = first_chance;
198 wine_server_add_data( req, context, sizeof(*context) );
199 wine_server_add_data( req, rec, sizeof(*rec) );
200 if (!wine_server_call( req )) handle = reply->handle;
203 if (!handle) return 0;
205 NTDLL_wait_for_multiple_objects( 1, &handle, 0, NULL, 0 );
207 SERVER_START_REQ( get_exception_status )
209 req->handle = handle;
210 wine_server_set_reply( req, context, sizeof(*context) );
211 ret = wine_server_call( req );
218 /**********************************************************************
219 * call_vectored_handlers
221 * Call the vectored handlers chain.
223 static LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
226 LONG ret = EXCEPTION_CONTINUE_SEARCH;
227 EXCEPTION_POINTERS except_ptrs;
229 except_ptrs.ExceptionRecord = rec;
230 except_ptrs.ContextRecord = context;
232 RtlEnterCriticalSection( &vectored_handlers_section );
233 LIST_FOR_EACH( ptr, &vectored_handlers )
235 VECTORED_HANDLER *handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
236 ret = handler->func( &except_ptrs );
237 if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
239 RtlLeaveCriticalSection( &vectored_handlers_section );
244 /*******************************************************************
245 * EXC_DefaultHandling
247 * Default handling for exceptions. Called when we didn't find a suitable handler.
249 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
251 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
253 if (rec->ExceptionFlags & EH_STACK_INVALID)
254 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
255 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
256 ERR("Process attempted to continue execution after noncontinuable exception.\n");
258 ERR("Unhandled exception code %lx flags %lx addr %p\n",
259 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
260 NtTerminateProcess( NtCurrentProcess(), 1 );
264 /***********************************************************************
265 * RtlRaiseException (NTDLL.@)
267 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
269 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
270 EXCEPTION_RECORD newrec;
273 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
274 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
275 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
277 if (HIWORD(rec->ExceptionInformation[1]))
278 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
279 rec->ExceptionAddress,
280 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
282 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
283 rec->ExceptionAddress,
284 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
289 TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
290 context->Eax, context->Ebx, context->Ecx,
291 context->Edx, context->Esi, context->Edi );
292 TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
293 context->Ebp, context->Esp, context->SegCs, context->SegDs,
294 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
298 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
300 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
302 frame = NtCurrentTeb()->Tib.ExceptionList;
304 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
306 /* Check frame address */
307 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
308 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
309 (ULONG_PTR)frame & 3)
311 rec->ExceptionFlags |= EH_STACK_INVALID;
316 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
317 if (frame == nested_frame)
319 /* no longer nested */
321 rec->ExceptionFlags &= ~EH_NESTED_CALL;
326 case ExceptionContinueExecution:
327 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
328 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
329 newrec.ExceptionFlags = EH_NONCONTINUABLE;
330 newrec.ExceptionRecord = rec;
331 newrec.NumberParameters = 0;
332 RtlRaiseException( &newrec ); /* never returns */
334 case ExceptionContinueSearch:
336 case ExceptionNestedException:
337 if (nested_frame < dispatch) nested_frame = dispatch;
338 rec->ExceptionFlags |= EH_NESTED_CALL;
341 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
342 newrec.ExceptionFlags = EH_NONCONTINUABLE;
343 newrec.ExceptionRecord = rec;
344 newrec.NumberParameters = 0;
345 RtlRaiseException( &newrec ); /* never returns */
350 EXC_DefaultHandling( rec, context );
353 /**********************************************************************/
355 #ifdef DEFINE_REGS_ENTRYPOINT
356 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 4, 4 );
358 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
361 memset( &context, 0, sizeof(context) );
362 __regs_RtlRaiseException( rec, &context );
367 /*******************************************************************
368 * RtlUnwind (NTDLL.@)
370 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID unusedEip,
371 PEXCEPTION_RECORD pRecord, PVOID returnEax, CONTEXT *context )
373 EXCEPTION_RECORD record, newrec;
374 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
377 context->Eax = (DWORD)returnEax;
380 /* build an exception record, if we do not have one */
383 record.ExceptionCode = STATUS_UNWIND;
384 record.ExceptionFlags = 0;
385 record.ExceptionRecord = NULL;
386 record.ExceptionAddress = GET_IP(context);
387 record.NumberParameters = 0;
391 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
393 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
395 /* get chain of exception frames */
396 frame = NtCurrentTeb()->Tib.ExceptionList;
397 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
399 /* Check frame address */
400 if (pEndFrame && (frame > pEndFrame))
402 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
403 newrec.ExceptionFlags = EH_NONCONTINUABLE;
404 newrec.ExceptionRecord = pRecord;
405 newrec.NumberParameters = 0;
406 RtlRaiseException( &newrec ); /* never returns */
408 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
409 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
412 newrec.ExceptionCode = STATUS_BAD_STACK;
413 newrec.ExceptionFlags = EH_NONCONTINUABLE;
414 newrec.ExceptionRecord = pRecord;
415 newrec.NumberParameters = 0;
416 RtlRaiseException( &newrec ); /* never returns */
420 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
421 frame->Handler, EXC_UnwindHandler ))
423 case ExceptionContinueSearch:
425 case ExceptionCollidedUnwind:
429 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
430 newrec.ExceptionFlags = EH_NONCONTINUABLE;
431 newrec.ExceptionRecord = pRecord;
432 newrec.NumberParameters = 0;
433 RtlRaiseException( &newrec ); /* never returns */
436 frame = __wine_pop_frame( frame );
440 /**********************************************************************/
442 #ifdef DEFINE_REGS_ENTRYPOINT
443 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 16, 16 );
445 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
446 PEXCEPTION_RECORD pRecord, PVOID returnEax )
449 memset( &context, 0, sizeof(context) );
450 __regs_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
455 /*******************************************************************
456 * NtRaiseException (NTDLL.@)
458 void WINAPI __regs_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
459 BOOL first, CONTEXT *context )
461 __regs_RtlRaiseException( rec, ctx );
465 #ifdef DEFINE_REGS_ENTRYPOINT
466 DEFINE_REGS_ENTRYPOINT( NtRaiseException, 12, 12 );
468 void WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
471 memset( &context, 0, sizeof(context) );
472 __regs_NtRaiseException( rec, ctx, first, &context );
477 /***********************************************************************
478 * RtlRaiseStatus (NTDLL.@)
480 * Raise an exception with ExceptionCode = status
482 void WINAPI RtlRaiseStatus( NTSTATUS status )
484 EXCEPTION_RECORD ExceptionRec;
486 ExceptionRec.ExceptionCode = status;
487 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
488 ExceptionRec.ExceptionRecord = NULL;
489 ExceptionRec.NumberParameters = 0;
490 RtlRaiseException( &ExceptionRec );
494 /*******************************************************************
495 * RtlAddVectoredExceptionHandler (NTDLL.@)
497 PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
499 VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
502 handler->func = func;
503 RtlEnterCriticalSection( &vectored_handlers_section );
504 if (first) list_add_head( &vectored_handlers, &handler->entry );
505 else list_add_tail( &vectored_handlers, &handler->entry );
506 RtlLeaveCriticalSection( &vectored_handlers_section );
512 /*******************************************************************
513 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
515 ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
520 RtlEnterCriticalSection( &vectored_handlers_section );
521 LIST_FOR_EACH( ptr, &vectored_handlers )
523 VECTORED_HANDLER *curr_handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
524 if (curr_handler == handler)
531 RtlLeaveCriticalSection( &vectored_handlers_section );
532 if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
537 /*************************************************************
538 * __wine_exception_handler (NTDLL.@)
540 * Exception handler for exception blocks declared in Wine code.
542 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
543 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
545 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
547 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
548 return ExceptionContinueSearch;
550 if (wine_frame->u.filter == (void *)1) /* special hack for page faults */
552 if (record->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
553 return ExceptionContinueSearch;
555 else if (wine_frame->u.filter)
557 EXCEPTION_POINTERS ptrs;
558 ptrs.ExceptionRecord = record;
559 ptrs.ContextRecord = context;
560 switch(wine_frame->u.filter( &ptrs ))
562 case EXCEPTION_CONTINUE_SEARCH:
563 return ExceptionContinueSearch;
564 case EXCEPTION_CONTINUE_EXECUTION:
565 return ExceptionContinueExecution;
566 case EXCEPTION_EXECUTE_HANDLER:
569 MESSAGE( "Invalid return value from exception filter\n" );
573 /* hack to make GetExceptionCode() work in handler */
574 wine_frame->ExceptionCode = record->ExceptionCode;
575 wine_frame->ExceptionRecord = wine_frame;
577 RtlUnwind( frame, 0, record, 0 );
578 __wine_pop_frame( frame );
579 siglongjmp( wine_frame->jmp, 1 );
583 /*************************************************************
584 * __wine_finally_handler (NTDLL.@)
586 * Exception handler for try/finally blocks declared in Wine code.
588 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
589 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
591 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
593 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
594 wine_frame->u.finally_func( FALSE );
596 return ExceptionContinueSearch;
600 /*************************************************************
601 * __wine_spec_unimplemented_stub
603 * ntdll-specific implementation to avoid depending on kernel functions.
604 * Can be removed once ntdll.spec no longer contains stubs.
606 void __wine_spec_unimplemented_stub( const char *module, const char *function )
608 EXCEPTION_RECORD record;
610 record.ExceptionCode = EXCEPTION_WINE_STUB;
611 record.ExceptionFlags = EH_NONCONTINUABLE;
612 record.ExceptionRecord = NULL;
613 record.ExceptionAddress = __wine_spec_unimplemented_stub;
614 record.NumberParameters = 2;
615 record.ExceptionInformation[0] = (ULONG_PTR)module;
616 record.ExceptionInformation[1] = (ULONG_PTR)function;
617 RtlRaiseException( &record );