Search in sources :

Example 6 with SyntheticAccessorResolver

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

the class DisassembleCommand method getOptions.

protected BaksmaliOptions getOptions() {
    if (dexFile == null) {
        throw new IllegalStateException("You must call loadDexFile first");
    }
    final BaksmaliOptions options = new BaksmaliOptions();
    if (needsClassPath()) {
        try {
            options.classPath = analysisArguments.loadClassPathForDexFile(inputFile.getAbsoluteFile().getParentFile(), dexFile, shouldCheckPackagePrivateAccess());
        } catch (Exception ex) {
            System.err.println("\n\nError occurred while loading class path files. Aborting.");
            ex.printStackTrace(System.err);
            System.exit(-1);
        }
    }
    if (!resourceIdFiles.isEmpty()) {
        Map<String, File> resourceFiles = Maps.newHashMap();
        assert (resourceIdFiles.size() % 2) == 0;
        for (int i = 0; i < resourceIdFiles.size(); i += 2) {
            String resourcePrefix = resourceIdFiles.get(i);
            String publicXml = resourceIdFiles.get(i + 1);
            File publicXmlFile = new File(publicXml);
            if (!publicXmlFile.exists()) {
                System.err.println(String.format("Can't find file: %s", publicXmlFile));
                System.exit(-1);
            }
            resourceFiles.put(resourcePrefix, publicXmlFile);
        }
        try {
            options.loadResourceIds(resourceFiles);
        } catch (IOException ex) {
            System.err.println("Error while loading resource files:");
            ex.printStackTrace(System.err);
            System.exit(-1);
        } catch (SAXException ex) {
            System.err.println("Error while loading resource files:");
            ex.printStackTrace(System.err);
            System.exit(-1);
        }
    }
    options.parameterRegisters = parameterRegisters;
    options.localsDirective = localsDirective;
    options.sequentialLabels = sequentialLabels;
    options.debugInfo = debugInfo;
    options.codeOffsets = codeOffsets;
    options.accessorComments = accessorComments;
    options.implicitReferences = implicitReferences;
    options.normalizeVirtualMethods = normalizeVirtualMethods;
    options.registerInfo = 0;
    for (String registerInfoType : registerInfoTypes) {
        if (registerInfoType.equalsIgnoreCase("ALL")) {
            options.registerInfo |= BaksmaliOptions.ALL;
        } else if (registerInfoType.equalsIgnoreCase("ALLPRE")) {
            options.registerInfo |= BaksmaliOptions.ALLPRE;
        } else if (registerInfoType.equalsIgnoreCase("ALLPOST")) {
            options.registerInfo |= BaksmaliOptions.ALLPOST;
        } else if (registerInfoType.equalsIgnoreCase("ARGS")) {
            options.registerInfo |= BaksmaliOptions.ARGS;
        } else if (registerInfoType.equalsIgnoreCase("DEST")) {
            options.registerInfo |= BaksmaliOptions.DEST;
        } else if (registerInfoType.equalsIgnoreCase("MERGE")) {
            options.registerInfo |= BaksmaliOptions.MERGE;
        } else if (registerInfoType.equalsIgnoreCase("FULLMERGE")) {
            options.registerInfo |= BaksmaliOptions.FULLMERGE;
        } else {
            System.err.println(String.format("Invalid register info type: %s", registerInfoType));
            usage();
            System.exit(-1);
        }
        if ((options.registerInfo & BaksmaliOptions.FULLMERGE) != 0) {
            options.registerInfo &= ~BaksmaliOptions.MERGE;
        }
    }
    if (accessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(dexFile.getOpcodes(), dexFile.getClasses());
    }
    return options;
}
Also used : IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Example 7 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)7 org.jf.baksmali.baksmaliOptions (org.jf.baksmali.baksmaliOptions)4 ClassDef (org.jf.dexlib2.iface.ClassDef)4 DexFile (org.jf.dexlib2.iface.DexFile)3 SAXException (org.xml.sax.SAXException)3 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXParser (javax.xml.parsers.SAXParser)2 CustomInlineMethodResolver (org.jf.dexlib2.analysis.CustomInlineMethodResolver)2 ClassFileNameHandler (org.jf.util.ClassFileNameHandler)2 Attributes (org.xml.sax.Attributes)2 DefaultHandler (org.xml.sax.helpers.DefaultHandler)2 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 RecognitionException (org.antlr.runtime.RecognitionException)1 ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)1 Method (org.jf.dexlib2.iface.Method)1 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)1