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 NTSTATUS 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;
274 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
275 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
276 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
278 if (HIWORD(rec->ExceptionInformation[1]))
279 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
280 rec->ExceptionAddress,
281 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
283 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
284 rec->ExceptionAddress,
285 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
290 TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
291 context->Eax, context->Ebx, context->Ecx,
292 context->Edx, context->Esi, context->Edi );
293 TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
294 context->Ebp, context->Esp, context->SegCs, context->SegDs,
295 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
299 status = send_debug_event( rec, TRUE, context );
300 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED) return; /* continue execution */
302 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
304 frame = NtCurrentTeb()->Tib.ExceptionList;
306 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
308 /* Check frame address */
309 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
310 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
311 (ULONG_PTR)frame & 3)
313 rec->ExceptionFlags |= EH_STACK_INVALID;
318 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
319 if (frame == nested_frame)
321 /* no longer nested */
323 rec->ExceptionFlags &= ~EH_NESTED_CALL;
328 case ExceptionContinueExecution:
329 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
330 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
331 newrec.ExceptionFlags = EH_NONCONTINUABLE;
332 newrec.ExceptionRecord = rec;
333 newrec.NumberParameters = 0;
334 RtlRaiseException( &newrec ); /* never returns */
336 case ExceptionContinueSearch:
338 case ExceptionNestedException:
339 if (nested_frame < dispatch) nested_frame = dispatch;
340 rec->ExceptionFlags |= EH_NESTED_CALL;
343 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
344 newrec.ExceptionFlags = EH_NONCONTINUABLE;
345 newrec.ExceptionRecord = rec;
346 newrec.NumberParameters = 0;
347 RtlRaiseException( &newrec ); /* never returns */
352 EXC_DefaultHandling( rec, context );
355 /**********************************************************************/
357 #ifdef DEFINE_REGS_ENTRYPOINT
358 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 4, 4 );
360 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
363 memset( &context, 0, sizeof(context) );
364 __regs_RtlRaiseException( rec, &context );
369 /*******************************************************************
370 * RtlUnwind (NTDLL.@)
372 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID unusedEip,
373 PEXCEPTION_RECORD pRecord, PVOID returnEax, CONTEXT *context )
375 EXCEPTION_RECORD record, newrec;
376 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
379 context->Eax = (DWORD)returnEax;
382 /* build an exception record, if we do not have one */
385 record.ExceptionCode = STATUS_UNWIND;
386 record.ExceptionFlags = 0;
387 record.ExceptionRecord = NULL;
388 record.ExceptionAddress = GET_IP(context);
389 record.NumberParameters = 0;
393 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
395 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
397 /* get chain of exception frames */
398 frame = NtCurrentTeb()->Tib.ExceptionList;
399 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
401 /* Check frame address */
402 if (pEndFrame && (frame > pEndFrame))
404 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
405 newrec.ExceptionFlags = EH_NONCONTINUABLE;
406 newrec.ExceptionRecord = pRecord;
407 newrec.NumberParameters = 0;
408 RtlRaiseException( &newrec ); /* never returns */
410 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
411 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
414 newrec.ExceptionCode = STATUS_BAD_STACK;
415 newrec.ExceptionFlags = EH_NONCONTINUABLE;
416 newrec.ExceptionRecord = pRecord;
417 newrec.NumberParameters = 0;
418 RtlRaiseException( &newrec ); /* never returns */
422 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
423 frame->Handler, EXC_UnwindHandler ))
425 case ExceptionContinueSearch:
427 case ExceptionCollidedUnwind:
431 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
432 newrec.ExceptionFlags = EH_NONCONTINUABLE;
433 newrec.ExceptionRecord = pRecord;
434 newrec.NumberParameters = 0;
435 RtlRaiseException( &newrec ); /* never returns */
438 frame = __wine_pop_frame( frame );
442 /**********************************************************************/
444 #ifdef DEFINE_REGS_ENTRYPOINT
445 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 16, 16 );
447 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
448 PEXCEPTION_RECORD pRecord, PVOID returnEax )
451 memset( &context, 0, sizeof(context) );
452 __regs_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
457 /*******************************************************************
458 * NtRaiseException (NTDLL.@)
460 void WINAPI __regs_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
461 BOOL first, CONTEXT *context )
463 __regs_RtlRaiseException( rec, ctx );
467 #ifdef DEFINE_REGS_ENTRYPOINT
468 DEFINE_REGS_ENTRYPOINT( NtRaiseException, 12, 12 );
470 void WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
473 memset( &context, 0, sizeof(context) );
474 __regs_NtRaiseException( rec, ctx, first, &context );
479 /***********************************************************************
480 * RtlRaiseStatus (NTDLL.@)
482 * Raise an exception with ExceptionCode = status
484 void WINAPI RtlRaiseStatus( NTSTATUS status )
486 EXCEPTION_RECORD ExceptionRec;
488 ExceptionRec.ExceptionCode = status;
489 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
490 ExceptionRec.ExceptionRecord = NULL;
491 ExceptionRec.NumberParameters = 0;
492 RtlRaiseException( &ExceptionRec );
496 /*******************************************************************
497 * RtlAddVectoredExceptionHandler (NTDLL.@)
499 PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
501 VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
504 handler->func = func;
505 RtlEnterCriticalSection( &vectored_handlers_section );
506 if (first) list_add_head( &vectored_handlers, &handler->entry );
507 else list_add_tail( &vectored_handlers, &handler->entry );
508 RtlLeaveCriticalSection( &vectored_handlers_section );
514 /*******************************************************************
515 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
517 ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
522 RtlEnterCriticalSection( &vectored_handlers_section );
523 LIST_FOR_EACH( ptr, &vectored_handlers )
525 VECTORED_HANDLER *curr_handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
526 if (curr_handler == handler)
533 RtlLeaveCriticalSection( &vectored_handlers_section );
534 if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
539 /*************************************************************
540 * __wine_exception_handler (NTDLL.@)
542 * Exception handler for exception blocks declared in Wine code.
544 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
545 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
547 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
549 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
550 return ExceptionContinueSearch;
552 if (wine_frame->u.filter == (void *)1) /* special hack for page faults */
554 if (record->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
555 return ExceptionContinueSearch;
557 else if (wine_frame->u.filter)
559 EXCEPTION_POINTERS ptrs;
560 ptrs.ExceptionRecord = record;
561 ptrs.ContextRecord = context;
562 switch(wine_frame->u.filter( &ptrs ))
564 case EXCEPTION_CONTINUE_SEARCH:
565 return ExceptionContinueSearch;
566 case EXCEPTION_CONTINUE_EXECUTION:
567 return ExceptionContinueExecution;
568 case EXCEPTION_EXECUTE_HANDLER:
571 MESSAGE( "Invalid return value from exception filter\n" );
575 /* hack to make GetExceptionCode() work in handler */
576 wine_frame->ExceptionCode = record->ExceptionCode;
577 wine_frame->ExceptionRecord = wine_frame;
579 RtlUnwind( frame, 0, record, 0 );
580 __wine_pop_frame( frame );
581 siglongjmp( wine_frame->jmp, 1 );
585 /*************************************************************
586 * __wine_finally_handler (NTDLL.@)
588 * Exception handler for try/finally blocks declared in Wine code.
590 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
591 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
593 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
595 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
596 wine_frame->u.finally_func( FALSE );
598 return ExceptionContinueSearch;
602 /*************************************************************
603 * __wine_spec_unimplemented_stub
605 * ntdll-specific implementation to avoid depending on kernel functions.
606 * Can be removed once ntdll.spec no longer contains stubs.
608 void __wine_spec_unimplemented_stub( const char *module, const char *function )
610 EXCEPTION_RECORD record;
612 record.ExceptionCode = EXCEPTION_WINE_STUB;
613 record.ExceptionFlags = EH_NONCONTINUABLE;
614 record.ExceptionRecord = NULL;
615 record.ExceptionAddress = __wine_spec_unimplemented_stub;
616 record.NumberParameters = 2;
617 record.ExceptionInformation[0] = (ULONG_PTR)module;
618 record.ExceptionInformation[1] = (ULONG_PTR)function;
619 RtlRaiseException( &record );