Search in sources :

Example 1 with ClassPathResolver

use of org.jf.dexlib2.analysis.ClassPathResolver in project smali by JesusFreke.

the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Static.

@Test
public void testCustomMethodInlineTable_Static() throws IOException {
    List<ImmutableInstruction> instructions = Lists.newArrayList(new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID));
    ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null);
    ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.STATIC.getValue(), null, methodImpl);
    ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, ImmutableList.of(method), null);
    DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef));
    ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile);
    ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART);
    InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V");
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false);
    Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0);
    Assert.assertEquals(Opcode.INVOKE_STATIC, deodexedInstruction.getOpcode());
    MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
    Assert.assertEquals(method, methodReference);
}
Also used : ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableInstruction10x(org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x) DexFile(org.jf.dexlib2.iface.DexFile) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) ImmutableInstruction35mi(org.jf.dexlib2.immutable.instruction.ImmutableInstruction35mi) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableMethodImplementation(org.jf.dexlib2.immutable.ImmutableMethodImplementation) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) Test(org.junit.Test)

Example 2 with ClassPathResolver

use of org.jf.dexlib2.analysis.ClassPathResolver in project smali by JesusFreke.

the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Direct.

@Test
public void testCustomMethodInlineTable_Direct() throws IOException {
    List<ImmutableInstruction> instructions = Lists.newArrayList(new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID));
    ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null);
    ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PRIVATE.getValue(), null, methodImpl);
    ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, ImmutableList.of(method), null);
    DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef));
    ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile);
    ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART);
    InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V");
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false);
    Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0);
    Assert.assertEquals(Opcode.INVOKE_DIRECT, deodexedInstruction.getOpcode());
    MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
    Assert.assertEquals(method, methodReference);
}
Also used : ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableInstruction10x(org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x) DexFile(org.jf.dexlib2.iface.DexFile) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) ImmutableInstruction35mi(org.jf.dexlib2.immutable.instruction.ImmutableInstruction35mi) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableMethodImplementation(org.jf.dexlib2.immutable.ImmutableMethodImplementation) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) Test(org.junit.Test)

Example 3 with ClassPathResolver

use of org.jf.dexlib2.analysis.ClassPathResolver in project smali by JesusFreke.

the class AnalysisArguments method loadClassPathForDexFile.

@Nonnull
public ClassPath loadClassPathForDexFile(@Nonnull File dexFileDir, @Nonnull DexFile dexFile, boolean checkPackagePrivateAccess, int oatVersion) throws IOException {
    ClassPathResolver resolver;
    // regardless of the actual version of the oat file
    if (oatVersion == NOT_ART) {
        if (dexFile instanceof OatDexFile) {
            checkPackagePrivateAccess = true;
            oatVersion = ((OatDexFile) dexFile).getContainer().getOatVersion();
        }
    } else {
        // this should always be true for ART
        checkPackagePrivateAccess = true;
    }
    if (classPathDirectories == null || classPathDirectories.size() == 0) {
        classPathDirectories = Lists.newArrayList(dexFileDir.getPath());
    }
    List<String> filteredClassPathDirectories = Lists.newArrayList();
    if (classPathDirectories != null) {
        for (String dir : classPathDirectories) {
            File file = new File(dir);
            if (!file.exists()) {
                System.err.println(String.format("Warning: directory %s does not exist. Ignoring.", dir));
            } else if (!file.isDirectory()) {
                System.err.println(String.format("Warning: %s is not a directory. Ignoring.", dir));
            } else {
                filteredClassPathDirectories.add(dir);
            }
        }
    }
    if (bootClassPath == null) {
        // TODO: we should be able to get the api from the Opcodes object associated with the dexFile..
        // except that the oat version -> api mapping doesn't fully work yet
        resolver = new ClassPathResolver(filteredClassPathDirectories, classPath, dexFile);
    } else if (bootClassPath.size() == 1 && bootClassPath.get(0).length() == 0) {
        // --bootclasspath "" is a special case, denoting that no bootclasspath should be used
        resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), classPath, dexFile);
    } else {
        resolver = new ClassPathResolver(filteredClassPathDirectories, bootClassPath, classPath, dexFile);
    }
    if (oatVersion == 0 && dexFile instanceof OatDexFile) {
        oatVersion = ((OatDexFile) dexFile).getContainer().getOatVersion();
    }
    return new ClassPath(resolver.getResolvedClassProviders(), checkPackagePrivateAccess, oatVersion);
}
Also used : ClassPath(org.jf.dexlib2.analysis.ClassPath) ClassPathResolver(org.jf.dexlib2.analysis.ClassPathResolver) OatDexFile(org.jf.dexlib2.dexbacked.OatFile.OatDexFile) File(java.io.File) DexFile(org.jf.dexlib2.iface.DexFile) OatDexFile(org.jf.dexlib2.dexbacked.OatFile.OatDexFile) Nonnull(javax.annotation.Nonnull)

Example 4 with ClassPathResolver

use of org.jf.dexlib2.analysis.ClassPathResolver in project smali by JesusFreke.

the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Virtual.

@Test
public void testCustomMethodInlineTable_Virtual() throws IOException {
    List<ImmutableInstruction> instructions = Lists.newArrayList(new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID));
    ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null);
    ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PUBLIC.getValue(), null, methodImpl);
    ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, null, ImmutableList.of(method));
    DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef));
    ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile);
    ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART);
    InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V");
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false);
    Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0);
    Assert.assertEquals(Opcode.INVOKE_VIRTUAL, deodexedInstruction.getOpcode());
    MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
    Assert.assertEquals(method, methodReference);
}
Also used : ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableInstruction10x(org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x) DexFile(org.jf.dexlib2.iface.DexFile) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) ImmutableInstruction35mi(org.jf.dexlib2.immutable.instruction.ImmutableInstruction35mi) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableMethodImplementation(org.jf.dexlib2.immutable.ImmutableMethodImplementation) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) Test(org.junit.Test)

Aggregations

DexFile (org.jf.dexlib2.iface.DexFile)4 ClassDef (org.jf.dexlib2.iface.ClassDef)3 Instruction (org.jf.dexlib2.iface.instruction.Instruction)3 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)3 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)3 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)3 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)3 ImmutableMethodImplementation (org.jf.dexlib2.immutable.ImmutableMethodImplementation)3 ImmutableInstruction (org.jf.dexlib2.immutable.instruction.ImmutableInstruction)3 ImmutableInstruction10x (org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x)3 ImmutableInstruction35mi (org.jf.dexlib2.immutable.instruction.ImmutableInstruction35mi)3 Test (org.junit.Test)3 File (java.io.File)1 Nonnull (javax.annotation.Nonnull)1 ClassPath (org.jf.dexlib2.analysis.ClassPath)1 ClassPathResolver (org.jf.dexlib2.analysis.ClassPathResolver)1 OatDexFile (org.jf.dexlib2.dexbacked.OatFile.OatDexFile)1