Search in sources :

Example 6 with NewMultiArrayExpr

use of soot.jimple.NewMultiArrayExpr in project soot by Sable.

the class ArrayIndexLivenessAnalysis method getGenAndKillSetForDefnStmt.

private void getGenAndKillSetForDefnStmt(DefinitionStmt asstmt, HashMap<Stmt, HashSet<Value>> absgen, HashSet<Object> genset, HashSet<Value> absgenset, HashSet<Value> killset, HashSet<Value> condset) {
    /* kill left hand side */
    Value lhs = asstmt.getLeftOp();
    Value rhs = asstmt.getRightOp();
    boolean killarrayrelated = false;
    boolean killallarrayref = false;
    if (fieldin) {
        if (lhs instanceof Local) {
            HashSet<Value> related = localToFieldRef.get(lhs);
            if (related != null)
                killset.addAll(related);
        } else if (lhs instanceof StaticFieldRef) {
            killset.add(lhs);
            condset.add(lhs);
        } else if (lhs instanceof InstanceFieldRef) {
            SootField field = ((InstanceFieldRef) lhs).getField();
            HashSet<Value> related = fieldToFieldRef.get(field);
            if (related != null)
                killset.addAll(related);
            condset.add(lhs);
        }
        if (asstmt.containsInvokeExpr()) {
            /*
                Value expr = asstmt.getInvokeExpr();
                List parameters = ((InvokeExpr)expr).getArgs();

                // add the method invocation
                boolean killall = false;
                if (expr instanceof InstanceInvokeExpr)
                    killall = true;
                else
                {
                    for (int i=0; i<parameters.size(); i++)
                    {
                    Value para = (Value)parameters.get(i);
                    if (para.getType() instanceof RefType)
                    {
                        killall = true;
                        break;
                    }
                    }
                }
    
                if (killall)
                {
                    killset.addAll(allInstFieldRefs);
                }   
                */
            killset.addAll(allFieldRefs);
        }
    }
    if (arrayin) {
        // a = ... or i = ...
        if (lhs instanceof Local) {
            killarrayrelated = true;
        } else // a[i] = ...
        if (lhs instanceof ArrayRef) {
            killallarrayref = true;
            condset.add(lhs);
        }
        // invokeexpr kills all array references.
        if (asstmt.containsInvokeExpr()) {
            killallarrayref = true;
        }
    }
    if (csin) {
        HashSet<Value> exprs = localToExpr.get(lhs);
        if (exprs != null)
            killset.addAll(exprs);
        if (rhs instanceof BinopExpr) {
            Value op1 = ((BinopExpr) rhs).getOp1();
            Value op2 = ((BinopExpr) rhs).getOp2();
            if (rhs instanceof AddExpr) {
                if ((op1 instanceof Local) && (op2 instanceof Local))
                    genset.add(rhs);
            } else if (rhs instanceof MulExpr) {
                if ((op1 instanceof Local) || (op2 instanceof Local))
                    genset.add(rhs);
            } else if (rhs instanceof SubExpr) {
                if (op2 instanceof Local)
                    genset.add(rhs);
            }
        }
    }
    if ((lhs instanceof Local) && (fullSet.contains(lhs))) {
        killset.add(lhs);
        /* speculatively add lhs as live condition. */
        condset.add(lhs);
    } else if (lhs instanceof ArrayRef) {
        /* a[i] generate a and i. */
        Value base = ((ArrayRef) lhs).getBase();
        Value index = ((ArrayRef) lhs).getIndex();
        absgenset.add(base);
        if (index instanceof Local) {
            absgenset.add(index);
        }
    }
    if (rhs instanceof Local) {
        /*
              if (lhs instanceof Local && fullSet.contains(rhs))
              genset.add(rhs);
            */
        if (fullSet.contains(rhs))
            genset.add(rhs);
    /*
              if (fieldin && (lhs instanceof FieldRef))
              genset.add(rhs);
            */
    } else if (rhs instanceof FieldRef) {
        if (fieldin)
            genset.add(rhs);
    } else if (rhs instanceof ArrayRef) {
        /* lhs=a[i]. */
        Value base = ((ArrayRef) rhs).getBase();
        Value index = ((ArrayRef) rhs).getIndex();
        absgenset.add(base);
        if (index instanceof Local) {
            absgenset.add(index);
        }
        if (arrayin) {
            genset.add(rhs);
            if (rectarray)
                genset.add(Array2ndDimensionSymbol.v(base));
        }
    } else if (rhs instanceof NewArrayExpr) {
        /* a = new A[i]; */
        Value size = ((NewArrayExpr) rhs).getSize();
        if (size instanceof Local)
            genset.add(size);
    } else if (rhs instanceof NewMultiArrayExpr) {
        /* a = new A[i][]...;*/
        /* More precisely, we should track other dimensions. */
        List sizes = ((NewMultiArrayExpr) rhs).getSizes();
        Iterator sizeIt = sizes.iterator();
        while (sizeIt.hasNext()) {
            Value size = (Value) sizeIt.next();
            if (size instanceof Local)
                genset.add(size);
        }
    } else if (rhs instanceof LengthExpr) {
        /* lhs = lengthof rhs */
        Value op = ((LengthExpr) rhs).getOp();
        genset.add(op);
    } else if (rhs instanceof JAddExpr) {
        /* lhs = rhs+c, lhs=c+rhs */
        Value op1 = ((JAddExpr) rhs).getOp1();
        Value op2 = ((JAddExpr) rhs).getOp2();
        if ((op1 instanceof IntConstant) && (op2 instanceof Local)) {
            genset.add(op2);
        } else if ((op2 instanceof IntConstant) && (op1 instanceof Local)) {
            genset.add(op1);
        }
    } else if (rhs instanceof JSubExpr) {
        Value op1 = ((JSubExpr) rhs).getOp1();
        Value op2 = ((JSubExpr) rhs).getOp2();
        if ((op1 instanceof Local) && (op2 instanceof IntConstant)) {
            genset.add(op1);
        }
    }
    if (arrayin) {
        if (killarrayrelated)
            killArrayRelated.put(asstmt, lhs);
        if (killallarrayref)
            killAllArrayRef.put(asstmt, new Boolean(true));
    }
}
Also used : MulExpr(soot.jimple.MulExpr) JSubExpr(soot.jimple.internal.JSubExpr) FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) StaticFieldRef(soot.jimple.StaticFieldRef) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) LengthExpr(soot.jimple.LengthExpr) JSubExpr(soot.jimple.internal.JSubExpr) SubExpr(soot.jimple.SubExpr) Local(soot.Local) JAddExpr(soot.jimple.internal.JAddExpr) AddExpr(soot.jimple.AddExpr) StaticFieldRef(soot.jimple.StaticFieldRef) ArrayRef(soot.jimple.ArrayRef) NewArrayExpr(soot.jimple.NewArrayExpr) Value(soot.Value) InstanceFieldRef(soot.jimple.InstanceFieldRef) Iterator(java.util.Iterator) IntConstant(soot.jimple.IntConstant) SootField(soot.SootField) ArrayList(java.util.ArrayList) List(java.util.List) JAddExpr(soot.jimple.internal.JAddExpr) BinopExpr(soot.jimple.BinopExpr)

