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 /*******************************************************************
83 * Handler for exceptions happening inside a handler.
85 static DWORD
EXC_RaiseHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
86 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
88 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
89 return ExceptionContinueSearch
;
90 /* We shouldn't get here so we store faulty frame in dispatcher */
91 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
92 return ExceptionNestedException
;
96 /*******************************************************************
99 * Handler for exceptions happening inside an unwind handler.
101 static DWORD
EXC_UnwindHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
102 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
104 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
105 return ExceptionContinueSearch
;
106 /* We shouldn't get here so we store faulty frame in dispatcher */
107 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
108 return ExceptionCollidedUnwind
;
112 /*******************************************************************
115 * Call an exception handler, setting up an exception frame to catch exceptions
116 * happening during the handler execution.
118 * For i386 this function is implemented in assembler in signal_i386.c.
121 static DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
122 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
123 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
)
125 EXC_NESTED_FRAME newframe
;
128 newframe
.frame
.Handler
= nested_handler
;
129 newframe
.prevFrame
= frame
;
130 __wine_push_frame( &newframe
.frame
);
131 ret
= handler( record
, frame
, context
, dispatcher
);
132 __wine_pop_frame( &newframe
.frame
);
136 /* in signal_i386.c */
137 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
138 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
139 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
142 /**********************************************************************
145 * Wait until the thread is no longer suspended.
147 void wait_suspend( CONTEXT
*context
)
149 LARGE_INTEGER timeout
;
151 /* store the context we got at suspend time */
152 SERVER_START_REQ( set_thread_context
)
154 req
->handle
= GetCurrentThread();
155 req
->flags
= CONTEXT_FULL
;
157 wine_server_add_data( req
, context
, sizeof(*context
) );
158 wine_server_call( req
);
162 /* wait with 0 timeout, will only return once the thread is no longer suspended */
163 timeout
.QuadPart
= 0;
164 NTDLL_wait_for_multiple_objects( 0, NULL
, 0, &timeout
, 0 );
166 /* retrieve the new context */
167 SERVER_START_REQ( get_thread_context
)
169 req
->handle
= GetCurrentThread();
170 req
->flags
= CONTEXT_FULL
;
172 wine_server_set_reply( req
, context
, sizeof(*context
) );
173 wine_server_call( req
);
179 /**********************************************************************
182 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
184 static NTSTATUS
send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
189 if (!NtCurrentTeb()->Peb
->BeingDebugged
) return 0; /* no debugger present */
191 SERVER_START_REQ( queue_exception_event
)
193 req
->first
= first_chance
;
194 wine_server_add_data( req
, context
, sizeof(*context
) );
195 wine_server_add_data( req
, rec
, sizeof(*rec
) );
196 if (!wine_server_call( req
)) handle
= reply
->handle
;
199 if (!handle
) return 0;
201 NTDLL_wait_for_multiple_objects( 1, &handle
, 0, NULL
, 0 );
203 SERVER_START_REQ( get_exception_status
)
205 req
->handle
= handle
;
206 wine_server_set_reply( req
, context
, sizeof(*context
) );
207 ret
= wine_server_call( req
);
214 /**********************************************************************
215 * call_vectored_handlers
217 * Call the vectored handlers chain.
219 static LONG
call_vectored_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
222 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
223 EXCEPTION_POINTERS except_ptrs
;
225 except_ptrs
.ExceptionRecord
= rec
;
226 except_ptrs
.ContextRecord
= context
;
228 RtlEnterCriticalSection( &vectored_handlers_section
);
229 LIST_FOR_EACH( ptr
, &vectored_handlers
)
231 VECTORED_HANDLER
*handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
232 ret
= handler
->func( &except_ptrs
);
233 if (ret
== EXCEPTION_CONTINUE_EXECUTION
) break;
235 RtlLeaveCriticalSection( &vectored_handlers_section
);
240 /**********************************************************************
241 * call_stack_handlers
243 * Call the stack handlers chain.
245 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
247 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
250 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
252 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
254 /* Check frame address */
255 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
256 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
257 (ULONG_PTR
)frame
& 3)
259 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
264 TRACE( "calling handler at %p code=%lx flags=%lx\n",
265 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
266 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, EXC_RaiseHandler
);
267 TRACE( "handler at %p returned %lx\n", frame
->Handler
, res
);
269 if (frame
== nested_frame
)
271 /* no longer nested */
273 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
278 case ExceptionContinueExecution
:
279 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
280 return STATUS_NONCONTINUABLE_EXCEPTION
;
281 case ExceptionContinueSearch
:
283 case ExceptionNestedException
:
284 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
285 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
288 return STATUS_INVALID_DISPOSITION
;
292 return STATUS_UNHANDLED_EXCEPTION
;
296 /*******************************************************************
299 * Implementation of NtRaiseException.
301 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
309 TRACE( "code=%lx flags=%lx addr=%p\n",
310 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
311 for (c
= 0; c
< rec
->NumberParameters
; c
++)
312 TRACE( " info[%ld]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
313 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
315 if (rec
->ExceptionInformation
[1] >> 16)
316 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
317 rec
->ExceptionAddress
,
318 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
320 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
321 rec
->ExceptionAddress
,
322 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
327 TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
328 context
->Eax
, context
->Ebx
, context
->Ecx
,
329 context
->Edx
, context
->Esi
, context
->Edi
);
330 TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
331 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
332 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
335 status
= send_debug_event( rec
, TRUE
, context
);
336 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
337 return STATUS_SUCCESS
;
339 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
340 return STATUS_SUCCESS
;
342 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
346 /* last chance exception */
348 status
= send_debug_event( rec
, FALSE
, context
);
349 if (status
!= DBG_CONTINUE
)
351 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
352 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
353 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
354 ERR("Process attempted to continue execution after noncontinuable exception.\n");
356 ERR("Unhandled exception code %lx flags %lx addr %p\n",
357 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
358 NtTerminateProcess( NtCurrentProcess(), 1 );
360 return STATUS_SUCCESS
;
364 /*******************************************************************
365 * NtRaiseException (NTDLL.@)
367 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
369 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
370 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
374 /***********************************************************************
375 * RtlRaiseException (NTDLL.@)
377 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
379 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
380 if (status
!= STATUS_SUCCESS
)
382 EXCEPTION_RECORD newrec
;
383 newrec
.ExceptionCode
= status
;
384 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
385 newrec
.ExceptionRecord
= rec
;
386 newrec
.NumberParameters
= 0;
387 RtlRaiseException( &newrec
); /* never returns */
391 /**********************************************************************/
393 #ifdef DEFINE_REGS_ENTRYPOINT
394 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 4, 4 );
396 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
399 memset( &context
, 0, sizeof(context
) );
400 __regs_RtlRaiseException( rec
, &context
);
405 /*******************************************************************
406 * RtlUnwind (NTDLL.@)
408 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID unusedEip
,
409 PEXCEPTION_RECORD pRecord
, PVOID returnEax
, CONTEXT
*context
)
411 EXCEPTION_RECORD record
, newrec
;
412 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
416 context
->Eax
= (DWORD
)returnEax
;
419 /* build an exception record, if we do not have one */
422 record
.ExceptionCode
= STATUS_UNWIND
;
423 record
.ExceptionFlags
= 0;
424 record
.ExceptionRecord
= NULL
;
425 record
.ExceptionAddress
= GET_IP(context
);
426 record
.NumberParameters
= 0;
430 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
432 TRACE( "code=%lx flags=%lx\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
434 /* get chain of exception frames */
435 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
436 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
438 /* Check frame address */
439 if (pEndFrame
&& (frame
> pEndFrame
))
441 newrec
.ExceptionCode
= STATUS_INVALID_UNWIND_TARGET
;
442 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
443 newrec
.ExceptionRecord
= pRecord
;
444 newrec
.NumberParameters
= 0;
445 RtlRaiseException( &newrec
); /* never returns */
447 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
448 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
451 newrec
.ExceptionCode
= STATUS_BAD_STACK
;
452 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
453 newrec
.ExceptionRecord
= pRecord
;
454 newrec
.NumberParameters
= 0;
455 RtlRaiseException( &newrec
); /* never returns */
459 TRACE( "calling handler at %p code=%lx flags=%lx\n",
460 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
461 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, EXC_UnwindHandler
);
462 TRACE( "handler at %p returned %lx\n", frame
->Handler
, res
);
466 case ExceptionContinueSearch
:
468 case ExceptionCollidedUnwind
:
472 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
473 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
474 newrec
.ExceptionRecord
= pRecord
;
475 newrec
.NumberParameters
= 0;
476 RtlRaiseException( &newrec
); /* never returns */
479 frame
= __wine_pop_frame( frame
);
483 /**********************************************************************/
485 #ifdef DEFINE_REGS_ENTRYPOINT
486 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 16, 16 );
488 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID unusedEip
,
489 PEXCEPTION_RECORD pRecord
, PVOID returnEax
)
492 memset( &context
, 0, sizeof(context
) );
493 __regs_RtlUnwind( pEndFrame
, unusedEip
, pRecord
, returnEax
, &context
);
498 /***********************************************************************
499 * RtlRaiseStatus (NTDLL.@)
501 * Raise an exception with ExceptionCode = status
503 void WINAPI
RtlRaiseStatus( NTSTATUS status
)
505 EXCEPTION_RECORD ExceptionRec
;
507 ExceptionRec
.ExceptionCode
= status
;
508 ExceptionRec
.ExceptionFlags
= EH_NONCONTINUABLE
;
509 ExceptionRec
.ExceptionRecord
= NULL
;
510 ExceptionRec
.NumberParameters
= 0;
511 RtlRaiseException( &ExceptionRec
);
515 /*******************************************************************
516 * RtlAddVectoredExceptionHandler (NTDLL.@)
518 PVOID WINAPI
RtlAddVectoredExceptionHandler( ULONG first
, PVECTORED_EXCEPTION_HANDLER func
)
520 VECTORED_HANDLER
*handler
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler
) );
523 handler
->func
= func
;
524 RtlEnterCriticalSection( &vectored_handlers_section
);
525 if (first
) list_add_head( &vectored_handlers
, &handler
->entry
);
526 else list_add_tail( &vectored_handlers
, &handler
->entry
);
527 RtlLeaveCriticalSection( &vectored_handlers_section
);
533 /*******************************************************************
534 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
536 ULONG WINAPI
RtlRemoveVectoredExceptionHandler( PVOID handler
)
541 RtlEnterCriticalSection( &vectored_handlers_section
);
542 LIST_FOR_EACH( ptr
, &vectored_handlers
)
544 VECTORED_HANDLER
*curr_handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
545 if (curr_handler
== handler
)
552 RtlLeaveCriticalSection( &vectored_handlers_section
);
553 if (ret
) RtlFreeHeap( GetProcessHeap(), 0, handler
);
558 /*************************************************************
559 * __wine_exception_handler (NTDLL.@)
561 * Exception handler for exception blocks declared in Wine code.
563 DWORD
__wine_exception_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
564 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
566 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
568 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
569 return ExceptionContinueSearch
;
571 if (wine_frame
->u
.filter
== (void *)1) /* special hack for page faults */
573 if (record
->ExceptionCode
!= STATUS_ACCESS_VIOLATION
)
574 return ExceptionContinueSearch
;
576 else if (wine_frame
->u
.filter
)
578 EXCEPTION_POINTERS ptrs
;
579 ptrs
.ExceptionRecord
= record
;
580 ptrs
.ContextRecord
= context
;
581 switch(wine_frame
->u
.filter( &ptrs
))
583 case EXCEPTION_CONTINUE_SEARCH
:
584 return ExceptionContinueSearch
;
585 case EXCEPTION_CONTINUE_EXECUTION
:
586 return ExceptionContinueExecution
;
587 case EXCEPTION_EXECUTE_HANDLER
:
590 MESSAGE( "Invalid return value from exception filter\n" );
594 /* hack to make GetExceptionCode() work in handler */
595 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
596 wine_frame
->ExceptionRecord
= wine_frame
;
598 RtlUnwind( frame
, 0, record
, 0 );
599 __wine_pop_frame( frame
);
600 siglongjmp( wine_frame
->jmp
, 1 );
604 /*************************************************************
605 * __wine_finally_handler (NTDLL.@)
607 * Exception handler for try/finally blocks declared in Wine code.
609 DWORD
__wine_finally_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
610 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
612 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
614 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
615 wine_frame
->u
.finally_func( FALSE
);
617 return ExceptionContinueSearch
;
621 /*************************************************************
622 * __wine_spec_unimplemented_stub
624 * ntdll-specific implementation to avoid depending on kernel functions.
625 * Can be removed once ntdll.spec no longer contains stubs.
627 void __wine_spec_unimplemented_stub( const char *module
, const char *function
)
629 EXCEPTION_RECORD record
;
631 record
.ExceptionCode
= EXCEPTION_WINE_STUB
;
632 record
.ExceptionFlags
= EH_NONCONTINUABLE
;
633 record
.ExceptionRecord
= NULL
;
634 record
.ExceptionAddress
= __wine_spec_unimplemented_stub
;
635 record
.NumberParameters
= 2;
636 record
.ExceptionInformation
[0] = (ULONG_PTR
)module
;
637 record
.ExceptionInformation
[1] = (ULONG_PTR
)function
;
638 RtlRaiseException( &record
);