Search in sources :

Example 21 with SootField

use of soot.SootField 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 22 with SootField

use of soot.SootField in project soot by Sable.

the class ArrayIndexLivenessAnalysis method getAllRelatedMaps.

private void getAllRelatedMaps(Body body) {
    Iterator unitIt = body.getUnits().iterator();
    while (unitIt.hasNext()) {
        Stmt stmt = (Stmt) unitIt.next();
        if (csin) {
            if (stmt instanceof DefinitionStmt) {
                Value rhs = ((DefinitionStmt) stmt).getRightOp();
                if (rhs instanceof BinopExpr) {
                    Value op1 = ((BinopExpr) rhs).getOp1();
                    Value op2 = ((BinopExpr) rhs).getOp2();
                    if (rhs instanceof AddExpr) {
                        // op1 + op2 --> a + b
                        if ((op1 instanceof Local) && (op2 instanceof Local)) {
                            HashSet<Value> refs = localToExpr.get(op1);
                            if (refs == null) {
                                refs = new HashSet<Value>();
                                localToExpr.put(op1, refs);
                            }
                            refs.add(rhs);
                            refs = localToExpr.get(op2);
                            if (refs == null) {
                                refs = new HashSet<Value>();
                                localToExpr.put(op2, refs);
                            }
                            refs.add(rhs);
                        }
                    } else // a * b, a * c, c * a
                    if (rhs instanceof MulExpr) {
                        HashSet<Value> refs = localToExpr.get(op1);
                        if (refs == null) {
                            refs = new HashSet<Value>();
                            localToExpr.put(op1, refs);
                        }
                        refs.add(rhs);
                        refs = localToExpr.get(op2);
                        if (refs == null) {
                            refs = new HashSet<Value>();
                            localToExpr.put(op2, refs);
                        }
                        refs.add(rhs);
                    } else if (rhs instanceof SubExpr) {
                        if (op2 instanceof Local) {
                            HashSet<Value> refs = localToExpr.get(op2);
                            if (refs == null) {
                                refs = new HashSet<Value>();
                                localToExpr.put(op2, refs);
                            }
                            refs.add(rhs);
                            if (op1 instanceof Local) {
                                refs = localToExpr.get(op1);
                                if (refs == null) {
                                    refs = new HashSet<Value>();
                                    localToExpr.put(op1, refs);
                                }
                                refs.add(rhs);
                            }
                        }
                    }
                }
            }
        }
        for (ValueBox vbox : stmt.getUseAndDefBoxes()) {
            Value v = vbox.getValue();
            if (fieldin) {
                if (v instanceof InstanceFieldRef) {
                    Value base = ((InstanceFieldRef) v).getBase();
                    SootField field = ((InstanceFieldRef) v).getField();
                    HashSet<Value> baseset = localToFieldRef.get(base);
                    if (baseset == null) {
                        baseset = new HashSet<Value>();
                        localToFieldRef.put(base, baseset);
                    }
                    baseset.add(v);
                    HashSet<Value> fieldset = fieldToFieldRef.get(field);
                    if (fieldset == null) {
                        fieldset = new HashSet<Value>();
                        fieldToFieldRef.put(field, fieldset);
                    }
                    fieldset.add(v);
                }
                if (v instanceof FieldRef)
                    allFieldRefs.add(v);
            }
            if (arrayin) {
            // a = ...   --> kill all a[x] nodes.
            // a[i] = .. --> kill all array references.
            // m(a)      --> kill all array references
            // i = ...   --> kill all array reference with index as i
            /*
                if (v instanceof ArrayRef)
                {
                    Value base = ((ArrayRef)v).getBase();
                    Value index = ((ArrayRef)v).getIndex();

                    HashSet refset = (HashSet)localToArrayRef.get(base);
                    if (refset == null)
                    {
                        refset = new HashSet();
                        localToArrayRef.put(base, refset);
                    }
                    refset.add(v);

                    if (index instanceof Local)
                    {
                        refset = (HashSet)localToArrayRef.get(index);
                        if (refset == null)
                        {
                            refset = new HashSet();
                            localToArrayRef.put(index, refset);
                        }

                        refset.add(v);
                    }           
                    allArrayRefs.add(v);
                }
                */
            }
        }
    }
}
Also used : MulExpr(soot.jimple.MulExpr) FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) StaticFieldRef(soot.jimple.StaticFieldRef) JSubExpr(soot.jimple.internal.JSubExpr) SubExpr(soot.jimple.SubExpr) Local(soot.Local) JAddExpr(soot.jimple.internal.JAddExpr) AddExpr(soot.jimple.AddExpr) IfStmt(soot.jimple.IfStmt) Stmt(soot.jimple.Stmt) DefinitionStmt(soot.jimple.DefinitionStmt) ValueBox(soot.ValueBox) Iterator(java.util.Iterator) Value(soot.Value) InstanceFieldRef(soot.jimple.InstanceFieldRef) SootField(soot.SootField) DefinitionStmt(soot.jimple.DefinitionStmt) BinopExpr(soot.jimple.BinopExpr) HashSet(java.util.HashSet)

