Search in sources :

Example 1 with MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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 MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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 MethodImplementation

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

the class SmalideaMethod method getImplementation.

@Nullable
@Override
public MethodImplementation getImplementation() {
    if (psiMethod instanceof SmaliMethod) {
        final SmaliMethod smaliMethod = (SmaliMethod) this.psiMethod;
        List<SmaliInstruction> instructions = smaliMethod.getInstructions();
        if (instructions.size() == 0) {
            return null;
        }
        // TODO: cache this?
        return new MethodImplementation() {

            @Override
            public int getRegisterCount() {
                return smaliMethod.getRegisterCount();
            }

            @Nonnull
            @Override
            public Iterable<? extends Instruction> getInstructions() {
                return Lists.transform(smaliMethod.getInstructions(), new Function<SmaliInstruction, Instruction>() {

                    @Override
                    public Instruction apply(SmaliInstruction smaliInstruction) {
                        return SmalideaInstruction.of(smaliInstruction);
                    }
                });
            }

            @Nonnull
            @Override
            public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() {
                return Lists.transform(smaliMethod.getCatchStatements(), new Function<SmaliCatchStatement, TryBlock<? extends ExceptionHandler>>() {

                    @Override
                    public TryBlock<? extends ExceptionHandler> apply(SmaliCatchStatement smaliCatchStatement) {
                        assert smaliCatchStatement != null;
                        return new SmalideaTryBlock(smaliCatchStatement);
                    }
                });
            }

            @Nonnull
            @Override
            public Iterable<? extends DebugItem> getDebugItems() {
                // TODO: implement this
                return ImmutableList.of();
            }
        };
    }
    return null;
}
Also used : SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) SmaliCatchStatement(org.jf.smalidea.psi.impl.SmaliCatchStatement) SmaliInstruction(org.jf.smalidea.psi.impl.SmaliInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) SmaliInstruction(org.jf.smalidea.psi.impl.SmaliInstruction) SmalideaInstruction(org.jf.smalidea.dexlib.instruction.SmalideaInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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 MethodImplementation

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

the class ClassDefinition method writeDirectMethods.

private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();
    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }
    for (Method method : directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');
        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);
        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Aggregations

MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)29 Test (org.junit.Test)18 Method (org.jf.dexlib2.iface.Method)17 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)13 ClassDef (org.jf.dexlib2.iface.ClassDef)12 Instruction (org.jf.dexlib2.iface.instruction.Instruction)12 DexFile (org.jf.dexlib2.iface.DexFile)11 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)11 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)9 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)8 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)8 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)8 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 HashSet (java.util.HashSet)6 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)6 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)6 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)6