summaryrefslogtreecommitdiff
path: root/src/vm/contexts.cpp
blob: 038c5e5a45f7592659fe110cbacdb34fd17fdc19 (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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// Contexts.CPP
//

// 
// Implementation for class Context
//


#include "common.h"

#ifdef FEATURE_REMOTING

#include "context.h"
#include "excep.h"
#include "field.h"
#include "remoting.h"
#include "perfcounters.h"
#include "specialstatics.h"
#include "appdomain.inl"

#ifdef FEATURE_COMINTEROP
#include "runtimecallablewrapper.h"
#endif // FEATURE_COMINTEROP

#ifndef DACCESS_COMPILE

#define CONTEXT_SIGNATURE   (0x2b585443)        // CTX+
#define CONTEXT_DESTROYED   (0x2d585443)        // CTX-

// Lock for safe operations
CrstStatic Context::s_ContextCrst;


Context::Context(AppDomain *pDomain)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM());
        PRECONDITION(CheckPointer(pDomain));
    }
    CONTRACTL_END;
    
    m_pDomain = pDomain;
    m_Signature = CONTEXT_SIGNATURE;

    // This needs to be a LongWeakHandle since we want to be able
    // to run finalizers on Proxies while the Context itself
    // unreachable. When running the finalizer we will have to
    // transition into the context like a regular remote call.
    // If this is a short weak handle, it ceases being updated
    // as soon as the context is unreachable. By making it a strong
    // handle, it is updated till the context::finalize is run.

    m_ExposedObjectHandle = pDomain->CreateLongWeakHandle(NULL);

    // Set the pointers to the static data storage
    m_pUnsharedStaticData = NULL;
    m_pSharedStaticData = NULL;

    COUNTER_ONLY(GetPerfCounters().m_Context.cContexts++);
}

Context::~Context()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;
    
    BOOL fADUnloaded = m_pDomain->NoAccessToHandleTable();
    if (!fADUnloaded)
    {
        DestroyLongWeakHandle(m_ExposedObjectHandle);
    }

    m_pDomain = NULL;
    m_Signature = CONTEXT_DESTROYED;

    // Cleanup the static data storage
    if(m_pUnsharedStaticData)
    {
        for(WORD i = 0; i < m_pUnsharedStaticData->cElem; i++)
        {
            delete [] (BYTE *) (m_pUnsharedStaticData->dataPtr[i]);
        }
        delete [] m_pUnsharedStaticData;
        m_pUnsharedStaticData = NULL;
    }

    if(m_pSharedStaticData)
    {
        for(WORD i = 0; i < m_pSharedStaticData->cElem; i++)
        {
            delete [] (BYTE *) (m_pSharedStaticData->dataPtr[i]);
        }
        delete [] m_pSharedStaticData;
        m_pSharedStaticData = NULL;
    }

    // Destroy pinning handles associated with this context
    ObjectHandleList::NodeType* pHandleNode; 
    while ((pHandleNode = m_PinnedContextStatics.UnlinkHead() ) != NULL)
    {
        if (!fADUnloaded)
        {
            DestroyPinningHandle(pHandleNode->data);
        }
        delete pHandleNode;
    }

    COUNTER_ONLY(GetPerfCounters().m_Context.cContexts--);
}

Context* Context::CreateNewContext(AppDomain *pDomain)
{
    CONTRACT (Context*)
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM());
        PRECONDITION(CheckPointer(pDomain));
    }
    CONTRACT_END;
    
    Context *p = new Context(pDomain);
    RETURN p;
}

void Context::Initialize()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Initialize the context critical section
    s_ContextCrst.Init(CrstContexts, (CrstFlags)(CRST_REENTRANCY|CRST_HOST_BREAKABLE));
}

