explorer:
[reactos.git] / reactos / include / wine / exception.h
1 /*
2 * Wine exception handling
3 *
4 * Copyright (c) 1999 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
23
24 #include <setjmp.h>
25 #include <windef.h>
26 #include <excpt.h>
27
28 /* The following definitions allow using exceptions in Wine and Winelib code
29 *
30 * They should be used like this:
31 *
32 * __TRY
33 * {
34 * do some stuff that can raise an exception
35 * }
36 * __EXCEPT(filter_func,param)
37 * {
38 * handle the exception here
39 * }
40 * __ENDTRY
41 *
42 * or
43 *
44 * __TRY
45 * {
46 * do some stuff that can raise an exception
47 * }
48 * __FINALLY(finally_func,param)
49 *
50 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
51 * macro, and return one of the EXCEPTION_* code; it can use
52 * GetExceptionInformation and GetExceptionCode to retrieve the
53 * exception info.
54 *
55 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
56 *
57 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
58 * break out of the current block. You cannot use 'return', 'goto'
59 * or 'longjmp' to leave a __TRY block, as this will surely crash.
60 * You can use them to leave a __EXCEPT block though.
61 *
62 * -- AJ
63 */
64
65 typedef struct _EXCEPTION_REGISTRATION_RECORD
66 {
67 struct _EXCEPTION_REGISTRATION_RECORD *Prev;
68 PEXCEPTION_HANDLER Handler;
69 } EXCEPTION_REGISTRATION_RECORD, *PEXCEPTION_REGISTRATION_RECORD;
70
71 /* Define this if you want to use your compiler built-in __try/__except support.
72 * This is only useful when compiling to a native Windows binary, as the built-in
73 * compiler exceptions will most certainly not work under Winelib.
74 */
75 #ifdef USE_COMPILER_EXCEPTIONS
76
77 #define __TRY __try
78 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
79 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
80 #define __ENDTRY /*nothing*/
81 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
82
83 #else /* USE_COMPILER_EXCEPTIONS */
84
85 #ifndef __GNUC__
86 #define __attribute__(x) /* nothing */
87 #endif
88
89 #define __TRY \
90 do { __WINE_FRAME __f; \
91 int __first = 1; \
92 for (;;) if (!__first) \
93 { \
94 do {
95
96 #define __EXCEPT(func) \
97 } while(0); \
98 __wine_pop_frame( &__f.frame ); \
99 break; \
100 } else { \
101 __f.frame.Handler = __wine_exception_handler; \
102 __f.u.filter = (func); \
103 __wine_push_frame( &__f.frame ); \
104 if (setjmp( __f.jmp )) { \
105 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
106 do {
107
108 #define __ENDTRY \
109 } while (0); \
110 break; \
111 } \
112 __first = 0; \
113 } \
114 } while (0);
115
116 #define __FINALLY(func) \
117 } while(0); \
118 __wine_pop_frame( &__f.frame ); \
119 (func)(1); \
120 break; \
121 } else { \
122 __f.frame.Handler = __wine_finally_handler; \
123 __f.u.finally_func = (func); \
124 __wine_push_frame( &__f.frame ); \
125 __first = 0; \
126 } \
127 } while (0);
128
129
130 typedef DWORD (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
131 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
132
133 /* convenience handler for page fault exceptions */
134 #define __EXCEPT_PAGE_FAULT __EXCEPT( (__WINE_FILTER)1 )
135
136 #define WINE_EXCEPTION_FILTER(func) DWORD CALLBACK func( PEXCEPTION_POINTERS __eptr )
137 #define WINE_FINALLY_FUNC(func) void CALLBACK func( BOOL __normal )
138
139 #define GetExceptionInformation() (__eptr)
140 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
141
142 #undef AbnormalTermination
143 #define AbnormalTermination() (!__normal)
144
145 typedef struct __tagWINE_FRAME
146 {
147 EXCEPTION_REGISTRATION_RECORD frame;
148 union
149 {
150 /* exception data */
151 __WINE_FILTER filter;
152 /* finally data */
153 __WINE_FINALLY finally_func;
154 } u;
155 jmp_buf jmp;
156 /* hack to make GetExceptionCode() work in handler */
157 DWORD ExceptionCode;
158 const struct __tagWINE_FRAME *ExceptionRecord;
159 } __WINE_FRAME;
160
161 #endif /* USE_COMPILER_EXCEPTIONS */
162
163 static __inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
164 {
165 #if defined(__GNUC__) && defined(__i386__)
166 EXCEPTION_REGISTRATION_RECORD *prev;
167 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
168 "\n\tmovl %0,(%1)"
169 "\n\t.byte 0x64\n\tmovl %1,(0)"
170 : "=&r" (prev) : "r" (frame) : "memory" );
171 return prev;
172 #else
173 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
174 frame->Prev = (void *)teb->ExceptionList;
175 teb->ExceptionList = (void *)frame;
176 return frame->Prev;
177 #endif
178 }
179
180 static __inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
181 {
182 #if defined(__GNUC__) && defined(__i386__)
183 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
184 : : "r" (frame->Prev) : "memory" );
185 return frame->Prev;
186
187 #else
188 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
189 teb->ExceptionList = (void *)frame->Prev;
190 return frame->Prev;
191 #endif
192 }
193
194 #ifndef USE_COMPILER_EXCEPTIONS
195
196 extern VOID NTAPI RtlUnwind(PVOID,PVOID,PEXCEPTION_RECORD,PVOID);
197
198 static __inline EXCEPTION_DISPOSITION
199 __wine_exception_handler( struct _EXCEPTION_RECORD *record, void *frame,
200 struct _CONTEXT *context, void *pdispatcher )
201 {
202 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
203
204 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
205 return ExceptionContinueSearch;
206
207 if (wine_frame->u.filter == (void *)1) /* special hack for page faults */
208 {
209 if (record->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
210 return ExceptionContinueSearch;
211 }
212 else if (wine_frame->u.filter)
213 {
214 EXCEPTION_POINTERS ptrs;
215 ptrs.ExceptionRecord = record;
216 ptrs.ContextRecord = context;
217 switch(wine_frame->u.filter( &ptrs ))
218 {
219 case EXCEPTION_CONTINUE_SEARCH:
220 return ExceptionContinueSearch;
221 case EXCEPTION_CONTINUE_EXECUTION:
222 return ExceptionContinueExecution;
223 case EXCEPTION_EXECUTE_HANDLER:
224 break;
225 default:
226 break;
227 }
228 }
229 /* hack to make GetExceptionCode() work in handler */
230 wine_frame->ExceptionCode = record->ExceptionCode;
231 wine_frame->ExceptionRecord = wine_frame;
232
233 RtlUnwind( frame, 0, record, 0 );
234 __wine_pop_frame( frame );
235 longjmp( wine_frame->jmp, 1 );
236 }
237
238
239 static __inline EXCEPTION_DISPOSITION
240 __wine_finally_handler( struct _EXCEPTION_RECORD *record, void *frame,
241 struct _CONTEXT *context, void *pdispatcher )
242 {
243 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
244 {
245 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
246 wine_frame->u.finally_func( FALSE );
247 }
248 return ExceptionContinueSearch;
249 }
250
251 #endif /* USE_COMPILER_EXCEPTIONS */
252
253
254 /* Wine-specific exceptions codes */
255
256 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
257 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
258
259 /* unhandled return status from vm86 mode */
260 #define EXCEPTION_VM86_INTx 0x80000110
261 #define EXCEPTION_VM86_STI 0x80000111
262 #define EXCEPTION_VM86_PICRETURN 0x80000112
263
264 extern void __wine_enter_vm86( CONTEXT *context );
265
266 static __inline WINE_EXCEPTION_FILTER(__wine_pagefault_filter)
267 {
268 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
269 return EXCEPTION_CONTINUE_SEARCH;
270 return EXCEPTION_EXECUTE_HANDLER;
271 }
272
273 #endif /* __WINE_WINE_EXCEPTION_H */