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\n\t"
98 : "0" (func
), "r" (ebp
)
99 : "ecx", "edx", "memory" );
105 static DWORD
MSVCRT_nested_handler(PEXCEPTION_RECORD rec
,
106 EXCEPTION_REGISTRATION_RECORD
* frame
,
108 EXCEPTION_REGISTRATION_RECORD
** dispatch
)
110 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
111 return ExceptionContinueSearch
;
113 return ExceptionCollidedUnwind
;
117 /*********************************************************************
118 * _EH_prolog (MSVCRT.@)
121 /* Provided for VC++ binary compatibility only */
122 __ASM_GLOBAL_FUNC(_EH_prolog
,
126 "movl %esp, %fs:0\n\t"
127 "movl 12(%esp), %eax\n\t"
128 "movl %ebp, 12(%esp)\n\t"
129 "leal 12(%esp), %ebp\n\t"
133 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
, void *ebp
)
135 EXCEPTION_REGISTRATION_RECORD reg
;
137 TRACE("(%p,%d,%d)\n",frame
, frame
->trylevel
, trylevel
);
139 /* Register a handler in case of a nested exception */
140 reg
.Handler
= MSVCRT_nested_handler
;
141 reg
.Prev
= NtCurrentTeb()->Tib
.ExceptionList
;
142 __wine_push_frame(®
);
144 while (frame
->trylevel
!= TRYLEVEL_END
&& frame
->trylevel
!= trylevel
)
146 int level
= frame
->trylevel
;
147 frame
->trylevel
= frame
->scopetable
[level
].previousTryLevel
;
148 if (!frame
->scopetable
[level
].lpfnFilter
)
150 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
151 level
, frame
->scopetable
[level
].lpfnHandler
, ebp
);
152 call_unwind_func( frame
->scopetable
[level
].lpfnHandler
, ebp
);
155 __wine_pop_frame(®
);
156 TRACE("unwound OK\n");
159 /*******************************************************************
160 * _local_unwind2 (MSVCRT.@)
162 void CDECL
_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
)
164 msvcrt_local_unwind2( frame
, trylevel
, &frame
->_ebp
);
167 #endif /* __i386__ */
169 /*******************************************************************
170 * _global_unwind2 (MSVCRT.@)
172 void CDECL
_global_unwind2(EXCEPTION_REGISTRATION_RECORD
* frame
)
174 TRACE("(%p)\n",frame
);
175 RtlUnwind( frame
, 0, 0, 0 );
178 /*********************************************************************
179 * _except_handler2 (MSVCRT.@)
181 int CDECL
_except_handler2(PEXCEPTION_RECORD rec
,
182 EXCEPTION_REGISTRATION_RECORD
* frame
,
184 EXCEPTION_REGISTRATION_RECORD
** dispatcher
)
186 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
187 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
188 frame
->Handler
, context
, dispatcher
);
189 return ExceptionContinueSearch
;
192 /*********************************************************************
193 * _except_handler3 (MSVCRT.@)
195 int CDECL
_except_handler3(PEXCEPTION_RECORD rec
,
196 MSVCRT_EXCEPTION_FRAME
* frame
,
197 PCONTEXT context
, void* dispatcher
)
199 #if defined(__GNUC__) && defined(__i386__)
200 int retval
, trylevel
;
201 EXCEPTION_POINTERS exceptPtrs
;
202 PSCOPETABLE pScopeTable
;
204 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
205 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
206 frame
->handler
, context
, dispatcher
);
208 __asm__
__volatile__ ("cld");
210 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
212 /* Unwinding the current frame */
213 msvcrt_local_unwind2(frame
, TRYLEVEL_END
, &frame
->_ebp
);
214 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
215 return ExceptionContinueSearch
;
219 /* Hunting for handler */
220 exceptPtrs
.ExceptionRecord
= rec
;
221 exceptPtrs
.ContextRecord
= context
;
222 *((DWORD
*)frame
-1) = (DWORD
)&exceptPtrs
;
223 trylevel
= frame
->trylevel
;
224 pScopeTable
= frame
->scopetable
;
226 while (trylevel
!= TRYLEVEL_END
)
228 if (pScopeTable
[trylevel
].lpfnFilter
)
230 TRACE("filter = %p\n", pScopeTable
[trylevel
].lpfnFilter
);
232 retval
= call_filter( pScopeTable
[trylevel
].lpfnFilter
, &exceptPtrs
, &frame
->_ebp
);
234 TRACE("filter returned %s\n", retval
== EXCEPTION_CONTINUE_EXECUTION
?
235 "CONTINUE_EXECUTION" : retval
== EXCEPTION_EXECUTE_HANDLER
?
236 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
238 if (retval
== EXCEPTION_CONTINUE_EXECUTION
)
239 return ExceptionContinueExecution
;
241 if (retval
== EXCEPTION_EXECUTE_HANDLER
)
243 /* Unwind all higher frames, this one will handle the exception */
244 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)frame
);
245 msvcrt_local_unwind2(frame
, trylevel
, &frame
->_ebp
);
247 /* Set our trylevel to the enclosing block, and call the __finally
248 * code, which won't return
250 frame
->trylevel
= pScopeTable
->previousTryLevel
;
251 TRACE("__finally block %p\n",pScopeTable
[trylevel
].lpfnHandler
);
252 call_finally_block(pScopeTable
[trylevel
].lpfnHandler
, &frame
->_ebp
);
253 ERR("Returned from __finally block - expect crash!\n");
256 trylevel
= pScopeTable
->previousTryLevel
;
260 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
261 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
262 frame
->handler
, context
, dispatcher
);
264 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
265 return ExceptionContinueSearch
;
268 /*********************************************************************
269 * _abnormal_termination (MSVCRT.@)
271 int CDECL
_abnormal_termination(void)
273 FIXME("(void)stub\n");
278 * setjmp/longjmp implementation
282 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
283 typedef void (__stdcall
*MSVCRT_unwind_function
)(const struct MSVCRT___JUMP_BUFFER
*);
285 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
286 /* and then jumps to the C backend function */
287 #define DEFINE_SETJMP_ENTRYPOINT(name) \
288 __ASM_GLOBAL_FUNC( name, \
289 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
290 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
291 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
292 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
293 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
294 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
295 "movl 0(%esp),%eax\n\t" \
296 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
297 "jmp " __ASM_NAME("__regs_") # name )
299 /* restore the registers from the jmp buf upon longjmp */
300 extern void DECLSPEC_NORETURN
longjmp_set_regs( struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
);
301 __ASM_GLOBAL_FUNC( longjmp_set_regs
,
302 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
303 "movl 8(%esp),%eax\n\t" /* retval */
304 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
305 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
306 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
307 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
308 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
309 "addl $4,%esp\n\t" /* get rid of return address */
310 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
313 * The signatures of the setjmp/longjmp functions do not match that
314 * declared in the setjmp header so they don't follow the regular naming
315 * convention to avoid conflicts.
318 /*******************************************************************
321 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp
)
322 int CDECL
__regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER
*jmp
)
324 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
325 if (jmp
->Registration
== ~0UL)
326 jmp
->TryLevel
= TRYLEVEL_END
;
328 jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
330 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
331 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
335 /*******************************************************************
336 * _setjmp3 (MSVCRT.@)
338 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3
)
339 int CDECL
__regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER
*jmp
, int nb_args
, ...)
341 jmp
->Cookie
= MSVCRT_JMP_MAGIC
;
343 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
344 if (jmp
->Registration
== ~0UL)
346 jmp
->TryLevel
= TRYLEVEL_END
;
353 va_start( args
, nb_args
);
354 if (nb_args
> 0) jmp
->UnwindFunc
= va_arg( args
, unsigned long );
355 if (nb_args
> 1) jmp
->TryLevel
= va_arg( args
, unsigned long );
356 else jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
357 for (i
= 0; i
< 6 && i
< nb_args
- 2; i
++)
358 jmp
->UnwindData
[i
] = va_arg( args
, unsigned long );
362 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
363 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
367 /*********************************************************************
370 int CDECL
MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
)
372 unsigned long cur_frame
= 0;
374 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
375 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
, retval
);
377 cur_frame
=(unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
378 TRACE("cur_frame=%lx\n",cur_frame
);
380 if (cur_frame
!= jmp
->Registration
)
381 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)jmp
->Registration
);
383 if (jmp
->Registration
)
385 if (!IsBadReadPtr(&jmp
->Cookie
, sizeof(long)) &&
386 jmp
->Cookie
== MSVCRT_JMP_MAGIC
&& jmp
->UnwindFunc
)
388 MSVCRT_unwind_function unwind_func
;
390 unwind_func
=(MSVCRT_unwind_function
)jmp
->UnwindFunc
;
394 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
,
395 jmp
->TryLevel
, (void *)jmp
->Ebp
);
401 longjmp_set_regs( jmp
, retval
);
404 /*********************************************************************
405 * _seh_longjmp_unwind (MSVCRT.@)
407 void __stdcall
_seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER
*jmp
)
409 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
, jmp
->TryLevel
, (void *)jmp
->Ebp
);
413 static MSVCRT___sighandler_t sighandlers
[MSVCRT_NSIG
] = { MSVCRT_SIG_DFL
};
415 static BOOL WINAPI
msvcrt_console_handler(DWORD ctrlType
)
422 if (sighandlers
[MSVCRT_SIGINT
])
424 if (sighandlers
[MSVCRT_SIGINT
] != MSVCRT_SIG_IGN
)
425 sighandlers
[MSVCRT_SIGINT
](MSVCRT_SIGINT
);
433 typedef void (*float_handler
)(int, int);
435 /* The exception codes are actually NTSTATUS values */
440 } float_exception_map
[] = {
441 { EXCEPTION_FLT_DENORMAL_OPERAND
, _FPE_DENORMAL
},
442 { EXCEPTION_FLT_DIVIDE_BY_ZERO
, _FPE_ZERODIVIDE
},
443 { EXCEPTION_FLT_INEXACT_RESULT
, _FPE_INEXACT
},
444 { EXCEPTION_FLT_INVALID_OPERATION
, _FPE_INVALID
},
445 { EXCEPTION_FLT_OVERFLOW
, _FPE_OVERFLOW
},
446 { EXCEPTION_FLT_STACK_CHECK
, _FPE_STACKOVERFLOW
},
447 { EXCEPTION_FLT_UNDERFLOW
, _FPE_UNDERFLOW
},
450 static LONG WINAPI
msvcrt_exception_filter(struct _EXCEPTION_POINTERS
*except
)
452 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
453 MSVCRT___sighandler_t handler
;
455 if (!except
|| !except
->ExceptionRecord
)
456 return EXCEPTION_CONTINUE_SEARCH
;
458 switch (except
->ExceptionRecord
->ExceptionCode
)
460 case EXCEPTION_ACCESS_VIOLATION
:
461 if ((handler
= sighandlers
[MSVCRT_SIGSEGV
]) != MSVCRT_SIG_DFL
)
463 if (handler
!= MSVCRT_SIG_IGN
)
465 sighandlers
[MSVCRT_SIGSEGV
] = MSVCRT_SIG_DFL
;
466 handler(MSVCRT_SIGSEGV
);
468 ret
= EXCEPTION_CONTINUE_EXECUTION
;
472 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
473 * the FPE signal handler takes as a second argument the type of
474 * floating point exception.
476 case EXCEPTION_FLT_DENORMAL_OPERAND
:
477 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
478 case EXCEPTION_FLT_INEXACT_RESULT
:
479 case EXCEPTION_FLT_INVALID_OPERATION
:
480 case EXCEPTION_FLT_OVERFLOW
:
481 case EXCEPTION_FLT_STACK_CHECK
:
482 case EXCEPTION_FLT_UNDERFLOW
:
483 if ((handler
= sighandlers
[MSVCRT_SIGFPE
]) != MSVCRT_SIG_DFL
)
485 if (handler
!= MSVCRT_SIG_IGN
)
487 int i
, float_signal
= _FPE_INVALID
;
489 sighandlers
[MSVCRT_SIGFPE
] = MSVCRT_SIG_DFL
;
490 for (i
= 0; i
< sizeof(float_exception_map
) /
491 sizeof(float_exception_map
[0]); i
++)
493 if (float_exception_map
[i
].status
==
494 except
->ExceptionRecord
->ExceptionCode
)
496 float_signal
= float_exception_map
[i
].signal
;
500 ((float_handler
)handler
)(MSVCRT_SIGFPE
, float_signal
);
502 ret
= EXCEPTION_CONTINUE_EXECUTION
;
505 case EXCEPTION_ILLEGAL_INSTRUCTION
:
506 if ((handler
= sighandlers
[MSVCRT_SIGILL
]) != MSVCRT_SIG_DFL
)
508 if (handler
!= MSVCRT_SIG_IGN
)
510 sighandlers
[MSVCRT_SIGILL
] = MSVCRT_SIG_DFL
;
511 handler(MSVCRT_SIGILL
);
513 ret
= EXCEPTION_CONTINUE_EXECUTION
;
520 void msvcrt_init_signals(void)
522 SetConsoleCtrlHandler(msvcrt_console_handler
, TRUE
);
523 SetUnhandledExceptionFilter(msvcrt_exception_filter
);
526 void msvcrt_free_signals(void)
528 SetConsoleCtrlHandler(msvcrt_console_handler
, FALSE
);
529 SetUnhandledExceptionFilter(NULL
);
532 /*********************************************************************
534 * MS signal handling is described here:
535 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
536 * Some signals may never be generated except through an explicit call to
539 MSVCRT___sighandler_t CDECL
MSVCRT_signal(int sig
, MSVCRT___sighandler_t func
)
541 MSVCRT___sighandler_t ret
= MSVCRT_SIG_ERR
;
543 TRACE("(%d, %p)\n", sig
, func
);
545 if (func
== MSVCRT_SIG_ERR
) return MSVCRT_SIG_ERR
;
549 /* Cases handled internally. Note SIGTERM is never generated by Windows,
550 * so we effectively mask it.
558 ret
= sighandlers
[sig
];
559 sighandlers
[sig
] = func
;
562 ret
= MSVCRT_SIG_ERR
;
567 /*********************************************************************
570 int CDECL
MSVCRT_raise(int sig
)
572 MSVCRT___sighandler_t handler
;
574 TRACE("(%d)\n", sig
);
584 handler
= sighandlers
[sig
];
585 if (handler
== MSVCRT_SIG_DFL
) MSVCRT__exit(3);
586 if (handler
!= MSVCRT_SIG_IGN
)
588 sighandlers
[sig
] = MSVCRT_SIG_DFL
;
589 if (sig
== MSVCRT_SIGFPE
)
590 ((float_handler
)handler
)(sig
, _FPE_EXPLICITGEN
);
601 /*********************************************************************
602 * _XcptFilter (MSVCRT.@)
604 int CDECL
_XcptFilter(NTSTATUS ex
, PEXCEPTION_POINTERS ptr
)
606 TRACE("(%08x,%p)\n", ex
, ptr
);
607 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
608 return msvcrt_exception_filter(ptr
);