Search in sources :

Example 1 with GrimpBody

use of soot.grimp.GrimpBody in project soot by Sable.

the class DavaBody method copy_Body.

/*
	 *  Copy and patch a GrimpBody so that it can be used to output Java.
	 */
private void copy_Body(Body body) {
    if (!(body instanceof GrimpBody))
        throw new RuntimeException("You can only create a DavaBody from a GrimpBody!");
    GrimpBody grimpBody = (GrimpBody) body;
    /*
		 *  Import body contents from Grimp.
		 */
    {
        HashMap<Switchable, Switchable> bindings = new HashMap<Switchable, Switchable>();
        HashMap<Unit, Unit> reverse_binding = new HashMap<Unit, Unit>();
        // Clone units in body's statement list
        for (Unit original : grimpBody.getUnits()) {
            Unit copy = (Unit) original.clone();
            // Add cloned unit to our unitChain.
            getUnits().addLast(copy);
            // Build old <-> new map to be able to patch up references to other units
            // within the cloned units. (these are still refering to the original
            // unit objects).
            bindings.put(original, copy);
            reverse_binding.put(copy, original);
        }
        // patch up the switch statments
        for (Unit u : getUnits()) {
            Stmt s = (Stmt) u;
            if (s instanceof TableSwitchStmt) {
                TableSwitchStmt ts = (TableSwitchStmt) s;
                TableSwitchStmt original_switch = (TableSwitchStmt) reverse_binding.get(u);
                ts.setDefaultTarget((Unit) bindings.get(original_switch.getDefaultTarget()));
                LinkedList<Unit> new_target_list = new LinkedList<Unit>();
                int target_count = ts.getHighIndex() - ts.getLowIndex() + 1;
                for (int i = 0; i < target_count; i++) new_target_list.add((Unit) bindings.get(original_switch.getTarget(i)));
                ts.setTargets(new_target_list);
            }
            if (s instanceof LookupSwitchStmt) {
                LookupSwitchStmt ls = (LookupSwitchStmt) s;
                LookupSwitchStmt original_switch = (LookupSwitchStmt) reverse_binding.get(u);
                ls.setDefaultTarget((Unit) bindings.get(original_switch.getDefaultTarget()));
                Unit[] new_target_list = new Unit[original_switch.getTargetCount()];
                for (int i = 0; i < original_switch.getTargetCount(); i++) new_target_list[i] = (Unit) (bindings.get(original_switch.getTarget(i)));
                ls.setTargets(new_target_list);
                ls.setLookupValues(original_switch.getLookupValues());
            }
        }
        // Clone locals.
        for (Local original : grimpBody.getLocals()) {
            Local copy = Dava.v().newLocal(original.getName(), original.getType());
            getLocals().add(copy);
            // Build old <-> new mapping.
            bindings.put(original, copy);
        }
        // Patch up references within units using our (old <-> new) map.
        for (UnitBox box : getAllUnitBoxes()) {
            Unit newObject, oldObject = box.getUnit();
            // it's clone.
            if ((newObject = (Unit) bindings.get(oldObject)) != null)
                box.setUnit(newObject);
        }
        // backpatch all local variables.
        for (ValueBox vb : getUseAndDefBoxes()) {
            if (vb.getValue() instanceof Local)
                vb.setValue((Value) bindings.get(vb.getValue()));
        }
        // clone the traps
        for (Trap originalTrap : grimpBody.getTraps()) {
            Trap cloneTrap = (Trap) originalTrap.clone();
            Unit handlerUnit = (Unit) bindings.get(originalTrap.getHandlerUnit());
            cloneTrap.setHandlerUnit(handlerUnit);
            cloneTrap.setBeginUnit((Unit) bindings.get(originalTrap.getBeginUnit()));
            cloneTrap.setEndUnit((Unit) bindings.get(originalTrap.getEndUnit()));
            getTraps().add(cloneTrap);
        }
    }
    /*
		 *  Add one level of indirection to "if", "switch", and exceptional control flow.
		 *  This allows for easy handling of breaks, continues and exceptional loops.
		 */
    {
        PatchingChain<Unit> units = getUnits();
        Iterator<Unit> it = units.snapshotIterator();
        while (it.hasNext()) {
            Unit u = it.next();
            Stmt s = (Stmt) u;
            if (s instanceof IfStmt) {
                IfStmt ifs = (IfStmt) s;
                JGotoStmt jgs = new JGotoStmt((Unit) units.getSuccOf(u));
                units.insertAfter(jgs, u);
                JGotoStmt jumper = new JGotoStmt((Unit) ifs.getTarget());
                units.insertAfter(jumper, jgs);
                ifs.setTarget((Unit) jumper);
            } else if (s instanceof TableSwitchStmt) {
                TableSwitchStmt tss = (TableSwitchStmt) s;
                int targetCount = tss.getHighIndex() - tss.getLowIndex() + 1;
                for (int i = 0; i < targetCount; i++) {
                    JGotoStmt jgs = new JGotoStmt((Unit) tss.getTarget(i));
                    units.insertAfter(jgs, tss);
                    tss.setTarget(i, (Unit) jgs);
                }
                JGotoStmt jgs = new JGotoStmt((Unit) tss.getDefaultTarget());
                units.insertAfter(jgs, tss);
                tss.setDefaultTarget((Unit) jgs);
            } else if (s instanceof LookupSwitchStmt) {
                LookupSwitchStmt lss = (LookupSwitchStmt) s;
                for (int i = 0; i < lss.getTargetCount(); i++) {
                    JGotoStmt jgs = new JGotoStmt((Unit) lss.getTarget(i));
                    units.insertAfter(jgs, lss);
                    lss.setTarget(i, (Unit) jgs);
                }
                JGotoStmt jgs = new JGotoStmt((Unit) lss.getDefaultTarget());
                units.insertAfter(jgs, lss);
                lss.setDefaultTarget((Unit) jgs);
            }
        }
        for (Trap t : getTraps()) {
            JGotoStmt jgs = new JGotoStmt((Unit) t.getHandlerUnit());
            units.addLast(jgs);
            t.setHandlerUnit((Unit) jgs);
        }
    }
    /*
		 *  Fix up the grimp representations of statements so they can be compiled as java.
		 */
    {
        for (Local l : getLocals()) {
            Type t = l.getType();
            if (t instanceof RefType) {
                RefType rt = (RefType) t;
                String className = rt.getSootClass().toString();
                String packageName = rt.getSootClass().getJavaPackageName();
                String classPackageName = packageName;
                if (className.lastIndexOf('.') > 0) {
                    // 0 doesnt make sense
                    classPackageName = className.substring(0, className.lastIndexOf('.'));
                }
                if (!packageName.equals(classPackageName))
                    throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
                addToImportList(className);
            // addPackage(rt.getSootClass().getJavaPackageName());
            }
        }
        for (Unit u : getUnits()) {
            Stmt s = (Stmt) u;
            if (s instanceof IfStmt)
                javafy(((IfStmt) s).getConditionBox());
            else if (s instanceof ThrowStmt)
                javafy(((ThrowStmt) s).getOpBox());
            else if (s instanceof TableSwitchStmt)
                javafy(((TableSwitchStmt) s).getKeyBox());
            else if (s instanceof LookupSwitchStmt)
                javafy(((LookupSwitchStmt) s).getKeyBox());
            else if (s instanceof MonitorStmt)
                javafy(((MonitorStmt) s).getOpBox());
            else if (s instanceof DefinitionStmt) {
                DefinitionStmt ds = (DefinitionStmt) s;
                javafy(ds.getRightOpBox());
                javafy(ds.getLeftOpBox());
                if (ds.getRightOp() instanceof IntConstant)
                    ds.getRightOpBox().setValue(DIntConstant.v(((IntConstant) ds.getRightOp()).value, ds.getLeftOp().getType()));
            } else if (s instanceof ReturnStmt) {
                ReturnStmt rs = (ReturnStmt) s;
                if (rs.getOp() instanceof IntConstant)
                    rs.getOpBox().setValue(DIntConstant.v(((IntConstant) rs.getOp()).value, body.getMethod().getReturnType()));
                else
                    javafy(rs.getOpBox());
            } else if (s instanceof InvokeStmt)
                javafy(((InvokeStmt) s).getInvokeExprBox());
        }
    }
    /*
		 *  Convert references to "this" and parameters.
		 */
    {
        for (Unit u : getUnits()) {
            Stmt s = (Stmt) u;
            if (s instanceof IdentityStmt) {
                IdentityStmt ids = (IdentityStmt) s;
                Value ids_rightOp = ids.getRightOp();
                Value ids_leftOp = ids.getLeftOp();
                if ((ids_leftOp instanceof Local) && (ids_rightOp instanceof ThisRef)) {
                    Local thisLocal = (Local) ids_leftOp;
                    thisLocals.add(thisLocal);
                    thisLocal.setName("this");
                }
            }
            if (s instanceof DefinitionStmt) {
                DefinitionStmt ds = (DefinitionStmt) s;
                Value rightOp = ds.getRightOp();
                if (rightOp instanceof ParameterRef)
                    pMap.put(((ParameterRef) rightOp).getIndex(), ds.getLeftOp());
                if (rightOp instanceof CaughtExceptionRef)
                    caughtrefs.add((CaughtExceptionRef) rightOp);
            }
        }
    }
    /*
		 *  Fix up the calls to other constructors.  Note, this is seriously underbuilt.
		 */
    {
        for (Unit u : getUnits()) {
            Stmt s = (Stmt) u;
            if (s instanceof InvokeStmt) {
                InvokeStmt ivs = (InvokeStmt) s;
                Value ie = ivs.getInvokeExpr();
                if (ie instanceof InstanceInvokeExpr) {
                    InstanceInvokeExpr iie = (InstanceInvokeExpr) ie;
                    Value base = iie.getBase();
                    if ((base instanceof Local) && (((Local) base).getName().equals("this"))) {
                        SootMethodRef m = iie.getMethodRef();
                        String name = m.name();
                        if ((name.equals(SootMethod.constructorName)) || (name.equals(SootMethod.staticInitializerName))) {
                            if (constructorUnit != null)
                                throw new RuntimeException("More than one candidate for constructor found.");
                            constructorExpr = iie;
                            constructorUnit = (Unit) s;
                        }
                    }
                }
            }
        }
    }
}
Also used : InvokeStmt(soot.jimple.InvokeStmt) CaughtExceptionRef(soot.jimple.CaughtExceptionRef) HashMap(java.util.HashMap) Switchable(soot.util.Switchable) Unit(soot.Unit) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) JGotoStmt(soot.jimple.internal.JGotoStmt) IfStmt(soot.jimple.IfStmt) LookupSwitchStmt(soot.jimple.LookupSwitchStmt) Stmt(soot.jimple.Stmt) DefinitionStmt(soot.jimple.DefinitionStmt) InvokeStmt(soot.jimple.InvokeStmt) ThrowStmt(soot.jimple.ThrowStmt) IdentityStmt(soot.jimple.IdentityStmt) TableSwitchStmt(soot.jimple.TableSwitchStmt) MonitorStmt(soot.jimple.MonitorStmt) ReturnStmt(soot.jimple.ReturnStmt) RefType(soot.RefType) TableSwitchStmt(soot.jimple.TableSwitchStmt) Iterator(java.util.Iterator) IntConstant(soot.jimple.IntConstant) DIntConstant(soot.dava.internal.javaRep.DIntConstant) GrimpBody(soot.grimp.GrimpBody) IdentityStmt(soot.jimple.IdentityStmt) UnitBox(soot.UnitBox) PatchingChain(soot.PatchingChain) SootMethodRef(soot.SootMethodRef) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) LookupSwitchStmt(soot.jimple.LookupSwitchStmt) Trap(soot.Trap) MonitorStmt(soot.jimple.MonitorStmt) LinkedList(java.util.LinkedList) JGotoStmt(soot.jimple.internal.JGotoStmt) Type(soot.Type) RefType(soot.RefType) IntType(soot.IntType) IfStmt(soot.jimple.IfStmt) ParameterRef(soot.jimple.ParameterRef) DThisRef(soot.dava.internal.javaRep.DThisRef) ThisRef(soot.jimple.ThisRef) ValueBox(soot.ValueBox) Value(soot.Value) ThrowStmt(soot.jimple.ThrowStmt) DefinitionStmt(soot.jimple.DefinitionStmt) ReturnStmt(soot.jimple.ReturnStmt)

