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 com.google.common.collect.Maps; 34 import com.google.common.collect.RangeMap; 35 36 import javax.annotation.Nonnull; 37 import javax.annotation.Nullable; 38 import java.util.EnumMap; 39 import java.util.HashMap; 40 41 import static com.android.tools.smali.dexlib2.VersionMap.NO_VERSION; 42 import static com.android.tools.smali.dexlib2.VersionMap.mapApiToArtVersion; 43 import static com.android.tools.smali.dexlib2.VersionMap.mapArtVersionToApi; 44 45 public class Opcodes { 46 47 /** 48 * Either the api level for dalvik opcodes, or the art version for art opcodes 49 */ 50 public final int api; 51 public final int artVersion; 52 @Nonnull private final Opcode[] opcodesByValue = new Opcode[256]; 53 @Nonnull private final EnumMap<Opcode, Short> opcodeValues; 54 @Nonnull private final HashMap<String, Opcode> opcodesByName; 55 56 @Nonnull forApi(int api)57 public static Opcodes forApi(int api) { 58 return new Opcodes(api, NO_VERSION); 59 } 60 61 @Nonnull forArtVersion(int artVersion)62 public static Opcodes forArtVersion(int artVersion) { 63 return new Opcodes(NO_VERSION, artVersion); 64 } 65 66 @Nonnull forDexVersion(int dexVersion)67 public static Opcodes forDexVersion(int dexVersion) { 68 int api = VersionMap.mapDexVersionToApi(dexVersion); 69 if (api == NO_VERSION) { 70 throw new RuntimeException("Unsupported dex version " + dexVersion); 71 } 72 return new Opcodes(api, NO_VERSION); 73 } 74 75 /** 76 * @return a default Opcodes instance for when the exact Opcodes to use doesn't matter or isn't known 77 */ 78 @Nonnull getDefault()79 public static Opcodes getDefault() { 80 // The last pre-art api 81 return forApi(20); 82 } 83 Opcodes(int api, int artVersion)84 private Opcodes(int api, int artVersion) { 85 if (api >= 21) { 86 this.api = api; 87 this.artVersion = mapApiToArtVersion(api); 88 } else if (artVersion >= 0 && artVersion < 39) { 89 this.api = mapArtVersionToApi(artVersion); 90 this.artVersion = artVersion; 91 } else { 92 this.api = api; 93 this.artVersion = artVersion; 94 } 95 96 opcodeValues = new EnumMap<Opcode, Short>(Opcode.class); 97 opcodesByName = Maps.newHashMap(); 98 99 int version; 100 if (isArt()) { 101 version = this.artVersion; 102 } else { 103 version = this.api; 104 } 105 106 for (Opcode opcode: Opcode.values()) { 107 RangeMap<Integer, Short> versionToValueMap; 108 109 if (isArt()) { 110 versionToValueMap = opcode.artVersionToValueMap; 111 } else { 112 versionToValueMap = opcode.apiToValueMap; 113 } 114 115 Short opcodeValue = versionToValueMap.get(version); 116 if (opcodeValue != null) { 117 if (!opcode.format.isPayloadFormat) { 118 opcodesByValue[opcodeValue] = opcode; 119 } 120 opcodeValues.put(opcode, opcodeValue); 121 opcodesByName.put(opcode.name.toLowerCase(), opcode); 122 } 123 } 124 } 125 126 @Nullable getOpcodeByName(@onnull String opcodeName)127 public Opcode getOpcodeByName(@Nonnull String opcodeName) { 128 return opcodesByName.get(opcodeName.toLowerCase()); 129 } 130 131 @Nullable getOpcodeByValue(int opcodeValue)132 public Opcode getOpcodeByValue(int opcodeValue) { 133 switch (opcodeValue) { 134 case 0x100: 135 return Opcode.PACKED_SWITCH_PAYLOAD; 136 case 0x200: 137 return Opcode.SPARSE_SWITCH_PAYLOAD; 138 case 0x300: 139 return Opcode.ARRAY_PAYLOAD; 140 default: 141 if (opcodeValue >= 0 && opcodeValue < opcodesByValue.length) { 142 return opcodesByValue[opcodeValue]; 143 } 144 return null; 145 } 146 } 147 148 @Nullable getOpcodeValue(@onnull Opcode opcode)149 public Short getOpcodeValue(@Nonnull Opcode opcode) { 150 return opcodeValues.get(opcode); 151 } 152 isArt()153 public boolean isArt() { 154 return artVersion != NO_VERSION; 155 } 156 } 157