BOOL Context::ValidateContext(Context *pCtx)
{  
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SO_TOLERANT;
        PRECONDITION(CheckPointer(pCtx));
    }
    CONTRACTL_END;  

    BOOL bRet = FALSE;
    
    EX_TRY
    {
        if (pCtx->m_Signature == CONTEXT_SIGNATURE)
            bRet = TRUE;
    }
    EX_CATCH
    {
        // Swallow exceptions - if not a valid ctx, just return false.
    }
    EX_END_CATCH(RethrowTerminalExceptions);
    
    return bRet;
}

// if the object we are creating is a proxy to another appdomain, want to create the wrapper for the
// new object in the appdomain of the proxy target
Context* Context::GetExecutionContext(OBJECTREF pObj)
{
    CONTRACT (Context*)
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(pObj != NULL);
        POSTCONDITION(CheckPointer(RETVAL, NULL_OK));
    }
    CONTRACT_END;
    
    Context *pContext = NULL;
    if (pObj->IsTransparentProxy()) 
        pContext = CRemotingServices::GetServerContextForProxy(pObj);
    if (pContext == NULL)
        pContext = GetAppDomain()->GetDefaultContext();

    RETURN pContext;
}

OBJECTREF Context::GetExposedObject()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;
    
    if (ObjectFromHandle(m_ExposedObjectHandle) == NULL)
    {
        // This call should fault in the managed context for the thread
        MethodDescCallSite getCurrentContext(METHOD__THREAD__GET_CURRENT_CONTEXT);
        CONTEXTBASEREF ctx = (CONTEXTBASEREF) getCurrentContext.Call_RetOBJECTREF((ARG_SLOT*)NULL);

        GCPROTECT_BEGIN(ctx);
        {
            // Take a lock to make sure that only one thread creates the object.
            // This locking may be too severe!
            CrstHolder ch(&s_ContextCrst);

            // Check to see if another thread has not already created the exposed object.
            if (ObjectFromHandle(m_ExposedObjectHandle) == NULL)
            {
                // Keep a weak reference to the exposed object.
                StoreObjectInHandle(m_ExposedObjectHandle, (OBJECTREF) ctx);

                ctx->SetInternalContext(this);
            }
        }
        GCPROTECT_END();

    }
    return ObjectFromHandle(m_ExposedObjectHandle);
}

void Context::SetExposedObject(OBJECTREF exposed)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(exposed != NULL);
        PRECONDITION(ObjectFromHandle(m_ExposedObjectHandle) == NULL);
    }
    CONTRACTL_END;
    
    StoreObjectInHandle(m_ExposedObjectHandle, exposed);
}

// This is called by EE to transition into a context(possibly in
// another appdomain) and execute the method Context::ExecuteCallBack
// with the private data provided to this method
void Context::RequestCallBack(ADID appDomainID, Context* targetCtxID, void* privateData)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(targetCtxID));
        PRECONDITION(CheckPointer(privateData));
        PRECONDITION(ValidateContext((Context*)targetCtxID));
    }
    CONTRACTL_END;
    // a warning: don't touch targetCtxID until you verified appDomainID, 
    // unless the latter is CURRENT_APPDOMAIN_ID

    // Get the current context of the thread. This is assumed as
    // the context where the request originated
    Context *pCurrCtx;
    pCurrCtx = GetCurrentContext();

    // Check that the target context is not the same (presumably the caller has checked for it).
    _ASSERTE(pCurrCtx != targetCtxID);

    // Check if we might be going to a context in another appDomain.
    ADID targetDomainID;

    if (appDomainID == CURRENT_APPDOMAIN_ID)
    {
        targetDomainID = (ADID)0;
        _ASSERTE(targetCtxID->GetDomain()==::GetAppDomain());
    }
    else
    {
        targetDomainID=appDomainID;
#ifdef _DEBUG        
        AppDomainFromIDHolder ad(appDomainID, FALSE);
        if (!ad.IsUnloaded())
            _ASSERTE(targetCtxID->GetDomain()->GetId()==appDomainID);
#endif
    }

    // we need to be co-operative mode for jitting
    GCX_COOP();

    MethodDescCallSite callback(METHOD__CONTEXT__CALLBACK);

    ARG_SLOT args[3];
    args[0] = PtrToArgSlot(targetCtxID);
    args[1] = PtrToArgSlot(privateData);
    args[2] = (ARG_SLOT) (size_t)targetDomainID.m_dwId;

    callback.Call(args);
}

