Search in sources :

Example 1 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class PatchMethodTool method modifyMethodAndFix.

private static MethodImplementation modifyMethodAndFix(@Nonnull MethodImplementation implementation, Method method) {
    MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
    System.out.println(mutableImplementation.getRegisterCount());
    List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
    mutableImplementation.addInstruction(0, new BuilderInstruction21c(Opcode.CONST_STRING, 0, new ImmutableStringReference("AndFix:" + method.getDefiningClass().replace("/", "."))));
    mutableImplementation.addInstruction(1, new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, 0, 0, 0, 0, 0, new ImmutableMethodReference("Landroid/util/Log;", "e", Lists.newArrayList("Ljava/lang/String;", "Ljava/lang/String;"), "I")));
    return mutableImplementation;
}
Also used : ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) BuilderInstruction(org.jf.dexlib2.builder.BuilderInstruction) BuilderInstruction35c(org.jf.dexlib2.builder.instruction.BuilderInstruction35c) BuilderInstruction21c(org.jf.dexlib2.builder.instruction.BuilderInstruction21c) MutableMethodImplementation(org.jf.dexlib2.builder.MutableMethodImplementation) ImmutableStringReference(org.jf.dexlib2.immutable.reference.ImmutableStringReference)

Example 2 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class PatchMethodTool method modifyMethodTpatch.

private static MethodImplementation modifyMethodTpatch(@Nonnull MethodImplementation implementation, Method method) {
    MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
    System.out.println(mutableImplementation.getRegisterCount());
    List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
    boolean isModified = false;
    for (int i = 0; i < instructions.size(); i++) {
        isModified = false;
        if (instructions.get(i).getOpcode() == Opcode.INVOKE_DIRECT) {
            if (!isModified) {
                mutableImplementation.addInstruction(i++, new BuilderInstruction21c(Opcode.CONST_STRING, 0, new ImmutableStringReference("tpatch:" + method.getDefiningClass().replace("/", "."))));
                mutableImplementation.addInstruction(i++, new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, 0, 0, 0, 0, 0, new ImmutableMethodReference("Landroid/util/Log;", "e", Lists.newArrayList("Ljava/lang/String;", "Ljava/lang/String;"), "I")));
                isModified = true;
                break;
            }
        }
    //            mutableImplementation.addInstruction(instructions.get(i));
    }
    return mutableImplementation;
}
Also used : ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) BuilderInstruction(org.jf.dexlib2.builder.BuilderInstruction) BuilderInstruction35c(org.jf.dexlib2.builder.instruction.BuilderInstruction35c) BuilderInstruction21c(org.jf.dexlib2.builder.instruction.BuilderInstruction21c) MutableMethodImplementation(org.jf.dexlib2.builder.MutableMethodImplementation) ImmutableStringReference(org.jf.dexlib2.immutable.reference.ImmutableStringReference)

Example 3 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class MethodDefinition method writeParameters.

private static void writeParameters(IndentingWriter writer, Method method, List<? extends MethodParameter> parameters, baksmaliOptions options) throws IOException {
    boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
    int registerNumber = isStatic ? 0 : 1;
    for (MethodParameter parameter : parameters) {
        String parameterType = parameter.getType();
        String parameterName = parameter.getName();
        Collection<? extends Annotation> annotations = parameter.getAnnotations();
        if (parameterName != null || annotations.size() != 0) {
            writer.write(".param p");
            writer.printSignedIntAsDec(registerNumber);
            if (parameterName != null && options.outputDebugInfo) {
                writer.write(", ");
                ReferenceFormatter.writeStringReference(writer, parameterName);
            }
            writer.write("    # ");
            writer.write(parameterType);
            writer.write("\n");
            if (annotations.size() > 0) {
                writer.indent(4);
                String containingClass = null;
                if (options.useImplicitReferences) {
                    containingClass = method.getDefiningClass();
                }
                AnnotationFormatter.writeTo(writer, annotations, containingClass);
                writer.deindent(4);
                writer.write(".end param\n");
            }
        }
        registerNumber++;
        if (TypeUtils.isWideType(parameterType)) {
            registerNumber++;
        }
    }
}
Also used : MethodParameter(org.jf.dexlib2.iface.MethodParameter)

Example 4 with Method

use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.

the class SmalideaMethodTest method testSparseSwitch.

public void testSparseSwitch() {
    String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_sparse-switch()V\n" + "    .registers 1\n" + "    .annotation runtime Lorg/junit/Test;\n" + "    .end annotation\n" + "\n" + "    const v0, 13\n" + "\n" + ":switch\n" + "    sparse-switch v0, :SparseSwitch\n" + "\n" + ":Label10\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label20\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label15\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label13\n" + "    return-void\n" + "\n" + ":Label99\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":SparseSwitch\n" + "    .sparse-switch\n" + "        10 -> :Label10\n" + "        13 -> :Label13\n" + "        15 -> :Label15\n" + "        20 -> :Label20\n" + "        99 -> :Label99\n" + "    .end sparse-switch\n" + ".end method";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
    SmaliClass smaliClass = file.getPsiClass();
    SmaliMethod smaliMethod = smaliClass.getMethods()[0];
    SmalideaMethod method = new SmalideaMethod(smaliMethod);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    SparseSwitchPayload sparseSwitchPayload = (SparseSwitchPayload) instructions.get(11);
    List<? extends SwitchElement> switchElements = sparseSwitchPayload.getSwitchElements();
    Assert.assertEquals(5, switchElements.size());
    checkSwitchElement(switchElements.get(0), 10, 6);
    checkSwitchElement(switchElements.get(1), 13, 30);
    checkSwitchElement(switchElements.get(2), 15, 22);
    checkSwitchElement(switchElements.get(3), 20, 14);
    checkSwitchElement(switchElements.get(4), 99, 32);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Example 5 with Method

use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.

the class SmaliInstruction method getAnalyzedInstructionFromMethod.

@Nullable
private AnalyzedInstruction getAnalyzedInstructionFromMethod() {
    SmaliMethod method = getParentMethod();
    MethodAnalyzer analyzer = method.getMethodAnalyzer();
    if (analyzer == null) {
        return null;
    }
    int thisOffset = this.getOffset() / 2;
    int codeOffset = 0;
    for (AnalyzedInstruction instruction : analyzer.getAnalyzedInstructions()) {
        if (codeOffset == thisOffset) {
            return instruction;
        }
        assert codeOffset < thisOffset;
        codeOffset += instruction.getOriginalInstruction().getCodeUnits();
    }
    assert false;
    return null;
}
Also used : MethodAnalyzer(org.jf.dexlib2.analysis.MethodAnalyzer) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction) Nullable(org.jetbrains.annotations.Nullable)

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