Search in sources :

Example 1 with SyntheticAccessorResolver

use of org.jf.dexlib2.util.SyntheticAccessorResolver in project dex2jar by pxb1988.

the class SmaliTest method baksmali.

private static String baksmali(DexBackedClassDef def) throws IOException {
    baksmaliOptions opts = new baksmaliOptions();
    opts.outputDebugInfo = false;
    opts.syntheticAccessorResolver = new SyntheticAccessorResolver(Collections.EMPTY_LIST);
    ClassDefinition classDefinition = new ClassDefinition(opts, def);
    StringWriter bufWriter = new StringWriter();
    IndentingWriter writer = new IndentingWriter(bufWriter);
    classDefinition.writeTo((IndentingWriter) writer);
    writer.flush();
    return bufWriter.toString();
}
Also used : org.jf.baksmali.baksmaliOptions(org.jf.baksmali.baksmaliOptions) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Example 2 with SyntheticAccessorResolver

use of org.jf.dexlib2.util.SyntheticAccessorResolver in project atlas by alibaba.

the class SmaliDiffUtils method getBuildOption.

public static BaksmaliOptions getBuildOption(Iterable<? extends ClassDef> collection, int apiLevel) {
    BaksmaliOptions options = new BaksmaliOptions();
    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = true;
    options.codeOffsets = false;
    options.accessorComments = false;
    // 128
    options.registerInfo = 0;
    options.inlineResolver = null;
    options.apiLevel = apiLevel;
    if (!options.accessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(), collection);
    }
    return options;
}
Also used : BaksmaliOptions(org.jf.baksmali.BaksmaliOptions) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Example 3 with SyntheticAccessorResolver

use of org.jf.dexlib2.util.SyntheticAccessorResolver in project atlas by alibaba.

the class SmaliCodeUtils method createBaksmaliOptions.

/**
 * 生成解析成smali代码的选项
 *
 * @return
 */
private static BaksmaliOptions createBaksmaliOptions(ClassDef classDef) {
    BaksmaliOptions options = new BaksmaliOptions();
    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = false;
    options.codeOffsets = false;
    options.accessorComments = false;
    // 128
    options.registerInfo = 0;
    options.inlineResolver = null;
    options.apiLevel = DEFAULT_API_LEVEL;
    List<ClassDef> classDefs = Lists.newArrayList();
    classDefs.add(classDef);
    options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(), classDefs);
    return options;
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) BaksmaliOptions(org.jf.baksmali.BaksmaliOptions) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Example 4 with SyntheticAccessorResolver

use of org.jf.dexlib2.util.SyntheticAccessorResolver in project atlas by alibaba.

the class BakSmali method disassembleDexFile.

public static boolean disassembleDexFile(DexFile dexFile, final baksmaliOptions options) {
    if (options.registerInfo != 0 || options.deodex) {
        try {
            Iterable<String> extraClassPathEntries;
            if (options.extraClassPathEntries != null) {
                extraClassPathEntries = options.extraClassPathEntries;
            } else {
                extraClassPathEntries = ImmutableList.of();
            }
            options.classPath = ClassPath.fromClassPath(options.bootClassPathDirs, Iterables.concat(options.bootClassPathEntries, extraClassPathEntries), dexFile, options.apiLevel, options.checkPackagePrivateAccess, options.experimental);
            if (options.customInlineDefinitions != null) {
                options.inlineResolver = new CustomInlineMethodResolver(options.classPath, options.customInlineDefinitions);
            }
        } catch (Exception ex) {
            System.err.println("\n\nError occurred while loading boot class path files. Aborting.");
            ex.printStackTrace(System.err);
            return false;
        }
    }
    if (options.resourceIdFileEntries != null) {
        class PublicHandler extends DefaultHandler {

            String prefix = null;

            public PublicHandler(String prefix) {
                super();
                this.prefix = prefix;
            }

            public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException {
                if (qName.equals("public")) {
                    String type = attr.getValue("type");
                    String name = attr.getValue("name").replace('.', '_');
                    Integer public_key = Integer.decode(attr.getValue("id"));
                    String public_val = new StringBuffer().append(prefix).append(".").append(type).append(".").append(name).toString();
                    options.resourceIds.put(public_key, public_val);
                }
            }
        }
        ;
        for (Map.Entry<String, String> entry : options.resourceIdFileEntries.entrySet()) {
            try {
                SAXParser saxp = SAXParserFactory.newInstance().newSAXParser();
                String prefix = entry.getValue();
                saxp.parse(entry.getKey(), new PublicHandler(prefix));
            } catch (ParserConfigurationException e) {
                continue;
            } catch (SAXException e) {
                continue;
            } catch (IOException e) {
                continue;
            }
        }
    }
    File outputDirectoryFile = new File(options.outputDirectory);
    if (!outputDirectoryFile.exists()) {
        if (!outputDirectoryFile.mkdirs()) {
            System.err.println("Can't create the output directory " + options.outputDirectory);
            return false;
        }
    }
    //sort the classes, so that if we're on a case-insensitive file system and need to handle classes with file
    //name collisions, then we'll use the same name for each class, if the dex file goes through multiple
    //baksmali/smali cycles for some reason. If a class with a colliding name is added or removed, the filenames
    //may still change of course
    List<? extends ClassDef> classDefs = Ordering.natural().sortedCopy(dexFile.getClasses());
    if (!options.noAccessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(classDefs);
    }
    final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDirectoryFile, ".smali");
    ExecutorService executor = Executors.newFixedThreadPool(options.jobs);
    List<Future<Boolean>> tasks = Lists.newArrayList();
    for (final ClassDef classDef : classDefs) {
        tasks.add(executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return disassembleClass(classDef, fileNameHandler, options);
            }
        }));
    }
    boolean errorOccurred = false;
    try {
        for (Future<Boolean> task : tasks) {
            while (true) {
                try {
                    if (!task.get()) {
                        errorOccurred = true;
                    }
                } catch (InterruptedException ex) {
                    continue;
                } catch (ExecutionException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            }
        }
    } finally {
        executor.shutdown();
    }
    return !errorOccurred;
}
Also used : CustomInlineMethodResolver(org.jf.dexlib2.analysis.CustomInlineMethodResolver) ClassFileNameHandler(org.jf.util.ClassFileNameHandler) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver) ClassDef(org.jf.dexlib2.iface.ClassDef) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Map(java.util.Map) DexFile(org.jf.dexlib2.iface.DexFile)

