Search in sources :

Example 1 with DexBuilder

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

the class Smali method assemble.

/**
     * Assemble the specified files, using the given options
     *
     * @param options a SmaliOptions object with the options to run smali with
     * @param input The files/directories to process
     * @return true if assembly completed with no errors, or false if errors were encountered
     */
public static boolean assemble(final SmaliOptions options, List<String> input) throws IOException {
    LinkedHashSet<File> filesToProcessSet = new LinkedHashSet<File>();
    for (String fileToProcess : input) {
        File argFile = new File(fileToProcess);
        if (!argFile.exists()) {
            throw new IllegalArgumentException("Cannot find file or directory \"" + fileToProcess + "\"");
        }
        if (argFile.isDirectory()) {
            getSmaliFilesInDir(argFile, filesToProcessSet);
        } else if (argFile.isFile()) {
            filesToProcessSet.add(argFile);
        }
    }
    boolean errors = false;
    final DexBuilder dexBuilder = new DexBuilder(Opcodes.forApi(options.apiLevel));
    ExecutorService executor = Executors.newFixedThreadPool(options.jobs);
    List<Future<Boolean>> tasks = Lists.newArrayList();
    for (final File file : filesToProcessSet) {
        tasks.add(executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return assembleSmaliFile(file, dexBuilder, options);
            }
        }));
    }
    for (Future<Boolean> task : tasks) {
        while (true) {
            try {
                try {
                    if (!task.get()) {
                        errors = true;
                    }
                } catch (ExecutionException ex) {
                    throw new RuntimeException(ex);
                }
            } catch (InterruptedException ex) {
                continue;
            }
            break;
        }
    }
    executor.shutdown();
    if (errors) {
        return false;
    }
    dexBuilder.writeTo(new FileDataStore(new File(options.outputDexFile)));
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) File(java.io.File) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore)

Example 2 with DexBuilder

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

the class JumboStringConversionTest method testJumboStringConversion_NonMethodBuilder.

@Test
public void testJumboStringConversion_NonMethodBuilder() throws IOException {
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    final List<Instruction> instructions = Lists.newArrayList();
    for (int i = 0; i < 66000; i++) {
        final StringReference ref = dexBuilder.internStringReference(String.format("%08d", i));
        instructions.add(new Instruction21c() {

            @Override
            public int getRegisterA() {
                return 0;
            }

            @Nonnull
            @Override
            public Reference getReference() {
                return ref;
            }

            @Override
            public int getReferenceType() {
                return ReferenceType.STRING;
            }

            @Override
            public Opcode getOpcode() {
                return Opcode.CONST_STRING;
            }

            @Override
            public int getCodeUnits() {
                return getOpcode().format.size / 2;
            }
        });
    }
    instructions.add(new ImmutableInstruction10x(Opcode.RETURN_VOID));
    MethodImplementation methodImpl = new MethodImplementation() {

        @Override
        public int getRegisterCount() {
            return 1;
        }

        @Nonnull
        @Override
        public Iterable<? extends Instruction> getInstructions() {
            return instructions;
        }

        @Nonnull
        @Override
        public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() {
            return ImmutableList.of();
        }

        @Nonnull
        @Override
        public Iterable<? extends DebugItem> getDebugItems() {
            return ImmutableList.of();
        }
    };
    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(), methodImpl)));
    MemoryDataStore dexStore = new MemoryDataStore();
    dexBuilder.writeTo(dexStore);
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dexStore.getData());
    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> actualInstructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(66001, actualInstructions.size());
    for (int i = 0; i < 65536; i++) {
        Assert.assertEquals(Opcode.CONST_STRING, actualInstructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) actualInstructions.get(i)).getReference()).getString());
    }
    for (int i = 65536; i < 66000; i++) {
        Assert.assertEquals(Opcode.CONST_STRING_JUMBO, actualInstructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) actualInstructions.get(i)).getReference()).getString());
    }
    Assert.assertEquals(Opcode.RETURN_VOID, actualInstructions.get(66000).getOpcode());
}
Also used : BuilderInstruction21c(org.jf.dexlib2.builder.instruction.BuilderInstruction21c) Instruction21c(org.jf.dexlib2.iface.instruction.formats.Instruction21c) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) Nonnull(javax.annotation.Nonnull) Reference(org.jf.dexlib2.iface.reference.Reference) StringReference(org.jf.dexlib2.iface.reference.StringReference) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) Opcode(org.jf.dexlib2.Opcode) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) StringReference(org.jf.dexlib2.iface.reference.StringReference) ImmutableInstruction10x(org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) Test(org.junit.Test)

Example 3 with DexBuilder

use of org.jf.dexlib2.writer.builder.DexBuilder 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(), methodBuilder.getMethodImplementation())));
    MemoryDataStore dexStore = new MemoryDataStore();
    dexBuilder.writeTo(dexStore);
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dexStore.getData());
    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 4 with DexBuilder

use of org.jf.dexlib2.writer.builder.DexBuilder in project Apktool by iBotPeaches.

the class SmaliBuilder method build.

private void build() throws AndrolibException {
    try {
        DexBuilder dexBuilder;
        if (mApiLevel > 0) {
            dexBuilder = DexBuilder.makeDexBuilder(Opcodes.forApi(mApiLevel));
        } else {
            dexBuilder = DexBuilder.makeDexBuilder();
        }
        for (String fileName : mSmaliDir.getDirectory().getFiles(true)) {
            buildFile(fileName, dexBuilder);
        }
        dexBuilder.writeTo(new FileDataStore(new File(mDexFile.getAbsolutePath())));
    } catch (IOException | DirectoryException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : AndrolibException(brut.androlib.AndrolibException) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) ExtFile(brut.directory.ExtFile) DirectoryException(brut.directory.DirectoryException) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder)

Example 5 with DexBuilder

use of org.jf.dexlib2.writer.builder.DexBuilder in project atlas by alibaba.

the class SmaliUtils method assembleSmaliFile.

/**
     * 将smali文件夹转换为dex文件
     * @param smaliFolder
     * @param outDexFile
     * @return
     */
public static boolean assembleSmaliFile(File smaliFolder, File outDexFile) throws IOException, RecognitionException {
    Collection<File> smaliFiles = FileUtils.listFiles(smaliFolder, new String[] { "smali" }, true);
    if (null != smaliFiles && smaliFiles.size() > 0) {
        DexBuilder dexBuilder = DexBuilder.makeDexBuilder();
        for (File smaliFile : smaliFiles) {
            SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true);
        }
        dexBuilder.writeTo(new FileDataStore(outDexFile));
        return true;
    } else {
        return false;
    }
}
Also used : File(java.io.File) DexFile(org.jf.dexlib2.iface.DexFile) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder)

Aggregations

DexBuilder (org.jf.dexlib2.writer.builder.DexBuilder)7 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)4 FileDataStore (org.jf.dexlib2.writer.io.FileDataStore)4 File (java.io.File)3 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)3 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)2 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)2 Test (org.junit.Test)2 AndrolibException (brut.androlib.AndrolibException)1 DirectoryException (brut.directory.DirectoryException)1 ExtFile (brut.directory.ExtFile)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Nonnull (javax.annotation.Nonnull)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 TokenSource (org.antlr.runtime.TokenSource)1 CommonTree (org.antlr.runtime.tree.CommonTree)1 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)1