summaryrefslogtreecommitdiff
path: root/src/vm/exceptionhandling.h
blob: 71bcab749fa5e23c173f9de8ec9f28d38bfcaf6e (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
// 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 __EXCEPTION_HANDLING_h__
#define __EXCEPTION_HANDLING_h__

#ifdef WIN64EXCEPTIONS

// This address lies in the NULL pointer partition of the process memory.
// Accessing it will result in AV.
#define INVALID_RESUME_ADDRESS 0x000000000000bad0

#include "exstatecommon.h"

LONG WINAPI CLRVectoredExceptionHandlerShim(PEXCEPTION_POINTERS pExceptionInfo);

EXTERN_C EXCEPTION_DISPOSITION
ProcessCLRException(IN     PEXCEPTION_RECORD     pExceptionRecord
          WIN64_ARG(IN     ULONG64               MemoryStackFp)
      NOT_WIN64_ARG(IN     ULONG                 MemoryStackFp),
                    IN OUT PT_CONTEXT            pContextRecord,
                    IN OUT PT_DISPATCHER_CONTEXT pDispatcherContext);


#ifndef FEATURE_PAL
void __declspec(noinline)
ClrUnwindEx(EXCEPTION_RECORD* pExceptionRecord,
                 UINT_PTR          ReturnValue,
                 UINT_PTR          TargetIP,
                 UINT_PTR          TargetFrameSp);
#endif // !FEATURE_PAL

typedef DWORD_PTR   (HandlerFn)(UINT_PTR uStackFrame, Object* pExceptionObj);

enum CLRUnwindStatus { UnwindPending, FirstPassComplete, SecondPassComplete };

enum TrackerMemoryType
{
    memManaged = 0x0001,
    memUnmanaged = 0x0002,
    memBoth = 0x0003,
};

// Enum that specifies the type of EH funclet we are about to invoke
enum EHFuncletType
{
    Filter = 0x0001,
    FaultFinally = 0x0002,
    Catch = 0x0004,
};

typedef DPTR(class ExceptionTracker) PTR_ExceptionTracker;
class ExceptionTracker
{
    friend class TrackerAllocator;
    friend class ThreadExceptionState;
    friend class ClrDataExceptionState;
#ifdef DACCESS_COMPILE
    friend class ClrDataAccess;
#endif // DACCESS_COMPILE

    friend void FreeTrackerMemory(ExceptionTracker* pTracker, TrackerMemoryType mem);

private:
    class StackRange;
public:

    ExceptionTracker() :
        m_pThread(NULL),
        m_hThrowable(NULL)
    {
#ifndef DACCESS_COMPILE
        m_StackTraceInfo.Init();
#endif //  DACCESS_COMPILE

#ifndef FEATURE_PAL        
        // Init the WatsonBucketTracker
        m_WatsonBucketTracker.Init();
#endif // !FEATURE_PAL        

#ifdef FEATURE_CORRUPTING_EXCEPTIONS
        // Initialize the default exception severity to NotCorrupting
        m_CorruptionSeverity = NotSet;
#endif // FEATURE_CORRUPTING_EXCEPTIONS

        // By default, mark the tracker as not having delivered the first
        // chance exception notification
        m_fDeliveredFirstChanceNotification = FALSE;

        m_sfFirstPassTopmostFrame.Clear();
        
        m_dwIndexClauseForCatch = 0;
        m_sfEstablisherOfActualHandlerFrame.Clear();
        m_sfCallerOfActualHandlerFrame.Clear();

        m_fFixupCallerSPForGCReporting = false;

        m_fResetEnclosingClauseSPForCatchFunclet = FALSE;

        m_sfCurrentEstablisherFrame.Clear();
        m_sfLastUnwoundEstablisherFrame.Clear();
        m_pInitialExplicitFrame = NULL;
        m_pLimitFrame = NULL;
        m_csfEHClauseOfCollapsedTracker.Clear();

#ifdef FEATURE_PAL
        m_fOwnsExceptionPointers = FALSE;
#endif
    }

    ExceptionTracker(DWORD_PTR             dwExceptionPc,
                     PTR_EXCEPTION_RECORD  pExceptionRecord,
                     PTR_CONTEXT           pContextRecord) :
        m_pPrevNestedInfo((ExceptionTracker*)NULL),
        m_pThread(GetThread()),
        m_hThrowable(NULL),
        m_uCatchToCallPC(NULL),
        m_pSkipToParentFunctionMD(NULL),
// these members were added for resume frame processing
        m_pClauseForCatchToken(NULL),
// end resume frame members
        m_ExceptionCode(pExceptionRecord->ExceptionCode)
    {
        m_ptrs.ExceptionRecord  = pExceptionRecord;
        m_ptrs.ContextRecord    = pContextRecord;

        m_pLimitFrame = NULL;
        
        if (IsInstanceTaggedSEHCode(pExceptionRecord->ExceptionCode) && ::WasThrownByUs(pExceptionRecord, pExceptionRecord->ExceptionCode))
        {
            m_ExceptionFlags.SetWasThrownByUs();
        }

        m_StackTraceInfo.Init();

#ifndef FEATURE_PAL        
        // Init the WatsonBucketTracker
        m_WatsonBucketTracker.Init();
#endif // !FEATURE_PAL        

#ifdef FEATURE_CORRUPTING_EXCEPTIONS
        // Initialize the default exception severity to NotCorrupting
        m_CorruptionSeverity = NotSet;
#endif // FEATURE_CORRUPTING_EXCEPTIONS

        // By default, mark the tracker as not having delivered the first
        // chance exception notification
        m_fDeliveredFirstChanceNotification = FALSE;

        m_dwIndexClauseForCatch = 0;
        m_sfEstablisherOfActualHandlerFrame.Clear();
        m_sfCallerOfActualHandlerFrame.Clear();
        
        m_sfFirstPassTopmostFrame.Clear();
        
        m_fFixupCallerSPForGCReporting = false;

        m_fResetEnclosingClauseSPForCatchFunclet = FALSE;

        m_sfCurrentEstablisherFrame.Clear();
        m_sfLastUnwoundEstablisherFrame.Clear();
        m_pInitialExplicitFrame = NULL;
        m_csfEHClauseOfCollapsedTracker.Clear();

#ifdef FEATURE_PAL
        m_fOwnsExceptionPointers = FALSE;
#endif
    }

    ~ExceptionTracker()
    {
        ReleaseResources();
    }

    enum StackTraceState
    {
        STS_Append,
        STS_FirstRethrowFrame,
        STS_NewException,
    };

    static void InitializeCrawlFrame(CrawlFrame* pcfThisFrame, Thread* pThread, StackFrame sf, REGDISPLAY* pRD, 
                                     PT_DISPATCHER_CONTEXT pDispatcherContext, DWORD_PTR ControlPCForEHSearch, 
                                     UINT_PTR* puMethodStartPC,
                                     ExceptionTracker *pCurrentTracker);
    
    void InitializeCurrentContextForCrawlFrame(CrawlFrame* pcfThisFrame, PT_DISPATCHER_CONTEXT pDispatcherContext, StackFrame sfEstablisherFrame);

    static void InitializeCrawlFrameForExplicitFrame(CrawlFrame* pcfThisFrame, Frame* pFrame, MethodDesc *pMD);

#ifndef DACCESS_COMPILE
    static void ResetThreadAbortStatus(PTR_Thread pThread, CrawlFrame *pCf, StackFrame sfCurrentStackFrame);
#endif // !DACCESS_COMPILE

    CLRUnwindStatus ProcessOSExceptionNotification(
        PEXCEPTION_RECORD pExceptionRecord,
        PT_CONTEXT pContextRecord,
        PT_DISPATCHER_CONTEXT pDispatcherContext,
        DWORD dwExceptionFlags,
        StackFrame sf,
        Thread* pThread,
        StackTraceState STState ARM_ARG(PVOID pICFSetAsLimitFrame));

    CLRUnwindStatus ProcessExplicitFrame(
        CrawlFrame* pcfThisFrame,
        StackFrame sf,
        BOOL fIsFirstPass,
        StackTraceState& STState
        );

    CLRUnwindStatus ProcessManagedCallFrame(
        CrawlFrame* pcfThisFrame,
        StackFrame sf,
        StackFrame sfEstablisherFrame,
        EXCEPTION_RECORD* pExceptionRecord,
        StackTraceState STState,
        UINT_PTR uMethodStartPC,
        DWORD dwExceptionFlags,
        DWORD dwTACatchHandlerClauseIndex,
        StackFrame sfEstablisherOfActualHandlerFrame
        );

    bool UpdateScannedStackRange(StackFrame sf, bool fIsFirstPass);

    void FirstPassIsComplete();
    void SecondPassIsComplete(MethodDesc* pMD, StackFrame sfResumeStackFrame);

    CLRUnwindStatus HandleFunclets(bool* pfProcessThisFrame, bool fIsFirstPass, 
        MethodDesc * pMD, bool fFunclet, StackFrame sf);

    static OBJECTREF CreateThrowable(
        PEXCEPTION_RECORD pExceptionRecord,
        BOOL bAsynchronousThreadStop
        );

    DWORD                   GetExceptionCode()      { return m_ExceptionCode;       }
    INDEBUG(inline  bool    IsValid());
    INDEBUG(static UINT_PTR DebugComputeNestingLevel());

    inline OBJECTREF GetThrowable()
    {
        CONTRACTL
        {
            MODE_COOPERATIVE;
            NOTHROW;
            GC_NOTRIGGER;
        }
        CONTRACTL_END;

        if (NULL != m_hThrowable)
        {
            return ObjectFromHandle(m_hThrowable);
        }

        return NULL;
    }

    // Return a StackFrame of the current frame for parent frame checking purposes.
    // Don't use this StackFrame in any way except to pass it back to the ExceptionTracker
    // via IsUnwoundToTargetParentFrame().
    static StackFrame GetStackFrameForParentCheck(CrawlFrame * pCF);

    static bool IsInStackRegionUnwoundBySpecifiedException(CrawlFrame * pCF, PTR_ExceptionTracker pExceptionTracker);
    static bool IsInStackRegionUnwoundByCurrentException(CrawlFrame * pCF);

    static bool HasFrameBeenUnwoundByAnyActiveException(CrawlFrame * pCF);
    void SetCurrentEstablisherFrame(StackFrame sfEstablisher)
    {
        LIMITED_METHOD_CONTRACT;

        m_sfCurrentEstablisherFrame = sfEstablisher;
    }

    StackFrame GetCurrentEstablisherFrame()
    {
        LIMITED_METHOD_CONTRACT;

        return m_sfCurrentEstablisherFrame;
    }

    void SetLastUnwoundEstablisherFrame(StackFrame sfEstablisher)
    {
        LIMITED_METHOD_CONTRACT;

        m_sfLastUnwoundEstablisherFrame = sfEstablisher;
    }

    StackFrame GetLastUnwoundEstablisherFrame()
    {
        LIMITED_METHOD_CONTRACT;

        return m_sfLastUnwoundEstablisherFrame;
    }

    PTR_Frame GetInitialExplicitFrame()
    {
        LIMITED_METHOD_CONTRACT;

        return m_pInitialExplicitFrame;
    }

#ifdef FEATURE_PAL
    // Reset the range of explicit frames, the limit frame and the scanned
    // stack range before unwinding a sequence of native frames. These frames
    // will be in the unwound part of the stack.
    void CleanupBeforeNativeFramesUnwind()
    {
        m_pInitialExplicitFrame = NULL;
        m_pLimitFrame = NULL;
        m_ScannedStackRange.Reset();
    }
#endif // FEATURE_PAL

    // Determines if we have unwound to the specified parent method frame.
    // Currently this is only used for funclet skipping.
    static bool IsUnwoundToTargetParentFrame(CrawlFrame * pCF, StackFrame sfParent);
    static bool IsUnwoundToTargetParentFrame(StackFrame sfToCheck, StackFrame sfParent);

    // Given the CrawlFrame for a funclet frame, return the frame pointer of the enclosing funclet frame.
    // For filter funclet frames and a normal method frames, this function returns a NULL StackFrame.
    //
    // <WARNING>
    // It is not valid to call this function on an arbitrary funclet.  You have to be doing a full stackwalk from
    // the leaf frame and skipping method frames as indicated by the return value of this function.  This function 
    // relies on the ExceptionTrackers, which are collapsed in the second pass when a nested exception escapes.  
    // When this happens, we'll lose information on the funclet represented by the collapsed tracker.
    // </WARNING>
    //
    // Return Value:
    // StackFrame.IsNull()   - no skipping is necessary
    // StackFrame.IsMaxVal() - skip one frame and then ask again
    // Anything else         - skip to the method frame indicated by the return value and ask again
    static StackFrame FindParentStackFrameForStackWalk(CrawlFrame* pCF, bool fForGCReporting = false);

    // Given the CrawlFrame for a filter funclet frame, return the frame pointer of the parent method frame.
    // It also returns the relative offset and the caller SP of the parent method frame.
    //
    // <WARNING>
    // The same warning for FindParentStackFrameForStackWalk() also applies here.  Moreoever, although
    // this function seems to be more convenient, it may potentially trigger a full stackwalk!  Do not
    // call this unless you know absolutely what you are doing.  In most cases FindParentStackFrameForStackWalk()
    // is what you need.
    // </WARNING>
    //
    // Return Value:
    // StackFrame.IsNull()   - no skipping is necessary
    // Anything else         - the StackFrame of the parent method frame
    static StackFrame FindParentStackFrameEx(CrawlFrame* pCF, 
                                             DWORD*      pParentOffset,
                                             UINT_PTR*   pParentCallerSP);

    static void
        PopTrackers(StackFrame sfResumeFrame,
                    bool fPopWhenEqual);

    static void
        PopTrackers(void* pvStackPointer);

    static void
        PopTrackerIfEscaping(void* pvStackPointer);

    static ExceptionTracker*
        GetOrCreateTracker(UINT_PTR ControlPc,
                           StackFrame sf,
                           EXCEPTION_RECORD* pExceptionRecord,
                           T_CONTEXT* pContextRecord,
                           BOOL bAsynchronousThreadStop,
                           bool fIsFirstPass,
                           StackTraceState* pSTState);

    static void
        ResumeExecution(T_CONTEXT*            pContextRecord,
                        EXCEPTION_RECORD*   pExceptionRecord
                        );

    void ResetLimitFrame();

#ifdef DACCESS_COMPILE
    void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
#endif // DACCESS_COMPILE

    static void DebugLogTrackerRanges(__in_z const char *pszTag);

    bool IsStackOverflowException();

#if defined(FEATURE_PAL) && !defined(CROSS_COMPILE)
    void TakeExceptionPointersOwnership(PAL_SEHException* ex)
    {
        _ASSERTE(ex->GetExceptionRecord() == m_ptrs.ExceptionRecord);
        _ASSERTE(ex->GetContextRecord() == m_ptrs.ContextRecord);
        ex->Clear();
        m_fOwnsExceptionPointers = TRUE;
    }
#endif // FEATURE_PAL && !CROSS_COMPILE

private:
    DWORD_PTR
        CallHandler(UINT_PTR                dwHandlerStartPC,
                    StackFrame              sf,
                    EE_ILEXCEPTION_CLAUSE*  pEHClause,
                    MethodDesc*             pMD,
                    EHFuncletType funcletType
                    ARM_ARG(PT_CONTEXT pContextRecord)
                    ARM64_ARG(PT_CONTEXT pContextRecord)
                    );

    inline static BOOL
        ClauseCoversPC(EE_ILEXCEPTION_CLAUSE* pEHClause,
                       DWORD dwOffset);

    static bool 
        IsFilterStartOffset(EE_ILEXCEPTION_CLAUSE* pEHClause, DWORD_PTR dwHandlerStartPC);

#ifndef DACCESS_COMPILE
    void DestroyExceptionHandle()
    {
        // Never, ever destroy a preallocated exception handle.
        if ((m_hThrowable != NULL) && !CLRException::IsPreallocatedExceptionHandle(m_hThrowable))
        {
            DestroyHandle(m_hThrowable);
        }

        m_hThrowable = NULL;
    }
#endif // !DACCESS_COMPILE

    void SaveStackTrace();

    inline BOOL CanAllocateMemory()
    {
        CONTRACTL
        {
            MODE_COOPERATIVE;
            NOTHROW;
            GC_NOTRIGGER;
        }
        CONTRACTL_END;

        OBJECTREF oThrowable = GetThrowable();

        return !(oThrowable == CLRException::GetPreallocatedOutOfMemoryException()) &&
               !(oThrowable == CLRException::GetPreallocatedStackOverflowException());
    }

    INDEBUG(inline  BOOL        ThrowableIsValid());

    bool HandleNestedExceptionEscape(StackFrame sf, bool fIsFirstPass);

#if defined(DEBUGGING_SUPPORTED)
    BOOL NotifyDebuggerOfStub(Thread* pThread, StackFrame sf, Frame* pCurrentFrame);

    void
        MakeCallbacksRelatedToHandler(bool fBeforeCallingHandler,
                                      Thread* pThread,
                                      MethodDesc* pMD,
                                      EE_ILEXCEPTION_CLAUSE* pEHClause,
                                      DWORD_PTR dwHandlerStartPC,
                                      StackFrame sf);
#else  // !DEBUGGING_SUPPORTED
    void
        MakeCallbacksRelatedToHandler(bool fBeforeCallingHandler,
                                      Thread* pThread,
                                      MethodDesc* pMD,
                                      EE_ILEXCEPTION_CLAUSE* pEHClause,
                                      DWORD_PTR dwHandlerStartPC,
                                      StackFrame sf) {return;}
#endif // !DEBUGGING_SUPPORTED

    // private helpers
    static StackFrame GetCallerSPOfParentOfNonExceptionallyInvokedFunclet(CrawlFrame *pCF);
    
    static StackFrame FindParentStackFrameHelper(CrawlFrame* pCF,
                                                 bool*       pfRealParent,
                                                 DWORD*      pParentOffset, 
                                                 UINT_PTR*   pParentCallerSP,
                                                 bool        fForGCReporting = false);

    static StackFrame RareFindParentStackFrame(CrawlFrame* pCF,
                                               DWORD*      pParentOffset, 
                                               UINT_PTR*   pParentCallerSP);

    static StackWalkAction RareFindParentStackFrameCallback(CrawlFrame* pCF, LPVOID pData); 

    struct DAC_EXCEPTION_POINTERS
    {
        PTR_EXCEPTION_RECORD    ExceptionRecord;
        PTR_CONTEXT             ContextRecord;
    };

public:

    static UINT_PTR FinishSecondPass(Thread* pThread, UINT_PTR uResumePC, StackFrame sf,
                                     T_CONTEXT* pContextRecord, ExceptionTracker *pTracker, bool* pfAborting = NULL);
    UINT_PTR CallCatchHandler(T_CONTEXT* pContextRecord, bool* pfAborting = NULL);

    static bool FindNonvolatileRegisterPointers(Thread* pThread, UINT_PTR uOriginalSP, REGDISPLAY* pRegDisplay, TADDR uResumeFrameFP);
    static void UpdateNonvolatileRegisters(T_CONTEXT* pContextRecord, REGDISPLAY *pRegDisplay, bool fAborting);

    PTR_Frame GetLimitFrame()
    {
        LIMITED_METHOD_CONTRACT;
        return m_pLimitFrame;
    }

    StackRange GetScannedStackRange()
    {
        LIMITED_METHOD_CONTRACT;

        return m_ScannedStackRange;
    }

    UINT_PTR GetCatchToCallPC()
    {
        LIMITED_METHOD_CONTRACT;

        return m_uCatchToCallPC;
    }

    EE_ILEXCEPTION_CLAUSE GetEHClauseForCatch()
    {
        return m_ClauseForCatch;
    }

    // Returns the topmost frame seen during the first pass.
    StackFrame GetTopmostStackFrameFromFirstPass()
    {
        LIMITED_METHOD_CONTRACT;

        return m_sfFirstPassTopmostFrame;
    }

#ifdef _DEBUG
    StackFrame GetResumeStackFrame()
    {
        LIMITED_METHOD_CONTRACT;

        return m_sfResumeStackFrame;
    }

    PTR_EXCEPTION_CLAUSE_TOKEN GetCatchHandlerExceptionClauseToken()
    {
        LIMITED_METHOD_CONTRACT;

        return m_pClauseForCatchToken;
    }
#endif // _DEBUG

    DWORD GetCatchHandlerExceptionClauseIndex()
    {
        LIMITED_METHOD_CONTRACT;

        return m_dwIndexClauseForCatch;
    }

    StackFrame GetEstablisherOfActualHandlingFrame()
    {
        LIMITED_METHOD_CONTRACT;
        
        return m_sfEstablisherOfActualHandlerFrame;
    }

    StackFrame GetCallerOfActualHandlingFrame()
    {
        LIMITED_METHOD_CONTRACT;
    
        return m_sfCallerOfActualHandlerFrame;
    }

    StackFrame GetCallerOfEnclosingClause()
    {
        LIMITED_METHOD_CONTRACT;

        return m_EnclosingClauseInfoForGCReporting.GetEnclosingClauseCallerSP();
    }

    StackFrame GetCallerOfCollapsedEnclosingClause()
    {
        LIMITED_METHOD_CONTRACT;

        return m_EnclosingClauseInfoOfCollapsedTracker.GetEnclosingClauseCallerSP();
    }

#ifndef FEATURE_PAL
private:
    EHWatsonBucketTracker m_WatsonBucketTracker;
public:
    inline PTR_EHWatsonBucketTracker GetWatsonBucketTracker()
    {
        LIMITED_METHOD_CONTRACT;
        return PTR_EHWatsonBucketTracker(PTR_HOST_MEMBER_TADDR(ExceptionTracker, this, m_WatsonBucketTracker));
    }
#endif // !FEATURE_PAL        

#ifdef FEATURE_CORRUPTING_EXCEPTIONS
private:
    CorruptionSeverity      m_CorruptionSeverity;
public:
    inline CorruptionSeverity GetCorruptionSeverity()
    {
        LIMITED_METHOD_CONTRACT;

        return (CorruptionSeverity)GET_CORRUPTION_SEVERITY(m_CorruptionSeverity);
    }

    inline void SetCorruptionSeverity(CorruptionSeverity severityToSet)
    {
        LIMITED_METHOD_CONTRACT;

        m_CorruptionSeverity = severityToSet;
    }
#endif // FEATURE_CORRUPTING_EXCEPTIONS

private:
    BOOL                    m_fDeliveredFirstChanceNotification;

public:
    inline BOOL DeliveredFirstChanceNotification()
    {
        LIMITED_METHOD_CONTRACT;

        return m_fDeliveredFirstChanceNotification;
    }

    inline void SetFirstChanceNotificationStatus(BOOL fDelivered)
    {
        LIMITED_METHOD_CONTRACT;
    
        m_fDeliveredFirstChanceNotification = fDelivered;
    }

    // Returns the exception tracker previous to the current
    inline PTR_ExceptionTracker GetPreviousExceptionTracker()
    {
        LIMITED_METHOD_CONTRACT;

        return m_pPrevNestedInfo;
    }

    // Returns the throwble associated with the tracker as handle
    inline OBJECTHANDLE GetThrowableAsHandle()
    {
        LIMITED_METHOD_CONTRACT;

        return m_hThrowable;
    }

    bool IsInFirstPass()
    {
        return !m_ExceptionFlags.UnwindHasStarted();
    }
    
private: ;

    void ReleaseResources();

    void SetEnclosingClauseInfo(bool     fEnclosingClauseIsFunclet,
                                DWORD    dwEnclosingClauseOffset,
                                UINT_PTR uEnclosingClauseCallerSP);

    class StackRange
    {
    public:
        StackRange();
        void Reset();
        bool IsEmpty();
        bool IsSupersededBy(StackFrame sf);
        void CombineWith(StackFrame sfCurrent, StackRange* pPreviousRange);
        bool Contains(StackFrame sf);
        void ExtendUpperBound(StackFrame sf);
        void ExtendLowerBound(StackFrame sf);
        void TrimLowerBound(StackFrame sf);
        StackFrame GetLowerBound();
        StackFrame GetUpperBound();
        INDEBUG(bool IsDisjointWithAndLowerThan(StackRange* pOtherRange));
    private:
        INDEBUG(bool IsConsistent());

    private:
        // <TODO> can we use a smaller encoding? </TODO>
        StackFrame          m_sfLowBound;
        StackFrame          m_sfHighBound;
    };

    struct EnclosingClauseInfo
    {
    public:
        EnclosingClauseInfo();
        EnclosingClauseInfo(bool fEnclosingClauseIsFunclet, DWORD dwEnclosingClauseOffset, UINT_PTR uEnclosingClauseCallerSP);

        bool     EnclosingClauseIsFunclet();
        DWORD    GetEnclosingClauseOffset();
        UINT_PTR GetEnclosingClauseCallerSP();
        void     SetEnclosingClauseCallerSP(UINT_PTR callerSP);

        bool operator==(const EnclosingClauseInfo & rhs);

    private:
        UINT_PTR m_uEnclosingClauseCallerSP;
        DWORD    m_dwEnclosingClauseOffset;
        bool     m_fEnclosingClauseIsFunclet;
    };

    PTR_ExceptionTracker    m_pPrevNestedInfo;
    Thread*                 m_pThread;          // this is used as an IsValid/IsFree field -- if it's NULL, the allocator can
                                                // reuse its memory, if it's non-NULL, it better be a valid thread pointer

    StackRange              m_ScannedStackRange;
    DAC_EXCEPTION_POINTERS  m_ptrs;
#ifdef FEATURE_PAL
    BOOL                    m_fOwnsExceptionPointers;
#endif
    OBJECTHANDLE            m_hThrowable;
    StackTraceInfo          m_StackTraceInfo;
    UINT_PTR                m_uCatchToCallPC;
    BOOL           m_fResetEnclosingClauseSPForCatchFunclet;
    
    union
    {
        MethodDesc*         m_pSkipToParentFunctionMD;      // SKIPTOPARENT
        MethodDesc*         m_pMethodDescOfCatcher;
    };

    StackFrame              m_sfResumeStackFrame;           // RESUMEFRAME
    StackFrame              m_sfFirstPassTopmostFrame;      // Topmost frame seen during first pass
    PTR_EXCEPTION_CLAUSE_TOKEN m_pClauseForCatchToken;              // RESUMEFRAME
    EE_ILEXCEPTION_CLAUSE   m_ClauseForCatch;
    // Index of EH clause that will catch the exception
    DWORD                   m_dwIndexClauseForCatch;
    
    // Establisher frame of the managed frame that contains
    // the handler for the exception (corresponding
    // to the EH index we save off in m_dwIndexClauseForCatch)
    StackFrame              m_sfEstablisherOfActualHandlerFrame;
    StackFrame              m_sfCallerOfActualHandlerFrame;

    ExceptionFlags          m_ExceptionFlags;
    DWORD                   m_ExceptionCode;

    PTR_Frame               m_pLimitFrame;

#ifdef DEBUGGING_SUPPORTED
    //
    // DEBUGGER STATE
    //
    DebuggerExState         m_DebuggerExState;
#endif // DEBUGGING_SUPPORTED

    //
    // Information for the funclet we are calling
    //
    EHClauseInfo            m_EHClauseInfo;

    // This flag indicates whether the SP we pass to a funclet is for an enclosing funclet.
    EnclosingClauseInfo     m_EnclosingClauseInfo;
    
    // This stores the actual callerSP of the frame that is about to execute the funclet.
    // It differs from "m_EnclosingClauseInfo" where upon detecting a nested exception,
    // the latter can contain the callerSP of the original funclet instead of that of the
    // current frame.
    EnclosingClauseInfo     m_EnclosingClauseInfoForGCReporting;
    bool                    m_fFixupCallerSPForGCReporting;

    StackFrame              m_sfCurrentEstablisherFrame;
    StackFrame              m_sfLastUnwoundEstablisherFrame;
    PTR_Frame               m_pInitialExplicitFrame;
    CallerStackFrame        m_csfEHClauseOfCollapsedTracker;
    EnclosingClauseInfo     m_EnclosingClauseInfoOfCollapsedTracker;
};

#if defined(WIN64EXCEPTIONS)
PTR_ExceptionTracker GetEHTrackerForPreallocatedException(OBJECTREF oPreAllocThrowable, PTR_ExceptionTracker pStartingEHTracker);
#endif // WIN64EXCEPTIONS

class TrackerAllocator
{
public:
    void                Init();
    void                Terminate();
    ExceptionTracker*   GetTrackerMemory();
    void                FreeTrackerMemory(ExceptionTracker* pTracker);

private:

    struct Page;

    struct PageHeader
    {
        Page*               m_pNext;
        LONG                m_idxFirstFree;
    };

    enum
    {
        //
        // Due to the unexpected growth of the ExceptionTracker struct, 
        // OS_PAGE_SIZE does not seem appropriate anymore on x64, and
        // we should behave the same on x64 as on ia64 regardless of
        // the difference between the page sizes on the platforms.
        //
        TRACKER_ALLOCATOR_PAGE_SIZE         = 8*1024,
        TRACKER_ALLOCATOR_MAX_OOM_SPINS     = 20,
        TRACKER_ALLOCATOR_OOM_SPIN_DELAY    = 100,
        NUM_TRACKERS_PER_PAGE               = ((TRACKER_ALLOCATOR_PAGE_SIZE - sizeof(PageHeader)) / sizeof(ExceptionTracker)),
    };

    struct Page
    {
        PageHeader          m_header;
        ExceptionTracker    m_rgTrackers[NUM_TRACKERS_PER_PAGE];
    };

    static_assert_no_msg(sizeof(Page) <= TRACKER_ALLOCATOR_PAGE_SIZE);

    Page* m_pFirstPage;
    Crst* m_pCrst;
};

#endif // WIN64EXCEPTIONS

#endif  // __EXCEPTION_HANDLING_h__