Search in sources :

Example 66 with Method

use of org.jf.dexlib2.iface.Method in project soot by Sable.

the class DexPrinter method toMethods.

private Collection<Method> toMethods(SootClass clazz) {
    if (clazz.getMethods().isEmpty())
        return null;
    String classType = SootToDexUtils.getDexTypeDescriptor(clazz.getType());
    List<Method> methods = new ArrayList<Method>();
    for (SootMethod sm : clazz.getMethods()) {
        if (sm.isPhantom()) {
            // Do not print method bodies for inherited methods
            continue;
        }
        MethodImplementation impl = toMethodImplementation(sm);
        List<String> parameterNames = null;
        if (sm.hasTag("ParamNamesTag"))
            parameterNames = ((ParamNamesTag) sm.getTag("ParamNamesTag")).getNames();
        int paramIdx = 0;
        List<MethodParameter> parameters = null;
        if (sm.getParameterCount() > 0) {
            parameters = new ArrayList<MethodParameter>();
            for (Type tp : sm.getParameterTypes()) {
                String paramType = SootToDexUtils.getDexTypeDescriptor(tp);
                parameters.add(new ImmutableMethodParameter(paramType, buildMethodParameterAnnotations(sm, paramIdx), sm.isConcrete() && parameterNames != null ? parameterNames.get(paramIdx) : null));
                paramIdx++;
            }
        }
        String returnType = SootToDexUtils.getDexTypeDescriptor(sm.getReturnType());
        int accessFlags = SootToDexUtils.getDexAccessFlags(sm);
        ImmutableMethod meth = new ImmutableMethod(classType, sm.getName(), parameters, returnType, accessFlags, buildMethodAnnotations(sm), impl);
        methods.add(meth);
    }
    return methods;
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ArrayList(java.util.ArrayList) ImmutableMethodParameter(org.jf.dexlib2.immutable.ImmutableMethodParameter) Method(org.jf.dexlib2.iface.Method) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) SootMethod(soot.SootMethod) ParamNamesTag(soot.tagkit.ParamNamesTag) BooleanType(soot.BooleanType) Type(soot.Type) DexType(soot.dexpler.DexType) RefType(soot.RefType) ShortType(soot.ShortType) ByteType(soot.ByteType) IntType(soot.IntType) CharType(soot.CharType) SootMethod(soot.SootMethod) ImmutableMethodParameter(org.jf.dexlib2.immutable.ImmutableMethodParameter) MethodParameter(org.jf.dexlib2.iface.MethodParameter)

Example 67 with Method

use of org.jf.dexlib2.iface.Method in project soot by Sable.

the class DexAnnotation method handleAnnotationElement.

