InlineMethodResolver.java revision 9531284b1b6a29371ae8d8e6cfe3e1f1bfe23296
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.iface.Method;
36import org.jf.dexlib2.iface.instruction.InlineIndexInstruction;
37import org.jf.dexlib2.iface.instruction.VariableRegisterInstruction;
38import org.jf.dexlib2.immutable.ImmutableMethod;
39import org.jf.dexlib2.immutable.ImmutableMethodParameter;
40import org.jf.dexlib2.immutable.util.ParamUtil;
41
42import javax.annotation.Nonnull;
43
44public 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.PRIVATE.value;
50    public static final int DIRECT = 0x2; // AccessFlags.PRIVATE.value;
51
52    @Nonnull
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
63    protected InlineMethodResolver() {
64    }
65
66    @Nonnull
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
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
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
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
119
120        public InlineMethodResolver_version36() {
121            //The 5th and 6th entries differ between froyo and gingerbread. We have to look at the parameters being
122            //passed to distinguish between them.
123
124            //froyo
125            indexOfIMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "I", "I");
126            indexOfIIMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "II", "I");
127
128            //gingerbread
129            fastIndexOfMethod = inlineMethod(DIRECT, "Ljava/lang/String;", "fastIndexOf", "II", "I");
130            isEmptyMethod = inlineMethod(VIRTUAL, "Ljava/lang/String;", "isEmpty", "", "Z");
131
132            inlineMethods = new Method[] {
133                inlineMethod(STATIC, "Lorg/apache/harmony/dalvik/NativeTestTarget;", "emptyInlineMethod", "", "V"),
134                inlineMethod(VIRTUAL, "Ljava/lang/String;", "charAt", "I", "C"),
135                inlineMethod(VIRTUAL, "Ljava/lang/String;", "compareTo", "Ljava/lang/String;", "I"),
136                inlineMethod(VIRTUAL, "Ljava/lang/String;", "equals", "Ljava/lang/Object;", "Z"),
137                //froyo: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "I", "I"),
138                //gingerbread: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "fastIndexOf", "II", "I"),
139                null,
140                //froyo: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "indexOf", "II", "I"),
141                //gingerbread: deodexUtil.new InlineMethod(VIRTUAL, "Ljava/lang/String;", "isEmpty", "", "Z"),
142                null,
143                inlineMethod(VIRTUAL, "Ljava/lang/String;", "length", "", "I"),
144                inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "I", "I"),
145                inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "J", "J"),
146                inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "F", "F"),
147                inlineMethod(STATIC, "Ljava/lang/Math;", "abs", "D", "D"),
148                inlineMethod(STATIC, "Ljava/lang/Math;", "min", "II", "I"),
149                inlineMethod(STATIC, "Ljava/lang/Math;", "max", "II", "I"),
150                inlineMethod(STATIC, "Ljava/lang/Math;", "sqrt", "D", "D"),
151                inlineMethod(STATIC, "Ljava/lang/Math;", "cos", "D", "D"),
152                inlineMethod(STATIC, "Ljava/lang/Math;", "sin", "D", "D"),
153                inlineMethod(STATIC, "Ljava/lang/Float;", "floatToIntBits", "F", "I"),
154                inlineMethod(STATIC, "Ljava/lang/Float;", "floatToRawIntBits", "F", "I"),
155                inlineMethod(STATIC, "Ljava/lang/Float;", "intBitsToFloat", "I", "F"),
156                inlineMethod(STATIC, "Ljava/lang/Double;", "doubleToLongBits", "D", "J"),
157                inlineMethod(STATIC, "Ljava/lang/Double;", "doubleToRawLongBits", "D", "J"),
158                inlineMethod(STATIC, "Ljava/lang/Double;", "longBitsToDouble", "J", "D"),
159                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "I", "I"),
160                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "J", "J"),
161                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "F", "F"),
162                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "abs", "D", "D"),
163                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "min", "II", "I"),
164                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "max", "II", "I"),
165                inlineMethod(STATIC, "Ljava/lang/StrictMath;", "sqrt", "D", "D"),
166            };
167        }
168
169        @Override
170        @Nonnull
171        public Method resolveExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) {
172            InlineIndexInstruction instruction = (InlineIndexInstruction)analyzedInstruction.instruction;
173            int inlineIndex = instruction.getInlineIndex();
174
175            if (inlineIndex < 0 || inlineIndex >= inlineMethods.length) {
176                throw new RuntimeException("Invalid method index: " + inlineIndex);
177            }
178
179            if (inlineIndex == 4) {
180                int parameterCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
181                if (parameterCount == 2) {
182                    return indexOfIMethod;
183                } else if (parameterCount == 3) {
184                    return fastIndexOfMethod;
185                } else {
186                    throw new RuntimeException("Could not determine the correct inline method to use");
187                }
188            } else if (inlineIndex == 5) {
189                int parameterCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
190                if (parameterCount == 3) {
191                    return indexOfIIMethod;
192                } else if (parameterCount == 1) {
193                    return isEmptyMethod;
194                } else {
195                    throw new RuntimeException("Could not determine the correct inline method to use");
196                }
197            }
198
199            return inlineMethods[inlineIndex];
200        }
201    }
202}
203