Search in sources :

Example 16 with MethodImplementationBuilder

use of org.jf.dexlib2.builder.MethodImplementationBuilder in project smali by JesusFreke.

the class FixGotoTest method testFixGoto16ToGoto32.

@Test
public void testFixGoto16ToGoto32() {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(1);
    Label gotoTarget = builder.getLabel("gotoTarget");
    builder.addInstruction(new BuilderInstruction20t(Opcode.GOTO_16, gotoTarget));
    for (int i = 0; i < 70000; i++) {
        builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    }
    builder.addLabel("gotoTarget");
    builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    MethodImplementation impl = builder.getMethodImplementation();
    List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(70002, instructions.size());
    Assert.assertEquals(Opcode.GOTO_32, instructions.get(0).getOpcode());
    Assert.assertEquals(70003, ((OffsetInstruction) instructions.get(0)).getCodeOffset());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction20t(org.jf.dexlib2.builder.instruction.BuilderInstruction20t) Test(org.junit.Test)

Example 17 with MethodImplementationBuilder

use of org.jf.dexlib2.builder.MethodImplementationBuilder in project smali by JesusFreke.

the class JumboStringConversionTest method testJumboStringConversion.

@Test
public void testJumboStringConversion() throws IOException {
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    MethodImplementationBuilder methodBuilder = new MethodImplementationBuilder(1);
    for (int i = 0; i < 66000; i++) {
        methodBuilder.addInstruction(new BuilderInstruction21c(Opcode.CONST_STRING, 0, dexBuilder.internStringReference(String.format("%08d", i))));
    }
    methodBuilder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    dexBuilder.internClassDef("Ltest;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.<Annotation>of(), null, ImmutableList.of(dexBuilder.internMethod("Ltest;", "test", null, "V", 0, ImmutableSet.<Annotation>of(), ImmutableSet.of(), methodBuilder.getMethodImplementation())));
    MemoryDataStore dexStore = new MemoryDataStore();
    dexBuilder.writeTo(dexStore);
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dexStore.getBuffer());
    ClassDef classDef = Iterables.getFirst(dexFile.getClasses(), null);
    Assert.assertNotNull(classDef);
    Method method = Iterables.getFirst(classDef.getMethods(), null);
    Assert.assertNotNull(method);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(66001, instructions.size());
    for (int i = 0; i < 65536; i++) {
        Assert.assertEquals(Opcode.CONST_STRING, instructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) instructions.get(i)).getReference()).getString());
    }
    for (int i = 65536; i < 66000; i++) {
        Assert.assertEquals(Opcode.CONST_STRING_JUMBO, instructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) instructions.get(i)).getReference()).getString());
    }
    Assert.assertEquals(Opcode.RETURN_VOID, instructions.get(66000).getOpcode());
}
Also used : BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) BuilderInstruction21c(org.jf.dexlib2.builder.instruction.BuilderInstruction21c) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) MethodImplementationBuilder(org.jf.dexlib2.builder.MethodImplementationBuilder) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) Test(org.junit.Test)

Example 18 with MethodImplementationBuilder

use of org.jf.dexlib2.builder.MethodImplementationBuilder in project smali by JesusFreke.

the class CallSiteTest method testBuilderCallSite.

