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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
31 #define WIN32_NO_STATUS
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 RTL_CRITICAL_SECTION vectored_handlers_section
;
59 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
61 0, 0, &vectored_handlers_section
,
62 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
63 0, 0, { (DWORD_PTR
)(__FILE__
": vectored_handlers_section") }
65 static RTL_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)
73 #elif defined(__ALPHA__)
74 # define GET_IP(context) ((LPVOID)(context)->Fir)
75 #elif defined(__x86_64__)
76 # define GET_IP(context) ((LPVOID)(context)->Rip)
78 # 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 ret
= handler( record
, frame
, context
, dispatcher
);
133 __wine_pop_frame( &newframe
.frame
);
137 /* in signal_i386.c */
138 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
139 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
140 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
143 /**********************************************************************
146 * Wait until the thread is no longer suspended.
148 void wait_suspend( CONTEXT
*context
)
150 LARGE_INTEGER timeout
;
151 int saved_errno
= errno
;
153 /* store the context we got at suspend time */
154 SERVER_START_REQ( set_thread_context
)
156 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
157 req
->flags
= CONTEXT_FULL
;
159 wine_server_add_data( req
, context
, sizeof(*context
) );
160 wine_server_call( req
);
164 /* wait with 0 timeout, will only return once the thread is no longer suspended */
165 timeout
.QuadPart
= 0;
166 NTDLL_wait_for_multiple_objects( 0, NULL
, SELECT_INTERRUPTIBLE
, &timeout
, 0 );
168 /* retrieve the new context */
169 SERVER_START_REQ( get_thread_context
)
171 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
172 req
->flags
= CONTEXT_FULL
;
174 wine_server_set_reply( req
, context
, sizeof(*context
) );
175 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 client_ptr_t params
[EXCEPTION_MAXIMUM_PARAMETERS
];
195 if (!NtCurrentTeb()->Peb
->BeingDebugged
) return 0; /* no debugger present */
197 for (i
= 0; i
< min( rec
->NumberParameters
, EXCEPTION_MAXIMUM_PARAMETERS
); i
++)
198 params
[i
] = rec
->ExceptionInformation
[i
];
200 SERVER_START_REQ( queue_exception_event
)
202 req
->first
= first_chance
;
203 req
->code
= rec
->ExceptionCode
;
204 req
->flags
= rec
->ExceptionFlags
;
205 req
->record
= wine_server_client_ptr( rec
->ExceptionRecord
);
206 req
->address
= wine_server_client_ptr( rec
->ExceptionAddress
);
207 req
->len
= i
* sizeof(params
[0]);
208 wine_server_add_data( req
, params
, req
->len
);
209 wine_server_add_data( req
, context
, sizeof(*context
) );
210 if (!wine_server_call( req
)) handle
= wine_server_ptr_handle( reply
->handle
);
213 if (!handle
) return 0;
215 NTDLL_wait_for_multiple_objects( 1, &handle
, SELECT_INTERRUPTIBLE
, NULL
, 0 );
217 SERVER_START_REQ( get_exception_status
)
219 req
->handle
= wine_server_obj_handle( handle
);
220 wine_server_set_reply( req
, context
, sizeof(*context
) );
221 ret
= wine_server_call( req
);
228 /**********************************************************************
229 * call_vectored_handlers
231 * Call the vectored handlers chain.
233 static LONG
call_vectored_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
236 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
237 EXCEPTION_POINTERS except_ptrs
;
239 except_ptrs
.ExceptionRecord
= rec
;
240 except_ptrs
.ContextRecord
= context
;
242 RtlEnterCriticalSection( &vectored_handlers_section
);
243 LIST_FOR_EACH( ptr
, &vectored_handlers
)
245 VECTORED_HANDLER
*handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
246 ret
= handler
->func( &except_ptrs
);
247 if (ret
== EXCEPTION_CONTINUE_EXECUTION
) break;
249 RtlLeaveCriticalSection( &vectored_handlers_section
);
254 /**********************************************************************
255 * call_stack_handlers
257 * Call the stack handlers chain.
259 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
261 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
264 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
266 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
268 /* Check frame address */
269 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
270 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
271 (ULONG_PTR
)frame
& 3)
273 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
278 TRACE( "calling handler at %p code=%x flags=%x\n",
279 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
280 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, EXC_RaiseHandler
);
281 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
283 if (frame
== nested_frame
)
285 /* no longer nested */
287 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
292 case ExceptionContinueExecution
:
293 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
294 return STATUS_NONCONTINUABLE_EXCEPTION
;
295 case ExceptionContinueSearch
:
297 case ExceptionNestedException
:
298 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
299 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
302 return STATUS_INVALID_DISPOSITION
;
306 return STATUS_UNHANDLED_EXCEPTION
;
310 /*******************************************************************
313 * Implementation of NtRaiseException.
315 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
323 TRACE( "code=%x flags=%x addr=%p\n",
324 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
325 for (c
= 0; c
< rec
->NumberParameters
; c
++)
326 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
327 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
329 if (rec
->ExceptionInformation
[1] >> 16)
330 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
331 rec
->ExceptionAddress
,
332 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
334 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
335 rec
->ExceptionAddress
,
336 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
341 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
342 context
->Eax
, context
->Ebx
, context
->Ecx
,
343 context
->Edx
, context
->Esi
, context
->Edi
);
344 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
345 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
346 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
349 status
= send_debug_event( rec
, TRUE
, context
);
350 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
351 return STATUS_SUCCESS
;
354 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
355 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
358 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
359 return STATUS_SUCCESS
;
361 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
365 /* last chance exception */
367 status
= send_debug_event( rec
, FALSE
, context
);
368 if (status
!= DBG_CONTINUE
)
370 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
371 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
372 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
373 ERR("Process attempted to continue execution after noncontinuable exception.\n");
375 ERR("Unhandled exception code %x flags %x addr %p\n",
376 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
377 NtTerminateProcess( NtCurrentProcess(), 1 );
379 return STATUS_SUCCESS
;
383 /*******************************************************************
384 * NtRaiseException (NTDLL.@)
386 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
388 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
389 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
393 /***********************************************************************
394 * RtlCaptureContext (NTDLL.@)
396 void WINAPI
__regs_RtlCaptureContext( CONTEXT
*context_out
, CONTEXT
*context_in
)
398 *context_out
= *context_in
;
401 /**********************************************************************/
403 #ifdef DEFINE_REGS_ENTRYPOINT
404 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext
, 4, 4 )
406 void WINAPI
RtlCaptureContext( CONTEXT
*context_out
)
408 memset( context_out
, 0, sizeof(*context_out
) );
413 /***********************************************************************
414 * RtlRaiseException (NTDLL.@)
416 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
418 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
419 if (status
!= STATUS_SUCCESS
)
421 EXCEPTION_RECORD newrec
;
422 newrec
.ExceptionCode
= status
;
423 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
424 newrec
.ExceptionRecord
= rec
;
425 newrec
.NumberParameters
= 0;
426 RtlRaiseException( &newrec
); /* never returns */
430 /**********************************************************************/
432 #ifdef DEFINE_REGS_ENTRYPOINT
433 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 4, 4 )
435 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
438 RtlCaptureContext( &context
);
439 __regs_RtlRaiseException( rec
, &context
);
444 /*******************************************************************
445 * RtlUnwind (NTDLL.@)
447 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID unusedEip
,
448 PEXCEPTION_RECORD pRecord
, PVOID returnEax
, CONTEXT
*context
)
450 EXCEPTION_RECORD record
, newrec
;
451 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
455 context
->Eax
= (DWORD
)returnEax
;
458 /* build an exception record, if we do not have one */
461 record
.ExceptionCode
= STATUS_UNWIND
;
462 record
.ExceptionFlags
= 0;
463 record
.ExceptionRecord
= NULL
;
464 record
.ExceptionAddress
= GET_IP(context
);
465 record
.NumberParameters
= 0;
469 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
471 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
473 /* get chain of exception frames */
474 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
475 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
477 /* Check frame address */
478 if (pEndFrame
&& (frame
> pEndFrame
))
480 newrec
.ExceptionCode
= STATUS_INVALID_UNWIND_TARGET
;
481 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
482 newrec
.ExceptionRecord
= pRecord
;
483 newrec
.NumberParameters
= 0;
484 RtlRaiseException( &newrec
); /* never returns */
486 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
487 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
490 newrec
.ExceptionCode
= STATUS_BAD_STACK
;
491 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
492 newrec
.ExceptionRecord
= pRecord
;
493 newrec
.NumberParameters
= 0;
494 RtlRaiseException( &newrec
); /* never returns */
498 TRACE( "calling handler at %p code=%x flags=%x\n",
499 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
500 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, EXC_UnwindHandler
);
501 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
505 case ExceptionContinueSearch
:
507 case ExceptionCollidedUnwind
:
511 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
512 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
513 newrec
.ExceptionRecord
= pRecord
;
514 newrec
.NumberParameters
= 0;
515 RtlRaiseException( &newrec
); /* never returns */
518 frame
= __wine_pop_frame( frame
);
522 /**********************************************************************/
524 #ifdef DEFINE_REGS_ENTRYPOINT
525 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 16, 16 )
527 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID unusedEip
,
528 PEXCEPTION_RECORD pRecord
, PVOID returnEax
)
531 RtlCaptureContext( &context
);
532 __regs_RtlUnwind( pEndFrame
, unusedEip
, pRecord
, returnEax
, &context
);
537 /***********************************************************************
538 * RtlRaiseStatus (NTDLL.@)
540 * Raise an exception with ExceptionCode = status
542 void WINAPI
RtlRaiseStatus( NTSTATUS status
)
544 EXCEPTION_RECORD ExceptionRec
;
546 ExceptionRec
.ExceptionCode
= status
;
547 ExceptionRec
.ExceptionFlags
= EH_NONCONTINUABLE
;
548 ExceptionRec
.ExceptionRecord
= NULL
;
549 ExceptionRec
.NumberParameters
= 0;
550 RtlRaiseException( &ExceptionRec
);
554 /*******************************************************************
555 * RtlAddVectoredExceptionHandler (NTDLL.@)
557 PVOID WINAPI
RtlAddVectoredExceptionHandler( ULONG first
, PVECTORED_EXCEPTION_HANDLER func
)
559 VECTORED_HANDLER
*handler
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler
) );
562 handler
->func
= func
;
563 RtlEnterCriticalSection( &vectored_handlers_section
);
564 if (first
) list_add_head( &vectored_handlers
, &handler
->entry
);
565 else list_add_tail( &vectored_handlers
, &handler
->entry
);
566 RtlLeaveCriticalSection( &vectored_handlers_section
);
572 /*******************************************************************
573 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
575 ULONG WINAPI
RtlRemoveVectoredExceptionHandler( PVOID handler
)
580 RtlEnterCriticalSection( &vectored_handlers_section
);
581 LIST_FOR_EACH( ptr
, &vectored_handlers
)
583 VECTORED_HANDLER
*curr_handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
584 if (curr_handler
== handler
)
591 RtlLeaveCriticalSection( &vectored_handlers_section
);
592 if (ret
) RtlFreeHeap( GetProcessHeap(), 0, handler
);
597 /*************************************************************
598 * __wine_spec_unimplemented_stub
600 * ntdll-specific implementation to avoid depending on kernel functions.
601 * Can be removed once ntdll.spec no longer contains stubs.
603 void __wine_spec_unimplemented_stub( const char *module
, const char *function
)
605 EXCEPTION_RECORD record
;
607 record
.ExceptionCode
= EXCEPTION_WINE_STUB
;
608 record
.ExceptionFlags
= EH_NONCONTINUABLE
;
609 record
.ExceptionRecord
= NULL
;
610 record
.ExceptionAddress
= __wine_spec_unimplemented_stub
;
611 record
.NumberParameters
= 2;
612 record
.ExceptionInformation
[0] = (ULONG_PTR
)module
;
613 record
.ExceptionInformation
[1] = (ULONG_PTR
)function
;
614 for (;;) RtlRaiseException( &record
);