Example 2 with GrimpBody

use of soot.grimp.GrimpBody in project soot by Sable.

the class ConstructorFolder method internalTransform.

/**
 * This method change all new Obj/<init>(args) pairs to new Obj(args) idioms.
 */
protected void internalTransform(Body b, String phaseName, Map options) {
    GrimpBody body = (GrimpBody) b;
    if (Options.v().verbose())
        logger.debug("[" + body.getMethod().getName() + "] Folding constructors...");
    Chain units = body.getUnits();
    List<Unit> stmtList = new ArrayList<Unit>();
    stmtList.addAll(units);
    Iterator<Unit> it = stmtList.iterator();
    LocalUses localUses = LocalUses.Factory.newLocalUses(b);
    /* fold in NewExpr's with specialinvoke's */
    while (it.hasNext()) {
        Stmt s = (Stmt) it.next();
        if (!(s instanceof AssignStmt))
            continue;
        /* this should be generalized to ArrayRefs */
        Value lhs = ((AssignStmt) s).getLeftOp();
        if (!(lhs instanceof Local))
            continue;
        Value rhs = ((AssignStmt) s).getRightOp();
        if (!(rhs instanceof NewExpr))
            continue;
        /* TO BE IMPLEMENTED LATER: move any copy of the object reference
             for lhs down beyond the NewInvokeExpr, with the rationale
             being that you can't modify the object before the constructor
             call in any case.

             Also, do note that any new's (object creation) without
             corresponding constructors must be dead. */
        List lu = localUses.getUsesOf(s);
        Iterator luIter = lu.iterator();
        boolean MadeNewInvokeExpr = false;
        while (luIter.hasNext()) {
            Unit use = ((UnitValueBoxPair) (luIter.next())).unit;
            if (!(use instanceof InvokeStmt))
                continue;
            InvokeStmt is = (InvokeStmt) use;
            if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) || lhs != ((SpecialInvokeExpr) is.getInvokeExpr()).getBase())
                continue;
            SpecialInvokeExpr oldInvoke = ((SpecialInvokeExpr) is.getInvokeExpr());
            LinkedList invokeArgs = new LinkedList();
            for (int i = 0; i < oldInvoke.getArgCount(); i++) invokeArgs.add(oldInvoke.getArg(i));
            AssignStmt constructStmt = Grimp.v().newAssignStmt((AssignStmt) s);
            constructStmt.setRightOp(Grimp.v().newNewInvokeExpr(((NewExpr) rhs).getBaseType(), oldInvoke.getMethodRef(), invokeArgs));
            MadeNewInvokeExpr = true;
            use.redirectJumpsToThisTo(constructStmt);
            units.insertBefore(constructStmt, use);
            units.remove(use);
        }
        if (MadeNewInvokeExpr) {
            units.remove(s);
        }
    }
}
Also used : Chain(soot.util.Chain) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) ArrayList(java.util.ArrayList) Local(soot.Local) SimpleLocalUses(soot.toolkits.scalar.SimpleLocalUses) LocalUses(soot.toolkits.scalar.LocalUses) Unit(soot.Unit) LinkedList(java.util.LinkedList) InvokeStmt(soot.jimple.InvokeStmt) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) Value(soot.Value) NewExpr(soot.jimple.NewExpr) Iterator(java.util.Iterator) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair) GrimpBody(soot.grimp.GrimpBody) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 Local (soot.Local)2 Unit (soot.Unit)2 Value (soot.Value)2 GrimpBody (soot.grimp.GrimpBody)2 InvokeStmt (soot.jimple.InvokeStmt)2 Stmt (soot.jimple.Stmt)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 IntType (soot.IntType)1 PatchingChain (soot.PatchingChain)1 RefType (soot.RefType)1 SootMethodRef (soot.SootMethodRef)1 Trap (soot.Trap)1 Type (soot.Type)1 UnitBox (soot.UnitBox)1 ValueBox (soot.ValueBox)1 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)1