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
/
jitinterface.cpp
14578 lines (11958 loc) · 467 KB
/
jitinterface.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ===========================================================================
// File: JITinterface.CPP
//
// ===========================================================================
#include "common.h"
#include "jitinterface.h"
#include "codeman.h"
#include "method.hpp"
#include "class.h"
#include "object.h"
#include "field.h"
#include "stublink.h"
#include "virtualcallstub.h"
#include "corjit.h"
#include "eeconfig.h"
#include "excep.h"
#include "log.h"
#include "excep.h"
#include "float.h" // for isnan
#include "dbginterface.h"
#include "dllimport.h"
#include "gcheaputilities.h"
#include "comdelegate.h"
#include "jitperf.h" // to track jit perf
#include "corprof.h"
#include "eeprofinterfaces.h"
#ifdef PROFILING_SUPPORTED
#include "proftoeeinterfaceimpl.h"
#include "eetoprofinterfaceimpl.h"
#include "eetoprofinterfaceimpl.inl"
#include "profilepriv.h"
#include "rejit.h"
#endif // PROFILING_SUPPORTED
#include "ecall.h"
#include "generics.h"
#include "typestring.h"
#include "typedesc.h"
#include "genericdict.h"
#include "array.h"
#include "debuginfostore.h"
#include "safemath.h"
#include "runtimehandles.h"
#include "sigbuilder.h"
#include "openum.h"
#ifdef HAVE_GCCOVER
#include "gccover.h"
#endif // HAVE_GCCOVER
#ifdef FEATURE_PREJIT
#include "compile.h"
#include "corcompile.h"
#endif // FEATURE_PREJIT
#ifdef FEATURE_INTERPRETER
#include "interpreter.h"
#endif // FEATURE_INTERPRETER
#ifdef FEATURE_PERFMAP
#include "perfmap.h"
#endif
// The Stack Overflow probe takes place in the COOPERATIVE_TRANSITION_BEGIN() macro
//
#define JIT_TO_EE_TRANSITION() MAKE_CURRENT_THREAD_AVAILABLE_EX(m_pThread); \
_ASSERTE(CURRENT_THREAD == GetThread()); \
INSTALL_UNWIND_AND_CONTINUE_HANDLER_NO_PROBE; \
COOPERATIVE_TRANSITION_BEGIN(); \
START_NON_JIT_PERF();
#define EE_TO_JIT_TRANSITION() STOP_NON_JIT_PERF(); \
COOPERATIVE_TRANSITION_END(); \
UNINSTALL_UNWIND_AND_CONTINUE_HANDLER_NO_PROBE;
#define JIT_TO_EE_TRANSITION_LEAF()
#define EE_TO_JIT_TRANSITION_LEAF()
#if defined(CROSSGEN_COMPILE)
static const char *const hlpNameTable[CORINFO_HELP_COUNT] = {
#define JITHELPER(code, pfnHelper, sig) #code,
#include "jithelpers.h"
};
#endif
#ifdef DACCESS_COMPILE
// The real definitions are in jithelpers.cpp. However, those files are not included in the DAC build.
// Hence, we add them here.
GARY_IMPL(VMHELPDEF, hlpFuncTable, CORINFO_HELP_COUNT);
GARY_IMPL(VMHELPDEF, hlpDynamicFuncTable, DYNAMIC_CORINFO_HELP_COUNT);
#else // DACCESS_COMPILE
/*********************************************************************/
inline CORINFO_MODULE_HANDLE GetScopeHandle(MethodDesc* method)
{
LIMITED_METHOD_CONTRACT;
if (method->IsDynamicMethod())
{
return MakeDynamicScope(method->AsDynamicMethodDesc()->GetResolver());
}
else
{
return GetScopeHandle(method->GetModule());
}
}
//This is common refactored code from within several of the access check functions.
BOOL ModifyCheckForDynamicMethod(DynamicResolver *pResolver,
TypeHandle *pOwnerTypeForSecurity,
AccessCheckOptions::AccessCheckType *pAccessCheckType,
DynamicResolver** ppAccessContext)
{
CONTRACTL {
STANDARD_VM_CHECK;
PRECONDITION(CheckPointer(pResolver));
PRECONDITION(CheckPointer(pOwnerTypeForSecurity));
PRECONDITION(CheckPointer(pAccessCheckType));
PRECONDITION(CheckPointer(ppAccessContext));
PRECONDITION(*pAccessCheckType == AccessCheckOptions::kNormalAccessibilityChecks);
} CONTRACTL_END;
BOOL doAccessCheck = TRUE;
//Do not blindly initialize fields, since they've already got important values.
DynamicResolver::SecurityControlFlags dwSecurityFlags = DynamicResolver::Default;
TypeHandle dynamicOwner;
pResolver->GetJitContext(&dwSecurityFlags, &dynamicOwner);
if (!dynamicOwner.IsNull())
*pOwnerTypeForSecurity = dynamicOwner;
if (dwSecurityFlags & DynamicResolver::SkipVisibilityChecks)
{
doAccessCheck = FALSE;
}
else if (dwSecurityFlags & DynamicResolver::RestrictedSkipVisibilityChecks)
{
*pAccessCheckType = AccessCheckOptions::kRestrictedMemberAccessNoTransparency;
}
else
{
*pAccessCheckType = AccessCheckOptions::kNormalAccessNoTransparency;
}
return doAccessCheck;
}
/*****************************************************************************/
// Initialize from data we passed across to the JIT
inline static void GetTypeContext(const CORINFO_SIG_INST *info, SigTypeContext *pTypeContext)
{
LIMITED_METHOD_CONTRACT;
SigTypeContext::InitTypeContext(
Instantiation((TypeHandle *) info->classInst, info->classInstCount),
Instantiation((TypeHandle *) info->methInst, info->methInstCount),
pTypeContext);
}
static MethodDesc* GetMethodFromContext(CORINFO_CONTEXT_HANDLE context)
{
LIMITED_METHOD_CONTRACT;
if (((size_t) context & CORINFO_CONTEXTFLAGS_MASK) == CORINFO_CONTEXTFLAGS_CLASS)
{
return NULL;
}
else
{
return GetMethod((CORINFO_METHOD_HANDLE)((size_t) context & ~CORINFO_CONTEXTFLAGS_MASK));
}
}
static TypeHandle GetTypeFromContext(CORINFO_CONTEXT_HANDLE context)
{
LIMITED_METHOD_CONTRACT;
if (((size_t) context & CORINFO_CONTEXTFLAGS_MASK) == CORINFO_CONTEXTFLAGS_CLASS)
{
return TypeHandle((CORINFO_CLASS_HANDLE) ((size_t) context & ~CORINFO_CONTEXTFLAGS_MASK));
}
else
{
MethodTable * pMT = GetMethodFromContext(context)->GetMethodTable();
return TypeHandle(pMT);
}
}
// Initialize from a context parameter passed to the JIT and back. This is a parameter
// that indicates which method is being jitted.
inline static void GetTypeContext(CORINFO_CONTEXT_HANDLE context, SigTypeContext *pTypeContext)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(context != NULL);
}
CONTRACTL_END;
if (GetMethodFromContext(context))
{
SigTypeContext::InitTypeContext(GetMethodFromContext(context), pTypeContext);
}
else
{
SigTypeContext::InitTypeContext(GetTypeFromContext(context), pTypeContext);
}
}
static BOOL ContextIsShared(CORINFO_CONTEXT_HANDLE context)
{
LIMITED_METHOD_CONTRACT;
MethodDesc *pContextMD = GetMethodFromContext(context);
if (pContextMD != NULL)
{
return pContextMD->IsSharedByGenericInstantiations();
}
else
{
// Type handle contexts are non-shared and are used for inlining of
// non-generic methods in generic classes
return FALSE;
}
}
// Returns true if context is providing any generic variables
static BOOL ContextIsInstantiated(CORINFO_CONTEXT_HANDLE context)
{
LIMITED_METHOD_CONTRACT;
if (GetMethodFromContext(context))
{
return GetMethodFromContext(context)->HasClassOrMethodInstantiation();
}
else
{
return GetTypeFromContext(context).HasInstantiation();
}
}
/*********************************************************************/
// This normalizes EE type information into the form expected by the JIT.
//
// If typeHnd contains exact type information, then *clsRet will contain
// the normalized CORINFO_CLASS_HANDLE information on return.
// Static
CorInfoType CEEInfo::asCorInfoType(CorElementType eeType,
TypeHandle typeHnd, /* optional in */
CORINFO_CLASS_HANDLE *clsRet/* optional out */ ) {
CONTRACT(CorInfoType) {
THROWS;
GC_TRIGGERS;
PRECONDITION((CorTypeInfo::IsGenericVariable(eeType)) ==
(!typeHnd.IsNull() && typeHnd.IsGenericVariable()));
PRECONDITION(eeType != ELEMENT_TYPE_GENERICINST);
} CONTRACT_END;
TypeHandle typeHndUpdated = typeHnd;
if (!typeHnd.IsNull())
{
CorElementType normType = typeHnd.GetInternalCorElementType();
// If we have a type handle, then it has the better type
// in some cases
if (eeType == ELEMENT_TYPE_VALUETYPE && !CorTypeInfo::IsObjRef(normType))
eeType = normType;
// Zap the typeHnd when the type _really_ is a primitive
// as far as verification is concerned. Returning a null class
// handle means it is is a primitive.
//
// Enums are exactly like primitives, even from a verification standpoint,
// so we zap the type handle in this case.
//
// However RuntimeTypeHandle etc. are reported as E_T_INT (or something like that)
// but don't count as primitives as far as verification is concerned...
//
// To make things stranger, TypedReference returns true for "IsTruePrimitive".
// However the JIT likes us to report the type handle in that case.
if (!typeHnd.IsTypeDesc() && (
(typeHnd.AsMethodTable()->IsTruePrimitive() && typeHnd != TypeHandle(g_TypedReferenceMT))
|| typeHnd.AsMethodTable()->IsEnum()) )
{
typeHndUpdated = TypeHandle();
}
}
static const BYTE map[] = {
CORINFO_TYPE_UNDEF,
CORINFO_TYPE_VOID,
CORINFO_TYPE_BOOL,
CORINFO_TYPE_CHAR,
CORINFO_TYPE_BYTE,
CORINFO_TYPE_UBYTE,
CORINFO_TYPE_SHORT,
CORINFO_TYPE_USHORT,
CORINFO_TYPE_INT,
CORINFO_TYPE_UINT,
CORINFO_TYPE_LONG,
CORINFO_TYPE_ULONG,
CORINFO_TYPE_FLOAT,
CORINFO_TYPE_DOUBLE,
CORINFO_TYPE_STRING,
CORINFO_TYPE_PTR, // PTR
CORINFO_TYPE_BYREF,
CORINFO_TYPE_VALUECLASS,
CORINFO_TYPE_CLASS,
CORINFO_TYPE_VAR, // VAR (type variable)
CORINFO_TYPE_CLASS, // ARRAY
CORINFO_TYPE_CLASS, // WITH
CORINFO_TYPE_REFANY,
CORINFO_TYPE_UNDEF, // VALUEARRAY_UNSUPPORTED
CORINFO_TYPE_NATIVEINT, // I
CORINFO_TYPE_NATIVEUINT, // U
CORINFO_TYPE_UNDEF, // R_UNSUPPORTED
// put the correct type when we know our implementation
CORINFO_TYPE_PTR, // FNPTR
CORINFO_TYPE_CLASS, // OBJECT
CORINFO_TYPE_CLASS, // SZARRAY
CORINFO_TYPE_VAR, // MVAR
CORINFO_TYPE_UNDEF, // CMOD_REQD
CORINFO_TYPE_UNDEF, // CMOD_OPT
CORINFO_TYPE_UNDEF, // INTERNAL
};
_ASSERTE(sizeof(map) == ELEMENT_TYPE_MAX);
_ASSERTE(eeType < (CorElementType) sizeof(map));
// spot check of the map
_ASSERTE((CorInfoType) map[ELEMENT_TYPE_I4] == CORINFO_TYPE_INT);
_ASSERTE((CorInfoType) map[ELEMENT_TYPE_PTR] == CORINFO_TYPE_PTR);
_ASSERTE((CorInfoType) map[ELEMENT_TYPE_TYPEDBYREF] == CORINFO_TYPE_REFANY);
CorInfoType res = ((unsigned)eeType < ELEMENT_TYPE_MAX) ? ((CorInfoType) map[(unsigned)eeType]) : CORINFO_TYPE_UNDEF;
if (clsRet)
*clsRet = CORINFO_CLASS_HANDLE(typeHndUpdated.AsPtr());
RETURN res;
}
inline static CorInfoType toJitType(TypeHandle typeHnd, CORINFO_CLASS_HANDLE *clsRet = NULL)
{
WRAPPER_NO_CONTRACT;
return CEEInfo::asCorInfoType(typeHnd.GetInternalCorElementType(), typeHnd, clsRet);
}
void CheckForEquivalenceAndLoadTypeBeforeCodeIsRun(Module *pModule, mdToken token, Module *pDefModule, mdToken defToken, const SigParser *ptr, SigTypeContext *pTypeContext, void *pData)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
if (IsTypeDefEquivalent(defToken, pDefModule))
{
SigPointer sigPtr(*ptr);
TypeHandle th = sigPtr.GetTypeHandleThrowing(pModule, pTypeContext);
((ICorDynamicInfo *)pData)->classMustBeLoadedBeforeCodeIsRun(CORINFO_CLASS_HANDLE(th.AsPtr()));
}
}
inline static void TypeEquivalenceFixupSpecificationHelper(ICorDynamicInfo * pCorInfo, MethodDesc *pMD)
{
STANDARD_VM_CONTRACT;
// A fixup is necessary to ensure that the parameters to the method are loaded before the method
// is called. In these cases we will not perform the appropriate loading when we load parameter
// types because with type equivalence, the parameter types at the call site do not necessarily
// match that those in the actual function. (They must be equivalent, but not necessarily the same.)
// In non-ngen scenarios this code here will force the types to be loaded directly by the call to
// HasTypeEquivalentStructParameters.
if (!pMD->IsVirtual())
{
if (pMD->HasTypeEquivalentStructParameters())
{
if (IsCompilationProcess())
pMD->WalkValueTypeParameters(pMD->GetMethodTable(), CheckForEquivalenceAndLoadTypeBeforeCodeIsRun, pCorInfo);
}
}
else
{
if (pMD->GetMethodTable()->DependsOnEquivalentOrForwardedStructs())
{
if (pMD->HasTypeEquivalentStructParameters())
pCorInfo->classMustBeLoadedBeforeCodeIsRun((CORINFO_CLASS_HANDLE)pMD->GetMethodTable());
}
}
}
//---------------------------------------------------------------------------------------
//
//@GENERICS:
// The method handle is used to instantiate method and class type parameters
// It's also used to determine whether an extra dictionary parameter is required
//
// sig - Input metadata signature
// scopeHnd - The signature is to be interpreted in the context of this scope (module)
// token - Metadata token used to refer to the signature (may be mdTokenNil for dynamic methods)
// sigRet - Resulting output signature in a format that is understood by native compilers
// pContextMD - The method with any instantiation information (may be NULL)
// localSig - Is it a local variables declaration, or a method signature (with return type, etc).
// contextType - The type with any instantiaton information
//
//static
void
CEEInfo::ConvToJitSig(
PCCOR_SIGNATURE pSig,
DWORD cbSig,
CORINFO_MODULE_HANDLE scopeHnd,
mdToken token,
CORINFO_SIG_INFO * sigRet,
MethodDesc * pContextMD,
bool localSig,
TypeHandle contextType)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
} CONTRACTL_END;
SigTypeContext typeContext;
uint32_t sigRetFlags = 0;
if (pContextMD)
{
SigTypeContext::InitTypeContext(pContextMD, contextType, &typeContext);
if (pContextMD->ShouldSuppressGCTransition())
sigRetFlags |= CORINFO_SIGFLAG_SUPPRESS_GC_TRANSITION;
}
else
{
SigTypeContext::InitTypeContext(contextType, &typeContext);
}
_ASSERTE(CORINFO_CALLCONV_DEFAULT == (CorInfoCallConv) IMAGE_CEE_CS_CALLCONV_DEFAULT);
_ASSERTE(CORINFO_CALLCONV_VARARG == (CorInfoCallConv) IMAGE_CEE_CS_CALLCONV_VARARG);
_ASSERTE(CORINFO_CALLCONV_MASK == (CorInfoCallConv) IMAGE_CEE_CS_CALLCONV_MASK);
_ASSERTE(CORINFO_CALLCONV_HASTHIS == (CorInfoCallConv) IMAGE_CEE_CS_CALLCONV_HASTHIS);
TypeHandle typeHnd = TypeHandle();
sigRet->pSig = pSig;
sigRet->cbSig = cbSig;
sigRet->retTypeClass = 0;
sigRet->retTypeSigClass = 0;
sigRet->scope = scopeHnd;
sigRet->token = token;
sigRet->sigInst.classInst = (CORINFO_CLASS_HANDLE *) typeContext.m_classInst.GetRawArgs();
sigRet->sigInst.classInstCount = (unsigned) typeContext.m_classInst.GetNumArgs();
sigRet->sigInst.methInst = (CORINFO_CLASS_HANDLE *) typeContext.m_methodInst.GetRawArgs();
sigRet->sigInst.methInstCount = (unsigned) typeContext.m_methodInst.GetNumArgs();
SigPointer sig(pSig, cbSig);
if (!localSig)
{
// This is a method signature which includes calling convention, return type,
// arguments, etc
_ASSERTE(!sig.IsNull());
Module * module = GetModule(scopeHnd);
ULONG data;
IfFailThrow(sig.GetCallingConvInfo(&data));
sigRet->callConv = (CorInfoCallConv) data;
#if defined(PLATFORM_UNIX) || defined(_TARGET_ARM_)
if ((isCallConv(sigRet->callConv, IMAGE_CEE_CS_CALLCONV_VARARG)) ||
(isCallConv(sigRet->callConv, IMAGE_CEE_CS_CALLCONV_NATIVEVARARG)))
{
// This signature corresponds to a method that uses varargs, which are not supported.
COMPlusThrow(kInvalidProgramException, IDS_EE_VARARG_NOT_SUPPORTED);
}
#endif // defined(PLATFORM_UNIX) || defined(_TARGET_ARM_)
// Skip number of type arguments
if (sigRet->callConv & IMAGE_CEE_CS_CALLCONV_GENERIC)
IfFailThrow(sig.GetData(NULL));
ULONG numArgs;
IfFailThrow(sig.GetData(&numArgs));
if (numArgs != (unsigned short) numArgs)
COMPlusThrowHR(COR_E_INVALIDPROGRAM);
sigRet->numArgs = (unsigned short) numArgs;
CorElementType type = sig.PeekElemTypeClosed(module, &typeContext);
if (!CorTypeInfo::IsPrimitiveType(type))
{
typeHnd = sig.GetTypeHandleThrowing(module, &typeContext);
_ASSERTE(!typeHnd.IsNull());
// I believe it doesn't make any diff. if this is
// GetInternalCorElementType or GetSignatureCorElementType
type = typeHnd.GetSignatureCorElementType();
}
sigRet->retType = CEEInfo::asCorInfoType(type, typeHnd, &sigRet->retTypeClass);
sigRet->retTypeSigClass = CORINFO_CLASS_HANDLE(typeHnd.AsPtr());
IfFailThrow(sig.SkipExactlyOne()); // must to a skip so we skip any class tokens associated with the return type
_ASSERTE(sigRet->retType < CORINFO_TYPE_COUNT);
sigRet->args = (CORINFO_ARG_LIST_HANDLE)sig.GetPtr();
}
else
{
// This is local variables declaration
sigRetFlags |= CORINFO_SIGFLAG_IS_LOCAL_SIG;
sigRet->callConv = CORINFO_CALLCONV_DEFAULT;
sigRet->retType = CORINFO_TYPE_VOID;
sigRet->numArgs = 0;
if (!sig.IsNull())
{
ULONG callConv;
IfFailThrow(sig.GetCallingConvInfo(&callConv));
if (callConv != IMAGE_CEE_CS_CALLCONV_LOCAL_SIG)
{
COMPlusThrowHR(COR_E_BADIMAGEFORMAT, BFA_CALLCONV_NOT_LOCAL_SIG);
}
ULONG numArgs;
IfFailThrow(sig.GetData(&numArgs));
if (numArgs != (unsigned short) numArgs)
COMPlusThrowHR(COR_E_INVALIDPROGRAM);
sigRet->numArgs = (unsigned short) numArgs;
}
sigRet->args = (CORINFO_ARG_LIST_HANDLE)sig.GetPtr();
}
// Set computed flags
sigRet->flags = sigRetFlags;
_ASSERTE(SigInfoFlagsAreValid(sigRet));
} // CEEInfo::ConvToJitSig
//---------------------------------------------------------------------------------------
//
CORINFO_CLASS_HANDLE CEEInfo::getTokenTypeAsHandle (CORINFO_RESOLVED_TOKEN * pResolvedToken)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
} CONTRACTL_END;
CORINFO_CLASS_HANDLE tokenType = NULL;
JIT_TO_EE_TRANSITION();
_ASSERTE((pResolvedToken->hMethod == NULL) || (pResolvedToken->hField == NULL));
BinderClassID classID = CLASS__TYPE_HANDLE;
if (pResolvedToken->hMethod != NULL)
{
classID = CLASS__METHOD_HANDLE;
}
else
if (pResolvedToken->hField != NULL)
{
classID = CLASS__FIELD_HANDLE;
}
tokenType = CORINFO_CLASS_HANDLE(MscorlibBinder::GetClass(classID));
EE_TO_JIT_TRANSITION();
return tokenType;
}
/*********************************************************************/
size_t CEEInfo::findNameOfToken (
CORINFO_MODULE_HANDLE scopeHnd,
mdToken metaTOK,
__out_ecount (FQNameCapacity) char * szFQName,
size_t FQNameCapacity)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
} CONTRACTL_END;
size_t NameLen = 0;
JIT_TO_EE_TRANSITION();
if (IsDynamicScope(scopeHnd))
{
strncpy_s (szFQName, FQNameCapacity, "DynamicToken", FQNameCapacity - 1);
NameLen = strlen (szFQName);
}
else
{
Module* module = (Module *)scopeHnd;
NameLen = findNameOfToken(module, metaTOK, szFQName, FQNameCapacity);
}
EE_TO_JIT_TRANSITION();
return NameLen;
}
CorInfoCanSkipVerificationResult CEEInfo::canSkipMethodVerification(CORINFO_METHOD_HANDLE ftnHnd)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
} CONTRACTL_END;
return CORINFO_VERIFICATION_CAN_SKIP;
}
/*********************************************************************/
BOOL CEEInfo::shouldEnforceCallvirtRestriction(
CORINFO_MODULE_HANDLE scopeHnd)
{
LIMITED_METHOD_CONTRACT;
return TRUE;
}
#ifdef FEATURE_READYTORUN_COMPILER
// Returns true if assemblies are in the same version bubble
// Right now each assembly is in its own version bubble.
// If the need arises (i.e. performance issues) we will define sets of assemblies (e.g. all app assemblies)
// The main point is that all this logic is concentrated in one place.
// NOTICE: If you change this logic to allow multi-assembly version bubbles you
// need to consider the impact on diagnostic tools. Currently there is an inlining
// table which tracks inliner/inlinee relationships in R2R images but it is not
// yet capable of encoding cross-assembly inlines. The scenario where this
// may show are instrumenting profilers that want to instrument a given method A
// using the ReJit APIs. If method A happens to inlined within method B in another
// assembly then the profiler needs to know that so it can rejit B too.
// The recommended approach is to upgrade the inlining table (vm\inlinetracking.h\.cpp)
// now that presumably R2R images have some way to refer to methods in other
// assemblies in their version bubble. Chat with the diagnostics team if you need more
// details.
//
// There already is a case where cross-assembly inlining occurs in an
// unreported fashion for methods marked NonVersionable. There is a specific
// exemption called out for this on ICorProfilerInfo6::EnumNgenModuleMethodsInliningThisMethod
// and the impact of the cut was vetted with partners. It would not be appropriate
// to increase that unreported set without additional review.
bool IsInSameVersionBubble(Assembly * current, Assembly * target)
{
LIMITED_METHOD_CONTRACT;
// trivial case: current and target are identical
// DO NOT change this without reading the notice above
if (current == target)
return true;
return IsLargeVersionBubbleEnabled();
}
// Returns true if the assemblies defining current and target are in the same version bubble
static bool IsInSameVersionBubble(MethodDesc* pCurMD, MethodDesc *pTargetMD)
{
LIMITED_METHOD_CONTRACT;
// DO NOT change this without reading the notice above
if (IsInSameVersionBubble(pCurMD->GetModule()->GetAssembly(),
pTargetMD->GetModule()->GetAssembly()))
{
return true;
}
if (IsReadyToRunCompilation())
{
if (pTargetMD->GetModule()->GetMDImport()->GetCustomAttributeByName(pTargetMD->GetMemberDef(),
NONVERSIONABLE_TYPE, NULL, NULL) == S_OK)
{
return true;
}
}
return false;
}
#endif // FEATURE_READYTORUN_COMPILER
static bool CallerAndCalleeInSystemVersionBubble(MethodDesc* pCaller, MethodDesc* pCallee)
{
LIMITED_METHOD_CONTRACT;
#ifdef FEATURE_READYTORUN_COMPILER
if (IsReadyToRunCompilation())
return pCallee->GetModule()->IsSystem() && IsInSameVersionBubble(pCaller, pCallee);
#endif
return false;
}
/*********************************************************************/
CorInfoCanSkipVerificationResult CEEInfo::canSkipVerification(
CORINFO_MODULE_HANDLE moduleHnd)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
} CONTRACTL_END;
return CORINFO_VERIFICATION_CAN_SKIP;
}
/*********************************************************************/
// Checks if the given metadata token is valid
BOOL CEEInfo::isValidToken (
CORINFO_MODULE_HANDLE module,
mdToken metaTOK)
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
} CONTRACTL_END;
BOOL result = FALSE;
JIT_TO_EE_TRANSITION_LEAF();
if (IsDynamicScope(module))
{
// No explicit token validation for dynamic code. Validation is
// side-effect of token resolution.
result = TRUE;
}
else
{
result = ((Module *)module)->GetMDImport()->IsValidToken(metaTOK);
}
EE_TO_JIT_TRANSITION_LEAF();
return result;
}
/*********************************************************************/
// Checks if the given metadata token is valid StringRef
BOOL CEEInfo::isValidStringRef (
CORINFO_MODULE_HANDLE module,
mdToken metaTOK)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
} CONTRACTL_END;
BOOL result = FALSE;
JIT_TO_EE_TRANSITION();
if (IsDynamicScope(module))
{
result = GetDynamicResolver(module)->IsValidStringRef(metaTOK);
}
else
{
result = ((Module *)module)->CheckStringRef(metaTOK);
if (result)
{
DWORD dwCharCount;
LPCWSTR pString;
result = (!FAILED(((Module *)module)->GetMDImport()->GetUserString(metaTOK, &dwCharCount, NULL, &pString)) &&
pString != NULL);
}
}
EE_TO_JIT_TRANSITION();
return result;
}
/* static */
size_t CEEInfo::findNameOfToken (Module* module,
mdToken metaTOK,
__out_ecount (FQNameCapacity) char * szFQName,
size_t FQNameCapacity)
{
CONTRACTL {
NOTHROW;
GC_TRIGGERS;
} CONTRACTL_END;
#ifdef _DEBUG
PCCOR_SIGNATURE sig = NULL;
DWORD cSig;
LPCUTF8 pszNamespace = NULL;
LPCUTF8 pszClassName = NULL;
mdToken tokType = TypeFromToken(metaTOK);
switch(tokType)
{
case mdtTypeRef:
{
if (FAILED(module->GetMDImport()->GetNameOfTypeRef(metaTOK, &pszNamespace, &pszClassName)))
{
pszNamespace = pszClassName = "Invalid TypeRef record";
}
ns::MakePath(szFQName, (int)FQNameCapacity, pszNamespace, pszClassName);
break;
}
case mdtTypeDef:
{
if (FAILED(module->GetMDImport()->GetNameOfTypeDef(metaTOK, &pszClassName, &pszNamespace)))
{
pszClassName = pszNamespace = "Invalid TypeDef record";
}
ns::MakePath(szFQName, (int)FQNameCapacity, pszNamespace, pszClassName);
break;
}
case mdtFieldDef:
{
LPCSTR szFieldName;
if (FAILED(module->GetMDImport()->GetNameOfFieldDef(metaTOK, &szFieldName)))
{
szFieldName = "Invalid FieldDef record";
}
strncpy_s(szFQName, FQNameCapacity, (char*)szFieldName, FQNameCapacity - 1);
break;
}
case mdtMethodDef:
{
LPCSTR szMethodName;
if (FAILED(module->GetMDImport()->GetNameOfMethodDef(metaTOK, &szMethodName)))
{
szMethodName = "Invalid MethodDef record";
}
strncpy_s(szFQName, FQNameCapacity, (char*)szMethodName, FQNameCapacity - 1);
break;
}
case mdtMemberRef:
{
LPCSTR szName;
if (FAILED(module->GetMDImport()->GetNameAndSigOfMemberRef((mdMemberRef)metaTOK, &sig, &cSig, &szName)))
{
szName = "Invalid MemberRef record";
}
strncpy_s(szFQName, FQNameCapacity, (char *)szName, FQNameCapacity - 1);
break;
}
default:
sprintf_s(szFQName, FQNameCapacity, "!TK_%x", metaTOK);
break;
}
#else // !_DEBUG
strncpy_s (szFQName, FQNameCapacity, "<UNKNOWN>", FQNameCapacity - 1);
#endif // _DEBUG
return strlen (szFQName);
}
CorInfoHelpFunc CEEInfo::getLazyStringLiteralHelper(CORINFO_MODULE_HANDLE handle)
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
} CONTRACTL_END;
CorInfoHelpFunc result = CORINFO_HELP_UNDEF;
JIT_TO_EE_TRANSITION_LEAF();
result = IsDynamicScope(handle) ? CORINFO_HELP_UNDEF : CORINFO_HELP_STRCNS;
EE_TO_JIT_TRANSITION_LEAF();
return result;
}
CHECK CheckContext(CORINFO_MODULE_HANDLE scopeHnd, CORINFO_CONTEXT_HANDLE context)
{
CHECK_MSG(scopeHnd != NULL, "Illegal null scope");
CHECK_MSG(((size_t) context & ~CORINFO_CONTEXTFLAGS_MASK) != NULL, "Illegal null context");
if (((size_t) context & CORINFO_CONTEXTFLAGS_MASK) == CORINFO_CONTEXTFLAGS_CLASS)
{
TypeHandle handle((CORINFO_CLASS_HANDLE) ((size_t) context & ~CORINFO_CONTEXTFLAGS_MASK));
CHECK_MSG(handle.GetModule() == GetModule(scopeHnd), "Inconsistent scope and context");
}
else
{
MethodDesc* handle = (MethodDesc*) ((size_t) context & ~CORINFO_CONTEXTFLAGS_MASK);
CHECK_MSG(handle->GetModule() == GetModule(scopeHnd), "Inconsistent scope and context");
}
CHECK_OK;
}
static DECLSPEC_NORETURN void ThrowBadTokenException(CORINFO_RESOLVED_TOKEN * pResolvedToken)
{
switch (pResolvedToken->tokenType & CORINFO_TOKENKIND_Mask)
{
case CORINFO_TOKENKIND_Class:
COMPlusThrowHR(COR_E_BADIMAGEFORMAT, BFA_BAD_CLASS_TOKEN);
case CORINFO_TOKENKIND_Method:
COMPlusThrowHR(COR_E_BADIMAGEFORMAT, BFA_INVALID_METHOD_TOKEN);
case CORINFO_TOKENKIND_Field:
COMPlusThrowHR(COR_E_BADIMAGEFORMAT, BFA_BAD_FIELD_TOKEN);
default:
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
}
}
/*********************************************************************/
void CEEInfo::resolveToken(/* IN, OUT */ CORINFO_RESOLVED_TOKEN * pResolvedToken)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
} CONTRACTL_END;
JIT_TO_EE_TRANSITION();
_ASSERTE(CheckContext(pResolvedToken->tokenScope, pResolvedToken->tokenContext));
pResolvedToken->pTypeSpec = NULL;
pResolvedToken->cbTypeSpec = NULL;
pResolvedToken->pMethodSpec = NULL;
pResolvedToken->cbMethodSpec = NULL;
TypeHandle th;
MethodDesc * pMD = NULL;
FieldDesc * pFD = NULL;
CorInfoTokenKind tokenType = pResolvedToken->tokenType;
if (IsDynamicScope(pResolvedToken->tokenScope))
{
GetDynamicResolver(pResolvedToken->tokenScope)->ResolveToken(pResolvedToken->token, &th, &pMD, &pFD);
//
// Check that we got the expected handles and fill in missing data if necessary
//
CorTokenType tkType = (CorTokenType)TypeFromToken(pResolvedToken->token);
if (pMD != NULL)
{
if ((tkType != mdtMethodDef) && (tkType != mdtMemberRef))
ThrowBadTokenException(pResolvedToken);
if ((tokenType & CORINFO_TOKENKIND_Method) == 0)
ThrowBadTokenException(pResolvedToken);
if (th.IsNull())
th = pMD->GetMethodTable();
// "PermitUninstDefOrRef" check
if ((tokenType != CORINFO_TOKENKIND_Ldtoken) && pMD->ContainsGenericVariables())
{
COMPlusThrow(kInvalidProgramException);
}
// if this is a BoxedEntryPointStub get the UnboxedEntryPoint one
if (pMD->IsUnboxingStub())
{
pMD = pMD->GetMethodTable()->GetUnboxedEntryPointMD(pMD);
}
// Activate target if required
if (tokenType != CORINFO_TOKENKIND_Ldtoken)
{
ScanTokenForDynamicScope(pResolvedToken, th, pMD);
}
}
else
if (pFD != NULL)
{