@Test
public void testBuilderCallSite() throws IOException {
    DexBuilder dexBuilder = new DexBuilder(Opcodes.forArtVersion(111));
    BuilderCallSiteReference callSite = dexBuilder.internCallSite(new ImmutableCallSiteReference("call_site_1", new ImmutableMethodHandleReference(MethodHandleType.INVOKE_STATIC, new ImmutableMethodReference("Lcls1;", "loader", ImmutableList.of("Ljava/lang/invoke/Lookup;", "Ljava/lang/String;", "Ljava/lang/invoke/MethodType;"), "Ljava/lang/invoke/CallSite;")), "someMethod", new ImmutableMethodProtoReference(ImmutableList.of(), "V"), ImmutableList.of()));
    MethodImplementationBuilder methodImplementationBuilder = new MethodImplementationBuilder(10);
    methodImplementationBuilder.addInstruction(new BuilderInstruction35c(Opcode.INVOKE_CUSTOM, 0, 0, 0, 0, 0, 0, callSite));
    BuilderMethod method = dexBuilder.internMethod("Lcls1;", "method1", null, "V", 0, ImmutableSet.of(), ImmutableSet.of(), methodImplementationBuilder.getMethodImplementation());
    dexBuilder.internClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, ImmutableSet.of(), null, ImmutableList.of(method));
    File tempFile = File.createTempFile("dex", ".dex");
    dexBuilder.writeTo(new FileDataStore(tempFile));
    verifyDexFile(DexFileFactory.loadDexFile(tempFile, Opcodes.forArtVersion(111)));
}
Also used : ImmutableMethodProtoReference(org.jf.dexlib2.immutable.reference.ImmutableMethodProtoReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) ImmutableMethodHandleReference(org.jf.dexlib2.immutable.reference.ImmutableMethodHandleReference) BuilderInstruction35c(org.jf.dexlib2.builder.instruction.BuilderInstruction35c) BuilderMethod(org.jf.dexlib2.writer.builder.BuilderMethod) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) File(java.io.File) DexFile(org.jf.dexlib2.iface.DexFile) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) BuilderCallSiteReference(org.jf.dexlib2.writer.builder.BuilderCallSiteReference) MethodImplementationBuilder(org.jf.dexlib2.builder.MethodImplementationBuilder) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) ImmutableCallSiteReference(org.jf.dexlib2.immutable.reference.ImmutableCallSiteReference) Test(org.junit.Test)

Example 19 with MethodImplementationBuilder

use of org.jf.dexlib2.builder.MethodImplementationBuilder in project soot by Sable.

the class DexPrinter method toTries.

