forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHierarchy.java
More file actions
727 lines (619 loc) · 24.3 KB
/
Hierarchy.java
File metadata and controls
727 lines (619 loc) · 24.3 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
package soot;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 1999 Raja Vallee-Rai
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import soot.jimple.SpecialInvokeExpr;
import soot.util.ArraySet;
import soot.util.Chain;
/**
* Represents the class hierarchy. It is closely linked to a Scene, and must be recreated if the Scene changes.
*
* The general convention is that if a method name contains "Including", then it returns the non-strict result; otherwise, it
* does a strict query (e.g. strict superclass).
*/
public class Hierarchy {
// These two maps are not filled in the constructor.
protected Map<SootClass, List<SootClass>> classToSubclasses;
protected Map<SootClass, List<SootClass>> interfaceToSubinterfaces;
protected Map<SootClass, List<SootClass>> interfaceToSuperinterfaces;
protected Map<SootClass, List<SootClass>> classToDirSubclasses;
protected Map<SootClass, List<SootClass>> interfaceToDirSubinterfaces;
protected Map<SootClass, List<SootClass>> interfaceToDirSuperinterfaces;
// This holds the direct implementers.
protected Map<SootClass, List<SootClass>> interfaceToDirImplementers;
final Scene sc;
final int state;
/** Constructs a hierarchy from the current scene. */
public Hierarchy() {
this.sc = Scene.v();
this.state = sc.getState();
// Well, this used to be describable by 'Duh'.
// Construct the subclasses hierarchy and the subinterfaces hierarchy.
{
Chain<SootClass> allClasses = sc.getClasses();
final int mapSize = allClasses.size() * 2 + 1;
this.classToSubclasses = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
this.interfaceToSubinterfaces = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
this.interfaceToSuperinterfaces = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
this.classToDirSubclasses = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
this.interfaceToDirSubinterfaces = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
this.interfaceToDirSuperinterfaces = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
this.interfaceToDirImplementers = new HashMap<SootClass, List<SootClass>>(mapSize, 0.7f);
initializeHierarchy(allClasses);
}
}
/**
* Initializes the hierarchy given a chain of all classes that shall be included in the hierarchy
*
* @param allClasses
* The chain of all classes to be included in the hierarchy
*/
protected void initializeHierarchy(Chain<SootClass> allClasses) {
for (SootClass c : allClasses) {
if (c.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
if (c.isInterface()) {
interfaceToDirSubinterfaces.put(c, new ArrayList<SootClass>());
interfaceToDirSuperinterfaces.put(c, new ArrayList<SootClass>());
interfaceToDirImplementers.put(c, new ArrayList<SootClass>());
} else {
classToDirSubclasses.put(c, new ArrayList<SootClass>());
}
}
for (SootClass c : allClasses) {
if (c.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
if (c.hasSuperclass()) {
if (c.isInterface()) {
List<SootClass> l2 = interfaceToDirSuperinterfaces.get(c);
for (SootClass i : c.getInterfaces()) {
if (c.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
List<SootClass> l = interfaceToDirSubinterfaces.get(i);
if (l != null) {
l.add(c);
}
if (l2 != null) {
l2.add(i);
}
}
} else {
List<SootClass> l = classToDirSubclasses.get(c.getSuperclass());
if (l != null) {
l.add(c);
}
for (SootClass i : c.getInterfaces()) {
if (c.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
List<SootClass> l2 = interfaceToDirImplementers.get(i);
if (l2 != null) {
l2.add(c);
}
}
}
}
}
// Fill the directImplementers lists with subclasses.
for (SootClass c : allClasses) {
if (c.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
if (c.isInterface()) {
Set<SootClass> s = new ArraySet<SootClass>();
for (SootClass c0 : interfaceToDirImplementers.get(c)) {
if (c.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
s.addAll(getSubclassesOfIncluding(c0));
}
interfaceToDirImplementers.put(c, new ArrayList<SootClass>(s));
} else if (c.hasSuperclass()) {
List<SootClass> l = classToDirSubclasses.get(c);
if (l != null) {
classToDirSubclasses.put(c, new ArrayList<>(l));
}
}
}
}
protected void checkState() {
if (state != sc.getState()) {
throw new ConcurrentModificationException("Scene changed for Hierarchy!");
}
}
// This includes c in the list of subclasses.
/** Returns a list of subclasses of c, including itself. */
public List<SootClass> getSubclassesOfIncluding(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (c.isInterface()) {
throw new RuntimeException("class needed!");
}
List<SootClass> subclasses = getSubclassesOf(c);
List<SootClass> result = new ArrayList<SootClass>(subclasses.size() + 1);
result.addAll(subclasses);
result.add(c);
return Collections.unmodifiableList(result);
}
/** Returns a list of subclasses of c, excluding itself. */
public List<SootClass> getSubclassesOf(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (c.isInterface()) {
throw new RuntimeException("class needed!");
}
checkState();
// If already cached, return the value.
List<SootClass> retVal = classToSubclasses.get(c);
if (retVal != null) {
return retVal;
}
// Otherwise, build up the hashmap.
{
ArrayList<SootClass> l = new ArrayList<SootClass>();
for (SootClass cls : classToDirSubclasses.get(c)) {
if (cls.resolvingLevel() < SootClass.HIERARCHY) {
continue;
}
l.addAll(getSubclassesOfIncluding(cls));
}
l.trimToSize();
retVal = Collections.unmodifiableList(l);
}
classToSubclasses.put(c, retVal);
return retVal;
}
/**
* Returns a list of superclasses of {@code sootClass}, including itself.
*
* @param sootClass
* the <strong>class</strong> of which superclasses will be taken. Must not be {@code null} or interface
* @return list of superclasses, including itself
* @throws IllegalArgumentException
* when passed class is an interface
* @throws NullPointerException
* when passed argument is {@code null}
*/
public List<SootClass> getSuperclassesOfIncluding(SootClass sootClass) {
List<SootClass> superclasses = getSuperclassesOf(sootClass);
List<SootClass> result = new ArrayList<>(superclasses.size() + 1);
result.add(sootClass);
result.addAll(superclasses);
return Collections.unmodifiableList(result);
}
/**
* Returns a list of <strong>direct</strong> superclasses of passed class in reverse order, starting with its parent.
*
* @param sootClass
* the <strong>class</strong> of which superclasses will be taken. Must not be {@code null} or interface
* @return list of superclasses
* @throws IllegalArgumentException
* when passed class is an interface
* @throws NullPointerException
* when passed argument is {@code null}
*/
public List<SootClass> getSuperclassesOf(SootClass sootClass) {
sootClass.checkLevel(SootClass.HIERARCHY);
if (sootClass.isInterface()) {
throw new IllegalArgumentException(sootClass.getName() + " is an interface, but class is expected");
}
checkState();
final List<SootClass> superclasses = new ArrayList<>();
for (SootClass current = sootClass; current.hasSuperclass();) {
SootClass superclass = current.getSuperclass();
superclasses.add(superclass);
current = superclass;
}
return Collections.unmodifiableList(superclasses);
}
/**
* Returns a list of subinterfaces of sootClass, including itself.
*
* @param sootClass
* the <strong>interface</strong> of which subinterfaces will be taken. Must not be {@code null} or class
* @return list of subinterfaces, including passed one
* @throws IllegalArgumentException
* when passed class is a class
* @throws NullPointerException
* when passed argument is {@code null}
*/
public List<SootClass> getSubinterfacesOfIncluding(SootClass sootClass) {
List<SootClass> subinterfaces = getSubinterfacesOf(sootClass);
List<SootClass> result = new ArrayList<>(subinterfaces.size() + 1);
result.addAll(subinterfaces);
result.add(sootClass);
return Collections.unmodifiableList(result);
}
/**
* Returns a list of subinterfaces of sootClass, excluding itself.
*
* @param sootClass
* the <strong>interface</strong> of which subinterfaces will be taken. Must not be {@code null} or class
* @return list of subinterfaces, including passed one
* @throws IllegalArgumentException
* when passed sootClass is a class
* @throws NullPointerException
* when passed argument is {@code null}
*/
public List<SootClass> getSubinterfacesOf(SootClass sootClass) {
sootClass.checkLevel(SootClass.HIERARCHY);
if (!sootClass.isInterface()) {
throw new IllegalArgumentException(sootClass.getName() + " is a class, but interface is expected");
}
checkState();
// If already cached, return the value.
List<SootClass> retVal = interfaceToSubinterfaces.get(sootClass);
if (retVal != null) {
return retVal;
}
// Otherwise, build up the hashmap.
{
List<SootClass> directSubInterfaces = interfaceToDirSubinterfaces.get(sootClass);
if (directSubInterfaces == null || directSubInterfaces.isEmpty()) {
return Collections.emptyList();
}
final ArrayList<SootClass> l = new ArrayList<>();
for (SootClass si : directSubInterfaces) {
l.addAll(getSubinterfacesOfIncluding(si));
}
l.trimToSize();
retVal = Collections.unmodifiableList(l);
}
interfaceToSubinterfaces.put(sootClass, retVal);
return retVal;
}
/** Returns a list of superinterfaces of c, including itself. */
public List<SootClass> getSuperinterfacesOfIncluding(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (!c.isInterface()) {
throw new RuntimeException("interface needed!");
}
List<SootClass> superinterfaces = getSuperinterfacesOf(c);
List<SootClass> result = new ArrayList<SootClass>(superinterfaces.size() + 1);
result.addAll(superinterfaces);
result.add(c);
return Collections.unmodifiableList(result);
}
/** Returns a list of superinterfaces of c, excluding itself. */
public List<SootClass> getSuperinterfacesOf(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (!c.isInterface()) {
throw new RuntimeException("interface needed!");
}
checkState();
// If already cached, return the value.
List<SootClass> retVal = interfaceToSuperinterfaces.get(c);
if (retVal != null) {
return retVal;
}
// Otherwise, build up the hashmap.
{
ArrayList<SootClass> l = new ArrayList<SootClass>();
for (SootClass si : interfaceToDirSuperinterfaces.get(c)) {
l.addAll(getSuperinterfacesOfIncluding(si));
}
l.trimToSize();
retVal = Collections.unmodifiableList(l);
}
interfaceToSuperinterfaces.put(c, retVal);
return retVal;
}
/** Returns a list of direct superclasses of c, excluding c. */
public List<SootClass> getDirectSuperclassesOf(SootClass c) {
throw new RuntimeException("Not implemented yet!");
}
/** Returns a list of direct subclasses of c, excluding c. */
public List<SootClass> getDirectSubclassesOf(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (c.isInterface()) {
throw new RuntimeException("class needed!");
}
checkState();
return Collections.unmodifiableList(classToDirSubclasses.get(c));
}
// This includes c in the list of subclasses.
/** Returns a list of direct subclasses of c, including c. */
public List<SootClass> getDirectSubclassesOfIncluding(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (c.isInterface()) {
throw new RuntimeException("class needed!");
}
checkState();
List<SootClass> subclasses = classToDirSubclasses.get(c);
List<SootClass> l = new ArrayList<SootClass>(subclasses.size() + 1);
l.addAll(subclasses);
l.add(c);
return Collections.unmodifiableList(l);
}
/** Returns a list of direct superinterfaces of c. */
public List<SootClass> getDirectSuperinterfacesOf(SootClass c) {
throw new RuntimeException("Not implemented yet!");
}
/** Returns a list of direct subinterfaces of c. */
public List<SootClass> getDirectSubinterfacesOf(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (!c.isInterface()) {
throw new RuntimeException("interface needed!");
}
checkState();
return Collections.unmodifiableList(interfaceToDirSubinterfaces.get(c));
}
/** Returns a list of direct subinterfaces of c, including itself. */
public List<SootClass> getDirectSubinterfacesOfIncluding(SootClass c) {
c.checkLevel(SootClass.HIERARCHY);
if (!c.isInterface()) {
throw new RuntimeException("interface needed!");
}
checkState();
List<SootClass> subinterfaces = interfaceToDirSubinterfaces.get(c);
List<SootClass> l = new ArrayList<SootClass>(subinterfaces.size() + 1);
l.addAll(subinterfaces);
l.add(c);
return Collections.unmodifiableList(l);
}
/** Returns a list of direct implementers of c, excluding itself. */
public List<SootClass> getDirectImplementersOf(SootClass i) {
i.checkLevel(SootClass.HIERARCHY);
if (!i.isInterface()) {
throw new RuntimeException("interface needed; got " + i);
}
checkState();
return Collections.unmodifiableList(interfaceToDirImplementers.get(i));
}
/** Returns a list of implementers of c, excluding itself. */
public List<SootClass> getImplementersOf(SootClass i) {
i.checkLevel(SootClass.HIERARCHY);
if (!i.isInterface()) {
throw new RuntimeException("interface needed; got " + i);
}
checkState();
ArraySet<SootClass> set = new ArraySet<SootClass>();
for (SootClass c : getSubinterfacesOfIncluding(i)) {
set.addAll(getDirectImplementersOf(c));
}
return Collections.unmodifiableList(new ArrayList<SootClass>(set));
}
/**
* Returns true if child is a subclass of possibleParent. If one of the known parent classes is phantom, we conservatively
* assume that the current class might be a child.
*/
public boolean isClassSubclassOf(SootClass child, SootClass possibleParent) {
child.checkLevel(SootClass.HIERARCHY);
possibleParent.checkLevel(SootClass.HIERARCHY);
List<SootClass> parentClasses = getSuperclassesOf(child);
if (parentClasses.contains(possibleParent)) {
return true;
}
for (SootClass sc : parentClasses) {
if (sc.isPhantom()) {
return true;
}
}
return false;
}
/**
* Returns true if child is, or is a subclass of, possibleParent. If one of the known parent classes is phantom, we
* conservatively assume that the current class might be a child.
*/
public boolean isClassSubclassOfIncluding(SootClass child, SootClass possibleParent) {
child.checkLevel(SootClass.HIERARCHY);
possibleParent.checkLevel(SootClass.HIERARCHY);
List<SootClass> parentClasses = getSuperclassesOfIncluding(child);
if (parentClasses.contains(possibleParent)) {
return true;
}
for (SootClass sc : parentClasses) {
if (sc.isPhantom()) {
return true;
}
}
return false;
}
/** Returns true if child is a direct subclass of possibleParent. */
public boolean isClassDirectSubclassOf(SootClass c, SootClass c2) {
throw new RuntimeException("Not implemented yet!");
}
/** Returns true if child is a superclass of possibleParent. */
public boolean isClassSuperclassOf(SootClass parent, SootClass possibleChild) {
parent.checkLevel(SootClass.HIERARCHY);
possibleChild.checkLevel(SootClass.HIERARCHY);
return getSubclassesOf(parent).contains(possibleChild);
}
/** Returns true if parent is, or is a superclass of, possibleChild. */
public boolean isClassSuperclassOfIncluding(SootClass parent, SootClass possibleChild) {
parent.checkLevel(SootClass.HIERARCHY);
possibleChild.checkLevel(SootClass.HIERARCHY);
return getSubclassesOfIncluding(parent).contains(possibleChild);
}
/** Returns true if child is a subinterface of possibleParent. */
public boolean isInterfaceSubinterfaceOf(SootClass child, SootClass possibleParent) {
child.checkLevel(SootClass.HIERARCHY);
possibleParent.checkLevel(SootClass.HIERARCHY);
return getSubinterfacesOf(possibleParent).contains(child);
}
/** Returns true if child is a direct subinterface of possibleParent. */
public boolean isInterfaceDirectSubinterfaceOf(SootClass child, SootClass possibleParent) {
child.checkLevel(SootClass.HIERARCHY);
possibleParent.checkLevel(SootClass.HIERARCHY);
return getDirectSubinterfacesOf(possibleParent).contains(child);
}
/** Returns true if parent is a superinterface of possibleChild. */
public boolean isInterfaceSuperinterfaceOf(SootClass parent, SootClass possibleChild) {
parent.checkLevel(SootClass.HIERARCHY);
possibleChild.checkLevel(SootClass.HIERARCHY);
return getSuperinterfacesOf(possibleChild).contains(parent);
}
/** Returns true if parent is a direct superinterface of possibleChild. */
public boolean isInterfaceDirectSuperinterfaceOf(SootClass parent, SootClass possibleChild) {
parent.checkLevel(SootClass.HIERARCHY);
possibleChild.checkLevel(SootClass.HIERARCHY);
return getDirectSuperinterfacesOf(possibleChild).contains(parent);
}
/**
* Returns the most specific type which is an ancestor of both c1 and c2.
*/
public SootClass getLeastCommonSuperclassOf(SootClass c1, SootClass c2) {
c1.checkLevel(SootClass.HIERARCHY);
c2.checkLevel(SootClass.HIERARCHY);
throw new RuntimeException("Not implemented yet!");
}
// Questions about method invocation.
/**
* Checks whether check is a visible class in view of the from class. It assumes that protected and private classes do not
* exit. If they exist and check is either protected or private, the check will return false.
*/
public boolean isVisible(SootClass from, SootClass check) {
if (check.isPublic()) {
return true;
}
if (check.isProtected() || check.isPrivate()) {
return false;
}
// package visibility
return from.getJavaPackageName().equals(check.getJavaPackageName());
}
/**
* Returns true if the classmember m is visible from code in the class from.
*/
public boolean isVisible(SootClass from, ClassMember m) {
from.checkLevel(SootClass.HIERARCHY);
final SootClass declaringClass = m.getDeclaringClass();
declaringClass.checkLevel(SootClass.HIERARCHY);
if (!isVisible(from, declaringClass)) {
return false;
}
if (m.isPublic()) {
return true;
}
if (m.isPrivate()) {
return from.equals(declaringClass);
}
if (m.isProtected()) {
return isClassSubclassOfIncluding(from, declaringClass)
|| from.getJavaPackageName().equals(declaringClass.getJavaPackageName());
}
// m is package
return from.getJavaPackageName().equals(declaringClass.getJavaPackageName());
}
/**
* Given an object of actual type C (o = new C()), returns the method which will be called on an o.f() invocation.
*/
public SootMethod resolveConcreteDispatch(SootClass concreteType, SootMethod m) {
concreteType.checkLevel(SootClass.HIERARCHY);
m.getDeclaringClass().checkLevel(SootClass.HIERARCHY);
checkState();
if (concreteType.isInterface()) {
throw new RuntimeException("class needed!");
}
final String methodSig = m.getSubSignature();
for (SootClass c : getSuperclassesOfIncluding(concreteType)) {
SootMethod sm = c.getMethodUnsafe(methodSig);
if (sm != null && isVisible(c, m)) {
return sm;
}
}
throw new RuntimeException("could not resolve concrete dispatch!\nType: " + concreteType + "\nMethod: " + m);
}
/**
* Given a set of definite receiver types, returns a list of possible targets.
*/
public List<SootMethod> resolveConcreteDispatch(List<Type> classes, SootMethod m) {
m.getDeclaringClass().checkLevel(SootClass.HIERARCHY);
checkState();
Set<SootMethod> s = new ArraySet<SootMethod>();
for (Type cls : classes) {
if (cls instanceof RefType) {
s.add(resolveConcreteDispatch(((RefType) cls).getSootClass(), m));
} else if (cls instanceof ArrayType) {
s.add(resolveConcreteDispatch(Scene.v().getObjectType().getSootClass(), m));
} else {
throw new RuntimeException("Unable to resolve concrete dispatch of type " + cls);
}
}
return Collections.unmodifiableList(new ArrayList<SootMethod>(s));
}
// what can get called for c & all its subclasses
/**
* Given an abstract dispatch to an object of type c and a method m, gives a list of possible receiver methods.
*/
public List<SootMethod> resolveAbstractDispatch(SootClass c, SootMethod m) {
c.checkLevel(SootClass.HIERARCHY);
m.getDeclaringClass().checkLevel(SootClass.HIERARCHY);
checkState();
Collection<SootClass> classes;
if (c.isInterface()) {
classes = new HashSet<SootClass>();
for (SootClass sootClass : getImplementersOf(c)) {
classes.addAll(getSubclassesOfIncluding(sootClass));
}
} else {
classes = getSubclassesOfIncluding(c);
}
Set<SootMethod> s = new ArraySet<SootMethod>();
for (SootClass cl : classes) {
if (!Modifier.isAbstract(cl.getModifiers())) {
s.add(resolveConcreteDispatch(cl, m));
}
}
return Collections.unmodifiableList(new ArrayList<SootMethod>(s));
}
// what can get called if you have a set of possible receiver types
/**
* Returns a list of possible targets for the given method and set of receiver types.
*/
public List<SootMethod> resolveAbstractDispatch(List<SootClass> classes, SootMethod m) {
m.getDeclaringClass().checkLevel(SootClass.HIERARCHY);
Set<SootMethod> s = new ArraySet<SootMethod>();
for (SootClass sootClass : classes) {
s.addAll(resolveAbstractDispatch(sootClass, m));
}
return Collections.unmodifiableList(new ArrayList<SootMethod>(s));
}
/** Returns the target for the given SpecialInvokeExpr. */
public SootMethod resolveSpecialDispatch(SpecialInvokeExpr ie, SootMethod container) {
final SootClass containerClass = container.getDeclaringClass();
containerClass.checkLevel(SootClass.HIERARCHY);
final SootMethod target = ie.getMethod();
final SootClass targetClass = target.getDeclaringClass();
targetClass.checkLevel(SootClass.HIERARCHY);
/*
* This is a bizarre condition! Hopefully the implementation is correct. See VM Spec, 2nd Edition, Chapter 6, in the
* definition of invokespecial.
*/
if ("<init>".equals(target.getName()) || target.isPrivate()) {
return target;
} else if (isClassSubclassOf(targetClass, containerClass)) {
return resolveConcreteDispatch(containerClass, target);
} else {
return target;
}
}
}