Example 7 with NewMultiArrayExpr

use of soot.jimple.NewMultiArrayExpr in project soot by Sable.

the class DeadAssignmentEliminator method internalTransform.

/**
 * Eliminates dead code in a linear fashion.  Complexity is linear
 * with respect to the statements.
 *
 * Does not work on grimp code because of the check on the right hand
 * side for side effects.
 */
@Override
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
    boolean eliminateOnlyStackLocals = PhaseOptions.getBoolean(options, "only-stack-locals");
    final Options soptions = Options.v();
    if (soptions.verbose()) {
        logger.debug("[" + b.getMethod().getName() + "] Eliminating dead code...");
    }
    if (soptions.time()) {
        Timers.v().deadCodeTimer.start();
    }
    Chain<Unit> units = b.getUnits();
    Deque<Unit> q = new ArrayDeque<Unit>(units.size());
    // Make a first pass through the statements, noting
    // the statements we must absolutely keep.
    boolean isStatic = b.getMethod().isStatic();
    boolean allEssential = true;
    boolean checkInvoke = false;
    Local thisLocal = null;
    for (Iterator<Unit> it = units.iterator(); it.hasNext(); ) {
        Unit s = it.next();
        boolean isEssential = true;
        if (s instanceof NopStmt) {
            // Hack: do not remove nop if is is used for a Trap
            // which is at the very end of the code.
            boolean removeNop = it.hasNext();
            if (!removeNop) {
                removeNop = true;
                for (Trap t : b.getTraps()) {
                    if (t.getEndUnit() == s) {
                        removeNop = false;
                        break;
                    }
                }
            }
            if (removeNop) {
                it.remove();
                continue;
            }
        } else if (s instanceof AssignStmt) {
            AssignStmt as = (AssignStmt) s;
            Value lhs = as.getLeftOp();
            Value rhs = as.getRightOp();
            // Stmt is of the form a = a which is useless
            if (lhs == rhs && lhs instanceof Local) {
                it.remove();
                continue;
            }
            if (lhs instanceof Local && (!eliminateOnlyStackLocals || ((Local) lhs).getName().startsWith("$") || lhs.getType() instanceof NullType)) {
                isEssential = false;
                if (!checkInvoke) {
                    checkInvoke = as.containsInvokeExpr();
                }
                if (rhs instanceof CastExpr) {
                    // CastExpr          : can trigger ClassCastException, but null-casts never fail
                    CastExpr ce = (CastExpr) rhs;
                    Type t = ce.getCastType();
                    Value v = ce.getOp();
                    isEssential = !(v instanceof NullConstant && t instanceof RefType);
                } else if (rhs instanceof InvokeExpr || rhs instanceof ArrayRef || rhs instanceof NewExpr || rhs instanceof NewArrayExpr || rhs instanceof NewMultiArrayExpr) {
                    // ArrayRef          : can have side effects (like throwing a null pointer exception)
                    // InvokeExpr        : can have side effects (like throwing a null pointer exception)
                    // NewArrayExpr      : can throw exception
                    // NewMultiArrayExpr : can throw exception
                    // NewExpr           : can trigger class initialization
                    isEssential = true;
                } else if (rhs instanceof FieldRef) {
                    // Can trigger class initialization
                    isEssential = true;
                    if (rhs instanceof InstanceFieldRef) {
                        InstanceFieldRef ifr = (InstanceFieldRef) rhs;
                        if (!isStatic && thisLocal == null) {
                            thisLocal = b.getThisLocal();
                        }
                        // Any InstanceFieldRef may have side effects,
                        // unless the base is reading from 'this'
                        // in a non-static method
                        isEssential = (isStatic || thisLocal != ifr.getBase());
                    }
                } else if (rhs instanceof DivExpr || rhs instanceof RemExpr) {
                    BinopExpr expr = (BinopExpr) rhs;
                    Type t1 = expr.getOp1().getType();
                    Type t2 = expr.getOp2().getType();
                    // Can trigger a division by zero
                    boolean t2Int = t2 instanceof IntType;
                    isEssential = t2Int || t1 instanceof IntType || t1 instanceof LongType || t2 instanceof LongType || t1 instanceof UnknownType || t2 instanceof UnknownType;
                    if (isEssential && t2Int) {
                        Value v = expr.getOp2();
                        if (v instanceof IntConstant) {
                            IntConstant i = (IntConstant) v;
                            isEssential = (i.value == 0);
                        } else
                            // could be 0, we don't know
                            isEssential = true;
                    }
                    if (isEssential && t2 instanceof LongType) {
                        Value v = expr.getOp2();
                        if (v instanceof LongConstant) {
                            LongConstant l = (LongConstant) v;
                            isEssential = (l.value == 0);
                        } else
                            // could be 0, we don't know
                            isEssential = true;
                    }
                }
            }
        }
        if (isEssential) {
            q.addFirst(s);
        }
        allEssential &= isEssential;
    }
    if (checkInvoke || !allEssential) {
        // Add all the statements which are used to compute values
        // for the essential statements, recursively
        final LocalDefs localDefs = LocalDefs.Factory.newLocalDefs(b);
        if (!allEssential) {
            Set<Unit> essential = new HashSet<Unit>(b.getUnits().size());
            while (!q.isEmpty()) {
                Unit s = q.removeFirst();
                if (essential.add(s)) {
                    for (ValueBox box : s.getUseBoxes()) {
                        Value v = box.getValue();
                        if (v instanceof Local) {
                            Local l = (Local) v;
                            List<Unit> defs = localDefs.getDefsOfAt(l, s);
                            if (defs != null)
                                q.addAll(defs);
                        }
                    }
                }
            }
            // Remove the dead statements
            units.retainAll(essential);
        }
        if (checkInvoke) {
            final LocalUses localUses = LocalUses.Factory.newLocalUses(b, localDefs);
            // Eliminate dead assignments from invokes such as x = f(), where
            // x is no longer used
            List<AssignStmt> postProcess = new ArrayList<AssignStmt>();
            for (Unit u : units) {
                if (u instanceof AssignStmt) {
                    AssignStmt s = (AssignStmt) u;
                    if (s.containsInvokeExpr()) {
                        // Just find one use of l which is essential
                        boolean deadAssignment = true;
                        for (UnitValueBoxPair pair : localUses.getUsesOf(s)) {
                            if (units.contains(pair.unit)) {
                                deadAssignment = false;
                                break;
                            }
                        }
                        if (deadAssignment) {
                            postProcess.add(s);
                        }
                    }
                }
            }
            final Jimple jimple = Jimple.v();
            for (AssignStmt s : postProcess) {
                // Transform it into a simple invoke.
                Stmt newInvoke = jimple.newInvokeStmt(s.getInvokeExpr());
                newInvoke.addAllTagsOf(s);
                units.swapWith(s, newInvoke);
                // If we have a callgraph, we need to fix it
                if (Scene.v().hasCallGraph())
                    Scene.v().getCallGraph().swapEdgesOutOf(s, newInvoke);
            }
        }
    }
    if (soptions.time()) {
        Timers.v().deadCodeTimer.end();
    }
}
Also used : Options(soot.options.Options) PhaseOptions(soot.PhaseOptions) LongType(soot.LongType) AssignStmt(soot.jimple.AssignStmt) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) ArrayList(java.util.ArrayList) Unit(soot.Unit) IntType(soot.IntType) NopStmt(soot.jimple.NopStmt) AssignStmt(soot.jimple.AssignStmt) Stmt(soot.jimple.Stmt) RefType(soot.RefType) ArrayRef(soot.jimple.ArrayRef) InvokeExpr(soot.jimple.InvokeExpr) CastExpr(soot.jimple.CastExpr) InstanceFieldRef(soot.jimple.InstanceFieldRef) IntConstant(soot.jimple.IntConstant) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair) HashSet(java.util.HashSet) LongConstant(soot.jimple.LongConstant) FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) Local(soot.Local) NullConstant(soot.jimple.NullConstant) Trap(soot.Trap) LocalUses(soot.toolkits.scalar.LocalUses) LocalDefs(soot.toolkits.scalar.LocalDefs) ArrayDeque(java.util.ArrayDeque) UnknownType(soot.UnknownType) RefType(soot.RefType) Type(soot.Type) UnknownType(soot.UnknownType) IntType(soot.IntType) LongType(soot.LongType) NullType(soot.NullType) DivExpr(soot.jimple.DivExpr) NewArrayExpr(soot.jimple.NewArrayExpr) NopStmt(soot.jimple.NopStmt) RemExpr(soot.jimple.RemExpr) ValueBox(soot.ValueBox) Value(soot.Value) NewExpr(soot.jimple.NewExpr) Jimple(soot.jimple.Jimple) NullType(soot.NullType) BinopExpr(soot.jimple.BinopExpr)

