2 * PROJECT: ReactOS system libraries
3 * LICENSE: GNU GPL - See COPYING in the top level directory
4 * PURPOSE: Header for PSEH3
5 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
8 /* For additional information see pseh3.c in the related library. */
15 typedef struct _SEH3$_SCOPE_TABLE
19 } SEH3$_SCOPE_TABLE
, *PSEH3$_SCOPE_TABLE
;
21 typedef struct _SEH3$_EXCEPTION_POINTERS
23 struct _EXCEPTION_RECORD
*ExceptionRecord
;
24 struct _CONTEXT
*ContextRecord
;
25 } SEH3$_EXCEPTION_POINTERS
, *PSEH3$_EXCEPTION_POINTERS
;
27 typedef struct _SEH3$_REGISTRATION_FRAME
29 /* First the Windows base record. Don't move this! */
30 struct _SEH3$_REGISTRATION_FRAME
*Next
;
33 /* Points to the end of the internal registration chain */
34 struct _SEH3$_REGISTRATION_FRAME
*EndOfChain
;
36 /* Pointer to the static scope table */
37 PSEH3$_SCOPE_TABLE ScopeTable
;
39 /* Except handler stores pointer to exception pointers here */
40 PSEH3$_EXCEPTION_POINTERS
volatile ExceptionPointers
;
42 /* Registers that we need to save */
46 } SEH3$_REGISTRATION_FRAME
,*PSEH3$_REGISTRATION_FRAME
;
48 /* Prevent gcc from inlining functions that use SEH. */
49 static inline __attribute__((always_inline
)) __attribute__((returns_twice
)) void _SEH3$
_PreventInlining() {}
51 extern inline __attribute__((always_inline
,gnu_inline
))
52 void _SEH3$
_UnregisterFrame(volatile SEH3$_REGISTRATION_FRAME
*RegistrationFrame
)
54 asm volatile ("movl %k[NewHead], %%fs:0"
55 : : [NewHead
] "ir" (RegistrationFrame
->Next
) : "memory");
58 extern inline __attribute__((always_inline
,gnu_inline
))
59 void _SEH3$
_UnregisterTryLevel(
60 volatile SEH3$_REGISTRATION_FRAME
*TrylevelFrame
)
62 volatile SEH3$_REGISTRATION_FRAME
*RegistrationFrame
;
63 asm volatile ("movl %%fs:0, %k[RegistrationFrame]"
64 : [RegistrationFrame
] "=r" (RegistrationFrame
) : );
65 RegistrationFrame
->EndOfChain
= TrylevelFrame
->Next
;
73 /* These are global dummy definitions, that get overwritten in the local context of __finally / __except blocks */
74 int __cdecl
__attribute__((error ("Can only be used inside a __finally block."))) _abnormal_termination(void);
75 unsigned long __cdecl
__attribute__((error("Can only be used inside an exception filter or __except block."))) _exception_code(void);
76 void * __cdecl
__attribute__((error("Can only be used inside an exception filter."))) _exception_info(void);
78 /* Define the registers that get clobbered, when reaching the __except block.
79 We specify ebp on optimized builds without frame pointer, since it will be
80 used by GCC as a general purpose register then. */
81 #if defined(__OPTIMIZE__) && defined(_ALLOW_OMIT_FRAME_POINTER)
82 #define _SEH3$_CLOBBER_ON_EXCEPTION "ebp", "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"
84 #define _SEH3$_CLOBBER_ON_EXCEPTION "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"
87 /* This attribute allows automatic cleanup of the registered frames */
88 #define _SEH3$_AUTO_CLEANUP __attribute__((cleanup(_SEH3$_AutoCleanup)))
90 #define _SEH3$_ASM_GOTO(_Asm, _Label, ...) asm goto (_Asm : : : "memory", ## __VA_ARGS__ : _Label)
92 #define _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
93 inline __attribute__((always_inline, gnu_inline)) \
94 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; }
96 /* This is an asm wrapper around _SEH3$_RegisterFrame */
97 #define _SEH3$_RegisterFrame(_TrylevelFrame, _DataTable, _Target) \
98 asm goto ("leal %0, %%ecx\n" \
99 "call __SEH3$_RegisterFrame\n" \
101 : "m" (*(_TrylevelFrame)), "a" (_DataTable) \
102 : "ecx", "edx", "memory" \
105 /* This is an asm wrapper around _SEH3$_EnterTryLevel */
106 #define _SEH3$_RegisterTryLevel(_TrylevelFrame, _DataTable, _Target) \
107 asm goto ("leal %0, %%ecx\n" \
108 "call __SEH3$_RegisterTryLevel\n" \
110 : "m" (*(_TrylevelFrame)), "a" (_DataTable) \
111 : "ecx", "edx", "memory" \
114 /* On GCC the filter function is a nested function with __fastcall calling
115 convention. The eax register contains a base address the function uses
116 to address the callers stack frame. __fastcall is chosen, because it gives
117 us an effective was of passing one argument to the function, that we need
118 to tell the function in a first pass to return informtion about the frame
119 base address. Since this is something GCC chooses arbitrarily, we call
120 the function with an arbitrary base address in eax first and then use the
121 result to calculate the correct address for a second call to the function. */
122 #define _SEH3$_DECLARE_FILTER_FUNC(_Name) \
123 auto int __fastcall _Name(int Action)
125 #define _SEH3$_NESTED_FUNC_OPEN(_Name) \
126 int __fastcall _Name(int Action) \
128 /* This is a fancy way to get information about the frame layout */ \
129 if (Action == 0) return (int)&_SEH3$_TrylevelFrame;
131 #define _SEH3$_DEFINE_FILTER_FUNC(_Name, expression) \
132 _SEH3$_NESTED_FUNC_OPEN(_Name) \
133 /* Declare the intrinsics for exception filters */ \
134 inline __attribute__((always_inline, gnu_inline)) \
135 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; } \
136 inline __attribute__((always_inline, gnu_inline)) \
137 void * _exception_info() { return _SEH3$_TrylevelFrame.ExceptionPointers; } \
139 /* Now handle the actual filter expression */ \
140 return (expression); \
143 #define _SEH3$_FINALLY_FUNC_OPEN(_Name) \
144 _SEH3$_NESTED_FUNC_OPEN(_Name) \
145 /* Declare the intrinsics for the finally function */ \
146 inline __attribute__((always_inline, gnu_inline)) \
147 int _abnormal_termination() { return (_SEH3$_TrylevelFrame.ScopeTable != 0); }
149 #define _SEH3$_FILTER(_Filter, _FilterExpression) \
150 (__builtin_constant_p(_FilterExpression) ? (void*)(unsigned long)(unsigned char)(unsigned long)(_FilterExpression) : _Filter)
152 #define _SEH3$_DEFINE_DUMMY_FINALLY(_Name) \
153 auto inline __attribute__((always_inline,gnu_inline)) int _Name(int Action) { return 0; }
155 #define _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
156 auto inline __attribute__((always_inline,gnu_inline)) void _Name(volatile SEH3$_REGISTRATION_FRAME *p)
158 #define _SEH3$_DEFINE_CLEANUP_FUNC(_Name) \
159 _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
161 /* Unregister the frame */ \
162 if (_SEH3$_TryLevel == 1) _SEH3$_UnregisterFrame(&_SEH3$_TrylevelFrame); \
163 else _SEH3$_UnregisterTryLevel(&_SEH3$_TrylevelFrame); \
165 /* Invoke the finally function (an inline dummy in the __except case) */ \
166 _SEH3$_FinallyFunction(1); \
169 /* This construct scares GCC so much, that it will stop moving code
170 around into places that are never executed. */
171 #define _SEH3$_SCARE_GCC() \
173 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_BeforeTry); \
174 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_HandlerTarget); \
175 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
176 asm volatile ("#" : "=a"(plabel) : "p"(&&_SEH3$_l_BeforeTry), "p"(&&_SEH3$_l_HandlerTarget), "p"(&&_SEH3$_l_OnException) \
177 : _SEH3$_CLOBBER_ON_EXCEPTION ); \
178 goto _SEH3$_l_OnException;
182 _SEH3$_PreventInlining(); \
183 /* Enter the outer scope */ \
185 /* Declare local labels */ \
186 __label__ _SEH3$_l_BeforeTry; \
187 __label__ _SEH3$_l_DoTry; \
188 __label__ _SEH3$_l_AfterTry; \
189 __label__ _SEH3$_l_EndTry; \
190 __label__ _SEH3$_l_HandlerTarget; \
191 __label__ _SEH3$_l_OnException; \
193 /* Count the try level. Outside of any __try, _SEH3$_TryLevel is 0 */ \
195 _SEH3$_PreviousTryLevel = _SEH3$_TryLevel, \
196 _SEH3$_TryLevel = _SEH3$_PreviousTryLevel + 1, \
199 /* Forward declaration of the auto cleanup function */ \
200 _SEH3$_DECLARE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
202 /* Allocate a registration frame */ \
203 volatile SEH3$_REGISTRATION_FRAME _SEH3$_AUTO_CLEANUP _SEH3$_TrylevelFrame; \
205 goto _SEH3$_l_BeforeTry; \
206 /* Silence warning */ goto _SEH3$_l_AfterTry; \
212 #define _SEH3_EXCEPT(...) \
213 /* End the try block */ \
215 _SEH3$_l_AfterTry: (void)0; \
216 goto _SEH3$_l_EndTry; \
218 _SEH3$_l_BeforeTry: (void)0; \
219 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
221 /* Forward declaration of the filter function */ \
222 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FilterFunction); \
224 /* Create a static data table that contains the jump target and filter function */ \
225 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { &&_SEH3$_l_HandlerTarget, _SEH3$_FILTER(&_SEH3$_FilterFunction, (__VA_ARGS__)) }; \
227 /* Register the registration record. */ \
228 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
229 else _SEH3$_RegisterTryLevel(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
231 /* Emit the filter function */ \
232 _SEH3$_DEFINE_FILTER_FUNC(_SEH3$_FilterFunction, (__VA_ARGS__)) \
234 /* Define an empty inline finally function */ \
235 _SEH3$_DEFINE_DUMMY_FINALLY(_SEH3$_FinallyFunction) \
237 /* Allow intrinsics for __except to be used */ \
238 _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
240 goto _SEH3$_l_DoTry; \
242 _SEH3$_l_HandlerTarget: (void)0; \
249 #define _SEH3_FINALLY \
250 /* End the try block */ \
252 _SEH3$_l_AfterTry: (void)0; \
253 /* Set ScopeTable to 0, this is used by _abnormal_termination() */ \
254 _SEH3$_TrylevelFrame.ScopeTable = 0; \
256 goto _SEH3$_l_EndTry; \
258 _SEH3$_l_BeforeTry: (void)0; \
259 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
261 /* Forward declaration of the finally function */ \
262 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FinallyFunction); \
264 /* Create a static data table that contains the finally function */ \
265 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { 0, &_SEH3$_FinallyFunction }; \
267 /* Register the registration record. */ \
268 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
269 else _SEH3$_RegisterTryLevel(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
271 goto _SEH3$_l_DoTry; \
273 _SEH3$_l_HandlerTarget: (void)0; \
275 _SEH3$_FINALLY_FUNC_OPEN(_SEH3$_FinallyFunction) \
276 /* This construct makes sure that the finally function returns */ \
277 /* a proper value at the end */ \
278 for (; ; (void)({return 0; 0;}))
284 goto _SEH3$_l_EndTry; \
286 _SEH3$_l_OnException: (void)0; \
287 /* Force GCC to create proper code pathes */ \
290 _SEH3$_l_EndTry:(void)0; \
291 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
293 /* Implementation of the auto cleanup function */ \
294 _SEH3$_DEFINE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
296 /* Close the outer scope */ \
299 #define _SEH3_LEAVE goto _SEH3$_l_AfterTry
301 #define _SEH3_VOLATILE volatile