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"
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
41 /* Exception record for handling exceptions happening inside exception handlers */
44 EXCEPTION_REGISTRATION_RECORD frame
;
45 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
49 # define GET_IP(context) ((LPVOID)(context)->Eip)
50 #elif defined(__sparc__)
51 # define GET_IP(context) ((LPVOID)(context)->pc)
52 #elif defined(__powerpc__)
53 # define GET_IP(context) ((LPVOID)(context)->Iar)
55 # error You must define GET_IP for this CPU
58 void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
59 void WINAPI
EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD
, LPVOID
,
60 PEXCEPTION_RECORD
, DWORD
, PCONTEXT
);
61 void WINAPI
EXC_NtRaiseException( PEXCEPTION_RECORD
, PCONTEXT
,
64 /*******************************************************************
67 * Handler for exceptions happening inside a handler.
69 static DWORD
EXC_RaiseHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
70 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
72 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
73 return ExceptionContinueSearch
;
74 /* We shouldn't get here so we store faulty frame in dispatcher */
75 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
76 return ExceptionNestedException
;
80 /*******************************************************************
83 * Handler for exceptions happening inside an unwind handler.
85 static DWORD
EXC_UnwindHandler( 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 ExceptionCollidedUnwind
;
96 /*******************************************************************
99 * Call an exception handler, setting up an exception frame to catch exceptions
100 * happening during the handler execution.
101 * Please do not change the first 4 parameters order in any way - some exceptions handlers
102 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
104 static DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
105 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
106 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
)
108 EXC_NESTED_FRAME newframe
;
111 newframe
.frame
.Handler
= nested_handler
;
112 newframe
.prevFrame
= frame
;
113 __wine_push_frame( &newframe
.frame
);
114 TRACE( "calling handler at %p code=%lx flags=%lx\n",
115 handler
, record
->ExceptionCode
, record
->ExceptionFlags
);
116 ret
= handler( record
, frame
, context
, dispatcher
);
117 TRACE( "handler returned %lx\n", ret
);
118 __wine_pop_frame( &newframe
.frame
);
123 /**********************************************************************
126 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
128 static int send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
133 SERVER_START_REQ( queue_exception_event
)
135 req
->first
= first_chance
;
136 wine_server_add_data( req
, context
, sizeof(*context
) );
137 wine_server_add_data( req
, rec
, sizeof(*rec
) );
138 if (!wine_server_call( req
)) handle
= reply
->handle
;
141 if (!handle
) return 0; /* no debugger present or other error */
143 /* No need to wait on the handle since the process gets suspended
144 * once the event is passed to the debugger, so when we get back
145 * here the event has been continued already.
147 SERVER_START_REQ( get_exception_status
)
149 req
->handle
= handle
;
150 wine_server_set_reply( req
, context
, sizeof(*context
) );
151 wine_server_call( req
);
160 /*******************************************************************
161 * EXC_DefaultHandling
163 * Default handling for exceptions. Called when we didn't find a suitable handler.
165 static void EXC_DefaultHandling( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
167 if (send_debug_event( rec
, FALSE
, context
) == DBG_CONTINUE
) return; /* continue execution */
169 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
170 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
171 else if (rec
->ExceptionCode
== EXCEPTION_NONCONTINUABLE_EXCEPTION
)
172 ERR("Process attempted to continue execution after noncontinuable exception.\n");
174 ERR("Unhandled exception code %lx flags %lx addr %p\n",
175 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
176 NtTerminateProcess( NtCurrentProcess(), 1 );
180 /***********************************************************************
181 * RtlRaiseException (NTDLL.@)
183 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException
, EXC_RtlRaiseException
, EXCEPTION_RECORD
* );
184 void WINAPI
EXC_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
186 PEXCEPTION_REGISTRATION_RECORD frame
, dispatch
, nested_frame
;
187 EXCEPTION_RECORD newrec
;
190 TRACE( "code=%lx flags=%lx addr=%p\n", rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
191 for (c
=0; c
<rec
->NumberParameters
; c
++) TRACE(" info[%ld]=%08lx\n", c
, rec
->ExceptionInformation
[c
]);
192 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
193 FIXME( "call to unimplemented function %s.%s\n",
194 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
196 if (send_debug_event( rec
, TRUE
, context
) == DBG_CONTINUE
) return; /* continue execution */
198 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
200 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
202 while (frame
!= (PEXCEPTION_REGISTRATION_RECORD
)~0UL)
204 /* Check frame address */
205 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
206 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
209 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
214 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, EXC_RaiseHandler
);
215 if (frame
== nested_frame
)
217 /* no longer nested */
219 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
224 case ExceptionContinueExecution
:
225 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return;
226 newrec
.ExceptionCode
= STATUS_NONCONTINUABLE_EXCEPTION
;
227 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
228 newrec
.ExceptionRecord
= rec
;
229 newrec
.NumberParameters
= 0;
230 RtlRaiseException( &newrec
); /* never returns */
232 case ExceptionContinueSearch
:
234 case ExceptionNestedException
:
235 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
236 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
239 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
240 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
241 newrec
.ExceptionRecord
= rec
;
242 newrec
.NumberParameters
= 0;
243 RtlRaiseException( &newrec
); /* never returns */
248 EXC_DefaultHandling( rec
, context
);
252 /*******************************************************************
253 * RtlUnwind (NTDLL.@)
255 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind
, EXC_RtlUnwind
,
256 PVOID
, PVOID
, PEXCEPTION_RECORD
, PVOID
);
257 void WINAPI
EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD pEndFrame
, LPVOID unusedEip
,
258 PEXCEPTION_RECORD pRecord
, DWORD returnEax
,
261 EXCEPTION_RECORD record
, newrec
;
262 PEXCEPTION_REGISTRATION_RECORD frame
, dispatch
;
265 context
->Eax
= returnEax
;
268 /* build an exception record, if we do not have one */
271 record
.ExceptionCode
= STATUS_UNWIND
;
272 record
.ExceptionFlags
= 0;
273 record
.ExceptionRecord
= NULL
;
274 record
.ExceptionAddress
= GET_IP(context
);
275 record
.NumberParameters
= 0;
279 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
281 TRACE( "code=%lx flags=%lx\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
283 /* get chain of exception frames */
284 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
285 while ((frame
!= (PEXCEPTION_REGISTRATION_RECORD
)~0UL) && (frame
!= pEndFrame
))
287 /* Check frame address */
288 if (pEndFrame
&& (frame
> pEndFrame
))
290 newrec
.ExceptionCode
= STATUS_INVALID_UNWIND_TARGET
;
291 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
292 newrec
.ExceptionRecord
= pRecord
;
293 newrec
.NumberParameters
= 0;
294 RtlRaiseException( &newrec
); /* never returns */
296 if (((void*)frame
< NtCurrentTeb()->Tib
.StackLimit
) ||
297 ((void*)(frame
+1) > NtCurrentTeb()->Tib
.StackBase
) ||
300 newrec
.ExceptionCode
= STATUS_BAD_STACK
;
301 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
302 newrec
.ExceptionRecord
= pRecord
;
303 newrec
.NumberParameters
= 0;
304 RtlRaiseException( &newrec
); /* never returns */
308 switch(EXC_CallHandler( pRecord
, frame
, context
, &dispatch
,
309 frame
->Handler
, EXC_UnwindHandler
))
311 case ExceptionContinueSearch
:
313 case ExceptionCollidedUnwind
:
317 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
318 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
319 newrec
.ExceptionRecord
= pRecord
;
320 newrec
.NumberParameters
= 0;
321 RtlRaiseException( &newrec
); /* never returns */
324 frame
= __wine_pop_frame( frame
);
329 /*******************************************************************
330 * NtRaiseException (NTDLL.@)
332 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException
, EXC_NtRaiseException
,
333 EXCEPTION_RECORD
*, CONTEXT
*, BOOL
);
334 void WINAPI
EXC_NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*ctx
,
335 BOOL first
, CONTEXT
*context
)
337 EXC_RtlRaiseException( rec
, ctx
);
342 /***********************************************************************
343 * RtlRaiseStatus (NTDLL.@)
345 * Raise an exception with ExceptionCode = status
347 void WINAPI
RtlRaiseStatus( NTSTATUS status
)
349 EXCEPTION_RECORD ExceptionRec
;
351 ExceptionRec
.ExceptionCode
= status
;
352 ExceptionRec
.ExceptionFlags
= EH_NONCONTINUABLE
;
353 ExceptionRec
.ExceptionRecord
= NULL
;
354 ExceptionRec
.NumberParameters
= 0;
355 RtlRaiseException( &ExceptionRec
);
359 /*************************************************************
360 * __wine_exception_handler (NTDLL.@)
362 * Exception handler for exception blocks declared in Wine code.
364 DWORD
__wine_exception_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
365 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
367 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
369 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
370 return ExceptionContinueSearch
;
371 if (wine_frame
->u
.filter
)
373 EXCEPTION_POINTERS ptrs
;
374 ptrs
.ExceptionRecord
= record
;
375 ptrs
.ContextRecord
= context
;
376 switch(wine_frame
->u
.filter( &ptrs
))
378 case EXCEPTION_CONTINUE_SEARCH
:
379 return ExceptionContinueSearch
;
380 case EXCEPTION_CONTINUE_EXECUTION
:
381 return ExceptionContinueExecution
;
382 case EXCEPTION_EXECUTE_HANDLER
:
385 MESSAGE( "Invalid return value from exception filter\n" );
389 /* hack to make GetExceptionCode() work in handler */
390 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
391 wine_frame
->ExceptionRecord
= wine_frame
;
393 RtlUnwind( frame
, 0, record
, 0 );
394 __wine_pop_frame( frame
);
395 longjmp( wine_frame
->jmp
, 1 );
399 /*************************************************************
400 * __wine_finally_handler (NTDLL.@)
402 * Exception handler for try/finally blocks declared in Wine code.
404 DWORD
__wine_finally_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
405 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
407 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
409 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
410 wine_frame
->u
.finally_func( FALSE
);
412 return ExceptionContinueSearch
;