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.android.tools.smali.util.ExceptionWithContext; 34 35 import javax.annotation.Nullable; 36 import java.util.HashMap; 37 38 public class VerificationError { 39 public static final int GENERIC = 1; 40 public static final int NO_SUCH_CLASS = 2; 41 public static final int NO_SUCH_FIELD = 3; 42 public static final int NO_SUCH_METHOD = 4; 43 public static final int ILLEGAL_CLASS_ACCESS = 5; 44 public static final int ILLEGAL_FIELD_ACCESS = 6; 45 public static final int ILLEGAL_METHOD_ACCESS = 7; 46 public static final int CLASS_CHANGE_ERROR = 8; 47 public static final int INSTANTIATION_ERROR = 9; 48 49 private static final HashMap<String, Integer> verificationErrorNames = new HashMap<>(); 50 51 static { 52 verificationErrorNames.put("generic-error", GENERIC); 53 verificationErrorNames.put("no-such-class", NO_SUCH_CLASS); 54 verificationErrorNames.put("no-such-field", NO_SUCH_FIELD); 55 verificationErrorNames.put("no-such-method", NO_SUCH_METHOD); 56 verificationErrorNames.put("illegal-class-access", ILLEGAL_CLASS_ACCESS); 57 verificationErrorNames.put("illegal-field-access", ILLEGAL_FIELD_ACCESS); 58 verificationErrorNames.put("illegal-method-access", ILLEGAL_METHOD_ACCESS); 59 verificationErrorNames.put("class-change-error", CLASS_CHANGE_ERROR); 60 verificationErrorNames.put("instantiation-error", INSTANTIATION_ERROR); 61 } 62 63 @Nullable getVerificationErrorName(int verificationError)64 public static String getVerificationErrorName(int verificationError) { 65 switch (verificationError) { 66 case GENERIC: 67 return "generic-error"; 68 case NO_SUCH_CLASS: 69 return "no-such-class"; 70 case NO_SUCH_FIELD: 71 return "no-such-field"; 72 case NO_SUCH_METHOD: 73 return "no-such-method"; 74 case ILLEGAL_CLASS_ACCESS: 75 return "illegal-class-access"; 76 case ILLEGAL_FIELD_ACCESS: 77 return "illegal-field-access"; 78 case ILLEGAL_METHOD_ACCESS: 79 return "illegal-method-access"; 80 case CLASS_CHANGE_ERROR: 81 return "class-change-error"; 82 case INSTANTIATION_ERROR: 83 return "instantiation-error"; 84 default: 85 return null; 86 } 87 } 88 getVerificationError(String verificationError)89 public static int getVerificationError(String verificationError) { 90 Integer ret = verificationErrorNames.get(verificationError); 91 if (ret == null) { 92 throw new ExceptionWithContext("Invalid verification error: %s", verificationError); 93 } 94 return ret; 95 } 96 isValidVerificationError(int verificationError)97 public static boolean isValidVerificationError(int verificationError) { 98 return verificationError > 0 && verificationError < 10; 99 } 100 } 101