source: trunk/include/excpt.h@ 21635

Last change on this file since 21635 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
RevLine 
[4]1/*
[21381]2 * Project Odin Software License can be found in LICENSE.TXT
[4]3 *
[21381]4 * Compiler-level Win32 SEH support for OS/2
[4]5 *
[21605]6 * Copyright 2010 Dmitriy Kuminov
[4]7 */
8
[21448]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 *
[21474]15 * 1. You cannot use the return statement within __try or __except or __finally
16 * blocks.
[21448]17 *
[21474]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 *
[21448]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
[21381]33#ifndef __EXCPT_H__
34#define __EXCPT_H__
35
[21387]36#include <windows.h>
[21381]37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#if defined(__GNUC__)
43
[21474]44struct ___seh_EXCEPTION_FRAME;
[21381]45typedef int (*__seh_PEXCEPTION_HANDLER)(PEXCEPTION_RECORD,
[21474]46 struct ___seh_EXCEPTION_FRAME *,
[21381]47 PCONTEXT, PVOID);
48
[21474]49#pragma pack(1)
50
51typedef struct ___seh_EXCEPTION_FRAME
[21381]52{
[21474]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;
[21633]68 /* +64 */ DWORD Win32FS;
[21381]69}
[21474]70__seh_EXCEPTION_FRAME;
[21381]71
[21474]72#pragma pack()
[21381]73
[21474]74extern int __seh_handler(PEXCEPTION_RECORD pRec,
75 struct ___seh_EXCEPTION_FRAME *pFrame,
76 PCONTEXT pContext, PVOID pVoid);
77
[21387]78#define _exception_code() (__seh_frame.Pointers.ExceptionRecord->ExceptionCode)
[21474]79#define _exception_info() ((void *)&__seh_frame.Pointers)
[21381]80
[21474]81#define exception_code _exception_code
82#define exception_info (PEXCEPTION_POINTERS)_exception_info
83
[21387]84#define GetExceptionCode _exception_code
[21474]85#define GetExceptionInformation (PEXCEPTION_POINTERS)_exception_info
[21387]86
[21381]87#define __try \
[21474]88 volatile __seh_EXCEPTION_FRAME __seh_frame; \
89 __seh_frame.pHandler = __seh_handler; \
[21427]90 __seh_frame.Pointers.ExceptionRecord = NULL; \
91 __seh_frame.Pointers.ContextRecord = NULL; \
[21382]92 __seh_frame.state = 0; \
93 __asm__("\n0:\n"); /* pFilterCallback */ \
94 for (; __seh_frame.state <= 3; ++__seh_frame.state) \
[21381]95 if (__seh_frame.state == 0) \
96 { \
[21474]97 /* install exception handler (both Win32 and OS/2 chains) */ \
98 __asm__ ("leal %0, %%ecx; " \
[21633]99 "movl %%fs, %%eax; " \
100 "andl $0x0000FFFF, %%eax; " \
101 "movl %%eax, 64(%%ecx); " \
[21381]102 "movl %%fs:0, %%eax; " \
103 "movl %%eax, 0(%%ecx); " \
[21474]104 "movl %%eax, 60(%%ecx); " \
[21382]105 "movl $0b, 8(%%ecx); " \
[21381]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); " \
[21474]118 "movl %%ecx, %%fs:0; " \
[21381]119 "popl %%fs; " \
120 "" \
121 "movl %%ecx, %%fs:0; " \
122 : : "m" (__seh_frame) \
123 : "%eax", "%ecx"); \
124 {
125
126#define __except(filter_expr) \
127 } \
[21448]128 /* cause the next state to be 3 */ \
[21381]129 __seh_frame.state = 2; \
130 } \
131 else if (__seh_frame.state == 1) { \
[21448]132 /* execption caught, call filter expression */ \
[21381]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) \
[21633]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; " \
[21624]143 "" \
[21633]144 "movl 64(%%ecx), %%eax; " \
145 "movl %%eax, %%fs; " \
146 "" \
[21474]147 "movl 60(%%ecx), %%eax; " \
[21381]148 "movl %%eax, %%fs:0; " \
[21474]149 "" \
150 "pushl %%fs; " \
151 "pushl $Dos32TIB; " \
152 "popl %%fs; " \
153 "movl 44(%%ecx), %%eax; " \
154 "movl %%eax, %%fs:0; " \
155 "popl %%fs; " \
[21633]156 : : "m"(__seh_frame) \
[21474]157 : "%eax", "%ecx"); \
[21448]158 else /* __seh_frame.state == 2 -> execute except block */
[21381]159
[21448]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) \
[21633]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; " \
[21624]177 "" \
[21633]178 "movl 64(%%ecx), %%eax; " \
179 "movl %%eax, %%fs; " \
180 "" \
[21474]181 "movl 60(%%ecx), %%eax; " \
[21448]182 "movl %%eax, %%fs:0; " \
[21474]183 "" \
184 "pushl %%fs; " \
185 "pushl $Dos32TIB; " \
186 "popl %%fs; " \
187 "movl 44(%%ecx), %%eax; " \
188 "movl %%eax, %%fs:0; " \
189 "popl %%fs; " \
[21633]190 : : "m"(__seh_frame) \
[21474]191 : "%eax", "%ecx"); \
[21448]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
[21381]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__ */
[21387]210
Note: See TracBrowser for help on using the repository browser.