6aa15dd9c15385eabc516a5d6340262564a0b533
[reactos.git] / reactos / include / reactos / libs / pseh / pseh3.h
1 /*
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)
6 */
7
8 /* For additional information see pseh3.c in the related library. */
9
10 #pragma once
11 #define _PSEH3_H_
12
13 #include <excpt.h>
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /* CLANG must safe non-volatiles, because it uses a return-twice algorithm */
20 #if defined(__clang__) && !defined(_SEH3$_FRAME_ALL_NONVOLATILES)
21 #define _SEH3$_FRAME_ALL_NONVOLATILES 1
22 #endif
23
24 enum
25 {
26 _SEH3$_NESTED_HANDLER = 0,
27 _SEH3$_CPP_HANDLER = 1,
28 _SEH3$_CLANG_HANDLER = 2,
29 #ifdef __clang__
30 _SEH3$_HANDLER_TYPE = _SEH3$_CLANG_HANDLER,
31 #elif defined(__cplusplus)
32 _SEH3$_HANDLER_TYPE = _SEH3$_CPP_HANDLER,
33 #else
34 _SEH3$_HANDLER_TYPE = _SEH3$_NESTED_HANDLER,
35 #endif
36 };
37
38 typedef struct _SEH3$_SCOPE_TABLE
39 {
40 void *Target;
41 void *Filter;
42 unsigned char TryLevel;
43 unsigned char HandlerType;
44 } SEH3$_SCOPE_TABLE, *PSEH3$_SCOPE_TABLE;
45
46 typedef struct _SEH3$_EXCEPTION_POINTERS
47 {
48 struct _EXCEPTION_RECORD *ExceptionRecord;
49 struct _CONTEXT *ContextRecord;
50 } SEH3$_EXCEPTION_POINTERS, *PSEH3$_EXCEPTION_POINTERS;
51
52 typedef struct _SEH3$_REGISTRATION_FRAME
53 {
54 /* First the Windows base record. Don't move this! */
55 struct _SEH3$_REGISTRATION_FRAME *Next;
56 void *Handler;
57
58 /* Points to the end of the internal registration chain */
59 struct _SEH3$_REGISTRATION_FRAME *EndOfChain;
60
61 /* Pointer to the static scope table */
62 PSEH3$_SCOPE_TABLE ScopeTable;
63
64 /* Except handler stores pointer to exception pointers here */
65 PSEH3$_EXCEPTION_POINTERS volatile ExceptionPointers;
66
67 /* Registers that we need to save */
68 unsigned long Esp;
69 unsigned long Ebp;
70
71 char* AllocaFrame;
72 #ifdef _SEH3$_FRAME_ALL_NONVOLATILES
73 unsigned long Ebx;
74 unsigned long Esi;
75 unsigned long Edi;
76 #endif
77 } SEH3$_REGISTRATION_FRAME ,*PSEH3$_REGISTRATION_FRAME;
78
79 /* Prevent gcc from inlining functions that use SEH. */
80 static inline __attribute__((always_inline)) __attribute__((returns_twice)) void _SEH3$_PreventInlining() {}
81
82 /* Unregister the root frame */
83 extern inline __attribute__((always_inline,gnu_inline))
84 void _SEH3$_UnregisterFrame(volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame)
85 {
86 asm volatile ("movl %k[NewHead], %%fs:0"
87 : : [NewHead] "ir" (RegistrationFrame->Next) : "memory");
88 }
89
90 /* Unregister a trylevel frame */
91 extern inline __attribute__((always_inline,gnu_inline))
92 void _SEH3$_UnregisterTryLevel(
93 volatile SEH3$_REGISTRATION_FRAME *TrylevelFrame)
94 {
95 volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame;
96 asm volatile ("movl %%fs:0, %k[RegistrationFrame]"
97 : [RegistrationFrame] "=r" (RegistrationFrame) : );
98 RegistrationFrame->EndOfChain = TrylevelFrame->Next;
99 }
100
101 enum
102 {
103 _SEH3$_TryLevel = 0,
104 };
105
106 #ifndef __clang__
107 /* These are global dummy definitions, that get overwritten in the local context of __finally / __except blocks */
108 int __cdecl __attribute__((error ("Can only be used inside a __finally block."))) _abnormal_termination(void);
109 unsigned long __cdecl __attribute__((error("Can only be used inside an exception filter or __except block."))) _exception_code(void);
110 void * __cdecl __attribute__((error("Can only be used inside an exception filter."))) _exception_info(void);
111 #endif
112
113 /* This attribute allows automatic cleanup of the registered frames */
114 #define _SEH3$_AUTO_CLEANUP __attribute__((cleanup(_SEH3$_AutoCleanup)))
115
116 /* CLANG specific definitions! */
117 #ifdef __clang__
118
119 /* CLANG thinks it is smart and optimizes the alloca away if it is 0 and with it the use of a frame register */
120 #define _SEH3$_EnforceFramePointer() asm volatile ("#\n" : : "m"(*(char*)__builtin_alloca(4)) : "%esp", "memory")
121
122 /* CLANG doesn't have asm goto! */
123 #define _SEH3$_ASM_GOTO(_Label, ...)
124
125 int
126 __attribute__((regparm(3)))
127 __attribute__((returns_twice))
128 _SEH3$_RegisterFrameWithNonVolatiles(
129 volatile SEH3$_REGISTRATION_FRAME* RegistrationFrame,
130 const SEH3$_SCOPE_TABLE* ScopeTable,
131 void* AllocaFrame);
132
133 #define _SEH3$_RegisterFrame_(_TrylevelFrame, _DataTable) \
134 do { \
135 int result = _SEH3$_RegisterFrameWithNonVolatiles(_TrylevelFrame, _DataTable, __builtin_alloca(0)); \
136 if (__builtin_expect(result != 0, 0)) \
137 { \
138 if (result == 1) goto _SEH3$_l_FilterOrFinally; \
139 if (result == 2) goto _SEH3$_l_HandlerTarget; \
140 goto _SEH3$_l_BeforeFilterOrFinally; \
141 } \
142 } while(0)
143
144 int
145 __attribute__((regparm(3)))
146 __attribute__((returns_twice))
147 _SEH3$_RegisterTryLevelWithNonVolatiles(
148 volatile SEH3$_REGISTRATION_FRAME* RegistrationFrame,
149 const SEH3$_SCOPE_TABLE* ScopeTable,
150 void* AllocaFrame);
151
152 #define _SEH3$_RegisterTryLevel_(_TrylevelFrame, _DataTable) \
153 do { \
154 int result = _SEH3$_RegisterTryLevelWithNonVolatiles(_TrylevelFrame, _DataTable, __builtin_alloca(0)); \
155 if (__builtin_expect(result != 0, 0)) \
156 { \
157 if (result == 1) goto _SEH3$_l_FilterOrFinally; \
158 if (result == 2) goto _SEH3$_l_HandlerTarget; \
159 goto _SEH3$_l_BeforeFilterOrFinally; \
160 } \
161 } while(0)
162
163 #define _SEH3$_SCARE_GCC()
164
165 #else /* !__clang__ */
166
167 /* This will make GCC use ebp, even if it was disabled by -fomit-frame-pointer */
168 #define _SEH3$_EnforceFramePointer() asm volatile ("#\n" : : "m"(*(char*)__builtin_alloca(0)) : "%esp", "memory")
169
170 #define _SEH3$_ASM_GOTO(_Label, ...) asm goto ("#\n" : : : "memory", ## __VA_ARGS__ : _Label)
171
172 #ifdef __cplusplus
173 #define _SEH3$_CALL_WRAPPER(_Function, _TrylevelFrame, _DataTable) \
174 asm goto ("leal %0, %%eax\n" \
175 "leal %1, %%edx\n" \
176 "call " #_Function "WithStackLayout\n" \
177 : \
178 : "m" (*(_TrylevelFrame)), "m" (*(_DataTable)), "c"(__builtin_alloca(0)) \
179 : "eax", "edx", "memory" \
180 : _SEH3$_l_HandlerTarget, _SEH3$_l_FilterOrFinally)
181
182 #else
183 #define _SEH3$_CALL_WRAPPER(_Function, _TrylevelFrame, _DataTable) \
184 asm goto ("leal %0, %%eax\n" \
185 "leal %1, %%edx\n" \
186 "call " #_Function "\n" \
187 : \
188 : "m" (*(_TrylevelFrame)), "m" (*(_DataTable)) \
189 : "eax", "edx", "ecx", "memory" \
190 : _SEH3$_l_HandlerTarget)
191 #endif
192
193 /* This is an asm wrapper around _SEH3$_RegisterFrame */
194 #define _SEH3$_RegisterFrame_(_TrylevelFrame, _DataTable) \
195 _SEH3$_CALL_WRAPPER(__SEH3$_RegisterFrame, _TrylevelFrame, _DataTable)
196
197 /* This is an asm wrapper around _SEH3$_RegisterTryLevel */
198 #define _SEH3$_RegisterTryLevel_(_TrylevelFrame, _DataTable) \
199 _SEH3$_CALL_WRAPPER(__SEH3$_RegisterTryLevel, _TrylevelFrame, _DataTable)
200
201 /* This construct scares GCC so much, that it will stop moving code
202 around into places that are never executed. */
203 #define _SEH3$_SCARE_GCC() \
204 void *plabel; \
205 _SEH3$_ASM_GOTO(_SEH3$_l_BeforeTry); \
206 _SEH3$_ASM_GOTO(_SEH3$_l_HandlerTarget); \
207 _SEH3$_ASM_GOTO(_SEH3$_l_OnException); \
208 asm volatile ("#" : "=a"(plabel) : "p"(&&_SEH3$_l_BeforeTry), "p"(&&_SEH3$_l_HandlerTarget), "p"(&&_SEH3$_l_OnException) \
209 : "ebx", "ecx", "edx", "esi", "edi", "flags", "memory" ); \
210 goto _SEH3$_l_OnException;
211
212 #endif /* __clang__ */
213
214 /* Neither CLANG nor C++ support nested functions */
215 #if defined(__cplusplus) || defined(__clang__)
216
217 /* Use the global unregister function */
218 void
219 __attribute__((regparm(1)))
220 _SEH3$_AutoCleanup(
221 volatile SEH3$_REGISTRATION_FRAME *Frame);
222
223 /* These are only dummies here */
224 #define _SEH3$_DECLARE_CLEANUP_FUNC(_Name)
225 #define _SEH3$_DEFINE_CLEANUP_FUNC(_Name)
226 #define _SEH3$_DECLARE_FILTER_FUNC(_Name)
227 #define _SEH3$_DEFINE_DUMMY_FINALLY(_Name)
228
229 /* On invocation, the AllocaFrame field is loaded with the return esp value */
230 #define _SEH3$_NESTED_FUNC_RETURN(_Result) \
231 /* Restore esp and return to the caller */ \
232 asm volatile ("movl %[FixedEsp], %%esp\nret\n" \
233 : : "a"(_Result), [FixedEsp]"m"(_SEH3$_TrylevelFrame.AllocaFrame) : "ebx", "ecx", "edx", "esi", "edi", "flags", "memory")
234
235 /* The filter "function" */
236 #define _SEH3$_DEFINE_FILTER_FUNC(_Name, expression) \
237 { \
238 /* Evaluate and return the filter expression */ \
239 asm volatile ("#\n" : : : "eax", "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"); \
240 _SEH3$_NESTED_FUNC_RETURN((expression)); \
241 }
242
243 #define _SEH3$_FINALLY_FUNC_OPEN(_Name) \
244 { \
245 asm volatile ("#\n" : : : "eax", "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"); \
246 /* This construct makes sure that the finally function returns */ \
247 /* a proper value at the end */ \
248 for (; ; (void)({_SEH3$_NESTED_FUNC_RETURN(0); 0;}))
249
250 #define _SEH3$_FILTER(_Filter, _FilterExpression) (&&_SEH3$_l_FilterOrFinally)
251 #define _SEH3$_FINALLY(_Finally) (&&_SEH3$_l_FilterOrFinally)
252
253 #define _SEH3$_DECLARE_EXCEPT_INTRINSICS()
254
255 /* Since we cannot use nested functions, we declare these globally as macros */
256 #define _abnormal_termination() (_SEH3$_TrylevelFrame.ExceptionPointers != 0)
257 #define _exception_code() (_SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode)
258 #define _exception_info() (_SEH3$_TrylevelFrame.ExceptionPointers)
259
260 #else /* __cplusplus || __clang__ */
261
262 #define _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
263 inline __attribute__((always_inline, gnu_inline)) \
264 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; }
265
266 /* On GCC the filter function is a nested function with __fastcall calling
267 convention. The eax register contains a base address the function uses
268 to address the callers stack frame. __fastcall is chosen, because it gives
269 us an effective was of passing one argument to the function, that we need
270 to tell the function in a first pass to return informtion about the frame
271 base address. Since this is something GCC chooses arbitrarily, we call
272 the function with an arbitrary base address in eax first and then use the
273 result to calculate the correct address for a second call to the function. */
274 #define _SEH3$_DECLARE_FILTER_FUNC(_Name) \
275 auto int __fastcall _Name(int Action)
276
277 #define _SEH3$_NESTED_FUNC_OPEN(_Name) \
278 int __fastcall _Name(int Action) \
279 { \
280 /* This is a fancy way to get information about the frame layout */ \
281 if (Action == 0) return (int)&_SEH3$_TrylevelFrame;
282
283 #define _SEH3$_DEFINE_FILTER_FUNC(_Name, expression) \
284 _SEH3$_NESTED_FUNC_OPEN(_Name) \
285 /* Declare the intrinsics for exception filters */ \
286 _Pragma("GCC diagnostic push") \
287 _Pragma("GCC diagnostic ignored \"-Wshadow\"") \
288 inline __attribute__((always_inline, gnu_inline)) \
289 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; } \
290 inline __attribute__((always_inline, gnu_inline)) \
291 void * _exception_info() { return _SEH3$_TrylevelFrame.ExceptionPointers; } \
292 _Pragma("GCC diagnostic pop") \
293 \
294 /* Now handle the actual filter expression */ \
295 return (expression); \
296 }
297
298 #define _SEH3$_FINALLY_FUNC_OPEN(_Name) \
299 _SEH3$_NESTED_FUNC_OPEN(_Name) \
300 /* Declare the intrinsics for the finally function */ \
301 inline __attribute__((always_inline, gnu_inline)) \
302 int _abnormal_termination() { return (_SEH3$_TrylevelFrame.ExceptionPointers != 0); } \
303 \
304 /* This construct makes sure that the finally function returns */ \
305 /* a proper value at the end */ \
306 for (; ; (void)({return 0; 0;}))
307
308 #define _SEH3$_FILTER(_Filter, _FilterExpression) \
309 (__builtin_constant_p(_FilterExpression) ? (void*)(unsigned long)(unsigned char)(unsigned long)(_FilterExpression) : _Filter)
310
311 #define _SEH3$_FINALLY(_Finally) (_Finally)
312
313 #define _SEH3$_DEFINE_DUMMY_FINALLY(_Name) \
314 auto inline __attribute__((always_inline,gnu_inline)) int _Name(int Action) { (void)Action; return 0; }
315
316 #define _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
317 auto inline __attribute__((always_inline,gnu_inline)) void _Name(volatile SEH3$_REGISTRATION_FRAME *p)
318
319 #define _SEH3$_DEFINE_CLEANUP_FUNC(_Name) \
320 _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
321 { \
322 (void)p; \
323 /* Unregister the frame */ \
324 if (_SEH3$_TryLevel == 1) _SEH3$_UnregisterFrame(&_SEH3$_TrylevelFrame); \
325 else _SEH3$_UnregisterTryLevel(&_SEH3$_TrylevelFrame); \
326 \
327 /* Invoke the finally function (an inline dummy in the __except case) */ \
328 _SEH3$_FinallyFunction(1); \
329 }
330
331 #endif /* __cplusplus || __clang__ */
332
333
334
335 #define _SEH3_TRY \
336 _SEH3$_PreventInlining(); \
337 /* Enter the outer scope */ \
338 do { \
339 /* Declare local labels */ \
340 __label__ _SEH3$_l_BeforeTry; \
341 __label__ _SEH3$_l_DoTry; \
342 __label__ _SEH3$_l_AfterTry; \
343 __label__ _SEH3$_l_EndTry; \
344 __label__ _SEH3$_l_HandlerTarget; \
345 __label__ _SEH3$_l_OnException; \
346 __label__ _SEH3$_l_BeforeFilterOrFinally; \
347 __label__ _SEH3$_l_FilterOrFinally; \
348 (void)&&_SEH3$_l_OnException; \
349 (void)&&_SEH3$_l_BeforeFilterOrFinally; \
350 (void)&&_SEH3$_l_FilterOrFinally; \
351 \
352 _Pragma("GCC diagnostic push") \
353 _Pragma("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
354 \
355 /* Count the try level. Outside of any __try, _SEH3$_TryLevel is 0 */ \
356 enum { \
357 _SEH3$_PreviousTryLevel = _SEH3$_TryLevel, \
358 _SEH3$_TryLevel = _SEH3$_PreviousTryLevel + 1, \
359 }; \
360 \
361 /* Forward declaration of the auto cleanup function */ \
362 _SEH3$_DECLARE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
363 \
364 /* Allocate a registration frame */ \
365 volatile SEH3$_REGISTRATION_FRAME _SEH3$_AUTO_CLEANUP _SEH3$_TrylevelFrame; \
366 \
367 _Pragma("GCC diagnostic pop") \
368 \
369 goto _SEH3$_l_BeforeTry; \
370 /* Silence warning */ goto _SEH3$_l_AfterTry; \
371 \
372 _SEH3$_l_DoTry: \
373 do
374
375
376 #define _SEH3_EXCEPT(...) \
377 /* End the try block */ \
378 while (0); \
379 _SEH3$_l_AfterTry: (void)0; \
380 goto _SEH3$_l_EndTry; \
381 \
382 _SEH3$_l_BeforeTry: (void)0; \
383 _SEH3$_ASM_GOTO(_SEH3$_l_OnException); \
384 \
385 _Pragma("GCC diagnostic push") \
386 _Pragma("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
387 \
388 /* Forward declaration of the filter function */ \
389 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FilterFunction); \
390 \
391 /* Create a static data table that contains the jump target and filter function */ \
392 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { &&_SEH3$_l_HandlerTarget, _SEH3$_FILTER(&_SEH3$_FilterFunction, (__VA_ARGS__)), _SEH3$_TryLevel, _SEH3$_HANDLER_TYPE }; \
393 \
394 /* Register the registration record. */ \
395 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame_(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable); \
396 else _SEH3$_RegisterTryLevel_(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable); \
397 \
398 /* Define an empty inline finally function */ \
399 _SEH3$_DEFINE_DUMMY_FINALLY(_SEH3$_FinallyFunction) \
400 \
401 /* Allow intrinsics for __except to be used */ \
402 _SEH3$_DECLARE_EXCEPT_INTRINSICS(); \
403 \
404 goto _SEH3$_l_DoTry; \
405 \
406 _SEH3$_l_BeforeFilterOrFinally: (void)0; \
407 /* Make sure the filter function doesn't use esp */ \
408 _SEH3$_EnforceFramePointer(); \
409 \
410 _SEH3$_l_FilterOrFinally: (void)0; \
411 /* Emit the filter function */ \
412 _SEH3$_DEFINE_FILTER_FUNC(_SEH3$_FilterFunction, (__VA_ARGS__)) \
413 \
414 _SEH3$_l_HandlerTarget: (void)0; \
415 _SEH3$_EnforceFramePointer(); \
416 \
417 if (1) \
418 { \
419 /* Prevent this block from being optimized away */ \
420 asm volatile ("#\n"); \
421 do
422
423
424 #define _SEH3_FINALLY \
425 /* End the try block */ \
426 while (0); \
427 _SEH3$_l_AfterTry: (void)0; \
428 /* Set ExceptionPointers to 0, this is used by _abnormal_termination() */ \
429 _SEH3$_TrylevelFrame.ExceptionPointers = 0; \
430 \
431 goto _SEH3$_l_EndTry; \
432 \
433 _SEH3$_l_BeforeTry: (void)0; \
434 _SEH3$_ASM_GOTO(_SEH3$_l_OnException); \
435 \
436 _Pragma("GCC diagnostic push") \
437 _Pragma("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
438 \
439 /* Forward declaration of the finally function */ \
440 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FinallyFunction); \
441 \
442 /* Create a static data table that contains the finally function */ \
443 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { 0, _SEH3$_FINALLY(&_SEH3$_FinallyFunction), _SEH3$_TryLevel, _SEH3$_HANDLER_TYPE }; \
444 \
445 /* Register the registration record. */ \
446 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame_(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable); \
447 else _SEH3$_RegisterTryLevel_(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable); \
448 _SEH3$_TrylevelFrame.ExceptionPointers = (PSEH3$_EXCEPTION_POINTERS)1; \
449 \
450 goto _SEH3$_l_DoTry; \
451 \
452 _SEH3$_l_HandlerTarget: (void)0; \
453 _SEH3$_EnforceFramePointer(); \
454 \
455 _SEH3$_l_BeforeFilterOrFinally: (void)0; \
456 _SEH3$_EnforceFramePointer(); \
457 _SEH3$_l_FilterOrFinally: (void)0; \
458 _SEH3$_FINALLY_FUNC_OPEN(_SEH3$_FinallyFunction)
459
460
461 #define _SEH3_END \
462 while (0); \
463 }; \
464 goto _SEH3$_l_EndTry; \
465 \
466 _SEH3$_l_OnException: (void)0; \
467 /* Force GCC to create proper code pathes */ \
468 _SEH3$_SCARE_GCC(); \
469 \
470 _SEH3$_l_EndTry:(void)0; \
471 _SEH3$_ASM_GOTO(_SEH3$_l_OnException); \
472 \
473 /* Implementation of the auto cleanup function */ \
474 _SEH3$_DEFINE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
475 \
476 _Pragma("GCC diagnostic pop") \
477 \
478 /* Close the outer scope */ \
479 } while (0);
480
481 #define _SEH3_LEAVE goto _SEH3$_l_AfterTry
482
483 #define _SEH3_VOLATILE volatile
484
485
486 #ifdef __cplusplus
487 }; // extern "C"
488 #endif