1 /* 2 * Copyright (c) 1997, 2019, 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_INTERPRETER_LINKRESOLVER_HPP 26 #define SHARE_INTERPRETER_LINKRESOLVER_HPP 27 28 #include "interpreter/bootstrapInfo.hpp" 29 #include "oops/method.hpp" 30 31 // All the necessary definitions for run-time link resolution. 32 33 // CallInfo provides all the information gathered for a particular 34 // linked call site after resolving it. A link is any reference 35 // made from within the bytecodes of a method to an object outside of 36 // that method. If the info is invalid, the link has not been resolved 37 // successfully. 38 39 class CallInfo : public StackObj { 40 public: 41 // Ways that a method call might be selected (or not) based on receiver type. 42 // Note that an invokevirtual instruction might be linked with no_dispatch, 43 // and an invokeinterface instruction might be linked with any of the three options 44 enum CallKind { 45 direct_call, // jump into resolved_method (must be concrete) 46 vtable_call, // select recv.klass.method_at_vtable(index) 47 itable_call, // select recv.klass.method_at_itable(resolved_method.holder, index) 48 unknown_kind = -1 49 }; 50 private: 51 Klass* _resolved_klass; // static receiver klass, resolved from a symbolic reference 52 Klass* _selected_klass; // dynamic receiver class (same as static, or subklass) 53 methodHandle _resolved_method; // static target method 54 methodHandle _selected_method; // dynamic (actual) target method 55 CallKind _call_kind; // kind of call (static(=bytecode static/special + 56 // others inferred), vtable, itable) 57 int _call_index; // vtable or itable index of selected class method (if any) 58 Handle _resolved_appendix; // extra argument in constant pool (if CPCE::has_appendix) 59 Handle _resolved_method_name; // Object holding the ResolvedMethodName 60 61 void set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS); 62 void set_interface(Klass* resolved_klass, Klass* selected_klass, 63 const methodHandle& resolved_method, 64 const methodHandle& selected_method, 65 int itable_index, TRAPS); 66 void set_virtual(Klass* resolved_klass, Klass* selected_klass, 67 const methodHandle& resolved_method, 68 const methodHandle& selected_method, 69 int vtable_index, TRAPS); 70 void set_handle(const methodHandle& resolved_method, 71 Handle resolved_appendix, TRAPS); 72 void set_handle(Klass* resolved_klass, 73 const methodHandle& resolved_method, 74 Handle resolved_appendix, TRAPS); 75 void set_common(Klass* resolved_klass, Klass* selected_klass, 76 const methodHandle& resolved_method, 77 const methodHandle& selected_method, 78 CallKind kind, 79 int index, TRAPS); 80 81 friend class BootstrapInfo; 82 friend class LinkResolver; 83 84 public: CallInfo()85 CallInfo() { 86 #ifndef PRODUCT 87 _call_kind = CallInfo::unknown_kind; 88 _call_index = Method::garbage_vtable_index; 89 #endif //PRODUCT 90 } 91 92 // utility to extract an effective CallInfo from a method and an optional receiver limit 93 // does not queue the method for compilation. This also creates a ResolvedMethodName 94 // object for the resolved_method. 95 CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS); 96 resolved_klass() const97 Klass* resolved_klass() const { return _resolved_klass; } selected_klass() const98 Klass* selected_klass() const { return _selected_klass; } resolved_method() const99 methodHandle resolved_method() const { return _resolved_method; } selected_method() const100 methodHandle selected_method() const { return _selected_method; } resolved_appendix() const101 Handle resolved_appendix() const { return _resolved_appendix; } resolved_method_name() const102 Handle resolved_method_name() const { return _resolved_method_name; } 103 // Materialize a java.lang.invoke.ResolvedMethodName for this resolved_method 104 void set_resolved_method_name(TRAPS); 105 result_type() const106 BasicType result_type() const { return selected_method()->result_type(); } call_kind() const107 CallKind call_kind() const { return _call_kind; } call_index() const108 int call_index() const { return _call_index; } vtable_index() const109 int vtable_index() const { 110 // Even for interface calls the vtable index could be non-negative. 111 // See CallInfo::set_interface. 112 assert(has_vtable_index() || is_statically_bound(), ""); 113 assert(call_kind() == vtable_call || call_kind() == direct_call, ""); 114 // The returned value is < 0 if the call is statically bound. 115 // But, the returned value may be >= 0 even if the kind is direct_call. 116 // It is up to the caller to decide which way to go. 117 return _call_index; 118 } itable_index() const119 int itable_index() const { 120 assert(call_kind() == itable_call, ""); 121 // The returned value is always >= 0, a valid itable index. 122 return _call_index; 123 } 124 125 // debugging 126 #ifdef ASSERT has_vtable_index() const127 bool has_vtable_index() const { return _call_index >= 0 && _call_kind != CallInfo::itable_call; } is_statically_bound() const128 bool is_statically_bound() const { return _call_index == Method::nonvirtual_vtable_index; } 129 #endif //ASSERT 130 void verify() PRODUCT_RETURN; 131 void print() PRODUCT_RETURN; 132 }; 133 134 135 // Condensed information from constant pool to use to resolve the method or field. 136 // resolved_klass = specified class (i.e., static receiver class) 137 // current_klass = sending method holder (i.e., class containing the method 138 // containing the call being resolved) 139 // current_method = sending method (relevant for field resolution) 140 class LinkInfo : public StackObj { 141 Symbol* _name; // extracted from JVM_CONSTANT_NameAndType 142 Symbol* _signature; 143 Klass* _resolved_klass; // class that the constant pool entry points to 144 Klass* _current_klass; // class that owns the constant pool 145 methodHandle _current_method; // sending method 146 bool _check_access; 147 constantTag _tag; 148 149 public: 150 enum AccessCheck { 151 needs_access_check, 152 skip_access_check 153 }; 154 155 LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS); 156 LinkInfo(const constantPoolHandle& pool, int index, TRAPS); 157 158 // Condensed information from other call sites within the vm. LinkInfo(Klass * resolved_klass,Symbol * name,Symbol * signature,Klass * current_klass,AccessCheck check_access=needs_access_check,constantTag tag=JVM_CONSTANT_Invalid)159 LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, Klass* current_klass, 160 AccessCheck check_access = needs_access_check, 161 constantTag tag = JVM_CONSTANT_Invalid) : 162 _name(name), 163 _signature(signature), _resolved_klass(resolved_klass), _current_klass(current_klass), _current_method(methodHandle()), 164 _check_access(check_access == needs_access_check), _tag(tag) {} 165 LinkInfo(Klass * resolved_klass,Symbol * name,Symbol * signature,const methodHandle & current_method,AccessCheck check_access=needs_access_check,constantTag tag=JVM_CONSTANT_Invalid)166 LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, const methodHandle& current_method, 167 AccessCheck check_access = needs_access_check, 168 constantTag tag = JVM_CONSTANT_Invalid) : 169 _name(name), 170 _signature(signature), _resolved_klass(resolved_klass), _current_klass(current_method->method_holder()), _current_method(current_method), 171 _check_access(check_access == needs_access_check), _tag(tag) {} 172 173 // Case where we just find the method and don't check access against the current class LinkInfo(Klass * resolved_klass,Symbol * name,Symbol * signature)174 LinkInfo(Klass* resolved_klass, Symbol*name, Symbol* signature) : 175 _name(name), 176 _signature(signature), _resolved_klass(resolved_klass), _current_klass(NULL), _current_method(methodHandle()), 177 _check_access(false), _tag(JVM_CONSTANT_Invalid) {} 178 179 // accessors name() const180 Symbol* name() const { return _name; } signature() const181 Symbol* signature() const { return _signature; } resolved_klass() const182 Klass* resolved_klass() const { return _resolved_klass; } current_klass() const183 Klass* current_klass() const { return _current_klass; } current_method() const184 methodHandle current_method() const { return _current_method; } tag() const185 constantTag tag() const { return _tag; } check_access() const186 bool check_access() const { return _check_access; } 187 188 void print() PRODUCT_RETURN; 189 }; 190 191 // Link information for getfield/putfield & getstatic/putstatic bytecodes 192 // is represented using a fieldDescriptor. 193 194 // The LinkResolver is used to resolve constant-pool references at run-time. 195 // It does all necessary link-time checks & throws exceptions if necessary. 196 197 class LinkResolver: AllStatic { 198 friend class klassVtable; 199 friend class klassItable; 200 201 private: 202 203 static Method* lookup_method_in_klasses(const LinkInfo& link_info, 204 bool checkpolymorphism, 205 bool in_imethod_resolve); 206 static Method* lookup_method_in_interfaces(const LinkInfo& link_info); 207 208 static methodHandle lookup_polymorphic_method(const LinkInfo& link_info, 209 Handle *appendix_result_or_null, TRAPS); 210 JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod() 211 // Not Linktime so doesn't take LinkInfo 212 static methodHandle lookup_instance_method_in_klasses (Klass* klass, Symbol* name, Symbol* signature, 213 Klass::PrivateLookupMode private_mode, TRAPS); 214 JVMCI_ONLY(private:) 215 216 // Similar loader constraint checking functions that throw 217 // LinkageError with descriptive message. 218 static void check_method_loader_constraints(const LinkInfo& link_info, 219 const methodHandle& resolved_method, 220 const char* method_type, TRAPS); 221 static void check_field_loader_constraints(Symbol* field, Symbol* sig, 222 Klass* current_klass, 223 Klass* sel_klass, TRAPS); 224 225 static methodHandle resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS); 226 static methodHandle resolve_method (const LinkInfo& link_info, Bytecodes::Code code, TRAPS); 227 228 static methodHandle linktime_resolve_static_method (const LinkInfo& link_info, TRAPS); 229 static methodHandle linktime_resolve_special_method (const LinkInfo& link_info, TRAPS); 230 static methodHandle linktime_resolve_virtual_method (const LinkInfo& link_info, TRAPS); 231 static methodHandle linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS); 232 233 static void runtime_resolve_special_method (CallInfo& result, 234 const LinkInfo& link_info, 235 const methodHandle& resolved_method, 236 Handle recv, TRAPS); 237 238 static void runtime_resolve_virtual_method (CallInfo& result, 239 const methodHandle& resolved_method, 240 Klass* resolved_klass, 241 Handle recv, 242 Klass* recv_klass, 243 bool check_null_and_abstract, TRAPS); 244 static void runtime_resolve_interface_method (CallInfo& result, 245 const methodHandle& resolved_method, 246 Klass* resolved_klass, 247 Handle recv, 248 Klass* recv_klass, 249 bool check_null_and_abstract, TRAPS); 250 251 static void check_field_accessability(Klass* ref_klass, 252 Klass* resolved_klass, 253 Klass* sel_klass, 254 const fieldDescriptor& fd, TRAPS); 255 static void check_method_accessability(Klass* ref_klass, 256 Klass* resolved_klass, 257 Klass* sel_klass, 258 const methodHandle& sel_method, TRAPS); 259 260 // runtime resolving from constant pool 261 static void resolve_invokestatic (CallInfo& result, 262 const constantPoolHandle& pool, int index, TRAPS); 263 static void resolve_invokespecial (CallInfo& result, Handle recv, 264 const constantPoolHandle& pool, int index, TRAPS); 265 static void resolve_invokevirtual (CallInfo& result, Handle recv, 266 const constantPoolHandle& pool, int index, TRAPS); 267 static void resolve_invokeinterface(CallInfo& result, Handle recv, 268 const constantPoolHandle& pool, int index, TRAPS); 269 static void resolve_invokedynamic (CallInfo& result, 270 const constantPoolHandle& pool, int index, TRAPS); 271 static void resolve_invokehandle (CallInfo& result, 272 const constantPoolHandle& pool, int index, TRAPS); 273 public: 274 // constant pool resolving 275 static void check_klass_accessability(Klass* ref_klass, Klass* sel_klass, 276 bool fold_type_to_class, TRAPS); 277 // The optional 'fold_type_to_class' means that a derived type (array) 278 // is first converted to the class it is derived from (element type). 279 // If this element type is not a class, then the check passes quietly. 280 // This is usually what is needed, but a few existing uses might break 281 // if this flag were always turned on. FIXME: See if it can be, always. check_klass_accessability(Klass * ref_klass,Klass * sel_klass,TRAPS)282 static void check_klass_accessability(Klass* ref_klass, Klass* sel_klass, TRAPS) { 283 return check_klass_accessability(ref_klass, sel_klass, false, THREAD); 284 } 285 286 // static resolving calls (will not run any Java code); 287 // used only from Bytecode_invoke::static_target 288 static methodHandle resolve_method_statically(Bytecodes::Code code, 289 const constantPoolHandle& pool, 290 int index, TRAPS); 291 292 static void resolve_field_access(fieldDescriptor& result, 293 const constantPoolHandle& pool, 294 int index, 295 const methodHandle& method, 296 Bytecodes::Code byte, TRAPS); 297 static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info, 298 Bytecodes::Code access_kind, 299 bool initialize_class, TRAPS); 300 301 static void resolve_static_call (CallInfo& result, 302 const LinkInfo& link_info, 303 bool initialize_klass, TRAPS); 304 static void resolve_special_call (CallInfo& result, 305 Handle recv, 306 const LinkInfo& link_info, 307 TRAPS); 308 static void resolve_virtual_call (CallInfo& result, Handle recv, Klass* recv_klass, 309 const LinkInfo& link_info, 310 bool check_null_and_abstract, TRAPS); 311 static void resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass, 312 const LinkInfo& link_info, 313 bool check_null_and_abstract, TRAPS); 314 static void resolve_handle_call (CallInfo& result, 315 const LinkInfo& link_info, TRAPS); 316 static void resolve_dynamic_call (CallInfo& result, 317 BootstrapInfo& bootstrap_specifier, TRAPS); 318 319 // same as above for compile-time resolution; but returns null handle instead of throwing 320 // an exception on error also, does not initialize klass (i.e., no side effects) 321 static methodHandle resolve_virtual_call_or_null (Klass* receiver_klass, 322 const LinkInfo& link_info); 323 static methodHandle resolve_interface_call_or_null(Klass* receiver_klass, 324 const LinkInfo& link_info); 325 static methodHandle resolve_static_call_or_null (const LinkInfo& link_info); 326 static methodHandle resolve_special_call_or_null (const LinkInfo& link_info); 327 328 static int vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method); 329 330 // same as above for compile-time resolution; returns vtable_index if current_klass if linked 331 static int resolve_virtual_vtable_index (Klass* receiver_klass, 332 const LinkInfo& link_info); 333 334 // static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful) 335 static methodHandle linktime_resolve_virtual_method_or_null (const LinkInfo& link_info); 336 static methodHandle linktime_resolve_interface_method_or_null(const LinkInfo& link_info); 337 338 // runtime resolving from constant pool 339 static void resolve_invoke(CallInfo& result, Handle recv, 340 const constantPoolHandle& pool, int index, 341 Bytecodes::Code byte, TRAPS); 342 343 // runtime resolving from attached method 344 static void resolve_invoke(CallInfo& result, Handle& recv, 345 const methodHandle& attached_method, 346 Bytecodes::Code byte, TRAPS); 347 348 public: 349 // Only resolved method known. throw_abstract_method_error(const methodHandle & resolved_method,TRAPS)350 static void throw_abstract_method_error(const methodHandle& resolved_method, TRAPS) { 351 throw_abstract_method_error(resolved_method, NULL, NULL, CHECK); 352 } 353 // Resolved method and receiver klass know. throw_abstract_method_error(const methodHandle & resolved_method,Klass * recv_klass,TRAPS)354 static void throw_abstract_method_error(const methodHandle& resolved_method, Klass *recv_klass, TRAPS) { 355 throw_abstract_method_error(resolved_method, NULL, recv_klass, CHECK); 356 } 357 // Selected method is abstract. 358 static void throw_abstract_method_error(const methodHandle& resolved_method, 359 const methodHandle& selected_method, 360 Klass *recv_klass, TRAPS); 361 }; 362 #endif // SHARE_INTERPRETER_LINKRESOLVER_HPP 363