/*** Definitions of callback executions for the various callbacks that are known to EE  ***/

// Callback for waits on waithandle
void Context::ExecuteWaitCallback(WaitArgs* waitArgs)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(waitArgs));
    }
    CONTRACTL_END;

    Thread* pCurThread = GetThread();
    _ASSERTE(pCurThread != NULL);
    
    // DoAppropriateWait switches to preemptive GC before entering the wait
    *(waitArgs->pResult) = pCurThread->DoAppropriateWait( waitArgs->numWaiters,
                                                          waitArgs->waitHandles,
                                                          waitArgs->waitAll,
                                                          waitArgs->millis,
                                                          waitArgs->alertable?WaitMode_Alertable:WaitMode_None);
}

// Callback for monitor wait on objects
void Context::ExecuteMonitorWaitCallback(MonitorWaitArgs* waitArgs)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(waitArgs));
    }
    CONTRACTL_END;
    
    Thread* pCurThread = GetThread();
    _ASSERTE(pCurThread != NULL);
    
    GCX_PREEMP();
    
    *(waitArgs->pResult) = pCurThread->Block(waitArgs->millis,
                                             waitArgs->syncState);
}

// Callback for signalandwait on waithandles
void Context::ExecuteSignalAndWaitCallback(SignalAndWaitArgs* signalAndWaitArgs)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(signalAndWaitArgs));
    }
    CONTRACTL_END;

    Thread* pCurThread = GetThread();
    _ASSERTE(pCurThread != NULL);
    
    // DoAppropriateWait switches to preemptive GC before entering the wait
    *(signalAndWaitArgs->pResult) = pCurThread->DoSignalAndWait( signalAndWaitArgs->waitHandles,
                                                          signalAndWaitArgs->millis,
                                                          signalAndWaitArgs->alertable);
}

//+----------------------------------------------------------------------------
//
//  Method:     Context::GetStaticFieldAddress   private
//
//  Synopsis:   Get the address of the field relative to the current context.
//              If an address has not been assigned yet then create one.
//