Example 8 with NewMultiArrayExpr

use of soot.jimple.NewMultiArrayExpr in project soot by Sable.

the class NullnessAnalysis method handleRefTypeAssignment.

private void handleRefTypeAssignment(DefinitionStmt assignStmt, AnalysisInfo out) {
    Value left = assignStmt.getLeftOp();
    Value right = assignStmt.getRightOp();
    // unbox casted value
    if (right instanceof JCastExpr) {
        JCastExpr castExpr = (JCastExpr) right;
        right = castExpr.getOp();
    }
    // if we have a definition (assignment) statement to a ref-like type, handle it,
    if (isAlwaysNonNull(right) || right instanceof NewExpr || right instanceof NewArrayExpr || right instanceof NewMultiArrayExpr || right instanceof ThisRef || right instanceof StringConstant || right instanceof ClassConstant || right instanceof CaughtExceptionRef) {
        // if we assign new... or @this, the result is non-null
        out.put(left, NON_NULL);
    } else if (right == NullConstant.v()) {
        // if we assign null, well, it's null
        out.put(left, NULL);
    } else if (left instanceof Local && right instanceof Local) {
        out.put(left, out.get(right));
    } else if (left instanceof Local && right instanceof PhiExpr) {
        handlePhiExpr(out, left, (PhiExpr) right);
    } else {
        out.put(left, TOP);
    }
}
Also used : CaughtExceptionRef(soot.jimple.CaughtExceptionRef) NewArrayExpr(soot.jimple.NewArrayExpr) ThisRef(soot.jimple.ThisRef) PhiExpr(soot.shimple.PhiExpr) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) Value(soot.Value) NewExpr(soot.jimple.NewExpr) Local(soot.Local) JCastExpr(soot.jimple.internal.JCastExpr) StringConstant(soot.jimple.StringConstant) ClassConstant(soot.jimple.ClassConstant)

