Merging r37048, r37051, r37052, r37055 from the-real-msvc branch
[reactos.git] / reactos / include / reactos / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
83
84 #else /* USE_COMPILER_EXCEPTIONS */
85
86 #ifndef __GNUC__
87 #define __attribute__(x) /* nothing */
88 #endif
89
90 #define __TRY \
91 do { __WINE_FRAME __f; \
92 int __first = 1; \
93 for (;;) if (!__first) \
94 { \
95 do {
96
97 #define __EXCEPT(func) \
98 } while(0); \
99 __wine_pop_frame( &__f.frame ); \
100 break; \
101 } else { \
102 __f.frame.Handler = __wine_exception_handler; \
103 __f.u.filter = (func); \
104 if (setjmp( __f.jmp )) { \
105 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
106 do {
107
108 /* convenience handler for page fault exceptions */
109 #define __EXCEPT_PAGE_FAULT \
110 } while(0); \
111 __wine_pop_frame( &__f.frame ); \
112 break; \
113 } else { \
114 __f.frame.Handler = __wine_exception_handler_page_fault; \
115 if (setjmp( __f.jmp )) { \
116 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
117 do {
118
119 /* convenience handler for all exception */
120 #define __EXCEPT_ALL \
121 } while(0); \
122 __wine_pop_frame( &__f.frame ); \
123 break; \
124 } else { \
125 __f.frame.Handler = __wine_exception_handler_all; \
126 if (setjmp( __f.jmp )) { \
127 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
128 do {
129
130 #define __ENDTRY \
131 } while (0); \
132 break; \
133 } \
134 __wine_push_frame( &__f.frame ); \
135 __first = 0; \
136 } \
137 } while (0);
138
139 #define __FINALLY(func) \
140 } while(0); \
141 __wine_pop_frame( &__f.frame ); \
142 (func)(1); \
143 break; \
144 } else { \
145 __f.frame.Handler = __wine_finally_handler; \
146 __f.u.finally_func = (func); \
147 __wine_push_frame( &__f.frame ); \
148 __first = 0; \
149 } \
150 } while (0);
151
152
153 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
154 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
155
156 #define WINE_EXCEPTION_FILTER(func) LONG WINAPI func( EXCEPTION_POINTERS *__eptr )
157 #define WINE_FINALLY_FUNC(func) void CALLBACK func( BOOL __normal )
158
159 #define GetExceptionInformation() (__eptr)
160 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
161
162 #undef AbnormalTermination
163 #define AbnormalTermination() (!__normal)
164
165 typedef struct __tagWINE_FRAME
166 {
167 EXCEPTION_REGISTRATION_RECORD frame;
168 union
169 {
170 /* exception data */
171 __WINE_FILTER filter;
172 /* finally data */
173 __WINE_FINALLY finally_func;
174 } u;
175 jmp_buf jmp;
176 /* hack to make GetExceptionCode() work in handler */
177 DWORD ExceptionCode;
178 const struct __tagWINE_FRAME *ExceptionRecord;
179 } __WINE_FRAME;
180
181 #endif /* USE_COMPILER_EXCEPTIONS */
182
183 static __inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
184 {
185 #if defined(__GNUC__) && defined(__i386__)
186 EXCEPTION_REGISTRATION_RECORD *prev;
187 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
188 "\n\tmovl %0,(%1)"
189 "\n\t.byte 0x64\n\tmovl %1,(0)"
190 : "=&r" (prev) : "r" (frame) : "memory" );
191 return prev;
192 #else
193 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
194 frame->Prev = (void *)teb->ExceptionList;
195 teb->ExceptionList = (void *)frame;
196 return frame->Prev;
197 #endif
198 }
199
200 static __inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
201 {
202 #if defined(__GNUC__) && defined(__i386__)
203 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
204 : : "r" (frame->Prev) : "memory" );
205 return frame->Prev;
206
207 #else
208 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
209 teb->ExceptionList = (void *)frame->Prev;
210 return frame->Prev;
211 #endif
212 }
213
214 #ifndef USE_COMPILER_EXCEPTIONS
215
216 static inline void DECLSPEC_NORETURN __wine_unwind_frame( EXCEPTION_RECORD *record,
217 EXCEPTION_REGISTRATION_RECORD *frame )
218 {
219 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
220
221 /* hack to make GetExceptionCode() work in handler */
222 wine_frame->ExceptionCode = record->ExceptionCode;
223 wine_frame->ExceptionRecord = wine_frame;
224
225 #if defined(__GNUC__) && defined(__i386__)
226 {
227 /* RtlUnwind clobbers registers on Windows */
228 int dummy1, dummy2, dummy3;
229 __asm__ __volatile__("pushl %%ebp\n\t"
230 "pushl %%ebx\n\t"
231 "pushl $0\n\t"
232 "pushl %2\n\t"
233 "pushl $0\n\t"
234 "pushl %1\n\t"
235 "call *%0\n\t"
236 "popl %%ebx\n\t"
237 "popl %%ebp"
238 : "=a" (dummy1), "=S" (dummy2), "=D" (dummy3)
239 : "0" (RtlUnwind), "1" (frame), "2" (record)
240 : "ecx", "edx", "memory" );
241 }
242 #else
243 RtlUnwind( frame, 0, record, 0 );
244 #endif
245 __wine_pop_frame( frame );
246 longjmp( wine_frame->jmp, 1 );
247 }
248
249 static __inline EXCEPTION_DISPOSITION
250 __wine_exception_handler( struct _EXCEPTION_RECORD *record, void *frame,
251 struct _CONTEXT *context, void *pdispatcher )
252 {
253 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
254
255 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
256 return ExceptionContinueSearch;
257
258 if (wine_frame->u.filter == (void *)1) /* special hack for page faults */
259 {
260 if (record->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
261 return ExceptionContinueSearch;
262 }
263 else if (wine_frame->u.filter)
264 {
265 EXCEPTION_POINTERS ptrs;
266 ptrs.ExceptionRecord = record;
267 ptrs.ContextRecord = context;
268 switch(wine_frame->u.filter( &ptrs ))
269 {
270 case EXCEPTION_CONTINUE_SEARCH:
271 return ExceptionContinueSearch;
272 case EXCEPTION_CONTINUE_EXECUTION:
273 return ExceptionContinueExecution;
274 case EXCEPTION_EXECUTE_HANDLER:
275 break;
276 default:
277 break;
278 }
279 }
280 __wine_unwind_frame( record, frame );
281 }
282
283 static __inline EXCEPTION_DISPOSITION
284 __wine_exception_handler_page_fault( struct _EXCEPTION_RECORD *record, void *frame,
285 struct _CONTEXT *context, void *pdispatcher )
286 {
287 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
288 return ExceptionContinueSearch;
289 if (record->ExceptionCode != STATUS_ACCESS_VIOLATION)
290 return ExceptionContinueSearch;
291 __wine_unwind_frame( record, frame );
292 }
293
294 static __inline EXCEPTION_DISPOSITION
295 __wine_exception_handler_all( struct _EXCEPTION_RECORD *record, void *frame,
296 struct _CONTEXT *context, void *pdispatcher )
297 {
298 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
299 return ExceptionContinueSearch;
300 __wine_unwind_frame( record, frame );
301 }
302
303 static __inline EXCEPTION_DISPOSITION
304 __wine_finally_handler( struct _EXCEPTION_RECORD *record, void *frame,
305 struct _CONTEXT *context, void *pdispatcher )
306 {
307 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
308 {
309 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
310 wine_frame->u.finally_func( FALSE );
311 }
312 return ExceptionContinueSearch;
313 }
314
315 #endif /* USE_COMPILER_EXCEPTIONS */
316
317 /* Exception handling flags - from OS/2 2.0 exception handling */
318
319 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
320 #define EH_NONCONTINUABLE 0x01
321 #define EH_UNWINDING 0x02
322 #define EH_EXIT_UNWIND 0x04
323 #define EH_STACK_INVALID 0x08
324 #define EH_NESTED_CALL 0x10
325
326 /* Wine-specific exceptions codes */
327
328 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
329 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
330
331 /* unhandled return status from vm86 mode */
332 #define EXCEPTION_VM86_INTx 0x80000110
333 #define EXCEPTION_VM86_STI 0x80000111
334 #define EXCEPTION_VM86_PICRETURN 0x80000112
335
336 extern void __wine_enter_vm86( CONTEXT *context );
337
338 static __inline WINE_EXCEPTION_FILTER(__wine_pagefault_filter)
339 {
340 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
341 return EXCEPTION_CONTINUE_SEARCH;
342 return EXCEPTION_EXECUTE_HANDLER;
343 }
344
345 #endif /* __WINE_WINE_EXCEPTION_H */