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 org.jf.dexlib2.iface.Method; 36 import org.jf.dexlib2.iface.instruction.InlineIndexInstruction; 37 import org.jf.dexlib2.iface.instruction.VariableRegisterInstruction; 38 import org.jf.dexlib2.immutable.ImmutableMethod; 39 import org.jf.dexlib2.immutable.ImmutableMethodParameter; 40 import org.jf.dexlib2.immutable.util.ParamUtil; 41 42 import javax.annotation.Nonnull; 43 44 public abstract class InlineMethodResolver { 45 // These are the possible values for the accessFlag field on a resolved inline method 46 // We can't use, e.g. AccessFlags.STATIC.value, because we need them to be a constant in order to use them as cases 47 // in switch statements 48 public static final int STATIC = 0x8; // AccessFlags.STATIC.value; 49 public static final int VIRTUAL = 0x1; // AccessFlags.PUBLIC.value; 50 public static final int DIRECT = 0x2; // AccessFlags.PRIVATE.value; 51 52 @Nonnull createInlineMethodResolver(int odexVersion)53 public static InlineMethodResolver createInlineMethodResolver(int odexVersion) { 54 if (odexVersion == 35) { 55 return new InlineMethodResolver_version35(); 56 } else if (odexVersion == 36) { 57 return new InlineMethodResolver_version36(); 58 } else { 59 throw new RuntimeException(String.format("odex version %d is not supported yet", odexVersion)); 60 } 61 } 62 InlineMethodResolver()63 protected InlineMethodResolver() { 64 } 65 66 @Nonnull inlineMethod(int accessFlags, @Nonnull String cls, @Nonnull String name, @Nonnull String params, @Nonnull String returnType)67 private static Method inlineMethod(int accessFlags, @Nonnull String cls, @Nonnull String name, 68 @Nonnull String params, @Nonnull String returnType) { 69 ImmutableList<ImmutableMethodParameter> paramList = ImmutableList.copyOf(ParamUtil.parseParamString(params)); 70 return new ImmutableMethod(cls, name, paramList, returnType, accessFlags, null, null); 71 } 72 resolveExecuteInline(@onnull AnalyzedInstruction instruction)73 @Nonnull public abstract Method resolveExecuteInline(@Nonnull AnalyzedInstruction instruction); 74 75 private static class InlineMethodResolver_version35 extends InlineMethodResolver 76 { 77 private final Method[] inlineMethods; 78 InlineMethodResolver_version35()79 public InlineMethodResolver_version35() { 80 inlineMethods = new Method[] { 81 inlineMethod(STATIC, "Lorg/apache/harmony/dalvik/NativeTestTarget;", "emptyInlineMethod", "", "V"), 82 inlineMethod(VIRTUAL, "Ljava/lang/String;", "charAt", "I", "C"), 83 inlineMethod(VIRTUAL, "Ljava/lang/String;", "compareTo", "Ljava/lang/String;", "I"), 84 inlineMethod(VIRTUAL, "Ljava/lang/String;", "equals", "Ljava/lang/Object;", "Z"), 85 inlineMethod(VIRTUAL, "Ljava/lang/String;", "length", "", "I"), 86 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "I", "I"), 87 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "J", "J"), 88 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "F", "F"), 89 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "D", "D"), 90 inlineMethod(STATIC, "Ljava/lang/Math;", "min", "II", "I"), 91 inlineMethod(STATIC, "Ljava/lang/Math;", "max", "II", "I"), 92 inlineMethod(STATIC, "Ljava/lang/Math;", "sqrt", "D", "D"), 93 inlineMethod(STATIC, "Ljava/lang/Math;", "cos", "D", "D"), 94 inlineMethod(STATIC, "Ljava/lang/Math;", "sin", "D", "D") 95 }; 96 } 97 98 @Override 99 @Nonnull resolveExecuteInline(@onnull AnalyzedInstruction analyzedInstruction)100 public Method resolveExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) { 101 InlineIndexInstruction instruction = (InlineIndexInstruction)analyzedInstruction.instruction; 102 int inlineIndex = instruction.getInlineIndex(); 103 104 if (inlineIndex < 0 || inlineIndex >= inlineMethods.length) { 105 throw new RuntimeException("Invalid inline index: " + inlineIndex); 106 } 107 return inlineMethods[inlineIndex]; 108 } 109 } 110 111 private static class InlineMethodResolver_version36 extends InlineMethodResolver 112 { 113 private final Method[] inlineMethods; 114 private final Method indexOfIMethod; 115 private final Method indexOfIIMethod; 116 private final Method fastIndexOfMethod; 117 private final Method isEmptyMethod; 118 InlineMethodResolver_version36()119 public InlineMethodResolver_version36() { 120 //The 5th and 6th entries differ between froyo and gingerbread. We have to look at the parameters being 121 //passed to distinguish between them. 122 123 //froyo 124 indexOfIMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "I", "I"); 125 indexOfIIMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "II", "I"); 126 127 //gingerbread 128 fastIndexOfMethod = inlineMethod(DIRECT, "Ljava/lang/String;", "fastIndexOf", "II", "I"); 129 isEmptyMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "isEmpty", "", "Z"); 130 131 inlineMethods = new Method[] { 132 inlineMethod(STATIC, "Lorg/apache/harmony/dalvik/NativeTestTarget;", "emptyInlineMethod", "", "V"), 133 inlineMethod(VIRTUAL, "Ljava/lang/String;", "charAt", "I", "C"), 134 inlineMethod(VIRTUAL, "Ljava/lang/String;", "compareTo", "Ljava/lang/String;", "I"), 135 inlineMethod(VIRTUAL, "Ljava/lang/String;", "equals", "Ljava/lang/Object;", "Z"), 136 //froyo: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "I", "I"), 137 //gingerbread: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "fastIndexOf", "II", "I"), 138 null, 139 //froyo: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "II", "I"), 140 //gingerbread: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "isEmpty", "", "Z"), 141 null, 142 inlineMethod(VIRTUAL, "Ljava/lang/String;", "length", "", "I"), 143 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "I", "I"), 144 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "J", "J"), 145 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "F", "F"), 146 inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "D", "D"), 147 inlineMethod(STATIC, "Ljava/lang/Math;", "min", "II", "I"), 148 inlineMethod(STATIC, "Ljava/lang/Math;", "max", "II", "I"), 149 inlineMethod(STATIC, "Ljava/lang/Math;", "sqrt", "D", "D"), 150 inlineMethod(STATIC, "Ljava/lang/Math;", "cos", "D", "D"), 151 inlineMethod(STATIC, "Ljava/lang/Math;", "sin", "D", "D"), 152 inlineMethod(STATIC, "Ljava/lang/Float;", "floatToIntBits", "F", "I"), 153 inlineMethod(STATIC, "Ljava/lang/Float;", "floatToRawIntBits", "F", "I"), 154 inlineMethod(STATIC, "Ljava/lang/Float;", "intBitsToFloat", "I", "F"), 155 inlineMethod(STATIC, "Ljava/lang/Double;", "doubleToLongBits", "D", "J"), 156 inlineMethod(STATIC, "Ljava/lang/Double;", "doubleToRawLongBits", "D", "J"), 157 inlineMethod(STATIC, "Ljava/lang/Double;", "longBitsToDouble", "J", "D"), 158 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "I", "I"), 159 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "J", "J"), 160 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "F", "F"), 161 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "D", "D"), 162 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "min", "II", "I"), 163 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "max", "II", "I"), 164 inlineMethod(STATIC, "Ljava/lang/StrictMath;", "sqrt", "D", "D"), 165 }; 166 } 167 168 @Override 169 @Nonnull resolveExecuteInline(@onnull AnalyzedInstruction analyzedInstruction)170 public Method resolveExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) { 171 InlineIndexInstruction instruction = (InlineIndexInstruction)analyzedInstruction.instruction; 172 int inlineIndex = instruction.getInlineIndex(); 173 174 if (inlineIndex < 0 || inlineIndex >= inlineMethods.length) { 175 throw new RuntimeException("Invalid method index: " + inlineIndex); 176 } 177 178 if (inlineIndex == 4) { 179 int parameterCount = ((VariableRegisterInstruction)instruction).getRegisterCount(); 180 if (parameterCount == 2) { 181 return indexOfIMethod; 182 } else if (parameterCount == 3) { 183 return fastIndexOfMethod; 184 } else { 185 throw new RuntimeException("Could not determine the correct inline method to use"); 186 } 187 } else if (inlineIndex == 5) { 188 int parameterCount = ((VariableRegisterInstruction)instruction).getRegisterCount(); 189 if (parameterCount == 3) { 190 return indexOfIIMethod; 191 } else if (parameterCount == 1) { 192 return isEmptyMethod; 193 } else { 194 throw new RuntimeException("Could not determine the correct inline method to use"); 195 } 196 } 197 198 return inlineMethods[inlineIndex]; 199 } 200 } 201 } 202