This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathobject.cpp
3510 lines (2943 loc) · 107 KB
/
object.cpp
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//
// OBJECT.CPP
//
// Definitions of a Com+ Object
//
#include "common.h"
#include "vars.hpp"
#include "class.h"
#include "object.h"
#include "threads.h"
#include "excep.h"
#include "eeconfig.h"
#include "gc.h"
#ifdef FEATURE_REMOTING
#include "remoting.h"
#endif
#include "field.h"
#include "gcscan.h"
#ifdef FEATURE_COMPRESSEDSTACK
void* CompressedStackObject::GetUnmanagedCompressedStack()
{
LIMITED_METHOD_CONTRACT;
return ((m_compressedStackHandle != NULL)?m_compressedStackHandle->GetHandle():NULL);
}
#endif // FEATURE_COMPRESSEDSTACK
#ifndef FEATURE_PAL
LPVOID FrameSecurityDescriptorBaseObject::GetCallerToken()
{
LIMITED_METHOD_CONTRACT;
return ((m_callerToken!= NULL)?m_callerToken->GetHandle():NULL);
}
LPVOID FrameSecurityDescriptorBaseObject::GetImpersonationToken()
{
LIMITED_METHOD_CONTRACT;
return ((m_impToken != NULL)?m_impToken->GetHandle():NULL);
}
#endif
SVAL_IMPL(INT32, ArrayBase, s_arrayBoundsZero);
// follow the necessary rules to get a new valid hashcode for an object
DWORD Object::ComputeHashCode()
{
DWORD hashCode;
// note that this algorithm now uses at most HASHCODE_BITS so that it will
// fit into the objheader if the hashcode has to be moved back into the objheader
// such as for an object that is being frozen
do
{
// we use the high order bits in this case because they're more random
hashCode = GetThread()->GetNewHashCode() >> (32-HASHCODE_BITS);
}
while (hashCode == 0); // need to enforce hashCode != 0
// verify that it really fits into HASHCODE_BITS
_ASSERTE((hashCode & ((1<<HASHCODE_BITS)-1)) == hashCode);
return hashCode;
}
#ifndef DACCESS_COMPILE
INT32 Object::GetHashCodeEx()
{
CONTRACTL
{
MODE_COOPERATIVE;
THROWS;
GC_NOTRIGGER;
SO_TOLERANT;
}
CONTRACTL_END
// This loop exists because we're inspecting the header dword of the object
// and it may change under us because of races with other threads.
// On top of that, it may have the spin lock bit set, in which case we're
// not supposed to change it.
// In all of these case, we need to retry the operation.
DWORD iter = 0;
DWORD dwSwitchCount = 0;
while (true)
{
DWORD bits = GetHeader()->GetBits();
if (bits & BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX)
{
if (bits & BIT_SBLK_IS_HASHCODE)
{
// Common case: the object already has a hash code
return bits & MASK_HASHCODE;
}
else
{
// We have a sync block index. This means if we already have a hash code,
// it is in the sync block, otherwise we generate a new one and store it there
SyncBlock *psb = GetSyncBlock();
DWORD hashCode = psb->GetHashCode();
if (hashCode != 0)
return hashCode;
hashCode = ComputeHashCode();
return psb->SetHashCode(hashCode);
}
}
else
{
// If a thread is holding the thin lock or an appdomain index is set, we need a syncblock
if ((bits & (SBLK_MASK_LOCK_THREADID | (SBLK_MASK_APPDOMAININDEX << SBLK_APPDOMAIN_SHIFT))) != 0)
{
GetSyncBlock();
// No need to replicate the above code dealing with sync blocks
// here - in the next iteration of the loop, we'll realize
// we have a syncblock, and we'll do the right thing.
}
else
{
// We want to change the header in this case, so we have to check the BIT_SBLK_SPIN_LOCK bit first
if (bits & BIT_SBLK_SPIN_LOCK)
{
iter++;
if ((iter % 1024) != 0 && g_SystemInfo.dwNumberOfProcessors > 1)
{
YieldProcessor(); // indicate to the processor that we are spining
}
else
{
__SwitchToThread(0, ++dwSwitchCount);
}
continue;
}
DWORD hashCode = ComputeHashCode();
DWORD newBits = bits | BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX | BIT_SBLK_IS_HASHCODE | hashCode;
if (GetHeader()->SetBits(newBits, bits) == bits)
return hashCode;
// Header changed under us - let's restart this whole thing.
}
}
}
}
#endif // #ifndef DACCESS_COMPILE
BOOL Object::ValidateObjectWithPossibleAV()
{
CANNOT_HAVE_CONTRACT;
SUPPORTS_DAC;
return GetGCSafeMethodTable()->ValidateWithPossibleAV();
}
#ifndef DACCESS_COMPILE
MethodTable *Object::GetTrueMethodTable()
{
CONTRACT(MethodTable*)
{
MODE_COOPERATIVE;
GC_NOTRIGGER;
NOTHROW;
SO_TOLERANT;
POSTCONDITION(CheckPointer(RETVAL));
}
CONTRACT_END;
MethodTable *mt = GetMethodTable();
#ifdef FEATURE_REMOTING
if(mt->IsTransparentProxy())
{
mt = ((TransparentProxyObject *)this)->GetMethodTableBeingProxied();
}
_ASSERTE(!mt->IsTransparentProxy());
#endif
RETURN mt;
}
TypeHandle Object::GetTrueTypeHandle()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_TOLERANT;
MODE_COOPERATIVE;
}
CONTRACTL_END;
if (m_pMethTab->IsArray())
return ((ArrayBase*) this)->GetTypeHandle();
else
return TypeHandle(GetTrueMethodTable());
}
// There are cases where it is not possible to get a type handle during a GC.
// If we can get the type handle, this method will return it.
// Otherwise, the method will return NULL.
TypeHandle Object::GetGCSafeTypeHandleIfPossible() const
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
if(!IsGCThread()) { MODE_COOPERATIVE; }
}
CONTRACTL_END;
// Although getting the type handle is unsafe and could cause recursive type lookups
// in some cases, it's always safe and straightforward to get to the MethodTable.
MethodTable * pMT = GetGCSafeMethodTable();
_ASSERTE(pMT != NULL);
// Don't look at types that belong to an unloading AppDomain, or else
// pObj->GetGCSafeTypeHandle() can AV. For example, we encountered this AV when pObj
// was an array like this:
//
// MyValueType1<MyValueType2>[] myArray
//
// where MyValueType1<T> & MyValueType2 are defined in different assemblies. In such
// a case, looking up the type handle for myArray requires looking in
// MyValueType1<T>'s module's m_AssemblyRefByNameTable, which is garbage if its
// AppDomain is unloading.
//
// Another AV was encountered in a similar case,
//
// MyRefType1<MyRefType2>[] myArray
//
// where MyRefType2's module was unloaded by the time the GC occurred. In at least
// one case, the GC was caused by the AD unload itself (AppDomain::Unload ->
// AppDomain::Exit -> GCInterface::AddMemoryPressure -> WKS::GCHeap::GarbageCollect).
//
// To protect against all scenarios, verify that
//
// * The MT of the object is not getting unloaded, OR
// * In the case of arrays (potentially of arrays of arrays of arrays ...), the
// MT of the innermost element is not getting unloaded. This then ensures the
// MT of the original object (i.e., array) itself must not be getting
// unloaded either, since the MTs of arrays and of their elements are
// allocated on the same loader heap, except the case where the array is
// Object[], in which case its MT is in mscorlib and thus doesn't unload.
MethodTable * pMTToCheck = pMT;
if (pMTToCheck->IsArray())
{
TypeHandle thElem = static_cast<const ArrayBase * const>(this)->GetArrayElementTypeHandle();
// Ideally, we would just call thElem.GetLoaderModule() here. Unfortunately, the
// current TypeDesc::GetLoaderModule() implementation depends on data structures
// that might have been unloaded already. So we just simulate
// TypeDesc::GetLoaderModule() for the limited array case that we care about. In
// case we're dealing with an array of arrays of arrays etc. traverse until we
// find the deepest element, and that's the type we'll check
while (thElem.HasTypeParam())
{
thElem = thElem.GetTypeParam();
}
pMTToCheck = thElem.GetMethodTable();
}
Module * pLoaderModule = pMTToCheck->GetLoaderModule();
BaseDomain * pBaseDomain = pLoaderModule->GetDomain();
if ((pBaseDomain != NULL) &&
(pBaseDomain->IsAppDomain()) &&
(pBaseDomain->AsAppDomain()->IsUnloading()))
{
return NULL;
}
// Don't look up types that are unloading due to Collectible Assemblies. Haven't been
// able to find a case where we actually encounter objects like this that can cause
// problems; however, it seems prudent to add this protection just in case.
LoaderAllocator * pLoaderAllocator = pLoaderModule->GetLoaderAllocator();
_ASSERTE(pLoaderAllocator != NULL);
if ((pLoaderAllocator->IsCollectible()) &&
(ObjectHandleIsNull(pLoaderAllocator->GetLoaderAllocatorObjectHandle())))
{
return NULL;
}
// Ok, it should now be safe to get the type handle
return GetGCSafeTypeHandle();
}
/* static */ BOOL Object::SupportsInterface(OBJECTREF pObj, MethodTable* pInterfaceMT)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
INJECT_FAULT(COMPlusThrowOM());
PRECONDITION(CheckPointer(pInterfaceMT));
PRECONDITION(pObj->GetTrueMethodTable()->IsRestored_NoLogging());
PRECONDITION(pInterfaceMT->IsInterface());
}
CONTRACTL_END
BOOL bSupportsItf = FALSE;
GCPROTECT_BEGIN(pObj)
{
// Make sure the interface method table has been restored.
pInterfaceMT->CheckRestore();
// Check to see if the static class definition indicates we implement the interface.
MethodTable * pMT = pObj->GetTrueMethodTable();
if (pMT->CanCastToInterface(pInterfaceMT))
{
bSupportsItf = TRUE;
}
#ifdef FEATURE_COMINTEROP
else
if (pMT->IsComObjectType())
{
// If this is a COM object, the static class definition might not be complete so we need
// to check if the COM object implements the interface.
bSupportsItf = ComObject::SupportsInterface(pObj, pInterfaceMT);
}
#endif // FEATURE_COMINTEROP
}
GCPROTECT_END();
return bSupportsItf;
}
Assembly *AssemblyBaseObject::GetAssembly()
{
WRAPPER_NO_CONTRACT;
return m_pAssembly->GetAssembly();
}
#ifdef _DEBUG
// Object::DEBUG_SetAppDomain specified DEBUG_ONLY in the contract to disable SO-tolerance
// checking for paths that are DEBUG-only.
//
// NOTE: currently this is only used by WIN64 allocation helpers, but they really should
// be calling the JIT helper SetObjectAppDomain (which currently only exists for
// x86).
void Object::DEBUG_SetAppDomain(AppDomain *pDomain)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
DEBUG_ONLY;
INJECT_FAULT(COMPlusThrowOM(););
PRECONDITION(CheckPointer(pDomain));
}
CONTRACTL_END;
/*_ASSERTE(GetThread()->IsSOTolerant());*/
SetAppDomain(pDomain);
}
#endif
void Object::SetAppDomain(AppDomain *pDomain)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
SO_INTOLERANT;
INJECT_FAULT(COMPlusThrowOM(););
PRECONDITION(CheckPointer(pDomain));
}
CONTRACTL_END;
#ifndef _DEBUG
if (!GetMethodTable()->IsDomainNeutral())
{
//
// If we have a per-app-domain method table, we can
// infer the app domain from the method table, so
// there is no reason to mark the object.
//
// But we don't do this in a debug build, because
// we want to be able to detect the case when the
// domain was unloaded from underneath an object (and
// the MethodTable will be toast in that case.)
//
_ASSERTE(pDomain == GetMethodTable()->GetDomain());
}
else
#endif
{
ADIndex index = pDomain->GetIndex();
GetHeader()->SetAppDomainIndex(index);
}
_ASSERTE(GetHeader()->GetAppDomainIndex().m_dwIndex != 0);
}
AppDomain *Object::GetAppDomain()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_TOLERANT;
MODE_COOPERATIVE;
}
CONTRACTL_END;
#ifndef _DEBUG
if (!GetMethodTable()->IsDomainNeutral())
return (AppDomain*) GetMethodTable()->GetDomain();
#endif
ADIndex index = GetHeader()->GetAppDomainIndex();
if (index.m_dwIndex == 0)
return NULL;
AppDomain *pDomain = SystemDomain::TestGetAppDomainAtIndex(index);
#if CHECK_APP_DOMAIN_LEAKS
if (! g_pConfig->AppDomainLeaks())
return pDomain;
if (IsAppDomainAgile())
return NULL;
//
// If an object has an index of an unloaded domain (its ok to be of a
// domain where an unload is in progress through), go ahead
// and make it agile. If this fails, we have an invalid reference
// to an unloaded domain. If it succeeds, the object is no longer
// contained in that app domain so we can continue.
//
if (pDomain == NULL)
{
if (SystemDomain::IndexOfAppDomainBeingUnloaded() == index) {
// if appdomain is unloading but still alive and is valid to have instances
// in that domain, then use it.
AppDomain *tmpDomain = SystemDomain::AppDomainBeingUnloaded();
if (tmpDomain && tmpDomain->ShouldHaveInstances())
pDomain = tmpDomain;
}
if (!pDomain && ! TrySetAppDomainAgile(FALSE))
{
_ASSERTE(!"Attempt to reference an object belonging to an unloaded domain");
}
}
#endif
return pDomain;
}
STRINGREF AllocateString(SString sstr)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
} CONTRACTL_END;
COUNT_T length = sstr.GetCount(); // count of WCHARs excluding terminating NULL
STRINGREF strObj = AllocateString(length);
memcpyNoGCRefs(strObj->GetBuffer(), sstr.GetUnicode(), length*sizeof(WCHAR));
return strObj;
}
CHARARRAYREF AllocateCharArray(DWORD dwArrayLength)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
}
CONTRACTL_END;
return (CHARARRAYREF)AllocatePrimitiveArray(ELEMENT_TYPE_CHAR, dwArrayLength);
}
#if CHECK_APP_DOMAIN_LEAKS
BOOL Object::IsAppDomainAgile()
{
WRAPPER_NO_CONTRACT;
DEBUG_ONLY_FUNCTION;
SyncBlock *psb = PassiveGetSyncBlock();
if (psb)
{
if (psb->IsAppDomainAgile())
return TRUE;
if (psb->IsCheckedForAppDomainAgile())
return FALSE;
}
return CheckAppDomain(NULL);
}
BOOL Object::TrySetAppDomainAgile(BOOL raiseAssert)
{
LIMITED_METHOD_CONTRACT;
FAULT_NOT_FATAL();
DEBUG_ONLY_FUNCTION;
BOOL ret = TRUE;
EX_TRY
{
ret = SetAppDomainAgile(raiseAssert);
}
EX_CATCH{}
EX_END_CATCH(SwallowAllExceptions);
return ret;
}
BOOL Object::ShouldCheckAppDomainAgile (BOOL raiseAssert, BOOL *pfResult)
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
DEBUG_ONLY_FUNCTION;
if (!g_pConfig->AppDomainLeaks())
{
*pfResult = TRUE;
return FALSE;
}
if (this == NULL)
{
*pfResult = TRUE;
return FALSE;
}
if (IsAppDomainAgile())
{
*pfResult = TRUE;
return FALSE;
}
// if it's not agile and we've already checked it, just bail early
if (IsCheckedForAppDomainAgile())
{
*pfResult = FALSE;
return FALSE;
}
if (IsTypeNeverAppDomainAgile())
{
if (raiseAssert)
_ASSERTE(!"Attempt to reference a domain bound object from an agile location");
*pfResult = FALSE;
return FALSE;
}
//
// Do not allow any object to be set to be agile unless we
// are compiling field access checking into the class. This
// will help guard against unintentional "agile" propagation
// as well.
//
if (!IsTypeAppDomainAgile() && !IsTypeCheckAppDomainAgile())
{
if (raiseAssert)
_ASSERTE(!"Attempt to reference a domain bound object from an agile location");
*pfResult = FALSE;
return FALSE;
}
return TRUE;
}
BOOL Object::SetAppDomainAgile(BOOL raiseAssert, SetAppDomainAgilePendingTable *pTable)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
INJECT_FAULT(COMPlusThrowOM(););
DEBUG_ONLY;
}
CONTRACTL_END;
BEGIN_DEBUG_ONLY_CODE;
BOOL fResult;
if (!this->ShouldCheckAppDomainAgile(raiseAssert, &fResult))
return fResult;
//
// If a SetAppDomainAgilePendingTable is provided, then SetAppDomainAgile
// was called via SetAppDomainAgile. Simply store this object in the
// table, and let the calling SetAppDomainAgile process it later in a
// non-recursive manner.
//
if (pTable == NULL)
{
pTable = (SetAppDomainAgilePendingTable *)ClrFlsGetValue(TlsIdx_AppDomainAgilePendingTable);
}
if (pTable)
{
//
// If the object is already being checked (on this thread or another),
// don't duplicate the effort. Return TRUE to tell the caller to
// continue processing other references. Since we're just testing
// the bit we don't need to take the spin lock.
//
ObjHeader* pOh = this->GetHeader();
_ASSERTE(pOh);
if (pOh->GetBits() & BIT_SBLK_AGILE_IN_PROGRESS)
{
return TRUE;
}
pTable->PushReference(this);
}
else
{
//
// Initialize the table of pending objects
//
SetAppDomainAgilePendingTable table;
class ResetPendingTable
{
public:
ResetPendingTable(SetAppDomainAgilePendingTable *pTable)
{
ClrFlsSetValue(TlsIdx_AppDomainAgilePendingTable, pTable);
}
~ResetPendingTable()
{
ClrFlsSetValue(TlsIdx_AppDomainAgilePendingTable, NULL);
}
};
ResetPendingTable resetPendingTable(&table);
//
// Iterate over the table, processing all referenced objects until the
// entire graph has its sync block marked, or a non-agile object is
// found. The loop will start with the current object, as though we
// just removed it from the table as a pending reference.
//
Object *pObject = this;
do
{
//
// Mark the object to identify recursion.
// ~SetAppDomainAgilePendingTable will clean up
// BIT_SBLK_AGILE_IN_PROGRESS, so attempt to push the object first
// in case it needs to throw an exception.
//
table.PushParent(pObject);
ObjHeader* pOh = pObject->GetHeader();
_ASSERTE(pOh);
bool fInProgress = false;
{
ENTER_SPIN_LOCK(pOh);
{
if (pOh->GetBits() & BIT_SBLK_AGILE_IN_PROGRESS)
{
fInProgress = true;
}
else
{
pOh->SetBit(BIT_SBLK_AGILE_IN_PROGRESS);
}
}
LEAVE_SPIN_LOCK(pOh);
}
if (fInProgress)
{
//
// Object is already being processed, so just remove it from
// the table and look for another object.
//
bool fReturnedToParent = false;
Object *pLastObject = table.GetPendingObject(&fReturnedToParent);
CONSISTENCY_CHECK(pLastObject == pObject && fReturnedToParent);
}
else
{
//
// Finish processing this object. Any references will be added to
// the table.
//
if (!pObject->SetAppDomainAgileWorker(raiseAssert, &table))
return FALSE;
}
//
// Find the next object to explore.
//
for (;;)
{
bool fReturnedToParent;
pObject = table.GetPendingObject(&fReturnedToParent);
//
// No more objects in the table?
//
if (!pObject)
break;
//
// If we've processed all objects reachable through an object,
// then clear BIT_SBLK_AGILE_IN_PROGRESS, and look for another
// object in the table.
//
if (fReturnedToParent)
{
pOh = pObject->GetHeader();
_ASSERTE(pOh);
ENTER_SPIN_LOCK(pOh);
pOh->ClrBit(BIT_SBLK_AGILE_IN_PROGRESS);
LEAVE_SPIN_LOCK(pOh);
}
else
{
//
// Re-check whether we should explore through this reference.
//
if (pObject->ShouldCheckAppDomainAgile(raiseAssert, &fResult))
break;
if (!fResult)
return FALSE;
}
}
}
while (pObject);
}
END_DEBUG_ONLY_CODE;
return TRUE;
}
BOOL Object::SetAppDomainAgileWorker(BOOL raiseAssert, SetAppDomainAgilePendingTable *pTable)
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
DEBUG_ONLY_FUNCTION;
BOOL ret = TRUE;
if (! IsTypeAppDomainAgile() && ! SetFieldsAgile(raiseAssert, pTable))
{
SetIsCheckedForAppDomainAgile();
ret = FALSE;
}
if (ret)
{
SetSyncBlockAppDomainAgile();
}
return ret;
}
SetAppDomainAgilePendingTable::SetAppDomainAgilePendingTable ()
: m_Stack(sizeof(PendingEntry))
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
DEBUG_ONLY_FUNCTION;
}
SetAppDomainAgilePendingTable::~SetAppDomainAgilePendingTable ()
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
DEBUG_ONLY_FUNCTION;
while (TRUE)
{
Object *pObj;
bool fObjMarked;
pObj = GetPendingObject(&fObjMarked);
if (pObj == NULL)
{
break;
}
if (fObjMarked)
{
ObjHeader* pOh = pObj->GetHeader();
_ASSERTE(pOh);
ENTER_SPIN_LOCK(pOh);
pOh->ClrBit(BIT_SBLK_AGILE_IN_PROGRESS);
LEAVE_SPIN_LOCK(pOh);
}
}
}
void Object::SetSyncBlockAppDomainAgile()
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
DEBUG_ONLY_FUNCTION;
SyncBlock *psb = PassiveGetSyncBlock();
if (! psb)
{
psb = GetSyncBlock();
}
psb->SetIsAppDomainAgile();
}
#if CHECK_APP_DOMAIN_LEAKS
BOOL Object::CheckAppDomain(AppDomain *pAppDomain)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
DEBUG_ONLY_FUNCTION;
if (!g_pConfig->AppDomainLeaks())
return TRUE;
if (this == NULL)
return TRUE;
if (IsAppDomainAgileRaw())
return TRUE;
#ifndef _DEBUG
MethodTable *pMT = GetGCSafeMethodTable();
if (!pMT->IsDomainNeutral())
return pAppDomain == pMT->GetDomain();
#endif
ADIndex index = GetHeader()->GetAppDomainIndex();
_ASSERTE(index.m_dwIndex != 0);
return (pAppDomain != NULL && index == pAppDomain->GetIndex());
}
#endif
BOOL Object::IsTypeAppDomainAgile()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
DEBUG_ONLY_FUNCTION;
MethodTable *pMT = GetGCSafeMethodTable();
if (pMT->IsArray())
{
TypeHandle th = pMT->GetApproxArrayElementTypeHandle();
return th.IsArrayOfElementsAppDomainAgile();
}
else
return pMT->GetClass()->IsAppDomainAgile();
}
BOOL Object::IsTypeCheckAppDomainAgile()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
DEBUG_ONLY_FUNCTION;
MethodTable *pMT = GetGCSafeMethodTable();
if (pMT->IsArray())
{
TypeHandle th = pMT->GetApproxArrayElementTypeHandle();
return th.IsArrayOfElementsCheckAppDomainAgile();
}
else
return pMT->GetClass()->IsCheckAppDomainAgile();
}
BOOL Object::IsTypeNeverAppDomainAgile()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
DEBUG_ONLY_FUNCTION;
return !IsTypeAppDomainAgile() && !IsTypeCheckAppDomainAgile();
}
BOOL Object::IsTypeTypesafeAppDomainAgile()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
DEBUG_ONLY_FUNCTION;
return IsTypeAppDomainAgile() && !IsTypeCheckAppDomainAgile();
}
BOOL Object::TryAssignAppDomain(AppDomain *pAppDomain, BOOL raiseAssert)
{
LIMITED_METHOD_CONTRACT;
FAULT_NOT_FATAL();
DEBUG_ONLY_FUNCTION;
BOOL ret = TRUE;
EX_TRY
{
ret = AssignAppDomain(pAppDomain,raiseAssert);
}
EX_CATCH{}
EX_END_CATCH(SwallowAllExceptions);
return ret;
}
BOOL Object::AssignAppDomain(AppDomain *pAppDomain, BOOL raiseAssert)
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
DEBUG_ONLY_FUNCTION;
if (!g_pConfig->AppDomainLeaks())
return TRUE;
if (CheckAppDomain(pAppDomain))
return TRUE;
//
// App domain does not match; try to make this object agile
//
if (IsTypeNeverAppDomainAgile())
{
if (raiseAssert)
{
if (pAppDomain == NULL)
_ASSERTE(!"Attempt to reference a domain bound object from an agile location");
else
_ASSERTE(!"Attempt to reference a domain bound object from a different domain");
}
return FALSE;
}
else
{
//
// Make object agile
//
if (! IsTypeAppDomainAgile() && ! SetFieldsAgile(raiseAssert))
{
SetIsCheckedForAppDomainAgile();
return FALSE;
}
SetSyncBlockAppDomainAgile();
return TRUE;
}
}
BOOL Object::AssignValueTypeAppDomain(MethodTable *pMT, void *base, AppDomain *pAppDomain, BOOL raiseAssert)
{