private void toTries(Collection<Trap> traps, StmtVisitor stmtV, MethodImplementationBuilder builder, LabelAssigner labelAssigner) {
    // Original code: assume that the mapping startCodeAddress -> TryItem is
    // enough for
    // a "code range", ignore different end Units / try lengths
    // That's definitely not enough since we can have two handlers H1, H2
    // with
    // H1:240-322, H2:242-322. There is no valid ordering for such
    // overlapping traps
    // in dex. Current solution: If there is already a trap T' for a
    // subrange of the
    // current trap T, merge T and T' on the fully range of T. This is not a
    // 100%
    // correct since we extend traps over the requested range, but it's
    // better than
    // the previous code that produced APKs which failed Dalvik's bytecode
    // verification.
    // (Steven Arzt, 09.08.2013)
    // There are cases in which we need to split traps, e.g. in cases like
    // ( (t1) ... (t2) )<big catch all around it> where the all three
    // handlers do
    // something different. That's why we run the TrapSplitter before we get
    // here.
    // (Steven Arzt, 25.09.2013)
    Map<CodeRange, List<ExceptionHandler>> codeRangesToTryItem = new LinkedHashMap<CodeRange, List<ExceptionHandler>>();
    for (Trap t : traps) {
        // see if there is old handler info at this code range
        Stmt beginStmt = (Stmt) t.getBeginUnit();
        Stmt endStmt = (Stmt) t.getEndUnit();
        int startCodeAddress = labelAssigner.getLabel(beginStmt).getCodeAddress();
        int endCodeAddress = labelAssigner.getLabel(endStmt).getCodeAddress();
        CodeRange range = new CodeRange(startCodeAddress, endCodeAddress);
        String exceptionType = SootToDexUtils.getDexTypeDescriptor(t.getException().getType());
        int codeAddress = labelAssigner.getLabel((Stmt) t.getHandlerUnit()).getCodeAddress();
        ImmutableExceptionHandler exceptionHandler = new ImmutableExceptionHandler(exceptionType, codeAddress);
        List<ExceptionHandler> newHandlers = new ArrayList<ExceptionHandler>();
        for (CodeRange r : codeRangesToTryItem.keySet()) {
            // trap over the bigger range containing this range
            if (r.containsRange(range)) {
                range.startAddress = r.startAddress;
                range.endAddress = r.endAddress;
                // copy the old handlers to a bigger array (the old one
                // cannot be modified...)
                List<ExceptionHandler> oldHandlers = codeRangesToTryItem.get(r);
                if (oldHandlers != null)
                    newHandlers.addAll(oldHandlers);
                break;
            } else // range.
            if (range.containsRange(r)) {
                range.startAddress = r.startAddress;
                range.endAddress = r.endAddress;
                // just use the newly found handler info
                List<ExceptionHandler> oldHandlers = codeRangesToTryItem.get(range);
                if (oldHandlers != null)
                    newHandlers.addAll(oldHandlers);
                // remove the old range, the new one will be added anyway
                // and contain
                // the merged handlers
                codeRangesToTryItem.remove(r);
                break;
            }
        }
        if (!newHandlers.contains(exceptionHandler))
            newHandlers.add(exceptionHandler);
        codeRangesToTryItem.put(range, newHandlers);
    }
    // Check for overlaps
    for (CodeRange r1 : codeRangesToTryItem.keySet()) {
        for (CodeRange r2 : codeRangesToTryItem.keySet()) {
            if (r1 != r2 && r1.overlaps(r2))
                LOGGER.warn("Trap region overlaps detected");
        }
    }
    for (CodeRange range : codeRangesToTryItem.keySet()) {
        boolean allCaughtForRange = false;
        for (ExceptionHandler handler : codeRangesToTryItem.get(range)) {
            // anyway.
            if (allCaughtForRange)
                continue;
            // therefore hack it using java.lang.Throwable.
            if ("Ljava/lang/Throwable;".equals(handler.getExceptionType())) {
                /*
					 * builder.addCatch(labelAssigner.getLabelAtAddress(range. startAddress),
					 * labelAssigner.getLabelAtAddress(range.endAddress),
					 * labelAssigner.getLabelAtAddress(handler. getHandlerCodeAddress()));
					 */
                allCaughtForRange = true;
            }
            // else
            builder.addCatch(new ImmutableTypeReference(handler.getExceptionType()), labelAssigner.getLabelAtAddress(range.startAddress), labelAssigner.getLabelAtAddress(range.endAddress), labelAssigner.getLabelAtAddress(handler.getHandlerCodeAddress()));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Trap(soot.Trap) LinkedHashMap(java.util.LinkedHashMap) NopStmt(soot.jimple.NopStmt) Stmt(soot.jimple.Stmt) IdentityStmt(soot.jimple.IdentityStmt) MonitorStmt(soot.jimple.MonitorStmt) ExceptionHandler(org.jf.dexlib2.iface.ExceptionHandler) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableTypeReference(org.jf.dexlib2.immutable.reference.ImmutableTypeReference) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler)

Example 20 with MethodImplementationBuilder

use of org.jf.dexlib2.builder.MethodImplementationBuilder in project smali by JesusFreke.

the class MethodAnalyzerTest method testInstanceOfNarrowingAfterMove_art.

@Test
public void testInstanceOfNarrowingAfterMove_art() throws IOException {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(3);
    builder.addInstruction(new BuilderInstruction12x(Opcode.MOVE_OBJECT, 1, 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, 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(3).getPreInstructionRegisterType(1).type.getType());
    Assert.assertEquals("Lmain;", analyzedInstructions.get(3).getPreInstructionRegisterType(2).type.getType());
    Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(1).type.getType());
    Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(2).type.getType());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction12x(org.jf.dexlib2.builder.instruction.BuilderInstruction12x) 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)

Aggregations

Test (org.junit.Test)22 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)15 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)12 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)9 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)8 DexFile (org.jf.dexlib2.iface.DexFile)7 Instruction (org.jf.dexlib2.iface.instruction.Instruction)7 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)7 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ClassDef (org.jf.dexlib2.iface.ClassDef)6 Method (org.jf.dexlib2.iface.Method)6 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)6 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)6 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)6 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)6 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)4 BuilderInstruction32x (org.jf.dexlib2.builder.instruction.BuilderInstruction32x)3 LinkedHashMap (java.util.LinkedHashMap)2 BuilderInstruction12x (org.jf.dexlib2.builder.instruction.BuilderInstruction12x)2