-
Notifications
You must be signed in to change notification settings - Fork 330
/
JVMClassInfo.java
950 lines (776 loc) · 34.3 KB
/
JVMClassInfo.java
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
/*
* Copyright (C) 2014, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The Java Pathfinder core (jpf-core) platform is licensed under the
* Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gov.nasa.jpf.jvm;
import gov.nasa.jpf.Config;
import gov.nasa.jpf.util.Misc;
import gov.nasa.jpf.util.StringSetMatcher;
import gov.nasa.jpf.vm.*;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.LinkedHashMap;
/**
* a ClassInfo that was created from a Java classfile
*/
public class JVMClassInfo extends ClassInfo {
/**
* this is the inner class that does the actual ClassInfo initialization from ClassFile. It is an inner class so that
* (a) it can set ClassInfo fields, (b) it can extend ClassFileReaderAdapter, and (c) we don't clutter JVMClassInfo with
* fields that are only temporarily used during parsing
*/
class Initializer extends ClassFileReaderAdapter {
protected ClassFile cf;
protected JVMCodeBuilder cb;
public Initializer (ClassFile cf, JVMCodeBuilder cb) throws ClassParseException {
this.cf = cf;
this.cb = cb;
cf.parse(this);
}
@Override
public void setClass (ClassFile cf, String clsName, String superClsName, int flags, int cpCount) throws ClassParseException {
JVMClassInfo.this.setClass(clsName, superClsName, flags, cpCount);
}
@Override
public void setClassAttribute (ClassFile cf, int attrIndex, String name, int attrLength) {
if (name == ClassFile.SOURCE_FILE_ATTR) {
cf.parseSourceFileAttr(this, null);
} else if (name == ClassFile.SIGNATURE_ATTR) {
cf.parseSignatureAttr(this, JVMClassInfo.this);
} else if (name == ClassFile.RUNTIME_VISIBLE_ANNOTATIONS_ATTR) {
cf.parseAnnotationsAttr(this, JVMClassInfo.this);
} else if (name == ClassFile.RUNTIME_INVISIBLE_ANNOTATIONS_ATTR) {
//cf.parseAnnotationsAttr(this, ClassInfo.this);
} else if (name == ClassFile.RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR) {
cf.parseTypeAnnotationsAttr(this, JVMClassInfo.this);
} else if (name == ClassFile.INNER_CLASSES_ATTR) {
cf.parseInnerClassesAttr(this, JVMClassInfo.this);
} else if (name == ClassFile.ENCLOSING_METHOD_ATTR) {
cf.parseEnclosingMethodAttr(this, JVMClassInfo.this);
} else if (name == ClassFile.BOOTSTRAP_METHOD_ATTR) {
cf.parseBootstrapMethodAttr(this, JVMClassInfo.this);
}
}
@Override
public void setBootstrapMethodCount (ClassFile cf, Object tag, int count) {
bootstrapMethods = new BootstrapMethodInfo[count];
}
@Override
public void setBootstrapMethod (ClassFile cf, Object tag, int idx, int refKind, String cls, String mth, String descriptor, int[] cpArgs) {
int lambdaRefKind = cf.mhRefTypeAt(cpArgs[1]);
int mrefIdx = cf.mhMethodRefIndexAt(cpArgs[1]);
String clsName = cf.methodClassNameAt(mrefIdx).replace('/', '.');
ClassInfo eclosingLambdaCls;
if(!clsName.equals(JVMClassInfo.this.getName())) {
eclosingLambdaCls = ClassLoaderInfo.getCurrentResolvedClassInfo(clsName);
} else {
eclosingLambdaCls = JVMClassInfo.this;
}
assert (eclosingLambdaCls!=null);
String mthName = cf.methodNameAt(mrefIdx);
String signature = cf.methodDescriptorAt(mrefIdx);
MethodInfo lambdaBody = eclosingLambdaCls.getMethod(mthName + signature, false);
String samDescriptor = cf.methodTypeDescriptorAt(cpArgs[2]);
if(lambdaBody!=null) {
bootstrapMethods[idx] = new BootstrapMethodInfo(lambdaRefKind, JVMClassInfo.this, lambdaBody, samDescriptor);
}
}
//--- inner/enclosing classes
@Override
public void setInnerClassCount (ClassFile cf, Object tag, int classCount) {
innerClassNames = new String[classCount];
}
@Override
public void setInnerClass (ClassFile cf, Object tag, int innerClsIndex,
String outerName, String innerName, String innerSimpleName, int accessFlags) {
// Ok, this is a total mess - some names are in dot notation, others use '/'
// and to make it even more confusing, some InnerClass attributes refer NOT
// to the currently parsed class, so we have to check if we are the outerName,
// but then 'outerName' can also be null instead of our own name.
// Oh, and there are also InnerClass attributes that have their own name as inner names
// (see java/lang/String$CaseInsensitiveComparator or ...System and java/lang/System$1 for instance)
if (outerName != null) {
outerName = Types.getClassNameFromTypeName(outerName);
}
innerName = Types.getClassNameFromTypeName(innerName);
if (!innerName.equals(name)) {
innerClassNames[innerClsIndex] = innerName;
} else {
// this refers to ourself, and can be a force fight with setEnclosingMethod
if (outerName != null) { // only set if this is a direct member, otherwise taken from setEnclosingMethod
setEnclosingClass(outerName);
}
}
}
@Override
public void setEnclosingMethod (ClassFile cf, Object tag, String enclosingClassName, String enclosingMethodName, String descriptor) {
setEnclosingClass(enclosingClassName);
if (enclosingMethodName != null) {
JVMClassInfo.this.setEnclosingMethod(enclosingMethodName + descriptor);
}
}
@Override
public void setInnerClassesDone (ClassFile cf, Object tag) {
// we have to check if we allocated too many - see the mess above
for (int i = 0; i < innerClassNames.length; i++) {
innerClassNames = Misc.stripNullElements(innerClassNames);
}
}
//--- source file
@Override
public void setSourceFile (ClassFile cf, Object tag, String fileName) {
JVMClassInfo.this.setSourceFile(fileName);
}
//--- interfaces
@Override
public void setInterfaceCount (ClassFile cf, int ifcCount) {
interfaceNames = new String[ifcCount];
}
@Override
public void setInterface (ClassFile cf, int ifcIndex, String ifcName) {
interfaceNames[ifcIndex] = Types.getClassNameFromTypeName(ifcName);
}
//--- fields
// unfortunately they are stored together in the ClassFile, i.e. we
// have to split them up once we are done
protected FieldInfo[] fields;
protected FieldInfo curFi; // need to cache for attributes
@Override
public void setFieldCount (ClassFile cf, int fieldCount) {
if (fieldCount > 0){
fields = new FieldInfo[fieldCount];
} else {
fields = null;
}
}
@Override
public void setField (ClassFile cf, int fieldIndex, int accessFlags, String name, String descriptor) {
FieldInfo fi = FieldInfo.create(name, descriptor, accessFlags);
fields[fieldIndex] = fi;
curFi = fi; // for attributes
}
@Override
public void setFieldAttribute (ClassFile cf, int fieldIndex, int attrIndex, String name, int attrLength) {
if (name == ClassFile.SIGNATURE_ATTR) {
cf.parseSignatureAttr(this, curFi);
} else if (name == ClassFile.CONST_VALUE_ATTR) {
cf.parseConstValueAttr(this, curFi);
} else if (name == ClassFile.RUNTIME_VISIBLE_ANNOTATIONS_ATTR) {
cf.parseAnnotationsAttr(this, curFi);
} else if (name == ClassFile.RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR) {
cf.parseTypeAnnotationsAttr(this, curFi);
} else if (name == ClassFile.RUNTIME_INVISIBLE_ANNOTATIONS_ATTR) {
//cf.parseAnnotationsAttr(this, curFi);
}
}
@Override
public void setConstantValue (ClassFile cf, Object tag, Object constVal) {
curFi.setConstantValue(constVal);
}
@Override
public void setFieldsDone (ClassFile cf) {
setFields(fields);
}
//--- declaredMethods
protected MethodInfo curMi;
@Override
public void setMethodCount (ClassFile cf, int methodCount) {
methods = new LinkedHashMap<String, MethodInfo>();
}
@Override
public void setMethod (ClassFile cf, int methodIndex, int accessFlags, String name, String signature) {
MethodInfo mi = MethodInfo.create(name, signature, accessFlags);
curMi = mi;
}
@Override
public void setMethodDone (ClassFile cf, int methodIndex){
curMi.setLocalVarAnnotations();
JVMClassInfo.this.setMethod(curMi);
}
@Override
public void setMethodAttribute (ClassFile cf, int methodIndex, int attrIndex, String name, int attrLength) {
if (name == ClassFile.CODE_ATTR) {
cf.parseCodeAttr(this, curMi);
} else if (name == ClassFile.SIGNATURE_ATTR) {
cf.parseSignatureAttr(this, curMi);
} else if (name == ClassFile.EXCEPTIONS_ATTR) {
cf.parseExceptionAttr(this, curMi);
} else if (name == ClassFile.RUNTIME_VISIBLE_ANNOTATIONS_ATTR) {
cf.parseAnnotationsAttr(this, curMi);
} else if (name == ClassFile.RUNTIME_INVISIBLE_ANNOTATIONS_ATTR) {
//cf.parseAnnotationsAttr(this, curMi);
} else if (name == ClassFile.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR) {
cf.parseParameterAnnotationsAttr(this, curMi);
} else if (name == ClassFile.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR) {
//cf.parseParameterAnnotationsAttr(this, curMi);
} else if (name == ClassFile.RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR) {
cf.parseTypeAnnotationsAttr(this, curMi);
}
}
//--- current methods throws list
protected String[] exceptions;
@Override
public void setExceptionCount (ClassFile cf, Object tag, int exceptionCount) {
exceptions = new String[exceptionCount];
}
@Override
public void setException (ClassFile cf, Object tag, int exceptionIndex, String exceptionType) {
exceptions[exceptionIndex] = Types.getClassNameFromTypeName(exceptionType);
}
@Override
public void setExceptionsDone (ClassFile cf, Object tag) {
curMi.setThrownExceptions(exceptions);
}
//--- current method exception handlers
protected ExceptionHandler[] handlers;
@Override
public void setExceptionHandlerTableCount (ClassFile cf, Object tag, int exceptionTableCount) {
handlers = new ExceptionHandler[exceptionTableCount];
}
@Override
public void setExceptionHandler (ClassFile cf, Object tag, int handlerIndex,
int startPc, int endPc, int handlerPc, String catchType) {
ExceptionHandler xh = new ExceptionHandler(catchType, startPc, endPc, handlerPc);
handlers[handlerIndex] = xh;
}
@Override
public void setExceptionHandlerTableDone (ClassFile cf, Object tag) {
curMi.setExceptionHandlers(handlers);
}
//--- current method code
@Override
public void setCode (ClassFile cf, Object tag, int maxStack, int maxLocals, int codeLength) {
curMi.setMaxLocals(maxLocals);
curMi.setMaxStack(maxStack);
cb.reset(cf, curMi);
cf.parseBytecode(cb, tag, codeLength);
cb.installCode();
}
@Override
public void setCodeAttribute (ClassFile cf, Object tag, int attrIndex, String name, int attrLength) {
if (name == ClassFile.LINE_NUMBER_TABLE_ATTR) {
cf.parseLineNumberTableAttr(this, tag);
} else if (name == ClassFile.LOCAL_VAR_TABLE_ATTR) {
cf.parseLocalVarTableAttr(this, tag);
} else if (name == ClassFile.RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR){
cf.parseTypeAnnotationsAttr(this, tag);
}
}
//--- current method line numbers
protected int[] lines, startPcs;
@Override
public void setLineNumberTableCount (ClassFile cf, Object tag, int lineNumberCount) {
lines = new int[lineNumberCount];
startPcs = new int[lineNumberCount];
}
@Override
public void setLineNumber (ClassFile cf, Object tag, int lineIndex, int lineNumber, int startPc) {
lines[lineIndex] = lineNumber;
startPcs[lineIndex] = startPc;
}
@Override
public void setLineNumberTableDone (ClassFile cf, Object tag) {
curMi.setLineNumbers(lines, startPcs);
}
//--- current method local variables
protected LocalVarInfo[] localVars;
@Override
public void setLocalVarTableCount (ClassFile cf, Object tag, int localVarCount) {
localVars = new LocalVarInfo[localVarCount];
}
@Override
public void setLocalVar (ClassFile cf, Object tag, int localVarIndex,
String varName, String descriptor, int scopeStartPc, int scopeEndPc, int slotIndex) {
LocalVarInfo lvi = new LocalVarInfo(varName, descriptor, "", scopeStartPc, scopeEndPc, slotIndex);
localVars[localVarIndex] = lvi;
}
@Override
public void setLocalVarTableDone (ClassFile cf, Object tag) {
curMi.setLocalVarTable(localVars);
}
//--- annotations
protected AnnotationInfo[] annotations;
protected AnnotationInfo curAi;
protected AnnotationInfo[][] parameterAnnotations;
protected Object[] values;
//--- declaration annotations
@Override
public void setAnnotationCount (ClassFile cf, Object tag, int annotationCount) {
annotations = new AnnotationInfo[annotationCount];
}
@Override
public void setAnnotation (ClassFile cf, Object tag, int annotationIndex, String annotationType) {
if (tag instanceof InfoObject) {
curAi = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
annotations[annotationIndex] = curAi;
}
}
@Override
public void setAnnotationsDone (ClassFile cf, Object tag) {
if (tag instanceof InfoObject) {
((InfoObject) tag).addAnnotations(annotations);
}
}
@Override
public void setParameterCount (ClassFile cf, Object tag, int parameterCount) {
parameterAnnotations = new AnnotationInfo[parameterCount][];
}
@Override
public void setParameterAnnotationCount (ClassFile cf, Object tag, int paramIndex, int annotationCount) {
annotations = new AnnotationInfo[annotationCount];
parameterAnnotations[paramIndex] = annotations;
}
@Override
public void setParameterAnnotation (ClassFile cf, Object tag, int annotationIndex, String annotationType) {
curAi = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
annotations[annotationIndex] = curAi;
}
@Override
public void setParametersDone (ClassFile cf, Object tag) {
curMi.setParameterAnnotations(parameterAnnotations);
}
//--- Java 8 type annotations
@Override
public void setTypeAnnotationCount(ClassFile cf, Object tag, int annotationCount){
annotations = new AnnotationInfo[annotationCount];
}
@Override
public void setTypeParameterAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int typeIndex, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new TypeParameterAnnotationInfo(base, targetType, typePath, typeIndex);
annotations[annotationIndex] = curAi;
}
@Override
public void setSuperTypeAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int superTypeIdx, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new SuperTypeAnnotationInfo(base, targetType, typePath, superTypeIdx);
annotations[annotationIndex] = curAi;
}
@Override
public void setTypeParameterBoundAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int typeIndex, int boundIndex, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new TypeParameterBoundAnnotationInfo(base, targetType, typePath, typeIndex, boundIndex);
annotations[annotationIndex] = curAi;
}
@Override
public void setTypeAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new TypeAnnotationInfo(base, targetType, typePath);
annotations[annotationIndex] = curAi;
}
@Override
public void setFormalParameterAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int paramIndex, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new FormalParameterAnnotationInfo(base, targetType, typePath, paramIndex);
annotations[annotationIndex] = curAi;
}
@Override
public void setThrowsAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int throwsTypeIdx, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new ThrowsAnnotationInfo(base, targetType, typePath, throwsTypeIdx);
annotations[annotationIndex] = curAi;
}
@Override
public void setVariableAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
long[] scopeEntries, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
VariableAnnotationInfo vai = new VariableAnnotationInfo(base, targetType, typePath, scopeEntries);
curAi = vai;
annotations[annotationIndex] = curAi;
}
@Override
public void setExceptionParameterAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int exceptionIndex, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi= new ExceptionParameterAnnotationInfo(base, targetType, typePath, exceptionIndex);
annotations[annotationIndex] = curAi;
}
@Override
public void setBytecodeAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int offset, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new BytecodeAnnotationInfo(base, targetType, typePath, offset);
annotations[annotationIndex] = curAi;
}
@Override
public void setBytecodeTypeParameterAnnotation(ClassFile cf, Object tag, int annotationIndex, int targetType,
int offset, int typeArgIdx, short[] typePath, String annotationType){
AnnotationInfo base = getResolvedAnnotationInfo(Types.getClassNameFromTypeName(annotationType));
curAi = new BytecodeTypeParameterAnnotationInfo(base, targetType, typePath, offset, typeArgIdx);
annotations[annotationIndex] = curAi;
}
@Override
public void setTypeAnnotationsDone(ClassFile cf, Object tag) {
if (tag instanceof InfoObject) {
int len = annotations.length;
AbstractTypeAnnotationInfo[] tais = new AbstractTypeAnnotationInfo[annotations.length];
for (int i=0; i<len; i++){
tais[i] = (AbstractTypeAnnotationInfo)annotations[i];
}
// we can get them in batches (e.g. VariableTypeAnnos from code attrs and ReturnTypeAnnos from method attrs
((InfoObject) tag).addTypeAnnotations( tais);
}
}
//--- AnnotationInfo values entries
@Override
public void setAnnotationValueCount (ClassFile cf, Object tag, int annotationIndex, int nValuePairs) {
// if we have values, we need to clone the defined annotation so that we can overwrite entries
curAi = curAi.cloneForOverriddenValues();
annotations[annotationIndex] = curAi;
}
@Override
public void setPrimitiveAnnotationValue (ClassFile cf, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, Object val) {
if (arrayIndex >= 0) {
values[arrayIndex] = val;
} else {
curAi.setClonedEntryValue(elementName, val);
}
}
@Override
public void setStringAnnotationValue (ClassFile cf, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, String val) {
if (arrayIndex >= 0) {
values[arrayIndex] = val;
} else {
curAi.setClonedEntryValue(elementName, val);
}
}
@Override
public void setClassAnnotationValue (ClassFile cf, Object tag, int annotationIndex, int valueIndex, String elementName,
int arrayIndex, String typeName) {
Object val = AnnotationInfo.getClassValue(typeName);
if (arrayIndex >= 0) {
values[arrayIndex] = val;
} else {
curAi.setClonedEntryValue(elementName, val);
}
}
@Override
public void setEnumAnnotationValue (ClassFile cf, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, String enumType, String enumValue) {
Object val = AnnotationInfo.getEnumValue(enumType, enumValue);
if (arrayIndex >= 0) {
values[arrayIndex] = val;
} else {
curAi.setClonedEntryValue(elementName, val);
}
}
@Override
public void setAnnotationValueElementCount (ClassFile cf, Object tag, int annotationIndex, int valueIndex,
String elementName, int elementCount) {
values = new Object[elementCount];
}
@Override
public void setAnnotationValueElementsDone (ClassFile cf, Object tag, int annotationIndex, int valueIndex, String elementName) {
curAi.setClonedEntryValue(elementName, values);
}
//--- common attrs
@Override
public void setSignature (ClassFile cf, Object tag, String signature) {
if (tag instanceof GenericSignatureHolder) {
((GenericSignatureHolder) tag).setGenericSignature(signature);
}
}
}
// since nested class init locking can explode the state space, we make it optional and controllable
protected static boolean nestedInit;
protected static StringSetMatcher includeNestedInit;
protected static StringSetMatcher excludeNestedInit;
protected static boolean init (Config config){
nestedInit = config.getBoolean("jvm.nested_init", false);
if (nestedInit){
includeNestedInit = StringSetMatcher.getNonEmpty(config.getStringArray("jvm.nested_init.include"));
excludeNestedInit = StringSetMatcher.getNonEmpty(config.getStringArray("jvm.nested_init.exclude"));
}
return true;
}
JVMClassInfo (String name, ClassLoaderInfo cli, ClassFile cf, String srcUrl, JVMCodeBuilder cb) throws ClassParseException {
super( name, cli, srcUrl);
new Initializer( cf, cb); // we just need the ctor
resolveAndLink();
}
//--- for annotation classinfos
// called on the annotation classinfo
@Override
protected ClassInfo createAnnotationProxy (String proxyName){
return new JVMClassInfo (this, proxyName, classLoader, null);
}
// concrete proxy ctor
protected JVMClassInfo (ClassInfo ciAnnotation, String proxyName, ClassLoaderInfo cli, String url) {
super( ciAnnotation, proxyName, cli, url);
}
/**
* This is called on the functional interface type. It creates a synthetic type which
* implements the functional interface and contains a method capturing the behavior
* of the lambda expression.
*/
@Override
protected ClassInfo createFuncObjClassInfo (BootstrapMethodInfo bootstrapMethod, String name, String samUniqueName, String[] fieldTypesName) {
return new JVMClassInfo(this, bootstrapMethod, name, samUniqueName, fieldTypesName);
}
protected JVMClassInfo (ClassInfo funcInterface, BootstrapMethodInfo bootstrapMethod, String name, String samUniqueName, String[] fieldTypesName) {
super(funcInterface, bootstrapMethod, name, fieldTypesName);
// creating a method corresponding to the single abstract method of the functional interface
methods = new HashMap<String, MethodInfo>();
MethodInfo fiMethod = funcInterface.getInterfaceAbstractMethod();
int modifiers = fiMethod.getModifiers() & (~Modifier.ABSTRACT);
int nLocals = fiMethod.getArgumentsSize();
int nOperands = this.nInstanceFields + nLocals;
MethodInfo mi = new MethodInfo(fiMethod.getName(), fiMethod.getSignature(), modifiers, nLocals, nOperands);
mi.linkToClass(this);
methods.put(mi.getUniqueName(), mi);
setLambdaDirectCallCode(mi, bootstrapMethod);
try {
resolveAndLink();
} catch (ClassParseException e) {
// we do not even get here - this a synthetic class, and at this point
// the interfaces are already loaded.
}
}
/**
* perform initialization of this class and its not-yet-initialized superclasses (top down),
* which includes calling clinit() methods
*
* This is overridden here to model a questionable yet consequential behavior of hotspot, which
* is holding derived class locks when initializing base classes. The generic implementation in
* ClassInfo uses non-nested locks (i.e. A.clinit() only synchronizes on A.class) and hence cannot
* produce the same static init deadlocks as hotspot. In order to catch such defects we implement
* nested locking here.
*
* The main difference is that the generic implementation only pushes DCSFs for required clinits
* and otherwise doesn't lock anything. Here, we create one static init specific DCSF which wraps
* all clinits in nested monitorenter/exits. We create this even if there is no clinit so that we
* mimic hotspot locking.
*
* Note this scheme also enables us to get rid of the automatic clinit sync (they don't have
* a 0x20 sync modifier in classfiles)
*
* @return true if client needs to re-execute because we pushed DirectCallStackFrames
*/
@Override
public boolean initializeClass(ThreadInfo ti) {
if (needsInitialization(ti)) {
if (nestedInit && StringSetMatcher.isMatch(name, includeNestedInit, excludeNestedInit)) {
registerClass(ti); // this is recursively upwards
int nOps = 2 * (getNumberOfSuperClasses() + 1); // this is just an upper bound for the number of operands we need
MethodInfo miInitialize = new MethodInfo("[initializeClass]", "()V", Modifier.STATIC, 0, nOps);
JVMDirectCallStackFrame frame = new JVMDirectCallStackFrame(miInitialize, null);
JVMCodeBuilder cb = getSystemCodeBuilder(null, miInitialize);
addClassInit(ti, frame, cb); // this is recursively upwards until we hit a initialized superclass
cb.directcallreturn();
cb.installCode();
// this is normally initialized in the ctor, but at that point we don't have the code yet
frame.setPC(miInitialize.getFirstInsn());
ti.pushFrame(frame);
return true; // client has to re-execute, we pushed a stackframe
} else { // use generic initialization without nested locks (directly calling clinits)
return super.initializeClass(ti);
}
} else {
return false; // nothing to do
}
}
protected void addClassInit (ThreadInfo ti, JVMDirectCallStackFrame frame, JVMCodeBuilder cb){
int clsObjRef = getClassObjectRef();
frame.pushRef(clsObjRef);
cb.monitorenter();
if (superClass != null && superClass.needsInitialization(ti)) {
((JVMClassInfo) superClass).addClassInit(ti, frame, cb); // go recursive
}
if (getMethod("<clinit>()V", false) != null) { // do we have a clinit
cb.invokeclinit(this);
} else {
cb.finishclinit(this);
// we can't just do call ci.setInitialized() since that has to be deferred
}
frame.pushRef(clsObjRef);
cb.monitorexit();
}
//--- call processing
protected JVMCodeBuilder getSystemCodeBuilder (ClassFile cf, MethodInfo mi){
JVMSystemClassLoaderInfo sysCl = (JVMSystemClassLoaderInfo) ClassLoaderInfo.getCurrentSystemClassLoader();
JVMCodeBuilder cb = sysCl.getSystemCodeBuilder(cf, mi);
return cb;
}
/**
* to be called from super proxy ctor
* this needs to be in the VM specific ClassInfo because we need to create code
*/
@Override
protected void setAnnotationValueGetterCode (MethodInfo pmi, FieldInfo fi){
JVMCodeBuilder cb = getSystemCodeBuilder(null, pmi);
cb.aload(0);
cb.getfield( pmi.getName(), name, pmi.getReturnType());
if (fi.isReference()) {
cb.areturn();
} else {
if (fi.getStorageSize() == 1) {
cb.ireturn();
} else {
cb.lreturn();
}
}
cb.installCode();
}
@Override
protected void setDirectCallCode (MethodInfo miDirectCall, MethodInfo miCallee){
JVMCodeBuilder cb = getSystemCodeBuilder(null, miDirectCall);
String calleeName = miCallee.getName();
String calleeSig = miCallee.getSignature();
if (miCallee.isStatic()){
if (miCallee.isClinit()) {
cb.invokeclinit(this);
} else {
cb.invokestatic( name, calleeName, calleeSig);
}
} else if (name.equals("<init>") || miCallee.isPrivate()){
cb.invokespecial( name, calleeName, calleeSig);
} else {
cb.invokevirtual( name, calleeName, calleeSig);
}
cb.directcallreturn();
cb.installCode();
}
@Override
protected void setNativeCallCode (NativeMethodInfo miNative){
JVMCodeBuilder cb = getSystemCodeBuilder(null, miNative);
cb.executenative(miNative);
cb.nativereturn();
cb.installCode();
}
@Override
protected void setRunStartCode (MethodInfo miStub, MethodInfo miRun){
JVMCodeBuilder cb = getSystemCodeBuilder(null, miStub);
cb.runStart( miStub);
cb.invokevirtual( name, miRun.getName(), miRun.getSignature());
cb.directcallreturn();
cb.installCode();
}
/**
* This method creates the body of the function object method that captures the
* lambda behavior.
*/
@Override
protected void setLambdaDirectCallCode (MethodInfo miDirectCall, BootstrapMethodInfo bootstrapMethod) {
MethodInfo miCallee = bootstrapMethod.getLambdaBody();
String samSignature = bootstrapMethod.getSamDescriptor();
JVMCodeBuilder cb = getSystemCodeBuilder(null, miDirectCall);
String calleeName = miCallee.getName();
String calleeSig = miCallee.getSignature();
ClassInfo callerCi = miDirectCall.getClassInfo();
// loading free variables, which are used in the body of the lambda
// expression and captured by the lexical scope. These variables
// are stored by the fields of the synthetic function object class
int n = callerCi.getNumberOfInstanceFields();
for(int i=0; i<n; i++) {
cb.aload(0);
FieldInfo fi = callerCi.getInstanceField(i);
cb.getfield(fi.getName(), callerCi.getName(), Types.getTypeSignature(fi.getSignature(), false));
}
// adding bytecode instructions to load input parameters of the lambda expression
n = miDirectCall.getArgumentsSize();
for(int i=1; i<n; i++) {
cb.aload(i);
}
String calleeClass = miCallee.getClassName();
// adding the bytecode instruction to invoke lambda method
switch (bootstrapMethod.getLambdaRefKind()) {
case ClassFile.REF_INVOKESTATIC:
cb.invokestatic(calleeClass, calleeName, calleeSig);
break;
case ClassFile.REF_INVOKEINTERFACE:
cb.invokeinterface(calleeClass, calleeName, calleeSig);
break;
case ClassFile.REF_INVOKEVIRTUAL:
cb.invokevirtual(calleeClass, calleeName, calleeSig);
break;
case ClassFile.REF_NEW_INVOKESPECIAL:
cb.new_(calleeClass);
cb.invokespecial(calleeClass, calleeName, calleeSig);
break;
case ClassFile.REF_INVOKESPECIAL:
cb.invokespecial(calleeClass, calleeName, calleeSig);
break;
}
String returnType = Types.getReturnTypeSignature(samSignature);
int len = returnType.length();
char c = returnType.charAt(0);
// adding a return statement for function object method
if (len == 1) {
switch (c) {
case 'B':
case 'I':
case 'C':
case 'Z':
case 'S':
cb.ireturn();
break;
case 'D':
cb.dreturn();
break;
case 'J':
cb.lreturn();
break;
case 'F':
cb.freturn();
break;
case 'V':
cb.return_();
break;
}
} else {
cb.areturn();
}
cb.installCode();
}
// create a stack frame that has properly initialized arguments
@Override
public StackFrame createStackFrame (ThreadInfo ti, MethodInfo callee){
if (callee.isMJI()){
NativeMethodInfo nativeCallee = (NativeMethodInfo) callee;
JVMNativeStackFrame calleeFrame = new JVMNativeStackFrame( nativeCallee);
calleeFrame.setArguments( ti);
return calleeFrame;
} else {
JVMStackFrame calleeFrame = new JVMStackFrame( callee);
calleeFrame.setCallArguments( ti);
return calleeFrame;
}
}
@Override
public DirectCallStackFrame createDirectCallStackFrame (ThreadInfo ti, MethodInfo miCallee, int nLocals){
int nOperands = miCallee.getNumberOfCallerStackSlots();
MethodInfo miDirect = new MethodInfo(miCallee, nLocals, nOperands);
setDirectCallCode( miDirect, miCallee);
return new JVMDirectCallStackFrame( miDirect, miCallee);
}
/**
* while this is a normal DirectCallStackFrame, it has different code which has to be created here
*/
@Override
public DirectCallStackFrame createRunStartStackFrame (ThreadInfo ti, MethodInfo miRun){
MethodInfo miDirect = new MethodInfo( miRun, 0, 1);
setRunStartCode( miDirect, miRun);
return new JVMDirectCallStackFrame( miDirect, miRun);
}
}