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
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
31 /* The following definitions allow using exceptions in Wine and Winelib code
33 * They should be used like this:
37 * do some stuff that can raise an exception
39 * __EXCEPT(filter_func)
41 * handle the exception here
49 * do some stuff that can raise an exception
51 * __FINALLY(finally_func)
53 * The filter_func and finally_func functions must be defined like this:
55 * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
57 * void CALLBACK finally_func( BOOL __normal ) { ... }
59 * The filter function must return one of the EXCEPTION_* code; it can
60 * use GetExceptionInformation() and GetExceptionCode() to retrieve the
63 * Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
64 * break out of the current block, but avoid using them because they
65 * won't work when compiling with native exceptions. You cannot use
66 * 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
67 * this will surely crash. You can use 'return', 'goto', or 'longjmp'
68 * to leave an __EXCEPT block though.
74 #define __attribute__(x) /* nothing */
77 /* Define this if you want to use your compiler built-in __try/__except support.
78 * This is only useful when compiling to a native Windows binary, as the built-in
79 * compiler exceptions will most certainly not work under Winelib.
81 #ifdef USE_COMPILER_EXCEPTIONS
84 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
85 #define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
86 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
87 #define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
88 #define __ENDTRY /*nothing*/
89 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
90 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
92 #else /* USE_COMPILER_EXCEPTIONS */
95 typedef struct { int reg
[16]; } __wine_jmp_buf
;
96 #elif defined(__x86_64__)
97 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part
[2]; } reg
[16]; } __wine_jmp_buf
;
98 #elif defined(__arm__)
99 typedef struct { int reg
[28]; } __wine_jmp_buf
;
100 #elif defined(__aarch64__)
101 typedef struct { __int64 reg
[24]; } __wine_jmp_buf
;
103 typedef struct { int reg
; } __wine_jmp_buf
;
106 extern int __cdecl
__attribute__ ((__nothrow__
,__returns_twice__
)) __wine_setjmpex( __wine_jmp_buf
*buf
,
107 EXCEPTION_REGISTRATION_RECORD
*frame
) DECLSPEC_HIDDEN
;
108 extern void __cdecl
__wine_longjmp( __wine_jmp_buf
*buf
, int retval
) DECLSPEC_HIDDEN DECLSPEC_NORETURN
;
109 extern void __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
110 void (*target
)(void) ) DECLSPEC_HIDDEN DECLSPEC_NORETURN
;
111 extern DWORD __cdecl
__wine_exception_handler( EXCEPTION_RECORD
*record
,
112 EXCEPTION_REGISTRATION_RECORD
*frame
,
114 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
115 extern DWORD __cdecl
__wine_exception_ctx_handler( EXCEPTION_RECORD
*record
,
116 EXCEPTION_REGISTRATION_RECORD
*frame
,
118 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
119 extern DWORD __cdecl
__wine_exception_handler_page_fault( EXCEPTION_RECORD
*record
,
120 EXCEPTION_REGISTRATION_RECORD
*frame
,
122 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
123 extern DWORD __cdecl
__wine_exception_handler_all( EXCEPTION_RECORD
*record
,
124 EXCEPTION_REGISTRATION_RECORD
*frame
,
126 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
127 extern DWORD __cdecl
__wine_finally_handler( EXCEPTION_RECORD
*record
,
128 EXCEPTION_REGISTRATION_RECORD
*frame
,
130 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
131 extern DWORD __cdecl
__wine_finally_ctx_handler( EXCEPTION_RECORD
*record
,
132 EXCEPTION_REGISTRATION_RECORD
*frame
,
134 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
137 do { __WINE_FRAME __f; \
139 for (;;) if (!__first) \
143 #define __EXCEPT(func) \
145 __wine_pop_frame( &__f.frame ); \
148 __f.frame.Handler = __wine_exception_handler; \
149 __f.u.filter = (func); \
150 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
151 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
154 #define __EXCEPT_CTX(func, context) \
156 __wine_pop_frame( &__f.frame ); \
159 __f.frame.Handler = __wine_exception_ctx_handler; \
160 __f.u.filter_ctx = (func); \
162 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
163 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
166 /* convenience handler for page fault exceptions */
167 #define __EXCEPT_PAGE_FAULT \
169 __wine_pop_frame( &__f.frame ); \
172 __f.frame.Handler = __wine_exception_handler_page_fault; \
173 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
174 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
177 /* convenience handler for all exceptions */
178 #define __EXCEPT_ALL \
180 __wine_pop_frame( &__f.frame ); \
183 __f.frame.Handler = __wine_exception_handler_all; \
184 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
185 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
192 __wine_push_frame( &__f.frame ); \
197 #define __FINALLY(func) \
199 __wine_pop_frame( &__f.frame ); \
203 __f.frame.Handler = __wine_finally_handler; \
204 __f.u.finally_func = (func); \
205 __wine_push_frame( &__f.frame ); \
210 #define __FINALLY_CTX(func, context) \
212 __wine_pop_frame( &__f.frame ); \
213 (func)(1, context); \
216 __f.frame.Handler = __wine_finally_ctx_handler; \
217 __f.u.finally_func_ctx = (func); \
219 __wine_push_frame( &__f.frame ); \
225 typedef LONG (CALLBACK
*__WINE_FILTER
)(PEXCEPTION_POINTERS
);
226 typedef LONG (CALLBACK
*__WINE_FILTER_CTX
)(PEXCEPTION_POINTERS
, void*);
227 typedef void (CALLBACK
*__WINE_FINALLY
)(BOOL
);
228 typedef void (CALLBACK
*__WINE_FINALLY_CTX
)(BOOL
, void*);
230 #define GetExceptionInformation() (__eptr)
231 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
232 #define AbnormalTermination() (!__normal)
234 typedef struct __tagWINE_FRAME
236 EXCEPTION_REGISTRATION_RECORD frame
;
240 __WINE_FILTER filter
;
241 __WINE_FILTER_CTX filter_ctx
;
243 __WINE_FINALLY finally_func
;
244 __WINE_FINALLY_CTX finally_func_ctx
;
248 /* hack to make GetExceptionCode() work in handler */
250 const struct __tagWINE_FRAME
*ExceptionRecord
;
253 #endif /* USE_COMPILER_EXCEPTIONS */
255 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_push_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
257 #if defined(__GNUC__) && defined(__i386__)
258 EXCEPTION_REGISTRATION_RECORD
*prev
;
259 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
261 "\n\t.byte 0x64\n\tmovl %1,(0)"
262 : "=&r" (prev
) : "r" (frame
) : "memory" );
265 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
266 frame
->Prev
= teb
->ExceptionList
;
267 teb
->ExceptionList
= frame
;
272 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
274 #if defined(__GNUC__) && defined(__i386__)
275 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
276 : : "r" (frame
->Prev
) : "memory" );
280 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
281 teb
->ExceptionList
= frame
->Prev
;
286 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_get_frame(void)
288 #if defined(__GNUC__) && defined(__i386__)
289 EXCEPTION_REGISTRATION_RECORD
*ret
;
290 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret
) );
293 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
294 return teb
->ExceptionList
;
298 /* Exception handling flags - from OS/2 2.0 exception handling */
300 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
301 #define EH_NONCONTINUABLE 0x01
302 #define EH_UNWINDING 0x02
303 #define EH_EXIT_UNWIND 0x04
304 #define EH_STACK_INVALID 0x08
305 #define EH_NESTED_CALL 0x10
306 #define EH_TARGET_UNWIND 0x20
307 #define EH_COLLIDED_UNWIND 0x40
309 /* Wine-specific exceptions codes */
311 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
312 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
318 #endif /* __WINE_WINE_EXCEPTION_H */