summaryrefslogtreecommitdiff
path: root/src/vm/exstate.h
blob: 3ecad13267e263da3aa7d7b015662500aacdfb74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

//

#ifndef __ExState_h__
#define __ExState_h__ 

class ExceptionFlags;
class DebuggerExState;
class EHClauseInfo;

#include "exceptionhandling.h"

#if !defined(WIN64EXCEPTIONS)
// ExInfo contains definitions for 32bit
#include "exinfo.h"
#endif // !defined(WIN64EXCEPTIONS)

#if !defined(DACCESS_COMPILE)
#define PRESERVE_WATSON_ACROSS_CONTEXTS 1
#endif

extern StackWalkAction COMPlusUnwindCallback(CrawlFrame *pCf, ThrowCallbackType *pData);

//
// This class serves as a forwarding and abstraction layer for the EH subsystem.
// Since we have two different implementations, this class is needed to unify 
// the EE's view of EH.  Ideally, this is just a step along the way to a unified
// EH subsystem.
//
typedef DPTR(class ThreadExceptionState) PTR_ThreadExceptionState;
class ThreadExceptionState
{
    friend class ClrDataExceptionState;
    friend class CheckAsmOffsets;
    friend class StackFrameIterator;

#ifdef DACCESS_COMPILE
    friend class ClrDataAccess;
#endif // DACCESS_COMPILE

    // ProfToEEInterfaceImpl::GetNotifiedExceptionClauseInfo needs access so that it can fetch the
    // ExceptionTracker or the ExInfo as appropriate for the platform
    friend class ProfToEEInterfaceImpl;

#ifdef WIN64EXCEPTIONS
    friend class ExceptionTracker;
#else
    friend class ExInfo;
#endif // WIN64EXCEPTIONS

public:
    
    void FreeAllStackTraces();
    void ClearThrowablesForUnload(IGCHandleStore* handleStore);

#ifdef _DEBUG
    typedef enum 
    {
        STEC_All,
        STEC_CurrentTrackerEqualNullOkHackForFatalStackOverflow,
#ifdef FEATURE_INTERPRETER
        STEC_CurrentTrackerEqualNullOkForInterpreter,
#endif // FEATURE_INTERPRETER
    } SetThrowableErrorChecking;
#endif

    void                SetThrowable(OBJECTREF throwable DEBUG_ARG(SetThrowableErrorChecking stecFlags = STEC_All));
    OBJECTREF           GetThrowable();
    OBJECTHANDLE        GetThrowableAsHandle();
    DWORD               GetExceptionCode();
    BOOL                IsComPlusException();
    EXCEPTION_POINTERS* GetExceptionPointers();
    PTR_EXCEPTION_RECORD GetExceptionRecord();
    PTR_CONTEXT          GetContextRecord();
    BOOL                IsExceptionInProgress();
    void                GetLeafFrameInfo(StackTraceElement* pStackTrace);

    ExceptionFlags*     GetFlags();

    ThreadExceptionState();
    ~ThreadExceptionState();

#if !defined(WIN64EXCEPTIONS)
      void              SetExceptionPointers(EXCEPTION_POINTERS *pExceptionPointers);
#endif


#ifdef DEBUGGING_SUPPORTED    
    // DebuggerExState stores information necessary for intercepting an exception
    DebuggerExState*    GetDebuggerState();

    // check to see if the current exception is interceptable
    BOOL                IsDebuggerInterceptable();
#endif // DEBUGGING_SUPPORTED

    EHClauseInfo*       GetCurrentEHClauseInfo();

#ifdef DACCESS_COMPILE
    void EnumChainMemoryRegions(CLRDataEnumMemoryFlags flags);
#endif // DACCESS_COMPILE
    
    // After unwinding from an SO, there may be stale exception state.
    void ClearExceptionStateAfterSO(void* pStackFrameSP);

    enum ThreadExceptionFlag
    {
        TEF_None                          = 0x00000000,

        // Right now this flag is only used on WIN64.  We set this flag near the end of the second pass when we pop 
        // the ExceptionTracker for the current exception but before we actually resume execution.  It is unsafe
        // to start a funclet-skipping stackwalk in this time window.
        TEF_InconsistentExceptionState    = 0x00000001,

        TEF_ForeignExceptionRaise         = 0x00000002,
    };

    void SetThreadExceptionFlag(ThreadExceptionFlag flag);
    void ResetThreadExceptionFlag(ThreadExceptionFlag flag);
    BOOL HasThreadExceptionFlag(ThreadExceptionFlag flag);

    inline void SetRaisingForeignException()
    {
        LIMITED_METHOD_CONTRACT;
        SetThreadExceptionFlag(TEF_ForeignExceptionRaise);
    }

    inline BOOL IsRaisingForeignException()
    {
        LIMITED_METHOD_CONTRACT;
        return HasThreadExceptionFlag(TEF_ForeignExceptionRaise);
    }

