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.
73 #if !defined(__GNUC__) && !defined(__clang__)
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 DECLSPEC_NORETURN __cdecl
__wine_longjmp( __wine_jmp_buf
*buf
, int retval
) DECLSPEC_HIDDEN
;
109 extern void DECLSPEC_NORETURN __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
110 void (*target
)(void) ) DECLSPEC_HIDDEN
;
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 #define __EXCEPT_HANDLER(handler) \
168 __wine_pop_frame( &__f.frame ); \
171 __f.frame.Handler = (handler); \
172 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
173 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
176 #define __EXCEPT_PAGE_FAULT __EXCEPT_HANDLER(__wine_exception_handler_page_fault)
177 #define __EXCEPT_ALL __EXCEPT_HANDLER(__wine_exception_handler_all)
183 __wine_push_frame( &__f.frame ); \
188 #define __FINALLY(func) \
190 __wine_pop_frame( &__f.frame ); \
194 __f.frame.Handler = __wine_finally_handler; \
195 __f.u.finally_func = (func); \
196 __wine_push_frame( &__f.frame ); \
201 #define __FINALLY_CTX(func, context) \
203 __wine_pop_frame( &__f.frame ); \
204 (func)(1, context); \
207 __f.frame.Handler = __wine_finally_ctx_handler; \
208 __f.u.finally_func_ctx = (func); \
210 __wine_push_frame( &__f.frame ); \
216 typedef LONG (CALLBACK
*__WINE_FILTER
)(PEXCEPTION_POINTERS
);
217 typedef LONG (CALLBACK
*__WINE_FILTER_CTX
)(PEXCEPTION_POINTERS
, void*);
218 typedef void (CALLBACK
*__WINE_FINALLY
)(BOOL
);
219 typedef void (CALLBACK
*__WINE_FINALLY_CTX
)(BOOL
, void*);
221 #define GetExceptionInformation() (__eptr)
222 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
223 #define AbnormalTermination() (!__normal)
225 typedef struct __tagWINE_FRAME
227 EXCEPTION_REGISTRATION_RECORD frame
;
231 __WINE_FILTER filter
;
232 __WINE_FILTER_CTX filter_ctx
;
234 __WINE_FINALLY finally_func
;
235 __WINE_FINALLY_CTX finally_func_ctx
;
239 /* hack to make GetExceptionCode() work in handler */
241 const struct __tagWINE_FRAME
*ExceptionRecord
;
244 #endif /* USE_COMPILER_EXCEPTIONS */
246 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_push_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
248 #if defined(__GNUC__) && defined(__i386__)
249 EXCEPTION_REGISTRATION_RECORD
*prev
;
250 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
252 "\n\t.byte 0x64\n\tmovl %1,(0)"
253 : "=&r" (prev
) : "r" (frame
) : "memory" );
256 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
257 frame
->Prev
= teb
->ExceptionList
;
258 teb
->ExceptionList
= frame
;
263 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
265 #if defined(__GNUC__) && defined(__i386__)
266 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
267 : : "r" (frame
->Prev
) : "memory" );
271 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
272 teb
->ExceptionList
= frame
->Prev
;
277 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_get_frame(void)
279 #if defined(__GNUC__) && defined(__i386__)
280 EXCEPTION_REGISTRATION_RECORD
*ret
;
281 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret
) );
284 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
285 return teb
->ExceptionList
;
289 /* Exception handling flags - from OS/2 2.0 exception handling */
291 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
292 #define EH_NONCONTINUABLE 0x01
293 #define EH_UNWINDING 0x02
294 #define EH_EXIT_UNWIND 0x04
295 #define EH_STACK_INVALID 0x08
296 #define EH_NESTED_CALL 0x10
297 #define EH_TARGET_UNWIND 0x20
298 #define EH_COLLIDED_UNWIND 0x40
300 /* Wine-specific exceptions codes */
302 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
303 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
309 #endif /* __WINE_WINE_EXCEPTION_H */