1999-05-12 15:10:39 +02:00
|
|
|
/*
|
|
|
|
* Wine exception handling
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999 Alexandre Julliard
|
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
|
1999-05-12 15:10:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __WINE_WINE_EXCEPTION_H
|
|
|
|
#define __WINE_WINE_EXCEPTION_H
|
|
|
|
|
2003-08-28 23:43:34 +02:00
|
|
|
#include <windef.h>
|
2021-10-07 12:06:54 +02:00
|
|
|
#include <winternl.h>
|
2008-02-21 17:13:37 +01:00
|
|
|
#include <excpt.h>
|
1999-05-12 15:10:39 +02:00
|
|
|
|
2008-11-06 10:43:31 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
1999-05-12 15:10:39 +02:00
|
|
|
/* The following definitions allow using exceptions in Wine and Winelib code
|
|
|
|
*
|
|
|
|
* They should be used like this:
|
|
|
|
*
|
1999-05-14 20:21:55 +02:00
|
|
|
* __TRY
|
1999-05-12 15:10:39 +02:00
|
|
|
* {
|
|
|
|
* do some stuff that can raise an exception
|
|
|
|
* }
|
2009-01-26 15:07:50 +01:00
|
|
|
* __EXCEPT(filter_func)
|
1999-05-12 15:10:39 +02:00
|
|
|
* {
|
|
|
|
* handle the exception here
|
|
|
|
* }
|
1999-05-14 20:21:55 +02:00
|
|
|
* __ENDTRY
|
1999-05-12 15:10:39 +02:00
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
1999-05-14 20:21:55 +02:00
|
|
|
* __TRY
|
1999-05-12 15:10:39 +02:00
|
|
|
* {
|
|
|
|
* do some stuff that can raise an exception
|
|
|
|
* }
|
2009-01-26 15:07:50 +01:00
|
|
|
* __FINALLY(finally_func)
|
1999-05-12 15:10:39 +02:00
|
|
|
*
|
2009-01-26 15:07:50 +01:00
|
|
|
* The filter_func and finally_func functions must be defined like this:
|
|
|
|
*
|
|
|
|
* LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
|
1999-05-12 15:10:39 +02:00
|
|
|
*
|
2009-01-26 15:07:50 +01:00
|
|
|
* void CALLBACK finally_func( BOOL __normal ) { ... }
|
|
|
|
*
|
|
|
|
* The filter function must return one of the EXCEPTION_* code; it can
|
|
|
|
* use GetExceptionInformation() and GetExceptionCode() to retrieve the
|
|
|
|
* exception info.
|
1999-05-12 15:10:39 +02:00
|
|
|
*
|
2017-12-14 07:23:28 +01:00
|
|
|
* Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
|
|
|
|
* break out of the current block, but avoid using them because they
|
|
|
|
* won't work when compiling with native exceptions. You cannot use
|
|
|
|
* 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
|
|
|
|
* this will surely crash. You can use 'return', 'goto', or 'longjmp'
|
|
|
|
* to leave an __EXCEPT block though.
|
1999-05-12 15:10:39 +02:00
|
|
|
*
|
|
|
|
* -- AJ
|
|
|
|
*/
|
|
|
|
|
2020-04-27 01:18:50 +02:00
|
|
|
#if !defined(__GNUC__) && !defined(__clang__)
|
2011-10-28 17:04:33 +02:00
|
|
|
#define __attribute__(x) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
1999-05-14 20:21:55 +02:00
|
|
|
/* Define this if you want to use your compiler built-in __try/__except support.
|
|
|
|
* This is only useful when compiling to a native Windows binary, as the built-in
|
|
|
|
* compiler exceptions will most certainly not work under Winelib.
|
|
|
|
*/
|
|
|
|
#ifdef USE_COMPILER_EXCEPTIONS
|
|
|
|
|
|
|
|
#define __TRY __try
|
1999-06-18 20:26:35 +02:00
|
|
|
#define __EXCEPT(func) __except((func)(GetExceptionInformation()))
|
2017-05-31 12:39:29 +02:00
|
|
|
#define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
|
1999-06-18 20:26:35 +02:00
|
|
|
#define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
|
2017-05-31 12:39:29 +02:00
|
|
|
#define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
|
1999-05-14 20:21:55 +02:00
|
|
|
#define __ENDTRY /*nothing*/
|
2005-12-16 16:58:47 +01:00
|
|
|
#define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
|
2008-02-15 16:43:24 +01:00
|
|
|
#define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
|
1999-05-14 20:21:55 +02:00
|
|
|
|
|
|
|
#else /* USE_COMPILER_EXCEPTIONS */
|
|
|
|
|
2019-09-25 16:26:59 +02:00
|
|
|
#ifdef __i386__
|
2019-06-18 10:17:43 +02:00
|
|
|
typedef struct { int reg[16]; } __wine_jmp_buf;
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part[2]; } reg[16]; } __wine_jmp_buf;
|
|
|
|
#elif defined(__arm__)
|
|
|
|
typedef struct { int reg[28]; } __wine_jmp_buf;
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
typedef struct { __int64 reg[24]; } __wine_jmp_buf;
|
|
|
|
#else
|
|
|
|
typedef struct { int reg; } __wine_jmp_buf;
|
2008-05-26 19:23:43 +02:00
|
|
|
#endif
|
|
|
|
|
2019-06-18 10:17:43 +02:00
|
|
|
extern int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) __wine_setjmpex( __wine_jmp_buf *buf,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame ) DECLSPEC_HIDDEN;
|
2020-04-15 18:27:22 +02:00
|
|
|
extern void DECLSPEC_NORETURN __cdecl __wine_longjmp( __wine_jmp_buf *buf, int retval ) DECLSPEC_HIDDEN;
|
|
|
|
extern void DECLSPEC_NORETURN __cdecl __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD* frame, EXCEPTION_RECORD *record,
|
|
|
|
void (*target)(void) ) DECLSPEC_HIDDEN;
|
2019-05-30 12:13:21 +02:00
|
|
|
extern DWORD __cdecl __wine_exception_handler( EXCEPTION_RECORD *record,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
|
|
|
|
extern DWORD __cdecl __wine_exception_ctx_handler( EXCEPTION_RECORD *record,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
|
|
|
|
extern DWORD __cdecl __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
|
|
|
|
extern DWORD __cdecl __wine_exception_handler_all( EXCEPTION_RECORD *record,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
|
|
|
|
extern DWORD __cdecl __wine_finally_handler( EXCEPTION_RECORD *record,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
|
|
|
|
extern DWORD __cdecl __wine_finally_ctx_handler( EXCEPTION_RECORD *record,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context,
|
|
|
|
EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
|
2010-04-08 14:41:01 +02:00
|
|
|
|
1999-05-14 20:21:55 +02:00
|
|
|
#define __TRY \
|
|
|
|
do { __WINE_FRAME __f; \
|
|
|
|
int __first = 1; \
|
|
|
|
for (;;) if (!__first) \
|
|
|
|
{ \
|
|
|
|
do {
|
1999-05-12 15:10:39 +02:00
|
|
|
|
1999-06-18 20:26:35 +02:00
|
|
|
#define __EXCEPT(func) \
|
1999-05-14 20:21:55 +02:00
|
|
|
} while(0); \
|
2000-08-06 04:42:46 +02:00
|
|
|
__wine_pop_frame( &__f.frame ); \
|
1999-05-14 20:21:55 +02:00
|
|
|
break; \
|
|
|
|
} else { \
|
2003-08-28 05:07:56 +02:00
|
|
|
__f.frame.Handler = __wine_exception_handler; \
|
1999-06-18 20:26:35 +02:00
|
|
|
__f.u.filter = (func); \
|
2019-06-18 10:17:43 +02:00
|
|
|
if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
|
2004-09-01 19:36:04 +02:00
|
|
|
const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
|
1999-05-14 20:21:55 +02:00
|
|
|
do {
|
|
|
|
|
2017-05-31 12:39:29 +02:00
|
|
|
#define __EXCEPT_CTX(func, context) \
|
|
|
|
} while(0); \
|
|
|
|
__wine_pop_frame( &__f.frame ); \
|
|
|
|
break; \
|
|
|
|
} else { \
|
|
|
|
__f.frame.Handler = __wine_exception_ctx_handler; \
|
|
|
|
__f.u.filter_ctx = (func); \
|
|
|
|
__f.ctx = context; \
|
2019-06-18 10:17:43 +02:00
|
|
|
if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
|
2017-05-31 12:39:29 +02:00
|
|
|
const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
|
|
|
|
do {
|
|
|
|
|
2020-08-24 13:30:19 +02:00
|
|
|
#define __EXCEPT_HANDLER(handler) \
|
2008-05-01 10:57:54 +02:00
|
|
|
} while(0); \
|
|
|
|
__wine_pop_frame( &__f.frame ); \
|
|
|
|
break; \
|
|
|
|
} else { \
|
2020-08-24 13:30:19 +02:00
|
|
|
__f.frame.Handler = (handler); \
|
2019-06-18 10:17:43 +02:00
|
|
|
if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
|
2008-05-01 10:57:54 +02:00
|
|
|
const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
|
|
|
|
do {
|
|
|
|
|
2020-08-24 13:30:19 +02:00
|
|
|
#define __EXCEPT_PAGE_FAULT __EXCEPT_HANDLER(__wine_exception_handler_page_fault)
|
|
|
|
#define __EXCEPT_ALL __EXCEPT_HANDLER(__wine_exception_handler_all)
|
2008-05-01 10:57:54 +02:00
|
|
|
|
1999-05-14 20:21:55 +02:00
|
|
|
#define __ENDTRY \
|
|
|
|
} while (0); \
|
|
|
|
break; \
|
|
|
|
} \
|
2008-05-01 11:07:08 +02:00
|
|
|
__wine_push_frame( &__f.frame ); \
|
1999-05-14 20:21:55 +02:00
|
|
|
__first = 0; \
|
|
|
|
} \
|
1999-05-12 15:10:39 +02:00
|
|
|
} while (0);
|
|
|
|
|
1999-06-18 20:26:35 +02:00
|
|
|
#define __FINALLY(func) \
|
1999-05-14 20:21:55 +02:00
|
|
|
} while(0); \
|
2000-08-06 04:42:46 +02:00
|
|
|
__wine_pop_frame( &__f.frame ); \
|
1999-06-18 20:26:35 +02:00
|
|
|
(func)(1); \
|
1999-05-14 20:21:55 +02:00
|
|
|
break; \
|
|
|
|
} else { \
|
2003-08-28 05:07:56 +02:00
|
|
|
__f.frame.Handler = __wine_finally_handler; \
|
1999-06-18 20:26:35 +02:00
|
|
|
__f.u.finally_func = (func); \
|
2000-08-06 04:42:46 +02:00
|
|
|
__wine_push_frame( &__f.frame ); \
|
1999-05-14 20:21:55 +02:00
|
|
|
__first = 0; \
|
|
|
|
} \
|
|
|
|
} while (0);
|
1999-05-12 15:10:39 +02:00
|
|
|
|
2017-05-31 12:39:29 +02:00
|
|
|
#define __FINALLY_CTX(func, context) \
|
|
|
|
} while(0); \
|
|
|
|
__wine_pop_frame( &__f.frame ); \
|
|
|
|
(func)(1, context); \
|
|
|
|
break; \
|
|
|
|
} else { \
|
|
|
|
__f.frame.Handler = __wine_finally_ctx_handler; \
|
|
|
|
__f.u.finally_func_ctx = (func); \
|
|
|
|
__f.ctx = context; \
|
|
|
|
__wine_push_frame( &__f.frame ); \
|
|
|
|
__first = 0; \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
1999-05-12 15:10:39 +02:00
|
|
|
|
2006-09-20 11:26:59 +02:00
|
|
|
typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
|
2017-05-31 12:39:29 +02:00
|
|
|
typedef LONG (CALLBACK *__WINE_FILTER_CTX)(PEXCEPTION_POINTERS, void*);
|
2002-02-01 00:22:07 +01:00
|
|
|
typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
|
2017-05-31 12:39:29 +02:00
|
|
|
typedef void (CALLBACK *__WINE_FINALLY_CTX)(BOOL, void*);
|
1999-05-12 15:10:39 +02:00
|
|
|
|
1999-05-14 20:21:55 +02:00
|
|
|
#define GetExceptionInformation() (__eptr)
|
1999-06-18 20:26:35 +02:00
|
|
|
#define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
|
1999-05-14 20:21:55 +02:00
|
|
|
#define AbnormalTermination() (!__normal)
|
1999-05-12 15:10:39 +02:00
|
|
|
|
1999-06-18 20:26:35 +02:00
|
|
|
typedef struct __tagWINE_FRAME
|
1999-05-12 15:10:39 +02:00
|
|
|
{
|
2003-08-28 05:07:56 +02:00
|
|
|
EXCEPTION_REGISTRATION_RECORD frame;
|
1999-05-12 15:10:39 +02:00
|
|
|
union
|
|
|
|
{
|
1999-06-18 20:26:35 +02:00
|
|
|
/* exception data */
|
|
|
|
__WINE_FILTER filter;
|
2017-05-31 12:39:29 +02:00
|
|
|
__WINE_FILTER_CTX filter_ctx;
|
1999-06-18 20:26:35 +02:00
|
|
|
/* finally data */
|
|
|
|
__WINE_FINALLY finally_func;
|
2017-05-31 12:39:29 +02:00
|
|
|
__WINE_FINALLY_CTX finally_func_ctx;
|
1999-05-12 15:10:39 +02:00
|
|
|
} u;
|
2017-05-31 12:39:29 +02:00
|
|
|
void *ctx;
|
2019-06-18 10:17:43 +02:00
|
|
|
__wine_jmp_buf jmp;
|
1999-06-18 20:26:35 +02:00
|
|
|
/* hack to make GetExceptionCode() work in handler */
|
|
|
|
DWORD ExceptionCode;
|
|
|
|
const struct __tagWINE_FRAME *ExceptionRecord;
|
1999-05-12 15:10:39 +02:00
|
|
|
} __WINE_FRAME;
|
|
|
|
|
1999-05-14 20:21:55 +02:00
|
|
|
#endif /* USE_COMPILER_EXCEPTIONS */
|
|
|
|
|
2003-08-28 05:07:56 +02:00
|
|
|
static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
|
1999-05-12 15:10:39 +02:00
|
|
|
{
|
1999-05-14 20:21:55 +02:00
|
|
|
#if defined(__GNUC__) && defined(__i386__)
|
2003-08-28 05:07:56 +02:00
|
|
|
EXCEPTION_REGISTRATION_RECORD *prev;
|
1999-05-14 20:21:55 +02:00
|
|
|
__asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
|
|
|
|
"\n\tmovl %0,(%1)"
|
|
|
|
"\n\t.byte 0x64\n\tmovl %1,(0)"
|
|
|
|
: "=&r" (prev) : "r" (frame) : "memory" );
|
|
|
|
return prev;
|
|
|
|
#else
|
2000-09-27 00:20:14 +02:00
|
|
|
NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
|
2008-07-03 12:57:43 +02:00
|
|
|
frame->Prev = teb->ExceptionList;
|
|
|
|
teb->ExceptionList = frame;
|
1999-05-14 20:21:55 +02:00
|
|
|
return frame->Prev;
|
|
|
|
#endif
|
1999-05-12 15:10:39 +02:00
|
|
|
}
|
|
|
|
|
2003-08-28 05:07:56 +02:00
|
|
|
static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
|
1999-05-12 15:10:39 +02:00
|
|
|
{
|
1999-05-14 20:21:55 +02:00
|
|
|
#if defined(__GNUC__) && defined(__i386__)
|
|
|
|
__asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
|
|
|
|
: : "r" (frame->Prev) : "memory" );
|
|
|
|
return frame->Prev;
|
|
|
|
|
|
|
|
#else
|
2000-09-27 00:20:14 +02:00
|
|
|
NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
|
2008-07-03 12:57:43 +02:00
|
|
|
teb->ExceptionList = frame->Prev;
|
1999-05-12 15:10:39 +02:00
|
|
|
return frame->Prev;
|
1999-05-14 20:21:55 +02:00
|
|
|
#endif
|
1999-05-12 15:10:39 +02:00
|
|
|
}
|
|
|
|
|
2009-06-18 13:32:26 +02:00
|
|
|
static inline EXCEPTION_REGISTRATION_RECORD *__wine_get_frame(void)
|
|
|
|
{
|
|
|
|
#if defined(__GNUC__) && defined(__i386__)
|
|
|
|
EXCEPTION_REGISTRATION_RECORD *ret;
|
|
|
|
__asm__ __volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret) );
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
|
|
|
|
return teb->ExceptionList;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-11-06 10:43:31 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-05-12 15:10:39 +02:00
|
|
|
#endif /* __WINE_WINE_EXCEPTION_H */
|