Search in sources :

Example 31 with ArrayType

use of soot.ArrayType in project soot by Sable.

the class Renamer method arraysGetTypeArray.

/*
	 * if there is an array int[] x. then if no other heuristic matches give it the name intArray 
	 */
private void arraysGetTypeArray() {
    Iterator<Local> it = heuristics.getLocalsIterator();
    while (it.hasNext()) {
        Local tempLocal = it.next();
        if (alreadyChanged(tempLocal)) {
            continue;
        }
        debug("arraysGetTypeArray", "checking " + tempLocal);
        Type type = tempLocal.getType();
        if (type instanceof ArrayType) {
            debug("arraysGetTypeArray", "Local:" + tempLocal + " is an Array Type: " + type.toString());
            String tempClassName = type.toString();
            // remember that a toString of an array gives you the square brackets
            if (tempClassName.indexOf('[') >= 0)
                tempClassName = tempClassName.substring(0, tempClassName.indexOf('['));
            // debug("arraysGetTypeArray","type of object is"+tempClassName);
            if (tempClassName.indexOf('.') != -1) {
                // contains a dot have to remove that
                tempClassName = tempClassName.substring(tempClassName.lastIndexOf('.') + 1);
            }
            String newName = tempClassName.toLowerCase();
            newName = newName + "Array";
            int count = 0;
            newName += count;
            count++;
            while (!isUniqueName(newName)) {
                newName = newName.substring(0, newName.length() - 1) + count;
                count++;
            }
            setName(tempLocal, newName);
        }
    }
}
Also used : ArrayType(soot.ArrayType) Type(soot.Type) RefLikeType(soot.RefLikeType) ArrayType(soot.ArrayType) Local(soot.Local)

Example 32 with ArrayType

use of soot.ArrayType in project soot by Sable.

the class DexAnnotation method handleClassAnnotation.

/**
 * Converts Class annotations from Dexlib to Jimple.
 *
 * @param h
 * @param classDef
 */
// .annotation "Ldalvik/annotation/AnnotationDefault;"
// .annotation "Ldalvik/annotation/EnclosingClass;"
// .annotation "Ldalvik/annotation/EnclosingMethod;"
// .annotation "Ldalvik/annotation/InnerClass;"
// .annotation "Ldalvik/annotation/MemberClasses;"
// .annotation "Ldalvik/annotation/Signature;"
// .annotation "Ldalvik/annotation/Throws;"
public void handleClassAnnotation(ClassDef classDef) {
    Set<? extends Annotation> aSet = classDef.getAnnotations();
    if (aSet == null || aSet.isEmpty())
        return;
    List<Tag> tags = handleAnnotation(aSet, classDef.getType());
    if (tags == null)
        return;
    InnerClassAttribute ica = null;
    for (Tag t : tags) if (t != null) {
        if (t instanceof InnerClassTag) {
            if (ica == null) {
                // Do we already have an InnerClassAttribute?
                ica = (InnerClassAttribute) clazz.getTag("InnerClassAttribute");
                // If not, create one
                if (ica == null) {
                    ica = new InnerClassAttribute();
                    clazz.addTag(ica);
                }
            }
            ica.add((InnerClassTag) t);
        } else if (t instanceof VisibilityAnnotationTag) {
            // If a dalvik/annotation/AnnotationDefault tag is present
            // in a class, its AnnotationElements must be propagated
            // to methods through the creation of new
            // AnnotationDefaultTag.
            VisibilityAnnotationTag vt = (VisibilityAnnotationTag) t;
            for (AnnotationTag a : vt.getAnnotations()) {
                if (a.getType().equals("Ldalvik/annotation/AnnotationDefault;")) {
                    for (AnnotationElem ae : a.getElems()) {
                        if (ae instanceof AnnotationAnnotationElem) {
                            AnnotationAnnotationElem aae = (AnnotationAnnotationElem) ae;
                            AnnotationTag at = aae.getValue();
                            // extract default elements
                            Map<String, AnnotationElem> defaults = new HashMap<String, AnnotationElem>();
                            for (AnnotationElem aelem : at.getElems()) {
                                defaults.put(aelem.getName(), aelem);
                            }
                            // and add tags on methods
                            for (SootMethod sm : clazz.getMethods()) {
                                String methodName = sm.getName();
                                if (defaults.containsKey(methodName)) {
                                    AnnotationElem e = defaults.get(methodName);
                                    // Okay, the name is the same, but
                                    // is it actually the same type?
                                    Type annotationType = getSootType(e);
                                    boolean isCorrectType = false;
                                    if (annotationType == null) {
                                        // we do not know the type of
                                        // the annotation, so we guess
                                        // it's the correct type.
                                        isCorrectType = true;
                                    } else {
                                        if (annotationType.equals(sm.getReturnType())) {
                                            isCorrectType = true;
                                        } else if (annotationType.equals(ARRAY_TYPE)) {
                                            if (sm.getReturnType() instanceof ArrayType)
                                                isCorrectType = true;
                                        }
                                    }
                                    if (isCorrectType && sm.getParameterCount() == 0) {
                                        e.setName("default");
                                        AnnotationDefaultTag d = new AnnotationDefaultTag(e);
                                        sm.addTag(d);
                                        // In case there is more than
                                        // one matching method, we only
                                        // use the first one
                                        defaults.remove(sm.getName());
                                    }
                                }
                            }
                            for (Entry<String, AnnotationElem> leftOverEntry : defaults.entrySet()) {
                                // We were not able to find a matching
                                // method for the tag, because the
                                // return signature
                                // does not match
                                SootMethod found = clazz.getMethodByNameUnsafe(leftOverEntry.getKey());
                                AnnotationElem element = leftOverEntry.getValue();
                                if (found != null) {
                                    element.setName("default");
                                    AnnotationDefaultTag d = new AnnotationDefaultTag(element);
                                    found.addTag(d);
                                }
                            }
                        }
                    }
                }
            }
            if (!(vt.getVisibility() == AnnotationConstants.RUNTIME_INVISIBLE))
                clazz.addTag(vt);
        } else {
            clazz.addTag(t);
        }
    }
}
Also used : InnerClassAttribute(soot.tagkit.InnerClassAttribute) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) HashMap(java.util.HashMap) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) ArrayType(soot.ArrayType) InnerClassTag(soot.tagkit.InnerClassTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) RefType(soot.RefType) Type(soot.Type) ArrayType(soot.ArrayType) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) SootMethod(soot.SootMethod) Tag(soot.tagkit.Tag) ParamNamesTag(soot.tagkit.ParamNamesTag) DeprecatedTag(soot.tagkit.DeprecatedTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) SignatureTag(soot.tagkit.SignatureTag) AnnotationTag(soot.tagkit.AnnotationTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem)