Example 23 with SootField

use of soot.SootField in project soot by Sable.

the class XMLPrinter method printXMLTo.

private void printXMLTo(SootClass cl, PrintWriter out) {
    root = new XMLRoot();
    XMLNode xmlRootNode = null;
    XMLNode xmlHistoryNode = null;
    XMLNode xmlClassNode = null;
    XMLNode xmlTempNode = null;
    // Print XML class output
    {
        // add header nodes
        xmlRootNode = root.addElement("jil");
        // add history node
        // TODO: grab the software version and command line
        String cmdlineStr = "";
        for (String element : Main.v().cmdLineArgs) {
            cmdlineStr += element + " ";
        }
        String dateStr = new Date().toString();
        xmlHistoryNode = xmlRootNode.addChild("history");
        xmlHistoryNode.addAttribute("created", dateStr);
        xmlHistoryNode.addChild("soot", new String[] { "version", "command", "timestamp" }, new String[] { Main.v().versionString, cmdlineStr.trim(), dateStr });
        // add class root node
        xmlClassNode = xmlRootNode.addChild("class", new String[] { "name" }, new String[] { Scene.v().quotedNameOf(cl.getName()).toString() });
        if (cl.getPackageName().length() > 0)
            xmlClassNode.addAttribute("package", cl.getPackageName());
        if (cl.hasSuperclass())
            xmlClassNode.addAttribute("extends", Scene.v().quotedNameOf(cl.getSuperclass().getName()).toString());
        // add modifiers subnode
        xmlTempNode = xmlClassNode.addChild("modifiers");
        StringTokenizer st = new StringTokenizer(Modifier.toString(cl.getModifiers()));
        while (st.hasMoreTokens()) xmlTempNode.addChild("modifier", new String[] { "name" }, new String[] { st.nextToken() + "" });
        xmlTempNode.addAttribute("count", xmlTempNode.getNumberOfChildren() + "");
    }
    // Print interfaces
    {
        xmlTempNode = xmlClassNode.addChild("interfaces", "", new String[] { "count" }, new String[] { cl.getInterfaceCount() + "" });
        Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
        if (interfaceIt.hasNext()) {
            while (interfaceIt.hasNext()) xmlTempNode.addChild("implements", "", new String[] { "class" }, new String[] { Scene.v().quotedNameOf(interfaceIt.next().getName()).toString() });
        }
    }
    // Print fields
    {
        xmlTempNode = xmlClassNode.addChild("fields", "", new String[] { "count" }, new String[] { cl.getFieldCount() + "" });
        Iterator<SootField> fieldIt = cl.getFields().iterator();
        if (fieldIt.hasNext()) {
            int i = 0;
            while (fieldIt.hasNext()) {
                SootField f = fieldIt.next();
                if (f.isPhantom())
                    continue;
                String type = f.getType().toString();
                String name = f.getName().toString();
                // add the field node
                XMLNode xmlFieldNode = xmlTempNode.addChild("field", "", new String[] { "id", "name", "type" }, new String[] { (i++) + "", name, type });
                XMLNode xmlModifiersNode = xmlFieldNode.addChild("modifiers");
                StringTokenizer st = new StringTokenizer(Modifier.toString(f.getModifiers()));
                while (st.hasMoreTokens()) xmlModifiersNode.addChild("modifier", new String[] { "name" }, new String[] { st.nextToken() + "" });
                xmlModifiersNode.addAttribute("count", xmlModifiersNode.getNumberOfChildren() + "");
            }
        }
    }
    // Print methods
    {
        Iterator<SootMethod> methodIt = cl.methodIterator();
        setXMLNode(xmlClassNode.addChild("methods", new String[] { "count" }, new String[] { cl.getMethodCount() + "" }));
        while (methodIt.hasNext()) {
            SootMethod method = methodIt.next();
            if (method.isPhantom())
                continue;
            if (!Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())) {
                if (!method.hasActiveBody())
                    throw new RuntimeException("method " + method.getName() + " has no active body!");
                else
                    printTo(method.getActiveBody(), out);
            }
        }
    }
    out.println(toString());
}
Also used : StringTokenizer(java.util.StringTokenizer) Iterator(java.util.Iterator) SootMethod(soot.SootMethod) SootField(soot.SootField) Date(java.util.Date)