//
//+----------------------------------------------------------------------------
LPVOID Context::GetStaticFieldAddress(FieldDesc *pFD)
{
    CONTRACT (LPVOID)
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM());
        PRECONDITION(CheckPointer(pFD));
        PRECONDITION(!s_ContextCrst.OwnedByCurrentThread());
        POSTCONDITION(CheckPointer(RETVAL));
    }
    CONTRACT_END;

    LPVOID          pvAddress       = NULL;
    Context*        pCtx            = NULL;
    // for static field the MethodTable is exact even for generic classes
    MethodTable*    pMT             = pFD->GetEnclosingMethodTable();
    BOOL            fIsShared       = pMT->IsDomainNeutral();
    DWORD           dwClassOffset   = pMT->GetContextStaticsOffset();
    DWORD           currElem        = 0;
    STATIC_DATA*    pData;
    
    // NOTE: if you change this method, you must also change
    // GetStaticFieldAddrNoCreate below.

    if (dwClassOffset == (DWORD)-1)
    {
        dwClassOffset = pMT->AllocateContextStaticsOffset();
    }

    // Retrieve the current context
    pCtx = GetCurrentContext();
    _ASSERTE(NULL != pCtx);

    // Acquire the context lock before accessing the static data pointer
    {
        CrstHolder ch(&s_ContextCrst);

        if(!fIsShared)
            pData = pCtx->m_pUnsharedStaticData;
        else
            pData = pCtx->m_pSharedStaticData;

        if(NULL != pData)
            currElem = pData->cElem;

        // Check whether we have allocated space for storing a pointer to
        // this class' context static store
        if(dwClassOffset >= currElem)
        {
            // Allocate space for storing pointers
            DWORD dwNewElem = (currElem == 0 ? 4 : currElem*2);
            
            // Ensure that we grow to a size larger than the index we intend to use
            while (dwNewElem <= dwClassOffset)
                dwNewElem = 2*dwNewElem;

            STATIC_DATA *pNew = (STATIC_DATA *)new BYTE[sizeof(STATIC_DATA) + dwNewElem*sizeof(LPVOID)];

            // Set the new count.
            pNew->cElem = dwNewElem;
                        
            if(NULL != pData)
            {
                // Copy the old data into the new data
                memcpy(&pNew->dataPtr[0], &pData->dataPtr[0], currElem*sizeof(LPVOID));

                // Delete the old data
                delete [] (BYTE*) pData;
            }

            // Zero init any new elements.
            ZeroMemory(&pNew->dataPtr[currElem], (dwNewElem - currElem)* sizeof(LPVOID));

            // Update the locals
            pData = pNew;

            // Reset the pointers in the context object to point to the
            // new memory
            if(!fIsShared)
                pCtx->m_pUnsharedStaticData = pData;
            else
                pCtx->m_pSharedStaticData = pData;
        }

        _ASSERTE(NULL != pData);

        // Check whether we have to allocate space for
        // the context local statics of this class
        if(NULL == pData->dataPtr[dwClassOffset])
        {
            DWORD dwSize = pMT->GetContextStaticsSize();

            // Allocate memory for context static fields
            LPBYTE pFields = new BYTE[dwSize];

            // Initialize the memory allocated for the fields
            ZeroMemory(pFields, dwSize);

            pData->dataPtr[dwClassOffset] = pFields;
        }

        _ASSERTE(NULL != pData->dataPtr[dwClassOffset]);

        pvAddress = (LPVOID)((LPBYTE)pData->dataPtr[dwClassOffset] + pFD->GetOffset());
        
        // For object and value class fields we have to allocate storage in the
        // __StaticContainer class in the managed heap
        if(pFD->IsObjRef() || pFD->IsByValue())
        {
            // in this case *pvAddress == bucket|index
            int *pSlot = (int*)pvAddress;
            pvAddress = NULL;
            pCtx->GetStaticFieldAddressSpecial(pFD, pMT, pSlot, &pvAddress);

            if (pFD->IsByValue())
            {
                _ASSERTE(pvAddress != NULL);
                pvAddress = (*((OBJECTREF*)pvAddress))->GetData();
            }
            // ************************************************
            // ************** WARNING *************************
            // Do not provoke GC from here to the point JIT gets
            // pvAddress back
            // ************************************************
            _ASSERTE(*pSlot > 0);
        }
    }

    RETURN pvAddress;
}


//+----------------------------------------------------------------------------
//
//  Method:     Context::GetStaticFieldAddressSpecial private
//
//  Synopsis:   Allocate an entry in the __StaticContainer class in the
//              managed heap for static objects and value classes
//

//
//+----------------------------------------------------------------------------

// NOTE: At one point we used to allocate these in the long lived handle table
// which is per-appdomain. However, that causes them to get rooted and not
// cleaned up until the appdomain gets unloaded. This is not very desirable
// since a context static object may hold a reference to the context itself or
// to a proxy in the context causing a whole lot of garbage to float around.
// Now (2/13/01) these are allocated from a managed structure rooted in each
// managed context.

