-
Notifications
You must be signed in to change notification settings - Fork 25
/
newverify.cpp
5188 lines (4085 loc) · 157 KB
/
newverify.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==++==
//
//
// Copyright (c) 2006 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
#include "reader.h"
#include "readerir.h"
#include "utilcode.h"
#define _JIT64_PEV_
#include "vererror.h"
#define ASSERTM(p,m) do { if (!(p)) {ReaderBase::GenIR_DebugError(__FILE__,__LINE__,m);}} while (0)
#define ASSERT(p) ASSERTM(p,#p)
#define UNREACHED 0
#ifndef _MSC_VER
#define ASSUME(p) __assume(p)
#else
#define ASSUME(p)
#endif
#if !defined(_DEBUG)
#define NODEBUG 1
#define RELEASE 1
#endif
#ifndef NODEBUG
#define ASSERTMNR(p,m) ASSERTM(p,m)
#define ASSERTNR(p) ASSERTMNR(p,#p)
#define ASSERTDBG(p) ASSERTM(p,#p)
#define TODO() ASSERTMNR(0,"TODO\n")
#else
#define ASSERTMNR(p,m) ASSUME(p)
#define ASSERTNR(p) ASSUME(p)
#define ASSERTDBG(p)
#define TODO()
#endif
#include "jit.h"
#include "gverify.h"
#include "corerror.h"
#include "newvstate.h"
#include <limits.h>
#if !defined(_DEBUG)
#define NODEBUG 1
#define RELEASE 1
#endif
#define NO_WAY(x) Verify(0,x)
#define BADCODE(c) (ReaderBase::verGlobalError(c))
#define BADCODE3(x,y,z) (ReaderBase::verGlobalError(x))
#define IMPL_LIMITATION(c) (ReaderBase::GenIR_DebugError(__FILE__,__LINE__,c))
#ifdef VASSERT
#undef VASSERT
#endif
#define VASSERT(x) ASSERTNR(x)
#define VP(s)
const CORINFO_CLASS_HANDLE BAD_CLASS_HANDLE = (CORINFO_CLASS_HANDLE) -1;
#define verbose 0
#define Verify(cond, msg) ReaderBase::VerifyOrReturn(cond, msg)
#define verCompatibleWith(child, parent) tiCompatibleWith(m_jitInfo, child, parent)
extern const char* opcodeName[];
#ifdef _MSC_VER
#pragma warning(push,4)
#endif
HRESULT verLastError;
#define MAX_TYPE_STRING_SIZE 512
void GetErrorMessage(ReaderBase *rbase, ICorJitInfo* pJitInfo)
{
pJitInfo->GetErrorMessage(rbase->extended_error_message, ERROR_MSG_SIZE);
//wprintf(extended_error_message);
}
#ifdef DEBUG
inline void ReaderBase::verDumpType(const vertype &v)
{
WCHAR wszTypeFound[MAX_TYPE_STRING_SIZE];
v.Dump();
CORINFO_CLASS_HANDLE c = v.m_cls;//v.GetClassHandle();
if (c)
printf("\nname:%s\n", m_jitInfo->getClassName(c));
v.ToString(wszTypeFound, MAX_TYPE_STRING_SIZE, m_jitInfo, getCurrentMethodHandle());
wprintf(L"%s", wszTypeFound);
}
#endif
inline bool ReaderBase::verIsValueClass(CORINFO_CLASS_HANDLE clsHnd)
{
return (getClassAttribs(clsHnd) & CORINFO_FLG_VALUECLASS) != 0;
}
inline bool ReaderBase::verIsValClassWithStackPtr(CORINFO_CLASS_HANDLE clsHnd)
{
DWORD flags = getClassAttribs(clsHnd);
return (flags & CORINFO_FLG_VALUECLASS)
&& (flags & CORINFO_FLG_CONTAINS_STACK_PTR);
}
inline bool ReaderBase::verIsGenericTypeVar(CORINFO_CLASS_HANDLE clsHnd)
{
return (getClassAttribs(clsHnd) & CORINFO_FLG_GENERIC_TYPE_VARIABLE) != 0;
}
inline bool verTypeIsComposite(CorInfoType type)
{
return type == CORINFO_TYPE_STRING
|| type == CORINFO_TYPE_PTR
|| type == CORINFO_TYPE_BYREF
|| type == CORINFO_TYPE_VALUECLASS
|| type == CORINFO_TYPE_CLASS
|| type == CORINFO_TYPE_REFANY;
}
void ReaderBase::VerifyIsDirectCallToken(mdToken tok)
{
#ifdef DEBUG
if (verbose) printf(" %08X", tok);
#endif
VerifyToken(tok);
if (TypeFromToken(tok) == mdtMethodDef ||
TypeFromToken(tok) == mdtMethodSpec ||
TypeFromToken(tok) == mdtMemberRef)
return;
else
Verify(false, MVER_E_TOKEN_TYPE_MEMBER);
}
bool ReaderBase::verIsCallToken(mdToken tok)
{
// same as VerifyIsDirectCallToken but could also be a call site sig used in calli
return (TypeFromToken(tok) == mdtMethodDef ||
TypeFromToken(tok) == mdtMethodSpec ||
TypeFromToken(tok) == mdtMemberRef ||
TypeFromToken(tok) == mdtSignature);
}
void ReaderBase::VerifyIsCallToken(mdToken tok)
{
// same as VerifyIsDirectCallToken but could also be a call site sig used in calli
#ifdef DEBUG
if (verbose) printf(" %08X", tok);
#endif
VerifyToken(tok);
if (verIsCallToken(tok))
return;
else
Verify(false, MVER_E_TOKEN_TYPE_MEMBER);
}
void ReaderBase::VerifyIsFieldToken(mdToken tok)
{
#ifdef DEBUG
if (verbose) printf(" %08X", tok);
#endif
if (TypeFromToken(tok) == mdtFieldDef ||
TypeFromToken(tok) == mdtMemberRef)
return;
else
Verify(false, MVER_E_TOKEN_TYPE_FIELD);
}
void ReaderBase::VerifyIsClassToken(mdToken tok)
{
#ifdef DEBUG
if (verbose) printf(" %08X", tok);
#endif
VerifyToken(tok);
if (
TypeFromToken(tok) == mdtTypeDef ||
TypeFromToken(tok) == mdtTypeRef ||
TypeFromToken(tok) == mdtTypeSpec)
return;
else
Verify(false, MVER_E_TOKEN_TYPE_MEMBER);
}
CORINFO_CLASS_HANDLE ReaderBase::verGetClassHandle(mdToken tok, CorInfoTokenKind tokenKind)
{
// if in peverify try to trap it and continue
// but if in JIT just let it go up
CORINFO_CLASS_HANDLE clsHnd = NULL;
PAL_TRY
{
clsHnd = ReaderBase::getClassHandle(tok,
getCurrentMethodHandle(),
getCurrentModuleHandle(),
tokenKind);
}
PAL_EXCEPT_FILTER(ReaderBase::EEJITFilter, m_jitInfo)
{
GetErrorMessage(this, m_jitInfo);
Verify(false, MVER_E_TOKEN_RESOLVE);
}
PAL_ENDTRY
return clsHnd;
}
inline void ReaderBase::eeGetCallSiteSig (unsigned sigTok,
CORINFO_MODULE_HANDLE scope,
CORINFO_METHOD_HANDLE context,
CORINFO_SIG_INFO* sigRet,
bool giveUp)
{
m_jitInfo->findCallSiteSig(scope, sigTok, context, sigRet);
if (giveUp &&
sigRet->retTypeClass == NULL &&
verTypeIsComposite(sigRet->retType)
)
{
NO_WAY(MVER_E_TOKEN_RESOLVE);
}
}
inline
void ReaderBase::eeGetMethodSig (CORINFO_METHOD_HANDLE methHnd,
CORINFO_SIG_INFO* sigRet,
bool giveUp,
CORINFO_CLASS_HANDLE owner)
{
m_jitInfo->getMethodSig(methHnd, sigRet, owner);
if (giveUp &&
verTypeIsComposite(sigRet->retType) &&
sigRet->retTypeClass == NULL)
{
NO_WAY(MVER_E_TOKEN_RESOLVE);
}
}
inline vertype ReaderBase::verGetArrayElemType(vertype ti)
{
// you need to check for null explictly since that is a success case
VASSERT(!ti.IsNullObjRef());
VerifyIsSDArray(ti);
CORINFO_CLASS_HANDLE childClassHandle = NULL;
CorInfoType ciType = m_jitInfo->getChildType(ti.GetClassHandleForObjRef(), &childClassHandle);
return verMakeTypeInfo(ciType, childClassHandle);
}
inline void ReaderBase::VerifyIsSDArray(const vertype &ti)
{
if (ti.IsNullObjRef()) // nulls are SD arrays
return;
Verify(ti.IsType(TI_REF), MVER_E_ARRAY_SD);
Verify(m_jitInfo->isSDArray(ti.GetClassHandleForObjRef()), MVER_E_ARRAY_SD);
}
inline bool ReaderBase::verIsByRefLike(const vertype& ti)
{
if (ti.IsByRef())
return TRUE;
if (!ti.IsType(TI_STRUCT))
return FALSE;
if (getClassAttribs(ti.GetClassHandleForValueClass()) & CORINFO_FLG_CONTAINS_STACK_PTR)
return TRUE;
return FALSE;
}
bool ReaderBase::verIsSafeToReturnByRef(const vertype& ti)
{
if (ti.IsPermanentHomeByRef())
{
return TRUE;
}
else
{
return FALSE;
}
}
BOOL ReaderBase::verIsBoxable(const vertype& ti)
{
return ( ti.IsPrimitiveType()
|| ti.IsObjRef() // includes boxed generic type variables
|| ti.IsUnboxedGenericTypeVar()
|| (ti.IsType(TI_STRUCT) &&
!verIsByRefLike(ti)));
}
// Is it a boxed value type?
bool ReaderBase::verIsBoxedValueType(const vertype &ti)
{
if (ti.GetType() == TI_REF)
{
return verIsValueClass(ti.GetClassHandleForObjRef());
}
else
{
return false;
}
}
void ReaderBase::VerifyIsBoxable(const vertype &v)
{
VerifyAndReportFound(verIsBoxable(v), v, MVER_E_VALCLASS_OBJREF_VAR);
}
void ReaderBase::VerifyIsByref(const vertype &v)
{
VerifyAndReportFound(v.IsByRef(), v, MVER_E_STACK_BYREF);
}
inline vertype ReaderBase::verMakeTypeInfo(CORINFO_CLASS_HANDLE clsHnd)
{
if (clsHnd == NULL)
return vertype();
if (verIsValueClass(clsHnd))
{
CorInfoType t = m_jitInfo->getTypeForPrimitiveValueClass(clsHnd);
// Meta-data validation should insure that CORINF_TYPE_BYREF should
// not occur here, so we may want to change this to an assert instead.
switch (t)
{
case CORINFO_TYPE_VOID:
case CORINFO_TYPE_BYREF:
return vertype();
break;
case CORINFO_TYPE_PTR:
Verify(0, MVER_E_UNMANAGED_POINTER);
return vertype();
break;
case CORINFO_TYPE_UNDEF:
case CORINFO_TYPE_VALUECLASS:
return vertype(TI_STRUCT, clsHnd);
break;
default:
return vertype(JITtype2tiType(t));
break;
}
}
else if (verIsGenericTypeVar(clsHnd))
{
// See comment in vertypeInfo.h for why we do it this way.
return(vertype(TI_REF, clsHnd, true));
}
else {
return(vertype(TI_REF, clsHnd));
}
}
vertype ReaderBase::verMakeTypeInfo(CorInfoType ciType, CORINFO_CLASS_HANDLE clsHnd)
{
VASSERT(ciType < CORINFO_TYPE_COUNT);
vertype tiResult;
switch(ciType)
{
case CORINFO_TYPE_STRING:
case CORINFO_TYPE_CLASS:
tiResult = verMakeTypeInfo(clsHnd);
Verify(tiResult.IsType(TI_REF), MVER_E_SIG_C_VC);
break;
case CORINFO_TYPE_VALUECLASS:
case CORINFO_TYPE_REFANY:
tiResult = verMakeTypeInfo(clsHnd);
// type must be constant with element type;
Verify(tiResult.IsValueClass(), MVER_E_SIG_VC_C);
break;
case CORINFO_TYPE_VAR:
return verMakeTypeInfo(clsHnd);
break;
case CORINFO_TYPE_PTR:
return vertype(TI_PTR);
break;
case CORINFO_TYPE_VOID:
return vertype();
break;
case CORINFO_TYPE_BYREF:
{
CORINFO_CLASS_HANDLE childClassHandle;
CorInfoType childType = m_jitInfo->getChildType(clsHnd, &childClassHandle);
// cannot have a byref to a byref
VerifyAndReportFound(childType != CORINFO_TYPE_BYREF,
tiResult, MVER_E_SIG_BYREF_BYREF);
tiResult = verMakeTypeInfo(childType, childClassHandle);
if (childClassHandle)
{
VerifyAndReportFound(!(CORINFO_FLG_CONTAINS_STACK_PTR & getClassAttribs(childClassHandle)),
tiResult, MVER_E_SIG_BYREF_TB_AH);
}
return ByRef(tiResult);
}
break;
default:
VASSERT(clsHnd != BAD_CLASS_HANDLE);
if (clsHnd) // If we have more precise information, use it
return vertype(TI_STRUCT, clsHnd);
else
return vertype(JITtype2tiType(ciType));
}
return tiResult;
}
inline bool verTypeIsGC(CorInfoType type)
{
return type == CORINFO_TYPE_CLASS || type == CORINFO_TYPE_BYREF;
}
inline vertype ReaderBase::verParseArgSigToTypeInfo(CORINFO_SIG_INFO* sig,
CORINFO_ARG_LIST_HANDLE args)
{
CORINFO_CLASS_HANDLE classHandle = NULL;
CorInfoType ciType = strip(m_jitInfo->getArgType(sig, args, &classHandle));
if (verTypeIsGC(ciType))
{
// For efficiency, getArgType only returns something in classHandle for
// value types. For other types that have addition type info, you
// have to call back explicitly
classHandle = m_jitInfo->getArgClass(sig, args);
//"Could not figure out Class specified in argument or local signature"
if (!classHandle)
BADCODE(MVER_E_TOKEN_RESOLVE);
VASSERT(classHandle);
}
return verMakeTypeInfo(ciType, classHandle);
}
inline CORINFO_CLASS_HANDLE ReaderBase::verGetExactMethodClass(mdMemberRef token, CORINFO_METHOD_HANDLE method)
{
mdToken parentTok = getMemberParent(token);
// if method may be shared, find the exact parent from the token
if (TypeFromToken(parentTok) == mdtTypeSpec)
return verGetClassHandle(parentTok);
else
return getMethodClass(method);
}
inline CORINFO_CLASS_HANDLE ReaderBase::verGetExactFieldClass(mdMemberRef token, CORINFO_FIELD_HANDLE field)
{
mdToken parentTok = getMemberParent(token);
// if field may be shared, find the exact parent from the token
if(TypeFromToken(parentTok) == mdtTypeSpec)
return verGetClassHandle(parentTok);
else
return getFieldClass(field);
}
/*****************************************************************************
*
* Functions to get various handles
*/
inline
CORINFO_CLASS_HANDLE ReaderBase::verifyClass(
CORINFO_CLASS_HANDLE cls
){
if (cls == 0) {
NO_WAY(MVER_E_TOKEN_RESOLVE);
}
return(cls);
}
inline
CORINFO_METHOD_HANDLE ReaderBase::verifyMethodHandle(
CORINFO_METHOD_HANDLE method
){
if (method == 0) {
BADCODE(MVER_E_TOKEN_RESOLVE);
}
return (method);
}
vertype
ReaderBase::verVerifyLDIND(
const vertype& ptr,
ti_types instrType
){
vertype ptrVal;
VASSERT(instrType != TI_STRUCT);
if (ptr.IsByRef())
{
ptrVal = DereferenceByRef(ptr);
if (instrType == TI_REF)
{
VerifyIsObjRef(ptrVal);
}
else
{
VerifyEqual(vertype(ptrVal).MakeByRef(), vertype(instrType).MakeByRef());
VerifyAndReportFound(instrType == ptrVal.GetRawType(), ptr,
MVER_E_STACK_UNEXPECTED);
}
}
else {
VerifyAndReportFound(false, ptr, MVER_E_STACK_BYREF);
}
return ptrVal;
}
vertype ReaderBase::verVerifySTIND(const vertype& ptr,
const vertype& value, ti_types instrType)
{
Verify(!ptr.IsReadonlyByRef(), MVER_E_READONLY_ILLEGAL_WRITE);//"write to readonly byref"
vertype ptrVal = verVerifyLDIND(ptr, instrType);
VerifyCompatibleWith(value, vertype(ptrVal).NormaliseForStack());
return ptrVal;
}
BOOL ReaderBase::verIsCallToInitThisPtr(CORINFO_CLASS_HANDLE context,
CORINFO_CLASS_HANDLE target)
{
// Either target == context, in this case calling an alternate .ctor
// Or target is the immediate parent of context
return ((target == context) ||
(target == m_jitInfo->getParentType(context)));
}
/*****************************************************************************
* Checks that a delegate creation is done using the following pattern:
* dup
* ldvirtftn targetMemberRef
* OR
* ldftn targetMemberRef
*
* 'delegateCreateStart' points at the last dup or ldftn in this basic block (null if
* not in this basic block)
*
* targetMemberRef is read from the code sequence.
* targetMemberRef is validated iff verificationNeeded.
*/
bool ReaderBase::verCheckDelegateCreation(ReaderBaseNS::OPCODE opcode,
VerificationState *vstate,
const BYTE *codeAddr,
mdMemberRef &targetMemberRef,
vertype ftnType,
vertype objType)
{
unsigned char *pToken;
unsigned char *delegateCreateStart = (unsigned char *) vstate->delegateCreateStart;
if (!delegateCreateStart)
return false;
unsigned int index = 0;
if (codeAddr - delegateCreateStart == 6) // LDFTN <TOK> takes 6 bytes
{
if (ParseMSILOpcode(delegateCreateStart, &pToken, &index, this) != ReaderBaseNS::CEE_LDFTN)
return false;
else
{
DWORD targetFlags = getMethodAttribs(ftnType.m_method);
if ((targetFlags & CORINFO_FLG_VIRTUAL) && ((targetFlags & CORINFO_FLG_FINAL) == 0))
{
if (!verIsBoxedValueType(objType))
{
Verify(objType.IsThisPtr(), MVER_E_LDFTN_NON_FINAL_VIRTUAL);
Verify(!m_thisPtrModified, MVER_E_LDFTN_NON_FINAL_VIRTUAL);
}
}
targetMemberRef = ReadToken(pToken);
return true;
}
}
else if (codeAddr - delegateCreateStart == 7) // DUP LDVIRTFTN <TOK> takes 7 bytes
{
if (ParseMSILOpcode(delegateCreateStart, NULL, &index, this) != ReaderBaseNS::CEE_DUP)
return false;
if (ParseMSILOpcode(delegateCreateStart + index, &pToken, &index, this) != ReaderBaseNS::CEE_LDVIRTFTN)
return false;
targetMemberRef = ReadToken(pToken);
return true;
}
else
return false;
}
void ReaderBase::verVerifyCall (
ReaderBaseNS::OPCODE opcode,
mdToken memberRef,
bool tailCall,
const BYTE* codeAddr,
VerificationState* vstate
){
CORINFO_METHOD_HANDLE methodHnd;
DWORD mflags;
CORINFO_SIG_INFO sig;
unsigned int popCount = 0;
bool isInlining = false;
unsigned int argCount = 0;
CORINFO_ARG_LIST_HANDLE args = NULL;
VerifyIsCallToken(memberRef);
methodHnd = verifyMethodHandle(getMethodHandle(memberRef));
mflags = getMethodAttribs(methodHnd);
// When verifying we always need the exact, never the shared, class of the method
// (getMethodClass will return a shared class if the method handle is in a shared class)
CORINFO_CLASS_HANDLE methodClassHnd = verGetExactMethodClass(memberRef, methodHnd);
VASSERT(methodClassHnd);
eeGetMethodSig(methodHnd, &sig, false, methodClassHnd);
if (sig.isVarArg())
eeGetCallSiteSig(memberRef, getCurrentModuleHandle(), getCurrentMethodHandle(), &sig, !isInlining);
DWORD clsFlags = getClassAttribs(methodClassHnd);
// opcode specific check
unsigned methodClassFlgs = getClassAttribs(methodClassHnd);
VASSERT(!tailCall); // Importer should not allow this
Verify((mflags & CORINFO_FLG_CONSTRUCTOR) && !(mflags & CORINFO_FLG_STATIC), MVER_E_CTOR);
if (methodClassFlgs & CORINFO_FLG_DELEGATE)
{
Verify(sig.numArgs == 2, MVER_E_DLGT_CTOR);
vertype tiDeclaredObj = verParseArgSigToTypeInfo(&sig, sig.args).NormaliseForStack();
vertype tiDeclaredFtn = verParseArgSigToTypeInfo(&sig, m_jitInfo->getArgNext(sig.args)).NormaliseForStack();
//"ftn arg needs to be a type IntPtr"
Verify(tiDeclaredFtn.IsType(TI_I), MVER_E_DLGT_SIG_I);
VASSERT(popCount == 0);
vertype tiActualObj = vstate->impStackTop(1);
vertype tiActualFtn = vstate->impStackTop(0);
// delegate needs method as first arg
Verify(tiActualFtn.IsMethod(), MVER_E_STACK_METHOD);
VerifyCompatibleWith(tiActualObj, tiDeclaredObj); // delegate object mismatch
Verify(tiActualObj.IsNullObjRef() || tiActualObj.IsObjRef(), MVER_E_DLGT_SIG_O);
CORINFO_CLASS_HANDLE objTypeHandle = tiActualObj.IsNullObjRef() ? NULL
: tiActualObj.GetClassHandleForObjRef();
mdToken delegateMethodRef = mdTokenNil;
Verify(verCheckDelegateCreation(opcode, vstate, codeAddr, delegateMethodRef,
tiActualFtn, tiActualObj),
MVER_E_DLGT_PATTERN); //"must create delegates with certain IL"
// the method signature must be compatible with the delegate's invoke method
// It is crucial to have verified verCheckDelegateCreation before using vstate->delegateMethodRef below;
// This ensures delegateMethodRef was last set by a valid delegate creation sequence
// For now, we just check it is at least a valid token.
VerifyIsDirectCallToken(delegateMethodRef);
VASSERT(delegateMethodRef == vstate->delegateMethodRef);
CORINFO_CLASS_HANDLE parentTypeHandle = verGetExactMethodClass(delegateMethodRef, tiActualFtn.GetMethod());
Verify(m_jitInfo->isCompatibleDelegate(objTypeHandle,
parentTypeHandle,
tiActualFtn.GetMethod(),
methodClassHnd,
getCurrentModuleHandle(),
delegateMethodRef,
memberRef),
MVER_E_DLGT_CTOR);
// in the case of protected methods, it is a requirement that the 'this'
// pointer be a subclass of the current context. Perform this check
BOOL targetIsStatic = m_jitInfo->getMethodAttribs(
tiActualFtn.GetMethod(),
getCurrentMethodHandle()) & CORINFO_FLG_STATIC;
CORINFO_CLASS_HANDLE instanceClassHnd = getCurrentMethodClass();
if (!(tiActualObj.IsNullObjRef() || targetIsStatic))
instanceClassHnd = tiActualObj.GetClassHandleForObjRef();
Verify(m_jitInfo->satisfiesClassConstraints(parentTypeHandle),
MVER_E_UNSATISFIED_METHOD_PARENT_INST); //"delegate target has unsatisfied class constraints"
Verify(m_jitInfo->satisfiesMethodConstraints(parentTypeHandle,tiActualFtn.GetMethod()),
MVER_E_UNSATISFIED_METHOD_INST); //"delegate target has unsatisfied method constraints");
Verify(m_jitInfo->canAccessMethod(getCurrentMethodHandle(),
parentTypeHandle,
tiActualFtn.GetMethod(),
instanceClassHnd),
MVER_E_METHOD_ACCESS);
goto DONE_ARGS;
}
Verify(!(mflags & CORINFO_FLG_ABSTRACT), MVER_E_CALL_ABSTRACT);
Verify(!((mflags & CORINFO_FLG_CONSTRUCTOR) && (methodClassFlgs & CORINFO_FLG_DELEGATE)),
MVER_E_DLGT_PATTERN); //"can only newobj a delegate constructor"
// check compatibility of the arguments
argCount = sig.numArgs;
args = sig.args;
while (argCount--)
{
vertype tiActual = vstate->impStackTop(popCount+argCount);
vertype tiDeclared = verParseArgSigToTypeInfo(&sig, args).NormaliseForStack();
VerifyCompatibleWith(tiActual, tiDeclared);
// check that the argument is not a byref for tailcalls
if (tailCall)
VerifyAndReportFound(!verIsByRefLike(tiDeclared), tiDeclared, MVER_E_TAIL_BYREF);
args = m_jitInfo->getArgNext(args);
}
DONE_ARGS:
// update popCount
popCount += sig.numArgs;
// check for 'this' which are on non-static methods, not called via NEWOBJ
CORINFO_CLASS_HANDLE instanceClassHnd = getCurrentMethodClass();
if (!(mflags & CORINFO_FLG_STATIC) && (opcode != ReaderBaseNS::CEE_NEWOBJ))
{
vertype tiThis = vstate->impStackTop(popCount);
popCount++;
if (getClassAttribs(methodClassHnd) & CORINFO_FLG_ARRAY)
methodClassHnd = m_jitInfo->findMethodClass(getCurrentModuleHandle(), memberRef, getCurrentMethodHandle());
// If it is null, we assume we can access it (since it will AV shortly)
// If it is anything but a refernce class, there is no hierarchy, so
// again, we don't need the precise instance class to compute 'protected' access
if (tiThis.IsType(TI_REF))
instanceClassHnd = tiThis.GetClassHandleForObjRef();
// Check type compatability of the this argument
vertype tiDeclaredThis = verMakeTypeInfo(methodClassHnd);
// this enables the following construct:
// local : native int pbase,
// ldarga.s pbase
// call instance int32 System.IntPtr::ToInt32()
if (tiDeclaredThis.IsValueClass())
tiDeclaredThis.MakeByRef();
// If this is a call to the base class .ctor, set thisPtr Init for
// this block.
if (mflags & CORINFO_FLG_CONSTRUCTOR)
{
if (m_verTrackObjCtorInitState && tiThis.IsThisPtr()
&& verIsCallToInitThisPtr(getCurrentMethodClass(),methodClassHnd))
{
vstate->setThisInitialized();
vstate->thisInitializedThisBlock = true;
tiThis.SetInitialisedObjRef();
}
else
{
// We allow direct calls to value type constructors
Verify(tiThis.IsByRef(), MVER_E_CALL_CTOR);
}
}
VerifyCompatibleWith(tiThis, tiDeclaredThis);
// also check the specil tailcall rule
VerifyAndReportFound(!(tailCall && verIsByRefLike(tiDeclaredThis)), tiDeclaredThis, MVER_E_TAIL_BYREF);
}
// check any constraints on the callee's class and type parameters
Verify(m_jitInfo->satisfiesClassConstraints(methodClassHnd),
MVER_E_UNSATISFIED_METHOD_PARENT_INST); //"method has unsatisfied class constraints
Verify(m_jitInfo->satisfiesMethodConstraints(methodClassHnd,methodHnd),
MVER_E_UNSATISFIED_METHOD_INST); //"method has unsatisfied method constraints"
// check access permission
Verify(m_jitInfo->canAccessMethod(getCurrentMethodHandle(),
methodClassHnd,
methodHnd,
instanceClassHnd), MVER_E_METHOD_ACCESS);
// special checks for tailcalls
if (tailCall)
{
// void return type gets morphed into the error type, so we have to treat them specially here
if (sig.retType == CORINFO_TYPE_VOID)
{
Verify(m_methodInfo->args.retType == CORINFO_TYPE_VOID, MVER_E_TAIL_RET_VOID);
}
else if (m_methodInfo->args.retType == CORINFO_TYPE_VOID)
{
Verify(false, MVER_E_TAIL_RET_TYPE);
}
else
{
vertype tiCalleeRetType = verMakeTypeInfo(sig.retType, sig.retTypeClass);
vertype tiCallerRetType = verMakeTypeInfo(m_methodInfo->args.retType,
m_methodInfo->args.retTypeClass);
VerifyCompatibleWith(NormaliseForStack(tiCalleeRetType),
NormaliseForStack(tiCallerRetType));
}
// for tailcall, stack must be empty
}
if (sig.retType != CORINFO_TYPE_VOID)
{
if (clsFlags & CORINFO_FLG_ARRAY)
{
eeGetCallSiteSig(memberRef, getCurrentModuleHandle(), getCurrentMethodHandle(), &sig);
}
vertype tiRetVal = verMakeTypeInfo(sig.retType, sig.retTypeClass);
tiRetVal.NormaliseForStack();
vstate->push(tiRetVal);
}
}
// ////////////////////////////////////////////////////////////////////////////////
// OPCODE VERIFICATION
// ////////////////////////////////////////////////////////////////////////////////
void
ReaderBase::initVerifyInfo(
void
){
m_needsRuntimeCallout = false;
m_verificationNeeded = verifyNeedsVerification(&m_canSkipVerificationResult);
m_isVerifiableCode = TRUE; // assume the code is verifiable unless proven otherwise
m_verBBList = NULL;
m_verTrackObjCtorInitState = true;
m_thisPtrModified = false;
// initialize and check constraints
if (m_verificationNeeded)
{
BOOL hasCircularClassConstraints,hasCircularMethodConstraints;
m_jitInfo->initConstraintsForVerification(getCurrentMethodHandle(),
&hasCircularClassConstraints,
&hasCircularMethodConstraints);
if (hasCircularClassConstraints)
BADCODE(MVER_E_CIRCULAR_VAR_CONSTRAINTS);
if (hasCircularMethodConstraints)
BADCODE(MVER_E_CIRCULAR_MVAR_CONSTRAINTS);
}
else if (!(m_flags & CORJIT_FLG_SKIP_VERIFICATION))
{
CorInfoCanSkipVerificationResult verResult = m_jitInfo->canSkipMethodVerification(getCurrentMethodHandle(), false);
if (verResult == CORINFO_VERIFICATION_RUNTIME_CHECK)
{
m_verificationNeeded = true;
m_needsRuntimeCallout = true;
}
}
}
void ReaderBase::verifyFinishBlock(VerificationState* vstate, FlowGraphNode * block)
{
PGLOBALVERIFYDATA gvData;
bool blockIsBad;
if (!m_verificationNeeded) return;
gvData = FgNodeGetGlobalVerifyData(block);
ASSERTNR(gvData);
blockIsBad = vstate->blockIsBad;
if (!blockIsBad)
{
if (vstate->tailPrefix == true) {
Verify(0, MVER_E_TAIL_CALL);
blockIsBad = true;
}
if (vstate->unalignedPrefix == true) {
blockIsBad = true;
Verify(0, MVER_E_UNALIGNED);
}
if (vstate->volatilePrefix == true) {
blockIsBad = true;
Verify(0, MVER_E_VOLATILE);
}
if (vstate->readonlyPrefix == true) {
blockIsBad = true;
Verify(0, MVER_E_BAD_READONLY_PREFIX);
}
if (vstate->constrainedPrefix == true) {
blockIsBad = true;
Verify(0, MVER_E_BAD_CONSTRAINED_PREFIX);
}
}
gvData->blockIsBad = blockIsBad;
gvData->thisInitialized = vstate->isThisInitialized();
// take this node off the worklist, if there is a cycle
// then it may get added back on later
if (gvData->isOnWorklist) {
// remove from worklist
gvData->isOnWorklist = FALSE;
if (gvData->worklistPrev)
gvData->worklistPrev->worklistNext = gvData->worklistNext;
else
m_gvWorklistHead = gvData->worklistNext;
if (gvData->worklistNext)
gvData->worklistNext->worklistPrev = gvData->worklistPrev;
else
m_gvWorklistTail = gvData->worklistPrev;
}
if (blockIsBad) return;
// Global verification must propagate stack across block bounds.
for (FlowGraphEdgeList * fgEdge = FgNodeGetSuccessorList(block);
fgEdge;
fgEdge = FgEdgeListGetNextSuccessor(fgEdge))
{
FlowGraphNode * succ = FgEdgeListGetSink(fgEdge);
PGLOBALVERIFYDATA gvSucc = FgNodeGetGlobalVerifyData(succ);
ASSERTNR(gvSucc);