Example 24 with SootField

use of soot.SootField in project soot by Sable.

the class ClassResolver method createFieldDecl.

/**
 * Field Declaration Creation
 */
private void createFieldDecl(polyglot.ast.FieldDecl field) {
    // System.out.println("field decl: "+field);
    int modifiers = Util.getModifier(field.fieldInstance().flags());
    String name = field.fieldInstance().name();
    soot.Type sootType = Util.getSootType(field.fieldInstance().type());
    soot.SootField sootField = Scene.v().makeSootField(name, sootType, modifiers);
    sootClass.addField(sootField);
    if (field.fieldInstance().flags().isStatic()) {
        if (field.init() != null) {
            if (field.flags().isFinal() && (field.type().type().isPrimitive() || (field.type().type().toString().equals("java.lang.String"))) && field.fieldInstance().isConstant()) {
                // System.out.println("adding constantValtag: to field:
                // "+sootField);
                addConstValTag(field, sootField);
            } else {
                if (staticFieldInits == null) {
                    staticFieldInits = new ArrayList<FieldDecl>();
                }
                staticFieldInits.add(field);
            }
        }
    } else {
        if (field.init() != null) {
            if (fieldInits == null) {
                fieldInits = new ArrayList<FieldDecl>();
            }
            fieldInits.add(field);
        }
    }
    Util.addLnPosTags(sootField, field.position());
}
Also used : FieldDecl(polyglot.ast.FieldDecl) SootField(soot.SootField)

Example 25 with SootField

use of soot.SootField in project soot by Sable.

the class ClassResolver method createClassDecl.

/**
 * Class Declaration Creation
 */
