1 /* 2 * Copyright 2013, Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 package com.android.tools.smali.dexlib2; 32 33 import static com.android.tools.smali.dexlib2.VersionMap.NO_VERSION; 34 import static com.android.tools.smali.dexlib2.VersionMap.mapApiToArtVersion; 35 import static com.android.tools.smali.dexlib2.VersionMap.mapArtVersionToApi; 36 37 import javax.annotation.Nonnull; 38 import javax.annotation.Nullable; 39 import java.util.EnumMap; 40 import java.util.HashMap; 41 42 import com.android.tools.smali.util.UnmodifiableRangeMap; 43 44 public class Opcodes { 45 46 /** 47 * Either the api level for dalvik opcodes, or the art version for art opcodes 48 */ 49 public final int api; 50 public final int artVersion; 51 @Nonnull private final Opcode[] opcodesByValue = new Opcode[256]; 52 @Nonnull private final EnumMap<Opcode, Short> opcodeValues; 53 @Nonnull private final HashMap<String, Opcode> opcodesByName; 54 55 @Nonnull forApi(int api)56 public static Opcodes forApi(int api) { 57 return new Opcodes(api, NO_VERSION); 58 } 59 60 @Nonnull forArtVersion(int artVersion)61 public static Opcodes forArtVersion(int artVersion) { 62 return new Opcodes(NO_VERSION, artVersion); 63 } 64 65 @Nonnull forDexVersion(int dexVersion)66 public static Opcodes forDexVersion(int dexVersion) { 67 int api = VersionMap.mapDexVersionToApi(dexVersion); 68 if (api == NO_VERSION) { 69 throw new RuntimeException("Unsupported dex version " + dexVersion); 70 } 71 return new Opcodes(api, NO_VERSION); 72 } 73 74 /** 75 * @return a default Opcodes instance for when the exact Opcodes to use doesn't matter or isn't known 76 */ 77 @Nonnull getDefault()78 public static Opcodes getDefault() { 79 // The last pre-art api 80 return forApi(20); 81 } 82 Opcodes(int api, int artVersion)83 private Opcodes(int api, int artVersion) { 84 if (api >= 21) { 85 this.api = api; 86 this.artVersion = mapApiToArtVersion(api); 87 } else if (artVersion >= 0 && artVersion < 39) { 88 this.api = mapArtVersionToApi(artVersion); 89 this.artVersion = artVersion; 90 } else { 91 this.api = api; 92 this.artVersion = artVersion; 93 } 94 95 opcodeValues = new EnumMap<Opcode, Short>(Opcode.class); 96 opcodesByName = new HashMap<>(); 97 98 int version; 99 if (isArt()) { 100 version = this.artVersion; 101 } else { 102 version = this.api; 103 } 104 105 for (Opcode opcode: Opcode.values()) { 106 UnmodifiableRangeMap<Integer, Short> versionToValueMap; 107 108 if (isArt()) { 109 versionToValueMap = opcode.artVersionToValueMap; 110 } else { 111 versionToValueMap = opcode.apiToValueMap; 112 } 113 114 Short opcodeValue = versionToValueMap.get(version); 115 if (opcodeValue != null) { 116 if (!opcode.format.isPayloadFormat) { 117 opcodesByValue[opcodeValue] = opcode; 118 } 119 opcodeValues.put(opcode, opcodeValue); 120 opcodesByName.put(opcode.name.toLowerCase(), opcode); 121 } 122 } 123 } 124 125 @Nullable getOpcodeByName(@onnull String opcodeName)126 public Opcode getOpcodeByName(@Nonnull String opcodeName) { 127 return opcodesByName.get(opcodeName.toLowerCase()); 128 } 129 130 @Nullable getOpcodeByValue(int opcodeValue)131 public Opcode getOpcodeByValue(int opcodeValue) { 132 switch (opcodeValue) { 133 case 0x100: 134 return Opcode.PACKED_SWITCH_PAYLOAD; 135 case 0x200: 136 return Opcode.SPARSE_SWITCH_PAYLOAD; 137 case 0x300: 138 return Opcode.ARRAY_PAYLOAD; 139 default: 140 if (opcodeValue >= 0 && opcodeValue < opcodesByValue.length) { 141 return opcodesByValue[opcodeValue]; 142 } 143 return null; 144 } 145 } 146 147 @Nullable getOpcodeValue(@onnull Opcode opcode)148 public Short getOpcodeValue(@Nonnull Opcode opcode) { 149 return opcodeValues.get(opcode); 150 } 151 isArt()152 public boolean isArt() { 153 return artVersion != NO_VERSION; 154 } 155 } 156