-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
methodtable.h
4021 lines (3341 loc) · 149 KB
/
methodtable.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// File: methodtable.h
//
#ifndef _METHODTABLE_H_
#define _METHODTABLE_H_
/*
* Include Files
*/
#include "vars.hpp"
#include "cor.h"
#include "hash.h"
#include "crst.h"
#include "cgensys.h"
#ifdef FEATURE_COMINTEROP
#include "stdinterfaces.h"
#endif
#include "slist.h"
#include "spinlock.h"
#include "typehandle.h"
#include "eehash.h"
#include "contractimpl.h"
#include "generics.h"
#include "gcinfotypes.h"
#include "enum_class_flags.h"
#include "threadstatics.h"
/*
* Forward Declarations
*/
class AppDomain;
class ArrayClass;
class ArrayMethodDesc;
class ClassLoader;
class FCallMethodDesc;
class EEClass;
class EnCFieldDesc;
class FieldDesc;
class JIT_TrialAlloc;
struct LayoutRawFieldInfo;
class MetaSig;
class MethodDesc;
class MethodDescChunk;
class MethodTable;
class Module;
class Object;
class Stub;
class Substitution;
class TypeHandle;
class Dictionary;
class AllocMemTracker;
class SimpleRWLock;
class MethodDataCache;
class EEClassLayoutInfo;
class EEClassNativeLayoutInfo;
#ifdef FEATURE_COMINTEROP
class ComCallWrapperTemplate;
#endif
#ifdef FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
class ClassFactoryBase;
#endif // FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
class ArgDestination;
enum class WellKnownAttribute : DWORD;
struct MethodTableAuxiliaryData;
typedef DPTR(MethodTableAuxiliaryData) PTR_MethodTableAuxiliaryData;
typedef DPTR(MethodTableAuxiliaryData const) PTR_Const_MethodTableAuxiliaryData;
enum class StaticsOffsetType
{
Normal,
ThreadLocal
};
enum class ResolveVirtualStaticMethodFlags
{
None = 0,
AllowNullResult = 1,
VerifyImplemented = 2,
AllowVariantMatches = 4,
InstantiateResultOverFinalMethodDesc = 8,
support_use_as_flags // Enable the template functions in enum_class_flags.h
};
enum class FindDefaultInterfaceImplementationFlags
{
None,
AllowVariance = 1,
ThrowOnConflict = 2,
InstantiateFoundMethodDesc = 4,
support_use_as_flags // Enable the template functions in enum_class_flags.h
};
enum class MethodTableStaticsFlags
{
None = 0,
Present = 0x1,
Generic = 0x2,
Thread = 0x4,
support_use_as_flags // Enable the template functions in enum_class_flags.h
};
enum class MethodDataComputeOptions
{
NoCache, // Do not place the results of getting the MethodData into the cache, but use it if it is there
NoCacheVirtualsOnly, // Do not place the results of getting the MethodData into the cache, but use it if it is there. If freshly computed, only fill in virtual data, and ignore non-virtuals
Cache, // Place result of getting MethodData into the cache if it is not already there
CacheOnly, // Get the MethodData from the cache. If not present, simply do not return one
};
//============================================================================
// This is the in-memory structure of a class and it will evolve.
//============================================================================
// <TODO>
// Add a sync block
// Also this class currently has everything public - this may changes
// Might also need to hold onto the meta data loader fot this class</TODO>
//
// A MethodTable contains an array of these structures, which describes each interface implemented
// by this class (directly declared or indirectly declared).
//
// Generic type instantiations (in C# syntax: C<ty_1,...,ty_n>) are represented by
// MethodTables, i.e. a new MethodTable gets allocated for each such instantiation.
// The entries in these tables (i.e. the code) are, however, often shared.
//
// In particular, a MethodTable's vtable contents (and hence method descriptors) may be
// shared between compatible instantiations (e.g. List<string> and List<object> have
// the same vtable *contents*). Likewise the EEClass will be shared between
// compatible instantiations whenever the vtable contents are.
//
// !!! Thus that it is _not_ generally the case that GetClass.GetMethodTable() == t. !!!
//
// Instantiated interfaces have their own method tables unique to the instantiation e.g. I<string> is
// distinct from I<int> and I<object>
//
// For generic types the interface map lists generic interfaces
// For instantiated types the interface map lists instantiated interfaces
// e.g. for C<T> : I<T>, J<string>
// the interface map for C would list I and J
// the interface map for C<int> would list I<int> and J<string>
//
struct InterfaceInfo_t
{
// Method table of the interface
PTR_MethodTable m_pMethodTable;
public:
FORCEINLINE PTR_MethodTable GetMethodTable()
{
LIMITED_METHOD_CONTRACT;
return VolatileLoadWithoutBarrier(&m_pMethodTable);
}
#ifndef DACCESS_COMPILE
void SetMethodTable(MethodTable * pMT)
{
LIMITED_METHOD_CONTRACT;
return VolatileStoreWithoutBarrier(&m_pMethodTable, pMT);
}
// Get approximate method table. This is used by the type loader before the type is fully loaded.
PTR_MethodTable GetApproxMethodTable(Module * pContainingModule);
#endif // !DACCESS_COMPILE
#ifndef DACCESS_COMPILE
InterfaceInfo_t(InterfaceInfo_t &right)
{
VolatileStoreWithoutBarrier(&m_pMethodTable, VolatileLoadWithoutBarrier(&right.m_pMethodTable));
}
#else // !DACCESS_COMPILE
private:
InterfaceInfo_t(InterfaceInfo_t &right);
#endif // !DACCESS_COMPILE
}; // struct InterfaceInfo_t
typedef DPTR(InterfaceInfo_t) PTR_InterfaceInfo;
namespace ClassCompat
{
struct InterfaceInfo_t;
};
// Data needed when simulating old VTable layout for COM Interop
// This is necessary as the data is saved in MethodDescs and we need
// to simulate different values without copying or changing the existing
// MethodDescs
//
// This will be created in a parallel array to ppMethodDescList and
// ppUnboxMethodDescList in the bmtMethAndFieldDescs structure below
struct InteropMethodTableSlotData
{
enum
{
e_DUPLICATE = 0x0001 // The entry is duplicate
};
MethodDesc *pMD; // The MethodDesc for this slot
WORD wSlot; // The simulated slot value for the MethodDesc
WORD wFlags; // The simulated duplicate value
MethodDesc *pDeclMD; // To keep track of MethodImpl's
void SetDuplicate()
{
wFlags |= e_DUPLICATE;
}
BOOL IsDuplicate() {
return ((BOOL)(wFlags & e_DUPLICATE));
}
WORD GetSlot() {
return wSlot;
}
void SetSlot(WORD wSlot) {
this->wSlot = wSlot;
}
}; // struct InteropMethodTableSlotData
#ifdef FEATURE_COMINTEROP
struct InteropMethodTableData
{
WORD cVTable; // Count of vtable slots
InteropMethodTableSlotData *pVTable; // Data for each slot
WORD cNonVTable; // Count of non-vtable slots
InteropMethodTableSlotData *pNonVTable; // Data for each slot
WORD cInterfaceMap; // Count of interfaces
ClassCompat::InterfaceInfo_t *
pInterfaceMap; // The interface map
// Utility methods
static WORD GetRealMethodDesc(MethodTable *pMT, MethodDesc *pMD);
static WORD GetSlotForMethodDesc(MethodTable *pMT, MethodDesc *pMD);
ClassCompat::InterfaceInfo_t* FindInterface(MethodTable *pInterface);
WORD GetStartSlotForInterface(MethodTable* pInterface);
};
class InteropMethodTableSlotDataMap
{
protected:
InteropMethodTableSlotData *m_pSlotData;
DWORD m_cSlotData;
DWORD m_iCurSlot;
public:
InteropMethodTableSlotDataMap(InteropMethodTableSlotData *pSlotData, DWORD cSlotData);
InteropMethodTableSlotData *GetData(MethodDesc *pMD);
BOOL Exists(MethodDesc *pMD);
protected:
InteropMethodTableSlotData *Exists_Helper(MethodDesc *pMD);
InteropMethodTableSlotData *GetNewEntry();
}; // class InteropMethodTableSlotDataMap
#endif // FEATURE_COMINTEROP
//
// This struct contains cached information on the GUID associated with a type.
//
struct GuidInfo
{
GUID m_Guid; // The actual guid of the type.
BOOL m_bGeneratedFromName; // A boolean indicating if it was generated from the
// name of the type.
};
typedef DPTR(GuidInfo) PTR_GuidInfo;
// GenericsDictInfo is stored at negative offset of the dictionary
struct GenericsDictInfo
{
#ifdef HOST_64BIT
DWORD m_dwPadding; // Just to keep the size a multiple of 8
#endif
// Total number of instantiation dictionaries including inherited ones
// i.e. how many instantiated classes (including this one) are there in the hierarchy?
// See comments about PerInstInfo
WORD m_wNumDicts;
// Number of type parameters (NOT including those of superclasses).
WORD m_wNumTyPars;
}; // struct GenericsDictInfo
typedef DPTR(GenericsDictInfo) PTR_GenericsDictInfo;
// These various statics structures exist directly before the MethodTableAuxiliaryData
// Any MethodTable which has static variables has this structure
struct DynamicStaticsInfo;
struct ThreadStaticsInfo;
struct GenericsStaticsInfo;
typedef DPTR(DynamicStaticsInfo) PTR_DynamicStaticsInfo;
typedef DPTR(ThreadStaticsInfo) PTR_ThreadStaticsInfo;
typedef DPTR(GenericsStaticsInfo) PTR_GenericsStaticsInfo;
//
// This struct consolidates the auxiliary parts of the MethodTable
// so that we can layout a variety of variable sized structures and
// access them from the MethodTable with a very small and simple set of
// indirections.
//
struct MethodTableAuxiliaryData
{
friend class MethodTable;
#if defined(DACCESS_COMPILE)
friend class NativeImageDumper;
#endif
enum
{
// AS YOU ADD NEW FLAGS PLEASE CONSIDER WHETHER Generics::NewInstantiation NEEDS
// TO BE UPDATED IN ORDER TO ENSURE THAT METHODTABLES DUPLICATED FOR GENERIC INSTANTIATIONS
// CARRY THE CORRECT INITIAL FLAGS.
enum_flag_Initialized = 0x0001,
enum_flag_HasCheckedCanCompareBitsOrUseFastGetHashCode = 0x0002, // Whether we have checked the overridden Equals or GetHashCode
enum_flag_CanCompareBitsOrUseFastGetHashCode = 0x0004, // Is any field type or sub field type overridden Equals or GetHashCode
enum_flag_IsTlsIndexAllocated = 0x0008,
enum_flag_HasApproxParent = 0x0010,
enum_flag_MayHaveOpenInterfaceInInterfaceMap = 0x0020,
enum_flag_IsNotFullyLoaded = 0x0040,
enum_flag_DependenciesLoaded = 0x0080, // class and all dependencies loaded up to CLASS_LOADED_BUT_NOT_VERIFIED
enum_flag_IsInitError = 0x0100,
enum_flag_IsStaticDataAllocated = 0x0200, // When this is set, if the class can be marked as initialized without any further code execution it will be.
enum_flag_HasCheckedStreamOverride = 0x0400,
enum_flag_StreamOverriddenRead = 0x0800,
enum_flag_StreamOverriddenWrite = 0x1000,
// unused enum = 0x2000,
// unused enum = 0x4000,
// unused enum = 0x8000,
};
union
{
DWORD m_dwFlags; // Lot of empty bits here.
struct
{
uint16_t m_loFlags;
int16_t m_offsetToNonVirtualSlots;
};
};
PTR_Module m_pLoaderModule;
// Non-unloadable context: internal RuntimeType object handle
// Unloadable context: slot index in LoaderAllocator's pinned table
RUNTIMETYPEHANDLE m_hExposedClassObject;
#ifdef _DEBUG
enum
{
// The MethodTable is in the right state to be published, and will be inevitably.
// Currently DEBUG only as it does not affect behavior in any way in a release build
enum_flagDebug_IsPublished = 0x2000,
enum_flagDebug_ParentMethodTablePointerValid = 0x4000,
enum_flagDebug_HasInjectedInterfaceDuplicates = 0x8000,
};
DWORD m_dwFlagsDebug;
// to avoid verify same method table too many times when it's not changing, we cache the GC count
// on which the method table is verified. When fast GC STRESS is turned on, we only verify the MT if
// current GC count is bigger than the number. Note most thing which will invalidate a MT will require a
// GC (like AD unload)
Volatile<DWORD> m_dwLastVerifedGCCnt;
#ifdef HOST_64BIT
DWORD m_dwPadding; // Just to keep the size a multiple of 8
#endif
// These pointers make it easier to examine the various statics structures in the debugger
PTR_DynamicStaticsInfo m_debugOnlyDynamicStatics;
PTR_GenericsStaticsInfo m_debugOnlyGenericStatics;
PTR_ThreadStaticsInfo m_debugOnlyThreadStatics;
#endif
public:
inline PTR_Module GetLoaderModule() const
{
return m_pLoaderModule;
}
#ifndef DACCESS_COMPILE
inline void SetLoaderModule(Module *pModule)
{
m_pLoaderModule = pModule;
}
#endif // DACCESS_COMPILE
#ifdef _DEBUG
inline BOOL IsParentMethodTablePointerValid() const
{
LIMITED_METHOD_DAC_CONTRACT;
return (m_dwFlagsDebug & enum_flagDebug_ParentMethodTablePointerValid);
}
inline void SetParentMethodTablePointerValid()
{
LIMITED_METHOD_CONTRACT;
m_dwFlagsDebug |= enum_flagDebug_ParentMethodTablePointerValid;
}
#endif
inline BOOL IsInitError() const
{
LIMITED_METHOD_DAC_CONTRACT;
return (VolatileLoad(&m_dwFlags) & enum_flag_IsInitError);
}
#ifndef DACCESS_COMPILE
inline void SetInitError()
{
LIMITED_METHOD_CONTRACT;
InterlockedOr((LONG*)&m_dwFlags, (LONG)enum_flag_IsInitError);
}
#endif
inline BOOL IsTlsIndexAllocated() const
{
LIMITED_METHOD_DAC_CONTRACT;
return (VolatileLoad(&m_dwFlags) & enum_flag_IsTlsIndexAllocated);
}
#ifndef DACCESS_COMPILE
inline void SetIsTlsIndexAllocated()
{
LIMITED_METHOD_CONTRACT;
InterlockedOr((LONG*)&m_dwFlags, (LONG)enum_flag_IsTlsIndexAllocated);
}
#endif
DWORD* getIsClassInitedFlagAddress()
{
LIMITED_METHOD_DAC_CONTRACT;
_ASSERTE(enum_flag_Initialized == 1); // This is an assumption in the JIT and in hand-written assembly at this time.
return &m_dwFlags;
}
inline BOOL IsClassInited() const
{
LIMITED_METHOD_DAC_CONTRACT;
return VolatileLoad(&m_dwFlags) & enum_flag_Initialized;
}
inline bool IsClassInitedOrPreinitedDecided(bool *initResult) const
{
LIMITED_METHOD_DAC_CONTRACT;
DWORD dwFlags = VolatileLoad(&m_dwFlags);
*initResult = m_dwFlags & enum_flag_Initialized;
return (dwFlags & (enum_flag_IsStaticDataAllocated|enum_flag_Initialized)) != 0;
}
#ifndef DACCESS_COMPILE
inline void SetClassInited()
{
LIMITED_METHOD_CONTRACT;
InterlockedOr((LONG*)&m_dwFlags, (LONG)enum_flag_Initialized);
}
#endif
inline BOOL IsStaticDataAllocated() const
{
LIMITED_METHOD_DAC_CONTRACT;
return (VolatileLoad(&m_dwFlags) & enum_flag_IsStaticDataAllocated);
}
#ifndef DACCESS_COMPILE
inline void SetIsStaticDataAllocated(bool markAsInitedToo)
{
LIMITED_METHOD_CONTRACT;
InterlockedOr((LONG*)&m_dwFlags, markAsInitedToo ? (LONG)(enum_flag_IsStaticDataAllocated|enum_flag_Initialized) : (LONG)enum_flag_IsStaticDataAllocated);
}
#endif
inline void SetStreamOverrideState(BOOL read, BOOL write)
{
LONG streamOverride =
enum_flag_HasCheckedStreamOverride
| (read ? enum_flag_StreamOverriddenRead : 0)
| (write ? enum_flag_StreamOverriddenWrite : 0);
InterlockedOr((LONG*)&m_dwFlags, streamOverride);
}
inline RUNTIMETYPEHANDLE GetExposedClassObjectHandle() const
{
LIMITED_METHOD_CONTRACT;
return m_hExposedClassObject;
}
void SetIsNotFullyLoadedForBuildMethodTable()
{
LIMITED_METHOD_CONTRACT;
// Used only during method table initialization - no need for logging or Interlocked Exchange.
m_dwFlags |= (MethodTableAuxiliaryData::enum_flag_IsNotFullyLoaded |
MethodTableAuxiliaryData::enum_flag_HasApproxParent);
}
void SetIsRestoredForBuildArrayMethodTable()
{
LIMITED_METHOD_CONTRACT;
// Array's parent is always precise
m_dwFlags &= ~(MethodTableAuxiliaryData::enum_flag_HasApproxParent);
}
#ifdef _DEBUG
#ifndef DACCESS_COMPILE
// Used in DEBUG builds to indicate that the MethodTable is in the right state to be published, and will be inevitably.
void SetIsPublished()
{
LIMITED_METHOD_CONTRACT;
m_dwFlagsDebug |= (MethodTableAuxiliaryData::enum_flagDebug_IsPublished);
}
#endif
// The MethodTable is in the right state to be published, and will be inevitably.
// Currently DEBUG only as it does not affect behavior in any way in a release build
bool IsPublished() const
{
LIMITED_METHOD_CONTRACT;
return (VolatileLoad(&m_dwFlagsDebug) & enum_flagDebug_IsPublished);
}
#endif // _DEBUG
// The NonVirtualSlots array grows backwards, so this pointer points at just AFTER the first entry in the array
// To access, use a construct like... GetNonVirtualSlotsArray(pAuxiliaryData)[-(1 + index)]
static inline PTR_PCODE GetNonVirtualSlotsArray(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData)
{
LIMITED_METHOD_DAC_CONTRACT;
return dac_cast<PTR_PCODE>(dac_cast<TADDR>(pAuxiliaryData) + pAuxiliaryData->GetOffsetToNonVirtualSlots());
}
inline int16_t GetOffsetToNonVirtualSlots() const
{
return m_offsetToNonVirtualSlots;
}
inline void SetOffsetToNonVirtualSlots(int16_t offset)
{
m_offsetToNonVirtualSlots = offset;
}
static inline PTR_DynamicStaticsInfo GetDynamicStaticsInfo(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData);
static inline PTR_GenericsStaticsInfo GetGenericStaticsInfo(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData);
static inline PTR_ThreadStaticsInfo GetThreadStaticsInfo(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData);
inline void SetMayHaveOpenInterfacesInInterfaceMap()
{
LIMITED_METHOD_CONTRACT;
InterlockedOr((LONG*)&m_dwFlags, MethodTableAuxiliaryData::enum_flag_MayHaveOpenInterfaceInInterfaceMap);
}
inline bool MayHaveOpenInterfacesInInterfaceMap() const
{
return !!(m_dwFlags & MethodTableAuxiliaryData::enum_flag_MayHaveOpenInterfaceInInterfaceMap);
}
}; // struct MethodTableAuxiliaryData
// All MethodTables which have static variables will have one of these. It contains the pointers necessary to
// find the normal (non-thread) static variables of the type.
struct DynamicStaticsInfo
{
private:
// The detail of whether or not the class has been initialized is stored in the statics pointers as well as in
// its normal flag location. This is done so that when getting the statics base for a class, we can get the statics
// base address and check to see if it is initialized without needing a barrier between reading the flag and reading
// the static field address.
static constexpr TADDR ISCLASSNOTINITED = 1;
static constexpr TADDR ISCLASSNOTINITEDMASK = ISCLASSNOTINITED;
static constexpr TADDR STATICSPOINTERMASK = ~ISCLASSNOTINITEDMASK;
void InterlockedSetClassInited(bool isGC)
{
TADDR oldVal;
TADDR oldValFromInterlockedOp;
TADDR *pAddr = isGC ? &m_pGCStatics : &m_pNonGCStatics;
do
{
oldVal = VolatileLoadWithoutBarrier(pAddr);
// Mask off the ISCLASSNOTINITED bit
oldValFromInterlockedOp = InterlockedCompareExchangeT(pAddr, oldVal & STATICSPOINTERMASK, oldVal);
} while(oldValFromInterlockedOp != oldVal); // We can loop if we happened to allocate the statics pointer in the middle of this operation
}
public:
TADDR m_pGCStatics; // Always access through helper methods to properly handle the ISCLASSNOTINITED bit
TADDR m_pNonGCStatics; // Always access through helper methods to properly handle the ISCLASSNOTINITED bit
PTR_MethodTable m_pMethodTable;
PTR_OBJECTREF GetGCStaticsPointer() { TADDR staticsVal = VolatileLoadWithoutBarrier(&m_pGCStatics); return dac_cast<PTR_OBJECTREF>(staticsVal & STATICSPOINTERMASK); }
PTR_BYTE GetNonGCStaticsPointer() { TADDR staticsVal = VolatileLoadWithoutBarrier(&m_pNonGCStatics); return dac_cast<PTR_BYTE>(staticsVal & STATICSPOINTERMASK); }
PTR_OBJECTREF GetGCStaticsPointerAssumeIsInited() { TADDR staticsVal = m_pGCStatics; _ASSERTE(staticsVal != 0); _ASSERTE((staticsVal & (ISCLASSNOTINITEDMASK)) == 0); return dac_cast<PTR_OBJECTREF>(staticsVal); }
PTR_BYTE GetNonGCStaticsPointerAssumeIsInited() { TADDR staticsVal = m_pNonGCStatics; _ASSERTE(staticsVal != 0); _ASSERTE((staticsVal & (ISCLASSNOTINITEDMASK)) == 0); return dac_cast<PTR_BYTE>(staticsVal); }
bool GetIsInitedAndGCStaticsPointerIfInited(PTR_OBJECTREF *ptrResult) { TADDR staticsVal = VolatileLoadWithoutBarrier(&m_pGCStatics); *ptrResult = dac_cast<PTR_OBJECTREF>(staticsVal); return !(staticsVal & ISCLASSNOTINITED); }
bool GetIsInitedAndNonGCStaticsPointerIfInited(PTR_BYTE *ptrResult) { TADDR staticsVal = VolatileLoadWithoutBarrier(&m_pNonGCStatics); *ptrResult = dac_cast<PTR_BYTE>(staticsVal); return !(staticsVal & ISCLASSNOTINITED); }
// This function sets the pointer portion of a statics pointer. It returns false if the statics value was already set.
bool InterlockedUpdateStaticsPointer(bool isGC, TADDR newVal, bool isClassInitedByUpdatingStaticPointer)
{
TADDR oldVal;
TADDR oldValFromInterlockedOp;
TADDR *pAddr = isGC ? &m_pGCStatics : &m_pNonGCStatics;
do
{
oldVal = VolatileLoad(pAddr);
// Check to see if statics value has already been set
if ((oldVal & STATICSPOINTERMASK) != 0)
{
// If it has, then we don't need to do anything
return false;
}
if (isClassInitedByUpdatingStaticPointer)
{
oldValFromInterlockedOp = InterlockedCompareExchangeT(pAddr, newVal, oldVal);
}
else
{
oldValFromInterlockedOp = InterlockedCompareExchangeT(pAddr, newVal | oldVal, oldVal);
}
} while(oldValFromInterlockedOp != oldVal);
return true;
}
void SetClassInited()
{
InterlockedSetClassInited(true);
InterlockedSetClassInited(false);
}
#ifndef DACCESS_COMPILE
void Init(MethodTable* pMT)
{
m_pGCStatics = ISCLASSNOTINITED;
m_pNonGCStatics = ISCLASSNOTINITED;
m_pMethodTable = pMT;
}
#endif
PTR_MethodTable GetMethodTable() const { return m_pMethodTable; }
};
/* static */ inline PTR_DynamicStaticsInfo MethodTableAuxiliaryData::GetDynamicStaticsInfo(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData)
{
return dac_cast<PTR_DynamicStaticsInfo>(dac_cast<TADDR>(pAuxiliaryData) - sizeof(DynamicStaticsInfo));
}
// Any Generic MethodTable which has static variables has this structure. Note that it ends
// with a DynamicStatics structure so that lookups for just DynamicStatics will find that structure
// when looking for statics pointers
// In addition, for simplicity in access, all MethodTables which have a ThreadStaticsInfo have this structure
// but it is unitialized and should not be used if the type is not generic
struct GenericsStaticsInfo
{
// Pointer to field descs for statics
PTR_FieldDesc m_pFieldDescs;
DynamicStaticsInfo m_DynamicStatics;
}; // struct GenericsStaticsInfo
/* static */ inline PTR_GenericsStaticsInfo MethodTableAuxiliaryData::GetGenericStaticsInfo(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData)
{
return dac_cast<PTR_GenericsStaticsInfo>(dac_cast<TADDR>(pAuxiliaryData) - sizeof(GenericsStaticsInfo));
}
// And MethodTable with Thread Statics has this structure. NOTE: This structure includes
// GenericsStatics which may not actually have the m_pFieldDescs filled in if the MethodTable
// is not actually Generic
struct ThreadStaticsInfo
{
TLSIndex NonGCTlsIndex;
TLSIndex GCTlsIndex;
GenericsStaticsInfo m_genericStatics;
void Init()
{
NonGCTlsIndex = TLSIndex::Unallocated();
GCTlsIndex = TLSIndex::Unallocated();
}
};
/* static */ inline PTR_ThreadStaticsInfo MethodTableAuxiliaryData::GetThreadStaticsInfo(PTR_Const_MethodTableAuxiliaryData pAuxiliaryData)
{
return dac_cast<PTR_ThreadStaticsInfo>(dac_cast<TADDR>(pAuxiliaryData) - sizeof(ThreadStaticsInfo));
}
#ifdef UNIX_AMD64_ABI_ITF
inline
SystemVClassificationType CorInfoType2UnixAmd64Classification(CorElementType eeType)
{
static const SystemVClassificationType toSystemVAmd64ClassificationTypeMap[] = {
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_END
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_VOID
SystemVClassificationTypeInteger, // ELEMENT_TYPE_BOOLEAN
SystemVClassificationTypeInteger, // ELEMENT_TYPE_CHAR
SystemVClassificationTypeInteger, // ELEMENT_TYPE_I1
SystemVClassificationTypeInteger, // ELEMENT_TYPE_U1
SystemVClassificationTypeInteger, // ELEMENT_TYPE_I2
SystemVClassificationTypeInteger, // ELEMENT_TYPE_U2
SystemVClassificationTypeInteger, // ELEMENT_TYPE_I4
SystemVClassificationTypeInteger, // ELEMENT_TYPE_U4
SystemVClassificationTypeInteger, // ELEMENT_TYPE_I8
SystemVClassificationTypeInteger, // ELEMENT_TYPE_U8
SystemVClassificationTypeSSE, // ELEMENT_TYPE_R4
SystemVClassificationTypeSSE, // ELEMENT_TYPE_R8
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_STRING
SystemVClassificationTypeInteger, // ELEMENT_TYPE_PTR
SystemVClassificationTypeIntegerByRef, // ELEMENT_TYPE_BYREF
SystemVClassificationTypeStruct, // ELEMENT_TYPE_VALUETYPE
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_CLASS
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_VAR (type variable)
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_ARRAY
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_GENERICINST
SystemVClassificationTypeStruct, // ELEMENT_TYPE_TYPEDBYREF
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_VALUEARRAY_UNSUPPORTED
SystemVClassificationTypeInteger, // ELEMENT_TYPE_I
SystemVClassificationTypeInteger, // ELEMENT_TYPE_U
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_R_UNSUPPORTED
// put the correct type when we know our implementation
SystemVClassificationTypeInteger, // ELEMENT_TYPE_FNPTR
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_OBJECT
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_SZARRAY
SystemVClassificationTypeIntegerReference, // ELEMENT_TYPE_MVAR
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_CMOD_REQD
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_CMOD_OPT
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_INTERNAL
SystemVClassificationTypeUnknown, // ELEMENT_TYPE_CMOD_INTERNAL
};
_ASSERTE(sizeof(toSystemVAmd64ClassificationTypeMap) == ELEMENT_TYPE_MAX);
_ASSERTE(eeType < (CorElementType) sizeof(toSystemVAmd64ClassificationTypeMap));
// spot check of the map
_ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_I4] == SystemVClassificationTypeInteger);
_ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_PTR] == SystemVClassificationTypeInteger);
_ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_VALUETYPE] == SystemVClassificationTypeStruct);
_ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_TYPEDBYREF] == SystemVClassificationTypeStruct);
_ASSERTE((SystemVClassificationType)toSystemVAmd64ClassificationTypeMap[ELEMENT_TYPE_BYREF] == SystemVClassificationTypeIntegerByRef);
return (((unsigned)eeType) < ELEMENT_TYPE_MAX) ? (toSystemVAmd64ClassificationTypeMap[(unsigned)eeType]) : SystemVClassificationTypeUnknown;
};
#define SYSTEMV_EIGHT_BYTE_SIZE_IN_BYTES 8 // Size of an eightbyte in bytes.
#define SYSTEMV_MAX_NUM_FIELDS_IN_REGISTER_PASSED_STRUCT 16 // Maximum number of fields in struct passed in registers
struct SystemVStructRegisterPassingHelper
{
SystemVStructRegisterPassingHelper(unsigned int totalStructSize) :
structSize(totalStructSize),
eightByteCount(0),
inEmbeddedStruct(false),
currentUniqueOffsetField(0),
largestFieldOffset(-1)
{
for (int i = 0; i < CLR_SYSTEMV_MAX_EIGHTBYTES_COUNT_TO_PASS_IN_REGISTERS; i++)
{
eightByteClassifications[i] = SystemVClassificationTypeNoClass;
eightByteSizes[i] = 0;
eightByteOffsets[i] = 0;
}
// Initialize the work arrays
for (int i = 0; i < SYSTEMV_MAX_NUM_FIELDS_IN_REGISTER_PASSED_STRUCT; i++)
{
fieldClassifications[i] = SystemVClassificationTypeNoClass;
fieldSizes[i] = 0;
fieldOffsets[i] = 0;
}
}
// Input state.
unsigned int structSize;
// These fields are the output; these are what is computed by the classification algorithm.
unsigned int eightByteCount;
SystemVClassificationType eightByteClassifications[CLR_SYSTEMV_MAX_EIGHTBYTES_COUNT_TO_PASS_IN_REGISTERS];
unsigned int eightByteSizes[CLR_SYSTEMV_MAX_EIGHTBYTES_COUNT_TO_PASS_IN_REGISTERS];
unsigned int eightByteOffsets[CLR_SYSTEMV_MAX_EIGHTBYTES_COUNT_TO_PASS_IN_REGISTERS];
// Helper members to track state.
bool inEmbeddedStruct;
unsigned int currentUniqueOffsetField; // A virtual field that could encompass many overlapping fields.
int largestFieldOffset;
SystemVClassificationType fieldClassifications[SYSTEMV_MAX_NUM_FIELDS_IN_REGISTER_PASSED_STRUCT];
unsigned int fieldSizes[SYSTEMV_MAX_NUM_FIELDS_IN_REGISTER_PASSED_STRUCT];
unsigned int fieldOffsets[SYSTEMV_MAX_NUM_FIELDS_IN_REGISTER_PASSED_STRUCT];
};
typedef DPTR(SystemVStructRegisterPassingHelper) SystemVStructRegisterPassingHelperPtr;
#endif // UNIX_AMD64_ABI_ITF
#if defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
// Bitfields for FpStructInRegistersInfo::flags
namespace FpStruct
{
enum Flags
{
// Positions of flags and bitfields
PosOnlyOne = 0,
PosBothFloat = 1,
PosFloatInt = 2,
PosIntFloat = 3,
PosSizeShift1st = 4, // 2 bits
PosSizeShift2nd = 6, // 2 bits
UseIntCallConv = 0, // struct is passed according to integer calling convention
// The flags and bitfields
OnlyOne = 1 << PosOnlyOne, // has only one field, which is floating-point
BothFloat = 1 << PosBothFloat, // has two fields, both are floating-point
FloatInt = 1 << PosFloatInt, // has two fields, 1st is floating and 2nd is integer
IntFloat = 1 << PosIntFloat, // has two fields, 2nd is floating and 1st is integer
SizeShift1stMask = 0b11 << PosSizeShift1st, // log2(size) of 1st field
SizeShift2ndMask = 0b11 << PosSizeShift2nd, // log2(size) of 2nd field
// Note: flags OnlyOne, BothFloat, FloatInt, and IntFloat are mutually exclusive
};
}
// On RISC-V and LoongArch a struct with up to two non-empty fields, at least one of them floating-point,
// can be passed in registers according to hardware FP calling convention. FpStructInRegistersInfo represents
// passing information for such parameters.
struct FpStructInRegistersInfo
{
FpStruct::Flags flags;
uint32_t offset1st;
uint32_t offset2nd;
unsigned SizeShift1st() const { return (flags >> FpStruct::PosSizeShift1st) & 0b11; }
unsigned SizeShift2nd() const { return (flags >> FpStruct::PosSizeShift2nd) & 0b11; }
unsigned Size1st() const { return 1u << SizeShift1st(); }
unsigned Size2nd() const { return 1u << SizeShift2nd(); }
const char* FlagName() const
{
switch (flags & (FpStruct::OnlyOne | FpStruct::BothFloat | FpStruct::FloatInt | FpStruct::IntFloat))
{
case FpStruct::OnlyOne: return "OnlyOne";
case FpStruct::BothFloat: return "BothFloat";
case FpStruct::FloatInt: return "FloatInt";
case FpStruct::IntFloat: return "IntFloat";
default: return "?";
}
}
};
#endif // defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
//===============================================================================================
//
// GC data appears before the beginning of the MethodTable
//
//@GENERICS:
// Each generic type has a corresponding "generic" method table that serves the following
// purposes:
// * The method table pointer is used as a representative for the generic type e.g. in reflection
// * MethodDescs for methods in the vtable are used for reflection; they should never be invoked.
// Some other information (e.g. BaseSize) makes no sense "generically" but unfortunately gets put in anyway.
//
// Each distinct instantiation of a generic type has its own MethodTable structure.
// However, the EEClass structure can be shared between compatible instantiations e.g. List<string> and List<object>.
// In that case, MethodDescs are also shared between compatible instantiations (but see below about generic methods).
// Hence the vtable entries for MethodTables belonging to such an EEClass are the same.
//
// The non-vtable section of such MethodTables are only present for one of the instantiations (the first one
// requested) as non-vtable entries are never accessed through the vtable pointer of an object so it's always possible
// to ensure that they are accessed through the representative MethodTable that contains them.
// A MethodTable is the fundamental representation of type in the runtime. It is this structure that
// objects point at (see code:Object). It holds the size and GC layout of the type, as well as the dispatch table
// for virtual dispach (but not interface dispatch). There is a distinct method table for every instance of
// a generic type. From here you can get to
//
// * code:EEClass
//
// Important fields
// * code:MethodTable.m_pEEClass - pointer to the cold part of the type.
// * code:MethodTable.m_pParentMethodTable - the method table of the parent type.
//
class MethodTableBuilder;
class MethodTable
{
/************************************
* FRIEND FUNCTIONS
************************************/
// DO NOT ADD FRIENDS UNLESS ABSOLUTELY NECESSARY
// USE ACCESSORS TO READ/WRITE private field members
// Special access for setting up String object method table correctly
friend class ClassLoader;
friend class JIT_TrialAlloc;
friend class Module;
friend class EEClass;
friend class MethodTableBuilder;
friend class CheckAsmOffsets;
#if defined(DACCESS_COMPILE)
friend class NativeImageDumper;
#endif
public:
// Do some sanity checking to make sure it's a method table
// and not pointing to some random memory. In particular
// check that (apart from the special case of instantiated generic types) we have
// GetCanonicalMethodTable() == this;
BOOL SanityCheck();
static void CallFinalizer(Object *obj);
public:
PTR_Module GetModule()
{
LIMITED_METHOD_CONTRACT;
return m_pModule;
}
#ifndef DACCESS_COMPILE
void SetModule(Module* pModule)
{
LIMITED_METHOD_CONTRACT;
m_pModule = pModule;
}
#endif
Assembly *GetAssembly();
PTR_Module GetModuleIfLoaded();
// For regular, non-constructed types, GetLoaderModule() == GetModule()
// For constructed types (e.g. int[], Dict<int[], C>) the hash table through which a type
// is accessed lives in a "loader module". The rule for determining the loader module must ensure
// that a type never outlives its loader module with respect to app-domain unloading
//
PTR_Module GetLoaderModule();
PTR_LoaderAllocator GetLoaderAllocator();
void SetLoaderAllocator(LoaderAllocator* pAllocator);
MethodTable *LoadEnclosingMethodTable(ClassLoadLevel targetLevel = CLASS_DEPENDENCIES_LOADED);
LPCWSTR GetPathForErrorMessages();
//-------------------------------------------------------------------
// COM INTEROP
//
#ifdef FEATURE_COMINTEROP
TypeHandle GetCoClassForInterface();
private:
TypeHandle SetupCoClassForInterface();
public:
DWORD IsComClassInterface();
// Retrieves the COM interface type.
CorIfaceAttr GetComInterfaceType();
void SetComInterfaceType(CorIfaceAttr ItfType);
OBJECTHANDLE GetOHDelegate();
void SetOHDelegate (OBJECTHANDLE _ohDelegate);
CorClassIfaceAttr GetComClassInterfaceType();
TypeHandle GetDefItfForComClassItf();
void GetEventInterfaceInfo(MethodTable **ppSrcItfType, MethodTable **ppEvProvType);
BOOL IsExtensibleRCW();
// Helper to get parent class skipping over COM class in
// the hierarchy
MethodTable* GetComPlusParentMethodTable();
DWORD IsComImport();
// class is a special COM event interface
int IsComEventItfType();
//-------------------------------------------------------------------
// Sparse VTables. These require a SparseVTableMap in the EEClass in
// order to record how the CLR's vtable slots map across to COM
// Interop slots.
//
int IsSparseForCOMInterop();