1 /* 2 * Copyright 2013, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.dexlib2.analysis; 33 34 import com.google.common.collect.ImmutableList; 35 import com.google.common.collect.Lists; 36 import org.jf.dexlib2.AccessFlags; 37 import org.jf.dexlib2.Opcode; 38 import org.jf.dexlib2.Opcodes; 39 import org.jf.dexlib2.iface.ClassDef; 40 import org.jf.dexlib2.iface.DexFile; 41 import org.jf.dexlib2.iface.instruction.Instruction; 42 import org.jf.dexlib2.iface.instruction.formats.Instruction35c; 43 import org.jf.dexlib2.iface.reference.MethodReference; 44 import org.jf.dexlib2.immutable.ImmutableClassDef; 45 import org.jf.dexlib2.immutable.ImmutableDexFile; 46 import org.jf.dexlib2.immutable.ImmutableMethod; 47 import org.jf.dexlib2.immutable.ImmutableMethodImplementation; 48 import org.jf.dexlib2.immutable.instruction.ImmutableInstruction; 49 import org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x; 50 import org.jf.dexlib2.immutable.instruction.ImmutableInstruction35mi; 51 import org.junit.Assert; 52 import org.junit.Test; 53 54 import java.util.List; 55 56 public class CustomMethodInlineTableTest { 57 @Test 58 public void testCustomMethodInlineTable_Virtual() { 59 List<ImmutableInstruction> instructions = Lists.newArrayList( 60 new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), 61 new ImmutableInstruction10x(Opcode.RETURN_VOID)); 62 63 ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null); 64 ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PUBLIC.getValue(), null, 65 methodImpl); 66 67 ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, 68 null, null, null, null, null, ImmutableList.of(method)); 69 70 DexFile dexFile = new ImmutableDexFile(Opcodes.forApi(19), ImmutableList.of(classDef)); 71 72 ClassPath classPath = ClassPath.fromClassPath(ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile, 73 15, false); 74 InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V"); 75 MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false); 76 77 Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0); 78 Assert.assertEquals(Opcode.INVOKE_VIRTUAL, deodexedInstruction.getOpcode()); 79 80 MethodReference methodReference = (MethodReference)((Instruction35c)deodexedInstruction).getReference(); 81 Assert.assertEquals(method, methodReference); 82 } 83 84 @Test 85 public void testCustomMethodInlineTable_Static() { 86 List<ImmutableInstruction> instructions = Lists.newArrayList( 87 new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), 88 new ImmutableInstruction10x(Opcode.RETURN_VOID)); 89 90 ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null); 91 ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.STATIC.getValue(), null, 92 methodImpl); 93 94 ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, 95 null, null, null, null, ImmutableList.of(method), null); 96 97 DexFile dexFile = new ImmutableDexFile(Opcodes.forApi(19), ImmutableList.of(classDef)); 98 99 ClassPath classPath = ClassPath.fromClassPath(ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile, 100 15, false); 101 InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V"); 102 MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false); 103 104 Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0); 105 Assert.assertEquals(Opcode.INVOKE_STATIC, deodexedInstruction.getOpcode()); 106 107 MethodReference methodReference = (MethodReference)((Instruction35c)deodexedInstruction).getReference(); 108 Assert.assertEquals(method, methodReference); 109 } 110 111 @Test 112 public void testCustomMethodInlineTable_Direct() { 113 List<ImmutableInstruction> instructions = Lists.newArrayList( 114 new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), 115 new ImmutableInstruction10x(Opcode.RETURN_VOID)); 116 117 ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null); 118 ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PRIVATE.getValue(), null, 119 methodImpl); 120 121 ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, 122 null, null, null, null, ImmutableList.of(method), null); 123 124 DexFile dexFile = new ImmutableDexFile(Opcodes.forApi(19), ImmutableList.of(classDef)); 125 126 ClassPath classPath = ClassPath.fromClassPath(ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile, 127 15, false); 128 InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V"); 129 MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false); 130 131 Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0); 132 Assert.assertEquals(Opcode.INVOKE_DIRECT, deodexedInstruction.getOpcode()); 133 134 MethodReference methodReference = (MethodReference)((Instruction35c)deodexedInstruction).getReference(); 135 Assert.assertEquals(method, methodReference); 136 } 137 } 138