Search in sources :

Example 26 with Method

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

the class MethodAnalyzerTest method testInstanceOfNarrowingEqz_art.

@Test
public void testInstanceOfNarrowingEqz_art() throws IOException {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(2);
    builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
    builder.addInstruction(new BuilderInstruction21t(Opcode.IF_EQZ, 0, builder.getLabel("not_instance_of")));
    builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    builder.addLabel("not_instance_of");
    builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    MethodImplementation methodImplementation = builder.getMethodImplementation();
    Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, methodImplementation);
    ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method));
    DexFile dexFile = new ImmutableDexFile(forArtVersion(56), Collections.singletonList(classDef));
    ClassPath classPath = new ClassPath(Lists.newArrayList(new DexClassProvider(dexFile)), true, 56);
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
    List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
    Assert.assertEquals("Lmain;", analyzedInstructions.get(2).getPreInstructionRegisterType(1).type.getType());
    Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ImmutableMethodParameter(org.jf.dexlib2.immutable.ImmutableMethodParameter) BuilderInstruction21t(org.jf.dexlib2.builder.instruction.BuilderInstruction21t) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) Method(org.jf.dexlib2.iface.Method) DexFile(org.jf.dexlib2.iface.DexFile) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) BuilderInstruction22c(org.jf.dexlib2.builder.instruction.BuilderInstruction22c) ImmutableTypeReference(org.jf.dexlib2.immutable.reference.ImmutableTypeReference) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) MethodImplementationBuilder(org.jf.dexlib2.builder.MethodImplementationBuilder) Test(org.junit.Test)

Example 27 with Method

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

the class ClassPool method intern.

public void intern(@Nonnull ClassDef classDef) {
    PoolClassDef poolClassDef = new PoolClassDef(classDef);
    PoolClassDef prev = internedItems.put(poolClassDef.getType(), poolClassDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", poolClassDef.getType());
    }
    dexPool.typeSection.intern(poolClassDef.getType());
    dexPool.typeSection.internNullable(poolClassDef.getSuperclass());
    dexPool.typeListSection.intern(poolClassDef.getInterfaces());
    dexPool.stringSection.internNullable(poolClassDef.getSourceFile());
    HashSet<String> fields = new HashSet<String>();
    for (Field field : poolClassDef.getFields()) {
        String fieldDescriptor = ReferenceUtil.getShortFieldDescriptor(field);
        if (!fields.add(fieldDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for field %s->%s", poolClassDef.getType(), fieldDescriptor);
        }
        dexPool.fieldSection.intern(field);
        EncodedValue initialValue = field.getInitialValue();
        if (initialValue != null) {
            dexPool.internEncodedValue(initialValue);
        }
        dexPool.annotationSetSection.intern(field.getAnnotations());
    }
    HashSet<String> methods = new HashSet<String>();
    for (PoolMethod method : poolClassDef.getMethods()) {
        String methodDescriptor = ReferenceUtil.getMethodDescriptor(method, true);
        if (!methods.add(methodDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for method %s->%s", poolClassDef.getType(), methodDescriptor);
        }
        dexPool.methodSection.intern(method);
        internCode(method);
        internDebug(method);
        dexPool.annotationSetSection.intern(method.getAnnotations());
        for (MethodParameter parameter : method.getParameters()) {
            dexPool.annotationSetSection.intern(parameter.getAnnotations());
        }
    }
    dexPool.annotationSetSection.intern(poolClassDef.getAnnotations());
}
Also used : EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) ExceptionWithContext(org.jf.util.ExceptionWithContext)

Example 28 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 29 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)

Example 30 with Method

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

the class MethodDefinition method addAnalyzedInstructionMethodItems.

private void addAnalyzedInstructionMethodItems(List<MethodItem> methodItems) {
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classDef.options.classPath, method, classDef.options.inlineResolver, classDef.options.normalizeVirtualMethods);
    AnalysisException analysisException = methodAnalyzer.getAnalysisException();
    if (analysisException != null) {
        // TODO: need to keep track of whether any errors occurred, so we can exit with a non-zero result
        methodItems.add(new CommentMethodItem(String.format("AnalysisException: %s", analysisException.getMessage()), analysisException.codeAddress, Integer.MIN_VALUE));
        analysisException.printStackTrace(System.err);
    }
    List<AnalyzedInstruction> instructions = methodAnalyzer.getAnalyzedInstructions();
    int currentCodeAddress = 0;
    for (int i = 0; i < instructions.size(); i++) {
        AnalyzedInstruction instruction = instructions.get(i);
        MethodItem methodItem = InstructionMethodItemFactory.makeInstructionFormatMethodItem(this, currentCodeAddress, instruction.getInstruction());
        methodItems.add(methodItem);
        if (instruction.getInstruction().getOpcode().format == Format.UnresolvedOdexInstruction) {
            methodItems.add(new CommentedOutMethodItem(InstructionMethodItemFactory.makeInstructionFormatMethodItem(this, currentCodeAddress, instruction.getOriginalInstruction())));
        }
        if (i != instructions.size() - 1) {
            methodItems.add(new BlankMethodItem(currentCodeAddress));
        }
        if (classDef.options.codeOffsets) {
            methodItems.add(new MethodItem(currentCodeAddress) {

                @Override
                public double getSortOrder() {
                    return -1000;
                }

                @Override
                public boolean writeTo(IndentingWriter writer) throws IOException {
                    writer.write("#@");
                    writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL);
                    return true;
                }
            });
        }
        if (classDef.options.registerInfo != 0 && !instruction.getInstruction().getOpcode().format.isPayloadFormat) {
            methodItems.add(new PreInstructionRegisterInfoMethodItem(classDef.options.registerInfo, methodAnalyzer, registerFormatter, instruction, currentCodeAddress));
            methodItems.add(new PostInstructionRegisterInfoMethodItem(registerFormatter, instruction, currentCodeAddress));
        }
        currentCodeAddress += instruction.getInstruction().getCodeUnits();
    }
}
Also used : DebugMethodItem(org.jf.baksmali.Adaptors.Debug.DebugMethodItem) MethodAnalyzer(org.jf.dexlib2.analysis.MethodAnalyzer) IOException(java.io.IOException) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction) AnalysisException(org.jf.dexlib2.analysis.AnalysisException) IndentingWriter(org.jf.util.IndentingWriter)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)21 Method (org.jf.dexlib2.iface.Method)21 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)19 Test (org.junit.Test)16 Instruction (org.jf.dexlib2.iface.instruction.Instruction)15 DexFile (org.jf.dexlib2.iface.DexFile)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)13 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)11 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)10 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 Nonnull (javax.annotation.Nonnull)6 Opcode (org.jf.dexlib2.Opcode)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 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6