Example 33 with ArrayType

use of soot.ArrayType in project soot by Sable.

the class Util method emptyBody.

/**
 * Remove all statements except from IdentityStatements for parameters.
 * Return default value (null or zero or nothing depending on the return
 * type).
 *
 * @param jBody
 */
public static void emptyBody(Body jBody) {
    // identity statements
    List<Unit> idStmts = new ArrayList<Unit>();
    List<Local> idLocals = new ArrayList<Local>();
    for (Unit u : jBody.getUnits()) {
        if (u instanceof IdentityStmt) {
            IdentityStmt i = (IdentityStmt) u;
            if (i.getRightOp() instanceof ParameterRef || i.getRightOp() instanceof ThisRef) {
                idStmts.add(u);
                idLocals.add((Local) i.getLeftOp());
            }
        }
    }
    jBody.getUnits().clear();
    jBody.getLocals().clear();
    jBody.getTraps().clear();
    final LocalGenerator lg = new LocalGenerator(jBody);
    for (Unit u : idStmts) jBody.getUnits().add(u);
    for (Local l : idLocals) jBody.getLocals().add(l);
    Type rType = jBody.getMethod().getReturnType();
    jBody.getUnits().add(Jimple.v().newNopStmt());
    if (rType instanceof VoidType) {
        jBody.getUnits().add(Jimple.v().newReturnVoidStmt());
    } else {
        Type t = jBody.getMethod().getReturnType();
        Local l = lg.generateLocal(t);
        AssignStmt ass = null;
        if (t instanceof RefType || t instanceof ArrayType) {
            ass = Jimple.v().newAssignStmt(l, NullConstant.v());
        } else if (t instanceof LongType) {
            ass = Jimple.v().newAssignStmt(l, LongConstant.v(0));
        } else if (t instanceof FloatType) {
            ass = Jimple.v().newAssignStmt(l, FloatConstant.v(0.0f));
        } else if (t instanceof IntType) {
            ass = Jimple.v().newAssignStmt(l, IntConstant.v(0));
        } else if (t instanceof DoubleType) {
            ass = Jimple.v().newAssignStmt(l, DoubleConstant.v(0));
        } else if (t instanceof BooleanType || t instanceof ByteType || t instanceof CharType || t instanceof ShortType) {
            ass = Jimple.v().newAssignStmt(l, IntConstant.v(0));
        } else {
            throw new RuntimeException("error: return type unknown: " + t + " class: " + t.getClass());
        }
        jBody.getUnits().add(ass);
        jBody.getUnits().add(Jimple.v().newReturnStmt(l));
    }
}
Also used : VoidType(soot.VoidType) LocalGenerator(soot.javaToJimple.LocalGenerator) LongType(soot.LongType) AssignStmt(soot.jimple.AssignStmt) ShortType(soot.ShortType) ArrayList(java.util.ArrayList) BooleanType(soot.BooleanType) Local(soot.Local) ByteType(soot.ByteType) Unit(soot.Unit) FloatType(soot.FloatType) IntType(soot.IntType) RefType(soot.RefType) ArrayType(soot.ArrayType) RefType(soot.RefType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) ArrayType(soot.ArrayType) Type(soot.Type) VoidType(soot.VoidType) ParameterRef(soot.jimple.ParameterRef) ThisRef(soot.jimple.ThisRef) DoubleType(soot.DoubleType) CharType(soot.CharType) IdentityStmt(soot.jimple.IdentityStmt)