void Context::GetStaticFieldAddressSpecial(FieldDesc *pFD, MethodTable *pMT, int *pSlot, LPVOID *ppvAddress)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        INJECT_FAULT(COMPlusThrowOM());
        PRECONDITION(CheckPointer(pFD));
        PRECONDITION(CheckPointer(pMT));
        PRECONDITION(CheckPointer(pSlot));
        PRECONDITION(CheckPointer(ppvAddress));
    }
    CONTRACTL_END;
    
    OBJECTREF *pObjRef = NULL;
    BOOL bNewSlot = (*pSlot == 0);
    
    if (bNewSlot)
    {
        // ! this line will trigger a GC, don't move it down
        // ! without protecting the args[] and other OBJECTREFS
        OBJECTREF orThis = GetExposedObject();;
        GCPROTECT_BEGIN(orThis);
        
        MethodDescCallSite reserveSlot(METHOD__CONTEXT__RESERVE_SLOT, &orThis);

        // We need to assign a location for this static field.
        // Call the managed helper
        ARG_SLOT args[1] =
        {
            ObjToArgSlot(orThis)
        };

        // The managed ReserveSlot methods counts on this!
        _ASSERTE(s_ContextCrst.OwnedByCurrentThread());
        _ASSERTE(args[0] != 0);

        *pSlot = reserveSlot.Call_RetI4(args);

        _ASSERTE(*pSlot>0);

        GCPROTECT_END();


        // to a boxed version of the value class.This allows the standard GC
        // algorithm to take care of internal pointers in the value class.
        if (pFD->IsByValue())
        {
            // Extract the type of the field
            TypeHandle  th = pFD->GetFieldTypeHandleThrowing();

            OBJECTHANDLE oh;            
            OBJECTREF obj = MethodTable::AllocateStaticBox(th.GetMethodTable(), pMT->HasFixedAddressVTStatics(), &oh);
            pObjRef = (OBJECTREF*)CalculateAddressForManagedStatic(*pSlot);

            if (oh != NULL)
            {            
                ObjectHandleList::NodeType* pNewNode = new ObjectHandleList::NodeType(oh);                
                m_PinnedContextStatics.LinkHead(pNewNode);
            }

            SetObjectReference( pObjRef, obj, GetAppDomain() );
        }
        else
        {
            pObjRef = (OBJECTREF*)CalculateAddressForManagedStatic(*pSlot);
        }
    }
    else
    {
        // If the field already has a location assigned we go through here
        pObjRef = (OBJECTREF*)CalculateAddressForManagedStatic(*pSlot);
    }

    *(ULONG_PTR *)ppvAddress =  (ULONG_PTR)pObjRef;
}

// This is called by the managed context constructor
FCIMPL2(void, Context::SetupInternalContext, ContextBaseObject* pThisUNSAFE, CLR_BOOL bDefault)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pThisUNSAFE != NULL);
        PRECONDITION(pThisUNSAFE->m_internalContext == NULL);
    }
    CONTRACTL_END;

    CONTEXTBASEREF pThis = (CONTEXTBASEREF) pThisUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_1(pThis);

    Context *pCtx;
    
    if (bDefault)
    {
        // We have to hook this up with the internal default
        // context for the current appDomain
        pCtx = GetThread()->GetDomain()->GetDefaultContext();
    }
    else
    {
        // Create the unmanaged backing context object
        pCtx = Context::CreateNewContext(GetThread()->GetDomain());
    }

    // Set the managed & unmanaged objects to point at each other.
    pThis->SetInternalContext(pCtx);
    pCtx->SetExposedObject((OBJECTREF)pThis);

    // Set the AppDomain field in the Managed context object
    pThis->SetExposedDomain(GetThread()->GetDomain()->GetExposedObject());

    if(bDefault)
        ((APPDOMAINREF)GetThread()->GetDomain()->GetExposedObject())->SetDefaultContext(pThis);

    COUNTER_ONLY(GetPerfCounters().m_Context.cContexts++);

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