Example 9 with NewMultiArrayExpr

use of soot.jimple.NewMultiArrayExpr in project soot by Sable.

the class OnFlyCallGraphBuilder method getImplicitTargets.

private void getImplicitTargets(SootMethod source) {
    final SootClass scl = source.getDeclaringClass();
    if (source.isNative() || source.isPhantom())
        return;
    if (source.getSubSignature().indexOf("<init>") >= 0) {
        handleInit(source, scl);
    }
    Body b = source.retrieveActiveBody();
    for (Unit u : b.getUnits()) {
        final Stmt s = (Stmt) u;
        if (s.containsInvokeExpr()) {
            InvokeExpr ie = s.getInvokeExpr();
            SootMethodRef methodRef = ie.getMethodRef();
            switch(methodRef.declaringClass().getName()) {
                case "java.lang.reflect.Method":
                    if (methodRef.getSubSignature().getString().equals("java.lang.Object invoke(java.lang.Object,java.lang.Object[])"))
                        reflectionModel.methodInvoke(source, s);
                    break;
                case "java.lang.Class":
                    if (methodRef.getSubSignature().getString().equals("java.lang.Object newInstance()"))
                        reflectionModel.classNewInstance(source, s);
                    break;
                case "java.lang.reflect.Constructor":
                    if (methodRef.getSubSignature().getString().equals("java.lang.Object newInstance(java.lang.Object[]))"))
                        reflectionModel.contructorNewInstance(source, s);
                    break;
            }
            if (methodRef.getSubSignature() == sigForName) {
                reflectionModel.classForName(source, s);
            }
            if (ie instanceof StaticInvokeExpr) {
                SootClass cl = ie.getMethodRef().declaringClass();
                for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
                    addEdge(source, s, clinit, Kind.CLINIT);
                }
            }
        }
        if (s.containsFieldRef()) {
            FieldRef fr = s.getFieldRef();
            if (fr instanceof StaticFieldRef) {
                SootClass cl = fr.getFieldRef().declaringClass();
                for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
                    addEdge(source, s, clinit, Kind.CLINIT);
                }
            }
        }
        if (s instanceof AssignStmt) {
            Value rhs = ((AssignStmt) s).getRightOp();
            if (rhs instanceof NewExpr) {
                NewExpr r = (NewExpr) rhs;
                SootClass cl = r.getBaseType().getSootClass();
                for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
                    addEdge(source, s, clinit, Kind.CLINIT);
                }
            } else if (rhs instanceof NewArrayExpr || rhs instanceof NewMultiArrayExpr) {
                Type t = rhs.getType();
                if (t instanceof ArrayType)
                    t = ((ArrayType) t).baseType;
                if (t instanceof RefType) {
                    SootClass cl = ((RefType) t).getSootClass();
                    for (SootMethod clinit : EntryPoints.v().clinitsOf(cl)) {
                        addEdge(source, s, clinit, Kind.CLINIT);
                    }
                }
            }
        }
    }
}
Also used : FieldRef(soot.jimple.FieldRef) StaticFieldRef(soot.jimple.StaticFieldRef) SootMethodRef(soot.SootMethodRef) AssignStmt(soot.jimple.AssignStmt) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) SootClass(soot.SootClass) Unit(soot.Unit) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) Stmt(soot.jimple.Stmt) StaticFieldRef(soot.jimple.StaticFieldRef) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) ArrayType(soot.ArrayType) RefType(soot.RefType) RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) CharType(soot.CharType) LongType(soot.LongType) NullType(soot.NullType) RefLikeType(soot.RefLikeType) ArrayType(soot.ArrayType) PrimType(soot.PrimType) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) DynamicInvokeExpr(soot.jimple.DynamicInvokeExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) NewArrayExpr(soot.jimple.NewArrayExpr) Value(soot.Value) NewExpr(soot.jimple.NewExpr) SootMethod(soot.SootMethod) Body(soot.Body)

