a6da2f69afac6f08a9ac7769f86b9827fa985624
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
1 package org.ibex.arenaj;
2 import soot.*;
3 import soot.jimple.*;
4 import soot.util.*;
5 import java.io.*;
6 import java.util.*;
7
8 public class Main extends SceneTransformer {    
9
10     public static final int initialSize = 1000;
11
12     private static Main instance = new Main();
13     private Main() { }
14     public static Main v() { return instance; }
15    
16     public static void main(String[] args) throws Exception {
17         if(args.length == 0) {
18             System.out.println("Syntax: java " + v().getClass().getName() + " [soot options]");
19             System.exit(0);
20         }
21         PackManager.v().getPack("wjtp").add(new Transform("wjtp.tx", Main.v()));
22         String[] args2 = new String[args.length + 14];
23         System.arraycopy(args, 0, args2, 0, args.length-1);
24         String sootcp =
25             System.getProperty("java.class.path") + 
26             File.pathSeparator +
27             System.getProperty("sun.boot.class.path");
28         String extDirs = System.getProperty("java.ext.dirs");
29         if (extDirs != null) {
30             StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
31             while(st.hasMoreTokens()) {
32                 String goo = st.nextToken();
33                 System.out.println("goo " + goo);
34                 File f = new File(goo);
35                 if (!f.exists() || !f.isDirectory()) continue;
36                 String[] jars = f.list();
37                 for(int i=0; i<jars.length; i++)
38                     sootcp += File.pathSeparator + f.getPath() + File.separatorChar + jars[i];
39             }
40         }
41         args2[args.length - 1] = "-w";
42         args2[args.length + 0] = "-p";
43         args2[args.length + 1] = "cg";
44         args2[args.length + 2] = "enabled:false";
45         args2[args.length + 3] = "-p";
46         args2[args.length + 4] = "wjap";
47         args2[args.length + 5] = "enabled:false";
48         args2[args.length + 6] = "-p";
49         args2[args.length + 7] = "jtp";
50         args2[args.length + 8] = "enabled:false";
51         //args2[args.length + 9] = "-keep-line-number";
52         args2[args.length + 9] = "-allow-phantom-refs";
53         args2[args.length + 10] = "-allow-phantom-refs";
54         args2[args.length + 11] = "-cp";
55         args2[args.length + 12] = sootcp;
56         System.out.println("sootcp => " + sootcp);
57         args2[args.length + 13] = args[args.length-1];
58         for(int i=0; i<args2.length; i++) System.out.println("args2["+i+"] = " + args2[i]);
59         soot.Main.main(args2);
60     }
61
62     static int tfr = 0;
63
64     public static Local newLocal(Body b, Type t) {
65         Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
66         b.getLocals().add(l);
67         return l;
68     }
69
70     public static Local viaLocal(Value v, Body b) {
71         Local l = newLocal(b, v.getType());
72         if (v instanceof IdentityRef) {
73             b.getUnits().addFirst(Jimple.v().newIdentityStmt(l, v));
74         } else {
75             b.getUnits().addFirst(Jimple.v().newAssignStmt(l, v));
76         }
77         return l;
78     }
79     public static Local viaLocal(Value v, Body b, Unit u) {
80         Local l = newLocal(b, v.getType());
81         if (v instanceof IdentityRef) {
82             b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
83         } else {
84             b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
85         }
86         return l;
87     }
88
89     HashMap map = new HashMap();
90     HashSet size_added = new HashSet();
91
92     public boolean   implementsGladiator(SootClass c)  { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
93     public SootField getSliceForField(SootField f)    { return getSliceForField(f.makeRef()); }
94     public SootField getSliceForField(SootFieldRef f) {
95         SootClass c  = f.declaringClass();
96         SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
97         String sig = f.declaringClass().getName()+"."+f.name();
98         if (map.get(sig) != null) return (SootField)map.get(sig);
99         Type t = f.type();
100         if (t instanceof RefType && implementsGladiator(((RefType)t).getSootClass())) t = IntType.v();
101         SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.name(),
102                                      t.makeArrayType(),
103                                      0);
104         oc.addField(nf);
105
106         Body body = getInitBody(oc);
107         Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
108         Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
109         body.getLocals().add(newArrLocal);
110         InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
111         body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
112         body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
113
114         map.put(sig, nf);
115         return nf;
116     }
117
118     public Body getInitBody(SootClass c) {
119         c.setApplicationClass();
120         List list = new LinkedList();
121         SootMethod m = c.getMethod("<init>", list);
122         if (!m.hasActiveBody()) {
123             JimpleBody b = (JimpleBody)Jimple.v().newBody(m);
124             m.setActiveBody(b);
125         }
126         return m.getActiveBody();
127     }
128
129     public boolean implementsGladiator(Type t) { return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass()); }
130
131     public void internalTransform(String phaseName, Map options) {
132         System.out.println("begun");
133         init();
134         List ac = new LinkedList();
135         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
136             SootClass sc = (SootClass)it.next();
137             sc.setLibraryClass();
138         }
139         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
140             SootClass sc = (SootClass)it.next();
141             if (implementsGladiator(sc)) {
142                 System.out.println("  application class: " + sc.getName());
143                 System.out.println("  application class: " + getParent(sc).getName());
144                 sc.setApplicationClass();
145                 getParent(sc).setApplicationClass();
146                 ac.add(sc);
147                 ac.add(getParent(sc));
148                 for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
149                     SootMethod m = (SootMethod)i2.next();
150                     if (m.isConcrete()) m.retrieveActiveBody();
151                 }
152                 for(Iterator i2 = getParent(sc).getMethods().iterator(); i2.hasNext();) {
153                     SootMethod m = (SootMethod)i2.next();
154                     if (m.isConcrete()) m.retrieveActiveBody();
155                 }
156             }
157         }
158         for(Iterator it = ac.iterator(); it.hasNext();) {
159             SootClass sc = (SootClass)it.next();
160             if (sc.isInterface()) continue;
161             if (!implementsGladiator(sc)) continue;
162             System.out.println("fixing Gladiator class: " + sc);
163             fixClass(sc);
164         }
165         for(Iterator it = ac.iterator(); it.hasNext();) {
166             SootClass sc = (SootClass)it.next();
167             if (sc.isInterface()) continue;
168             System.out.println("        updating class: " + sc);
169             nuke(sc);
170         }
171     }
172
173     public SootClass getParent(SootClass sc) {
174         return Scene.v().getSootClass(sc.getName().substring(0, sc.getName().lastIndexOf('$')));
175     }
176
177     public static InstanceFieldRef newIFR(Body body, SootFieldRef fr) {
178         return Jimple.v().newInstanceFieldRef(body.getThisLocal(), fr);
179     }
180
181     public Type objectType;
182     public List arrayCopyTypes = new LinkedList();
183     public SootMethodRef arrayCopy;
184     public void init() {
185         objectType = Scene.v().getSootClass("java.lang.Object").getType();
186         arrayCopyTypes.add(objectType);
187         arrayCopyTypes.add(IntType.v());
188         arrayCopyTypes.add(objectType);
189         arrayCopyTypes.add(IntType.v());
190         arrayCopyTypes.add(IntType.v());
191         arrayCopy = 
192             Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
193                                     "arraycopy",
194                                     arrayCopyTypes,
195                                     VoidType.v(),
196                                     true);
197     }
198
199     public static void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
200     public static void assign(Body b, Value left, Value right, Stmt where) {
201         b.getUnits().insertBefore(Jimple.v().newAssignStmt(left, right), where);
202     }
203
204     public void fixClass(SootClass sc) {
205         SootClass mc = getParent(sc);
206         String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
207         String incFuncName = subname + "$$inc";
208         SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
209         Body mcInitBody = getInitBody(mc);
210         mc.addMethod(method);
211         Body body = Jimple.v().newBody(method);
212         method.setActiveBody(body);
213         ((JimpleBody)body).insertIdentityStmts();
214         Local l  =  newLocal(body, IntType.v());
215         Local l2 =  newLocal(body, IntType.v());
216         Local l3 =  newLocal(body, IntType.v());
217         Local l4 =  newLocal(body, IntType.v());
218
219         Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
220         SootField maxField = new SootField(subname + "$$max", IntType.v());
221         mc.addField(maxField);
222         assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
223
224         SootField sfr = new SootField(subname + "$$size", IntType.v());
225         mc.addField(sfr);
226         assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
227         
228         assign(body, l,                                 newIFR(body, sfr.makeRef()));
229         assign(body, l2,                                Jimple.v().newAddExpr(l, IntConstant.v(1)));
230         assign(body, newIFR(body, sfr.makeRef()),       l2);
231         assign(body, l3,                                newIFR(body, maxField.makeRef()));
232
233         Stmt returnStmt = Jimple.v().newReturnStmt(l2);
234         body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
235
236         assign(body,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
237         assign(body,  newIFR(body, maxField.makeRef()), l4);
238
239         for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
240             SootField f = getSliceForField((SootField)it.next());
241             Local ll0 = newLocal(body, f.getType());
242             assign(body, ll0, newIFR(body,  f.makeRef()));
243             Local ll = newLocal(body, f.getType());
244             assign(body, ll, Jimple.v().newNewArrayExpr(((ArrayType)f.getType()).getElementType(), l4));
245             List args = new LinkedList();
246             args.add(ll0);
247             args.add(IntConstant.v(0));
248             args.add(ll);
249             args.add(IntConstant.v(0));
250             args.add(l3);
251             body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
252             body.getUnits().add(Jimple.v().newAssignStmt(newIFR(body,  f.makeRef()), ll));
253         }
254         for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
255             SootMethod m = (SootMethod)it.next();
256             if (!m.isConcrete()) continue;
257             boolean doremove = true;
258             Body mbody = m.getActiveBody();
259             if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
260                 doremove = false;
261                 SootClass c = m.getDeclaringClass();
262                 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
263                 List li = m.getParameterTypes();
264                 c.removeMethod(m);
265                 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
266                 JimpleBody bod = Jimple.v().newBody(nm);
267                 bod.importBodyContentsFrom(m.getActiveBody());
268                 nm.setActiveBody(bod);
269                 m = nm;
270                 mbody = bod;
271                 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
272                 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
273             } else {
274                 System.out.println("examining " + sc.getName() + "." + m.getSignature());
275                 m.retrieveActiveBody();
276             }
277             if (m.isStatic()) continue;
278
279             String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
280             List list = new LinkedList();
281             list.addAll(m.getParameterTypes());
282             list.add(IntType.v());
283             for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
284             SootMethod m2 = new SootMethod(name, list, m.getReturnType());
285             getParent(sc).addMethod(m2);
286
287             JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
288             ab.importBodyContentsFrom(mbody);
289             m2.setActiveBody(ab);
290             Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
291             ab.getLocals().add(loc);
292             // FIXME: insert assignment to this
293
294             for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
295                 Local loc2 = (Local)z.next();
296                 if (implementsGladiator(loc2.getType())) {
297                     loc2.setType(IntType.v());
298                 }
299             }
300
301             Chain units = ab.getUnits();
302             boolean touched = false;
303             Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
304             ab.getLocals().add(loc0);
305             for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
306                 Stmt s = (Stmt) stmtIt.next();
307                 if (s instanceof IdentityStmt) {
308                     IdentityStmt is = (IdentityStmt)s;
309                     Local left = (Local)is.getLeftOp();
310                     if (is.getRightOp() instanceof ThisRef) {
311                         left.setType(IntType.v());
312                         is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
313                         if (!touched) {
314                             units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getParent(sc).getType())));
315                             touched = true;
316                         }
317                     }
318                 }
319
320                 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
321                     Object o = i.next();
322                     if (o instanceof ValueBox) {
323                         ValueBox vb = (ValueBox)o;
324                         o = vb.getValue();
325                         /*
326                         if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
327                             System.out.println("thunking");
328                             vb.setValue(loc0);
329                         }
330                         */
331                         if (vb.getValue() instanceof ThisRef) {
332                             System.out.println(s);
333                             vb.setValue(loc);
334                         }
335                     }
336                 }
337
338             }
339             
340             if (doremove) sc.removeMethod(m);
341
342         }
343         body.getUnits().add(returnStmt);
344     }
345
346     public void nuke(SootClass c) {
347         for(Iterator it = c.getFields().iterator(); it.hasNext();) {
348             SootField f = (SootField)it.next();
349             Type t = f.getType();
350             if (t instanceof RefType) {
351                 RefType rt = (RefType)t;
352                 SootClass sc = rt.getSootClass();
353                 if (implementsGladiator(sc)) f.setType(IntType.v());
354             } else if (t instanceof ArrayType) {
355                 ArrayType at = (ArrayType)t;
356                 t = at.getElementType();
357                 if (!(t instanceof RefType)) continue;                
358                 RefType rt = (RefType)t;
359                 SootClass sc = rt.getSootClass();
360                 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
361             }
362         }
363
364         List list = new LinkedList(); list.addAll(c.getMethods());
365         for(Iterator it = list.iterator(); it.hasNext();) {
366             SootMethod m = (SootMethod)it.next();
367             Body b = null;
368             if (m.getName().endsWith("$$inc")) continue;
369             if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
370             List l2 = new LinkedList();
371             List l = m.getParameterTypes();
372             for(Iterator it2 = l.iterator(); it2.hasNext();) {
373                 Type t = (Type)it2.next();
374                 l2.add(implementsGladiator(t) ? IntType.v() : t);
375             }
376             Type t = m.getReturnType();
377             if (implementsGladiator(t)) {
378                 t = IntType.v();
379                 if (m.hasActiveBody()) {
380                     Body bod = m.getActiveBody();
381                     for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
382                         Stmt s = (Stmt) stmtIt.next();
383                         if (s instanceof ReturnStmt) {
384                             if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
385                                 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
386                             }
387                         }
388                     }
389                 }
390             }
391             String name = m.getName();
392             SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
393             if (b != null) {
394                 JimpleBody b2 = Jimple.v().newBody(meth);
395                 b2.importBodyContentsFrom(b);
396                 meth.setActiveBody(b2);
397             }
398             c.removeMethod(m);
399             c.addMethod(meth);
400         }
401
402     }
403
404     protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
405         if (body instanceof JimpleBody) {
406             JimpleBody b2 = Jimple.v().newBody(smeth);
407             b2.importBodyContentsFrom(body);
408             body = b2;
409         }
410         Chain units = body.getUnits();
411         System.out.println("fixBody("+body.getMethod()+")");
412         for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
413             Local l = (Local)it.next();
414             if (implementsGladiator(l.getType())) l.setType(IntType.v());
415         }
416         for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
417             Stmt s = (Stmt) stmtIt.next();
418             if (s instanceof DefinitionStmt) {
419                 DefinitionStmt ds = (DefinitionStmt)s;
420                 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
421                     ds.getRightOpBox().setValue(IntConstant.v(-1));
422                 }
423             }
424             if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
425                 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
426                     ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
427             List l = s.getUseAndDefBoxes();
428             for(Iterator it = l.iterator(); it.hasNext();) {
429                 Object o = it.next();
430                 if (o instanceof ValueBox) {
431                     ValueBox vb = (ValueBox)o;
432                     Value v = vb.getValue();
433                     
434                     if (v instanceof BinopExpr) {
435                         BinopExpr boe = (BinopExpr)v;
436                         Type t1 = boe.getOp1().getType();
437                         Type t2 = boe.getOp2().getType();
438                         if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
439                         if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
440                     }
441
442                     if (v instanceof NewExpr) {
443                         NewExpr ne = (NewExpr)v;
444                         if (implementsGladiator(ne.getBaseType())) {
445                             SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
446                             SootClass mc = getParent(sc);
447                             String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
448                             SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
449                             Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
450                             Local ll = viaLocal(invokeExpr, body, s);
451                             vb.setValue(ll);
452                             v = ll;
453                             continue;
454                         } 
455                     } else
456
457                     if (v instanceof InvokeExpr) {
458                         InvokeExpr ie = (InvokeExpr)v;
459                         SootMethodRef mr = ie.getMethodRef();
460                         String name = mr.name();
461                         if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
462                             InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
463                             List li = new LinkedList();
464                             li.addAll(iie.getArgs());
465                             LinkedList pl = new LinkedList();
466                             for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
467                                 Type t = (Type)it2.next();
468                                 pl.add(implementsGladiator(t) ? IntType.v() : t);
469                             }
470                             if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
471                                 name = "$init";
472                                 //li.remove(0);
473                                 //pl.remove(0);
474                             }
475                             pl.add(IntType.v());
476                             li.add(viaLocal(iie.getBase(),body,s));
477                             SootClass sc = mr.declaringClass();
478                             name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
479                             mr = Scene.v().makeMethodRef(getParent(sc),
480                                                          name,
481                                                          pl,
482                                                          implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
483                                                          false);
484                             ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
485                             vb.setValue(v = ie);
486
487                         } else if (!(v instanceof StaticInvokeExpr)) {
488                             List l0 = mr.parameterTypes();
489                             List l2 = new LinkedList();
490                             for(Iterator it2 = l0.iterator(); it2.hasNext();) {
491                                 Type t = (Type)it2.next();
492                                 l2.add(implementsGladiator(t) ? IntType.v() : t);
493                             }
494                             mr = Scene.v().makeMethodRef(mr.declaringClass(),
495                                                          mr.name(),
496                                                          l2,
497                                                          implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
498                                                          mr.isStatic());
499                             ie.setMethodRef(mr);
500                             vb.setValue(v = ie);                            
501                         }
502
503                         for(int i=0; i<ie.getArgCount(); i++) {
504                             ValueBox b = ie.getArgBox(i);
505                             Value val = b.getValue();
506                             if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
507                                 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
508                                 List typelist = new LinkedList();
509                                 typelist.add(IntType.v());
510                                 SootMethod intMethod = intClass.getMethod("<init>", typelist);
511                                 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
512                                 List list = new LinkedList();
513                                 list.add(val);
514                                 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
515                                                                                                             intMethod.makeRef(),
516                                                                                                             list)),
517                                                    s);
518                                 b.setValue(loc);
519                             }
520                             if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
521                                 b.setValue(IntConstant.v(-1));
522                             }
523                         }
524
525
526                     } else if (v instanceof CastExpr) {
527                         CastExpr ce = (CastExpr)v;
528                         if (implementsGladiator(ce.getCastType())) {
529                             SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass());
530                             SootClass ic = Scene.v().getSootClass("java.lang.Integer");
531                             ce.setCastType(ic.getType());
532
533                             Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
534                             Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
535
536                             Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
537                             body.getUnits().insertBefore(s2, s);
538
539                             Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
540                             body.getUnits().insertAfter(isNull, s2);
541
542                             Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
543                             body.getUnits().insertAfter(ifStmt, isNull);
544
545                             SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
546                             Stmt isNotNull =
547                                 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
548                             body.getUnits().insertAfter(isNotNull, ifStmt);
549
550                             vb.setValue(l2);
551                             qq = 0;  // ???
552                             break;
553                         }
554
555                     } else if (v instanceof InstanceFieldRef) {
556                         InstanceFieldRef ifr = (InstanceFieldRef)v;
557                         SootFieldRef fr = ifr.getFieldRef();
558                         Type t = fr.type();
559                         if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
560                             System.out.println("******************");
561                             vb.setValue(body.getThisLocal());
562                             System.out.println("s is " + s);
563                         } else if (implementsGladiator(fr.declaringClass())) {
564                             SootClass mc = getParent(fr.declaringClass());
565                             SootFieldRef sf = getSliceForField(fr).makeRef();
566                             InstanceFieldRef sfr = newIFR(body,  sf);
567                             vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
568                         }
569                         if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
570                             SootClass tc = ((RefType)t).getSootClass();
571                             SootClass mc = getParent(tc);
572                             ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
573                         }
574                     }
575
576                 }
577             }
578         }
579         body.validate();
580         return body;
581     }
582 }