1 /* 2 * Copyright (c) 1997, 2016, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_INTERPRETER_LINKRESOLVER_HPP 26 #define SHARE_VM_INTERPRETER_LINKRESOLVER_HPP 27 28 #include "oops/method.hpp" 29 30 // All the necessary definitions for run-time link resolution. 31 32 // CallInfo provides all the information gathered for a particular 33 // linked call site after resolving it. A link is any reference 34 // made from within the bytecodes of a method to an object outside of 35 // that method. If the info is invalid, the link has not been resolved 36 // successfully. 37 38 class CallInfo : public StackObj { 39 public: 40 // Ways that a method call might be selected (or not) based on receiver type. 41 // Note that an invokevirtual instruction might be linked with no_dispatch, 42 // and an invokeinterface instruction might be linked with any of the three options 43 enum CallKind { 44 direct_call, // jump into resolved_method (must be concrete) 45 vtable_call, // select recv.klass.method_at_vtable(index) 46 itable_call, // select recv.klass.method_at_itable(resolved_method.holder, index) 47 unknown_kind = -1 48 }; 49 private: 50 KlassHandle _resolved_klass; // static receiver klass, resolved from a symbolic reference 51 KlassHandle _selected_klass; // dynamic receiver class (same as static, or subklass) 52 methodHandle _resolved_method; // static target method 53 methodHandle _selected_method; // dynamic (actual) target method 54 CallKind _call_kind; // kind of call (static(=bytecode static/special + 55 // others inferred), vtable, itable) 56 int _call_index; // vtable or itable index of selected class method (if any) 57 Handle _resolved_appendix; // extra argument in constant pool (if CPCE::has_appendix) 58 Handle _resolved_method_type; // MethodType (for invokedynamic and invokehandle call sites) 59 60 void set_static(KlassHandle resolved_klass, const methodHandle& resolved_method, TRAPS); 61 void set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, 62 const methodHandle& resolved_method, 63 const methodHandle& selected_method, 64 int itable_index, TRAPS); 65 void set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, 66 const methodHandle& resolved_method, 67 const methodHandle& selected_method, 68 int vtable_index, TRAPS); 69 void set_handle(const methodHandle& resolved_method, 70 Handle resolved_appendix, Handle resolved_method_type, TRAPS); 71 void set_handle(KlassHandle resolved_klass, 72 const methodHandle& resolved_method, 73 Handle resolved_appendix, Handle resolved_method_type, TRAPS); 74 void set_common(KlassHandle resolved_klass, KlassHandle selected_klass, 75 const methodHandle& resolved_method, 76 const methodHandle& selected_method, 77 CallKind kind, 78 int index, TRAPS); 79 80 friend class LinkResolver; 81 82 public: 83 CallInfo() { 84 #ifndef PRODUCT 85 _call_kind = CallInfo::unknown_kind; 86 _call_index = Method::garbage_vtable_index; 87 #endif //PRODUCT 88 } 89 90 // utility to extract an effective CallInfo from a method and an optional receiver limit 91 // does not queue the method for compilation 92 CallInfo(Method* resolved_method, Klass* resolved_klass = NULL); 93 94 KlassHandle resolved_klass() const { return _resolved_klass; } 95 KlassHandle selected_klass() const { return _selected_klass; } 96 methodHandle resolved_method() const { return _resolved_method; } 97 methodHandle selected_method() const { return _selected_method; } 98 Handle resolved_appendix() const { return _resolved_appendix; } 99 Handle resolved_method_type() const { return _resolved_method_type; } 100 101 BasicType result_type() const { return selected_method()->result_type(); } 102 CallKind call_kind() const { return _call_kind; } 103 int call_index() const { return _call_index; } 104 int vtable_index() const { 105 // Even for interface calls the vtable index could be non-negative. 106 // See CallInfo::set_interface. 107 assert(has_vtable_index() || is_statically_bound(), ""); 108 assert(call_kind() == vtable_call || call_kind() == direct_call, ""); 109 // The returned value is < 0 if the call is statically bound. 110 // But, the returned value may be >= 0 even if the kind is direct_call. 111 // It is up to the caller to decide which way to go. 112 return _call_index; 113 } 114 int itable_index() const { 115 assert(call_kind() == itable_call, ""); 116 // The returned value is always >= 0, a valid itable index. 117 return _call_index; 118 } 119 120 // debugging 121 #ifdef ASSERT 122 bool has_vtable_index() const { return _call_index >= 0 && _call_kind != CallInfo::itable_call; } 123 bool is_statically_bound() const { return _call_index == Method::nonvirtual_vtable_index; } 124 #endif //ASSERT 125 void verify() PRODUCT_RETURN; 126 void print() PRODUCT_RETURN; 127 }; 128 129 130 // Condensed information from constant pool to use to resolve the method or field. 131 // resolved_klass = specified class (i.e., static receiver class) 132 // current_klass = sending method holder (i.e., class containing the method 133 // containing the call being resolved) 134 class LinkInfo : public StackObj { 135 Symbol* _name; // extracted from JVM_CONSTANT_NameAndType 136 Symbol* _signature; 137 KlassHandle _resolved_klass; // class that the constant pool entry points to 138 KlassHandle _current_klass; // class that owns the constant pool 139 bool _check_access; 140 constantTag _tag; 141 public: 142 enum AccessCheck { 143 needs_access_check, 144 skip_access_check 145 }; 146 147 LinkInfo(const constantPoolHandle& pool, int index, TRAPS); 148 149 // Condensed information from other call sites within the vm. 150 LinkInfo(KlassHandle resolved_klass, Symbol* name, Symbol* signature, KlassHandle current_klass, 151 AccessCheck check_access = needs_access_check, 152 constantTag tag = JVM_CONSTANT_Invalid) : 153 _resolved_klass(resolved_klass), 154 _name(name), _signature(signature), _current_klass(current_klass), 155 _check_access(check_access == needs_access_check), _tag(tag) {} 156 157 // Case where we just find the method and don't check access against the current class 158 LinkInfo(KlassHandle resolved_klass, Symbol*name, Symbol* signature) : 159 _resolved_klass(resolved_klass), 160 _name(name), _signature(signature), _current_klass(NULL), 161 _check_access(false), _tag(JVM_CONSTANT_Invalid) {} 162 163 // accessors 164 Symbol* name() const { return _name; } 165 Symbol* signature() const { return _signature; } 166 KlassHandle resolved_klass() const { return _resolved_klass; } 167 KlassHandle current_klass() const { return _current_klass; } 168 constantTag tag() const { return _tag; } 169 bool check_access() const { return _check_access; } 170 char* method_string() const; 171 172 void print() PRODUCT_RETURN; 173 }; 174 175 // Link information for getfield/putfield & getstatic/putstatic bytecodes 176 // is represented using a fieldDescriptor. 177 178 // The LinkResolver is used to resolve constant-pool references at run-time. 179 // It does all necessary link-time checks & throws exceptions if necessary. 180 181 class LinkResolver: AllStatic { 182 friend class klassVtable; 183 friend class klassItable; 184 185 private: 186 187 static methodHandle lookup_method_in_klasses(const LinkInfo& link_info, 188 bool checkpolymorphism, 189 bool in_imethod_resolve, TRAPS); 190 static methodHandle lookup_method_in_interfaces(const LinkInfo& link_info, TRAPS); 191 static methodHandle lookup_polymorphic_method(const LinkInfo& link_info, 192 Handle *appendix_result_or_null, 193 Handle *method_type_result, TRAPS); 194 JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod() 195 // Not Linktime so doesn't take LinkInfo 196 static methodHandle lookup_instance_method_in_klasses ( 197 KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 198 JVMCI_ONLY(private:) 199 200 // Similar loader constraint checking functions that throw 201 // LinkageError with descriptive message. 202 static void check_method_loader_constraints(const LinkInfo& link_info, 203 const methodHandle& resolved_method, 204 const char* method_type, TRAPS); 205 static void check_field_loader_constraints(Symbol* field, Symbol* sig, 206 KlassHandle current_klass, 207 KlassHandle sel_klass, TRAPS); 208 209 static methodHandle resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS); 210 static methodHandle resolve_method (const LinkInfo& link_info, Bytecodes::Code code, TRAPS); 211 212 static methodHandle linktime_resolve_static_method (const LinkInfo& link_info, TRAPS); 213 static methodHandle linktime_resolve_special_method (const LinkInfo& link_info, TRAPS); 214 static methodHandle linktime_resolve_virtual_method (const LinkInfo& link_info, TRAPS); 215 static methodHandle linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS); 216 217 static void runtime_resolve_special_method (CallInfo& result, 218 const methodHandle& resolved_method, 219 KlassHandle resolved_klass, 220 KlassHandle current_klass, 221 bool check_access, TRAPS); 222 static void runtime_resolve_virtual_method (CallInfo& result, 223 const methodHandle& resolved_method, 224 KlassHandle resolved_klass, 225 Handle recv, 226 KlassHandle recv_klass, 227 bool check_null_and_abstract, TRAPS); 228 static void runtime_resolve_interface_method (CallInfo& result, 229 const methodHandle& resolved_method, 230 KlassHandle resolved_klass, 231 Handle recv, 232 KlassHandle recv_klass, 233 bool check_null_and_abstract, TRAPS); 234 235 static void check_field_accessability(KlassHandle ref_klass, 236 KlassHandle resolved_klass, 237 KlassHandle sel_klass, 238 const fieldDescriptor& fd, TRAPS); 239 static void check_method_accessability(KlassHandle ref_klass, 240 KlassHandle resolved_klass, 241 KlassHandle sel_klass, 242 const methodHandle& sel_method, TRAPS); 243 244 // runtime resolving from constant pool 245 static void resolve_invokestatic (CallInfo& result, 246 const constantPoolHandle& pool, int index, TRAPS); 247 static void resolve_invokespecial (CallInfo& result, 248 const constantPoolHandle& pool, int index, TRAPS); 249 static void resolve_invokevirtual (CallInfo& result, Handle recv, 250 const constantPoolHandle& pool, int index, TRAPS); 251 static void resolve_invokeinterface(CallInfo& result, Handle recv, 252 const constantPoolHandle& pool, int index, TRAPS); 253 static void resolve_invokedynamic (CallInfo& result, 254 const constantPoolHandle& pool, int index, TRAPS); 255 static void resolve_invokehandle (CallInfo& result, 256 const constantPoolHandle& pool, int index, TRAPS); 257 public: 258 // constant pool resolving 259 static void check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS); 260 261 // static resolving calls (will not run any Java code); 262 // used only from Bytecode_invoke::static_target 263 static methodHandle resolve_method_statically(Bytecodes::Code code, 264 const constantPoolHandle& pool, 265 int index, TRAPS); 266 267 static void resolve_field_access(fieldDescriptor& result, 268 const constantPoolHandle& pool, 269 int index, Bytecodes::Code byte, TRAPS); 270 static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info, 271 Bytecodes::Code access_kind, 272 bool initialize_class, TRAPS); 273 274 static void resolve_static_call (CallInfo& result, 275 const LinkInfo& link_info, 276 bool initialize_klass, TRAPS); 277 static void resolve_special_call (CallInfo& result, 278 const LinkInfo& link_info, 279 TRAPS); 280 static void resolve_virtual_call (CallInfo& result, Handle recv, KlassHandle recv_klass, 281 const LinkInfo& link_info, 282 bool check_null_and_abstract, TRAPS); 283 static void resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, 284 const LinkInfo& link_info, 285 bool check_null_and_abstract, TRAPS); 286 static void resolve_handle_call (CallInfo& result, 287 const LinkInfo& link_info, TRAPS); 288 static void resolve_dynamic_call (CallInfo& result, Handle bootstrap_specifier, 289 Symbol* method_name, Symbol* method_signature, 290 KlassHandle current_klass, TRAPS); 291 292 // same as above for compile-time resolution; but returns null handle instead of throwing 293 // an exception on error also, does not initialize klass (i.e., no side effects) 294 static methodHandle resolve_virtual_call_or_null (KlassHandle receiver_klass, 295 const LinkInfo& link_info); 296 static methodHandle resolve_interface_call_or_null(KlassHandle receiver_klass, 297 const LinkInfo& link_info); 298 static methodHandle resolve_static_call_or_null (const LinkInfo& link_info); 299 static methodHandle resolve_special_call_or_null (const LinkInfo& link_info); 300 301 static int vtable_index_of_interface_method(KlassHandle klass, const methodHandle& resolved_method); 302 303 // same as above for compile-time resolution; returns vtable_index if current_klass if linked 304 static int resolve_virtual_vtable_index (KlassHandle receiver_klass, 305 const LinkInfo& link_info); 306 307 // static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful) 308 static methodHandle linktime_resolve_virtual_method_or_null (const LinkInfo& link_info); 309 static methodHandle linktime_resolve_interface_method_or_null(const LinkInfo& link_info); 310 311 // runtime resolving from constant pool 312 static void resolve_invoke(CallInfo& result, Handle recv, 313 const constantPoolHandle& pool, int index, 314 Bytecodes::Code byte, TRAPS); 315 316 // runtime resolving from attached method 317 static void resolve_invoke(CallInfo& result, Handle& recv, 318 const methodHandle& attached_method, 319 Bytecodes::Code byte, TRAPS); 320 }; 321 #endif // SHARE_VM_INTERPRETER_LINKRESOLVER_HPP