private ArrayList<AnnotationElem> handleAnnotationElement(AnnotationElement ae, List<? extends EncodedValue> evList) {
    ArrayList<AnnotationElem> aelemList = new ArrayList<AnnotationElem>();
    for (EncodedValue ev : evList) {
        int type = ev.getValueType();
        AnnotationElem elem = null;
        switch(type) {
            case // BYTE
            0x00:
                {
                    ByteEncodedValue v = (ByteEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'B', ae.getName());
                    break;
                }
            case // SHORT
            0x02:
                {
                    ShortEncodedValue v = (ShortEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'S', ae.getName());
                    break;
                }
            case // CHAR
            0x03:
                {
                    CharEncodedValue v = (CharEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'C', ae.getName());
                    break;
                }
            case // INT
            0x04:
                {
                    IntEncodedValue v = (IntEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'I', ae.getName());
                    break;
                }
            case // LONG
            0x06:
                {
                    LongEncodedValue v = (LongEncodedValue) ev;
                    elem = new AnnotationLongElem(v.getValue(), 'J', ae.getName());
                    break;
                }
            case // FLOAT
            0x10:
                {
                    FloatEncodedValue v = (FloatEncodedValue) ev;
                    elem = new AnnotationFloatElem(v.getValue(), 'F', ae.getName());
                    break;
                }
            case // DOUBLE
            0x11:
                {
                    DoubleEncodedValue v = (DoubleEncodedValue) ev;
                    elem = new AnnotationDoubleElem(v.getValue(), 'D', ae.getName());
                    break;
                }
            case // STRING
            0x17:
                {
                    StringEncodedValue v = (StringEncodedValue) ev;
                    elem = new AnnotationStringElem(v.getValue(), 's', ae.getName());
                    break;
                }
            case // TYPE
            0x18:
                {
                    TypeEncodedValue v = (TypeEncodedValue) ev;
                    elem = new AnnotationClassElem(v.getValue(), 'c', ae.getName());
                    break;
                }
            case // FIELD (Dalvik specific?)
            0x19:
                {
                    FieldEncodedValue v = (FieldEncodedValue) ev;
                    FieldReference fr = v.getValue();
                    String fieldSig = "";
                    fieldSig += DexType.toSootAT(fr.getDefiningClass()) + ": ";
                    fieldSig += DexType.toSootAT(fr.getType()) + " ";
                    fieldSig += fr.getName();
                    elem = new AnnotationStringElem(fieldSig, 'f', ae.getName());
                    break;
                }
            case // METHOD (Dalvik specific?)
            0x1a:
                {
                    MethodEncodedValue v = (MethodEncodedValue) ev;
                    MethodReference mr = v.getValue();
                    String className = DexType.toSootICAT(mr.getDefiningClass());
                    String returnType = DexType.toSootAT(mr.getReturnType());
                    String methodName = mr.getName();
                    String parameters = "";
                    for (CharSequence p : mr.getParameterTypes()) {
                        parameters += DexType.toSootAT(p.toString());
                    }
                    String mSig = className + " |" + methodName + " |" + parameters + " |" + returnType;
                    elem = new AnnotationStringElem(mSig, 'M', ae.getName());
                    break;
                }
            case // ENUM : Warning -> encoding Dalvik specific!
            0x1b:
                {
                    EnumEncodedValue v = (EnumEncodedValue) ev;
                    FieldReference fr = v.getValue();
                    elem = new AnnotationEnumElem(DexType.toSootAT(fr.getType()).toString(), fr.getName(), 'e', ae.getName());
                    break;
                }
            case // ARRAY
            0x1c:
                {
                    ArrayEncodedValue v = (ArrayEncodedValue) ev;
                    ArrayList<AnnotationElem> l = handleAnnotationElement(ae, v.getValue());
                    if (l != null)
                        elem = new AnnotationArrayElem(l, '[', ae.getName());
                    break;
                }
            case // ANNOTATION
            0x1d:
                {
                    AnnotationEncodedValue v = (AnnotationEncodedValue) ev;
                    AnnotationTag t = new AnnotationTag(DexType.toSootAT(v.getType()).toString());
                    for (AnnotationElement newElem : v.getElements()) {
                        List<EncodedValue> l = new ArrayList<EncodedValue>();
                        l.add(newElem.getValue());
                        List<AnnotationElem> aList = handleAnnotationElement(newElem, l);
                        if (aList != null)
                            for (AnnotationElem e : aList) t.addElem(e);
                    }
                    elem = new AnnotationAnnotationElem(t, '@', ae.getName());
                    break;
                }
            case // NULL (Dalvik specific?)
            0x1e:
                {
                    elem = new AnnotationStringElem(null, 'N', ae.getName());
                    break;
                }
            case // BOOLEAN
            0x1f:
                {
                    BooleanEncodedValue v = (BooleanEncodedValue) ev;
                    elem = new AnnotationBooleanElem(v.getValue(), 'Z', ae.getName());
                    break;
                }
            default:
                {
                    throw new RuntimeException("Unknown annotation element 0x" + Integer.toHexString(type));
                }
        }
        if (elem != null)
            aelemList.add(elem);
    }
    return aelemList;
}
Also used : AnnotationStringElem(soot.tagkit.AnnotationStringElem) AnnotationEncodedValue(org.jf.dexlib2.iface.value.AnnotationEncodedValue) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) FloatEncodedValue(org.jf.dexlib2.iface.value.FloatEncodedValue) ArrayList(java.util.ArrayList) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ShortEncodedValue(org.jf.dexlib2.iface.value.ShortEncodedValue) FieldEncodedValue(org.jf.dexlib2.iface.value.FieldEncodedValue) EnumEncodedValue(org.jf.dexlib2.iface.value.EnumEncodedValue) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) AnnotationFloatElem(soot.tagkit.AnnotationFloatElem) ByteEncodedValue(org.jf.dexlib2.iface.value.ByteEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) List(java.util.List) ArrayList(java.util.ArrayList) CharEncodedValue(org.jf.dexlib2.iface.value.CharEncodedValue) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) AnnotationBooleanElem(soot.tagkit.AnnotationBooleanElem) AnnotationLongElem(soot.tagkit.AnnotationLongElem) AnnotationArrayElem(soot.tagkit.AnnotationArrayElem) AnnotationEnumElem(soot.tagkit.AnnotationEnumElem) AnnotationEncodedValue(org.jf.dexlib2.iface.value.AnnotationEncodedValue) DoubleEncodedValue(org.jf.dexlib2.iface.value.DoubleEncodedValue) ShortEncodedValue(org.jf.dexlib2.iface.value.ShortEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) LongEncodedValue(org.jf.dexlib2.iface.value.LongEncodedValue) BooleanEncodedValue(org.jf.dexlib2.iface.value.BooleanEncodedValue) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) IntEncodedValue(org.jf.dexlib2.iface.value.IntEncodedValue) CharEncodedValue(org.jf.dexlib2.iface.value.CharEncodedValue) ByteEncodedValue(org.jf.dexlib2.iface.value.ByteEncodedValue) FieldEncodedValue(org.jf.dexlib2.iface.value.FieldEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) FloatEncodedValue(org.jf.dexlib2.iface.value.FloatEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) EnumEncodedValue(org.jf.dexlib2.iface.value.EnumEncodedValue) DoubleEncodedValue(org.jf.dexlib2.iface.value.DoubleEncodedValue) AnnotationDoubleElem(soot.tagkit.AnnotationDoubleElem) AnnotationIntElem(soot.tagkit.AnnotationIntElem) BooleanEncodedValue(org.jf.dexlib2.iface.value.BooleanEncodedValue) AnnotationClassElem(soot.tagkit.AnnotationClassElem) LongEncodedValue(org.jf.dexlib2.iface.value.LongEncodedValue) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) IntEncodedValue(org.jf.dexlib2.iface.value.IntEncodedValue)