    inline void ResetRaisingForeignException()
    {
        LIMITED_METHOD_CONTRACT;
        ResetThreadExceptionFlag(TEF_ForeignExceptionRaise);
    }

#if defined(_DEBUG)
    void AssertStackTraceInfo(StackTraceInfo *pSTI);
#endif // _debug

private:
    Thread* GetMyThread();

#ifdef WIN64EXCEPTIONS
    PTR_ExceptionTracker    m_pCurrentTracker;
    ExceptionTracker        m_OOMTracker;
public:
    PTR_ExceptionTracker    GetCurrentExceptionTracker()
    {
        LIMITED_METHOD_CONTRACT;
        return m_pCurrentTracker;
    }
#else
    ExInfo                  m_currentExInfo;
public:
    PTR_ExInfo                 GetCurrentExceptionTracker()
    {
        LIMITED_METHOD_CONTRACT;
        return PTR_ExInfo(PTR_HOST_MEMBER_TADDR(ThreadExceptionState, this, m_currentExInfo));
    }
#endif

#ifdef FEATURE_CORRUPTING_EXCEPTIONS
private:
    CorruptionSeverity      m_LastActiveExceptionCorruptionSeverity;
    BOOL                    m_fCanReflectionTargetHandleException;

public:
    // Returns the corruption severity of the last active exception
    inline CorruptionSeverity GetLastActiveExceptionCorruptionSeverity()
    {
        LIMITED_METHOD_CONTRACT;

        return (CorruptionSeverity)GET_CORRUPTION_SEVERITY(m_LastActiveExceptionCorruptionSeverity);
    }

    // Set the corruption severity of the last active exception
    inline void SetLastActiveExceptionCorruptionSeverity(CorruptionSeverity severityToSet)
    {
        LIMITED_METHOD_CONTRACT;

        m_LastActiveExceptionCorruptionSeverity = severityToSet;
    }

    // Returns a bool indicating if the last active exception's corruption severity should 
    // be used when exception is reraised (e.g. Reflection Invocation, AD transition, etc)
    inline BOOL ShouldLastActiveExceptionCorruptionSeverityBeReused()
    {
        LIMITED_METHOD_CONTRACT;

        return CAN_REUSE_CORRUPTION_SEVERITY(m_LastActiveExceptionCorruptionSeverity);
    }

    // Returns a BOOL to indicate if reflection target can handle CSE or not.
    // This is used in DispatchInfo::CanIDispatchTargetHandleException.
    inline BOOL CanReflectionTargetHandleException()
    {
        LIMITED_METHOD_CONTRACT;

        return m_fCanReflectionTargetHandleException;
    }

    // Sets a BOOL indicate if the Reflection invocation target can handle exception or not.
    // Used in ReflectionInvocation.cpp.
    inline void SetCanReflectionTargetHandleException(BOOL fCanReflectionTargetHandleException)
    {
        LIMITED_METHOD_CONTRACT;

        m_fCanReflectionTargetHandleException = fCanReflectionTargetHandleException;
    }
#endif // FEATURE_CORRUPTING_EXCEPTIONS

private:   
    ThreadExceptionFlag      m_flag;

#ifndef FEATURE_PAL        
private:
    EHWatsonBucketTracker    m_UEWatsonBucketTracker;
public:
    PTR_EHWatsonBucketTracker GetUEWatsonBucketTracker()
    {
        LIMITED_METHOD_CONTRACT;
        return PTR_EHWatsonBucketTracker(PTR_HOST_MEMBER_TADDR(ThreadExceptionState, this, m_UEWatsonBucketTracker));
    }
#endif // !FEATURE_PAL        

private:

#ifndef WIN64EXCEPTIONS
    
    //
    // @NICE: Ideally, these friends shouldn't all be enumerated like this.  If they were all part of the same
    // class, that would be nice.  I'm trying to avoid adding x86-specific accessors to this class as well as
    // trying to limit the visibility of the ExInfo struct since Win64 doesn't use ExInfo.
    //
    friend EXCEPTION_DISPOSITION COMPlusAfterUnwind(
            EXCEPTION_RECORD *pExceptionRecord,
            EXCEPTION_REGISTRATION_RECORD *pEstablisherFrame,
            ThrowCallbackType& tct);
    friend EXCEPTION_DISPOSITION COMPlusAfterUnwind(
            EXCEPTION_RECORD *pExceptionRecord,
            EXCEPTION_REGISTRATION_RECORD *pEstablisherFrame,
            ThrowCallbackType& tct,
            Frame *pStartFrame);

    friend EXCEPTION_HANDLER_IMPL(COMPlusFrameHandler);

    friend EXCEPTION_DISPOSITION __cdecl
    CPFH_RealFirstPassHandler(EXCEPTION_RECORD *pExceptionRecord,
                              EXCEPTION_REGISTRATION_RECORD *pEstablisherFrame,
                              CONTEXT *pContext,
                              void *pDispatcherContext,
                              BOOL bAsynchronousThreadStop,
                              BOOL fPGCDisabledOnEntry);

    friend EXCEPTION_DISPOSITION __cdecl
    CPFH_UnwindHandler(EXCEPTION_RECORD *pExceptionRecord,
                       EXCEPTION_REGISTRATION_RECORD *pEstablisherFrame,
                       CONTEXT *pContext,
                       void *pDispatcherContext);
    
