/
IFDSProblem.java
475 lines (435 loc) · 21 KB
/
IFDSProblem.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
package ca.uwaterloo.liang;
/* MockAnalysis
* Copyright (C) 2021 Qian Liang <q8liang@uwaterloo.ca>
*
* This library 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 library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import ca.uwaterloo.liang.util.Util;
import heros.DefaultSeeds;
import heros.FlowFunction;
import heros.FlowFunctions;
import heros.InterproceduralCFG;
import heros.flowfunc.Gen;
import heros.flowfunc.Identity;
import heros.flowfunc.KillAll;
import soot.Local;
import soot.NullType;
import soot.Scene;
import soot.SootClass;
import soot.SootField;
import soot.SootMethod;
import soot.Unit;
import soot.Value;
import soot.ValueBox;
import soot.jimple.ArrayRef;
import soot.jimple.AssignStmt;
import soot.jimple.CastExpr;
import soot.jimple.DefinitionStmt;
import soot.jimple.FieldRef;
import soot.jimple.InstanceFieldRef;
import soot.jimple.InterfaceInvokeExpr;
import soot.jimple.InvokeExpr;
import soot.jimple.InvokeStmt;
import soot.jimple.ReturnStmt;
import soot.jimple.ReturnVoidStmt;
import soot.jimple.SpecialInvokeExpr;
import soot.jimple.StaticFieldRef;
import soot.jimple.Stmt;
import soot.jimple.VirtualInvokeExpr;
import soot.jimple.internal.JimpleLocal;
import soot.jimple.toolkits.ide.DefaultJimpleIFDSTabulationProblem;
import soot.toolkits.scalar.Pair;
public class IFDSProblem extends DefaultJimpleIFDSTabulationProblem<Map<Value, MockStatus>, InterproceduralCFG<Unit,SootMethod>> {
InterproceduralCFG<Unit,SootMethod> icfg;
HashSet<SootField> annotatedMocks;
HashMap<SootClass, HashMap<SootField, MockStatus>> fieldMocks;
public IFDSProblem(InterproceduralCFG<Unit, SootMethod> icfg) {
super(icfg);
this.icfg = icfg;
this.annotatedMocks = AnnotatedAndInitMockTransformer.getAnnotatedMocks();
this.fieldMocks = MockAnalysisPreTransformer.getFieldMocks();
}
@Override
protected FlowFunctions<Unit, Map<Value, MockStatus>, SootMethod> createFlowFunctionsFactory() {
return new FlowFunctions<Unit, Map<Value, MockStatus>, SootMethod>(){
@Override
public FlowFunction<Map<Value, MockStatus>> getNormalFlowFunction(Unit curr, Unit succ){
//System.out.println("In getNormalFlowFunction; ");
return getNormalFlow(curr, succ);
}
@Override
public FlowFunction<Map<Value, MockStatus>> getCallFlowFunction(Unit callStmt, SootMethod destinationMethod) {
//System.out.println("In getCallFlowFunction; ");
return getCallFlow(callStmt, destinationMethod);
}
@Override
public FlowFunction<Map<Value, MockStatus>> getReturnFlowFunction(Unit callSite, SootMethod calleeMethod, Unit exitStmt, Unit returnSite){
//System.out.println("In getReturnFlowFunction; ");
return getReturnFlow(callSite, calleeMethod, exitStmt, returnSite);
}
@Override
public FlowFunction<Map<Value, MockStatus>> getCallToReturnFlowFunction(Unit callSite, Unit returnSite) {
//System.out.println("In getCallToReturnFlowFunction; ");
return getCallToReturnFlow(callSite, returnSite);
}
};
}
protected FlowFunction<Map<Value, MockStatus>> getCallToReturnFlow(Unit callSite, Unit returnSite) {
Stmt stmt = (Stmt) callSite;
InvokeExpr ie = stmt.getInvokeExpr();
final List<Value> callArgs = ie.getArgs();
final SootMethod method = ie.getMethod();
// if (method.getDeclaringClass().getPackageName().contains("ca.liang")) {
// System.out.println("In getCallToReturnFlow; ");
// System.out.println("Unit callSite: " + callSite);
// System.out.println("Unit returnSite: " + returnSite);
//
// System.out.println("SootMethod Invoked: " + method.getSignature());
// System.out.println();
// }
Value base = null;
Value leftOp = null;
if (ie instanceof VirtualInvokeExpr) {
VirtualInvokeExpr vie = (VirtualInvokeExpr) ie;
base = vie.getBase();
}
else if (ie instanceof InterfaceInvokeExpr) {
InterfaceInvokeExpr iie = (InterfaceInvokeExpr) ie;
base = iie.getBase();
}
else if (ie instanceof SpecialInvokeExpr) {
SpecialInvokeExpr iie = (SpecialInvokeExpr) ie;
base = iie.getBase();
}
if (callSite instanceof DefinitionStmt) {
DefinitionStmt defnStmt = (DefinitionStmt) callSite;
leftOp = defnStmt.getLeftOp();
}
final Value baseF = base;
final Value leftOpF = leftOp;
//System.out.println("baseF: " + baseF);
//System.out.println("leftOp: " + leftOpF);
// use assumption if no callees to analyze
if (icfg.getCalleesOfCallAt(callSite).isEmpty() ) {
//System.out.println("No callees to analyze");
return new FlowFunction<Map<Value, MockStatus>>() {
@Override
public Set<Map<Value, MockStatus>> computeTargets(Map<Value, MockStatus> source) {
Set<Map<Value, MockStatus>> ret = new HashSet<Map<Value, MockStatus>>();
ret.add(source);
if (baseF != null && leftOpF != null && source.containsKey(baseF)) {
Map<Value, MockStatus> hm_ret = new HashMap<Value, MockStatus>();
hm_ret.put(leftOpF, source.get(baseF));
ret.add(hm_ret);
}
if (leftOpF != null && source.containsKey(leftOpF) ) {
//&& callArgs.contains(leftOpF)
Map<Value, MockStatus> hm_ret = new HashMap<Value, MockStatus>();
hm_ret.put(leftOpF, source.get(leftOpF));
ret.add(hm_ret);
}
if (baseF != null && source.containsKey(baseF) ) {
//&& callArgs.contains(baseF)
Map<Value, MockStatus> hm_ret = new HashMap<Value, MockStatus>();
hm_ret.put(baseF, source.get(baseF));
ret.add(hm_ret);
}
return ret;
}
};
}
//System.out.println();
return Identity.v();
}
protected FlowFunction<Map<Value, MockStatus>> getReturnFlow(Unit callSite, SootMethod calleeMethod, Unit exitStmt,
Unit returnSite) {
Stmt stmt = (Stmt) callSite;
InvokeExpr ie = stmt.getInvokeExpr();
// if (IFDSDummyMainCreator.iccMethods.contains(calleeMethod)) {
// return KillAll.v();
// }
// if (calleeMethod.getDeclaringClass().getPackageName().contains("ca.liang")) {
// System.out.println("In getReturnFlow; ");
// System.out.println("Unit callSite: " + callSite);
// System.out.println("SootMethod calleeMethod: " + calleeMethod);
// System.out.println("Unit exitStmt: " + exitStmt);
// System.out.println("Unit returnSite: " + returnSite);
// System.out.println();
// }
Value base = null;
if (ie instanceof VirtualInvokeExpr) {
VirtualInvokeExpr vie = (VirtualInvokeExpr) ie;
base = vie.getBase();
}
else if (ie instanceof InterfaceInvokeExpr) {
InterfaceInvokeExpr iie = (InterfaceInvokeExpr) ie;
base = iie.getBase();
}
else if (ie instanceof SpecialInvokeExpr) {
SpecialInvokeExpr iie = (SpecialInvokeExpr) ie;
base = iie.getBase();
}
final Value baseF = base;
if (exitStmt instanceof ReturnStmt) {
ReturnStmt returnStmt = (ReturnStmt) exitStmt;
final Value retOp = returnStmt.getOp();
return new FlowFunction<Map<Value, MockStatus>>() {
@Override
public Set<Map<Value, MockStatus>> computeTargets(Map<Value, MockStatus> source) {
Set<Map<Value, MockStatus>> ret = new HashSet<Map<Value, MockStatus>>();
// if (source instanceof StaticFieldRef) {
// ret.add(source);
// }
ret.add(source);
if (callSite instanceof DefinitionStmt && source.containsKey(retOp)) {
DefinitionStmt defnStmt = (DefinitionStmt) callSite;
Map<Value, MockStatus> hm_def = new HashMap<Value, MockStatus>();
//System.out.println("Definition Statement Op: " + defnStmt.getLeftOp());
//System.out.println("MockStatus: " + source.get(retOp));
hm_def.put(defnStmt.getLeftOp(), source.get(retOp));
ret.add(hm_def);
}
if (baseF != null && source.containsKey(calleeMethod.retrieveActiveBody().getThisLocal())) {
Map<Value, MockStatus> hm_ret = new HashMap<Value, MockStatus>();
//System.out.println("Base final value: " + baseF);
//System.out.println("MockStatus: " + source.get(calleeMethod.retrieveActiveBody().getThisLocal()));
hm_ret.put(baseF, source.get(calleeMethod.retrieveActiveBody().getThisLocal()));
ret.add(hm_ret);
}
return ret;
}
};
}
if (exitStmt instanceof ReturnVoidStmt) {
return new FlowFunction<Map<Value, MockStatus>>() {
@Override
public Set<Map<Value, MockStatus>> computeTargets(Map<Value, MockStatus> source) {
Set<Map<Value, MockStatus>> ret = new HashSet<Map<Value, MockStatus>>();
// if (source instanceof StaticFieldRef) {
// ret.add(source);
// }
if (baseF != null && source.containsKey(calleeMethod.retrieveActiveBody().getThisLocal())) {
Map<Value, MockStatus> hm_ret = new HashMap<Value, MockStatus>();
hm_ret.put(baseF, source.get(calleeMethod.retrieveActiveBody().getThisLocal()));
ret.add(hm_ret);
}
return ret;
}
};
}
return KillAll.v();
}
protected FlowFunction<Map<Value, MockStatus>> getNormalFlow(Unit curr, Unit succ) {
// System.out.println("In getNormalFlow; ");
// System.out.println("Unit curr: " + curr);
// System.out.println("Unit succ: " + succ);
// System.out.println();
if (curr instanceof AssignStmt) {
final AssignStmt assign = (AssignStmt) curr;
final Value leftOp = assign.getLeftOp();
final Value rightOp = assign.getRightOp();
return new FlowFunction<Map<Value, MockStatus>>(){
@Override
public Set<Map<Value, MockStatus>> computeTargets(Map<Value, MockStatus> source){
// if (source.containsKey(leftOp)) {
// return Collections.emptySet();
// }
Set<Map<Value, MockStatus>> res = new HashSet<>();
res.add(source);
if (source.containsKey(rightOp)) {
Map<Value, MockStatus> hm = new HashMap<>();
hm.put(leftOp, source.get(rightOp));
//System.out.println("Left_val: " + leftOp);
//System.out.println("MockStatus: " + source.get(rightOp));
res.add(hm);
}
if (rightOp instanceof ArrayRef) {
Value base = ((ArrayRef)rightOp).getBase();
if (source.containsKey(base)) {
Map<Value, MockStatus> hm_arr = new HashMap<>();
//System.out.println("Left_val: " + leftOp);
//System.out.println("MockStatus: " + source.get(base));
hm_arr.put(leftOp, source.get(base));
res.add(hm_arr);
}
}
if (rightOp instanceof CastExpr) {
Value right_val = ((CastExpr) rightOp).getOp();
// if (source.containsKey(right_val)) {
// Map<Value, MockStatus> hm_cast = new HashMap<>();
// //System.out.println("Left_val: " + leftOp);
// //System.out.println("MockStatus: " + source.get(right_val));
// hm_cast.put(leftOp, source.get(right_val));
// res.add(hm_cast);
// }
if (source.containsKey(right_val) && source.get(right_val).getMock()
&& (assign.getLeftOp() instanceof Local || assign.getLeftOp() instanceof FieldRef) ) {
Map<Value, MockStatus> hm_cast = new HashMap<>();
MockStatus status = new MockStatus(true);
Value left_val = assign.getLeftOp();
hm_cast.put(left_val, status);
res.add(hm_cast);
System.out.println("Mock Local: " + right_val);
System.out.println("Mock Casted Local: " + left_val);
}
}
return res;
}
};
}
/*Stmt aStmt = (Stmt) curr;
if (aStmt.containsFieldRef()) {
Map<Value, MockStatus> fieldMockMap = new HashMap<>();
SootField sf = aStmt.getFieldRef().getField();
// First way to create mock: Mock Annotation
if (AnnotatedAndInitMockTransformer.getAnnotatedMocks().contains(sf)) {
//System.out.println("myAnnotatedMocks contain the mock wanted");
List<ValueBox> defBoxes = curr.getDefBoxes();
for (ValueBox vb: defBoxes) {
Value v = vb.getValue();
MockStatus status = new MockStatus(true);
fieldMockMap.put(v, status);
}
}
return new Gen<Map<Value, MockStatus>>(fieldMockMap);
}*/
if (curr instanceof InvokeStmt) {
final InvokeStmt invoke = (InvokeStmt) curr;
final InvokeExpr invkExpr = invoke.getInvokeExpr();
final SootMethod sootMethod = invkExpr.getMethod();
System.out.println("Invoke Statement: " + invoke);
return new FlowFunction<Map<Value, MockStatus>>(){
@Override
public Set<Map<Value, MockStatus>> computeTargets(Map<Value, MockStatus> source){
// Third way to create mock: Mock libraries' API. Example: mock(A.class)
// x = mock(X);
Set<Map<Value, MockStatus>> res = new HashSet<>();
res.add(source);
Map<Value, MockStatus> hm_mockSource = new HashMap<>();
if (Util.isMockAPI(sootMethod)) {
List<ValueBox> defBoxes = curr.getDefBoxes();
for (ValueBox vb: defBoxes) {
System.out.println("Unit: " + curr);
Value v = vb.getValue();
MockStatus status = new MockStatus(true);
hm_mockSource.put(v, status);
}
}
res.add(hm_mockSource);
return res;
}
};
}
return Identity.v();
}
protected FlowFunction<Map<Value, MockStatus>> getCallFlow(Unit callStmt, SootMethod destinationMethod) {
if ("<clinit>".equals(destinationMethod.getName())) {
return KillAll.v();
}
if (destinationMethod.getDeclaringClass().getPackageName().contains("ca.liang")) {
System.out.println("In getCallFlow; ");
System.out.println("Unit callStmt: " + callStmt);
System.out.println("SootMethod destinationMethod: " + destinationMethod);
System.out.println();
}
Stmt stmt = (Stmt) callStmt;
InvokeExpr ie = stmt.getInvokeExpr();
final List<Value> callArgs = ie.getArgs();
final List<Value> paramLocals = new ArrayList<Value>();
for (int i=0;i<destinationMethod.getParameterCount();i++) {
paramLocals.add(destinationMethod.getActiveBody().getParameterLocal(i));
}
Value base = null;
if (ie instanceof VirtualInvokeExpr) {
VirtualInvokeExpr vie = (VirtualInvokeExpr) ie;
base = vie.getBase();
}
else if (ie instanceof InterfaceInvokeExpr) {
InterfaceInvokeExpr iie = (InterfaceInvokeExpr) ie;
base = iie.getBase();
}
else if (ie instanceof SpecialInvokeExpr) {
SpecialInvokeExpr iie = (SpecialInvokeExpr) ie;
base = iie.getBase();
}
final Value baseF = base;
return new FlowFunction<Map<Value, MockStatus>>() {
@Override
public Set<Map<Value, MockStatus>> computeTargets(Map<Value, MockStatus> source) {
Set<Map<Value, MockStatus>> ret = new HashSet<>();
// if (source instanceof StaticFieldRef) {
// ret.add(source);
// }
ret.add(source);
for (int i=0; i<callArgs.size(); i++) {
Set<Value> keys = source.keySet();
for (Value key : keys) {
if (callArgs.get(i).equivTo(key) && i < paramLocals.size()) {
Map<Value, MockStatus> hm_local = new HashMap<Value, MockStatus>();
hm_local.put(paramLocals.get(i), source.get(key));
ret.add(hm_local);
}
}
}
if (source.containsKey(zeroValue())) {
Map<Value, MockStatus> hm_zeroVal = new HashMap<Value, MockStatus>();
for (int i=0; i<callArgs.size(); i++) {
hm_zeroVal.put(paramLocals.get(i), source.get(zeroValue()));
}
ret.add(hm_zeroVal);
}
if (baseF != null && source.containsKey(baseF)) {
Map<Value, MockStatus> hm_base = new HashMap<Value, MockStatus>();
hm_base.put(destinationMethod.retrieveActiveBody().getThisLocal(), source.get(baseF));
ret.add(hm_base);
}
ret.add(source);
Map<Value, MockStatus> hm_mockSource = new HashMap<>();
if (Util.isMockAPI(destinationMethod)) {
List<ValueBox> defBoxes = stmt.getDefBoxes();
for (ValueBox vb: defBoxes) {
System.out.println("Unit: " + stmt);
Value v = vb.getValue();
System.out.println("Value: " + v);
MockStatus status = new MockStatus(true);
hm_mockSource.put(v, status);
}
}
ret.add(hm_mockSource);
return ret;
}
};
}
@Override
public Map<Unit, Set<Map<Value, MockStatus>>> initialSeeds() {
return DefaultSeeds.make(Collections.singleton(Scene.v().getMainMethod().getActiveBody().getUnits().getFirst()),
zeroValue());
}
@Override
protected Map<Value, MockStatus> createZeroValue() {
Map<Value, MockStatus> initMap = new HashMap<Value, MockStatus>() {{
}};
initMap.put(new JimpleLocal("<<zero>>", NullType.v()), new MockStatus(false, false, false));
return initMap;
}
}