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
21 * FIXME: Incomplete support for nested exceptions/try block cleanup.
25 #include "wine/port.h"
32 #include "wine/exception.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
40 /* VC++ extensions to Win32 SEH */
41 typedef struct _SCOPETABLE
44 int (*lpfnFilter
)(PEXCEPTION_POINTERS
);
45 int (*lpfnHandler
)(void);
46 } SCOPETABLE
, *PSCOPETABLE
;
48 typedef struct _MSVCRT_EXCEPTION_FRAME
50 EXCEPTION_REGISTRATION_RECORD
*prev
;
51 void (*handler
)(PEXCEPTION_RECORD
, EXCEPTION_REGISTRATION_RECORD
*,
52 PCONTEXT
, PEXCEPTION_RECORD
);
53 PSCOPETABLE scopetable
;
56 PEXCEPTION_POINTERS xpointers
;
57 } MSVCRT_EXCEPTION_FRAME
;
59 #define TRYLEVEL_END (-1) /* End of trylevel list */
61 #if defined(__GNUC__) && defined(__i386__)
62 static inline void call_finally_block( void *code_block
, void *base_ptr
)
64 __asm__
__volatile__ ("movl %1,%%ebp; call *%%eax"
65 : : "a" (code_block
), "g" (base_ptr
));
68 static inline int call_filter( int (*func
)(PEXCEPTION_POINTERS
), void *arg
, void *ebp
)
71 __asm__
__volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
73 : "0" (func
), "r" (ebp
), "r" (arg
)
74 : "ecx", "edx", "memory" );
78 static inline int call_unwind_func( int (*func
)(void), void *ebp
)
81 __asm__
__volatile__ ("pushl %%ebp\n\t"
92 : "0" (func
), "r" (ebp
)
93 : "ecx", "edx", "memory" );
99 static DWORD
MSVCRT_nested_handler(PEXCEPTION_RECORD rec
,
100 EXCEPTION_REGISTRATION_RECORD
* frame
,
102 EXCEPTION_REGISTRATION_RECORD
** dispatch
)
104 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
105 return ExceptionContinueSearch
;
107 return ExceptionCollidedUnwind
;
111 /*********************************************************************
112 * _EH_prolog (MSVCRT.@)
115 /* Provided for VC++ binary compatibility only */
116 __ASM_GLOBAL_FUNC(_EH_prolog
,
120 "movl %esp, %fs:0\n\t"
121 "movl 12(%esp), %eax\n\t"
122 "movl %ebp, 12(%esp)\n\t"
123 "leal 12(%esp), %ebp\n\t"
127 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
, void *ebp
)
129 EXCEPTION_REGISTRATION_RECORD reg
;
131 TRACE("(%p,%d,%d)\n",frame
, frame
->trylevel
, trylevel
);
133 /* Register a handler in case of a nested exception */
134 reg
.Handler
= MSVCRT_nested_handler
;
135 reg
.Prev
= NtCurrentTeb()->Tib
.ExceptionList
;
136 __wine_push_frame(®
);
138 while (frame
->trylevel
!= TRYLEVEL_END
&& frame
->trylevel
!= trylevel
)
140 int level
= frame
->trylevel
;
141 frame
->trylevel
= frame
->scopetable
[level
].previousTryLevel
;
142 if (!frame
->scopetable
[level
].lpfnFilter
)
144 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
145 level
, frame
->scopetable
[level
].lpfnHandler
, ebp
);
146 call_unwind_func( frame
->scopetable
[level
].lpfnHandler
, ebp
);
149 __wine_pop_frame(®
);
150 TRACE("unwound OK\n");
153 /*******************************************************************
154 * _local_unwind2 (MSVCRT.@)
156 void CDECL
_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
)
158 msvcrt_local_unwind2( frame
, trylevel
, &frame
->_ebp
);
161 #endif /* __i386__ */
163 /*******************************************************************
164 * _global_unwind2 (MSVCRT.@)
166 void CDECL
_global_unwind2(EXCEPTION_REGISTRATION_RECORD
* frame
)
168 TRACE("(%p)\n",frame
);
169 RtlUnwind( frame
, 0, 0, 0 );
172 /*********************************************************************
173 * _except_handler2 (MSVCRT.@)
175 int CDECL
_except_handler2(PEXCEPTION_RECORD rec
,
176 EXCEPTION_REGISTRATION_RECORD
* frame
,
178 EXCEPTION_REGISTRATION_RECORD
** dispatcher
)
180 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
181 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
182 frame
->Handler
, context
, dispatcher
);
183 return ExceptionContinueSearch
;
186 /*********************************************************************
187 * _except_handler3 (MSVCRT.@)
189 int CDECL
_except_handler3(PEXCEPTION_RECORD rec
,
190 MSVCRT_EXCEPTION_FRAME
* frame
,
191 PCONTEXT context
, void* dispatcher
)
193 #if defined(__GNUC__) && defined(__i386__)
194 int retval
, trylevel
;
195 EXCEPTION_POINTERS exceptPtrs
;
196 PSCOPETABLE pScopeTable
;
198 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
199 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
200 frame
->handler
, context
, dispatcher
);
202 __asm__
__volatile__ ("cld");
204 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
206 /* Unwinding the current frame */
207 msvcrt_local_unwind2(frame
, TRYLEVEL_END
, &frame
->_ebp
);
208 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
209 return ExceptionContinueSearch
;
213 /* Hunting for handler */
214 exceptPtrs
.ExceptionRecord
= rec
;
215 exceptPtrs
.ContextRecord
= context
;
216 *((DWORD
*)frame
-1) = (DWORD
)&exceptPtrs
;
217 trylevel
= frame
->trylevel
;
218 pScopeTable
= frame
->scopetable
;
220 while (trylevel
!= TRYLEVEL_END
)
222 if (pScopeTable
[trylevel
].lpfnFilter
)
224 TRACE("filter = %p\n", pScopeTable
[trylevel
].lpfnFilter
);
226 retval
= call_filter( pScopeTable
[trylevel
].lpfnFilter
, &exceptPtrs
, &frame
->_ebp
);
228 TRACE("filter returned %s\n", retval
== EXCEPTION_CONTINUE_EXECUTION
?
229 "CONTINUE_EXECUTION" : retval
== EXCEPTION_EXECUTE_HANDLER
?
230 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
232 if (retval
== EXCEPTION_CONTINUE_EXECUTION
)
233 return ExceptionContinueExecution
;
235 if (retval
== EXCEPTION_EXECUTE_HANDLER
)
237 /* Unwind all higher frames, this one will handle the exception */
238 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)frame
);
239 msvcrt_local_unwind2(frame
, trylevel
, &frame
->_ebp
);
241 /* Set our trylevel to the enclosing block, and call the __finally
242 * code, which won't return
244 frame
->trylevel
= pScopeTable
->previousTryLevel
;
245 TRACE("__finally block %p\n",pScopeTable
[trylevel
].lpfnHandler
);
246 call_finally_block(pScopeTable
[trylevel
].lpfnHandler
, &frame
->_ebp
);
247 ERR("Returned from __finally block - expect crash!\n");
250 trylevel
= pScopeTable
->previousTryLevel
;
254 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
255 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
256 frame
->handler
, context
, dispatcher
);
258 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
259 return ExceptionContinueSearch
;
262 /*********************************************************************
263 * _abnormal_termination (MSVCRT.@)
265 int CDECL
_abnormal_termination(void)
267 FIXME("(void)stub\n");
272 * setjmp/longjmp implementation
276 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
277 typedef void (__stdcall
*MSVCRT_unwind_function
)(const struct MSVCRT___JUMP_BUFFER
*);
279 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
280 /* and then jumps to the C backend function */
281 #define DEFINE_SETJMP_ENTRYPOINT(name) \
282 __ASM_GLOBAL_FUNC( name, \
283 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
284 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
285 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
286 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
287 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
288 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
289 "movl 0(%esp),%eax\n\t" \
290 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
291 "jmp " __ASM_NAME("__regs_") # name )
293 /* restore the registers from the jmp buf upon longjmp */
294 extern void DECLSPEC_NORETURN
longjmp_set_regs( struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
);
295 __ASM_GLOBAL_FUNC( longjmp_set_regs
,
296 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
297 "movl 8(%esp),%eax\n\t" /* retval */
298 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
299 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
300 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
301 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
302 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
303 "addl $4,%esp\n\t" /* get rid of return address */
304 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
307 * The signatures of the setjmp/longjmp functions do not match that
308 * declared in the setjmp header so they don't follow the regular naming
309 * convention to avoid conflicts.
312 /*******************************************************************
315 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp
)
316 int CDECL
__regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER
*jmp
)
318 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
319 if (jmp
->Registration
== ~0UL)
320 jmp
->TryLevel
= TRYLEVEL_END
;
322 jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
324 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
325 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
329 /*******************************************************************
330 * _setjmp3 (MSVCRT.@)
332 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3
)
333 int CDECL
__regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER
*jmp
, int nb_args
, ...)
335 jmp
->Cookie
= MSVCRT_JMP_MAGIC
;
337 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
338 if (jmp
->Registration
== ~0UL)
340 jmp
->TryLevel
= TRYLEVEL_END
;
347 va_start( args
, nb_args
);
348 if (nb_args
> 0) jmp
->UnwindFunc
= va_arg( args
, unsigned long );
349 if (nb_args
> 1) jmp
->TryLevel
= va_arg( args
, unsigned long );
350 else jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
351 for (i
= 0; i
< 6 && i
< nb_args
- 2; i
++)
352 jmp
->UnwindData
[i
] = va_arg( args
, unsigned long );
356 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
357 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
361 /*********************************************************************
364 int CDECL
MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
)
366 unsigned long cur_frame
= 0;
368 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
369 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
, retval
);
371 cur_frame
=(unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
372 TRACE("cur_frame=%lx\n",cur_frame
);
374 if (cur_frame
!= jmp
->Registration
)
375 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)jmp
->Registration
);
377 if (jmp
->Registration
)
379 if (!IsBadReadPtr(&jmp
->Cookie
, sizeof(long)) &&
380 jmp
->Cookie
== MSVCRT_JMP_MAGIC
&& jmp
->UnwindFunc
)
382 MSVCRT_unwind_function unwind_func
;
384 unwind_func
=(MSVCRT_unwind_function
)jmp
->UnwindFunc
;
388 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
,
389 jmp
->TryLevel
, (void *)jmp
->Ebp
);
395 longjmp_set_regs( jmp
, retval
);
398 /*********************************************************************
399 * _seh_longjmp_unwind (MSVCRT.@)
401 void __stdcall
_seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER
*jmp
)
403 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
, jmp
->TryLevel
, (void *)jmp
->Ebp
);
407 static MSVCRT___sighandler_t sighandlers
[MSVCRT_NSIG
] = { MSVCRT_SIG_DFL
};
409 static BOOL WINAPI
msvcrt_console_handler(DWORD ctrlType
)
416 if (sighandlers
[MSVCRT_SIGINT
])
418 if (sighandlers
[MSVCRT_SIGINT
] != MSVCRT_SIG_IGN
)
419 sighandlers
[MSVCRT_SIGINT
](MSVCRT_SIGINT
);
427 typedef void (*float_handler
)(int, int);
429 /* The exception codes are actually NTSTATUS values */
434 } float_exception_map
[] = {
435 { EXCEPTION_FLT_DENORMAL_OPERAND
, MSVCRT__FPE_DENORMAL
},
436 { EXCEPTION_FLT_DIVIDE_BY_ZERO
, MSVCRT__FPE_ZERODIVIDE
},
437 { EXCEPTION_FLT_INEXACT_RESULT
, MSVCRT__FPE_INEXACT
},
438 { EXCEPTION_FLT_INVALID_OPERATION
, MSVCRT__FPE_INVALID
},
439 { EXCEPTION_FLT_OVERFLOW
, MSVCRT__FPE_OVERFLOW
},
440 { EXCEPTION_FLT_STACK_CHECK
, MSVCRT__FPE_STACKOVERFLOW
},
441 { EXCEPTION_FLT_UNDERFLOW
, MSVCRT__FPE_UNDERFLOW
},
444 static LONG WINAPI
msvcrt_exception_filter(struct _EXCEPTION_POINTERS
*except
)
446 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
447 MSVCRT___sighandler_t handler
;
449 if (!except
|| !except
->ExceptionRecord
)
450 return EXCEPTION_CONTINUE_SEARCH
;
452 switch (except
->ExceptionRecord
->ExceptionCode
)
454 case EXCEPTION_ACCESS_VIOLATION
:
455 if ((handler
= sighandlers
[MSVCRT_SIGSEGV
]) != MSVCRT_SIG_DFL
)
457 if (handler
!= MSVCRT_SIG_IGN
)
459 sighandlers
[MSVCRT_SIGSEGV
] = MSVCRT_SIG_DFL
;
460 handler(MSVCRT_SIGSEGV
);
462 ret
= EXCEPTION_CONTINUE_EXECUTION
;
465 /* According to msdn,
466 * the FPE signal handler takes as a second argument the type of
467 * floating point exception.
469 case EXCEPTION_FLT_DENORMAL_OPERAND
:
470 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
471 case EXCEPTION_FLT_INEXACT_RESULT
:
472 case EXCEPTION_FLT_INVALID_OPERATION
:
473 case EXCEPTION_FLT_OVERFLOW
:
474 case EXCEPTION_FLT_STACK_CHECK
:
475 case EXCEPTION_FLT_UNDERFLOW
:
476 if ((handler
= sighandlers
[MSVCRT_SIGFPE
]) != MSVCRT_SIG_DFL
)
478 if (handler
!= MSVCRT_SIG_IGN
)
481 int float_signal
= MSVCRT__FPE_INVALID
;
483 sighandlers
[MSVCRT_SIGFPE
] = MSVCRT_SIG_DFL
;
484 for (i
= 0; i
< sizeof(float_exception_map
) /
485 sizeof(float_exception_map
[0]); i
++)
487 if (float_exception_map
[i
].status
==
488 except
->ExceptionRecord
->ExceptionCode
)
490 float_signal
= float_exception_map
[i
].signal
;
494 ((float_handler
)handler
)(MSVCRT_SIGFPE
, float_signal
);
496 ret
= EXCEPTION_CONTINUE_EXECUTION
;
499 case EXCEPTION_ILLEGAL_INSTRUCTION
:
500 case EXCEPTION_PRIV_INSTRUCTION
:
501 if ((handler
= sighandlers
[MSVCRT_SIGILL
]) != MSVCRT_SIG_DFL
)
503 if (handler
!= MSVCRT_SIG_IGN
)
505 sighandlers
[MSVCRT_SIGILL
] = MSVCRT_SIG_DFL
;
506 handler(MSVCRT_SIGILL
);
508 ret
= EXCEPTION_CONTINUE_EXECUTION
;
515 void msvcrt_init_signals(void)
517 SetConsoleCtrlHandler(msvcrt_console_handler
, TRUE
);
518 SetUnhandledExceptionFilter(msvcrt_exception_filter
);
521 void msvcrt_free_signals(void)
523 SetConsoleCtrlHandler(msvcrt_console_handler
, FALSE
);
524 SetUnhandledExceptionFilter(NULL
);
527 /*********************************************************************
529 * Some signals may never be generated except through an explicit call to
532 MSVCRT___sighandler_t CDECL
MSVCRT_signal(int sig
, MSVCRT___sighandler_t func
)
534 MSVCRT___sighandler_t ret
= MSVCRT_SIG_ERR
;
536 TRACE("(%d, %p)\n", sig
, func
);
538 if (func
== MSVCRT_SIG_ERR
) return MSVCRT_SIG_ERR
;
542 /* Cases handled internally. Note SIGTERM is never generated by Windows,
543 * so we effectively mask it.
551 ret
= sighandlers
[sig
];
552 sighandlers
[sig
] = func
;
555 ret
= MSVCRT_SIG_ERR
;
560 /*********************************************************************
563 int CDECL
MSVCRT_raise(int sig
)
565 MSVCRT___sighandler_t handler
;
567 TRACE("(%d)\n", sig
);
577 handler
= sighandlers
[sig
];
578 if (handler
== MSVCRT_SIG_DFL
) MSVCRT__exit(3);
579 if (handler
!= MSVCRT_SIG_IGN
)
581 sighandlers
[sig
] = MSVCRT_SIG_DFL
;
582 if (sig
== MSVCRT_SIGFPE
)
583 ((float_handler
)handler
)(sig
, MSVCRT__FPE_EXPLICITGEN
);
594 /*********************************************************************
595 * _XcptFilter (MSVCRT.@)
597 int CDECL
_XcptFilter(NTSTATUS ex
, PEXCEPTION_POINTERS ptr
)
599 TRACE("(%08x,%p)\n", ex
, ptr
);
600 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
601 return msvcrt_exception_filter(ptr
);