    friend void CPFH_UnwindFrames1(Thread* pThread,
                                   EXCEPTION_REGISTRATION_RECORD* pEstablisherFrame,
                                   DWORD exceptionCode);

#ifdef _TARGET_X86_
    friend LPVOID COMPlusEndCatchWorker(Thread * pThread);
#endif

    friend StackWalkAction COMPlusThrowCallback(CrawlFrame *pCf, ThrowCallbackType *pData);

    friend StackWalkAction COMPlusUnwindCallback(CrawlFrame *pCf, ThrowCallbackType *pData);

#if defined(_TARGET_X86_)
    friend void ResumeAtJitEH(CrawlFrame* pCf, BYTE* startPC, EE_ILEXCEPTION_CLAUSE *EHClausePtr, 
                                   DWORD nestingLevel, Thread *pThread, BOOL unwindStack);
#endif // _TARGET_X86_                                   

    friend _EXCEPTION_HANDLER_DECL(COMPlusNestedExceptionHandler);

    friend void COMPlusCooperativeTransitionHandler(Frame* pFrame);

    friend bool ShouldHandleManagedFault(
                        EXCEPTION_RECORD*               pExceptionRecord,
                        CONTEXT*                        pContext,
                        EXCEPTION_REGISTRATION_RECORD*  pEstablisherFrame,
                        Thread*                         pThread);

    friend class Thread;
    // It it the following method that needs to be a friend.  But the prototype pulls in a lot more stuff,
    //  so just make the Thread class a friend.
    // friend StackWalkAction Thread::StackWalkFramesEx(PREGDISPLAY pRD, PSTACKWALKFRAMESCALLBACK pCallback,
    //                 VOID *pData, unsigned flags, Frame *pStartFrame);

#endif // WIN64EXCEPTIONS

};


// <WARNING>
// This holder is not thread safe.
// </WARNING>
class ThreadExceptionFlagHolder
{
public:
    ThreadExceptionFlagHolder(ThreadExceptionState::ThreadExceptionFlag flag);
    ~ThreadExceptionFlagHolder();

private:
    ThreadExceptionState*                       m_pExState;
    ThreadExceptionState::ThreadExceptionFlag   m_flag;
};

extern BOOL IsWatsonEnabled();

#ifndef FEATURE_PAL
// This preprocessor definition is used to capture watson buckets
// at AppDomain transition boundary in END_DOMAIN_TRANSITION macro.
//
// This essentially copies the watson bucket details from the current exception tracker
// to the UE watson bucket tracker only if it is preallocated exception and NOT
// thread abort. For preallocated thread abort, UE Watson bucket tracker would already have the
// bucket details.
//
// It also captures buckets for non-preallocated exceptions (including non-preallocated threadabort) since
// the object would have the IP inside it.
#define CAPTURE_BUCKETS_AT_TRANSITION(pThread, oThrowable)                                                                                  \
        if (IsWatsonEnabled())                                                                                                              \
        {                                                                                                                                   \
            /* Switch to COOP mode */                                                                                                       \
            GCX_COOP();                                                                                                                     \
                                                                                                                                            \
            /* oThrowable is actually GET_THROWABLE macro; so extract the actual throwable for once and for all */                          \
            OBJECTREF throwable = oThrowable;                                                                                               \
            if (CLRException::IsPreallocatedExceptionObject(throwable))                                                                     \
            {                                                                                                                               \
                if (pThread->GetExceptionState()->GetCurrentExceptionTracker() != NULL)                                                     \
                {                                                                                                                           \
                    if (!IsThrowableThreadAbortException(throwable))                                                                        \
                    {                                                                                                                       \
                        PTR_EHWatsonBucketTracker pWatsonBucketTracker = GetWatsonBucketTrackerForPreallocatedException(throwable, FALSE);  \
                        if (pWatsonBucketTracker != NULL)                                                                                   \
                        {                                                                                                                   \
                            pThread->GetExceptionState()->GetUEWatsonBucketTracker()->CopyEHWatsonBucketTracker(*(pWatsonBucketTracker));   \
                            pThread->GetExceptionState()->GetUEWatsonBucketTracker()->CaptureUnhandledInfoForWatson(TypeOfReportedError::UnhandledException, pThread, NULL); \
                            DEBUG_STMT(pThread->GetExceptionState()->GetUEWatsonBucketTracker()->SetCapturedAtADTransition();)              \
                        }                                                                                                                   \
                    }                                                                                                                       \
                }                                                                                                                           \
            }                                                                                                                               \
            else                                                                                                                            \
            {                                                                                                                               \
                SetupWatsonBucketsForNonPreallocatedExceptions(throwable);                                                                  \
            }                                                                                                                               \
        }                                                                   
#else // !FEATURE_PAL
#define CAPTURE_BUCKETS_AT_TRANSITION(pThread, oThrowable)
#endif // FEATURE_PAL

#endif // __ExState_h__