1 /* 2 * Copyright (c) 1997, 2024, 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 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_name; // Object holding the ResolvedMethodName 59 60 void set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS); 61 void set_interface(Klass* resolved_klass, 62 const methodHandle& resolved_method, 63 const methodHandle& selected_method, 64 int itable_index, TRAPS); 65 void set_virtual(Klass* resolved_klass, 66 const methodHandle& resolved_method, 67 const methodHandle& selected_method, 68 int vtable_index, TRAPS); 69 void set_handle(Klass* resolved_klass, 70 const methodHandle& resolved_method, 71 Handle resolved_appendix, TRAPS); 72 void set_common(Klass* resolved_klass, 73 const methodHandle& resolved_method, 74 const methodHandle& selected_method, 75 CallKind kind, 76 int index, TRAPS); 77 78 friend class BootstrapInfo; 79 friend class LinkResolver; 80 81 public: 82 CallInfo() { 83 #ifndef PRODUCT 84 _call_kind = CallInfo::unknown_kind; 85 _call_index = Method::garbage_vtable_index; 86 #endif //PRODUCT 87 } 88 89 // utility to extract an effective CallInfo from a method and an optional receiver limit 90 // does not queue the method for compilation. This also creates a ResolvedMethodName 91 // object for the resolved_method. 92 CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS); 93 94 Klass* resolved_klass() const { return _resolved_klass; } 95 Method* resolved_method() const; 96 Method* selected_method() const; 97 Handle resolved_appendix() const { return _resolved_appendix; } 98 Handle resolved_method_name() const { return _resolved_method_name; } 99 // Materialize a java.lang.invoke.ResolvedMethodName for this resolved_method 100 void set_resolved_method_name(TRAPS); 101 102 CallKind call_kind() const { return _call_kind; } 103 int vtable_index() const { 104 // Even for interface calls the vtable index could be non-negative. 105 // See CallInfo::set_interface. 106 assert(has_vtable_index() || is_statically_bound(), ""); 107 assert(call_kind() == vtable_call || call_kind() == direct_call, ""); 108 // The returned value is < 0 if the call is statically bound. 109 // But, the returned value may be >= 0 even if the kind is direct_call. 110 // It is up to the caller to decide which way to go. 111 return _call_index; 112 } 113 int itable_index() const { 114 assert(call_kind() == itable_call, ""); 115 // The returned value is always >= 0, a valid itable index. 116 return _call_index; 117 } 118 119 // debugging 120 #ifdef ASSERT 121 bool has_vtable_index() const { return _call_index >= 0 && _call_kind != CallInfo::itable_call; } 122 bool is_statically_bound() const { return _call_index == Method::nonvirtual_vtable_index; } 123 #endif //ASSERT 124 void verify() PRODUCT_RETURN; 125 void print() PRODUCT_RETURN; 126 }; 127 128 129 // Condensed information from constant pool to use to resolve the method or field. 130 // resolved_klass = specified class (i.e., static receiver class) 131 // current_klass = sending method holder (i.e., class containing the method 132 // containing the call being resolved) 133 // current_method = sending method (relevant for field resolution) 134 class LinkInfo : public StackObj { 135 Symbol* _name; // extracted from JVM_CONSTANT_NameAndType 136 Symbol* _signature; 137 Klass* _resolved_klass; // class that the constant pool entry points to 138 Klass* _current_klass; // class that owns the constant pool 139 methodHandle _current_method; // sending method 140 bool _check_access; 141 bool _check_loader_constraints; 142 constantTag _tag; 143 144 public: 145 enum class AccessCheck { required, skip }; 146 enum class LoaderConstraintCheck { required, skip }; 147 148 LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, Bytecodes::Code code, TRAPS); 149 LinkInfo(const constantPoolHandle& pool, int index, Bytecodes::Code code, TRAPS); 150 151 // Condensed information from other call sites within the vm. 152 LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, Klass* current_klass, 153 AccessCheck check_access = AccessCheck::required, 154 LoaderConstraintCheck check_loader_constraints = LoaderConstraintCheck::required, 155 constantTag tag = JVM_CONSTANT_Invalid) : 156 _name(name), 157 _signature(signature), 158 _resolved_klass(resolved_klass), 159 _current_klass(current_klass), 160 _current_method(methodHandle()), 161 _check_access(check_access == AccessCheck::required), 162 _check_loader_constraints(check_loader_constraints == LoaderConstraintCheck::required), 163 _tag(tag) { 164 assert(_resolved_klass != nullptr, "must always have a resolved_klass"); 165 } 166 167 LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, const methodHandle& current_method, 168 AccessCheck check_access = AccessCheck::required, 169 LoaderConstraintCheck check_loader_constraints = LoaderConstraintCheck::required, 170 constantTag tag = JVM_CONSTANT_Invalid) : 171 LinkInfo(resolved_klass, name, signature, current_method->method_holder(), check_access, check_loader_constraints, tag) { 172 _current_method = current_method; 173 } 174 175 // Case where we just find the method and don't check access against the current class, used by JavaCalls 176 LinkInfo(Klass* resolved_klass, Symbol*name, Symbol* signature) : 177 LinkInfo(resolved_klass, name, signature, nullptr, AccessCheck::skip, LoaderConstraintCheck::skip, 178 JVM_CONSTANT_Invalid) {} 179 180 // accessors 181 Symbol* name() const { return _name; } 182 Symbol* signature() const { return _signature; } 183 Klass* resolved_klass() const { return _resolved_klass; } 184 Klass* current_klass() const { return _current_klass; } 185 Method* current_method() const { return _current_method(); } 186 constantTag tag() const { return _tag; } 187 bool check_access() const { return _check_access; } 188 bool check_loader_constraints() const { return _check_loader_constraints; } 189 void print() PRODUCT_RETURN; 190 }; 191 192 enum class StaticMode : uint8_t { 193 dont_initialize_klass, 194 initialize_klass, 195 initialize_klass_preemptable 196 }; 197 198 // Link information for getfield/putfield & getstatic/putstatic bytecodes 199 // is represented using a fieldDescriptor. 200 201 // The LinkResolver is used to resolve constant-pool references at run-time. 202 // It does all necessary link-time checks & throws exceptions if necessary. 203 204 class LinkResolver: AllStatic { 205 friend class klassVtable; 206 friend class klassItable; 207 208 private: 209 210 static Method* lookup_method_in_klasses(const LinkInfo& link_info, 211 bool checkpolymorphism, 212 bool in_imethod_resolve); 213 static Method* lookup_method_in_interfaces(const LinkInfo& link_info); 214 215 static Method* lookup_polymorphic_method(const LinkInfo& link_info, 216 Handle *appendix_result_or_null, TRAPS); 217 JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod() 218 // Not Linktime so doesn't take LinkInfo 219 static Method* lookup_instance_method_in_klasses (Klass* klass, Symbol* name, Symbol* signature, 220 Klass::PrivateLookupMode private_mode); 221 JVMCI_ONLY(private:) 222 223 // Similar loader constraint checking functions that throw 224 // LinkageError with descriptive message. 225 static void check_method_loader_constraints(const LinkInfo& link_info, 226 const methodHandle& resolved_method, 227 const char* method_type, TRAPS); 228 static void check_field_loader_constraints(Symbol* field, Symbol* sig, 229 Klass* current_klass, 230 Klass* sel_klass, TRAPS); 231 232 static Method* resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS); 233 static Method* resolve_method (const LinkInfo& link_info, Bytecodes::Code code, TRAPS); 234 235 static Method* linktime_resolve_static_method (const LinkInfo& link_info, TRAPS); 236 static Method* linktime_resolve_special_method (const LinkInfo& link_info, TRAPS); 237 static Method* linktime_resolve_virtual_method (const LinkInfo& link_info, TRAPS); 238 static Method* linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS); 239 240 static void runtime_resolve_special_method (CallInfo& result, 241 const LinkInfo& link_info, 242 const methodHandle& resolved_method, 243 Handle recv, TRAPS); 244 245 static void runtime_resolve_virtual_method (CallInfo& result, 246 const methodHandle& resolved_method, 247 Klass* resolved_klass, 248 Handle recv, 249 Klass* recv_klass, 250 bool check_null_and_abstract, 251 bool is_abstract_interpretation, TRAPS); 252 static void runtime_resolve_interface_method (CallInfo& result, 253 const methodHandle& resolved_method, 254 Klass* resolved_klass, 255 Handle recv, 256 Klass* recv_klass, 257 bool check_null_and_abstract, 258 bool is_abstract_interpretation, TRAPS); 259 260 static bool resolve_previously_linked_invokehandle(CallInfo& result, 261 const LinkInfo& link_info, 262 const constantPoolHandle& pool, 263 int index, TRAPS); 264 265 static void check_field_accessability(Klass* ref_klass, 266 Klass* resolved_klass, 267 Klass* sel_klass, 268 const fieldDescriptor& fd, TRAPS); 269 static void check_method_accessability(Klass* ref_klass, 270 Klass* resolved_klass, 271 Klass* sel_klass, 272 const methodHandle& sel_method, TRAPS); 273 274 // runtime resolving from constant pool 275 static void resolve_invokestatic (CallInfo& result, 276 const constantPoolHandle& pool, int index, StaticMode mode, TRAPS); 277 static void resolve_invokespecial (CallInfo& result, Handle recv, 278 const constantPoolHandle& pool, int index, TRAPS); 279 static void resolve_invokevirtual (CallInfo& result, Handle recv, 280 const constantPoolHandle& pool, int index, TRAPS); 281 static void resolve_invokeinterface(CallInfo& result, Handle recv, 282 const constantPoolHandle& pool, int index, TRAPS); 283 static void resolve_invokedynamic (CallInfo& result, 284 const constantPoolHandle& pool, int index, TRAPS); 285 static void resolve_invokehandle (CallInfo& result, 286 const constantPoolHandle& pool, int index, TRAPS); 287 public: 288 // constant pool resolving 289 static void check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS); 290 291 // static resolving calls (will not run any Java code); 292 // used only from Bytecode_invoke::static_target 293 static Method* resolve_method_statically(Bytecodes::Code code, 294 const constantPoolHandle& pool, 295 int index, TRAPS); 296 297 static void resolve_continuation_enter(CallInfo& callinfo, TRAPS); 298 299 static void resolve_field_access(fieldDescriptor& result, 300 const constantPoolHandle& pool, 301 int index, 302 const methodHandle& method, 303 Bytecodes::Code byte, 304 StaticMode mode, TRAPS); 305 static void resolve_field_access(fieldDescriptor& result, 306 const constantPoolHandle& pool, 307 int index, 308 const methodHandle& method, 309 Bytecodes::Code byte, TRAPS) { 310 resolve_field_access(result, pool, index, method, byte, 311 StaticMode::initialize_klass, THREAD); 312 } 313 static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info, 314 Bytecodes::Code access_kind, 315 StaticMode mode, TRAPS); 316 317 static void resolve_static_call (CallInfo& result, 318 const LinkInfo& link_info, 319 StaticMode mode, TRAPS); 320 static void resolve_special_call (CallInfo& result, 321 Handle recv, 322 const LinkInfo& link_info, 323 TRAPS); 324 static void resolve_virtual_call (CallInfo& result, Handle recv, Klass* recv_klass, 325 const LinkInfo& link_info, 326 bool check_null_and_abstract, TRAPS); 327 static void resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass, 328 const LinkInfo& link_info, 329 bool check_null_and_abstract, TRAPS); 330 static void resolve_handle_call (CallInfo& result, 331 const LinkInfo& link_info, TRAPS); 332 static void resolve_dynamic_call (CallInfo& result, 333 BootstrapInfo& bootstrap_specifier, TRAPS); 334 335 static void cds_resolve_virtual_call (CallInfo& result, const LinkInfo& link_info, TRAPS); 336 static void cds_resolve_interface_call(CallInfo& result, const LinkInfo& link_info, TRAPS); 337 static void cds_resolve_special_call (CallInfo& result, const LinkInfo& link_info, TRAPS); 338 339 // same as above for compile-time resolution; but returns null handle instead of throwing 340 // an exception on error also, does not initialize klass (i.e., no side effects) 341 static Method* resolve_virtual_call_or_null(Klass* receiver_klass, 342 const LinkInfo& link_info); 343 static Method* resolve_interface_call_or_null(Klass* receiver_klass, 344 const LinkInfo& link_info); 345 static Method* resolve_static_call_or_null(const LinkInfo& link_info); 346 static Method* resolve_special_call_or_null(const LinkInfo& link_info); 347 348 static int vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method); 349 350 // same as above for compile-time resolution; returns vtable_index if current_klass if linked 351 static int resolve_virtual_vtable_index (Klass* receiver_klass, 352 const LinkInfo& link_info); 353 354 // static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful) 355 static Method* linktime_resolve_virtual_method_or_null (const LinkInfo& link_info); 356 static Method* linktime_resolve_interface_method_or_null(const LinkInfo& link_info); 357 358 // runtime resolving from constant pool 359 static void resolve_invoke(CallInfo& result, Handle recv, 360 const constantPoolHandle& pool, int index, 361 Bytecodes::Code byte, StaticMode static_mode, TRAPS); 362 static void resolve_invoke(CallInfo& result, Handle recv, 363 const constantPoolHandle& pool, int index, 364 Bytecodes::Code byte, TRAPS) { 365 resolve_invoke(result, recv, pool, index, byte, StaticMode::initialize_klass, THREAD); 366 } 367 368 // runtime resolving from attached method 369 static void resolve_invoke(CallInfo& result, Handle& recv, 370 const methodHandle& attached_method, 371 Bytecodes::Code byte, TRAPS); 372 373 // Only resolved method known. 374 static void throw_abstract_method_error(const methodHandle& resolved_method, TRAPS) { 375 throw_abstract_method_error(resolved_method, methodHandle(), nullptr, CHECK); 376 } 377 // Resolved method and receiver klass know. 378 static void throw_abstract_method_error(const methodHandle& resolved_method, Klass *recv_klass, TRAPS) { 379 throw_abstract_method_error(resolved_method, methodHandle(), recv_klass, CHECK); 380 } 381 // Selected method is abstract. 382 static void throw_abstract_method_error(const methodHandle& resolved_method, 383 const methodHandle& selected_method, 384 Klass *recv_klass, TRAPS); 385 }; 386 #endif // SHARE_INTERPRETER_LINKRESOLVER_HPP