-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysis.java
More file actions
879 lines (838 loc) · 41.4 KB
/
Analysis.java
File metadata and controls
879 lines (838 loc) · 41.4 KB
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
package staticAnalyzer;
import java.util.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.*;
// http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/types.html
// http://jakarta.apache.org/bcel/apidocs/index.html
class Analysis {
private Analyzer analyzer;
private Vector<BadArrayAccess> reports = new Vector<BadArrayAccess>();
private Set<MethodSignature> methods = new HashSet<MethodSignature>();
private Hashtable<MethodSignature,Variable> method_result =
new Hashtable<MethodSignature,Variable>();
public Analysis( Analyzer analyzer ) {
this.analyzer = analyzer;
}
protected void analyzeMethods( JavaClass jclass ) {
//System.out.println(jclass.getMethods().length+" methods to check");
for( Method m : jclass.getMethods() ) {
// skips method already analyzed
if ( method_result.get(new MethodSignature(m,jclass)) == null )
analyzeMethod(m,jclass);
}
//System.out.println(methods);
}
protected boolean analyzeMethod( Method m, JavaClass j ) {
return analyzeMethod(m,j,null);
}
protected boolean analyzeMethod( Method m, JavaClass j,
Vector<Variable> parameters) {
InstructionList il = new InstructionList(m.getCode().getCode());
MethodSignature ms = new MethodSignature(m,j);
boolean recursive = false;
Variable ret = null;
if( methods.contains(ms) ) {
recursive = true;
// return TOP since we don't know what to return!
ret = new Variable(m.getReturnType().getSignature()
,Variable.Kind.LOCAL
,Variable.DomainValue.TOP
,Integer.MAX_VALUE,0);
} else if ( m.isNative() ) {
// If method is native i don't know anything about it.
// If the method is abstract i don't have the runtime
// information to choose a method to analyze.
ret = new Variable(m.getReturnType().getSignature()
,Variable.Kind.LOCAL
,Variable.DomainValue.TOP
,Integer.MAX_VALUE,0);
} else if ( m.isAbstract() ) {
throw new RuntimeException("Analyzing abstract method!");
} else {
//@DEBUG
System.out.println("Analyzing "+m.getName());
System.out.println(""+il);
methods.add(ms);
InstructionHandle pc = il.getStart();
State state = new State(m,j);
if ( parameters == null ) {
// setup start,state for the method call when in app analysis.
// no need of anything in library (default for now) analysis.
Type tys[] = m.getArgumentTypes();
int i = 0;
for( Type ty : tys ) {
state.getVariables().add(new Variable(ty.getSignature()
,Variable.Kind.LOCAL
,Variable.DomainValue.TOP
,i,0));
i++;
}
} else {
// there are some arguments for the function
for( Variable v : parameters ) {
state.getVariables().add(v);
}
}
// analyze method instructions
state = analyzeInstructions(m,il
,pc.getPosition(),il.getEnd().getPosition(),state,j);
ret = state.getReturn();
}
// remove method from list of called methods in the stack
methods.remove(ms);
method_result.put(ms,ret);
return recursive;
}
protected State analyzeInstructions( Method m, InstructionList il
, int start_pc, int end_pc
, State s
, JavaClass j ) {
InstructionHandle pc = il.findHandle(start_pc);
Instruction i = pc.getInstruction();
int pci = pc.getPosition();
//System.out.println("analyze: start "+start_pc+", end "+end_pc);
while(true) {
pci = pc.getPosition();
if (pci > end_pc || pc == null) {
return s;
}
i = pc.getInstruction();
s.setPC(pci);
System.out.println(pci);
if ( i instanceof FCMPG
|| i instanceof FCMPL ) {
Variable v1,v2;
v1 = s.stackPop();
v2 = s.stackPop();
assert v1.getType().equals("F");
assert v2.getType().equals("F");
s.stackPush(new Variable("I",Variable.Kind.CONST,
Variable.DomainValue.TOP,Integer.MAX_VALUE,pci));
} else if ( i instanceof DCMPL
|| i instanceof DCMPG ) {
Variable v1,v2;
v1 = s.stackPop();
v2 = s.stackPop();
assert v1.getType().equals("D");
assert v2.getType().equals("D");
s.stackPush(new Variable("I",Variable.Kind.CONST,
Variable.DomainValue.TOP,Integer.MAX_VALUE,pci));
} else if ( i instanceof ConstantPushInstruction ) {
ConstantPushInstruction icpi = (ConstantPushInstruction)i;
ConstantPoolGen gen = new ConstantPoolGen(m.getConstantPool());
int n = icpi.getValue().intValue();
String signature = icpi.getType(gen).getSignature();
Variable.DomainValue f;
if ( n > 0 ) {
f = Variable.DomainValue.G0;
} else if ( n >= 0 ) {
f = Variable.DomainValue.GEQ0;
} else {
f = Variable.DomainValue.TOP;
}
Variable v = new Variable(signature,Variable.Kind.CONST
,f,Integer.MAX_VALUE
,pci);
s.stackPush(v);
} else if ( i instanceof ACONST_NULL ) {
s.stackPush(new Variable("Ljava/lang/Object",Variable.Kind.CONST
,Variable.DomainValue.TOP
,Integer.MAX_VALUE,pci));
} else if ( i instanceof LocalVariableInstruction ) {
if ( i instanceof LoadInstruction ) {
LoadInstruction li = (LoadInstruction)i;
s.stackPush(s.load(li.getIndex()));
} else if ( i instanceof StoreInstruction ) {
StoreInstruction si = (StoreInstruction)i;
Variable v = s.stackPop();
v.setIndex(si.getIndex());
v.setStartPC(pc.getNext().getPosition());
v.setKind(Variable.Kind.LOCAL);
s.store(si.getIndex(),v);
} else if ( i instanceof IINC ) {
IINC iinc = (IINC)i;
Variable v = s.load(iinc.getIndex());
v.iinc(iinc.getIncrement());
} else {
throw new RuntimeException(
"Unkown Local Variable instruction "+pci);
}
} else if ( i instanceof StackInstruction ) {
if ( i instanceof DUP ) {
Variable top = s.stackPeek();
assert top.getCategory() == 1;
s.stackPush(top);
} else if ( i instanceof DUP_X1 ) {
Variable top_0 = s.stackPop();
Variable top_1 = s.stackPop();
assert top_0.getCategory() == 1;
assert top_1.getCategory() == 1;
s.stackPush(top_0);
s.stackPush(top_1);
s.stackPush(top_0);
} else if ( i instanceof DUP_X2 ) {
Variable top_0 = s.stackPop();
Variable top_1 = s.stackPop();
Variable top_2 = null; // s.stackPop();
if ( top_0.getCategory() == 1 && top_1.getCategory() == 2 ) {
s.stackPush(top_0);
s.stackPush(top_1);
s.stackPush(top_0);
} else {
top_2 = s.stackPop();
assert top_0.getCategory() == 1;
assert top_1.getCategory() == 1;
assert top_2.getCategory() == 1;
s.stackPush(top_0);
s.stackPush(top_2);
s.stackPush(top_1);
s.stackPush(top_0);
}
} else if ( i instanceof DUP2 ) {
Variable top = s.stackPeek();
if ( top.getCategory() == 2) { // 1 category 2
s.stackPush(top);
} else { // 2 category 1
Variable top_0 = s.stackPop();
Variable top_1 = s.stackPop();
assert top_0.getCategory() == 1;
assert top_1.getCategory() == 1;
s.stackPush(top_1);
s.stackPush(top_0);
s.stackPush(top_1);
s.stackPush(top_0);
}
} else if ( i instanceof DUP2_X1 ) {
Variable top_0 = s.stackPop();
Variable top_1 = s.stackPop();
Variable top_2 = null;
if ( top_0.getCategory() == 2 && top_1.getCategory() == 1 ) {
s.stackPush(top_0);
s.stackPush(top_1);
s.stackPush(top_0);
} else {
top_2 = s.stackPop();
assert top_0.getCategory() == 1;
assert top_1.getCategory() == 1;
assert top_2.getCategory() == 1;
s.stackPush(top_1);
s.stackPush(top_0);
s.stackPush(top_2);
s.stackPush(top_1);
s.stackPush(top_0);
}
} else if ( i instanceof DUP2_X2 ) {
Variable top_0 = s.stackPop();
Variable top_1 = s.stackPop();
Variable top_2 = null;
Variable top_3 = null;
if ( top_0.getCategory() == 2 && top_1.getCategory() == 2 ) {
s.stackPush(top_0); // form 4
s.stackPush(top_1);
s.stackPush(top_0);
} else {
top_2 = s.stackPop();
if ( top_0.getCategory() == 1 && top_1.getCategory() == 1
&& top_2.getCategory() == 2 ) {
s.stackPush(top_1); // form 3
s.stackPush(top_0);
s.stackPush(top_2);
s.stackPush(top_1);
s.stackPush(top_0);
} else if ( top_0.getCategory() == 2
&& top_1.getCategory() == 1
&& top_2.getCategory() == 1 ) { // form 2
s.stackPush(top_0);
s.stackPush(top_2);
s.stackPush(top_1);
s.stackPush(top_0);
} else { // form 1
top_3 = s.stackPop();
assert top_0.getCategory() == 1;
assert top_1.getCategory() == 1;
assert top_2.getCategory() == 1;
assert top_3.getCategory() == 1;
s.stackPush(top_1);
s.stackPush(top_0);
s.stackPush(top_3);
s.stackPush(top_2);
s.stackPush(top_1);
s.stackPush(top_0);
}
}
} else if ( i instanceof POP ) {
Variable top = s.stackPop();
assert top.getCategory() == 1;
} else if ( i instanceof POP2 ) {
Variable top = s.stackPop();
if ( top.getCategory() == 1 ) {
Variable top_1 = s.stackPop();
assert top_1.getCategory() == 1;
}
} else if ( i instanceof SWAP ) {
Variable a = s.stackPop();
Variable b = s.stackPop();
assert a.getCategory() == 1;
assert b.getCategory() == 1;
s.stackPush(a);
s.stackPush(b);
}
} else if ( i instanceof ArrayInstruction ) {
Variable value = null;
if ( i instanceof StackConsumer ) { // store command
// eliminate value from stack.
value = s.stackPop();
}
Variable index = s.stackPop(); // index variable
Variable arrayref = s.stackPop(); // array reference
// check if the index is safe
if ( ! index.isSafe(arrayref) ) {
// load is not safe, add report.
makeNewReport(m,j,pc.getPosition());
//System.out.println("Access error found here");
// mark index as safe for arrayref.
index.addSafe(arrayref);
}
if ( i instanceof StackProducer ) {
s.stackPush(s.load(arrayref,index));
} else if ( i instanceof StackConsumer ) {
s.store(arrayref,index,value);
} else {
throw new RuntimeException(
"Unknown Array instruction "+pci);
}
} else if ( i instanceof ArithmeticInstruction ) {
Variable v1,v2,v;
v1 = v2 = s.stackPop(); // first value on the stack
if( i instanceof DADD || i instanceof FADD
|| i instanceof IADD || i instanceof LADD ) {
v1 = s.stackPop();
v = v1.add(v2);
} else if ( i instanceof DDIV || i instanceof FDIV
|| i instanceof IDIV || i instanceof LDIV ) {
v1 = s.stackPop();
v = v1.div(v2);
} else if ( i instanceof DMUL || i instanceof FMUL
|| i instanceof IMUL || i instanceof LMUL ) {
v1 = s.stackPop();
v = v1.mul(v2);
} else if ( i instanceof DNEG || i instanceof FNEG
|| i instanceof INEG || i instanceof LNEG ) {
v = v2.neg();
} else if ( i instanceof DREM || i instanceof FREM
|| i instanceof IREM || i instanceof LREM ) {
v1 = s.stackPop();
v = v1.rem(v2);
} else if ( i instanceof DSUB || i instanceof FSUB
|| i instanceof ISUB || i instanceof LSUB ) {
v1 = s.stackPop();
v = v1.sub(v2);
} else if ( i instanceof IAND || i instanceof LAND ) {
v1 = s.stackPop();
v = v1.and(v2);
} else if ( i instanceof ISHL || i instanceof LSHL ) {
v1 = s.stackPop();
v = v1.shl(v2);
} else if ( i instanceof ISHR || i instanceof LSHR ) {
v1 = s.stackPop();
v = v1.shr(v2);
} else if ( i instanceof IUSHR || i instanceof LUSHR ) {
v1 = s.stackPop();
v = v1.ushr(v2);
} else if ( i instanceof IOR || i instanceof LOR ) {
v1 = s.stackPop();
v = v1.or(v2);
} else if (i instanceof IXOR || i instanceof LXOR) {
v1 = s.stackPop();
v = v1.xor(v2);
} else {
throw new RuntimeException(
"Unknown arithmetic bytecode "+pci);
}
v.setStartPC(pci);
s.stackPush(v);
} else if ( i instanceof BranchInstruction ) {
if ( i instanceof IfInstruction ) {
Variable v1,v2;
State false_branch = s.clone();
State true_branch = s;
IfInstruction ifi = (IfInstruction)i;
InstructionHandle true_ih = ifi.getTarget();
if( i instanceof IF_ICMPEQ ) {
{ // true v1 == v2
v2 = true_branch.stackPop();
v1 = true_branch.stackPop();
v2.cmpeq(v1);
}
{ // false v1 ≠ v2
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
}
} else if ( i instanceof IF_ICMPNE ) {
{ // true v1 ≠ v2
true_branch.stackPop();
true_branch.stackPop();
}
{ // false v1 = v2
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
v2.cmpeq(v1);
}
} else if ( i instanceof IF_ICMPGE ) {
{ // true v1 >= v2
v2 = true_branch.stackPop();
v1 = true_branch.stackPop();
v1.cmpge(v2); // v1 >= v2
v2.cmple(v1); // v2 <= v1
}
{ // false v1 < v2
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
v1.cmplt(v2); // v1 < v2
v2.cmpge(v1); // v2 >= v1
}
} else if ( i instanceof IF_ICMPGT ) {
{ // true v1 > v2
v2 = true_branch.stackPop();
v1 = true_branch.stackPop();
v1.cmpgt(v2); // v1 > v2
v2.cmple(v1); // v2 <= v1
}
{ // false v1 <= v2
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
v1.cmple(v2); // v1 <= v2
v2.cmpge(v1); // v2 >= v1
}
} else if ( i instanceof IF_ICMPLE ) {
{ // true v1 <= v2
v2 = true_branch.stackPop();
v1 = true_branch.stackPop();
v1.cmple(v2); // v1 <= v2
v2.cmpge(v1); // v2 >= v1
}
{ // false v1 > v2
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
v1.cmpgt(v2); // v1 > v2
v2.cmple(v1); // v2 <= v1
}
} else if ( i instanceof IF_ICMPLT ) {
{ // true v1 < v2
v2 = true_branch.stackPop();
v1 = true_branch.stackPop();
v1.cmplt(v2); // v1 < v2
v2.cmpge(v1); // v2 >= v1
}
{ // false v1 >= v2
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
v1.cmpge(v2); // v1 >= v2
v2.cmple(v1); // v2 <= v1
}
} else if ( i instanceof IFLT ) {
{ // true < 0
v1 = true_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.TOP);
}
{ // false >= 0
v1 = false_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.GEQ0);
}
} else if ( i instanceof IFEQ ) {
{ // true =0
v1 = true_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.GEQ0);
}
{ // false ≠0
v1 = false_branch.stackPop();
}
} else if ( i instanceof IFGE ) {
{ // true >=0
v1 = true_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.GEQ0);
}
{ // false <0
v1 = false_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.TOP);
}
} else if ( i instanceof IFGT ) {
{ // true >0
v1 = true_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.G0);
}
{ // false <=0
v1 = false_branch.stackPop();
if( v1.getDomainValue() != Variable.DomainValue.GEQ0 ) {
v1.setDomainValue(Variable.DomainValue.TOP);
}
}
} else if ( i instanceof IFLE ) {
{ // true <=0
v1 = true_branch.stackPop();
if( v1.getDomainValue() != Variable.DomainValue.GEQ0 ) {
v1.setDomainValue(Variable.DomainValue.TOP);
}
}
{ // false >0
v1 = false_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.G0);
}
} else if ( i instanceof IFLT ) {
{ // true <0
v1 = true_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.TOP);
}
{ // false >=0
v1 = false_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.GEQ0);
}
} else if ( i instanceof IFNE ) {
{ // true ≠0
v1 = true_branch.stackPop();
}
{ // false =0
v1 = false_branch.stackPop();
v1.setDomainValue(Variable.DomainValue.GEQ0);
}
} else if ( i instanceof IFNULL
|| i instanceof IFNONNULL ) {
{ // true
v1 = true_branch.stackPop();
}
{ // false
v1 = false_branch.stackPop();
}
} else if ( i instanceof IF_ACMPEQ
|| i instanceof IF_ACMPNE ) {
{ // true
v2 = true_branch.stackPop();
v1 = true_branch.stackPop();
}
{ // false
v2 = false_branch.stackPop();
v1 = false_branch.stackPop();
}
} else {
throw new RuntimeException("Unknown if bytecode");
}
// System.out.println("false_branch "+false_branch);
// System.out.println("true_branch "+true_branch);
//System.out.println("analyzing false");
analyzeInstructions(m,il,pc.getNext().getPosition()
,min(true_ih.getPrev().getPosition(),end_pc)
,false_branch,j);
// System.out.println(false_branch);
// there is a then only if the else finished with a goto.
int jump = false_branch.getJump();
if( jump >= pci ) { // there has been a goto!
// System.out.println(false_branch);
//System.out.println("analyzing true");
pc = il.findHandle(false_branch.getPC()).getNext();
analyzeInstructions(m,il,pc.getPosition()
,min(end_pc,jump-1),true_branch,j);
// true_branch loop
jump = true_branch.getJump();
if ( jump < pci && jump >= 0 ) {
State loop;
do { // loop
loop = true_branch.clone();
analyzeInstructions(m,il,jump
,min(true_branch.getPC()-1,end_pc),loop,j);
} while(true_branch.intersect(loop));
}
}
// false branch loop
if ( jump < pci && jump >= 0 ) {
State loop;
do { // loop
// System.out.println("loop!");
loop = false_branch.clone();
analyzeInstructions(m,il,false_branch.getJump(),
min(false_branch.getPC()-1,end_pc),loop,j);
} while(false_branch.intersect(loop));
}
System.out.println("pre-intersect "+pc.getPosition()
+" max is "+end_pc);
true_branch.intersect(false_branch);
pc = il.findHandle(true_branch.getPC()).getNext();
if( pc != null ) {
// System.out.println("if end jump to "+pc.getPosition()
// +" max is "+end_pc);
}
//System.out.println("end if");
continue;
} else if (i instanceof GotoInstruction ) {
GotoInstruction ig = (GotoInstruction)i;
s.setJump(ig.getTarget().getPosition());
} else {
throw new RuntimeException(
"BranchInstruction not implemented yet: "+pci);
}
} else if ( i instanceof ConversionInstruction ) {
Variable v = s.stackPop().clone();
if ( i instanceof D2F ) {
assert v.getType().equals("D");
v.setType("F");
} else if ( i instanceof D2L ) {
assert v.getType().equals("D");
v.setType("J");
} else if ( i instanceof D2I ) {
assert v.getType().equals("D");
v.setType("I");
} else if ( i instanceof F2D ) {
assert v.getType().equals("F");
v.setType("D");
} else if ( i instanceof F2L ) {
assert v.getType().equals("F");
v.setType("J");
} else if ( i instanceof F2I ) {
assert v.getType().equals("F");
v.setType("I");
} else if ( i instanceof I2B
|| i instanceof I2C ) {
// can be ignored, does not change anything
// for this analysis. it can at most truncare the
// higher bits, reducing the value without changing
// the value in the abstract domain
} else if ( i instanceof I2D ) {
assert v.getType().equals("I");
v.setType("D");
} else if ( i instanceof I2L ) {
assert v.getType().equals("I");
v.setType("J");
} else if ( i instanceof I2F ) {
assert v.getType().equals("I");
v.setType("F");
} else if ( i instanceof L2D ) {
assert v.getType().equals("J");
v.setType("D");
} else if ( i instanceof L2I ) {
assert v.getType().equals("J");
v.setType("I");
} else if ( i instanceof L2F ) {
assert v.getType().equals("J");
v.setType("F");
} else {
throw new RuntimeException(
"Unkonwn Conversion Instruction :"+pci);
}
s.stackPush(v);
} else if ( i instanceof CPInstruction ) {
if ( i instanceof INSTANCEOF || i instanceof CHECKCAST ) {
s.stackPop();
ConstantPoolGen gen = new ConstantPoolGen(m.getConstantPool());
if ( i instanceof CHECKCAST ) {
CHECKCAST c = (CHECKCAST)i;
Type t = c.getType(gen);
s.stackPush(new Variable(t.getSignature()
,Variable.Kind.LOCAL
,Variable.DomainValue.TOP
,Integer.MAX_VALUE,pci));
} else if ( i instanceof INSTANCEOF ) {
s.stackPush(new Variable("I"
,Variable.Kind.LOCAL
,Variable.DomainValue.GEQ0
,Integer.MAX_VALUE,pci));
}
} else if ( i instanceof InvokeInstruction ) {
InvokeInstruction ii = (InvokeInstruction)i;
ConstantPoolGen gen = new ConstantPoolGen(
m.getConstantPool());
ReferenceType t = ii.getReferenceType(gen);
ObjectType tt = (ObjectType)t; //@TODO may be arraytype or others
if ( ! analyzer.isAnalyzable(tt.getClassName()) ) {
// skip instruction, un-analizable method. just clean
// the stack from the arguments.
for( Type ty : ii.getArgumentTypes(gen) )
s.stackPop();
//@TODO check if there is a return value to push
// or signal error (missing library)?
} else { // the method is in the classes to analyze
String class_name = tt.getClassName();
String method_name = ii.getMethodName(gen);
JavaClass cl = null;
Type ty[] = ii.getArgumentTypes(gen);
Method method = null;
System.out.println("Analyze call to "+method_name);
//@TODO remove arguments off the stack, should
//be passed in the method for better analysys?
for( Type argument : ii.getArgumentTypes(gen) )
s.stackPop();
// search method going up in the class tree
try {
boolean found = false;
cl = Repository.lookupClass(class_name);
do {
//System.out.println("method is in " + cl.getClassName()+"?");
Method[] ms = cl.getMethods();
for( Method clmethod : ms ) {
//System.out.println(method.getName()+"=="+method_name);
if ( clmethod.getName().equals(method_name) &&
equalTypes(clmethod.getArgumentTypes(),ty) ) {
if ( clmethod.isAbstract() ) {
found = false; // found, but keep going
break;
} else { // may be native
//System.out.println("Found");
method = clmethod;
found = true; // found, stop it
break;
}
}
}
if ( !found )
cl = cl.getSuperClass(); // becomes null if it is Object
} while( ! found && cl != null );
if( ! found && cl == null ) {
// method not found and climbed up to
// java.lang.Object!
throw new RuntimeException("Can't find method "+method_name);
}
} catch ( ClassNotFoundException e ) {
throw new RuntimeException(e);
}
MethodSignature ms = new MethodSignature(method,cl);
if( method_result.get(ms) == null ) {
boolean recursive = analyzeMethod(method,cl);
//System.out.println("Is recursive? "+recursive);
if( recursive ) {
assert method_result.get(ms).getDomainValue() == Variable.DomainValue.TOP;
analyzeMethod(method,cl);
Variable ret = method_result.get(ms);
while( ret.intersect(method_result.get(ms),s) ) {
analyzeMethod(method,cl);
}
method_result.remove(ms);
method_result.put(ms,ret);
}
}
// pop the object reference if needed
if ( ! (i instanceof INVOKESTATIC) )
s.stackPop();
if (method.getReturnType() != Type.VOID ) {
// System.out.println("put on stack result of "+ms);
// put result on top of the stack+ms
Variable ret = method_result.get(ms);
ret = ret.clone();
ret.cleanBounds();// must be null, there are references to variables
// that are not necessarly correct!
// putting in relation arguments with the result
// requires a lot more code.
s.stackPush(ret);
}
}
} else if ( i instanceof FieldInstruction ) {
if ( i instanceof GETSTATIC || i instanceof GETFIELD ) {
FieldInstruction a = (FieldInstruction)i;
ConstantPoolGen gen = new ConstantPoolGen(m.getConstantPool());
ReferenceType t = a.getReferenceType(gen);
if ( i instanceof GETFIELD ) {
s.stackPop(); // objectref
}
Variable v;
if ( t instanceof ObjectType ) {
v = new Variable(((ObjectType)t).getClassName()
,Variable.Kind.FIELD,Variable.DomainValue.TOP
,Integer.MAX_VALUE,pci);
s.stackPush(v); // object reference
} else if ( t instanceof ArrayType ) {
v = new Variable("["+((ArrayType)t).getElementType()
,Variable.Kind.FIELD,Variable.DomainValue.TOP
,Integer.MAX_VALUE,pci);
s.stackPush(v); // array reference
} else {
throw new RuntimeException(
"UninitializedObjectType not implemented: "+pci);
}
} else if( i instanceof PUTSTATIC ) {
s.stackPop(); // value
} else if ( i instanceof PUTFIELD ){
s.stackPop(); // value
s.stackPop(); // object ref
} else {
throw new RuntimeException(
"FieldInstruction not completed yet: "+pci);
}
} else if ( i instanceof LDC ) { // LDC_W included
LDC ii = (LDC)i;
Variable v = s.loadConstant(ii.getIndex());
s.stackPush(v);
} else if ( i instanceof NEW ) {
ConstantPoolGen gen = new ConstantPoolGen(m.getConstantPool());
ObjectType type = ((NEW)i).getLoadClassType(gen);
s.stackPush(new Variable(type.getSignature()
,Variable.Kind.LOCAL
,Variable.DomainValue.TOP
,Integer.MAX_VALUE,pci));
// throw new RuntimeException("NEW not completed");
} else {
throw new RuntimeException(
"CPInstruction not implemented yet: "+pci);
}
} else if ( i instanceof ARRAYLENGTH ) {
Variable arrayref = s.stackPop();
Variable edge = new Variable("I",Variable.Kind.LOCAL
,Variable.DomainValue.GEQ0
,Integer.MAX_VALUE,pci);
edge.addEdge(arrayref);
s.stackPush(edge);
} else if ( i instanceof MONITORENTER
|| i instanceof MONITOREXIT ) {
s.stackPop();
} else if ( i instanceof NOP || i instanceof BREAKPOINT
|| i instanceof CompoundInstruction
|| i instanceof IMPDEP1
|| i instanceof IMPDEP2 ) {
/* ignore. virtual instructions generated only from bcel
* classes, not real bytecode (compound) or opcodes to ignore
* (breakpoint,nop,..) */
} else if ( i instanceof ATHROW ) {
//@TODO if the exception catch is in the method body
//then continue, or else terminate.
throw new RuntimeException("athrow not yet implemented");
} else if ( i instanceof ReturnInstruction ) {
// set the return value and return the current state.
// do not removes any intermediate result.
if ( s.stackSize() > 0 ) {
s.setReturn(s.stackPop());
} else {
s.setReturn(new Variable("V"
,Variable.Kind.LOCAL
,Variable.DomainValue.TOP
,Integer.MAX_VALUE,pci));
}
return s;
} else {
throw new RuntimeException("Uknown type of bytecode: "+i);
}
// load next bytecode
pc = pc.getNext();
}
}
public Vector<BadArrayAccess> getReports() {
return reports;
}
private final void makeNewReport( Method m, JavaClass jclass, int idx ) {
int line = m.getLineNumberTable().getSourceLine(idx);
BadArrayAccess ba = new BadArrayAccess(jclass.getFileName()
,m.getName()
,line);
if ( ! reports.contains(ba) ) {
reports.add(ba);
}
}
private static final int min( int a, int b ) {
return a < b ? a : b;
}
private static final boolean equalTypes( Type a[], Type b[] ) {
if ( a.length != b.length )
return false;
for( int i = 0; i < a.length; i++ ) {
if( ! a[i].equals(b[i]) )
return false;
}
return true;
}
}