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
80 extern void DECLSPEC_NORETURN
__wine_call_from_32_restore_regs( const CONTEXT
*context
);
82 /*******************************************************************
85 * Handler for exceptions happening inside a handler.
87 static DWORD
EXC_RaiseHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
88 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
90 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
91 return ExceptionContinueSearch
;
92 /* We shouldn't get here so we store faulty frame in dispatcher */
93 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
94 return ExceptionNestedException
;
98 /*******************************************************************
101 * Handler for exceptions happening inside an unwind handler.
103 static DWORD
EXC_UnwindHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
104 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
106 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
107 return ExceptionContinueSearch
;
108 /* We shouldn't get here so we store faulty frame in dispatcher */
109 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
110 return ExceptionCollidedUnwind
;
114 /*******************************************************************
117 * Call an exception handler, setting up an exception frame to catch exceptions
118 * happening during the handler execution.
120 * For i386 this function is implemented in assembler in signal_i386.c.
123 static DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
124 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
125 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
)
127 EXC_NESTED_FRAME newframe
;
130 newframe
.frame
.Handler
= nested_handler
;
131 newframe
.prevFrame
= frame
;
132 __wine_push_frame( &newframe
.frame
);
133 TRACE( "calling handler at %p code=%lx flags=%lx\n",
134 handler
, record
->ExceptionCode
, record
->ExceptionFlags
);
135 ret
= handler( record
, frame
, context
, dispatcher
);
136 TRACE( "handler returned %lx\n", ret
);
137 __wine_pop_frame( &newframe
.frame
);
141 /* in signal_i386.c */
142 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
143 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
144 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
147 /**********************************************************************
150 * Wait until the thread is no longer suspended.
152 void wait_suspend( CONTEXT
*context
)
154 LARGE_INTEGER timeout
;
156 /* store the context we got at suspend time */
157 SERVER_START_REQ( set_thread_context
)
159 req
->handle
= GetCurrentThread();
160 req
->flags
= CONTEXT_FULL
;
162 wine_server_add_data( req
, context
, sizeof(*context
) );
163 wine_server_call( req
);
167 /* wait with 0 timeout, will only return once the thread is no longer suspended */
168 timeout
.QuadPart
= 0;
169 NTDLL_wait_for_multiple_objects( 0, NULL
, 0, &timeout
, 0 );
171 /* retrieve the new context */
172 SERVER_START_REQ( get_thread_context
)
174 req
->handle
= GetCurrentThread();
175 req
->flags
= CONTEXT_FULL
;
177 wine_server_set_reply( req
, context
, sizeof(*context
) );
178 wine_server_call( req
);
184 /**********************************************************************
187 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
189 static NTSTATUS
send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
194 if (!NtCurrentTeb()->Peb
->BeingDebugged
) return 0; /* no debugger present */
196 SERVER_START_REQ( queue_exception_event
)
198 req
->first
= first_chance
;
199 wine_server_add_data( req
, context
, sizeof(*context
) );
200 wine_server_add_data( req
, rec
, sizeof(*rec
) );
201 if (!wine_server_call( req
)) handle
= reply
->handle
;
204 if (!handle
) return 0;
206 NTDLL_wait_for_multiple_objects( 1, &handle
, 0, NULL
, 0 );
208 SERVER_START_REQ( get_exception_status
)
210 req
->handle
= handle
;
211 wine_server_set_reply( req
, context
, sizeof(*context
) );
212 ret
= wine_server_call( req
);
219 /**********************************************************************
220 * call_vectored_handlers
222 * Call the vectored handlers chain.
224 static LONG
call_vectored_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
227 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
228 EXCEPTION_POINTERS except_ptrs
;
230 except_ptrs
.ExceptionRecord
= rec
;
231 except_ptrs
.ContextRecord
= context
;
233 RtlEnterCriticalSection( &vectored_handlers_section
);
234 LIST_FOR_EACH( ptr
, &vectored_handlers
)
236 VECTORED_HANDLER
*handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
237 ret
= handler
->func( &except_ptrs
);
238 if (ret
== EXCEPTION_CONTINUE_EXECUTION
) break;
240 RtlLeaveCriticalSection( &vectored_handlers_section
);
245 /**********************************************************************
246 * call_stack_handlers
248 * Call the stack handlers chain.
250 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
252 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
255 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
257 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
259 /* Check frame address */
260 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
261 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
262 (ULONG_PTR
)frame
& 3)
264 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
269 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, EXC_RaiseHandler
);
270 if (frame
== nested_frame
)
272 /* no longer nested */
274 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
279 case ExceptionContinueExecution
:
280 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
281 return STATUS_NONCONTINUABLE_EXCEPTION
;
282 case ExceptionContinueSearch
:
284 case ExceptionNestedException
:
285 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
286 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
289 return STATUS_INVALID_DISPOSITION
;
293 return STATUS_UNHANDLED_EXCEPTION
;
297 /*******************************************************************
300 * Implementation of NtRaiseException.
302 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
310 TRACE( "code=%lx flags=%lx addr=%p\n",
311 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
312 for (c
= 0; c
< rec
->NumberParameters
; c
++)
313 TRACE( " info[%ld]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
314 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
316 if (rec
->ExceptionInformation
[1] >> 16)
317 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
318 rec
->ExceptionAddress
,
319 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
321 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
322 rec
->ExceptionAddress
,
323 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
328 TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
329 context
->Eax
, context
->Ebx
, context
->Ecx
,
330 context
->Edx
, context
->Esi
, context
->Edi
);
331 TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
332 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
333 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
336 status
= send_debug_event( rec
, TRUE
, context
);
337 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
338 return STATUS_SUCCESS
;
340 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
341 return STATUS_SUCCESS
;
343 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
347 /* last chance exception */
349 status
= send_debug_event( rec
, FALSE
, context
);
350 if (status
!= DBG_CONTINUE
)
352 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
353 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
354 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
355 ERR("Process attempted to continue execution after noncontinuable exception.\n");
357 ERR("Unhandled exception code %lx flags %lx addr %p\n",
358 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
359 NtTerminateProcess( NtCurrentProcess(), 1 );
361 return STATUS_SUCCESS
;
365 /*******************************************************************
366 * NtRaiseException (NTDLL.@)
368 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
370 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
371 #ifdef DEFINE_REGS_ENTRYPOINT
372 if (status
== STATUS_SUCCESS
) __wine_call_from_32_restore_regs( context
);
377 /***********************************************************************
378 * RtlRaiseException (NTDLL.@)
380 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
382 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
383 if (status
!= STATUS_SUCCESS
)
385 EXCEPTION_RECORD newrec
;
386 newrec
.ExceptionCode
= status
;
387 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
388 newrec
.ExceptionRecord
= rec
;
389 newrec
.NumberParameters
= 0;
390 RtlRaiseException( &newrec
); /* never returns */
394 /**********************************************************************/
396 #ifdef DEFINE_REGS_ENTRYPOINT
397 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 4, 4 );
399 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
402 memset( &context
, 0, sizeof(context
) );
403 __regs_RtlRaiseException( rec
, &context
);
408 /*******************************************************************
409 * RtlUnwind (NTDLL.@)
411 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID unusedEip
,
412 PEXCEPTION_RECORD pRecord
, PVOID returnEax
, CONTEXT
*context
)
414 EXCEPTION_RECORD record
, newrec
;
415 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
418 context
->Eax
= (DWORD
)returnEax
;
421 /* build an exception record, if we do not have one */
424 record
.ExceptionCode
= STATUS_UNWIND
;
425 record
.ExceptionFlags
= 0;
426 record
.ExceptionRecord
= NULL
;
427 record
.ExceptionAddress
= GET_IP(context
);
428 record
.NumberParameters
= 0;
432 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
434 TRACE( "code=%lx flags=%lx\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
436 /* get chain of exception frames */
437 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
438 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
440 /* Check frame address */
441 if (pEndFrame
&& (frame
> pEndFrame
))
443 newrec
.ExceptionCode
= STATUS_INVALID_UNWIND_TARGET
;
444 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
445 newrec
.ExceptionRecord
= pRecord
;
446 newrec
.NumberParameters
= 0;
447 RtlRaiseException( &newrec
); /* never returns */
449 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
450 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
453 newrec
.ExceptionCode
= STATUS_BAD_STACK
;
454 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
455 newrec
.ExceptionRecord
= pRecord
;
456 newrec
.NumberParameters
= 0;
457 RtlRaiseException( &newrec
); /* never returns */
461 switch(EXC_CallHandler( pRecord
, frame
, context
, &dispatch
,
462 frame
->Handler
, EXC_UnwindHandler
))
464 case ExceptionContinueSearch
:
466 case ExceptionCollidedUnwind
:
470 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
471 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
472 newrec
.ExceptionRecord
= pRecord
;
473 newrec
.NumberParameters
= 0;
474 RtlRaiseException( &newrec
); /* never returns */
477 frame
= __wine_pop_frame( frame
);
481 /**********************************************************************/
483 #ifdef DEFINE_REGS_ENTRYPOINT
484 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 16, 16 );
486 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID unusedEip
,
487 PEXCEPTION_RECORD pRecord
, PVOID returnEax
)
490 memset( &context
, 0, sizeof(context
) );
491 __regs_RtlUnwind( pEndFrame
, unusedEip
, pRecord
, returnEax
, &context
);
496 /***********************************************************************
497 * RtlRaiseStatus (NTDLL.@)
499 * Raise an exception with ExceptionCode = status
501 void WINAPI
RtlRaiseStatus( NTSTATUS status
)
503 EXCEPTION_RECORD ExceptionRec
;
505 ExceptionRec
.ExceptionCode
= status
;
506 ExceptionRec
.ExceptionFlags
= EH_NONCONTINUABLE
;
507 ExceptionRec
.ExceptionRecord
= NULL
;
508 ExceptionRec
.NumberParameters
= 0;
509 RtlRaiseException( &ExceptionRec
);
513 /*******************************************************************
514 * RtlAddVectoredExceptionHandler (NTDLL.@)
516 PVOID WINAPI
RtlAddVectoredExceptionHandler( ULONG first
, PVECTORED_EXCEPTION_HANDLER func
)
518 VECTORED_HANDLER
*handler
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler
) );
521 handler
->func
= func
;
522 RtlEnterCriticalSection( &vectored_handlers_section
);
523 if (first
) list_add_head( &vectored_handlers
, &handler
->entry
);
524 else list_add_tail( &vectored_handlers
, &handler
->entry
);
525 RtlLeaveCriticalSection( &vectored_handlers_section
);
531 /*******************************************************************
532 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
534 ULONG WINAPI
RtlRemoveVectoredExceptionHandler( PVOID handler
)
539 RtlEnterCriticalSection( &vectored_handlers_section
);
540 LIST_FOR_EACH( ptr
, &vectored_handlers
)
542 VECTORED_HANDLER
*curr_handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
543 if (curr_handler
== handler
)
550 RtlLeaveCriticalSection( &vectored_handlers_section
);
551 if (ret
) RtlFreeHeap( GetProcessHeap(), 0, handler
);
556 /*************************************************************
557 * __wine_exception_handler (NTDLL.@)
559 * Exception handler for exception blocks declared in Wine code.
561 DWORD
__wine_exception_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
562 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
564 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
566 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
567 return ExceptionContinueSearch
;
569 if (wine_frame
->u
.filter
== (void *)1) /* special hack for page faults */
571 if (record
->ExceptionCode
!= STATUS_ACCESS_VIOLATION
)
572 return ExceptionContinueSearch
;
574 else if (wine_frame
->u
.filter
)
576 EXCEPTION_POINTERS ptrs
;
577 ptrs
.ExceptionRecord
= record
;
578 ptrs
.ContextRecord
= context
;
579 switch(wine_frame
->u
.filter( &ptrs
))
581 case EXCEPTION_CONTINUE_SEARCH
:
582 return ExceptionContinueSearch
;
583 case EXCEPTION_CONTINUE_EXECUTION
:
584 return ExceptionContinueExecution
;
585 case EXCEPTION_EXECUTE_HANDLER
:
588 MESSAGE( "Invalid return value from exception filter\n" );
592 /* hack to make GetExceptionCode() work in handler */
593 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
594 wine_frame
->ExceptionRecord
= wine_frame
;
596 RtlUnwind( frame
, 0, record
, 0 );
597 __wine_pop_frame( frame
);
598 siglongjmp( wine_frame
->jmp
, 1 );
602 /*************************************************************
603 * __wine_finally_handler (NTDLL.@)
605 * Exception handler for try/finally blocks declared in Wine code.
607 DWORD
__wine_finally_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
608 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
610 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
612 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
613 wine_frame
->u
.finally_func( FALSE
);
615 return ExceptionContinueSearch
;
619 /*************************************************************
620 * __wine_spec_unimplemented_stub
622 * ntdll-specific implementation to avoid depending on kernel functions.
623 * Can be removed once ntdll.spec no longer contains stubs.
625 void __wine_spec_unimplemented_stub( const char *module
, const char *function
)
627 EXCEPTION_RECORD record
;
629 record
.ExceptionCode
= EXCEPTION_WINE_STUB
;
630 record
.ExceptionFlags
= EH_NONCONTINUABLE
;
631 record
.ExceptionRecord
= NULL
;
632 record
.ExceptionAddress
= __wine_spec_unimplemented_stub
;
633 record
.NumberParameters
= 2;
634 record
.ExceptionInformation
[0] = (ULONG_PTR
)module
;
635 record
.ExceptionInformation
[1] = (ULONG_PTR
)function
;
636 RtlRaiseException( &record
);