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