1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 // ===========================================================================
7 // Various macros and constants that are necessary to make the CLR portable.
10 // ===========================================================================
12 #ifndef __PALCLR_WIN_H__
13 #define __PALCLR_WIN_H__
16 // Macros for portable exception handling. The Win32 SEH is emulated using
17 // these macros and setjmp/longjmp on Unix
21 // - The filter has to be a function taking two parameters:
22 // LONG MyFilter(PEXCEPTION_POINTERS *pExceptionInfo, PVOID pv)
24 // - It is not possible to directly use the local variables in the filter.
25 // All the local information that the filter has to need to know about should
26 // be passed through pv parameter
28 // - Do not use goto to jump out of the PAL_TRY block
29 // (jumping out of the try block is not a good idea even on Win32, because of
30 // it causes stack unwind)
37 // } WIN_PAL_FINALLY {
45 // } WIN_PAL_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
51 // LONG MyFilter(PEXCEPTION_POINTERS *pExceptionInfo, PVOID pv)
57 // } WIN_PAL_EXCEPT_FILTER(MyFilter, NULL) {
73 // if (error) goto Done;
76 // } WIN_PAL_EXCEPT_FILTER(OtherFilter, ¶ms) {
88 #if defined(_DEBUG_IMPL) && !defined(JIT_BUILD) && !defined(JIT64_BUILD) && !defined(_ARM_) // @ARMTODO
89 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN \
90 BOOL ___oldOkayToThrowValue = FALSE; \
91 BOOL ___oldSOTolerantState = FALSE; \
92 ClrDebugState *___pState = GetClrDebugState(); \
95 ___oldOkayToThrowValue = ___pState->IsOkToThrow(); \
96 ___oldSOTolerantState = ___pState->IsSOTolerant(); \
97 ___pState->SetOkToThrow(TRUE); \
100 // Special version that avoids touching the debug state after doing work in a DllMain for process or thread detach.
101 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN_DLLMAIN(_reason) \
102 BOOL ___oldOkayToThrowValue = FALSE; \
103 BOOL ___oldSOTolerantState = FALSE; \
104 ClrDebugState *___pState = CheckClrDebugState(); \
109 ___oldOkayToThrowValue = ___pState->IsOkToThrow(); \
110 ___oldSOTolerantState = ___pState->IsSOTolerant(); \
111 ___pState->SetOkToThrow(TRUE); \
113 if ((_reason == DLL_PROCESS_DETACH) || (_reason == DLL_THREAD_DETACH)) \
117 ANNOTATION_TRY_BEGIN;
119 #define WIN_PAL_TRY_HANDLER_DBG_END \
120 ANNOTATION_TRY_END; \
124 if (___pState != NULL) \
126 _ASSERTE(___pState == CheckClrDebugState()); \
127 ___pState->SetOkToThrow(___oldOkayToThrowValue); \
128 ___pState->SetSOTolerance(___oldSOTolerantState); \
132 #define WIN_PAL_ENDTRY_NAKED_DBG \
135 RESTORE_SO_TOLERANCE_STATE; \
139 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN ANNOTATION_TRY_BEGIN;
140 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN_DLLMAIN(_reason) ANNOTATION_TRY_BEGIN;
141 #define WIN_PAL_TRY_HANDLER_DBG_END ANNOTATION_TRY_END;
142 #define WIN_PAL_ENDTRY_NAKED_DBG
143 #endif // defined(ENABLE_CONTRACTS_IMPL) && !defined(JIT64_BUILD)
145 #endif // __PALCLR_WIN_H__