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"
31 #include "wine/exception.h"
32 #include "stackframe.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
36 #include "msvcrt/excpt.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
40 /* Exception record for handling exceptions happening inside exception handlers */
43 EXCEPTION_FRAME frame
;
44 EXCEPTION_FRAME
*prevFrame
;
48 # define GET_IP(context) ((LPVOID)(context)->Eip)
51 # define GET_IP(context) ((LPVOID)(context)->pc)
54 # error You must define GET_IP for this CPU
57 extern void SIGNAL_Unblock(void);
59 void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
60 void WINAPI
EXC_RtlUnwind( PEXCEPTION_FRAME
, LPVOID
,
61 PEXCEPTION_RECORD
, DWORD
, PCONTEXT
);
62 void WINAPI
EXC_NtRaiseException( PEXCEPTION_RECORD
, PCONTEXT
,
65 /*******************************************************************
68 * Handler for exceptions happening inside a handler.
70 static DWORD
EXC_RaiseHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_FRAME
*frame
,
71 CONTEXT
*context
, EXCEPTION_FRAME
**dispatcher
)
73 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
74 return ExceptionContinueSearch
;
75 /* We shouldn't get here so we store faulty frame in dispatcher */
76 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
77 return ExceptionNestedException
;
81 /*******************************************************************
84 * Handler for exceptions happening inside an unwind handler.
86 static DWORD
EXC_UnwindHandler( EXCEPTION_RECORD
*rec
, EXCEPTION_FRAME
*frame
,
87 CONTEXT
*context
, EXCEPTION_FRAME
**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 ExceptionCollidedUnwind
;
97 /*******************************************************************
100 * Call an exception handler, setting up an exception frame to catch exceptions
101 * happening during the handler execution.
102 * Please do not change the first 4 parameters order in any way - some exceptions handlers
103 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
105 static DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_FRAME
*frame
,
106 CONTEXT
*context
, EXCEPTION_FRAME
**dispatcher
,
107 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
)
109 EXC_NESTED_FRAME newframe
;
112 newframe
.frame
.Handler
= nested_handler
;
113 newframe
.prevFrame
= frame
;
114 __wine_push_frame( &newframe
.frame
);
115 TRACE( "calling handler at %p code=%lx flags=%lx\n",
116 handler
, record
->ExceptionCode
, record
->ExceptionFlags
);
117 ret
= handler( record
, frame
, context
, dispatcher
);
118 TRACE( "handler returned %lx\n", ret
);
119 __wine_pop_frame( &newframe
.frame
);
124 /**********************************************************************
127 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
129 static int send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
134 SERVER_START_REQ( queue_exception_event
)
136 req
->first
= first_chance
;
137 wine_server_add_data( req
, context
, sizeof(*context
) );
138 wine_server_add_data( req
, rec
, sizeof(*rec
) );
139 if (!wine_server_call( req
)) handle
= reply
->handle
;
142 if (!handle
) return 0; /* no debugger present or other error */
144 /* No need to wait on the handle since the process gets suspended
145 * once the event is passed to the debugger, so when we get back
146 * here the event has been continued already.
148 SERVER_START_REQ( get_exception_status
)
150 req
->handle
= handle
;
151 wine_server_set_reply( req
, context
, sizeof(*context
) );
152 wine_server_call( req
);
161 /*******************************************************************
162 * EXC_DefaultHandling
164 * Default handling for exceptions. Called when we didn't find a suitable handler.
166 static void EXC_DefaultHandling( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
168 if (send_debug_event( rec
, FALSE
, context
) == DBG_CONTINUE
) return; /* continue execution */
170 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
171 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
172 else if (rec
->ExceptionCode
== EXCEPTION_NONCONTINUABLE_EXCEPTION
)
173 ERR("Process attempted to continue execution after noncontinuable exception.\n");
175 ERR("Unhandled exception code %lx flags %lx addr %p\n",
176 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
177 NtTerminateProcess( NtCurrentProcess(), 1 );
181 /***********************************************************************
182 * RtlRaiseException (NTDLL.@)
184 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException
, EXC_RtlRaiseException
, EXCEPTION_RECORD
* );
185 void WINAPI
EXC_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
187 PEXCEPTION_FRAME frame
, dispatch
, nested_frame
;
188 EXCEPTION_RECORD newrec
;
191 TRACE( "code=%lx flags=%lx addr=%p\n", rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
192 for (c
=0; c
<rec
->NumberParameters
; c
++) TRACE(" info[%ld]=%08lx\n", c
, rec
->ExceptionInformation
[c
]);
193 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
) TRACE(" stub=%s\n", (char*)rec
->ExceptionInformation
[1]);
195 if (send_debug_event( rec
, TRUE
, context
) == DBG_CONTINUE
) return; /* continue execution */
197 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
199 frame
= NtCurrentTeb()->except
;
201 while (frame
!= (PEXCEPTION_FRAME
)0xFFFFFFFF)
203 /* Check frame address */
204 if (((void*)frame
< NtCurrentTeb()->stack_low
) ||
205 ((void*)(frame
+1) > NtCurrentTeb()->stack_top
) ||
208 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
213 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, EXC_RaiseHandler
);
214 if (frame
== nested_frame
)
216 /* no longer nested */
218 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
223 case ExceptionContinueExecution
:
224 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return;
225 newrec
.ExceptionCode
= STATUS_NONCONTINUABLE_EXCEPTION
;
226 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
227 newrec
.ExceptionRecord
= rec
;
228 newrec
.NumberParameters
= 0;
229 RtlRaiseException( &newrec
); /* never returns */
231 case ExceptionContinueSearch
:
233 case ExceptionNestedException
:
234 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
235 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
238 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
239 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
240 newrec
.ExceptionRecord
= rec
;
241 newrec
.NumberParameters
= 0;
242 RtlRaiseException( &newrec
); /* never returns */
247 EXC_DefaultHandling( rec
, context
);
251 /*******************************************************************
252 * RtlUnwind (NTDLL.@)
254 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind
, EXC_RtlUnwind
,
255 PEXCEPTION_FRAME
, LPVOID
, PEXCEPTION_RECORD
, DWORD
);
256 void WINAPI
EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame
, LPVOID unusedEip
,
257 PEXCEPTION_RECORD pRecord
, DWORD returnEax
,
260 EXCEPTION_RECORD record
, newrec
;
261 PEXCEPTION_FRAME frame
, dispatch
;
264 context
->Eax
= returnEax
;
267 /* build an exception record, if we do not have one */
270 record
.ExceptionCode
= STATUS_UNWIND
;
271 record
.ExceptionFlags
= 0;
272 record
.ExceptionRecord
= NULL
;
273 record
.ExceptionAddress
= GET_IP(context
);
274 record
.NumberParameters
= 0;
278 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
280 TRACE( "code=%lx flags=%lx\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
282 /* get chain of exception frames */
283 frame
= NtCurrentTeb()->except
;
284 while ((frame
!= (PEXCEPTION_FRAME
)0xffffffff) && (frame
!= pEndFrame
))
286 /* Check frame address */
287 if (pEndFrame
&& (frame
> pEndFrame
))
289 newrec
.ExceptionCode
= STATUS_INVALID_UNWIND_TARGET
;
290 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
291 newrec
.ExceptionRecord
= pRecord
;
292 newrec
.NumberParameters
= 0;
293 RtlRaiseException( &newrec
); /* never returns */
295 if (((void*)frame
< NtCurrentTeb()->stack_low
) ||
296 ((void*)(frame
+1) > NtCurrentTeb()->stack_top
) ||
299 newrec
.ExceptionCode
= STATUS_BAD_STACK
;
300 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
301 newrec
.ExceptionRecord
= pRecord
;
302 newrec
.NumberParameters
= 0;
303 RtlRaiseException( &newrec
); /* never returns */
307 switch(EXC_CallHandler( pRecord
, frame
, context
, &dispatch
,
308 frame
->Handler
, EXC_UnwindHandler
))
310 case ExceptionContinueSearch
:
312 case ExceptionCollidedUnwind
:
316 newrec
.ExceptionCode
= STATUS_INVALID_DISPOSITION
;
317 newrec
.ExceptionFlags
= EH_NONCONTINUABLE
;
318 newrec
.ExceptionRecord
= pRecord
;
319 newrec
.NumberParameters
= 0;
320 RtlRaiseException( &newrec
); /* never returns */
323 frame
= __wine_pop_frame( frame
);
328 /*******************************************************************
329 * NtRaiseException (NTDLL.@)
331 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException
, EXC_NtRaiseException
,
332 EXCEPTION_RECORD
*, CONTEXT
*, BOOL
);
333 void WINAPI
EXC_NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*ctx
,
334 BOOL first
, CONTEXT
*context
)
336 EXC_RtlRaiseException( rec
, ctx
);
341 /***********************************************************************
342 * RtlRaiseStatus (NTDLL.@)
344 * Raise an exception with ExceptionCode = status
346 void WINAPI
RtlRaiseStatus( NTSTATUS status
)
348 EXCEPTION_RECORD ExceptionRec
;
350 ExceptionRec
.ExceptionCode
= status
;
351 ExceptionRec
.ExceptionFlags
= EH_NONCONTINUABLE
;
352 ExceptionRec
.ExceptionRecord
= NULL
;
353 ExceptionRec
.NumberParameters
= 0;
354 RtlRaiseException( &ExceptionRec
);
358 /*************************************************************
359 * __wine_exception_handler (NTDLL.@)
361 * Exception handler for exception blocks declared in Wine code.
363 DWORD
__wine_exception_handler( EXCEPTION_RECORD
*record
, EXCEPTION_FRAME
*frame
,
364 CONTEXT
*context
, LPVOID pdispatcher
)
366 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
368 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
369 return ExceptionContinueSearch
;
370 if (wine_frame
->u
.filter
)
372 EXCEPTION_POINTERS ptrs
;
373 ptrs
.ExceptionRecord
= record
;
374 ptrs
.ContextRecord
= context
;
375 switch(wine_frame
->u
.filter( &ptrs
))
377 case EXCEPTION_CONTINUE_SEARCH
:
378 return ExceptionContinueSearch
;
379 case EXCEPTION_CONTINUE_EXECUTION
:
380 return ExceptionContinueExecution
;
381 case EXCEPTION_EXECUTE_HANDLER
:
384 MESSAGE( "Invalid return value from exception filter\n" );
388 /* hack to make GetExceptionCode() work in handler */
389 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
390 wine_frame
->ExceptionRecord
= wine_frame
;
392 RtlUnwind( frame
, 0, record
, 0 );
393 __wine_pop_frame( frame
);
394 longjmp( wine_frame
->jmp
, 1 );
398 /*************************************************************
399 * __wine_finally_handler (NTDLL.@)
401 * Exception handler for try/finally blocks declared in Wine code.
403 DWORD
__wine_finally_handler( EXCEPTION_RECORD
*record
, EXCEPTION_FRAME
*frame
,
404 CONTEXT
*context
, LPVOID pdispatcher
)
406 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
408 if (!(record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
409 return ExceptionContinueSearch
;
410 wine_frame
->u
.finally_func( FALSE
);
411 return ExceptionContinueSearch
;