Example 34 with ArrayType

use of soot.ArrayType in project soot by Sable.

the class AputInstruction method getTargetType.

@Override
protected Type getTargetType(DexBody body) {
    Instruction23x aPutInstr = (Instruction23x) instruction;
    Type t = body.getRegisterLocal(aPutInstr.getRegisterB()).getType();
    if (t instanceof ArrayType)
        return ((ArrayType) t).getElementType();
    else
        return UnknownType.v();
}
Also used : ArrayType(soot.ArrayType) UnknownType(soot.UnknownType) ArrayType(soot.ArrayType) IntType(soot.IntType) Type(soot.Type) Instruction23x(org.jf.dexlib2.iface.instruction.formats.Instruction23x)

Example 35 with ArrayType

use of soot.ArrayType in project soot by Sable.

the class FilledNewArrayRangeInstruction method jimplify.

@Override
public void jimplify(DexBody body) {
    if (!(instruction instanceof Instruction3rc))
        throw new IllegalArgumentException("Expected Instruction3rc but got: " + instruction.getClass());
    Instruction3rc filledNewArrayInstr = (Instruction3rc) instruction;
    // NopStmt nopStmtBeginning = Jimple.v().newNopStmt();
    // body.add(nopStmtBeginning);
    int usedRegister = filledNewArrayInstr.getRegisterCount();
    Type t = DexType.toSoot((TypeReference) filledNewArrayInstr.getReference());
    // NewArrayExpr needs the ElementType as it increases the array dimension by 1
    Type arrayType = ((ArrayType) t).getElementType();
    NewArrayExpr arrayExpr = Jimple.v().newNewArrayExpr(arrayType, IntConstant.v(usedRegister));
    Local arrayLocal = body.getStoreResultLocal();
    AssignStmt assignStmt = Jimple.v().newAssignStmt(arrayLocal, arrayExpr);
    body.add(assignStmt);
    for (int i = 0; i < usedRegister; i++) {
        ArrayRef arrayRef = Jimple.v().newArrayRef(arrayLocal, IntConstant.v(i));
        AssignStmt assign = Jimple.v().newAssignStmt(arrayRef, body.getRegisterLocal(i + filledNewArrayInstr.getStartRegister()));
        addTags(assign);
        body.add(assign);
    }
    // NopStmt nopStmtEnd = Jimple.v().newNopStmt();
    // body.add(nopStmtEnd);
    // defineBlock(nopStmtBeginning,nopStmtEnd);
    setUnit(assignStmt);
    if (IDalvikTyper.ENABLE_DVKTYPER) {
        // Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assignStmt);
        DalvikTyper.v().setType(assignStmt.getLeftOpBox(), arrayExpr.getType(), false);
    // DalvikTyper.v().addConstraint(assignStmt.getLeftOpBox(), assignStmt.getRightOpBox());
    }
}
Also used : ArrayType(soot.ArrayType) ArrayRef(soot.jimple.ArrayRef) Instruction3rc(org.jf.dexlib2.iface.instruction.formats.Instruction3rc) ArrayType(soot.ArrayType) Type(soot.Type) DexType(soot.dexpler.DexType) NewArrayExpr(soot.jimple.NewArrayExpr) AssignStmt(soot.jimple.AssignStmt) Local(soot.Local)

Aggregations

ArrayType (soot.ArrayType)44 Type (soot.Type)34 RefType (soot.RefType)26 Local (soot.Local)19 Value (soot.Value)18 IntType (soot.IntType)17 BooleanType (soot.BooleanType)14 NewArrayExpr (soot.jimple.NewArrayExpr)13 ByteType (soot.ByteType)12 FloatType (soot.FloatType)12 NullType (soot.NullType)12 ShortType (soot.ShortType)12 ArrayRef (soot.jimple.ArrayRef)12 CharType (soot.CharType)11 DoubleType (soot.DoubleType)11 LongType (soot.LongType)11 AssignStmt (soot.jimple.AssignStmt)11 SootClass (soot.SootClass)10 InvokeExpr (soot.jimple.InvokeExpr)10 PrimType (soot.PrimType)9