private void createClassDecl(polyglot.ast.ClassDecl cDecl) {
    // add outer class tag if neccessary (if class is not top-level)
    if (!cDecl.type().isTopLevel()) {
        SootClass outerClass = ((soot.RefType) Util.getSootType(cDecl.type().outer())).getSootClass();
        if (InitialResolver.v().getInnerClassInfoMap() == null) {
            InitialResolver.v().setInnerClassInfoMap(new HashMap<SootClass, InnerClassInfo>());
        }
        InitialResolver.v().getInnerClassInfoMap().put(sootClass, new InnerClassInfo(outerClass, cDecl.name(), InnerClassInfo.NESTED));
        sootClass.setOuterClass(outerClass);
    }
    // modifiers
    polyglot.types.Flags flags = cDecl.flags();
    addModifiers(flags, cDecl);
    // super class
    if (cDecl.superClass() == null) {
        soot.SootClass superClass = soot.Scene.v().getSootClass("java.lang.Object");
        sootClass.setSuperclass(superClass);
    } else {
        sootClass.setSuperclass(((soot.RefType) Util.getSootType(cDecl.superClass().type())).getSootClass());
        if (((polyglot.types.ClassType) cDecl.superClass().type()).isNested()) {
            polyglot.types.ClassType superType = (polyglot.types.ClassType) cDecl.superClass().type();
            // add inner clas tag
            Util.addInnerClassTag(sootClass, sootClass.getName(), ((soot.RefType) Util.getSootType(superType.outer())).toString(), superType.name(), Util.getModifier(superType.flags()));
        }
    }
    // implements
    Iterator interfacesIt = cDecl.interfaces().iterator();
    while (interfacesIt.hasNext()) {
        polyglot.ast.TypeNode next = (polyglot.ast.TypeNode) interfacesIt.next();
        sootClass.addInterface(((soot.RefType) Util.getSootType(next.type())).getSootClass());
    }
    findReferences(cDecl);
    createClassBody(cDecl.body());
    // handle initialization of fields
    // static fields init in clinit
    // other fields init in init
    handleFieldInits();
    if ((staticFieldInits != null) || (staticInitializerBlocks != null)) {
        soot.SootMethod clinitMethod;
        if (!sootClass.declaresMethod("<clinit>", new ArrayList(), soot.VoidType.v())) {
            clinitMethod = Scene.v().makeSootMethod("<clinit>", new ArrayList(), soot.VoidType.v(), soot.Modifier.STATIC, new ArrayList<SootClass>());
            sootClass.addMethod(clinitMethod);
            PolyglotMethodSource mSource = new PolyglotMethodSource();
            mSource.setJBB(InitialResolver.v().getJBBFactory().createJimpleBodyBuilder());
            clinitMethod.setSource(mSource);
        } else {
            clinitMethod = sootClass.getMethod("<clinit>", new ArrayList(), soot.VoidType.v());
        }
        ((PolyglotMethodSource) clinitMethod.getSource()).setStaticFieldInits(staticFieldInits);
        ((PolyglotMethodSource) clinitMethod.getSource()).setStaticInitializerBlocks(staticInitializerBlocks);
    }
    // add final locals to local inner classes inits
    if (cDecl.type().isLocal()) {
        AnonLocalClassInfo info = InitialResolver.v().finalLocalInfo().get(new polyglot.util.IdentityKey(cDecl.type()));
        ArrayList<SootField> finalsList = addFinalLocals(cDecl.body(), info.finalLocalsAvail(), cDecl.type(), info);
        Iterator it = sootClass.getMethods().iterator();
        while (it.hasNext()) {
            soot.SootMethod meth = (soot.SootMethod) it.next();
            if (meth.getName().equals("<init>")) {
                ((PolyglotMethodSource) meth.getSource()).setFinalsList(finalsList);
            }
        }
        if (!info.inStaticMethod()) {
            polyglot.types.ClassType outerType = cDecl.type().outer();
            addOuterClassThisRefToInit(outerType);
            addOuterClassThisRefField(outerType);
        }
    } else // and out class field ref (only for non-static inner classes
    if (cDecl.type().isNested() && !cDecl.flags().isStatic()) {
        polyglot.types.ClassType outerType = cDecl.type().outer();
        addOuterClassThisRefToInit(outerType);
        addOuterClassThisRefField(outerType);
    }
    Util.addLnPosTags(sootClass, cDecl.position());
}
Also used : ArrayList(java.util.ArrayList) IdentityKey(polyglot.util.IdentityKey) SootClass(soot.SootClass) SootClass(soot.SootClass) RefType(soot.RefType) SootMethod(soot.SootMethod) Iterator(java.util.Iterator) SootMethod(soot.SootMethod) SootField(soot.SootField)

Aggregations

SootField (soot.SootField)73 SootMethod (soot.SootMethod)29 SootClass (soot.SootClass)26 RefType (soot.RefType)22 ArrayList (java.util.ArrayList)19 Value (soot.Value)17 Iterator (java.util.Iterator)15 Local (soot.Local)14 Type (soot.Type)14 Unit (soot.Unit)13 FieldRef (soot.jimple.FieldRef)12 BooleanType (soot.BooleanType)10 PrimType (soot.PrimType)10 VoidType (soot.VoidType)10 Stmt (soot.jimple.Stmt)10 ByteType (soot.ByteType)8 CharType (soot.CharType)8 DoubleType (soot.DoubleType)8 FloatType (soot.FloatType)8 IntType (soot.IntType)8