Example 10 with NewMultiArrayExpr

use of soot.jimple.NewMultiArrayExpr in project soot by Sable.

the class UseChecker method caseAssignStmt.

public void caseAssignStmt(AssignStmt stmt) {
    Value lhs = stmt.getLeftOp();
    Value rhs = stmt.getRightOp();
    Type tlhs = null;
    if (lhs instanceof Local)
        tlhs = this.tg.get((Local) lhs);
    else if (lhs instanceof ArrayRef) {
        ArrayRef aref = (ArrayRef) lhs;
        Local base = (Local) aref.getBase();
        // Try to force Type integrity. The left side must agree on the
        // element type of the right side array reference.
        ArrayType at = null;
        Type tgType = this.tg.get(base);
        if (tgType instanceof ArrayType)
            at = (ArrayType) tgType;
        else {
            // is java.lang.Object
            if (tgType == Scene.v().getObjectType() && rhs instanceof Local) {
                Type rhsType = this.tg.get((Local) rhs);
                if (rhsType instanceof PrimType) {
                    if (defs == null) {
                        defs = LocalDefs.Factory.newLocalDefs(jb);
                        uses = LocalUses.Factory.newLocalUses(jb, defs);
                    }
                    // Check the original type of the array from the alloc site
                    for (Unit defU : defs.getDefsOfAt(base, stmt)) {
                        if (defU instanceof AssignStmt) {
                            AssignStmt defUas = (AssignStmt) defU;
                            if (defUas.getRightOp() instanceof NewArrayExpr) {
                                at = (ArrayType) defUas.getRightOp().getType();
                                break;
                            }
                        }
                    }
                }
            }
            if (at == null)
                at = tgType.makeArrayType();
        }
        tlhs = ((ArrayType) at).getElementType();
        this.handleArrayRef(aref, stmt);
        aref.setBase((Local) this.uv.visit(aref.getBase(), at, stmt));
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
        stmt.setLeftOp(this.uv.visit(lhs, tlhs, stmt));
    } else if (lhs instanceof FieldRef) {
        tlhs = ((FieldRef) lhs).getFieldRef().type();
        if (lhs instanceof InstanceFieldRef)
            this.handleInstanceFieldRef((InstanceFieldRef) lhs, stmt);
    }
    // They may have been changed above
    lhs = stmt.getLeftOp();
    rhs = stmt.getRightOp();
    if (rhs instanceof Local)
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    else if (rhs instanceof ArrayRef) {
        ArrayRef aref = (ArrayRef) rhs;
        Local base = (Local) aref.getBase();
        // try to force Type integrity
        ArrayType at = null;
        Type et = null;
        if (this.tg.get(base) instanceof ArrayType)
            at = (ArrayType) this.tg.get(base);
        else {
            Type bt = this.tg.get(base);
            // For some fixed type T, we assume that we can fix the array to T[].
            if (bt instanceof RefType || bt instanceof NullType) {
                RefType rt = bt instanceof NullType ? null : (RefType) bt;
                if (rt == null || rt.getSootClass().getName().equals("java.lang.Object") || rt.getSootClass().getName().equals("java.io.Serializable") || rt.getSootClass().getName().equals("java.lang.Cloneable")) {
                    if (defs == null) {
                        defs = LocalDefs.Factory.newLocalDefs(jb);
                        uses = LocalUses.Factory.newLocalUses(jb, defs);
                    }
                    outer: for (UnitValueBoxPair usePair : uses.getUsesOf(stmt)) {
                        Stmt useStmt = (Stmt) usePair.getUnit();
                        // from the callee's signature=
                        if (useStmt.containsInvokeExpr()) {
                            for (int i = 0; i < useStmt.getInvokeExpr().getArgCount(); i++) {
                                if (useStmt.getInvokeExpr().getArg(i) == usePair.getValueBox().getValue()) {
                                    et = useStmt.getInvokeExpr().getMethod().getParameterType(i);
                                    at = et.makeArrayType();
                                    break outer;
                                }
                            }
                        } else // if the other value is a primitive.
                        if (useStmt instanceof IfStmt) {
                            IfStmt ifStmt = (IfStmt) useStmt;
                            if (ifStmt.getCondition() instanceof EqExpr) {
                                EqExpr expr = (EqExpr) ifStmt.getCondition();
                                final Value other;
                                if (expr.getOp1() == usePair.getValueBox().getValue())
                                    other = expr.getOp2();
                                else
                                    other = expr.getOp1();
                                Type newEt = getTargetType(other);
                                if (newEt != null)
                                    et = newEt;
                            }
                        } else if (useStmt instanceof AssignStmt) {
                            // For binary expressions, we can look for type information in the
                            // other operands
                            AssignStmt useAssignStmt = (AssignStmt) useStmt;
                            if (useAssignStmt.getRightOp() instanceof BinopExpr) {
                                BinopExpr binOp = (BinopExpr) useAssignStmt.getRightOp();
                                final Value other;
                                if (binOp.getOp1() == usePair.getValueBox().getValue())
                                    other = binOp.getOp2();
                                else
                                    other = binOp.getOp1();
                                Type newEt = getTargetType(other);
                                if (newEt != null)
                                    et = newEt;
                            }
                        } else if (useStmt instanceof ReturnStmt) {
                            et = jb.getMethod().getReturnType();
                        }
                    }
                }
            }
            if (at == null)
                at = et.makeArrayType();
        }
        Type trhs = ((ArrayType) at).getElementType();
        this.handleArrayRef(aref, stmt);
        aref.setBase((Local) this.uv.visit(aref.getBase(), at, stmt));
        stmt.setRightOp(this.uv.visit(rhs, trhs, stmt));
    } else if (rhs instanceof InstanceFieldRef) {
        this.handleInstanceFieldRef((InstanceFieldRef) rhs, stmt);
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    } else if (rhs instanceof BinopExpr)
        this.handleBinopExpr((BinopExpr) rhs, stmt, tlhs);
    else if (rhs instanceof InvokeExpr) {
        this.handleInvokeExpr((InvokeExpr) rhs, stmt);
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    } else if (rhs instanceof CastExpr)
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    else if (rhs instanceof InstanceOfExpr) {
        InstanceOfExpr ioe = (InstanceOfExpr) rhs;
        ioe.setOp(this.uv.visit(ioe.getOp(), RefType.v("java.lang.Object"), stmt));
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    } else if (rhs instanceof NewArrayExpr) {
        NewArrayExpr nae = (NewArrayExpr) rhs;
        nae.setSize(this.uv.visit(nae.getSize(), IntType.v(), stmt));
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    } else if (rhs instanceof NewMultiArrayExpr) {
        NewMultiArrayExpr nmae = (NewMultiArrayExpr) rhs;
        for (int i = 0; i < nmae.getSizeCount(); i++) nmae.setSize(i, this.uv.visit(nmae.getSize(i), IntType.v(), stmt));
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    } else if (rhs instanceof LengthExpr) {
        stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
    } else if (rhs instanceof NegExpr) {
        ((NegExpr) rhs).setOp(this.uv.visit(((NegExpr) rhs).getOp(), tlhs, stmt));
    } else if (rhs instanceof Constant)
        if (!(rhs instanceof NullConstant))
            stmt.setRightOp(this.uv.visit(rhs, tlhs, stmt));
}
Also used : AssignStmt(soot.jimple.AssignStmt) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) NullConstant(soot.jimple.NullConstant) Constant(soot.jimple.Constant) NegExpr(soot.jimple.NegExpr) Unit(soot.Unit) BreakpointStmt(soot.jimple.BreakpointStmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) InvokeStmt(soot.jimple.InvokeStmt) EnterMonitorStmt(soot.jimple.EnterMonitorStmt) NopStmt(soot.jimple.NopStmt) GotoStmt(soot.jimple.GotoStmt) AssignStmt(soot.jimple.AssignStmt) ThrowStmt(soot.jimple.ThrowStmt) IfStmt(soot.jimple.IfStmt) IdentityStmt(soot.jimple.IdentityStmt) TableSwitchStmt(soot.jimple.TableSwitchStmt) LookupSwitchStmt(soot.jimple.LookupSwitchStmt) ReturnStmt(soot.jimple.ReturnStmt) ExitMonitorStmt(soot.jimple.ExitMonitorStmt) Stmt(soot.jimple.Stmt) ArrayRef(soot.jimple.ArrayRef) ArrayType(soot.ArrayType) RefType(soot.RefType) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) InstanceFieldRef(soot.jimple.InstanceFieldRef) CastExpr(soot.jimple.CastExpr) PrimType(soot.PrimType) UnitValueBoxPair(soot.toolkits.scalar.UnitValueBoxPair) FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) LengthExpr(soot.jimple.LengthExpr) Local(soot.Local) NullConstant(soot.jimple.NullConstant) InstanceOfExpr(soot.jimple.InstanceOfExpr) RefType(soot.RefType) BooleanType(soot.BooleanType) Type(soot.Type) IntType(soot.IntType) NullType(soot.NullType) ArrayType(soot.ArrayType) IntegerType(soot.IntegerType) PrimType(soot.PrimType) IfStmt(soot.jimple.IfStmt) NewArrayExpr(soot.jimple.NewArrayExpr) EqExpr(soot.jimple.EqExpr) Value(soot.Value) NullType(soot.NullType) ReturnStmt(soot.jimple.ReturnStmt) BinopExpr(soot.jimple.BinopExpr)

Aggregations

NewMultiArrayExpr (soot.jimple.NewMultiArrayExpr)13 NewArrayExpr (soot.jimple.NewArrayExpr)10 Local (soot.Local)9 Value (soot.Value)9 InvokeExpr (soot.jimple.InvokeExpr)9 NullType (soot.NullType)8 NewExpr (soot.jimple.NewExpr)8 ArrayType (soot.ArrayType)7 Type (soot.Type)7 ArrayRef (soot.jimple.ArrayRef)7 BinopExpr (soot.jimple.BinopExpr)7 CastExpr (soot.jimple.CastExpr)7 InstanceFieldRef (soot.jimple.InstanceFieldRef)7 LengthExpr (soot.jimple.LengthExpr)6 IntType (soot.IntType)5 RefType (soot.RefType)5 AddExpr (soot.jimple.AddExpr)5 DivExpr (soot.jimple.DivExpr)5 EqExpr (soot.jimple.EqExpr)5 StaticFieldRef (soot.jimple.StaticFieldRef)5