forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsload.hpp
1001 lines (824 loc) · 40 KB
/
clsload.hpp
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: clsload.hpp
//
//
//
// ============================================================================
#ifndef _H_CLSLOAD
#define _H_CLSLOAD
#include "crst.h"
#include "eehash.h"
#include "vars.hpp"
#include "stubmgr.h"
#include "typehandle.h"
#include "object.h" // only needed for def. of PTRARRAYREF
#include "classloadlevel.h"
#include "specstrings.h"
#include "simplerwlock.hpp"
#include "classhash.h"
// SystemDomain is a friend of ClassLoader.
class SystemDomain;
class Assembly;
class ClassLoader;
class TypeKey;
class PendingTypeLoadTable;
class EEClass;
class Thread;
class EETypeHashTable;
class DynamicResolver;
class SigPointer;
// Hash table parameter for unresolved class hash
#define UNRESOLVED_CLASS_HASH_BUCKETS 8
// This is information required to look up a type in the loader. Besides the
// basic name there is the meta data information for the type, whether the
// the name is case sensitive, and tokens not to load. This last item allows
// the loader to prevent a type from being recursively loaded.
typedef enum NameHandleTable
{
nhCaseSensitive = 0,
nhCaseInsensitive = 1
} NameHandleTable;
class HashedTypeEntry
{
public:
typedef enum
{
IsNullEntry, // Uninitialized HashedTypeEntry
IsHashedTokenEntry, // Entry is a token value in a R2R hashtable in from the R2R module
IsHashedClassEntry // Entry is a EEClassHashEntry_t from the hashtable constructed at
// module load time
} EntryType;
typedef struct
{
mdToken m_TypeToken;
Module * m_pModule;
} TokenTypeEntry;
private:
EntryType m_EntryType;
PTR_EEClassHashEntry m_pClassHashEntry;
TokenTypeEntry m_TokenAndModulePair;
public:
HashedTypeEntry()
{
m_EntryType = EntryType::IsNullEntry;
m_pClassHashEntry = PTR_NULL;
}
EntryType GetEntryType() const { return m_EntryType; }
bool IsNull() const { return m_EntryType == EntryType::IsNullEntry; }
const HashedTypeEntry& SetClassHashBasedEntryValue(PTR_EEClassHashEntry pClassHashEntry)
{
LIMITED_METHOD_CONTRACT;
m_EntryType = EntryType::IsHashedClassEntry;
m_pClassHashEntry = pClassHashEntry;
return *this;
}
PTR_EEClassHashEntry GetClassHashBasedEntryValue() const
{
LIMITED_METHOD_CONTRACT;
_ASSERT(m_EntryType == EntryType::IsHashedClassEntry);
return m_pClassHashEntry;
}
const HashedTypeEntry& SetTokenBasedEntryValue(mdTypeDef typeToken, Module * pModule)
{
LIMITED_METHOD_CONTRACT;
m_EntryType = EntryType::IsHashedTokenEntry;
m_TokenAndModulePair.m_TypeToken = typeToken;
m_TokenAndModulePair.m_pModule = pModule;
return *this;
}
const TokenTypeEntry& GetTokenBasedEntryValue() const
{
LIMITED_METHOD_CONTRACT;
_ASSERT(m_EntryType == EntryType::IsHashedTokenEntry);
return m_TokenAndModulePair;
}
};
class NameHandle
{
friend class ClassLoader;
LPCUTF8 m_nameSpace;
LPCUTF8 m_name;
PTR_ModuleBase m_pTypeScope;
mdToken m_mdType;
mdToken m_mdTokenNotToLoad;
NameHandleTable m_WhichTable;
HashedTypeEntry m_Bucket;
public:
NameHandle()
{
LIMITED_METHOD_CONTRACT;
memset((void*) this, 0, sizeof(*this));
}
NameHandle(LPCUTF8 nameSpace, LPCUTF8 name) :
m_nameSpace(nameSpace),
m_name(name),
m_pTypeScope(PTR_NULL),
m_mdType(mdTokenNil),
m_mdTokenNotToLoad(tdNoTypes),
m_WhichTable(nhCaseSensitive),
m_Bucket()
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
_ASSERTE(nameSpace != NULL);
_ASSERTE(name != NULL);
}
NameHandle(ModuleBase* pModule, mdToken token);
NameHandle(Module* pModule, mdToken token);
NameHandle(const NameHandle & p)
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
m_nameSpace = p.m_nameSpace;
m_name = p.m_name;
m_pTypeScope = p.m_pTypeScope;
m_mdType = p.m_mdType;
m_mdTokenNotToLoad = p.m_mdTokenNotToLoad;
m_WhichTable = p.m_WhichTable;
m_Bucket = p.m_Bucket;
}
void SetName(LPCUTF8 pNameSpace, LPCUTF8 pName)
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC_HOST_ONLY;
_ASSERTE(pNameSpace != NULL);
_ASSERTE(pName != NULL);
m_nameSpace = pNameSpace;
m_name = pName;
}
LPCUTF8 GetName() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_name;
}
LPCUTF8 GetNameSpace() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_nameSpace;
}
#ifndef DACCESS_COMPILE
void SetTypeToken(Module* pModule, mdToken mdToken)
{
LIMITED_METHOD_CONTRACT;
m_pTypeScope = dac_cast<PTR_ModuleBase>(pModule);
m_mdType = mdToken;
}
#endif
PTR_ModuleBase GetTypeModule() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_pTypeScope;
}
mdToken GetTypeToken() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_mdType;
}
void SetTokenNotToLoad(mdToken mdtok)
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC; // "this" must be a host address
m_mdTokenNotToLoad = mdtok;
}
mdToken GetTokenNotToLoad() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_mdTokenNotToLoad;
}
void SetCaseInsensitive()
{
LIMITED_METHOD_CONTRACT;
m_WhichTable = nhCaseInsensitive;
}
NameHandleTable GetTable() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_WhichTable;
}
void SetBucket(const HashedTypeEntry& bucket)
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC; // "this" must be a host address
m_Bucket = bucket;
}
const HashedTypeEntry& GetBucket() const
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return m_Bucket;
}
static BOOL OKToLoad(mdToken token, mdToken tokenNotToLoad)
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
return (token == 0 || token != tokenNotToLoad) && tokenNotToLoad != tdAllTypes;
}
BOOL OKToLoad() const
{
WRAPPER_NO_CONTRACT;
SUPPORTS_DAC;
return OKToLoad(m_mdType, m_mdTokenNotToLoad);
}
};
//******************************************************************************
// AccessCheckContext encapsulates input of an accessibility check
class AccessCheckContext
{
public:
AccessCheckContext(MethodDesc* pCallerMethod, MethodTable* pCallerType, Assembly* pCallerAssembly)
: m_pCallerMethod(pCallerMethod),
m_pCallerMT(pCallerType),
m_pCallerAssembly(pCallerAssembly)
{
CONTRACTL
{
LIMITED_METHOD_CONTRACT;
PRECONDITION(CheckPointer(pCallerMethod, NULL_OK));
PRECONDITION(CheckPointer(pCallerType, NULL_OK));
PRECONDITION(CheckPointer(pCallerAssembly));
}
CONTRACTL_END;
}
AccessCheckContext(MethodDesc* pCallerMethod);
AccessCheckContext(MethodDesc* pCallerMethod, MethodTable* pCallerType);
MethodDesc* GetCallerMethod()
{
LIMITED_METHOD_CONTRACT;
return m_pCallerMethod;
}
MethodTable* GetCallerMT()
{
LIMITED_METHOD_CONTRACT;
return m_pCallerMT;
}
Assembly* GetCallerAssembly()
{
WRAPPER_NO_CONTRACT;
return m_pCallerAssembly;
}
private:
MethodDesc* m_pCallerMethod;
MethodTable* m_pCallerMT;
Assembly* m_pCallerAssembly;
};
//******************************************************************************
// This type specifies the kind of accessibility checks to perform.
// On failure, it can be configured to either return FALSE or to throw an exception.
class AccessCheckOptions
{
public:
enum AccessCheckType
{
// Used by statically compiled code.
// Desktop: Just do normal accessibility checks. No security demands.
// CoreCLR: Just do normal accessibility checks.
kNormalAccessibilityChecks,
// Used only for resource loading and reflection inovcation when the target is remoted.
// Desktop: If normal accessiblity checks fail, return TRUE if a demand for MemberAccess succeeds
// CoreCLR: If normal accessiblity checks fail, return TRUE if a the caller is Security(Safe)Critical
kMemberAccess,
// Used by Reflection invocation and DynamicMethod with RestrictedSkipVisibility.
// Desktop: If normal accessiblity checks fail, return TRUE if a demand for RestrictedMemberAccess
// and grant set of the target assembly succeeds.
// CoreCLR: If normal accessiblity checks fail, return TRUE if the callee is App transparent code (in a user assembly)
kRestrictedMemberAccess,
// Used by normal DynamicMethods in full trust CoreCLR
// CoreCLR: Do normal visibility checks but bypass transparency checks.
kNormalAccessNoTransparency,
// Used by DynamicMethods with restrictedSkipVisibility in full trust CoreCLR
// CoreCLR: Do RestrictedMemberAccess visibility checks but bypass transparency checks.
kRestrictedMemberAccessNoTransparency,
};
AccessCheckOptions(
AccessCheckType accessCheckType,
DynamicResolver * pAccessContext,
BOOL throwIfTargetIsInaccessible,
MethodTable * pTargetMT);
AccessCheckOptions(
AccessCheckType accessCheckType,
DynamicResolver * pAccessContext,
BOOL throwIfTargetIsInaccessible,
MethodDesc * pTargetMD);
AccessCheckOptions(
AccessCheckType accessCheckType,
DynamicResolver * pAccessContext,
BOOL throwIfTargetIsInaccessible,
FieldDesc * pTargetFD);
AccessCheckOptions(
const AccessCheckOptions & templateAccessCheckOptions,
BOOL throwIfTargetIsInaccessible);
// Follow standard rules for doing accessability
BOOL DoNormalAccessibilityChecks() const
{
LIMITED_METHOD_CONTRACT;
return m_accessCheckType == kNormalAccessibilityChecks;
}
BOOL Throws() const
{
LIMITED_METHOD_CONTRACT;
return m_fThrowIfTargetIsInaccessible;
}
BOOL DemandMemberAccessOrFail(AccessCheckContext *pContext, MethodTable * pTargetMT, BOOL visibilityCheck) const;
BOOL FailOrThrow(AccessCheckContext *pContext) const;
static AccessCheckOptions* s_pNormalAccessChecks;
static void Startup();
private:
void Initialize(
AccessCheckType accessCheckType,
BOOL throwIfTargetIsInaccessible,
MethodTable * pTargetMT,
MethodDesc * pTargetMD,
FieldDesc * pTargetFD);
BOOL DemandMemberAccess(AccessCheckContext *pContext, MethodTable * pTargetMT, BOOL visibilityCheck) const;
void ThrowAccessException(
AccessCheckContext* pContext,
MethodTable* pFailureMT = NULL,
Exception* pInnerException = NULL) const;
MethodTable * m_pTargetMT;
MethodDesc * m_pTargetMethod;
FieldDesc * m_pTargetField;
AccessCheckType m_accessCheckType;
// The context used to determine if access is allowed. It is the resolver that carries the compressed-stack used to do the Demand.
// If this is NULL, the access is checked against the current call-stack.
// This is non-NULL only for m_accessCheckType==kRestrictedMemberAccess
DynamicResolver * m_pAccessContext;
// If the target is not accessible, should the API return FALSE, or should it throw an exception?
BOOL m_fThrowIfTargetIsInaccessible;
};
void DECLSPEC_NORETURN ThrowFieldAccessException(MethodDesc *pCallerMD,
FieldDesc *pFD,
UINT messageID = 0,
Exception *pInnerException = NULL);
void DECLSPEC_NORETURN ThrowMethodAccessException(MethodDesc *pCallerMD,
MethodDesc *pCalleeMD,
UINT messageID = 0,
Exception *pInnerException = NULL);
void DECLSPEC_NORETURN ThrowTypeAccessException(MethodDesc *pCallerMD,
MethodTable *pMT,
UINT messageID = 0,
Exception *pInnerException = NULL);
void DECLSPEC_NORETURN ThrowFieldAccessException(AccessCheckContext* pContext,
FieldDesc *pFD,
UINT messageID = 0,
Exception *pInnerException = NULL);
void DECLSPEC_NORETURN ThrowMethodAccessException(AccessCheckContext* pContext,
MethodDesc *pCalleeMD,
UINT messageID = 0,
Exception *pInnerException = NULL);
void DECLSPEC_NORETURN ThrowTypeAccessException(AccessCheckContext* pContext,
MethodTable *pMT,
UINT messageID = 0,
Exception *pInnerException = NULL);
//---------------------------------------------------------------------------------------
//
class ClassLoader
{
friend class PendingTypeLoadTable;
friend class MethodTableBuilder;
friend class AppDomain;
friend class Assembly;
friend class Module;
friend class InstantiatedMethodDesc;
// the following two classes are friends because they will call LoadTypeHandleForTypeKey by token directly
friend class COMDynamicWrite;
friend class COMModule;
private:
// Protects addition of elements to module's m_pAvailableClasses.
// (indeed thus protects addition of elements to any m_pAvailableClasses in any
// of the modules managed by this loader)
CrstExplicitInit m_AvailableClassLock;
CrstExplicitInit m_AvailableTypesLock;
// Do we have any modules which need to have their classes added to
// the available list?
Volatile<LONG> m_cUnhashedModules;
// Back reference to the assembly
PTR_Assembly m_pAssembly;
public:
#ifdef _DEBUG
DWORD m_dwDebugMethods;
DWORD m_dwDebugFieldDescs; // Doesn't include anything we don't allocate a FieldDesc for
DWORD m_dwDebugClasses;
DWORD m_dwDebugDuplicateInterfaceSlots;
DWORD m_dwGCSize;
DWORD m_dwInterfaceMapSize;
DWORD m_dwMethodTableSize;
DWORD m_dwVtableData;
DWORD m_dwStaticFieldData;
DWORD m_dwFieldDescData;
DWORD m_dwMethodDescData;
size_t m_dwEEClassData;
#endif
public:
ClassLoader(Assembly *pAssembly);
~ClassLoader();
private:
VOID PopulateAvailableClassHashTable(Module *pModule,
AllocMemTracker *pamTracker);
void LazyPopulateCaseSensitiveHashTablesDontHaveLock();
void LazyPopulateCaseSensitiveHashTables();
void LazyPopulateCaseInsensitiveHashTables();
// Lookup the hash table entry from the hash table
void GetClassValue(NameHandleTable nhTable,
const NameHandle *pName,
HashDatum *pData,
EEClassHashTable **ppTable,
Module* pLookInThisModuleOnly,
HashedTypeEntry* pFoundEntry,
Loader::LoadFlag loadFlag,
BOOL& needsToBuildHashtable);
public:
//#LoaderModule
// LoaderModule determines in which module an item gets placed.
// For everything except parameterized types and methods the choice is easy.
//
// If NGEN'ing we may choose to place the item into the current module (which is different from runtime behavior).
//
// The rule for determining the loader module must ensure that a type or method never outlives its loader module
// with respect to app-domain unloading
static Module * ComputeLoaderModule(MethodTable * pMT,
mdToken token, // the token of the method
Instantiation methodInst); // the type arguments to the method (if any)
static Module * ComputeLoaderModule(const TypeKey * typeKey);
inline static PTR_Module ComputeLoaderModuleForFunctionPointer(TypeHandle * pRetAndArgTypes, DWORD NumArgsPlusRetType);
inline static PTR_Module ComputeLoaderModuleForParamType(TypeHandle paramType);
private:
static PTR_Module ComputeLoaderModuleWorker(Module *pDefinitionModule, // the module that declares the generic type or method
mdToken token,
Instantiation classInst, // the type arguments to the type (if any)
Instantiation methodInst); // the type arguments to the method (if any)
BOOL FindClassModuleThrowing(
const NameHandle * pName,
TypeHandle * pType,
mdToken * pmdClassToken,
Module ** ppModule,
mdToken * pmdFoundExportedType,
HashedTypeEntry * pEntry,
Module * pLookInThisModuleOnly,
Loader::LoadFlag loadFlag);
public:
void Init(AllocMemTracker *pamTracker);
PTR_Assembly GetAssembly();
void FreeModules();
#ifdef DACCESS_COMPILE
void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
#endif
//==================================================================================
// Main entry points to class loader
// Organized as follows:
// by token:
// TypeDef
// TypeDefOrRef
// TypeDefOrRefOrSpec
// by constructed type:
// ArrayType
// PointerOrByrefType
// FnPtrType
// GenericInstantiation
// by name:
// ByName
// Each takes a parameter comes, with the following semantics:
// fLoadTypes=DontLoadTypes: if type isn't already in the loader's table, return NULL
// fLoadTypes=LoadTypes: if type isn't already in the loader's table, then create it
// Each comes in two variants, LoadXThrowing and LoadXNoThrow, the latter being just
// an exception-handling wrapper around the former.
//
// Each also allows types to be loaded only up to a particular level (see classloadlevel.h).
// The class loader itself makes use of these levels to "break" recursion across
// generic instantiations. External clients should leave the parameter at its default
// value (CLASS_LOADED).
//==================================================================================
public:
// We use enums for these flags so that we can easily search the codebase to
// determine where the flags are set to their non-default values.
//
// This enum tells us what to do if the load fails. If ThrowIfNotFound is used
// with a HRESULT-returning NOTHROW function then it actually indicates that
// an error-HRESULT will be returned.
// The ThrowButNullV11McppWorkaround value means ThrowIfNotFound, except when the case
// of a Nil ResolutionScope for a value type (erroneously generated by Everett MCPP
// compiler.)
typedef enum { ThrowIfNotFound, ReturnNullIfNotFound, ThrowButNullV11McppWorkaround } NotFoundAction;
// This flag indicates whether we should accept an uninstantiatednaked TypeDef or TypeRef
// for a generic type definition, where "uninstantiated" means "not used as part of
// a TypeSpec"
typedef enum { FailIfUninstDefOrRef, PermitUninstDefOrRef } PermitUninstantiatedFlag;
// This flag indicates whether we want to "load" the type if it isn't already in the
// loader's tables and has reached the load level desired.
typedef enum { LoadTypes, DontLoadTypes } LoadTypesFlag;
// Load types by token (Def, Ref and Spec)
static TypeHandle LoadTypeDefThrowing(Module *pModule,
mdToken typeDef,
NotFoundAction fNotFound = ThrowIfNotFound,
PermitUninstantiatedFlag fUninstantiated = FailIfUninstDefOrRef,
mdToken tokenNotToLoad = tdNoTypes,
ClassLoadLevel level = CLASS_LOADED,
Instantiation * pTargetInstantiation = NULL /* used to verify arity of the loaded type */);
static TypeHandle LoadTypeDefOrRefThrowing(ModuleBase *pModule,
mdToken typeRefOrDef,
NotFoundAction fNotFound = ThrowIfNotFound,
PermitUninstantiatedFlag fUninstantiated = FailIfUninstDefOrRef,
mdToken tokenNotToLoad = tdNoTypes,
ClassLoadLevel level = CLASS_LOADED);
static TypeHandle LoadTypeDefOrRefOrSpecThrowing(Module *pModule,
mdToken typeRefOrDefOrSpec,
const SigTypeContext *pTypeContext,
NotFoundAction fNotFound = ThrowIfNotFound,
PermitUninstantiatedFlag fUninstantiated = FailIfUninstDefOrRef,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED,
BOOL dropGenericArgumentLevel = FALSE,
const Substitution *pSubst = NULL /* substitution to apply if the token is a type spec with generic variables */,
MethodTable *pMTInterfaceMapOwner = NULL);
// Load constructed types by providing their constituents
static TypeHandle LoadPointerOrByrefTypeThrowing(CorElementType typ,
TypeHandle baseType,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED);
// The resulting type behaves like the unmanaged view of a given value type.
static TypeHandle LoadNativeValueTypeThrowing(TypeHandle baseType,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED);
static TypeHandle LoadArrayTypeThrowing(TypeHandle baseType,
CorElementType typ = ELEMENT_TYPE_SZARRAY,
unsigned rank = 0,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED);
static TypeHandle LoadFnptrTypeThrowing(BYTE callConv,
DWORD numArgs,
TypeHandle* retAndArgTypes,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED);
// External class loader entry point
// Load types by name - doesn't support nested types.
// See overload using NameHandle.
static TypeHandle LoadTypeByNameThrowing(Assembly *pAssembly,
LPCUTF8 nameSpace,
LPCUTF8 name,
NotFoundAction fNotFound = ThrowIfNotFound,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED);
// Load types using a NameHandle.
static TypeHandle LoadTypeByNameThrowing(Assembly *pAssembly,
NameHandle *pNameHandle,
NotFoundAction fNotFound = ThrowIfNotFound,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED);
// Resolve a TypeRef to a TypeDef
// (Just a no-op on TypeDefs)
// Return FALSE if operation failed (e.g. type does not exist)
// *pfUsesTypeForwarder is set to TRUE if a type forwarder is found. It is never set to FALSE.
static BOOL ResolveTokenToTypeDefThrowing(ModuleBase * pTypeRefModule,
mdTypeRef typeRefToken,
Module ** ppTypeDefModule,
mdTypeDef * pTypeDefToken,
Loader::LoadFlag loadFlag = Loader::Load,
BOOL * pfUsesTypeForwarder = NULL);
// Resolve a name to a TypeDef
// Return FALSE if operation failed (e.g. type does not exist)
// *pfUsesTypeForwarder is set to TRUE if a type forwarder is found. It is never set to FALSE.
static BOOL ResolveNameToTypeDefThrowing(Module * pTypeRefModule,
const NameHandle * pName,
Module ** ppTypeDefModule,
mdTypeDef * pTypeDefToken,
Loader::LoadFlag loadFlag = Loader::Load,
BOOL * pfUsesTypeForwarder = NULL);
static void EnsureLoaded(TypeHandle typeHnd, ClassLoadLevel level = CLASS_LOADED);
public:
// Look up a class by name
//
// Guaranteed to only return NULL if pName->OKToLoad() returns FALSE.
// Thus when type loads are enabled this will return non-null.
TypeHandle LoadTypeHandleThrowIfFailed(NameHandle* pName, ClassLoadLevel level = CLASS_LOADED,
Module* pLookInThisModuleOnly=NULL);
public:
// Looks up class in the local module table, if it is there it succeeds,
// Otherwise it fails, This is meant only for optimizations etc
static TypeHandle LookupTypeDefOrRefInModule(ModuleBase *pModule, mdToken cl, ClassLoadLevel *pLoadLevel = NULL);
private:
VOID AddAvailableClassDontHaveLock(Module *pModule,
mdTypeDef classdef,
AllocMemTracker *pamTracker);
VOID AddAvailableClassHaveLock(Module * pModule,
mdTypeDef classdef,
SArray<EEClassHashEntry_t *>* classEntries,
AllocMemTracker * pamTracker);
VOID AddExportedTypeDontHaveLock(Module *pManifestModule,
mdExportedType cl,
AllocMemTracker *pamTracker);
VOID AddExportedTypeHaveLock(Module *pManifestModule,
mdExportedType cl,
SArray<EEClassHashEntry_t *>* exportedEntries,
AllocMemTracker *pamTracker);
public:
// For an generic type instance return the representative within the class of
// all type handles that share code. For example,
// <int> --> <int>,
// <object> --> <__Canon>,
// <string> --> <__Canon>,
// <List<string>> --> <__Canon>,
// <Struct<string>> --> <Struct<__Canon>>
//
// If the code for the type handle is not shared then return
// the type handle itself.
static TypeHandle CanonicalizeGenericArg(TypeHandle genericArg);
// Determine if the specified type representation induces a sharable
// set of compatible instantiations when used as a type parameter to
// a generic type or method.
//
// For example, when sharing at reference types "object" and "Struct<object>"
// both induce sets of compatible instantiations, e.g. when used to build types
// "List<object>" and "List<Struct<object>>" respectively.
static BOOL IsSharableInstantiation(Instantiation inst);
// Determine if it is normalized canonical generic instantiation.
// Dictionary<__Canon, __Canon> -> TRUE
// Dictionary<__Canon, int> -> TRUE
// Dictionary<__Canon, String> -> FALSE
static BOOL IsCanonicalGenericInstantiation(Instantiation inst);
// Determine if it is the entirely-canonical generic instantiation
// Dictionary<__Canon, __Canon> -> TRUE
// Dictionary<anything else> -> FALSE
static BOOL IsTypicalSharedInstantiation(Instantiation inst);
// Return TRUE if inst is the typical instantiation for the type or method specified by pModule/token
static BOOL IsTypicalInstantiation(Module *pModule, mdToken token, Instantiation inst);
// Load canonical shared instantiation for type key (each instantiation argument is
// substituted by CanonicalizeGenericArg)
static TypeHandle LoadCanonicalGenericInstantiation(const TypeKey *pTypeKey,
LoadTypesFlag fLoadTypes/*=LoadTypes*/,
ClassLoadLevel level/*=CLASS_LOADED*/);
// Create a generic instantiation.
// If typeDef is not a generic type then throw an exception
// If its arity does not match nGenericClassArgCount then throw an exception
// The pointer to the instantiation is not persisted e.g. the type parameters can be stack-allocated.
// If inst=NULL then <__Canon,...,__Canon> is assumed
// If fLoadTypes=DontLoadTypes then the type handle is not created if it is not
// already present in the tables.
static TypeHandle LoadGenericInstantiationThrowing(Module *pModule,
mdTypeDef typeDef,
Instantiation inst,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED,
const InstantiationContext *pInstContext = NULL,
BOOL fFromNativeImage = FALSE);
// Public access Check APIs
public:
static BOOL CanAccessClass(
AccessCheckContext* pContext,
MethodTable* pTargetClass,
Assembly* pTargetAssembly,
const AccessCheckOptions & accessCheckOptions = *AccessCheckOptions::s_pNormalAccessChecks);
static BOOL CanAccess(
AccessCheckContext* pContext,
MethodTable* pTargetClass,
Assembly* pTargetAssembly,
DWORD dwMemberAttrs,
MethodDesc* pOptionalTargetMethod,
FieldDesc* pOptionalTargetField,
const AccessCheckOptions & accessCheckOptions = *AccessCheckOptions::s_pNormalAccessChecks);
private:
// Access check helpers
static BOOL CanAccessMethodInstantiation(
AccessCheckContext* pContext,
MethodDesc* pOptionalTargetMethod,
const AccessCheckOptions & accessCheckOptions);
static BOOL CanAccessFamily(
MethodTable* pCurrentClass,
MethodTable* pTargetClass);
static BOOL CheckAccessMember(
AccessCheckContext* pContext,
MethodTable* pTargetClass,
Assembly* pTargetAssembly,
DWORD dwMemberAttrs,
MethodDesc* pOptionalTargetMethod,
FieldDesc* pOptionalTargetField,
const AccessCheckOptions & accessCheckOptions = *AccessCheckOptions::s_pNormalAccessChecks);
public:
//Creates a key with both the namespace and name converted to lowercase and
//made into a proper namespace-path.
VOID CreateCanonicallyCasedKey(LPCUTF8 pszNameSpace, LPCUTF8 pszName,
_Out_ LPUTF8 *ppszOutNameSpace, _Out_ LPUTF8 *ppszOutName);
static HRESULT FindTypeDefByExportedType(IMDInternalImport *pCTImport,
mdExportedType mdCurrent,
IMDInternalImport *pTDImport,
mdTypeDef *mtd);
class AvailableClasses_LockHolder : public CrstHolder
{
public:
AvailableClasses_LockHolder(ClassLoader *classLoader)
: CrstHolder(&classLoader->m_AvailableClassLock)
{
WRAPPER_NO_CONTRACT;
}
};
friend class AvailableClasses_LockHolder;
private:
static TypeHandle LoadConstructedTypeThrowing(const TypeKey *pKey,
LoadTypesFlag fLoadTypes = LoadTypes,
ClassLoadLevel level = CLASS_LOADED,
const InstantiationContext *pInstContext = NULL);
static TypeHandle LookupTypeKey(const TypeKey *pKey, EETypeHashTable *pTable);
static TypeHandle LookupInLoaderModule(const TypeKey* pKey);
// Lookup a handle in the appropriate table
// (declaring module for TypeDef or loader-module for constructed types)
static TypeHandle LookupTypeHandleForTypeKey(const TypeKey *pTypeKey);
static void DECLSPEC_NORETURN ThrowTypeLoadException(const TypeKey *pKey, UINT resIDWhy);
public:
//Attempts to find/load/create a type handle but does not throw
// if used in "find" mode.
TypeHandle LoadTypeHandleThrowing(NameHandle* pName, ClassLoadLevel level = CLASS_LOADED,
Module* pLookInThisModuleOnly=NULL);
static void ValidateMethodsWithCovariantReturnTypes(MethodTable* pMT);
private:
#ifndef DACCESS_COMPILE
// Perform a single phase of class loading
// If no type handle has yet been created, typeHnd is null.
static TypeHandle DoIncrementalLoad(const TypeKey *pTypeKey,
TypeHandle typeHnd,
ClassLoadLevel workLevel);
// Phase CLASS_LOAD_CREATE of class loading
static TypeHandle CreateTypeHandleForTypeKey(const TypeKey *pTypeKey,
AllocMemTracker *pamTracker);
// Publish the type in the loader's tables
static TypeHandle PublishType(const TypeKey *pTypeKey, TypeHandle typeHnd);
// Notify profiler and debugger that a type load has completed
// Also update perf counters
static void Notify(TypeHandle typeHnd);
// Phase CLASS_LOAD_EXACTPARENTS of class loading
// Load exact parents and interfaces and dependent structures (generics dictionary, vtable fixes)
static void LoadExactParents(MethodTable* pMT);
static void LoadExactParentAndInterfacesTransitively(MethodTable *pMT);
static void PropagateCovariantReturnMethodImplSlots(MethodTable* pMT);
static bool IsCompatibleWith(TypeHandle hType1, TypeHandle hType2);
static CorElementType GetReducedTypeElementType(TypeHandle hType);
static CorElementType GetVerificationTypeElementType(TypeHandle hType);
static bool AreVerificationTypesEqual(TypeHandle hType1, TypeHandle hType2);
static bool IsMethodSignatureCompatibleWith(FnPtrTypeDesc* fn1TD, FnPtrTypeDesc* fn2TD);
// Create a non-canonical instantiation of a generic type based off the canonical instantiation
// (For example, MethodTable for List<string> is based on the MethodTable for List<__Canon>)
static TypeHandle CreateTypeHandleForNonCanonicalGenericInstantiation(const TypeKey *pTypeKey,
AllocMemTracker *pamTracker);
// Loads a class. This is the inner call from the multi-threaded load. This load must
// be protected in some manner.
// If we're attempting to load a fresh instantiated type then genericArgs should be filled in
static TypeHandle CreateTypeHandleForTypeDefThrowing(Module *pModule,
mdTypeDef cl,
Instantiation inst,
AllocMemTracker *pamTracker);
// The token must be a type def. GC must be enabled.
// If we're attempting to load a fresh instantiated type then genericArgs should be filled in
TypeHandle LoadTypeHandleForTypeKey(const TypeKey *pTypeKey,
TypeHandle typeHnd,
ClassLoadLevel level = CLASS_LOADED,
const InstantiationContext *pInstContext = NULL);
TypeHandle LoadTypeHandleForTypeKeyNoLock(const TypeKey *pTypeKey,
ClassLoadLevel level = CLASS_LOADED,
const InstantiationContext *pInstContext = NULL);
// Used for initial loading of parent class and implemented interfaces
// When tok represents an instantiated type return an *approximate* instantiated
// type (where reference type arguments are replaced by Object)
static
TypeHandle
LoadApproxTypeThrowing(
Module * pModule,
mdToken tok,
SigPointer * pSigInst,
const SigTypeContext * pClassTypeContext);
// Returns the parent of a token. The token must be a typedef.
// If the parent is a shared constructed type (e.g. class C : List<string>) then
// only the canonical instantiation is loaded at this point.
// This is to avoid cycles in the loader e.g. on class C : D<C> or class C<T> : D<C<T>>
// We fix up the exact parent later in LoadInstantiatedInfo.
static
MethodTable *
LoadApproxParentThrowing(
Module * pModule,
mdToken cl,
SigPointer * pParentInst,
const SigTypeContext * pClassTypeContext);
// Locates the enclosing class of a token if any. The token must be a typedef.
static VOID GetEnclosingClassThrowing(IMDInternalImport *pInternalImport,
Module *pModule,
mdTypeDef cl,
mdTypeDef *tdEnclosing);
// Insert the class in the classes hash table and if needed in the case insensitive one
EEClassHashEntry_t *InsertValue(EEClassHashTable *pClassHash,
EEClassHashTable *pClassCaseInsHash,
LPCUTF8 pszNamespace,
LPCUTF8 pszClassName,
HashDatum Data,
EEClassHashEntry_t *pEncloser,
AllocMemTracker *pamTracker);
// don't call this directly.
TypeHandle LoadTypeHandleForTypeKey_Body(const TypeKey *pTypeKey,
TypeHandle typeHnd,
ClassLoadLevel targetLevel);
#endif //!DACCESS_COMPILE
}; // class ClassLoader