source: trunk/include/excpt.h@ 21633

Last change on this file since 21633 was 21633, checked in by dmik, 14 years ago

kernel32: Fixed crashes in code involving _try/except due to garbage in FS:[0]. See OpenJDK ticket #96 for details.

File size: 11.0 KB
Line 
1/*
2 * Project Odin Software License can be found in LICENSE.TXT
3 *
4 * Compiler-level Win32 SEH support for OS/2
5 *
6 * Copyright 2010 Dmitriy Kuminov
7 */
8
9/*
10 * NOTE: This __try/__except and __try/__finally/__leave implementation is not
11 * backed up by the low level compiler support and therefore the following
12 * limitations exist comparing to the MSVC implementation (breaking them will
13 * crash the application):
14 *
15 * 1. You cannot use the return statement within __try or __except or __finally
16 * blocks.
17 *
18 * 2. You cannot use the goto statement or the longjmp() function within __try
19 * or __except or __finally blocks if it passes control outside these blocks.
20 *
21 * 2. If you use __try and friends inside a do/while/for/switch block, you will
22 * lose the meaning of break and continue statements and must not use them.
23 *
24 * 3. The scopes of C and C++ exception blocks may not overlap (i.e. you cannot
25 * use try/catch inside __try/__except and vice versa).
26 *
27 * 4. There may be some other (yet unknown) limitations.
28 *
29 * Fortunately, in most cases, these limitations may be worked around by
30 * slightly changing the original source code.
31 */
32
33#ifndef __EXCPT_H__
34#define __EXCPT_H__
35
36#include <windows.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#if defined(__GNUC__)
43
44struct ___seh_EXCEPTION_FRAME;
45typedef int (*__seh_PEXCEPTION_HANDLER)(PEXCEPTION_RECORD,
46 struct ___seh_EXCEPTION_FRAME *,
47 PCONTEXT, PVOID);
48
49#pragma pack(1)
50
51typedef struct ___seh_EXCEPTION_FRAME
52{
53 /* + 0 */ struct ___seh_EXCEPTION_FRAME *pPrev;
54 /* + 4 */ __seh_PEXCEPTION_HANDLER pHandler;
55 /* + 8 */ void *pFilterCallback;
56 /* +12 */ void *pHandlerCallback;
57 /* +16 */ void *pHandlerContext;
58 /* +20 */ int filterResult;
59 /* +24 */ DWORD EBX;
60 /* +28 */ DWORD ESI;
61 /* +32 */ DWORD EDI;
62 /* +36 */ DWORD EBP;
63 /* +40 */ DWORD ESP;
64 /* +44 */ DWORD pPrevFrameOS2;
65 /* +48 */ EXCEPTION_POINTERS Pointers;
66 /* +56 */ int state;
67 /* +60 */ DWORD pPrevFrameWin32;
68 /* +64 */ DWORD Win32FS;
69}
70__seh_EXCEPTION_FRAME;
71
72#pragma pack()
73
74extern int __seh_handler(PEXCEPTION_RECORD pRec,
75 struct ___seh_EXCEPTION_FRAME *pFrame,
76 PCONTEXT pContext, PVOID pVoid);
77
78#define _exception_code() (__seh_frame.Pointers.ExceptionRecord->ExceptionCode)
79#define _exception_info() ((void *)&__seh_frame.Pointers)
80
81#define exception_code _exception_code
82#define exception_info (PEXCEPTION_POINTERS)_exception_info
83
84#define GetExceptionCode _exception_code
85#define GetExceptionInformation (PEXCEPTION_POINTERS)_exception_info
86
87#define __try \
88 volatile __seh_EXCEPTION_FRAME __seh_frame; \
89 __seh_frame.pHandler = __seh_handler; \
90 __seh_frame.Pointers.ExceptionRecord = NULL; \
91 __seh_frame.Pointers.ContextRecord = NULL; \
92 __seh_frame.state = 0; \
93 __asm__("\n0:\n"); /* pFilterCallback */ \
94 for (; __seh_frame.state <= 3; ++__seh_frame.state) \
95 if (__seh_frame.state == 0) \
96 { \
97 /* install exception handler (both Win32 and OS/2 chains) */ \
98 __asm__ ("leal %0, %%ecx; " \
99 "movl %%fs, %%eax; " \
100 "andl $0x0000FFFF, %%eax; " \
101 "movl %%eax, 64(%%ecx); " \
102 "movl %%fs:0, %%eax; " \
103 "movl %%eax, 0(%%ecx); " \
104 "movl %%eax, 60(%%ecx); " \
105 "movl $0b, 8(%%ecx); " \
106 "" \
107 "movl %%ebx, 24(%%ecx); " \
108 "movl %%esi, 28(%%ecx); " \
109 "movl %%edi, 32(%%ecx); " \
110 "movl %%ebp, 36(%%ecx); " \
111 "movl %%esp, 40(%%ecx); " \
112 "" \
113 "pushl %%fs; " \
114 "pushl $Dos32TIB; " \
115 "popl %%fs; " \
116 "movl %%fs:0, %%eax; " \
117 "movl %%eax, 44(%%ecx); " \
118 "movl %%ecx, %%fs:0; " \
119 "popl %%fs; " \
120 "" \
121 "movl %%ecx, %%fs:0; " \
122 : : "m" (__seh_frame) \
123 : "%eax", "%ecx"); \
124 {
125
126#define __except(filter_expr) \
127 } \
128 /* cause the next state to be 3 */ \
129 __seh_frame.state = 2; \
130 } \
131 else if (__seh_frame.state == 1) { \
132 /* execption caught, call filter expression */ \
133 __seh_frame.filterResult = (filter_expr); \
134 __asm__("leal %0, %%ebx; jmp *%1" \
135 : : "m"(__seh_frame), "m"(__seh_frame.pHandlerCallback) \
136 : "%ebx"); \
137 } \
138 else if (__seh_frame.state == 3) \
139 /* remove exception handler (note that for some reason SMP kernel \
140 * seems to garbage the Win32FS:[0] cell with the OS/2 exception \
141 * registration record, so use the original __seh_frame value) */ \
142 __asm__ ("leal %0, %%ecx; " \
143 "" \
144 "movl 64(%%ecx), %%eax; " \
145 "movl %%eax, %%fs; " \
146 "" \
147 "movl 60(%%ecx), %%eax; " \
148 "movl %%eax, %%fs:0; " \
149 "" \
150 "pushl %%fs; " \
151 "pushl $Dos32TIB; " \
152 "popl %%fs; " \
153 "movl 44(%%ecx), %%eax; " \
154 "movl %%eax, %%fs:0; " \
155 "popl %%fs; " \
156 : : "m"(__seh_frame) \
157 : "%eax", "%ecx"); \
158 else /* __seh_frame.state == 2 -> execute except block */
159
160#define __finally \
161 } \
162 /* cause the next state to be 2 */ \
163 __seh_frame.state = 1; \
164 } \
165 else if (__seh_frame.state == 1) { \
166 /* execption caught, handle and proceed to the filally block */ \
167 __seh_frame.filterResult = EXCEPTION_EXECUTE_HANDLER; \
168 __asm__("leal %0, %%ebx; jmp *%1" \
169 : : "m"(__seh_frame), "m"(__seh_frame.pHandlerCallback) \
170 : "%ebx"); \
171 } \
172 else if (__seh_frame.state == 3) \
173 /* remove exception handler (note that for some reason SMP kernel \
174 * seems to garbage the Win32FS:[0] cell with the OS/2 exception \
175 * registration record, so use the original __seh_frame value) */ \
176 __asm__ ("leal %0, %%ecx; " \
177 "" \
178 "movl 64(%%ecx), %%eax; " \
179 "movl %%eax, %%fs; " \
180 "" \
181 "movl 60(%%ecx), %%eax; " \
182 "movl %%eax, %%fs:0; " \
183 "" \
184 "pushl %%fs; " \
185 "pushl $Dos32TIB; " \
186 "popl %%fs; " \
187 "movl 44(%%ecx), %%eax; " \
188 "movl %%eax, %%fs:0; " \
189 "popl %%fs; " \
190 : : "m"(__seh_frame) \
191 : "%eax", "%ecx"); \
192 else /* __seh_frame.state == 2 -> execute finally block */
193
194#define __leave \
195 /* cause the next state to be 2 */ \
196 __seh_frame.state = 1; \
197 continue;
198
199#else /* defined(__GNUC__) */
200
201#warning "Structured exception handling is not supported for this compiler!"
202
203#endif /* defined(__GNUC__) */
204
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* __EXCPT_H__ */
210
Note: See TracBrowser for help on using the repository browser.