2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll exception handling
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2005-05-13 19:44:28 +02:00
|
|
|
* Copyright 2005 Juan Lang
|
2001-01-11 00:59:25 +01:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
*
|
2001-01-11 00:59:25 +01:00
|
|
|
* See http://www.microsoft.com/msj/0197/exception/exception.htm,
|
|
|
|
* but don't believe all of it.
|
|
|
|
*
|
2001-01-12 21:41:03 +01:00
|
|
|
* FIXME: Incomplete support for nested exceptions/try block cleanup.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2002-04-25 23:40:56 +02:00
|
|
|
|
2001-06-29 21:19:53 +02:00
|
|
|
#include "config.h"
|
2002-04-25 23:40:56 +02:00
|
|
|
#include "wine/port.h"
|
2001-06-29 21:19:53 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2001-01-12 21:41:03 +01:00
|
|
|
#include "wine/exception.h"
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
2002-12-13 00:34:01 +01:00
|
|
|
#include "excpt.h"
|
2005-05-13 19:44:28 +02:00
|
|
|
#include "wincon.h"
|
|
|
|
#include "msvcrt/float.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2006-01-05 14:36:37 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(seh);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* VC++ extensions to Win32 SEH */
|
|
|
|
typedef struct _SCOPETABLE
|
|
|
|
{
|
2002-10-10 19:56:48 +02:00
|
|
|
int previousTryLevel;
|
2001-04-10 23:16:07 +02:00
|
|
|
int (*lpfnFilter)(PEXCEPTION_POINTERS);
|
|
|
|
int (*lpfnHandler)(void);
|
2001-01-11 00:59:25 +01:00
|
|
|
} SCOPETABLE, *PSCOPETABLE;
|
|
|
|
|
2001-01-12 21:41:03 +01:00
|
|
|
typedef struct _MSVCRT_EXCEPTION_FRAME
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-08-28 05:07:56 +02:00
|
|
|
EXCEPTION_REGISTRATION_RECORD *prev;
|
2004-08-22 04:07:13 +02:00
|
|
|
void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
|
2001-01-11 00:59:25 +01:00
|
|
|
PCONTEXT, PEXCEPTION_RECORD);
|
|
|
|
PSCOPETABLE scopetable;
|
2002-10-10 19:56:48 +02:00
|
|
|
int trylevel;
|
2001-01-11 00:59:25 +01:00
|
|
|
int _ebp;
|
|
|
|
PEXCEPTION_POINTERS xpointers;
|
2001-01-12 21:41:03 +01:00
|
|
|
} MSVCRT_EXCEPTION_FRAME;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-10-10 19:56:48 +02:00
|
|
|
#define TRYLEVEL_END (-1) /* End of trylevel list */
|
2001-01-12 21:41:03 +01:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && defined(__i386__)
|
2007-03-19 20:48:07 +01:00
|
|
|
static inline void call_finally_block( void *code_block, void *base_ptr )
|
2001-09-20 21:33:37 +02:00
|
|
|
{
|
2006-12-27 00:19:50 +01:00
|
|
|
__asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
|
2001-09-20 21:33:37 +02:00
|
|
|
: : "a" (code_block), "g" (base_ptr));
|
|
|
|
}
|
2002-10-02 20:34:58 +02:00
|
|
|
|
2007-06-08 12:56:19 +02:00
|
|
|
static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
|
2002-10-02 20:34:58 +02:00
|
|
|
{
|
2007-06-08 12:56:19 +02:00
|
|
|
int ret;
|
2002-10-02 20:34:58 +02:00
|
|
|
__asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
|
2002-11-01 02:50:51 +01:00
|
|
|
: "=a" (ret)
|
2004-12-02 19:19:25 +01:00
|
|
|
: "0" (func), "r" (ebp), "r" (arg)
|
2002-11-01 02:50:51 +01:00
|
|
|
: "ecx", "edx", "memory" );
|
2002-10-02 20:34:58 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2007-06-08 12:56:19 +02:00
|
|
|
|
|
|
|
static inline int call_unwind_func( int (*func)(void), void *ebp )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
__asm__ __volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%0; popl %%ebp"
|
|
|
|
: "=a" (ret)
|
|
|
|
: "0" (func), "r" (ebp)
|
|
|
|
: "ecx", "edx", "memory" );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-05-17 01:08:01 +02:00
|
|
|
#endif
|
2001-01-12 21:41:03 +01:00
|
|
|
|
2001-04-10 23:16:07 +02:00
|
|
|
static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
|
2003-08-28 05:07:56 +02:00
|
|
|
EXCEPTION_REGISTRATION_RECORD* frame,
|
2004-09-01 06:58:21 +02:00
|
|
|
PCONTEXT context,
|
2003-08-28 05:07:56 +02:00
|
|
|
EXCEPTION_REGISTRATION_RECORD** dispatch)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2007-03-13 15:53:09 +01:00
|
|
|
if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
|
2001-01-12 21:41:03 +01:00
|
|
|
return ExceptionContinueSearch;
|
|
|
|
*dispatch = frame;
|
|
|
|
return ExceptionCollidedUnwind;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-01-11 23:30:19 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _EH_prolog (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
#ifdef __i386__
|
2003-05-13 02:41:57 +02:00
|
|
|
/* Provided for VC++ binary compatibility only */
|
2001-04-10 23:16:07 +02:00
|
|
|
__ASM_GLOBAL_FUNC(_EH_prolog,
|
2002-10-10 19:56:48 +02:00
|
|
|
"pushl $-1\n\t"
|
2001-01-11 23:30:19 +01:00
|
|
|
"pushl %eax\n\t"
|
|
|
|
"pushl %fs:0\n\t"
|
|
|
|
"movl %esp, %fs:0\n\t"
|
|
|
|
"movl 12(%esp), %eax\n\t"
|
|
|
|
"movl %ebp, 12(%esp)\n\t"
|
|
|
|
"leal 12(%esp), %ebp\n\t"
|
|
|
|
"pushl %eax\n\t"
|
2007-01-13 00:38:07 +01:00
|
|
|
"ret")
|
2001-01-11 23:30:19 +01:00
|
|
|
|
2007-06-08 12:56:19 +02:00
|
|
|
static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-08-28 05:07:56 +02:00
|
|
|
EXCEPTION_REGISTRATION_RECORD reg;
|
2001-01-12 21:41:03 +01:00
|
|
|
|
2002-10-10 19:56:48 +02:00
|
|
|
TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
|
2001-01-12 21:41:03 +01:00
|
|
|
|
|
|
|
/* Register a handler in case of a nested exception */
|
2007-03-13 15:53:09 +01:00
|
|
|
reg.Handler = MSVCRT_nested_handler;
|
2003-08-28 05:44:41 +02:00
|
|
|
reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
|
2001-01-12 21:41:03 +01:00
|
|
|
__wine_push_frame(®);
|
|
|
|
|
|
|
|
while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
|
|
|
|
{
|
2007-03-13 15:53:09 +01:00
|
|
|
int level = frame->trylevel;
|
|
|
|
frame->trylevel = frame->scopetable[level].previousTryLevel;
|
|
|
|
if (!frame->scopetable[level].lpfnFilter)
|
|
|
|
{
|
2007-06-08 12:56:19 +02:00
|
|
|
TRACE( "__try block cleanup level %d handler %p ebp %p\n",
|
|
|
|
level, frame->scopetable[level].lpfnHandler, ebp );
|
|
|
|
call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
|
2007-03-13 15:53:09 +01:00
|
|
|
}
|
2001-01-12 21:41:03 +01:00
|
|
|
}
|
|
|
|
__wine_pop_frame(®);
|
|
|
|
TRACE("unwound OK\n");
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2007-06-08 12:56:19 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* _local_unwind2 (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
|
|
|
|
{
|
|
|
|
msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __i386__ */
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* _global_unwind2 (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
|
|
|
|
{
|
|
|
|
TRACE("(%p)\n",frame);
|
|
|
|
RtlUnwind( frame, 0, 0, 0 );
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _except_handler2 (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _except_handler2(PEXCEPTION_RECORD rec,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD* frame,
|
|
|
|
PCONTEXT context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD** dispatcher)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2006-10-03 20:39:05 +02:00
|
|
|
FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
|
2001-01-11 00:59:25 +01:00
|
|
|
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
|
|
|
|
frame->Handler, context, dispatcher);
|
|
|
|
return ExceptionContinueSearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _except_handler3 (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _except_handler3(PEXCEPTION_RECORD rec,
|
|
|
|
MSVCRT_EXCEPTION_FRAME* frame,
|
|
|
|
PCONTEXT context, void* dispatcher)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-01-12 21:41:03 +01:00
|
|
|
#if defined(__GNUC__) && defined(__i386__)
|
2007-06-08 12:56:19 +02:00
|
|
|
int retval, trylevel;
|
2001-01-12 21:41:03 +01:00
|
|
|
EXCEPTION_POINTERS exceptPtrs;
|
|
|
|
PSCOPETABLE pScopeTable;
|
|
|
|
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
|
2001-01-11 00:59:25 +01:00
|
|
|
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
|
|
|
|
frame->handler, context, dispatcher);
|
2001-01-12 21:41:03 +01:00
|
|
|
|
|
|
|
__asm__ __volatile__ ("cld");
|
|
|
|
|
|
|
|
if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
|
|
|
|
{
|
|
|
|
/* Unwinding the current frame */
|
2007-06-08 12:56:19 +02:00
|
|
|
msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
|
2005-05-19 16:21:21 +02:00
|
|
|
TRACE("unwound current frame, returning ExceptionContinueSearch\n");
|
2001-01-12 21:41:03 +01:00
|
|
|
return ExceptionContinueSearch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Hunting for handler */
|
|
|
|
exceptPtrs.ExceptionRecord = rec;
|
|
|
|
exceptPtrs.ContextRecord = context;
|
|
|
|
*((DWORD *)frame-1) = (DWORD)&exceptPtrs;
|
|
|
|
trylevel = frame->trylevel;
|
|
|
|
pScopeTable = frame->scopetable;
|
|
|
|
|
|
|
|
while (trylevel != TRYLEVEL_END)
|
|
|
|
{
|
|
|
|
if (pScopeTable[trylevel].lpfnFilter)
|
|
|
|
{
|
|
|
|
TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
|
|
|
|
|
2002-10-02 20:34:58 +02:00
|
|
|
retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
|
2001-01-12 21:41:03 +01:00
|
|
|
|
|
|
|
TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
|
|
|
|
"CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
|
|
|
|
"EXECUTE_HANDLER" : "CONTINUE_SEARCH");
|
|
|
|
|
|
|
|
if (retval == EXCEPTION_CONTINUE_EXECUTION)
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
|
|
|
|
if (retval == EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Unwind all higher frames, this one will handle the exception */
|
2004-08-14 01:22:30 +02:00
|
|
|
_global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
|
2007-06-08 12:56:19 +02:00
|
|
|
msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
|
2001-01-12 21:41:03 +01:00
|
|
|
|
|
|
|
/* Set our trylevel to the enclosing block, and call the __finally
|
|
|
|
* code, which won't return
|
|
|
|
*/
|
|
|
|
frame->trylevel = pScopeTable->previousTryLevel;
|
|
|
|
TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
|
2001-09-20 21:33:37 +02:00
|
|
|
call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
|
2001-01-12 21:41:03 +01:00
|
|
|
ERR("Returned from __finally block - expect crash!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trylevel = pScopeTable->previousTryLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2007-03-18 15:10:07 +01:00
|
|
|
FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
|
2001-01-12 21:41:03 +01:00
|
|
|
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
|
|
|
|
frame->handler, context, dispatcher);
|
|
|
|
#endif
|
2005-05-19 16:21:21 +02:00
|
|
|
TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
|
2001-01-11 00:59:25 +01:00
|
|
|
return ExceptionContinueSearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _abnormal_termination (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _abnormal_termination(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
FIXME("(void)stub\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-21 20:23:30 +01:00
|
|
|
/*
|
|
|
|
* setjmp/longjmp implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
#define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
|
2006-12-15 13:41:48 +01:00
|
|
|
typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
|
2001-12-21 20:23:30 +01:00
|
|
|
|
2006-01-05 14:36:37 +01:00
|
|
|
/* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
|
|
|
|
/* and then jumps to the C backend function */
|
|
|
|
#define DEFINE_SETJMP_ENTRYPOINT(name) \
|
|
|
|
__ASM_GLOBAL_FUNC( name, \
|
|
|
|
"movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
|
|
|
|
"movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
|
|
|
|
"movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
|
|
|
|
"movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
|
|
|
|
"movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
|
|
|
|
"movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
|
|
|
|
"movl 0(%esp),%eax\n\t" \
|
|
|
|
"movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
|
|
|
|
"jmp " __ASM_NAME("__regs_") # name )
|
|
|
|
|
|
|
|
/* restore the registers from the jmp buf upon longjmp */
|
|
|
|
extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
|
|
|
|
__ASM_GLOBAL_FUNC( longjmp_set_regs,
|
|
|
|
"movl 4(%esp),%ecx\n\t" /* jmp_buf */
|
|
|
|
"movl 8(%esp),%eax\n\t" /* retval */
|
|
|
|
"movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
|
|
|
|
"movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
|
|
|
|
"movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
|
|
|
|
"movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
|
|
|
|
"movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
|
|
|
|
"addl $4,%esp\n\t" /* get rid of return address */
|
2007-01-13 00:38:07 +01:00
|
|
|
"jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
|
2006-01-05 14:36:37 +01:00
|
|
|
|
2001-12-21 20:23:30 +01:00
|
|
|
/*
|
2002-06-01 01:06:46 +02:00
|
|
|
* The signatures of the setjmp/longjmp functions do not match that
|
|
|
|
* declared in the setjmp header so they don't follow the regular naming
|
2001-12-21 20:23:30 +01:00
|
|
|
* convention to avoid conflicts.
|
|
|
|
*/
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* _setjmp (MSVCRT.@)
|
|
|
|
*/
|
2007-01-13 00:38:07 +01:00
|
|
|
DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-08-28 05:44:41 +02:00
|
|
|
jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
|
2007-06-08 12:56:19 +02:00
|
|
|
if (jmp->Registration == ~0UL)
|
2001-12-21 20:23:30 +01:00
|
|
|
jmp->TryLevel = TRYLEVEL_END;
|
|
|
|
else
|
|
|
|
jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
|
2006-01-05 14:36:37 +01:00
|
|
|
|
|
|
|
TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
|
|
|
|
jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
|
|
|
|
return 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-03-19 20:13:40 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* _setjmp3 (MSVCRT.@)
|
|
|
|
*/
|
2007-01-13 00:38:07 +01:00
|
|
|
DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
|
2006-08-18 20:56:43 +02:00
|
|
|
int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
|
2001-03-19 20:13:40 +01:00
|
|
|
{
|
2001-12-21 20:23:30 +01:00
|
|
|
jmp->Cookie = MSVCRT_JMP_MAGIC;
|
|
|
|
jmp->UnwindFunc = 0;
|
2003-08-28 05:44:41 +02:00
|
|
|
jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
|
2007-06-08 12:56:19 +02:00
|
|
|
if (jmp->Registration == ~0UL)
|
2001-12-21 20:23:30 +01:00
|
|
|
{
|
|
|
|
jmp->TryLevel = TRYLEVEL_END;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-18 20:56:43 +02:00
|
|
|
int i;
|
|
|
|
va_list args;
|
2001-12-21 20:23:30 +01:00
|
|
|
|
2006-08-18 20:56:43 +02:00
|
|
|
va_start( args, nb_args );
|
|
|
|
if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
|
|
|
|
if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
|
2001-12-21 20:23:30 +01:00
|
|
|
else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
|
2006-08-18 20:56:43 +02:00
|
|
|
for (i = 0; i < 6 && i < nb_args - 2; i++)
|
|
|
|
jmp->UnwindData[i] = va_arg( args, unsigned long );
|
|
|
|
va_end( args );
|
2001-12-21 20:23:30 +01:00
|
|
|
}
|
2006-01-05 14:36:37 +01:00
|
|
|
|
|
|
|
TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
|
|
|
|
jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
|
|
|
|
return 0;
|
2001-03-19 20:13:40 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* longjmp (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-12-21 20:23:30 +01:00
|
|
|
unsigned long cur_frame = 0;
|
|
|
|
|
2006-01-05 14:36:37 +01:00
|
|
|
TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
|
|
|
|
jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
|
2001-12-21 20:23:30 +01:00
|
|
|
|
2003-08-28 05:44:41 +02:00
|
|
|
cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
|
2001-12-21 20:23:30 +01:00
|
|
|
TRACE("cur_frame=%lx\n",cur_frame);
|
|
|
|
|
|
|
|
if (cur_frame != jmp->Registration)
|
2004-08-22 04:07:13 +02:00
|
|
|
_global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
|
2001-12-21 20:23:30 +01:00
|
|
|
|
|
|
|
if (jmp->Registration)
|
|
|
|
{
|
|
|
|
if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
|
|
|
|
jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
|
|
|
|
{
|
|
|
|
MSVCRT_unwind_function unwind_func;
|
|
|
|
|
|
|
|
unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
|
|
|
|
unwind_func(jmp);
|
|
|
|
}
|
|
|
|
else
|
2007-06-08 12:56:19 +02:00
|
|
|
msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
|
|
|
|
jmp->TryLevel, (void *)jmp->Ebp);
|
2001-12-21 20:23:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!retval)
|
|
|
|
retval = 1;
|
|
|
|
|
2006-01-05 14:36:37 +01:00
|
|
|
longjmp_set_regs( jmp, retval );
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-10-16 20:59:38 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _seh_longjmp_unwind (MSVCRT.@)
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
|
2002-10-16 20:59:38 +02:00
|
|
|
{
|
2007-06-08 12:56:19 +02:00
|
|
|
msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
|
2002-10-16 20:59:38 +02:00
|
|
|
}
|
2002-10-31 23:04:53 +01:00
|
|
|
#endif /* i386 */
|
2002-10-16 20:59:38 +02:00
|
|
|
|
2005-12-01 12:48:44 +01:00
|
|
|
static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
|
2005-05-13 19:44:28 +02:00
|
|
|
|
|
|
|
static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
switch (ctrlType)
|
|
|
|
{
|
|
|
|
case CTRL_C_EVENT:
|
2005-12-01 12:48:44 +01:00
|
|
|
if (sighandlers[MSVCRT_SIGINT])
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
2005-12-01 12:48:44 +01:00
|
|
|
if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
|
|
|
|
sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
|
2005-05-13 19:44:28 +02:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*float_handler)(int, int);
|
|
|
|
|
|
|
|
/* The exception codes are actually NTSTATUS values */
|
2007-01-13 21:31:01 +01:00
|
|
|
static const struct
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
int signal;
|
|
|
|
} float_exception_map[] = {
|
|
|
|
{ EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
|
|
|
|
{ EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
|
|
|
|
{ EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
|
|
|
|
{ EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
|
|
|
|
{ EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
|
|
|
|
{ EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
|
|
|
|
{ EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
|
|
|
|
};
|
|
|
|
|
|
|
|
static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
|
|
|
|
{
|
|
|
|
LONG ret = EXCEPTION_CONTINUE_SEARCH;
|
2007-02-08 21:33:33 +01:00
|
|
|
MSVCRT___sighandler_t handler;
|
2005-05-13 19:44:28 +02:00
|
|
|
|
2005-05-19 16:21:21 +02:00
|
|
|
if (!except || !except->ExceptionRecord)
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
2005-05-13 19:44:28 +02:00
|
|
|
switch (except->ExceptionRecord->ExceptionCode)
|
|
|
|
{
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
2007-02-08 21:33:33 +01:00
|
|
|
if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
2007-02-08 21:33:33 +01:00
|
|
|
if (handler != MSVCRT_SIG_IGN)
|
|
|
|
{
|
|
|
|
sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
|
|
|
|
handler(MSVCRT_SIGSEGV);
|
|
|
|
}
|
2005-05-13 19:44:28 +02:00
|
|
|
ret = EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* According to
|
|
|
|
* http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
|
|
|
|
* the FPE signal handler takes as a second argument the type of
|
|
|
|
* floating point exception.
|
|
|
|
*/
|
|
|
|
case EXCEPTION_FLT_DENORMAL_OPERAND:
|
|
|
|
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
|
|
|
case EXCEPTION_FLT_INEXACT_RESULT:
|
|
|
|
case EXCEPTION_FLT_INVALID_OPERATION:
|
|
|
|
case EXCEPTION_FLT_OVERFLOW:
|
|
|
|
case EXCEPTION_FLT_STACK_CHECK:
|
|
|
|
case EXCEPTION_FLT_UNDERFLOW:
|
2007-02-08 21:33:33 +01:00
|
|
|
if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
2007-02-08 21:33:33 +01:00
|
|
|
if (handler != MSVCRT_SIG_IGN)
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
|
|
|
int i, float_signal = _FPE_INVALID;
|
|
|
|
|
2007-02-08 21:33:33 +01:00
|
|
|
sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
|
2005-05-13 19:44:28 +02:00
|
|
|
for (i = 0; i < sizeof(float_exception_map) /
|
2007-02-08 21:33:33 +01:00
|
|
|
sizeof(float_exception_map[0]); i++)
|
|
|
|
{
|
2005-05-13 19:44:28 +02:00
|
|
|
if (float_exception_map[i].status ==
|
2007-02-08 21:33:33 +01:00
|
|
|
except->ExceptionRecord->ExceptionCode)
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
|
|
|
float_signal = float_exception_map[i].signal;
|
|
|
|
break;
|
|
|
|
}
|
2007-02-08 21:33:33 +01:00
|
|
|
}
|
|
|
|
((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
|
2005-05-13 19:44:28 +02:00
|
|
|
}
|
|
|
|
ret = EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
2007-02-08 21:33:33 +01:00
|
|
|
if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
|
2005-05-13 19:44:28 +02:00
|
|
|
{
|
2007-02-08 21:33:33 +01:00
|
|
|
if (handler != MSVCRT_SIG_IGN)
|
|
|
|
{
|
|
|
|
sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
|
|
|
|
handler(MSVCRT_SIGILL);
|
|
|
|
}
|
2005-05-13 19:44:28 +02:00
|
|
|
ret = EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void msvcrt_init_signals(void)
|
|
|
|
{
|
|
|
|
SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
|
|
|
|
SetUnhandledExceptionFilter(msvcrt_exception_filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void msvcrt_free_signals(void)
|
|
|
|
{
|
|
|
|
SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
|
|
|
|
SetUnhandledExceptionFilter(NULL);
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* signal (MSVCRT.@)
|
2005-05-13 19:44:28 +02:00
|
|
|
* MS signal handling is described here:
|
|
|
|
* http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
|
|
|
|
* Some signals may never be generated except through an explicit call to
|
|
|
|
* raise.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2005-12-01 12:48:44 +01:00
|
|
|
MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
|
2005-05-13 19:44:28 +02:00
|
|
|
|
|
|
|
TRACE("(%d, %p)\n", sig, func);
|
|
|
|
|
2005-12-01 12:48:44 +01:00
|
|
|
if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
|
2005-05-13 19:44:28 +02:00
|
|
|
|
|
|
|
switch (sig)
|
|
|
|
{
|
|
|
|
/* Cases handled internally. Note SIGTERM is never generated by Windows,
|
|
|
|
* so we effectively mask it.
|
|
|
|
*/
|
2005-12-01 12:48:44 +01:00
|
|
|
case MSVCRT_SIGABRT:
|
|
|
|
case MSVCRT_SIGFPE:
|
|
|
|
case MSVCRT_SIGILL:
|
|
|
|
case MSVCRT_SIGSEGV:
|
|
|
|
case MSVCRT_SIGINT:
|
|
|
|
case MSVCRT_SIGTERM:
|
2005-05-13 19:44:28 +02:00
|
|
|
ret = sighandlers[sig];
|
|
|
|
sighandlers[sig] = func;
|
|
|
|
break;
|
|
|
|
default:
|
2005-12-01 12:48:44 +01:00
|
|
|
ret = MSVCRT_SIG_ERR;
|
2005-05-13 19:44:28 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2005-05-19 16:21:21 +02:00
|
|
|
|
2007-02-08 21:33:39 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* raise (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL MSVCRT_raise(int sig)
|
|
|
|
{
|
|
|
|
MSVCRT___sighandler_t handler;
|
|
|
|
|
|
|
|
TRACE("(%d)\n", sig);
|
|
|
|
|
|
|
|
switch (sig)
|
|
|
|
{
|
|
|
|
case MSVCRT_SIGABRT:
|
|
|
|
case MSVCRT_SIGFPE:
|
|
|
|
case MSVCRT_SIGILL:
|
|
|
|
case MSVCRT_SIGSEGV:
|
|
|
|
case MSVCRT_SIGINT:
|
|
|
|
case MSVCRT_SIGTERM:
|
|
|
|
handler = sighandlers[sig];
|
|
|
|
if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
|
|
|
|
if (handler != MSVCRT_SIG_IGN)
|
|
|
|
{
|
|
|
|
sighandlers[sig] = MSVCRT_SIG_DFL;
|
|
|
|
if (sig == MSVCRT_SIGFPE)
|
|
|
|
((float_handler)handler)(sig, _FPE_EXPLICITGEN);
|
|
|
|
else
|
|
|
|
handler(sig);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-19 16:21:21 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _XcptFilter (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
|
2005-05-19 16:21:21 +02:00
|
|
|
{
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("(%08x,%p)\n", ex, ptr);
|
2005-05-19 16:21:21 +02:00
|
|
|
/* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
|
|
|
|
return msvcrt_exception_filter(ptr);
|
|
|
|
}
|