This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
forked from gluck/ikvm
/
verifier.cs
3766 lines (3627 loc) · 112 KB
/
verifier.cs
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) 2002-2010 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using IKVM.Internal;
using InstructionFlags = IKVM.Internal.ClassFile.Method.InstructionFlags;
using ExceptionTableEntry = IKVM.Internal.ClassFile.Method.ExceptionTableEntry;
sealed class InstructionState
{
private struct LocalStoreSites
{
private int[] data;
private int count;
private bool shared;
internal LocalStoreSites Copy()
{
LocalStoreSites n = new LocalStoreSites();
n.data = data;
n.count = count;
n.shared = true;
return n;
}
internal static LocalStoreSites Alloc()
{
LocalStoreSites n = new LocalStoreSites();
n.data = new int[4];
return n;
}
internal void Add(int store)
{
for(int i = 0; i < count; i++)
{
if(data[i] == store)
{
return;
}
}
if(count == data.Length)
{
int[] newarray = new int[data.Length * 2];
Buffer.BlockCopy(data, 0, newarray, 0, data.Length * 4);
data = newarray;
shared = false;
}
if(shared)
{
shared = false;
data = (int[])data.Clone();
}
data[count++] = store;
}
internal int this[int index]
{
get
{
return data[index];
}
}
internal int Count
{
get
{
return count;
}
}
internal static void MarkShared(LocalStoreSites[] localStoreSites)
{
for(int i = 0; i < localStoreSites.Length; i++)
{
localStoreSites[i].shared = true;
}
}
}
private TypeWrapper[] stack;
private int stackSize;
private int stackEnd;
private TypeWrapper[] locals;
private bool unitializedThis;
internal bool changed = true;
private enum ShareFlags : byte
{
None = 0,
Stack = 1,
Locals = 2,
All = Stack | Locals
}
private ShareFlags flags;
private InstructionState(TypeWrapper[] stack, int stackSize, int stackEnd, TypeWrapper[] locals, bool unitializedThis)
{
this.flags = ShareFlags.All;
this.stack = stack;
this.stackSize = stackSize;
this.stackEnd = stackEnd;
this.locals = locals;
this.unitializedThis = unitializedThis;
}
internal InstructionState(int maxLocals, int maxStack)
{
this.flags = ShareFlags.None;
this.stack = new TypeWrapper[maxStack];
this.stackEnd = maxStack;
this.locals = new TypeWrapper[maxLocals];
}
internal InstructionState Copy()
{
return new InstructionState(stack, stackSize, stackEnd, locals, unitializedThis);
}
internal void CopyTo(InstructionState target)
{
target.flags = ShareFlags.All;
target.stack = stack;
target.stackSize = stackSize;
target.stackEnd = stackEnd;
target.locals = locals;
target.unitializedThis = unitializedThis;
target.changed = true;
}
internal InstructionState CopyLocals()
{
InstructionState copy = new InstructionState(new TypeWrapper[stack.Length], 0, stack.Length, locals, unitializedThis);
copy.flags &= ~ShareFlags.Stack;
return copy;
}
public static InstructionState operator+(InstructionState s1, InstructionState s2)
{
if(s1 == null)
{
return s2.Copy();
}
if(s1.stackSize != s2.stackSize || s1.stackEnd != s2.stackEnd)
{
throw new VerifyError(string.Format("Inconsistent stack height: {0} != {1}",
s1.stackSize + s1.stack.Length - s1.stackEnd,
s2.stackSize + s2.stack.Length - s2.stackEnd));
}
InstructionState s = s1.Copy();
s.changed = s1.changed;
for(int i = 0; i < s.stackSize; i++)
{
TypeWrapper type = s.stack[i];
TypeWrapper type2 = s2.stack[i];
if(type == type2)
{
// perfect match, nothing to do
}
else if((type == VerifierTypeWrapper.ExtendedDouble && type2 == PrimitiveTypeWrapper.DOUBLE)
|| (type2 == VerifierTypeWrapper.ExtendedDouble && type == PrimitiveTypeWrapper.DOUBLE))
{
if(type != VerifierTypeWrapper.ExtendedDouble)
{
s.StackCopyOnWrite();
s.stack[i] = VerifierTypeWrapper.ExtendedDouble;
s.changed = true;
}
}
else if((type == VerifierTypeWrapper.ExtendedFloat && type2 == PrimitiveTypeWrapper.FLOAT)
|| (type2 == VerifierTypeWrapper.ExtendedFloat && type == PrimitiveTypeWrapper.FLOAT))
{
if(type != VerifierTypeWrapper.ExtendedFloat)
{
s.StackCopyOnWrite();
s.stack[i] = VerifierTypeWrapper.ExtendedFloat;
s.changed = true;
}
}
else if(!type.IsPrimitive)
{
TypeWrapper baseType = InstructionState.FindCommonBaseType(type, type2);
if(baseType == VerifierTypeWrapper.Invalid)
{
throw new VerifyError(string.Format("cannot merge {0} and {1}", type.Name, type2.Name));
}
if(type != baseType)
{
s.StackCopyOnWrite();
s.stack[i] = baseType;
s.changed = true;
}
}
else
{
throw new VerifyError(string.Format("cannot merge {0} and {1}", type.Name, type2.Name));
}
}
for(int i = 0; i < s.locals.Length; i++)
{
TypeWrapper type = s.locals[i];
TypeWrapper type2 = s2.locals[i];
TypeWrapper baseType = InstructionState.FindCommonBaseType(type, type2);
if(type != baseType)
{
s.LocalsCopyOnWrite();
s.locals[i] = baseType;
s.changed = true;
}
}
if(!s.unitializedThis && s2.unitializedThis)
{
s.unitializedThis = true;
s.changed = true;
}
return s;
}
private static LocalStoreSites MergeStoreSites(LocalStoreSites h1, LocalStoreSites h2)
{
if(h1.Count == 0)
{
return h2.Copy();
}
if(h2.Count == 0)
{
return h1.Copy();
}
LocalStoreSites h = h1.Copy();
for(int i = 0; i < h2.Count; i++)
{
h.Add(h2[i]);
}
return h;
}
internal void SetUnitializedThis(bool state)
{
unitializedThis = state;
}
internal void CheckUninitializedThis()
{
if(unitializedThis)
{
throw new VerifyError("Base class constructor wasn't called");
}
}
internal static TypeWrapper FindCommonBaseType(TypeWrapper type1, TypeWrapper type2)
{
if(type1 == type2)
{
return type1;
}
if(type1 == VerifierTypeWrapper.Null)
{
return type2;
}
if(type2 == VerifierTypeWrapper.Null)
{
return type1;
}
if(type1 == VerifierTypeWrapper.Invalid || type2 == VerifierTypeWrapper.Invalid)
{
return VerifierTypeWrapper.Invalid;
}
if(VerifierTypeWrapper.IsFaultBlockException(type1))
{
VerifierTypeWrapper.ClearFaultBlockException(type1);
return FindCommonBaseType(CoreClasses.java.lang.Throwable.Wrapper, type2);
}
if(VerifierTypeWrapper.IsFaultBlockException(type2))
{
VerifierTypeWrapper.ClearFaultBlockException(type2);
return FindCommonBaseType(type1, CoreClasses.java.lang.Throwable.Wrapper);
}
if(type1.IsPrimitive || type2.IsPrimitive)
{
return VerifierTypeWrapper.Invalid;
}
if(type1 == VerifierTypeWrapper.UninitializedThis || type2 == VerifierTypeWrapper.UninitializedThis)
{
return VerifierTypeWrapper.Invalid;
}
if(VerifierTypeWrapper.IsNew(type1) || VerifierTypeWrapper.IsNew(type2))
{
return VerifierTypeWrapper.Invalid;
}
if(VerifierTypeWrapper.IsThis(type1))
{
type1 = ((VerifierTypeWrapper)type1).UnderlyingType;
}
if(VerifierTypeWrapper.IsThis(type2))
{
type2 = ((VerifierTypeWrapper)type2).UnderlyingType;
}
if(type1.IsUnloadable || type2.IsUnloadable)
{
return VerifierTypeWrapper.Unloadable;
}
if(type1.ArrayRank > 0 && type2.ArrayRank > 0)
{
int rank = 1;
int rank1 = type1.ArrayRank - 1;
int rank2 = type2.ArrayRank - 1;
TypeWrapper elem1 = type1.ElementTypeWrapper;
TypeWrapper elem2 = type2.ElementTypeWrapper;
while(rank1 != 0 && rank2 != 0)
{
elem1 = elem1.ElementTypeWrapper;
elem2 = elem2.ElementTypeWrapper;
rank++;
rank1--;
rank2--;
}
// NOTE arrays of value types have special merging semantics!
// NOTE we don't have to test for the case where the element types are the same, because that
// is only relevant if the ranks are the same, but if that is the case the types are completely
// identical, in which case the identity test at the top of this method already returned.
TypeWrapper baseType;
if(elem1.IsPrimitive || elem2.IsPrimitive || elem1.IsNonPrimitiveValueType || elem2.IsNonPrimitiveValueType)
{
baseType = CoreClasses.java.lang.Object.Wrapper;
rank--;
if(rank == 0)
{
return baseType;
}
}
else
{
baseType = FindCommonBaseTypeHelper(elem1, elem2);
}
return baseType.MakeArrayType(rank);
}
return FindCommonBaseTypeHelper(type1, type2);
}
private static TypeWrapper FindCommonBaseTypeHelper(TypeWrapper t1, TypeWrapper t2)
{
if(t1 == t2)
{
return t1;
}
if(t1.IsInterface || t2.IsInterface)
{
// NOTE according to a paper by Alessandro Coglio & Allen Goldberg titled
// "Type Safety in the JVM: Some Problems in Java 2 SDK 1.2 and Proposed Solutions"
// the common base of two interfaces is java.lang.Object, and there is special
// treatment for java.lang.Object types that allow it to be assigned to any interface
// type, the JVM's typesafety then depends on the invokeinterface instruction to make
// sure that the reference actually implements the interface.
// NOTE the ECMA CLI spec also specifies this interface merging algorithm, so we can't
// really do anything more clever than this.
return CoreClasses.java.lang.Object.Wrapper;
}
Stack<TypeWrapper> st1 = new Stack<TypeWrapper>();
Stack<TypeWrapper> st2 = new Stack<TypeWrapper>();
while(t1 != null)
{
st1.Push(t1);
t1 = t1.BaseTypeWrapper;
}
while(t2 != null)
{
st2.Push(t2);
t2 = t2.BaseTypeWrapper;
}
TypeWrapper type = null;
for(;;)
{
t1 = st1.Count > 0 ? st1.Pop() : null;
t2 = st2.Count > 0 ? st2.Pop() : null;
if(t1 != t2)
{
return type;
}
type = t1;
}
}
private void SetLocal1(int index, TypeWrapper type)
{
try
{
LocalsCopyOnWrite();
if(index > 0 && locals[index - 1] != VerifierTypeWrapper.Invalid && locals[index - 1].IsWidePrimitive)
{
locals[index - 1] = VerifierTypeWrapper.Invalid;
}
locals[index] = type;
}
catch(IndexOutOfRangeException)
{
throw new VerifyError("Illegal local variable number");
}
}
private void SetLocal2(int index, TypeWrapper type)
{
try
{
LocalsCopyOnWrite();
if(index > 0 && locals[index - 1] != VerifierTypeWrapper.Invalid && locals[index - 1].IsWidePrimitive)
{
locals[index - 1] = VerifierTypeWrapper.Invalid;
}
locals[index] = type;
locals[index + 1] = VerifierTypeWrapper.Invalid;
}
catch(IndexOutOfRangeException)
{
throw new VerifyError("Illegal local variable number");
}
}
internal void GetLocalInt(int index)
{
if(GetLocalType(index) != PrimitiveTypeWrapper.INT)
{
throw new VerifyError("Invalid local type");
}
}
internal void SetLocalInt(int index, int instructionIndex)
{
SetLocal1(index, PrimitiveTypeWrapper.INT);
}
internal void GetLocalLong(int index)
{
if(GetLocalType(index) != PrimitiveTypeWrapper.LONG)
{
throw new VerifyError("incorrect local type, not long");
}
}
internal void SetLocalLong(int index, int instructionIndex)
{
SetLocal2(index, PrimitiveTypeWrapper.LONG);
}
internal void GetLocalFloat(int index)
{
if(GetLocalType(index) != PrimitiveTypeWrapper.FLOAT)
{
throw new VerifyError("incorrect local type, not float");
}
}
internal void SetLocalFloat(int index, int instructionIndex)
{
SetLocal1(index, PrimitiveTypeWrapper.FLOAT);
}
internal void GetLocalDouble(int index)
{
if(GetLocalType(index) != PrimitiveTypeWrapper.DOUBLE)
{
throw new VerifyError("incorrect local type, not double");
}
}
internal void SetLocalDouble(int index, int instructionIndex)
{
SetLocal2(index, PrimitiveTypeWrapper.DOUBLE);
}
internal TypeWrapper GetLocalType(int index)
{
try
{
return locals[index];
}
catch(IndexOutOfRangeException)
{
throw new VerifyError("Illegal local variable number");
}
}
// this is used by the compiler (indirectly, through MethodAnalyzer.GetLocalTypeWrapper),
// we've already verified the code so we know we won't run outside the array boundary,
// and we don't need to record the fact that we're reading the local.
internal TypeWrapper GetLocalTypeEx(int index)
{
return locals[index];
}
internal void SetLocalType(int index, TypeWrapper type, int instructionIndex)
{
if(type.IsWidePrimitive)
{
SetLocal2(index, type);
}
else
{
SetLocal1(index, type);
}
}
internal void PushType(TypeWrapper type)
{
if(type.IsIntOnStackPrimitive)
{
type = PrimitiveTypeWrapper.INT;
}
PushHelper(type);
}
internal void PushInt()
{
PushHelper(PrimitiveTypeWrapper.INT);
}
internal void PushLong()
{
PushHelper(PrimitiveTypeWrapper.LONG);
}
internal void PushFloat()
{
PushHelper(PrimitiveTypeWrapper.FLOAT);
}
internal void PushExtendedFloat()
{
PushHelper(VerifierTypeWrapper.ExtendedFloat);
}
internal void PushDouble()
{
PushHelper(PrimitiveTypeWrapper.DOUBLE);
}
internal void PushExtendedDouble()
{
PushHelper(VerifierTypeWrapper.ExtendedDouble);
}
internal void PopInt()
{
if(PopAnyType() != PrimitiveTypeWrapper.INT)
{
throw new VerifyError("Int expected on stack");
}
}
internal bool PopFloat()
{
TypeWrapper tw = PopAnyType();
if(tw != PrimitiveTypeWrapper.FLOAT && tw != VerifierTypeWrapper.ExtendedFloat)
{
throw new VerifyError("Float expected on stack");
}
return tw == VerifierTypeWrapper.ExtendedFloat;
}
internal bool PopDouble()
{
TypeWrapper tw = PopAnyType();
if(tw != PrimitiveTypeWrapper.DOUBLE && tw != VerifierTypeWrapper.ExtendedDouble)
{
throw new VerifyError("Double expected on stack");
}
return tw == VerifierTypeWrapper.ExtendedDouble;
}
internal void PopLong()
{
if(PopAnyType() != PrimitiveTypeWrapper.LONG)
{
throw new VerifyError("Long expected on stack");
}
}
internal TypeWrapper PopArrayType()
{
TypeWrapper type = PopAnyType();
if(!VerifierTypeWrapper.IsNullOrUnloadable(type) && type.ArrayRank == 0)
{
throw new VerifyError("Array reference expected on stack");
}
return type;
}
// null or an initialized object reference
internal TypeWrapper PopObjectType()
{
TypeWrapper type = PopType();
if(type.IsPrimitive || VerifierTypeWrapper.IsNew(type) || type == VerifierTypeWrapper.UninitializedThis)
{
throw new VerifyError("Expected object reference on stack");
}
return type;
}
// null or an initialized object reference derived from baseType (or baseType)
internal TypeWrapper PopObjectType(TypeWrapper baseType)
{
TypeWrapper type = PopObjectType();
// HACK because of the way interfaces references works, if baseType
// is an interface or array of interfaces, any reference will be accepted
if(!baseType.IsUnloadable && !baseType.IsInterfaceOrInterfaceArray && !(type.IsUnloadable || type.IsAssignableTo(baseType)))
{
throw new VerifyError("Unexpected type " + type.Name + " where " + baseType.Name + " was expected");
}
return type;
}
internal TypeWrapper PeekType()
{
if(stackSize == 0)
{
throw new VerifyError("Unable to pop operand off an empty stack");
}
return stack[stackSize - 1];
}
internal void MultiPopAnyType(int count)
{
while(count-- != 0)
{
PopAnyType();
}
}
internal TypeWrapper PopFaultBlockException()
{
return stack[--stackSize];
}
internal TypeWrapper PopAnyType()
{
if(stackSize == 0)
{
throw new VerifyError("Unable to pop operand off an empty stack");
}
TypeWrapper type = stack[--stackSize];
if(type.IsWidePrimitive || type == VerifierTypeWrapper.ExtendedDouble)
{
stackEnd++;
}
if(VerifierTypeWrapper.IsThis(type))
{
type = ((VerifierTypeWrapper)type).UnderlyingType;
}
if(VerifierTypeWrapper.IsFaultBlockException(type))
{
VerifierTypeWrapper.ClearFaultBlockException(type);
type = CoreClasses.java.lang.Throwable.Wrapper;
}
return type;
}
// NOTE this can *not* be used to pop double or long
internal TypeWrapper PopType()
{
TypeWrapper type = PopAnyType();
if(type.IsWidePrimitive || type == VerifierTypeWrapper.ExtendedDouble)
{
throw new VerifyError("Attempt to split long or double on the stack");
}
return type;
}
// this will accept null, a primitive type of the specified type or an initialized reference of the
// specified type or derived from it
// NOTE this can also be used to pop double or long
internal TypeWrapper PopType(TypeWrapper baseType)
{
if(baseType.IsIntOnStackPrimitive)
{
baseType = PrimitiveTypeWrapper.INT;
}
TypeWrapper type = PopAnyType();
if(VerifierTypeWrapper.IsNew(type) || type == VerifierTypeWrapper.UninitializedThis)
{
throw new VerifyError("Expecting to find object/array on stack");
}
if(type != baseType &&
!((type.IsUnloadable && !baseType.IsPrimitive) || (baseType.IsUnloadable && !type.IsPrimitive) ||
type.IsAssignableTo(baseType)))
{
// HACK because of the way interfaces references works, if baseType
// is an interface or array of interfaces, any reference will be accepted
if(baseType.IsInterfaceOrInterfaceArray && !type.IsPrimitive)
{
return type;
}
if(type == VerifierTypeWrapper.ExtendedDouble && baseType == PrimitiveTypeWrapper.DOUBLE)
{
return type;
}
if(type == VerifierTypeWrapper.ExtendedFloat && baseType == PrimitiveTypeWrapper.FLOAT)
{
return type;
}
throw new VerifyError("Unexpected type " + type.Name + " where " + baseType.Name + " was expected");
}
return type;
}
internal int GetStackHeight()
{
return stackSize;
}
internal TypeWrapper GetStackSlot(int pos)
{
TypeWrapper tw = stack[stackSize - 1 - pos];
if(tw == VerifierTypeWrapper.ExtendedDouble)
{
tw = PrimitiveTypeWrapper.DOUBLE;
}
else if(tw == VerifierTypeWrapper.ExtendedFloat)
{
tw = PrimitiveTypeWrapper.FLOAT;
}
return tw;
}
internal TypeWrapper GetStackSlotEx(int pos)
{
return stack[stackSize - 1 - pos];
}
internal TypeWrapper GetStackByIndex(int index)
{
return stack[index];
}
private void PushHelper(TypeWrapper type)
{
if(type.IsWidePrimitive || type == VerifierTypeWrapper.ExtendedDouble)
{
stackEnd--;
}
if(stackSize >= stackEnd)
{
throw new VerifyError("Stack overflow");
}
StackCopyOnWrite();
stack[stackSize++] = type;
}
internal void MarkInitialized(TypeWrapper type, TypeWrapper initType, int instructionIndex)
{
System.Diagnostics.Debug.Assert(type != null && initType != null);
for(int i = 0; i < locals.Length; i++)
{
if(locals[i] == type)
{
LocalsCopyOnWrite();
locals[i] = initType;
}
}
for(int i = 0; i < stackSize; i++)
{
if(stack[i] == type)
{
StackCopyOnWrite();
stack[i] = initType;
}
}
}
private void StackCopyOnWrite()
{
if((flags & ShareFlags.Stack) != 0)
{
flags &= ~ShareFlags.Stack;
stack = (TypeWrapper[])stack.Clone();
}
}
private void LocalsCopyOnWrite()
{
if((flags & ShareFlags.Locals) != 0)
{
flags &= ~ShareFlags.Locals;
locals = (TypeWrapper[])locals.Clone();
}
}
internal void DumpLocals()
{
Console.Write("// ");
string sep = "";
for(int i = 0; i < locals.Length; i++)
{
Console.Write(sep);
Console.Write(locals[i]);
sep = ", ";
}
Console.WriteLine();
}
internal void DumpStack()
{
Console.Write("// ");
string sep = "";
for(int i = 0; i < stackSize; i++)
{
Console.Write(sep);
Console.Write(stack[i]);
sep = ", ";
}
Console.WriteLine();
}
// this method ensures that no uninitialized object are in the current state
internal void CheckUninitializedObjRefs()
{
for(int i = 0; i < locals.Length; i++)
{
TypeWrapper type = locals[i];
if(VerifierTypeWrapper.IsNew(type))
{
throw new VerifyError("uninitialized object ref in local (2)");
}
}
for(int i = 0; i < stackSize; i++)
{
TypeWrapper type = stack[i];
if(VerifierTypeWrapper.IsNew(type))
{
throw new VerifyError("uninitialized object ref on stack");
}
}
}
internal void ClearFaultBlockException()
{
if(VerifierTypeWrapper.IsFaultBlockException(stack[0]))
{
StackCopyOnWrite();
changed = true;
stack[0] = CoreClasses.java.lang.Throwable.Wrapper;
}
}
}
struct StackState
{
private InstructionState state;
private int sp;
internal StackState(InstructionState state)
{
this.state = state;
sp = state.GetStackHeight();
}
internal TypeWrapper PeekType()
{
if(sp == 0)
{
throw new VerifyError("Unable to pop operand off an empty stack");
}
TypeWrapper type = state.GetStackByIndex(sp - 1);
if(VerifierTypeWrapper.IsThis(type))
{
type = ((VerifierTypeWrapper)type).UnderlyingType;
}
return type;
}
internal TypeWrapper PopAnyType()
{
if(sp == 0)
{
throw new VerifyError("Unable to pop operand off an empty stack");
}
TypeWrapper type = state.GetStackByIndex(--sp);
if(VerifierTypeWrapper.IsThis(type))
{
type = ((VerifierTypeWrapper)type).UnderlyingType;
}
if(VerifierTypeWrapper.IsFaultBlockException(type))
{
VerifierTypeWrapper.ClearFaultBlockException(type);
type = CoreClasses.java.lang.Throwable.Wrapper;
}
return type;
}
internal TypeWrapper PopType(TypeWrapper baseType)
{
if(baseType.IsIntOnStackPrimitive)
{
baseType = PrimitiveTypeWrapper.INT;
}
TypeWrapper type = PopAnyType();
if(VerifierTypeWrapper.IsNew(type) || type == VerifierTypeWrapper.UninitializedThis)
{
throw new VerifyError("Expecting to find object/array on stack");
}
if(type != baseType &&
!((type.IsUnloadable && !baseType.IsPrimitive) || (baseType.IsUnloadable && !type.IsPrimitive) ||
type.IsAssignableTo(baseType)))
{
// HACK because of the way interfaces references works, if baseType
// is an interface or array of interfaces, any reference will be accepted
if(baseType.IsInterfaceOrInterfaceArray && !type.IsPrimitive)
{
return type;
}
if(type == VerifierTypeWrapper.ExtendedDouble && baseType == PrimitiveTypeWrapper.DOUBLE)
{
return type;
}
if(type == VerifierTypeWrapper.ExtendedFloat && baseType == PrimitiveTypeWrapper.FLOAT)
{
return type;
}
throw new VerifyError("Unexpected type " + type.Name + " where " + baseType.Name + " was expected");
}
return type;
}
// NOTE this can *not* be used to pop double or long
internal TypeWrapper PopType()
{
TypeWrapper type = PopAnyType();
if(type.IsWidePrimitive || type == VerifierTypeWrapper.ExtendedDouble)
{
throw new VerifyError("Attempt to split long or double on the stack");
}
return type;
}
internal void PopInt()
{
if(PopAnyType() != PrimitiveTypeWrapper.INT)
{
throw new VerifyError("Int expected on stack");
}
}
internal void PopFloat()
{
TypeWrapper tw = PopAnyType();
if(tw != PrimitiveTypeWrapper.FLOAT && tw != VerifierTypeWrapper.ExtendedFloat)
{
throw new VerifyError("Float expected on stack");
}
}
internal void PopDouble()
{
TypeWrapper tw = PopAnyType();
if(tw != PrimitiveTypeWrapper.DOUBLE && tw != VerifierTypeWrapper.ExtendedDouble)
{
throw new VerifyError("Double expected on stack");
}
}
internal void PopLong()
{
if(PopAnyType() != PrimitiveTypeWrapper.LONG)
{
throw new VerifyError("Long expected on stack");
}
}
internal TypeWrapper PopArrayType()
{
TypeWrapper type = PopAnyType();
if(!VerifierTypeWrapper.IsNullOrUnloadable(type) && type.ArrayRank == 0)
{
throw new VerifyError("Array reference expected on stack");
}
return type;
}
// either null or an initialized object reference