From 2d5642bdfc7f316f23851e2ced189213f6f17198 Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 10 May 2005 03:35:32 +0000 Subject: [PATCH] checkpoint darcs-hash:20050510033532-5007d-6c4dcadb382ad229c2b632f384f4f465b20632c7.gz --- src/org/ibex/arenaj/Main.java | 333 +++++++++++++++++++++++++++++++---------- 1 file changed, 254 insertions(+), 79 deletions(-) diff --git a/src/org/ibex/arenaj/Main.java b/src/org/ibex/arenaj/Main.java index 27e2643..674dfb6 100644 --- a/src/org/ibex/arenaj/Main.java +++ b/src/org/ibex/arenaj/Main.java @@ -19,20 +19,23 @@ public class Main extends SceneTransformer { System.exit(0); } PackManager.v().getPack("wjtp").add(new Transform("wjtp.tx", Main.v())); - String[] args2 = new String[args.length + 11]; + String[] args2 = new String[args.length + 14]; System.arraycopy(args, 0, args2, 0, args.length-1); String sootcp = System.getProperty("java.class.path") + File.pathSeparator + System.getProperty("sun.boot.class.path"); - if (System.getProperty("java.ext.dirs") != null) { - StringTokenizer st = new StringTokenizer(File.pathSeparatorChar+"", System.getProperty("java.ext.dirs")); + String extDirs = System.getProperty("java.ext.dirs"); + if (extDirs != null) { + StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+""); while(st.hasMoreTokens()) { - File f = new File(st.nextToken()); + String goo = st.nextToken(); + System.out.println("goo " + goo); + File f = new File(goo); if (!f.exists() || !f.isDirectory()) continue; String[] jars = f.list(); for(int i=0; i " + sootcp); + args2[args.length + 13] = args[args.length-1]; + for(int i=0; i", new LinkedList()).getActiveBody(); + + Body body = getInitBody(oc); Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize)); Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType()); body.getLocals().add(newArrLocal); - InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef()); + InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), nf.makeRef()); body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal)); body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr)); + map.put(sig, nf); return nf; } - private Body body; + public Body getInitBody(SootClass c) { + c.setApplicationClass(); + List list = new LinkedList(); + SootMethod m = c.getMethod("", list); + if (!m.hasActiveBody()) { + JimpleBody b = (JimpleBody)Jimple.v().newBody(m); + m.setActiveBody(b); + } + return m.getActiveBody(); + } + + public Local thisLocal(SootClass c, Body b) { return viaLocal(Jimple.v().newThisRef(c.getType()), b); } public SootFieldRef getGladiatorFieldSizeRef(SootClass c) { SootClass mc = getParent(c); String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$size"; @@ -98,8 +134,8 @@ public class Main extends SceneTransformer { SootField f = new SootField(name, IntType.v()); mc.addField(f); - Body body = mc.getMethod("", new LinkedList()).getActiveBody(); - InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef()); + Body body = getInitBody(mc); + InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef()); body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0))); map.put(c, f); @@ -113,8 +149,8 @@ public class Main extends SceneTransformer { SootField f = new SootField(name, IntType.v()); mc.addField(f); - Body body = mc.getMethod("", new LinkedList()).getActiveBody(); - InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef()); + Body body = getInitBody(mc); + InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef()); body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(initialSize))); map.put(c, f); @@ -122,34 +158,48 @@ public class Main extends SceneTransformer { return Scene.v().makeFieldRef(mc, name, IntType.v(), false); } - public boolean isGladiatorType(Type t) { - return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass()); - } + public boolean isGladiatorType(Type t) { return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass()); } - public SootMethodRef convert(SootMethodRef mr) { - List l = mr.parameterTypes(); - List l2 = new LinkedList(); - for(Iterator it2 = l.iterator(); it2.hasNext();) { - Type t = (Type)it2.next(); - l2.add(isGladiatorType(t) ? IntType.v() : t); - } - return Scene.v().makeMethodRef(mr.declaringClass(), - mr.name(), - l2, - isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(), - mr.isStatic()); - } + + boolean done = false; public void internalTransform(String phaseName, Map options) { - for(Iterator it = Scene.v().getApplicationClasses().iterator(); it.hasNext();) { + System.out.println("begun"); + List ac = new LinkedList(); + for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) { + SootClass sc = (SootClass)it.next(); + sc.setLibraryClass(); + } + for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) { SootClass sc = (SootClass)it.next(); + if (isGladiator(sc)) { + System.out.println(" application class: " + sc.getName()); + System.out.println(" application class: " + getParent(sc).getName()); + sc.setApplicationClass(); + getParent(sc).setApplicationClass(); + ac.add(sc); + ac.add(getParent(sc)); + for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) { + SootMethod m = (SootMethod)i2.next(); + if (m.isConcrete()) m.retrieveActiveBody(); + } + for(Iterator i2 = getParent(sc).getMethods().iterator(); i2.hasNext();) { + SootMethod m = (SootMethod)i2.next(); + if (m.isConcrete()) m.retrieveActiveBody(); + } + } + } + for(Iterator it = ac.iterator(); it.hasNext();) { + SootClass sc = (SootClass)it.next(); + if (sc.isInterface()) continue; if (!isGladiator(sc)) continue; System.out.println("fixing Gladiator class: " + sc); fixClass(sc); } - for(Iterator it = Scene.v().getApplicationClasses().iterator(); it.hasNext();) { + for(Iterator it = ac.iterator(); it.hasNext();) { SootClass sc = (SootClass)it.next(); + if (sc.isInterface()) continue; System.out.println(" updating class: " + sc); nuke(sc); } @@ -166,8 +216,8 @@ public class Main extends SceneTransformer { IntType.v(), Modifier.PRIVATE, new LinkedList()); mc.addMethod(method); - method.setActiveBody(body = Jimple.v().newBody(method)); - body.setMethod(method); + Body body = Jimple.v().newBody(method); + method.setActiveBody(body); ((JimpleBody)body).insertIdentityStmts(); Local l = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l); @@ -221,7 +271,79 @@ public class Main extends SceneTransformer { body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args))); body.getUnits().add(Jimple.v().newAssignStmt(ifr,ll)); } + for(Iterator it = sc.getMethods().iterator(); it.hasNext();) { + SootMethod m = (SootMethod)it.next(); + if (!m.isConcrete()) continue; + if (isGladiator(m.getDeclaringClass()) && m.getName().equals("")) { + SootClass c = m.getDeclaringClass(); + String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$$init"; + List li = m.getParameterTypes(); + li.remove(0); + li.add(IntType.v()); + c.removeMethod(m); + SootMethod nm = new SootMethod(name, li, isGladiatorType(m.getReturnType()) ? IntType.v() : m.getReturnType()); + JimpleBody bod = Jimple.v().newBody(nm); + getParent(c).addMethod(nm); + //bod.insertIdentityStmts(); + bod.importBodyContentsFrom(m.getActiveBody()); + nm.setActiveBody(bod); + continue; + } + System.out.println("examining " + sc.getName() + "." + m.getSignature()); + m.retrieveActiveBody(); + if (m.isStatic()) continue; + + String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName(); + List list = new LinkedList(); + list.addAll(m.getParameterTypes()); + list.add(IntType.v()); + for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next()); + SootMethod m2 = new SootMethod(name, list, m.getReturnType()); + getParent(sc).addMethod(m2); + JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2); + ab.importBodyContentsFrom(m.getActiveBody()); + m2.setActiveBody(ab); + //Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType()); + //ab.getLocals().add(loc); + // FIXME: insert assignment to this + + for(Iterator z = ab.getLocals().iterator(); z.hasNext();) { + Local loc2 = (Local)z.next(); + if (isGladiatorType(loc2.getType())) { + loc2.setType(IntType.v()); + } + } + + Chain units = ab.getUnits(); + for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) { + Stmt s = (Stmt) stmtIt.next(); + if (s instanceof IdentityStmt) { + IdentityStmt is = (IdentityStmt)s; + Local left = (Local)is.getLeftOp(); + if (is.getRightOp() instanceof ThisRef) { + left.setType(IntType.v()); + is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount())); + break; + } + } + /* + for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) { + Object o = i.next(); + if (o instanceof ValueBox) { + ValueBox vb = (ValueBox)o; + if (vb.getValue() instanceof ThisRef) { + System.out.println(s); + vb.setValue(loc); + } + } + } + */ + } + + sc.removeMethod(m); + + } body.getUnits().add(stmt); } @@ -246,10 +368,8 @@ public class Main extends SceneTransformer { List list = new LinkedList(); list.addAll(c.getMethods()); for(Iterator it = list.iterator(); it.hasNext();) { SootMethod m = (SootMethod)it.next(); - if (isGladiator(c) && m.getName().equals("")) continue; - if (m.hasActiveBody() && !m.isStatic()) m.setActiveBody(fixBody(m.getActiveBody())); - if (m.getName().equals("")) continue; - if (m.getName().equals("")) continue; + Body b = null; + if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m); List l2 = new LinkedList(); List l = m.getParameterTypes(); for(Iterator it2 = l.iterator(); it2.hasNext();) { @@ -257,42 +377,55 @@ public class Main extends SceneTransformer { l2.add(isGladiatorType(t) ? IntType.v() : t); } Type t = m.getReturnType(); - c.removeMethod(m); if (isGladiatorType(t)) { t = IntType.v(); - Body bod = m.getActiveBody(); - for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) { - Stmt s = (Stmt) stmtIt.next(); - if (s instanceof ReturnStmt) { - if (((ReturnStmt)s).getOp().getType() instanceof NullType) { - ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1)); + if (m.hasActiveBody()) { + Body bod = m.getActiveBody(); + for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) { + Stmt s = (Stmt) stmtIt.next(); + if (s instanceof ReturnStmt) { + if (((ReturnStmt)s).getOp().getType() instanceof NullType) { + ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1)); + } } } } } - SootMethod meth = new SootMethod(m.getName(), l2, t, m.getModifiers()); - meth.setActiveBody(m.getActiveBody()); + String name = m.getName(); + SootMethod meth = new SootMethod(name, l2, isGladiatorType(t) ? IntType.v() : t, m.getModifiers()); + if (b != null) { + JimpleBody b2 = Jimple.v().newBody(meth); + b2.importBodyContentsFrom(b); + meth.setActiveBody(b2); + } + c.removeMethod(m); c.addMethod(meth); } } - protected Body fixBody(Body body) { - this.body = body; + protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) { + if (body instanceof JimpleBody) { + JimpleBody b2 = Jimple.v().newBody(smeth); + //b2.insertIdentityStmts(); + b2.importBodyContentsFrom(body); + body = b2; + } Chain units = body.getUnits(); + System.out.println("fixBody("+body.getMethod()+")"); for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) { Local l = (Local)it.next(); if (isGladiatorType(l.getType())) l.setType(IntType.v()); } for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) { Stmt s = (Stmt) stmtIt.next(); - List l = s.getUseAndDefBoxes(); if (s instanceof DefinitionStmt) { DefinitionStmt ds = (DefinitionStmt)s; if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) { ds.getRightOpBox().setValue(IntConstant.v(-1)); } } + List l = s.getUseAndDefBoxes(); for(Iterator it = l.iterator(); it.hasNext();) { Object o = it.next(); if (o instanceof ValueBox) { @@ -309,16 +442,64 @@ public class Main extends SceneTransformer { } } + if (v instanceof NewExpr) { + NewExpr ne = (NewExpr)v; + if (isGladiatorType(ne.getBaseType())) { + SootClass sc = ((RefType)ne.getBaseType()).getSootClass(); + SootClass mc = getParent(sc); + String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc"; + SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false); + Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(thisLocal(mc,body), smr); + Local ll = viaLocal(invokeExpr, body, s); + vb.setValue(ll); + v = ll; + continue; + } + } else + if (v instanceof InvokeExpr) { InvokeExpr ie = (InvokeExpr)v; SootMethodRef mr = ie.getMethodRef(); - if (isGladiator(mr.declaringClass())) { - body.getUnits().remove(s); - qq = 0; - break; + String name = mr.name(); + if (v instanceof InstanceInvokeExpr && isGladiator(mr.declaringClass())) { + InstanceInvokeExpr iie = (InstanceInvokeExpr)v; + List li = new LinkedList(); + li.addAll(iie.getArgs()); + LinkedList pl = new LinkedList(); + pl.addAll(mr.parameterTypes()); + if (mr.name().equals("") && isGladiator(mr.declaringClass())) { + name = "$init"; + li.remove(0); + pl.remove(0); + //pl.addFirst(body.getThisLocal()); + } + pl.add(IntType.v()); + li.add(viaLocal(iie.getBase(),body,s)); + SootClass sc = mr.declaringClass(); + name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name; + mr = Scene.v().makeMethodRef(getParent(sc), + name, + pl, + isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(), + false); + ie = Jimple.v().newVirtualInvokeExpr(viaLocal(thisLocal(getParent(sc),body),body,s), mr, li); + vb.setValue(v = ie); + } else { + List l0 = mr.parameterTypes(); + List l2 = new LinkedList(); + for(Iterator it2 = l0.iterator(); it2.hasNext();) { + Type t = (Type)it2.next(); + l2.add(isGladiatorType(t) ? IntType.v() : t); + } + mr = Scene.v().makeMethodRef(mr.declaringClass(), + mr.name(), + l2, + isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(), + mr.isStatic()); + ie.setMethodRef(mr); + vb.setValue(v = ie); } - mr = convert(mr); - ie.setMethodRef(mr); + for(int i=0; i