Example 5 with SyntheticAccessorResolver

use of org.jf.dexlib2.util.SyntheticAccessorResolver in project smali by JesusFreke.

the class AccessorTest method testAccessors.

@Test
public void testAccessors() throws IOException {
    URL url = AccessorTest.class.getClassLoader().getResource("accessorTest.dex");
    Assert.assertNotNull(url);
    DexFile f = DexFileFactory.loadDexFile(url.getFile(), Opcodes.getDefault());
    SyntheticAccessorResolver sar = new SyntheticAccessorResolver(f.getOpcodes(), f.getClasses());
    ClassDef accessorTypesClass = null;
    ClassDef accessorsClass = null;
    for (ClassDef classDef : f.getClasses()) {
        String className = classDef.getType();
        if (className.equals("Lorg/jf/dexlib2/AccessorTypes;")) {
            accessorTypesClass = classDef;
        } else if (className.equals("Lorg/jf/dexlib2/AccessorTypes$Accessors;")) {
            accessorsClass = classDef;
        }
    }
    Assert.assertNotNull(accessorTypesClass);
    Assert.assertNotNull(accessorsClass);
    for (Method method : accessorsClass.getMethods()) {
        Matcher m = accessorMethodPattern.matcher(method.getName());
        if (!m.matches()) {
            continue;
        }
        String type = m.group(1);
        String operation = m.group(2);
        MethodImplementation methodImpl = method.getImplementation();
        Assert.assertNotNull(methodImpl);
        for (Instruction instruction : methodImpl.getInstructions()) {
            Opcode opcode = instruction.getOpcode();
            if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) {
                MethodReference accessorMethod = (MethodReference) ((ReferenceInstruction) instruction).getReference();
                SyntheticAccessorResolver.AccessedMember accessedMember = sar.getAccessedMember(accessorMethod);
                Assert.assertNotNull(String.format("Could not resolve accessor for %s_%s", type, operation), accessedMember);
                int operationType = operationTypes.get(operation);
                Assert.assertEquals(operationType, accessedMember.accessedMemberType);
                Assert.assertEquals(String.format("%s_val", type), ((FieldReference) accessedMember.accessedMember).getName());
            }
        }
    }
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) Matcher(java.util.regex.Matcher) Method(org.jf.dexlib2.iface.Method) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) URL(java.net.URL) DexFile(org.jf.dexlib2.iface.DexFile) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver) ClassDef(org.jf.dexlib2.iface.ClassDef) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) Test(org.junit.Test)

Aggregations

SyntheticAccessorResolver (org.jf.dexlib2.util.SyntheticAccessorResolver)6 ClassDef (org.jf.dexlib2.iface.ClassDef)3 BaksmaliOptions (org.jf.baksmali.BaksmaliOptions)2 DexFile (org.jf.dexlib2.iface.DexFile)2 SAXException (org.xml.sax.SAXException)2 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)1 org.jf.baksmali.baksmaliOptions (org.jf.baksmali.baksmaliOptions)1 CustomInlineMethodResolver (org.jf.dexlib2.analysis.CustomInlineMethodResolver)1 Method (org.jf.dexlib2.iface.Method)1 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)1 Instruction (org.jf.dexlib2.iface.instruction.Instruction)1 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)1 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)1