Example 68 with Method

use of org.jf.dexlib2.iface.Method in project soot by Sable.

the class DexAnnotation method handleMethodAnnotation.

/**
 * Converts method and method parameters annotations from Dexlib to Jimple
 *
 * @param h
 * @param method
 */
public void handleMethodAnnotation(Host h, Method method) {
    Set<? extends Annotation> aSet = method.getAnnotations();
    if (!(aSet == null || aSet.isEmpty())) {
        List<Tag> tags = handleAnnotation(aSet, null);
        if (tags != null)
            for (Tag t : tags) if (t != null) {
                h.addTag(t);
            }
    }
    String[] parameterNames = null;
    int i = 0;
    for (MethodParameter p : method.getParameters()) {
        String name = p.getName();
        if (name != null) {
            parameterNames = new String[method.getParameters().size()];
            parameterNames[i] = name;
        }
        i++;
    }
    if (parameterNames != null) {
        h.addTag(new ParamNamesTag(parameterNames));
    }
    // Is there any parameter annotation?
    boolean doParam = false;
    List<? extends MethodParameter> parameters = method.getParameters();
    for (MethodParameter p : parameters) {
        if (p.getAnnotations().size() > 0) {
            doParam = true;
            break;
        }
    }
    if (doParam) {
        VisibilityParameterAnnotationTag tag = new VisibilityParameterAnnotationTag(parameters.size(), AnnotationConstants.RUNTIME_VISIBLE);
        for (MethodParameter p : parameters) {
            List<Tag> tags = handleAnnotation(p.getAnnotations(), null);
            // so that we keep the order intact.
            if (tags == null) {
                tag.addVisibilityAnnotation(null);
                continue;
            }
            VisibilityAnnotationTag paramVat = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_VISIBLE);
            tag.addVisibilityAnnotation(paramVat);
            for (Tag t : tags) {
                if (t == null)
                    continue;
                AnnotationTag vat = null;
                if (!(t instanceof VisibilityAnnotationTag)) {
                    if (t instanceof DeprecatedTag) {
                        vat = new AnnotationTag("Ljava/lang/Deprecated;");
                    } else if (t instanceof SignatureTag) {
                        SignatureTag sig = (SignatureTag) t;
                        ArrayList<AnnotationElem> sigElements = new ArrayList<AnnotationElem>();
                        for (String s : SootToDexUtils.splitSignature(sig.getSignature())) sigElements.add(new AnnotationStringElem(s, 's', "value"));
                        AnnotationElem elem = new AnnotationArrayElem(sigElements, '[', "value");
                        vat = new AnnotationTag("Ldalvik/annotation/Signature;", Collections.singleton(elem));
                    } else {
                        throw new RuntimeException("error: unhandled tag for parameter annotation in method " + h + " (" + t + ").");
                    }
                } else {
                    vat = ((VisibilityAnnotationTag) t).getAnnotations().get(0);
                }
                paramVat.addAnnotation(vat);
            }
        }
        if (tag.getVisibilityAnnotations().size() > 0)
            h.addTag(tag);
    }
}
Also used : DeprecatedTag(soot.tagkit.DeprecatedTag) AnnotationStringElem(soot.tagkit.AnnotationStringElem) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ArrayList(java.util.ArrayList) AnnotationArrayElem(soot.tagkit.AnnotationArrayElem) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ParamNamesTag(soot.tagkit.ParamNamesTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) SignatureTag(soot.tagkit.SignatureTag) 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) MethodParameter(org.jf.dexlib2.iface.MethodParameter) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem)

Example 69 with Method

use of org.jf.dexlib2.iface.Method in project soot by Sable.

the class DexBody method jimplify.

