2 * msvcrt.dll exception handling
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2005 Juan Lang
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 * See http://www.microsoft.com/msj/0197/exception/exception.htm,
24 * but don't believe all of it.
26 * FIXME: Incomplete support for nested exceptions/try block cleanup.
30 #include "wine/port.h"
37 #include "wine/exception.h"
41 #include "msvcrt/float.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
46 /* VC++ extensions to Win32 SEH */
47 typedef struct _SCOPETABLE
50 int (*lpfnFilter
)(PEXCEPTION_POINTERS
);
51 int (*lpfnHandler
)(void);
52 } SCOPETABLE
, *PSCOPETABLE
;
54 typedef struct _MSVCRT_EXCEPTION_FRAME
56 EXCEPTION_REGISTRATION_RECORD
*prev
;
57 void (*handler
)(PEXCEPTION_RECORD
, EXCEPTION_REGISTRATION_RECORD
*,
58 PCONTEXT
, PEXCEPTION_RECORD
);
59 PSCOPETABLE scopetable
;
62 PEXCEPTION_POINTERS xpointers
;
63 } MSVCRT_EXCEPTION_FRAME
;
65 #define TRYLEVEL_END (-1) /* End of trylevel list */
67 #if defined(__GNUC__) && defined(__i386__)
68 static inline void call_finally_block( void *code_block
, void *base_ptr
)
70 __asm__
__volatile__ ("movl %1,%%ebp; call *%%eax"
71 : : "a" (code_block
), "g" (base_ptr
));
74 static inline int call_filter( int (*func
)(PEXCEPTION_POINTERS
), void *arg
, void *ebp
)
77 __asm__
__volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
79 : "0" (func
), "r" (ebp
), "r" (arg
)
80 : "ecx", "edx", "memory" );
84 static inline int call_unwind_func( int (*func
)(void), void *ebp
)
87 __asm__
__volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%0; popl %%ebp"
89 : "0" (func
), "r" (ebp
)
90 : "ecx", "edx", "memory" );
96 static DWORD
MSVCRT_nested_handler(PEXCEPTION_RECORD rec
,
97 EXCEPTION_REGISTRATION_RECORD
* frame
,
99 EXCEPTION_REGISTRATION_RECORD
** dispatch
)
101 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
102 return ExceptionContinueSearch
;
104 return ExceptionCollidedUnwind
;
108 /*********************************************************************
109 * _EH_prolog (MSVCRT.@)
112 /* Provided for VC++ binary compatibility only */
113 __ASM_GLOBAL_FUNC(_EH_prolog
,
117 "movl %esp, %fs:0\n\t"
118 "movl 12(%esp), %eax\n\t"
119 "movl %ebp, 12(%esp)\n\t"
120 "leal 12(%esp), %ebp\n\t"
124 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
, void *ebp
)
126 EXCEPTION_REGISTRATION_RECORD reg
;
128 TRACE("(%p,%d,%d)\n",frame
, frame
->trylevel
, trylevel
);
130 /* Register a handler in case of a nested exception */
131 reg
.Handler
= MSVCRT_nested_handler
;
132 reg
.Prev
= NtCurrentTeb()->Tib
.ExceptionList
;
133 __wine_push_frame(®
);
135 while (frame
->trylevel
!= TRYLEVEL_END
&& frame
->trylevel
!= trylevel
)
137 int level
= frame
->trylevel
;
138 frame
->trylevel
= frame
->scopetable
[level
].previousTryLevel
;
139 if (!frame
->scopetable
[level
].lpfnFilter
)
141 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
142 level
, frame
->scopetable
[level
].lpfnHandler
, ebp
);
143 call_unwind_func( frame
->scopetable
[level
].lpfnHandler
, ebp
);
146 __wine_pop_frame(®
);
147 TRACE("unwound OK\n");
150 /*******************************************************************
151 * _local_unwind2 (MSVCRT.@)
153 void CDECL
_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
)
155 msvcrt_local_unwind2( frame
, trylevel
, &frame
->_ebp
);
158 #endif /* __i386__ */
160 /*******************************************************************
161 * _global_unwind2 (MSVCRT.@)
163 void CDECL
_global_unwind2(EXCEPTION_REGISTRATION_RECORD
* frame
)
165 TRACE("(%p)\n",frame
);
166 RtlUnwind( frame
, 0, 0, 0 );
169 /*********************************************************************
170 * _except_handler2 (MSVCRT.@)
172 int CDECL
_except_handler2(PEXCEPTION_RECORD rec
,
173 EXCEPTION_REGISTRATION_RECORD
* frame
,
175 EXCEPTION_REGISTRATION_RECORD
** dispatcher
)
177 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
178 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
179 frame
->Handler
, context
, dispatcher
);
180 return ExceptionContinueSearch
;
183 /*********************************************************************
184 * _except_handler3 (MSVCRT.@)
186 int CDECL
_except_handler3(PEXCEPTION_RECORD rec
,
187 MSVCRT_EXCEPTION_FRAME
* frame
,
188 PCONTEXT context
, void* dispatcher
)
190 #if defined(__GNUC__) && defined(__i386__)
191 int retval
, trylevel
;
192 EXCEPTION_POINTERS exceptPtrs
;
193 PSCOPETABLE pScopeTable
;
195 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
196 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
197 frame
->handler
, context
, dispatcher
);
199 __asm__
__volatile__ ("cld");
201 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
203 /* Unwinding the current frame */
204 msvcrt_local_unwind2(frame
, TRYLEVEL_END
, &frame
->_ebp
);
205 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
206 return ExceptionContinueSearch
;
210 /* Hunting for handler */
211 exceptPtrs
.ExceptionRecord
= rec
;
212 exceptPtrs
.ContextRecord
= context
;
213 *((DWORD
*)frame
-1) = (DWORD
)&exceptPtrs
;
214 trylevel
= frame
->trylevel
;
215 pScopeTable
= frame
->scopetable
;
217 while (trylevel
!= TRYLEVEL_END
)
219 if (pScopeTable
[trylevel
].lpfnFilter
)
221 TRACE("filter = %p\n", pScopeTable
[trylevel
].lpfnFilter
);
223 retval
= call_filter( pScopeTable
[trylevel
].lpfnFilter
, &exceptPtrs
, &frame
->_ebp
);
225 TRACE("filter returned %s\n", retval
== EXCEPTION_CONTINUE_EXECUTION
?
226 "CONTINUE_EXECUTION" : retval
== EXCEPTION_EXECUTE_HANDLER
?
227 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
229 if (retval
== EXCEPTION_CONTINUE_EXECUTION
)
230 return ExceptionContinueExecution
;
232 if (retval
== EXCEPTION_EXECUTE_HANDLER
)
234 /* Unwind all higher frames, this one will handle the exception */
235 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)frame
);
236 msvcrt_local_unwind2(frame
, trylevel
, &frame
->_ebp
);
238 /* Set our trylevel to the enclosing block, and call the __finally
239 * code, which won't return
241 frame
->trylevel
= pScopeTable
->previousTryLevel
;
242 TRACE("__finally block %p\n",pScopeTable
[trylevel
].lpfnHandler
);
243 call_finally_block(pScopeTable
[trylevel
].lpfnHandler
, &frame
->_ebp
);
244 ERR("Returned from __finally block - expect crash!\n");
247 trylevel
= pScopeTable
->previousTryLevel
;
251 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
252 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
253 frame
->handler
, context
, dispatcher
);
255 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
256 return ExceptionContinueSearch
;
259 /*********************************************************************
260 * _abnormal_termination (MSVCRT.@)
262 int CDECL
_abnormal_termination(void)
264 FIXME("(void)stub\n");
269 * setjmp/longjmp implementation
273 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
274 typedef void (__stdcall
*MSVCRT_unwind_function
)(const struct MSVCRT___JUMP_BUFFER
*);
276 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
277 /* and then jumps to the C backend function */
278 #define DEFINE_SETJMP_ENTRYPOINT(name) \
279 __ASM_GLOBAL_FUNC( name, \
280 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
281 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
282 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
283 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
284 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
285 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
286 "movl 0(%esp),%eax\n\t" \
287 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
288 "jmp " __ASM_NAME("__regs_") # name )
290 /* restore the registers from the jmp buf upon longjmp */
291 extern void DECLSPEC_NORETURN
longjmp_set_regs( struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
);
292 __ASM_GLOBAL_FUNC( longjmp_set_regs
,
293 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
294 "movl 8(%esp),%eax\n\t" /* retval */
295 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
296 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
297 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
298 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
299 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
300 "addl $4,%esp\n\t" /* get rid of return address */
301 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
304 * The signatures of the setjmp/longjmp functions do not match that
305 * declared in the setjmp header so they don't follow the regular naming
306 * convention to avoid conflicts.
309 /*******************************************************************
312 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp
)
313 int CDECL
__regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER
*jmp
)
315 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
316 if (jmp
->Registration
== ~0UL)
317 jmp
->TryLevel
= TRYLEVEL_END
;
319 jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
321 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
322 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
326 /*******************************************************************
327 * _setjmp3 (MSVCRT.@)
329 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3
)
330 int CDECL
__regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER
*jmp
, int nb_args
, ...)
332 jmp
->Cookie
= MSVCRT_JMP_MAGIC
;
334 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
335 if (jmp
->Registration
== ~0UL)
337 jmp
->TryLevel
= TRYLEVEL_END
;
344 va_start( args
, nb_args
);
345 if (nb_args
> 0) jmp
->UnwindFunc
= va_arg( args
, unsigned long );
346 if (nb_args
> 1) jmp
->TryLevel
= va_arg( args
, unsigned long );
347 else jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
348 for (i
= 0; i
< 6 && i
< nb_args
- 2; i
++)
349 jmp
->UnwindData
[i
] = va_arg( args
, unsigned long );
353 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
354 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
358 /*********************************************************************
361 int CDECL
MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
)
363 unsigned long cur_frame
= 0;
365 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
366 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
, retval
);
368 cur_frame
=(unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
369 TRACE("cur_frame=%lx\n",cur_frame
);
371 if (cur_frame
!= jmp
->Registration
)
372 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)jmp
->Registration
);
374 if (jmp
->Registration
)
376 if (!IsBadReadPtr(&jmp
->Cookie
, sizeof(long)) &&
377 jmp
->Cookie
== MSVCRT_JMP_MAGIC
&& jmp
->UnwindFunc
)
379 MSVCRT_unwind_function unwind_func
;
381 unwind_func
=(MSVCRT_unwind_function
)jmp
->UnwindFunc
;
385 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
,
386 jmp
->TryLevel
, (void *)jmp
->Ebp
);
392 longjmp_set_regs( jmp
, retval
);
395 /*********************************************************************
396 * _seh_longjmp_unwind (MSVCRT.@)
398 void __stdcall
_seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER
*jmp
)
400 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
, jmp
->TryLevel
, (void *)jmp
->Ebp
);
404 static MSVCRT___sighandler_t sighandlers
[MSVCRT_NSIG
] = { MSVCRT_SIG_DFL
};
406 static BOOL WINAPI
msvcrt_console_handler(DWORD ctrlType
)
413 if (sighandlers
[MSVCRT_SIGINT
])
415 if (sighandlers
[MSVCRT_SIGINT
] != MSVCRT_SIG_IGN
)
416 sighandlers
[MSVCRT_SIGINT
](MSVCRT_SIGINT
);
424 typedef void (*float_handler
)(int, int);
426 /* The exception codes are actually NTSTATUS values */
431 } float_exception_map
[] = {
432 { EXCEPTION_FLT_DENORMAL_OPERAND
, _FPE_DENORMAL
},
433 { EXCEPTION_FLT_DIVIDE_BY_ZERO
, _FPE_ZERODIVIDE
},
434 { EXCEPTION_FLT_INEXACT_RESULT
, _FPE_INEXACT
},
435 { EXCEPTION_FLT_INVALID_OPERATION
, _FPE_INVALID
},
436 { EXCEPTION_FLT_OVERFLOW
, _FPE_OVERFLOW
},
437 { EXCEPTION_FLT_STACK_CHECK
, _FPE_STACKOVERFLOW
},
438 { EXCEPTION_FLT_UNDERFLOW
, _FPE_UNDERFLOW
},
441 static LONG WINAPI
msvcrt_exception_filter(struct _EXCEPTION_POINTERS
*except
)
443 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
444 MSVCRT___sighandler_t handler
;
446 if (!except
|| !except
->ExceptionRecord
)
447 return EXCEPTION_CONTINUE_SEARCH
;
449 switch (except
->ExceptionRecord
->ExceptionCode
)
451 case EXCEPTION_ACCESS_VIOLATION
:
452 if ((handler
= sighandlers
[MSVCRT_SIGSEGV
]) != MSVCRT_SIG_DFL
)
454 if (handler
!= MSVCRT_SIG_IGN
)
456 sighandlers
[MSVCRT_SIGSEGV
] = MSVCRT_SIG_DFL
;
457 handler(MSVCRT_SIGSEGV
);
459 ret
= EXCEPTION_CONTINUE_EXECUTION
;
463 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
464 * the FPE signal handler takes as a second argument the type of
465 * floating point exception.
467 case EXCEPTION_FLT_DENORMAL_OPERAND
:
468 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
469 case EXCEPTION_FLT_INEXACT_RESULT
:
470 case EXCEPTION_FLT_INVALID_OPERATION
:
471 case EXCEPTION_FLT_OVERFLOW
:
472 case EXCEPTION_FLT_STACK_CHECK
:
473 case EXCEPTION_FLT_UNDERFLOW
:
474 if ((handler
= sighandlers
[MSVCRT_SIGFPE
]) != MSVCRT_SIG_DFL
)
476 if (handler
!= MSVCRT_SIG_IGN
)
478 int i
, float_signal
= _FPE_INVALID
;
480 sighandlers
[MSVCRT_SIGFPE
] = MSVCRT_SIG_DFL
;
481 for (i
= 0; i
< sizeof(float_exception_map
) /
482 sizeof(float_exception_map
[0]); i
++)
484 if (float_exception_map
[i
].status
==
485 except
->ExceptionRecord
->ExceptionCode
)
487 float_signal
= float_exception_map
[i
].signal
;
491 ((float_handler
)handler
)(MSVCRT_SIGFPE
, float_signal
);
493 ret
= EXCEPTION_CONTINUE_EXECUTION
;
496 case EXCEPTION_ILLEGAL_INSTRUCTION
:
497 if ((handler
= sighandlers
[MSVCRT_SIGILL
]) != MSVCRT_SIG_DFL
)
499 if (handler
!= MSVCRT_SIG_IGN
)
501 sighandlers
[MSVCRT_SIGILL
] = MSVCRT_SIG_DFL
;
502 handler(MSVCRT_SIGILL
);
504 ret
= EXCEPTION_CONTINUE_EXECUTION
;
511 void msvcrt_init_signals(void)
513 SetConsoleCtrlHandler(msvcrt_console_handler
, TRUE
);
514 SetUnhandledExceptionFilter(msvcrt_exception_filter
);
517 void msvcrt_free_signals(void)
519 SetConsoleCtrlHandler(msvcrt_console_handler
, FALSE
);
520 SetUnhandledExceptionFilter(NULL
);
523 /*********************************************************************
525 * MS signal handling is described here:
526 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
527 * Some signals may never be generated except through an explicit call to
530 MSVCRT___sighandler_t CDECL
MSVCRT_signal(int sig
, MSVCRT___sighandler_t func
)
532 MSVCRT___sighandler_t ret
= MSVCRT_SIG_ERR
;
534 TRACE("(%d, %p)\n", sig
, func
);
536 if (func
== MSVCRT_SIG_ERR
) return MSVCRT_SIG_ERR
;
540 /* Cases handled internally. Note SIGTERM is never generated by Windows,
541 * so we effectively mask it.
549 ret
= sighandlers
[sig
];
550 sighandlers
[sig
] = func
;
553 ret
= MSVCRT_SIG_ERR
;
558 /*********************************************************************
561 int CDECL
MSVCRT_raise(int sig
)
563 MSVCRT___sighandler_t handler
;
565 TRACE("(%d)\n", sig
);
575 handler
= sighandlers
[sig
];
576 if (handler
== MSVCRT_SIG_DFL
) MSVCRT__exit(3);
577 if (handler
!= MSVCRT_SIG_IGN
)
579 sighandlers
[sig
] = MSVCRT_SIG_DFL
;
580 if (sig
== MSVCRT_SIGFPE
)
581 ((float_handler
)handler
)(sig
, _FPE_EXPLICITGEN
);
592 /*********************************************************************
593 * _XcptFilter (MSVCRT.@)
595 int CDECL
_XcptFilter(NTSTATUS ex
, PEXCEPTION_POINTERS ptr
)
597 TRACE("(%08x,%p)\n", ex
, ptr
);
598 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
599 return msvcrt_exception_filter(ptr
);