2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #pragma makedep install
25 #ifndef __WINE_WINE_EXCEPTION_H
26 #define __WINE_WINE_EXCEPTION_H
36 /* The following definitions allow using exceptions in Wine and Winelib code
38 * They should be used like this:
42 * do some stuff that can raise an exception
44 * __EXCEPT(filter_func)
46 * handle the exception here
54 * do some stuff that can raise an exception
56 * __FINALLY(finally_func)
58 * The filter_func and finally_func functions must be defined like this:
60 * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
62 * void CALLBACK finally_func( BOOL __normal ) { ... }
64 * The filter function must return one of the EXCEPTION_* code; it can
65 * use GetExceptionInformation() and GetExceptionCode() to retrieve the
68 * Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
69 * break out of the current block, but avoid using them because they
70 * won't work when compiling with native exceptions. You cannot use
71 * 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
72 * this will surely crash. You can use 'return', 'goto', or 'longjmp'
73 * to leave an __EXCEPT block though.
78 #if !defined(__GNUC__) && !defined(__clang__)
79 #define __attribute__(x) /* nothing */
82 /* Define this if you want to use your compiler built-in __try/__except support.
83 * This is only useful when compiling to a native Windows binary, as the built-in
84 * compiler exceptions will most certainly not work under Winelib.
86 #ifdef USE_COMPILER_EXCEPTIONS
89 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
90 #define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
91 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
92 #define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
93 #define __ENDTRY /*nothing*/
94 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
95 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
97 #else /* USE_COMPILER_EXCEPTIONS */
100 typedef struct { int reg
[16]; } __wine_jmp_buf
;
101 #elif defined(__x86_64__)
102 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part
[2]; } reg
[16]; } __wine_jmp_buf
;
103 #elif defined(__arm__)
104 typedef struct { int reg
[28]; } __wine_jmp_buf
;
105 #elif defined(__aarch64__)
106 typedef struct { __int64 reg
[24]; } __wine_jmp_buf
;
108 typedef struct { int reg
; } __wine_jmp_buf
;
111 extern int __cdecl
__attribute__ ((__nothrow__
,__returns_twice__
)) __wine_setjmpex( __wine_jmp_buf
*buf
,
112 EXCEPTION_REGISTRATION_RECORD
*frame
) DECLSPEC_HIDDEN
;
113 extern void DECLSPEC_NORETURN __cdecl
__wine_longjmp( __wine_jmp_buf
*buf
, int retval
) DECLSPEC_HIDDEN
;
114 extern void DECLSPEC_NORETURN __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
115 void (*target
)(void) ) DECLSPEC_HIDDEN
;
116 extern DWORD __cdecl
__wine_exception_handler( EXCEPTION_RECORD
*record
,
117 EXCEPTION_REGISTRATION_RECORD
*frame
,
119 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
120 extern DWORD __cdecl
__wine_exception_ctx_handler( EXCEPTION_RECORD
*record
,
121 EXCEPTION_REGISTRATION_RECORD
*frame
,
123 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
124 extern DWORD __cdecl
__wine_exception_handler_page_fault( EXCEPTION_RECORD
*record
,
125 EXCEPTION_REGISTRATION_RECORD
*frame
,
127 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
128 extern DWORD __cdecl
__wine_exception_handler_all( EXCEPTION_RECORD
*record
,
129 EXCEPTION_REGISTRATION_RECORD
*frame
,
131 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
132 extern DWORD __cdecl
__wine_finally_handler( EXCEPTION_RECORD
*record
,
133 EXCEPTION_REGISTRATION_RECORD
*frame
,
135 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
136 extern DWORD __cdecl
__wine_finally_ctx_handler( EXCEPTION_RECORD
*record
,
137 EXCEPTION_REGISTRATION_RECORD
*frame
,
139 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
142 do { __WINE_FRAME __f; \
144 for (;;) if (!__first) \
148 #define __EXCEPT(func) \
150 __wine_pop_frame( &__f.frame ); \
153 __f.frame.Handler = __wine_exception_handler; \
154 __f.u.filter = (func); \
155 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
156 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
159 #define __EXCEPT_CTX(func, context) \
161 __wine_pop_frame( &__f.frame ); \
164 __f.frame.Handler = __wine_exception_ctx_handler; \
165 __f.u.filter_ctx = (func); \
167 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
168 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
171 #define __EXCEPT_HANDLER(handler) \
173 __wine_pop_frame( &__f.frame ); \
176 __f.frame.Handler = (handler); \
177 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
178 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
181 #define __EXCEPT_PAGE_FAULT __EXCEPT_HANDLER(__wine_exception_handler_page_fault)
182 #define __EXCEPT_ALL __EXCEPT_HANDLER(__wine_exception_handler_all)
188 __wine_push_frame( &__f.frame ); \
193 #define __FINALLY(func) \
195 __wine_pop_frame( &__f.frame ); \
199 __f.frame.Handler = __wine_finally_handler; \
200 __f.u.finally_func = (func); \
201 __wine_push_frame( &__f.frame ); \
206 #define __FINALLY_CTX(func, context) \
208 __wine_pop_frame( &__f.frame ); \
209 (func)(1, context); \
212 __f.frame.Handler = __wine_finally_ctx_handler; \
213 __f.u.finally_func_ctx = (func); \
215 __wine_push_frame( &__f.frame ); \
221 typedef LONG (CALLBACK
*__WINE_FILTER
)(PEXCEPTION_POINTERS
);
222 typedef LONG (CALLBACK
*__WINE_FILTER_CTX
)(PEXCEPTION_POINTERS
, void*);
223 typedef void (CALLBACK
*__WINE_FINALLY
)(BOOL
);
224 typedef void (CALLBACK
*__WINE_FINALLY_CTX
)(BOOL
, void*);
226 #define GetExceptionInformation() (__eptr)
227 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
228 #define AbnormalTermination() (!__normal)
230 typedef struct __tagWINE_FRAME
232 EXCEPTION_REGISTRATION_RECORD frame
;
236 __WINE_FILTER filter
;
237 __WINE_FILTER_CTX filter_ctx
;
239 __WINE_FINALLY finally_func
;
240 __WINE_FINALLY_CTX finally_func_ctx
;
244 /* hack to make GetExceptionCode() work in handler */
246 const struct __tagWINE_FRAME
*ExceptionRecord
;
249 #endif /* USE_COMPILER_EXCEPTIONS */
251 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_push_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
253 #if defined(__GNUC__) && defined(__i386__)
254 EXCEPTION_REGISTRATION_RECORD
*prev
;
255 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
257 "\n\t.byte 0x64\n\tmovl %1,(0)"
258 : "=&r" (prev
) : "r" (frame
) : "memory" );
261 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
262 frame
->Prev
= teb
->ExceptionList
;
263 teb
->ExceptionList
= frame
;
268 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
270 #if defined(__GNUC__) && defined(__i386__)
271 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
272 : : "r" (frame
->Prev
) : "memory" );
276 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
277 teb
->ExceptionList
= frame
->Prev
;
282 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_get_frame(void)
284 #if defined(__GNUC__) && defined(__i386__)
285 EXCEPTION_REGISTRATION_RECORD
*ret
;
286 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret
) );
289 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
290 return teb
->ExceptionList
;
298 #endif /* __WINE_WINE_EXCEPTION_H */