/**
 * Return the jimple equivalent of this body.
 *
 * @param m
 *            the SootMethod that contains this body
 */
public Body jimplify(Body b, SootMethod m) {
    final Jimple jimple = Jimple.v();
    final UnknownType unknownType = UnknownType.v();
    final NullConstant nullConstant = NullConstant.v();
    final Options options = Options.v();
    /*
		 * Timer t_whole_jimplification = new Timer(); Timer t_num = new
		 * Timer(); Timer t_null = new Timer();
		 * 
		 * t_whole_jimplification.start();
		 */
    jBody = (JimpleBody) b;
    deferredInstructions = new ArrayList<DeferableInstruction>();
    instructionsToRetype = new HashSet<RetypeableInstruction>();
    if (IDalvikTyper.ENABLE_DVKTYPER) {
        DalvikTyper.v().clear();
    }
    // process method parameters and generate Jimple locals from Dalvik
    // registers
    List<Local> paramLocals = new LinkedList<Local>();
    if (!isStatic) {
        int thisRegister = numRegisters - numParameterRegisters - 1;
        // generateLocal(UnknownType.v());
        Local thisLocal = jimple.newLocal("$u" + thisRegister, unknownType);
        jBody.getLocals().add(thisLocal);
        registerLocals[thisRegister] = thisLocal;
        JIdentityStmt idStmt = (JIdentityStmt) jimple.newIdentityStmt(thisLocal, jimple.newThisRef(declaringClassType));
        add(idStmt);
        paramLocals.add(thisLocal);
        if (IDalvikTyper.ENABLE_DVKTYPER) {
            DalvikTyper.v().setType(idStmt.leftBox, jBody.getMethod().getDeclaringClass().getType(), false);
        }
    }
    {
        // index of parameter type
        int i = 0;
        // index
        int parameterRegister = numRegisters - numParameterRegisters;
        // register
        for (Type t : parameterTypes) {
            // may
            Local gen = jimple.newLocal("$u" + parameterRegister, unknownType);
            // only
            // use
            // UnknownType
            // here
            // because
            // the
            // local
            // may
            // be
            // reused
            // with
            // a
            // different
            // type
            // later
            // (before
            // splitting)
            jBody.getLocals().add(gen);
            registerLocals[parameterRegister] = gen;
            JIdentityStmt idStmt = (JIdentityStmt) jimple.newIdentityStmt(gen, jimple.newParameterRef(t, i++));
            add(idStmt);
            paramLocals.add(gen);
            if (IDalvikTyper.ENABLE_DVKTYPER) {
                DalvikTyper.v().setType(idStmt.leftBox, t, false);
            }
            // used later in the Dalvik bytecode
            if (t instanceof LongType || t instanceof DoubleType) {
                parameterRegister++;
                // may
                Local g = jimple.newLocal("$u" + parameterRegister, unknownType);
                // only
                // use
                // UnknownType
                // here
                // because
                // the
                // local
                // may
                // be
                // reused
                // with
                // a
                // different
                // type
                // later
                // (before
                // splitting)
                jBody.getLocals().add(g);
                registerLocals[parameterRegister] = g;
            }
            parameterRegister++;
        }
    }
    for (int i = 0; i < (numRegisters - numParameterRegisters - (isStatic ? 0 : 1)); i++) {
        registerLocals[i] = jimple.newLocal("$u" + i, unknownType);
        jBody.getLocals().add(registerLocals[i]);
    }
    // add local to store intermediate results
    storeResultLocal = jimple.newLocal("$u-1", unknownType);
    jBody.getLocals().add(storeResultLocal);
    // process bytecode instructions
    final boolean isOdex = dexFile instanceof DexBackedDexFile ? ((DexBackedDexFile) dexFile).isOdexFile() : false;
    ClassPath cp = null;
    if (isOdex) {
        String[] sootClasspath = options.soot_classpath().split(File.pathSeparator);
        List<String> classpathList = new ArrayList<String>();
        for (String str : sootClasspath) classpathList.add(str);
        try {
            ClassPathResolver resolver = new ClassPathResolver(classpathList, classpathList, classpathList, dexFile);
            cp = new ClassPath(resolver.getResolvedClassProviders().toArray(new ClassProvider[0]));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    int prevLineNumber = -1;
    for (DexlibAbstractInstruction instruction : instructions) {
        if (isOdex && instruction instanceof OdexInstruction)
            ((OdexInstruction) instruction).deOdex(dexFile, method, cp);
        if (dangling != null) {
            dangling.finalize(this, instruction);
            dangling = null;
        }
        instruction.jimplify(this);
        if (instruction.getLineNumber() > 0)
            prevLineNumber = instruction.getLineNumber();
        else {
            instruction.setLineNumber(prevLineNumber);
        }
    }
    for (DeferableInstruction instruction : deferredInstructions) {
        instruction.deferredJimplify(this);
    }
    if (tries != null)
        addTraps();
    int prevLn = -1;
    final boolean keepLineNumber = options.keep_line_number();
    for (DexlibAbstractInstruction instruction : instructions) {
        Unit unit = instruction.getUnit();
        int lineNumber = unit.getJavaSourceStartLineNumber();
        if (keepLineNumber && lineNumber < 0) {
            if (prevLn >= 0) {
                unit.addTag(new LineNumberTag(prevLn));
                unit.addTag(new SourceLineNumberTag(prevLn));
            }
        } else {
            prevLn = lineNumber;
        }
    }
    // At this point Jimple code is generated
    // Cleaning...
    instructions = null;
    // registerLocals = null;
    // storeResultLocal = null;
    instructionAtAddress.clear();
    // localGenerator = null;
    deferredInstructions = null;
    // instructionsToRetype = null;
    dangling = null;
    tries = null;
    /*
		 * We eliminate dead code. Dead code has been shown to occur under the
		 * following circumstances.
		 *
		 * 0006ec: 0d00 |00a2: move-exception v0 ... 0006f2: 0d00 |00a5:
		 * move-exception v0 ... 0x0041 - 0x008a Ljava/lang/Throwable; -> 0x00a5
		 * <any> -> 0x00a2
		 * 
		 * Here there are two traps both over the same region. But the same
		 * always fires, hence rendering the code at a2 unreachable. Dead code
		 * yields problems during local splitting because locals within dead
		 * code will not be split. Hence we remove all dead code here.
		 */
    // Fix traps that do not catch exceptions
    DexTrapStackFixer.v().transform(jBody);
    // Sort out jump chains
    DexJumpChainShortener.v().transform(jBody);
    // Make sure that we don't have any overlapping uses due to returns
    DexReturnInliner.v().transform(jBody);
    // Shortcut: Reduce array initializations
    DexArrayInitReducer.v().transform(jBody);
    // split first to find undefined uses
    getLocalSplitter().transform(jBody);
    // Remove dead code and the corresponding locals before assigning types
    getUnreachableCodeEliminator().transform(jBody);
    DeadAssignmentEliminator.v().transform(jBody);
    UnusedLocalEliminator.v().transform(jBody);
    for (RetypeableInstruction i : instructionsToRetype) i.retype(jBody);
    if (IDalvikTyper.ENABLE_DVKTYPER) {
        DexReturnValuePropagator.v().transform(jBody);
        getCopyPopagator().transform(jBody);
        DexNullThrowTransformer.v().transform(jBody);
        DalvikTyper.v().typeUntypedConstrantInDiv(jBody);
        DeadAssignmentEliminator.v().transform(jBody);
        UnusedLocalEliminator.v().transform(jBody);
        DalvikTyper.v().assignType(jBody);
        // jBody.validate();
        jBody.validateUses();
        jBody.validateValueBoxes();
    // jBody.checkInit();
    // Validate.validateArrays(jBody);
    // jBody.checkTypes();
    // jBody.checkLocals();
    } else {
        // t_num.start();
        DexNumTransformer.v().transform(jBody);
        // t_num.end();
        DexReturnValuePropagator.v().transform(jBody);
        getCopyPopagator().transform(jBody);
        DexNullThrowTransformer.v().transform(jBody);
        // t_null.start();
        DexNullTransformer.v().transform(jBody);
        // t_null.end();
        DexIfTransformer.v().transform(jBody);
        DeadAssignmentEliminator.v().transform(jBody);
        UnusedLocalEliminator.v().transform(jBody);
        // DexRefsChecker.v().transform(jBody);
        DexNullArrayRefTransformer.v().transform(jBody);
    }
    if (IDalvikTyper.ENABLE_DVKTYPER) {
        for (Local l : jBody.getLocals()) {
            l.setType(unknownType);
        }
    }
    // Remove "instanceof" checks on the null constant
    DexNullInstanceofTransformer.v().transform(jBody);
    TypeAssigner.v().transform(jBody);
    final RefType objectType = RefType.v("java.lang.Object");
    if (IDalvikTyper.ENABLE_DVKTYPER) {
        for (Unit u : jBody.getUnits()) {
            if (u instanceof IfStmt) {
                ConditionExpr expr = (ConditionExpr) ((IfStmt) u).getCondition();
                if (((expr instanceof EqExpr) || (expr instanceof NeExpr))) {
                    Value op1 = expr.getOp1();
                    Value op2 = expr.getOp2();
                    if (op1 instanceof Constant && op2 instanceof Local) {
                        Local l = (Local) op2;
                        Type ltype = l.getType();
                        if (ltype instanceof PrimType)
                            continue;
                        if (// by default
                        !(op1 instanceof IntConstant))
                            // in Dalvik
                            continue;
                        IntConstant icst = (IntConstant) op1;
                        int val = icst.value;
                        if (val != 0)
                            continue;
                        expr.setOp1(nullConstant);
                    } else if (op1 instanceof Local && op2 instanceof Constant) {
                        Local l = (Local) op1;
                        Type ltype = l.getType();
                        if (ltype instanceof PrimType)
                            continue;
                        if (// by default
                        !(op2 instanceof IntConstant))
                            // in Dalvik
                            continue;
                        IntConstant icst = (IntConstant) op2;
                        int val = icst.value;
                        if (val != 0)
                            continue;
                        expr.setOp2(nullConstant);
                    } else if (op1 instanceof Local && op2 instanceof Local) {
                    // nothing to do
                    } else if (op1 instanceof Constant && op2 instanceof Constant) {
                        if (op1 instanceof NullConstant && op2 instanceof NumericConstant) {
                            IntConstant nc = (IntConstant) op2;
                            if (nc.value != 0)
                                throw new RuntimeException("expected value 0 for int constant. Got " + expr);
                            expr.setOp2(NullConstant.v());
                        } else if (op2 instanceof NullConstant && op1 instanceof NumericConstant) {
                            IntConstant nc = (IntConstant) op1;
                            if (nc.value != 0)
                                throw new RuntimeException("expected value 0 for int constant. Got " + expr);
                            expr.setOp1(nullConstant);
                        }
                    } else {
                        throw new RuntimeException("error: do not handle if: " + u);
                    }
                }
            }
        }
        // For null_type locals: replace their use by NullConstant()
        List<ValueBox> uses = jBody.getUseBoxes();
        // List<ValueBox> defs = jBody.getDefBoxes();
        List<ValueBox> toNullConstantify = new ArrayList<ValueBox>();
        List<Local> toRemove = new ArrayList<Local>();
        for (Local l : jBody.getLocals()) {
            if (l.getType() instanceof NullType) {
                toRemove.add(l);
                for (ValueBox vb : uses) {
                    Value v = vb.getValue();
                    if (v == l)
                        toNullConstantify.add(vb);
                }
            }
        }
        for (ValueBox vb : toNullConstantify) {
            System.out.println("replace valuebox '" + vb + " with null constant");
            vb.setValue(nullConstant);
        }
        for (Local l : toRemove) {
            System.out.println("removing null_type local " + l);
            l.setType(objectType);
        }
    }
    // We pack locals that are not used in overlapping regions. This may
    // again lead to unused locals which we have to remove.
    LocalPacker.v().transform(jBody);
    UnusedLocalEliminator.v().transform(jBody);
    LocalNameStandardizer.v().transform(jBody);
    // on the fly.
    if (options.wrong_staticness() == Options.wrong_staticness_fix) {
        FieldStaticnessCorrector.v().transform(jBody);
        MethodStaticnessCorrector.v().transform(jBody);
    }
    // Inline PackManager.v().getPack("jb").apply(jBody);
    // Keep only transformations that have not been done
    // at this point.
    TrapTightener.v().transform(jBody);
    TrapMinimizer.v().transform(jBody);
    // LocalSplitter.v().transform(jBody);
    Aggregator.v().transform(jBody);
    // UnusedLocalEliminator.v().transform(jBody);
    // TypeAssigner.v().transform(jBody);
    // LocalPacker.v().transform(jBody);
    // LocalNameStandardizer.v().transform(jBody);
    // Remove if (null == null) goto x else <madness>. We can only do this
    // after we have run the constant propagation as we might not be able
    // to statically decide the conditions earlier.
    ConditionalBranchFolder.v().transform(jBody);
    // Remove unnecessary typecasts
    ConstantCastEliminator.v().transform(jBody);
    IdentityCastEliminator.v().transform(jBody);
    // Remove unnecessary logic operations
    IdentityOperationEliminator.v().transform(jBody);
    // We need to run this transformer since the conditional branch folder
    // might have rendered some code unreachable (well, it was unreachable
    // before as well, but we didn't know).
    UnreachableCodeEliminator.v().transform(jBody);
    // Not sure whether we need this even though we do it earlier on as
    // the earlier pass does not have type information
    // CopyPropagator.v().transform(jBody);
    // we might have gotten new dead assignments and unused locals through
    // copy propagation and unreachable code elimination, so we have to do
    // this again
    DeadAssignmentEliminator.v().transform(jBody);
    UnusedLocalEliminator.v().transform(jBody);
    NopEliminator.v().transform(jBody);
    // Remove unnecessary chains of return statements
    DexReturnPacker.v().transform(jBody);
    for (Unit u : jBody.getUnits()) {
        if (u instanceof AssignStmt) {
            AssignStmt ass = (AssignStmt) u;
            if (ass.getRightOp() instanceof CastExpr) {
                CastExpr c = (CastExpr) ass.getRightOp();
                if (c.getType() instanceof NullType) {
                    ass.setRightOp(nullConstant);
                }
            }
        }
        if (u instanceof DefinitionStmt) {
            DefinitionStmt def = (DefinitionStmt) u;
            // we must manually fix the hierarchy
            if (def.getLeftOp() instanceof Local && def.getRightOp() instanceof CaughtExceptionRef) {
                Type t = def.getLeftOp().getType();
                if (t instanceof RefType) {
                    RefType rt = (RefType) t;
                    if (rt.getSootClass().isPhantom() && !rt.getSootClass().hasSuperclass() && !rt.getSootClass().getName().equals("java.lang.Throwable"))
                        rt.getSootClass().setSuperclass(Scene.v().getSootClass("java.lang.Throwable"));
                }
            }
        }
    }
    // 
    for (Local l : jBody.getLocals()) {
        Type t = l.getType();
        if (t instanceof NullType) {
            l.setType(objectType);
        }
    }
    return jBody;
}
Also used : Options(soot.options.Options) ClassPath(org.jf.dexlib2.analysis.ClassPath) LongType(soot.LongType) CaughtExceptionRef(soot.jimple.CaughtExceptionRef) NullConstant(soot.jimple.NullConstant) Constant(soot.jimple.Constant) IntConstant(soot.jimple.IntConstant) NumericConstant(soot.jimple.NumericConstant) AssignStmt(soot.jimple.AssignStmt) ArrayList(java.util.ArrayList) Unit(soot.Unit) RetypeableInstruction(soot.dexpler.instructions.RetypeableInstruction) RefType(soot.RefType) LineNumberTag(soot.tagkit.LineNumberTag) SourceLineNumberTag(soot.tagkit.SourceLineNumberTag) CastExpr(soot.jimple.CastExpr) PrimType(soot.PrimType) IntConstant(soot.jimple.IntConstant) DeferableInstruction(soot.dexpler.instructions.DeferableInstruction) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) ClassPathResolver(org.jf.dexlib2.analysis.ClassPathResolver) DexlibAbstractInstruction(soot.dexpler.instructions.DexlibAbstractInstruction) NeExpr(soot.jimple.NeExpr) JIdentityStmt(soot.jimple.internal.JIdentityStmt) NullConstant(soot.jimple.NullConstant) Local(soot.Local) IOException(java.io.IOException) LinkedList(java.util.LinkedList) UnknownType(soot.UnknownType) RefType(soot.RefType) Type(soot.Type) UnknownType(soot.UnknownType) DoubleType(soot.DoubleType) LongType(soot.LongType) NullType(soot.NullType) PrimType(soot.PrimType) IfStmt(soot.jimple.IfStmt) OdexInstruction(soot.dexpler.instructions.OdexInstruction) DoubleType(soot.DoubleType) EqExpr(soot.jimple.EqExpr) NumericConstant(soot.jimple.NumericConstant) ValueBox(soot.ValueBox) ConditionExpr(soot.jimple.ConditionExpr) Value(soot.Value) SourceLineNumberTag(soot.tagkit.SourceLineNumberTag) Jimple(soot.jimple.Jimple) NullType(soot.NullType) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 70 with Method

use of org.jf.dexlib2.iface.Method in project soot by Sable.

the class DexClassLoader method makeSootClass.

public Dependencies makeSootClass(SootClass sc, ClassDef defItem, DexFile dexFile) {
    String superClass = defItem.getSuperclass();
    Dependencies deps = new Dependencies();
    // source file
    String sourceFile = defItem.getSourceFile();
    if (sourceFile != null) {
        sc.addTag(new SourceFileTag(sourceFile));
    }
    // super class for hierarchy level
    if (superClass != null) {
        String superClassName = Util.dottedClassName(superClass);
        SootClass sootSuperClass = SootResolver.v().makeClassRef(superClassName);
        sc.setSuperclass(sootSuperClass);
        deps.typesToHierarchy.add(sootSuperClass.getType());
    }
    // access flags
    int accessFlags = defItem.getAccessFlags();
    sc.setModifiers(accessFlags);
    // Retrieve interface names
    if (defItem.getInterfaces() != null) {
        for (String interfaceName : defItem.getInterfaces()) {
            String interfaceClassName = Util.dottedClassName(interfaceName);
            if (sc.implementsInterface(interfaceClassName))
                continue;
            SootClass interfaceClass = SootResolver.v().makeClassRef(interfaceClassName);
            interfaceClass.setModifiers(interfaceClass.getModifiers() | Modifier.INTERFACE);
            sc.addInterface(interfaceClass);
            deps.typesToHierarchy.add(interfaceClass.getType());
        }
    }
    if (Options.v().oaat() && sc.resolvingLevel() <= SootClass.HIERARCHY) {
        return deps;
    }
    DexAnnotation da = new DexAnnotation(sc, deps);
    // get the fields of the class
    for (Field sf : defItem.getStaticFields()) {
        loadField(sc, da, sf);
    }
    for (Field f : defItem.getInstanceFields()) {
        loadField(sc, da, f);
    }
    // get the methods of the class
    DexMethod dexMethod = createDexMethodFactory(dexFile, sc);
    for (Method method : defItem.getDirectMethods()) {
        loadMethod(method, sc, da, dexMethod);
    }
    for (Method method : defItem.getVirtualMethods()) {
        loadMethod(method, sc, da, dexMethod);
    }
    da.handleClassAnnotation(defItem);
    // In contrast to Java, Dalvik associates the InnerClassAttribute
    // with the inner class, not the outer one. We need to copy the
    // tags over to correspond to the Soot semantics.
    InnerClassAttribute ica = (InnerClassAttribute) sc.getTag("InnerClassAttribute");
    if (ica != null) {
        Iterator<InnerClassTag> innerTagIt = ica.getSpecs().iterator();
        while (innerTagIt.hasNext()) {
            Tag t = innerTagIt.next();
            if (t instanceof InnerClassTag) {
                InnerClassTag ict = (InnerClassTag) t;
                // Get the outer class name
                String outer = DexInnerClassParser.getOuterClassNameFromTag(ict);
                if (outer == null) {
                    // If we don't have any clue what the outer class is, we
                    // just remove
                    // the reference entirely
                    innerTagIt.remove();
                    continue;
                }
                // we leave it as it is
                if (outer.equals(sc.getName()))
                    continue;
                // Check the inner class to make sure that this tag actually
                // refers to the current class as the inner class
                String inner = ict.getInnerClass().replaceAll("/", ".");
                if (!inner.equals(sc.getName())) {
                    innerTagIt.remove();
                    continue;
                }
                SootClass osc = SootResolver.v().makeClassRef(outer);
                if (osc == sc) {
                    if (!sc.hasOuterClass())
                        continue;
                    osc = sc.getOuterClass();
                } else
                    deps.typesToHierarchy.add(osc.getType());
                // Get the InnerClassAttribute of the outer class
                InnerClassAttribute icat = (InnerClassAttribute) osc.getTag("InnerClassAttribute");
                if (icat == null) {
                    icat = new InnerClassAttribute();
                    osc.addTag(icat);
                }
                // Transfer the tag from the inner class to the outer class
                InnerClassTag newt = new InnerClassTag(ict.getInnerClass(), ict.getOuterClass(), ict.getShortName(), ict.getAccessFlags());
                icat.add(newt);
                // Remove the tag from the inner class as inner classes do
                // not have these tags in the Java / Soot semantics. The
                // DexPrinter will copy it back if we do dex->dex.
                innerTagIt.remove();
                // within the PackManager in method handleInnerClasses().
                if (!sc.hasTag("InnerClassTag")) {
                    if (((InnerClassTag) t).getInnerClass().replaceAll("/", ".").equals(sc.toString())) {
                        sc.addTag(t);
                    }
                }
            }
        }
        // remove tag if empty
        if (ica.getSpecs().isEmpty()) {
            sc.getTags().remove(ica);
        }
    }
    return deps;
}
Also used : InnerClassAttribute(soot.tagkit.InnerClassAttribute) SourceFileTag(soot.tagkit.SourceFileTag) SootMethod(soot.SootMethod) Method(org.jf.dexlib2.iface.Method) SootClass(soot.SootClass) SootField(soot.SootField) Field(org.jf.dexlib2.iface.Field) InnerClassTag(soot.tagkit.InnerClassTag) Dependencies(soot.javaToJimple.IInitialResolver.Dependencies) Tag(soot.tagkit.Tag) SourceFileTag(soot.tagkit.SourceFileTag) InnerClassTag(soot.tagkit.InnerClassTag)

Aggregations

Method (org.jf.dexlib2.iface.Method)30 ClassDef (org.jf.dexlib2.iface.ClassDef)27 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)23 Test (org.junit.Test)21 Instruction (org.jf.dexlib2.iface.instruction.Instruction)19 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)18 DexFile (org.jf.dexlib2.iface.DexFile)16 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)15 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)13 ArrayList (java.util.ArrayList)12 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)12 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)10 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)10 HashSet (java.util.HashSet)9 Nonnull (javax.annotation.Nonnull)9 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)8 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)8 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)8 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)8 File (java.io.File)7