1 /* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.invoke.util; 27 28 import java.lang.invoke.MethodType; 29 import sun.invoke.empty.Empty; 30 31 /** 32 * This class centralizes information about the JVM verifier 33 * and its requirements about type correctness. 34 * @author jrose 35 */ 36 public class VerifyType { 37 38 private VerifyType() { } // cannot instantiate 39 40 /** 41 * True if a value can be stacked as the source type and unstacked as the 42 * destination type, without violating the JVM's type consistency. 43 * <p> 44 * If both types are references, we apply the verifier's subclass check 45 * (or subtyping, if keepInterfaces). 46 * If the src type is a type guaranteed to be null (Void) it can be converted 47 * to any other reference type. 48 * <p> 49 * If both types are primitives, we apply the verifier's primitive conversions. 50 * These do not include Java conversions such as long to double, since those 51 * require computation and (in general) stack depth changes. 52 * But very simple 32-bit viewing changes, such as byte to int, 53 * are null conversions, because they do not require any computation. 54 * These conversions are from any type to a wider type up to 32 bits, 55 * as long as the conversion is not signed to unsigned (byte to char). 56 * <p> 57 * The primitive type 'void' does not interconvert with any other type, 58 * even though it is legal to drop any type from the stack and "return void". 59 * The stack effects, though are difference between void and any other type, 60 * so it is safer to report a non-trivial conversion. 61 * 62 * @param src the type of a stacked value 63 * @param dst the type by which we'd like to treat it 64 * @param keepInterfaces if false, we treat any interface as if it were Object 65 * @return whether the retyping can be done without motion or reformatting 66 */ 67 public static boolean isNullConversion(Class<?> src, Class<?> dst, boolean keepInterfaces) { 68 if (src == dst) return true; 69 // Verifier allows any interface to be treated as Object: 70 if (!keepInterfaces) { 71 if (dst.isInterface()) dst = Object.class; 72 if (src.isInterface()) src = Object.class; 73 if (src == dst) return true; // check again 74 } 75 if (isNullType(src)) return !dst.isPrimitive(); 76 if (!src.isPrimitive()) return dst.isAssignableFrom(src); 77 if (!dst.isPrimitive()) return false; 78 // Verifier allows an int to carry byte, short, char, or even boolean: 79 Wrapper sw = Wrapper.forPrimitiveType(src); 80 if (dst == int.class) return sw.isSubwordOrInt(); 81 Wrapper dw = Wrapper.forPrimitiveType(dst); 82 if (!sw.isSubwordOrInt()) return false; 83 if (!dw.isSubwordOrInt()) return false; 84 if (!dw.isSigned() && sw.isSigned()) return false; 85 return dw.bitWidth() > sw.bitWidth(); 86 } 87 88 /** 89 * Specialization of isNullConversion to reference types. 90 * @param src the type of a stacked value 91 * @param dst the reference type by which we'd like to treat it 92 * @return whether the retyping can be done without a cast 93 */ 94 public static boolean isNullReferenceConversion(Class<?> src, Class<?> dst) { 95 assert(!dst.isPrimitive()); 96 if (dst.isInterface()) return true; // verifier allows this 97 if (isNullType(src)) return true; 98 return dst.isAssignableFrom(src); 99 } 100 101 /** 102 * Is the given type java.lang.Null or an equivalent null-only type? 103 */ 104 public static boolean isNullType(Class<?> type) { 105 // Any reference statically typed as Void is guaranteed to be null. 106 // Therefore, it can be safely treated as a value of any 107 // other type that admits null, i.e., a reference type. 108 if (type == Void.class) return true; 109 // Locally known null-only class: 110 if (type == Empty.class) return true; 111 return false; 112 } 113 114 /** 115 * True if a method handle can receive a call under a slightly different 116 * method type, without moving or reformatting any stack elements. 117 * 118 * @param call the type of call being made 119 * @param recv the type of the method handle receiving the call 120 * @return whether the retyping can be done without motion or reformatting 121 */ 122 public static boolean isNullConversion(MethodType call, MethodType recv, boolean keepInterfaces) { 123 if (call == recv) return true; 124 int len = call.parameterCount(); 125 if (len != recv.parameterCount()) return false; 126 for (int i = 0; i < len; i++) 127 if (!isNullConversion(call.parameterType(i), recv.parameterType(i), keepInterfaces)) 128 return false; 129 return isNullConversion(recv.returnType(), call.returnType(), keepInterfaces); 130 } 131 132 /** 133 * Determine if the JVM verifier allows a value of type call to be 134 * passed to a formal parameter (or return variable) of type recv. 135 * Returns 1 if the verifier allows the types to match without conversion. 136 * Returns -1 if the types can be made to match by a JVM-supported adapter. 137 * Cases supported are: 138 * <ul><li>checkcast 139 * </li><li>conversion between any two integral types (but not floats) 140 * </li><li>unboxing from a wrapper to its corresponding primitive type 141 * </li><li>conversion in either direction between float and double 142 * </li></ul> 143 * (Autoboxing is not supported here; it must be done via Java code.) 144 * Returns 0 otherwise. 145 */ 146 public static int canPassUnchecked(Class<?> src, Class<?> dst) { 147 if (src == dst) 148 return 1; 149 150 if (dst.isPrimitive()) { 151 if (dst == void.class) 152 // Return anything to a caller expecting void. 153 // This is a property of the implementation, which links 154 // return values via a register rather than via a stack push. 155 // This makes it possible to ignore cleanly. 156 return 1; 157 if (src == void.class) 158 return 0; // void-to-something? 159 if (!src.isPrimitive()) 160 // Cannot pass a reference to any primitive type (exc. void). 161 return 0; 162 Wrapper sw = Wrapper.forPrimitiveType(src); 163 Wrapper dw = Wrapper.forPrimitiveType(dst); 164 if (sw.isSubwordOrInt() && dw.isSubwordOrInt()) { 165 if (sw.bitWidth() >= dw.bitWidth()) 166 return -1; // truncation may be required 167 if (!dw.isSigned() && sw.isSigned()) 168 return -1; // sign elimination may be required 169 return 1; 170 } 171 if (src == float.class || dst == float.class) { 172 if (src == double.class || dst == double.class) 173 return -1; // floating conversion may be required 174 else 175 return 0; // other primitive conversions NYI 176 } else { 177 // all fixed-point conversions are supported 178 return 0; 179 } 180 } else if (src.isPrimitive()) { 181 // Cannot pass a primitive to any reference type. 182 // (Maybe allow null.class?) 183 return 0; 184 } 185 186 // Handle reference types in the rest of the block: 187 188 // The verifier treats interfaces exactly like Object. 189 if (isNullReferenceConversion(src, dst)) 190 // pass any reference to object or an arb. interface 191 return 1; 192 // else it's a definite "maybe" (cast is required) 193 return -1; 194 } 195 196 public static boolean isSpreadArgType(Class<?> spreadArg) { 197 return spreadArg.isArray(); 198 } 199 public static Class<?> spreadArgElementType(Class<?> spreadArg, int i) { 200 return spreadArg.getComponentType(); 201 } 202 }