// This is called by the managed context finalizer
FCIMPL1(void, Context::CleanupInternalContext, ContextBaseObject* pThisUNSAFE)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pThisUNSAFE != NULL);
    }
    CONTRACTL_END;

    CONTEXTBASEREF pThis = (CONTEXTBASEREF) pThisUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_1(pThis);

    CONTEXTBASEREF refCtx = pThis;

    Context *pCtx = refCtx->m_internalContext;
    _ASSERTE(pCtx != NULL);
    
    if (ValidateContext(pCtx))
    {
        LOG((LF_APPDOMAIN, LL_INFO1000, "Context::CleanupInternalContext: %8.8x, %8.8x\n", OBJECTREFToObject(refCtx), pCtx));
        Context::FreeContext(pCtx);
    }

    COUNTER_ONLY(GetPerfCounters().m_Context.cContexts--);

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND


// This is where a call back request made by EE in Context::RequestCallBack
// actually gets "executed".
// At this point we have done a real context transition from the threads
// context when RequestCallBack was called to the destination context.
FCIMPL1(void, Context::ExecuteCallBack, LPVOID privateData)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(privateData));
    }
    CONTRACTL_END;

    HELPER_METHOD_FRAME_BEGIN_0();

    switch (((CallBackInfo*) privateData)->callbackId)
    {
        case Wait_callback:
        {
            WaitArgs* waitArgs;
            waitArgs = (WaitArgs*) ((CallBackInfo*) privateData)->callbackData;
            ExecuteWaitCallback(waitArgs);
            break;
        }

        case MonitorWait_callback:
        {
            MonitorWaitArgs* waitArgs;
            waitArgs = (MonitorWaitArgs*) ((CallBackInfo*) privateData)->callbackData;
            ExecuteMonitorWaitCallback(waitArgs);
            break;
        }

        case ADTransition_callback:
        {
            ADCallBackArgs* pCallArgs = (ADCallBackArgs*)(((CallBackInfo*) privateData)->callbackData);
            pCallArgs->pTarget(pCallArgs->pArguments);
            break;
        }

        case SignalAndWait_callback:
        {
            SignalAndWaitArgs* signalAndWaitArgs;
            signalAndWaitArgs = (SignalAndWaitArgs*)((CallBackInfo*)privateData)->callbackData;
            ExecuteSignalAndWaitCallback(signalAndWaitArgs);
            break;
        }
        // Add other callback types here
        default:
            _ASSERTE(!"Invalid callback type");
            break;
    }

    // This is EE's entry point to do whatever it wanted to do in
    // the targetContext. This will return back into the managed
    // world and transition back into the original context.

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND



#ifdef ENABLE_PERF_COUNTERS

FCIMPL0(LPVOID, GetPrivateContextsPerfCountersEx)
{
    FCALL_CONTRACT;

    return (LPVOID)GetPrivateContextsPerfCounters();
}
FCIMPLEND


#else
FCIMPL0(LPVOID, GetPrivateContextsPerfCountersEx)
{
    FCALL_CONTRACT;

    return NULL;
}
FCIMPLEND

#endif // ENABLE_PERF_COUNTERS

#endif // DACCESS_COMPILE

// This will NOT create the exposed object if there isn't one!
OBJECTREF Context::GetExposedObjectRaw()
{
    WRAPPER_NO_CONTRACT;
    
    return ObjectFromHandle(m_ExposedObjectHandle);
}


PTR_Object Context::GetExposedObjectRawUnchecked()
{
    LIMITED_METHOD_CONTRACT;

    return *PTR_PTR_Object(m_ExposedObjectHandle);
}

PTR_PTR_Object Context::GetExposedObjectRawUncheckedPtr()
{
    LIMITED_METHOD_CONTRACT;

    return PTR_PTR_Object(m_ExposedObjectHandle);
}

//+----------------------------------------------------------------------------
//
//  Method:     Context::GetStaticFieldAddrNoCreate   private
//
//  Synopsis:   Get the address of the field relative to the context given a thread.
//              If an address has not been assigned, return NULL.
//              No creating is allowed.
//

//
//+----------------------------------------------------------------------------
PTR_VOID Context::GetStaticFieldAddrNoCreate(FieldDesc *pFD)
{
    CONTRACT (PTR_VOID)
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(pFD));
        POSTCONDITION(CheckPointer(RETVAL, NULL_OK));
        SUPPORTS_DAC;
    }
    CONTRACT_END;
    
    PTR_VOID        pvAddress       = NULL;
    // for static field the MethodTable is exact even for generic classes
    MethodTable*    pMT             = pFD->GetEnclosingMethodTable();
    BOOL            fIsShared       = pMT->IsDomainNeutral();
    DWORD           dwClassOffset   = pMT->GetContextStaticsOffset();
    DWORD           currElem        = 0;
    STATIC_DATA*    pData;

    if (dwClassOffset == (DWORD)-1)
        RETURN NULL;
    
    if(!fIsShared)
        pData = m_pUnsharedStaticData;
    else
        pData = m_pSharedStaticData;

    if (NULL == pData)
        RETURN NULL;
    
    currElem = pData->cElem;

    // Check whether we have allocated space for storing a pointer to
    // this class' context static store
    if(dwClassOffset >= currElem || pData->dataPtr[dwClassOffset] == NULL)
        RETURN NULL;

    _ASSERTE(pData->dataPtr[dwClassOffset] != NULL);
    
    // We have allocated static storage for this data
    // Just return the address by getting the offset into the data
    pvAddress = PTR_VOID(dac_cast<PTR_BYTE>(pData->dataPtr[dwClassOffset]) + pFD->GetOffset());

    if(pFD->IsObjRef() || pFD->IsByValue())
    {
        if (*dac_cast<PTR_BYTE>(pvAddress) == NULL)
        {
            pvAddress = NULL;
            LOG((LF_SYNC, LL_ALWAYS, "dbgr: pvAddress = NULL"));
        }
        else
        {
            pvAddress = CalculateAddressForManagedStatic(*(PTR_int(pvAddress)));
            LOG((LF_SYNC, LL_ALWAYS, "dbgr: pvAddress = %lx", pvAddress));
            if (pFD->IsByValue())
            {
                _ASSERTE(pvAddress != NULL);
                pvAddress = (*(PTR_OBJECTREF(pvAddress)))->GetData();
            }
        }
    }

    RETURN pvAddress;
}


// This is used for context relative statics that are object refs
// These are stored in a structure in the managed context. The first
// time over an index and a bucket are determined and subsequently
// remembered in the location for the field in the per-context-per-class
// data structure.
// Here we map back from the index to the address of the object ref.
PTR_VOID Context::CalculateAddressForManagedStatic(int slot)
{
    CONTRACT (PTR_VOID)
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        POSTCONDITION(CheckPointer(RETVAL));
        SUPPORTS_DAC;
    }
    CONTRACT_END;
   
    PTR_OBJECTREF pObjRef;
    int bucket = (slot>>16);
    int index = (0x0000ffff&slot);
    
    // Now determine the address of the static field
    PTRARRAYREF bucketRef = NULL;

    bucketRef = ((CONTEXTBASEREF)GetExposedObjectRaw())->GetContextStaticsHolder();
    
    // walk the chain to our bucket
    while (bucket--)
        bucketRef = (PTRARRAYREF) bucketRef->GetAt(0);

    // Index 0 is used to point to the next bucket!
    _ASSERTE(index > 0);
    pObjRef = PTR_OBJECTREF(bucketRef->GetDataPtr())+index;

    RETURN (PTR_VOID(pObjRef));
}

#endif // FEATURE_REMOTING

#ifdef DACCESS_COMPILE

void
Context::EnumMemoryRegions(CLRDataEnumMemoryFlags flags)
{
    SUPPORTS_DAC;
    DAC_ENUM_DTHIS();

    if (m_pDomain.IsValid())
    {
        m_pDomain->EnumMemoryRegions(flags, true);
    }
}
#endif // #ifdef DACCESS_COMPILE