1 /*
   2  * Copyright (c) 1997, 2025, 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 #include "cds/archiveUtils.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/defaultMethods.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmClasses.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "compiler/compilationPolicy.hpp"
  33 #include "compiler/compileBroker.hpp"
  34 #include "gc/shared/collectedHeap.inline.hpp"
  35 #include "interpreter/bootstrapInfo.hpp"
  36 #include "interpreter/bytecode.hpp"
  37 #include "interpreter/interpreterRuntime.hpp"
  38 #include "interpreter/linkResolver.hpp"
  39 #include "jvm.h"
  40 #include "logging/log.hpp"
  41 #include "logging/logStream.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "oops/constantPool.inline.hpp"
  44 #include "oops/cpCache.inline.hpp"
  45 #include "oops/instanceKlass.inline.hpp"
  46 #include "oops/klass.inline.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/objArrayKlass.hpp"
  49 #include "oops/objArrayOop.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/resolvedIndyEntry.hpp"
  52 #include "oops/symbolHandle.hpp"
  53 #include "prims/jvmtiExport.hpp"
  54 #include "prims/methodHandles.hpp"
  55 #include "runtime/fieldDescriptor.inline.hpp"
  56 #include "runtime/frame.inline.hpp"
  57 #include "runtime/handles.inline.hpp"
  58 #include "runtime/javaThread.inline.hpp"
  59 #include "runtime/perfData.hpp"
  60 #include "runtime/reflection.hpp"
  61 #include "runtime/safepointVerifiers.hpp"
  62 #include "runtime/sharedRuntime.hpp"
  63 #include "runtime/signature.hpp"
  64 #include "runtime/vmThread.hpp"
  65 #include "utilities/macros.hpp"
  66 #if INCLUDE_JFR
  67 #include "jfr/jfr.hpp"
  68 #endif
  69 
  70 //------------------------------------------------------------------------------------------------------------------------
  71 // Implementation of CallInfo
  72 
  73 
  74 void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
  75   int vtable_index = Method::nonvirtual_vtable_index;
  76   set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  77 }
  78 
  79 
  80 void CallInfo::set_interface(Klass* resolved_klass,
  81                              const methodHandle& resolved_method,
  82                              const methodHandle& selected_method,
  83                              int itable_index, TRAPS) {
  84   // This is only called for interface methods. If the resolved_method
  85   // comes from java/lang/Object, it can be the subject of a virtual call, so
  86   // we should pick the vtable index from the resolved method.
  87   // In that case, the caller must call set_virtual instead of set_interface.
  88   assert(resolved_method->method_holder()->is_interface(), "");
  89   assert(itable_index == resolved_method->itable_index(), "");
  90   set_common(resolved_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  91 }
  92 
  93 void CallInfo::set_virtual(Klass* resolved_klass,
  94                            const methodHandle& resolved_method,
  95                            const methodHandle& selected_method,
  96                            int vtable_index, TRAPS) {
  97   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  98   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  99   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
 100   set_common(resolved_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
 101   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
 102 }
 103 
 104 void CallInfo::set_handle(Klass* resolved_klass,
 105                           const methodHandle& resolved_method,
 106                           Handle resolved_appendix, TRAPS) {
 107   guarantee(resolved_method.not_null(), "resolved method is null");
 108   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 109          resolved_method->is_compiled_lambda_form(),
 110          "linkMethod must return one of these");
 111   int vtable_index = Method::nonvirtual_vtable_index;
 112   assert(!resolved_method->has_vtable_index(), "");
 113   set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 114   _resolved_appendix = resolved_appendix;
 115 }
 116 
 117 // Redefinition safepoint may have updated the method. Make sure the new version of the method is returned.
 118 // Callers are responsible for not safepointing and storing this method somewhere safe where redefinition
 119 // can replace it if runs again.  Safe places are constant pool cache and code cache metadata.
 120 // The old method is safe in CallInfo since its a methodHandle (it won't get deleted), and accessed with these
 121 // accessors.
 122 Method* CallInfo::resolved_method() const {
 123   if (JvmtiExport::can_hotswap_or_post_breakpoint() && _resolved_method->is_old()) {
 124     return _resolved_method->get_new_method();
 125   } else {
 126     return _resolved_method();
 127   }
 128 }
 129 
 130 Method* CallInfo::selected_method() const {
 131   if (JvmtiExport::can_hotswap_or_post_breakpoint() && _selected_method->is_old()) {
 132     return _selected_method->get_new_method();
 133   } else {
 134     return _selected_method();
 135   }
 136 }
 137 
 138 void CallInfo::set_common(Klass* resolved_klass,
 139                           const methodHandle& resolved_method,
 140                           const methodHandle& selected_method,
 141                           CallKind kind,
 142                           int index,
 143                           TRAPS) {
 144   if (selected_method.not_null()) {
 145     assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 146   }
 147   _resolved_klass  = resolved_klass;
 148   _resolved_method = resolved_method;
 149   _selected_method = selected_method;
 150   _call_kind       = kind;
 151   _call_index      = index;
 152   _resolved_appendix = Handle();
 153   DEBUG_ONLY(verify());  // verify before making side effects
 154 
 155   if (selected_method.not_null()) {
 156     CompilationPolicy::compile_if_required(selected_method, THREAD);
 157   }
 158 }
 159 
 160 // utility query for unreflecting a method
 161 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
 162   Klass* resolved_method_holder = resolved_method->method_holder();
 163   if (resolved_klass == nullptr) { // 2nd argument defaults to holder of 1st
 164     resolved_klass = resolved_method_holder;
 165   }
 166   _resolved_klass  = resolved_klass;
 167   _resolved_method = methodHandle(THREAD, resolved_method);
 168   _selected_method = methodHandle(THREAD, resolved_method);
 169   // classify:
 170   CallKind kind = CallInfo::unknown_kind;
 171   int index = resolved_method->vtable_index();
 172   if (resolved_method->can_be_statically_bound()) {
 173     kind = CallInfo::direct_call;
 174   } else if (!resolved_method_holder->is_interface()) {
 175     // Could be an Object method inherited into an interface, but still a vtable call.
 176     kind = CallInfo::vtable_call;
 177   } else if (!resolved_klass->is_interface()) {
 178     // A default or miranda method.  Compute the vtable index.
 179     index = LinkResolver::vtable_index_of_interface_method(resolved_klass, _resolved_method);
 180     assert(index >= 0 , "we should have valid vtable index at this point");
 181 
 182     kind = CallInfo::vtable_call;
 183   } else if (resolved_method->has_vtable_index()) {
 184     // Can occur if an interface redeclares a method of Object.
 185 
 186 #ifdef ASSERT
 187     // Ensure that this is really the case.
 188     Klass* object_klass = vmClasses::Object_klass();
 189     Method * object_resolved_method = object_klass->vtable().method_at(index);
 190     assert(object_resolved_method->name() == resolved_method->name(),
 191       "Object and interface method names should match at vtable index %d, %s != %s",
 192       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
 193     assert(object_resolved_method->signature() == resolved_method->signature(),
 194       "Object and interface method signatures should match at vtable index %d, %s != %s",
 195       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
 196 #endif // ASSERT
 197 
 198     kind = CallInfo::vtable_call;
 199   } else {
 200     // A regular interface call.
 201     kind = CallInfo::itable_call;
 202     index = resolved_method->itable_index();
 203   }
 204   assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
 205   _call_kind  = kind;
 206   _call_index = index;
 207   _resolved_appendix = Handle();
 208   // Find or create a ResolvedMethod instance for this Method*
 209   set_resolved_method_name(CHECK);
 210 
 211   DEBUG_ONLY(verify());
 212 }
 213 
 214 void CallInfo::set_resolved_method_name(TRAPS) {
 215   assert(_resolved_method() != nullptr, "Should already have a Method*");
 216   oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, CHECK);
 217   _resolved_method_name = Handle(THREAD, rmethod_name);
 218 }
 219 
 220 #ifdef ASSERT
 221 void CallInfo::verify() {
 222   switch (call_kind()) {  // the meaning and allowed value of index depends on kind
 223   case CallInfo::direct_call:
 224     if (_call_index == Method::nonvirtual_vtable_index)  break;
 225     // else fall through to check vtable index:
 226   case CallInfo::vtable_call:
 227     assert(resolved_klass()->verify_vtable_index(_call_index), "");
 228     break;
 229   case CallInfo::itable_call:
 230     assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
 231     break;
 232   case CallInfo::unknown_kind:
 233     assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
 234     break;
 235   default:
 236     fatal("Unexpected call kind %d", call_kind());
 237   }
 238 }
 239 #endif // ASSERT
 240 
 241 #ifndef PRODUCT
 242 void CallInfo::print() {
 243   ResourceMark rm;
 244   const char* kindstr;
 245   switch (_call_kind) {
 246   case direct_call: kindstr = "direct";  break;
 247   case vtable_call: kindstr = "vtable";  break;
 248   case itable_call: kindstr = "itable";  break;
 249   default         : kindstr = "unknown"; break;
 250   }
 251   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 252                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 253 }
 254 #endif
 255 
 256 //------------------------------------------------------------------------------------------------------------------------
 257 // Implementation of LinkInfo
 258 
 259 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, Bytecodes::Code code, TRAPS) {
 260    // resolve klass
 261   _resolved_klass = pool->klass_ref_at(index, code, CHECK);
 262 
 263   // Get name, signature, and static klass
 264   _name          = pool->name_ref_at(index, code);
 265   _signature     = pool->signature_ref_at(index, code);
 266   _tag           = pool->tag_ref_at(index, code);
 267   _current_klass = pool->pool_holder();
 268   _current_method = current_method;
 269 
 270   // Coming from the constant pool always checks access
 271   _check_access  = true;
 272   _check_loader_constraints = true;
 273 }
 274 
 275 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, Bytecodes::Code code, TRAPS) {
 276    // resolve klass
 277   _resolved_klass = pool->klass_ref_at(index, code, CHECK);
 278 
 279   // Get name, signature, and static klass
 280   _name          = pool->name_ref_at(index, code);
 281   _signature     = pool->signature_ref_at(index, code);
 282   _tag           = pool->tag_ref_at(index, code);
 283   _current_klass = pool->pool_holder();
 284   _current_method = methodHandle();
 285 
 286   // Coming from the constant pool always checks access
 287   _check_access  = true;
 288   _check_loader_constraints = true;
 289 }
 290 
 291 #ifndef PRODUCT
 292 void LinkInfo::print() {
 293   ResourceMark rm;
 294   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s check_loader_constraints=%s",
 295                 _resolved_klass->name()->as_C_string(),
 296                 _name->as_C_string(),
 297                 _signature->as_C_string(),
 298                 _current_klass == nullptr ? "(none)" : _current_klass->name()->as_C_string(),
 299                 _check_access ? "true" : "false",
 300                 _check_loader_constraints ? "true" : "false");
 301 
 302 }
 303 #endif // PRODUCT
 304 //------------------------------------------------------------------------------------------------------------------------
 305 // Klass resolution
 306 
 307 void LinkResolver::check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS) {
 308   Klass* base_klass = sel_klass;
 309   if (sel_klass->is_objArray_klass()) {
 310     base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();
 311   }
 312   // The element type could be a typeArray - we only need the access
 313   // check if it is a reference to another class.
 314   if (!base_klass->is_instance_klass()) {
 315     return;  // no relevant check to do
 316   }
 317 
 318   Reflection::VerifyClassAccessResults vca_result =
 319     Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true);
 320   if (vca_result != Reflection::ACCESS_OK) {
 321     ResourceMark rm(THREAD);
 322     char* msg = Reflection::verify_class_access_msg(ref_klass,
 323                                                     InstanceKlass::cast(base_klass),
 324                                                     vca_result);
 325 
 326     // Names are all known to be < 64k so we know this formatted message is not excessively large.
 327 
 328     bool same_module = (base_klass->module() == ref_klass->module());
 329     if (msg == nullptr) {
 330       Exceptions::fthrow(
 331         THREAD_AND_LOCATION,
 332         vmSymbols::java_lang_IllegalAccessError(),
 333         "failed to access class %s from class %s (%s%s%s)",
 334         base_klass->external_name(),
 335         ref_klass->external_name(),
 336         (same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(),
 337         (same_module) ? "" : "; ",
 338         (same_module) ? "" : ref_klass->class_in_module_of_loader());
 339     } else {
 340       // Use module specific message returned by verify_class_access_msg().
 341       Exceptions::fthrow(
 342         THREAD_AND_LOCATION,
 343         vmSymbols::java_lang_IllegalAccessError(),
 344         "%s", msg);
 345     }
 346   }
 347 }
 348 
 349 //------------------------------------------------------------------------------------------------------------------------
 350 // Method resolution
 351 //
 352 // According to JVM spec. $5.4.3c & $5.4.3d
 353 
 354 // Look up method in klasses, including static methods
 355 // Then look up local default methods
 356 Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 357                                                bool checkpolymorphism,
 358                                                bool in_imethod_resolve) {
 359   NoSafepointVerifier nsv;  // Method* returned may not be reclaimed
 360 
 361   Klass* klass = link_info.resolved_klass();
 362   Symbol* name = link_info.name();
 363   Symbol* signature = link_info.signature();
 364 
 365   // Ignore overpasses so statics can be found during resolution
 366   Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::skip);
 367 
 368   if (klass->is_array_klass()) {
 369     // Only consider klass and super klass for arrays
 370     return result;
 371   }
 372 
 373   InstanceKlass* ik = InstanceKlass::cast(klass);
 374 
 375   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 376   // ignore static and non-public methods of java.lang.Object,
 377   // like clone and finalize.
 378   if (in_imethod_resolve &&
 379       result != nullptr &&
 380       ik->is_interface() &&
 381       (result->is_static() || !result->is_public()) &&
 382       result->method_holder() == vmClasses::Object_klass()) {
 383     result = nullptr;
 384   }
 385 
 386   // Before considering default methods, check for an overpass in the
 387   // current class if a method has not been found.
 388   if (result == nullptr) {
 389     result = ik->find_method(name, signature);
 390   }
 391 
 392   if (result == nullptr) {
 393     Array<Method*>* default_methods = ik->default_methods();
 394     if (default_methods != nullptr) {
 395       result = InstanceKlass::find_method(default_methods, name, signature);
 396     }
 397   }
 398 
 399   if (checkpolymorphism && result != nullptr) {
 400     vmIntrinsics::ID iid = result->intrinsic_id();
 401     if (MethodHandles::is_signature_polymorphic(iid)) {
 402       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 403       return nullptr;
 404     }
 405   }
 406   return result;
 407 }
 408 
 409 // returns first instance method
 410 // Looks up method in classes, then looks up local default methods
 411 Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 412                                                         Symbol* name,
 413                                                         Symbol* signature,
 414                                                         Klass::PrivateLookupMode private_mode) {
 415   Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
 416 
 417   while (result != nullptr && result->is_static() && result->method_holder()->super() != nullptr) {
 418     Klass* super_klass = result->method_holder()->super();
 419     result = super_klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
 420   }
 421 
 422   if (klass->is_array_klass()) {
 423     // Only consider klass and super klass for arrays
 424     return result;
 425   }
 426 
 427   if (result == nullptr) {
 428     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 429     if (default_methods != nullptr) {
 430       result = InstanceKlass::find_method(default_methods, name, signature);
 431       assert(result == nullptr || !result->is_static(), "static defaults not allowed");
 432     }
 433   }
 434   return result;
 435 }
 436 
 437 int LinkResolver::vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method) {
 438   InstanceKlass* ik = InstanceKlass::cast(klass);
 439   return ik->vtable_index_of_interface_method(resolved_method());
 440 }
 441 
 442 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
 443   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 444 
 445   // Specify 'true' in order to skip default methods when searching the
 446   // interfaces.  Function lookup_method_in_klasses() already looked for
 447   // the method in the default methods table.
 448   return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::DefaultsLookupMode::skip);
 449 }
 450 
 451 Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,
 452                                                 Handle *appendix_result_or_null,
 453                                                 TRAPS) {
 454   ResourceMark rm(THREAD);
 455   Klass* klass = link_info.resolved_klass();
 456   Symbol* name = link_info.name();
 457   Symbol* full_signature = link_info.signature();
 458   LogTarget(Info, methodhandles) lt_mh;
 459 
 460   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 461   log_info(methodhandles)("lookup_polymorphic_method iid=%s %s.%s%s",
 462                           vmIntrinsics::name_at(iid), klass->external_name(),
 463                           name->as_C_string(), full_signature->as_C_string());
 464   if ((klass == vmClasses::MethodHandle_klass() ||
 465        klass == vmClasses::VarHandle_klass()) &&
 466       iid != vmIntrinsics::_none) {
 467     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 468       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 469       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 470       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 471       TempNewSymbol basic_signature =
 472         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg);
 473       log_info(methodhandles)("lookup_polymorphic_method %s %s => basic %s",
 474                               name->as_C_string(),
 475                               full_signature->as_C_string(),
 476                               basic_signature->as_C_string());
 477       Method* result = SystemDictionary::find_method_handle_intrinsic(iid,
 478                                                               basic_signature,
 479                                                               CHECK_NULL);
 480       if (result != nullptr) {
 481         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 482         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
 483         assert(basic_signature == result->signature(), "predict the result signature");
 484         if (lt_mh.is_enabled()) {
 485           LogStream ls(lt_mh);
 486           ls.print("lookup_polymorphic_method => intrinsic ");
 487           result->print_on(&ls);
 488         }
 489       }
 490       return result;
 491     } else if (iid == vmIntrinsics::_invokeGeneric
 492                && THREAD->can_call_java()
 493                && appendix_result_or_null != nullptr) {
 494       // This is a method with type-checking semantics.
 495       // We will ask Java code to spin an adapter method for it.
 496       if (!MethodHandles::enabled()) {
 497         // Make sure the Java part of the runtime has been booted up.
 498         Klass* natives = vmClasses::MethodHandleNatives_klass();
 499         if (natives == nullptr || InstanceKlass::cast(natives)->is_not_initialized()) {
 500           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
 501                                             Handle(),
 502                                             true,
 503                                             CHECK_NULL);
 504         }
 505       }
 506 
 507       Handle appendix;
 508       Method* result = SystemDictionary::find_method_handle_invoker(klass,
 509                                                                     name,
 510                                                                     full_signature,
 511                                                                     link_info.current_klass(),
 512                                                                     &appendix,
 513                                                                     CHECK_NULL);
 514       if (lt_mh.is_enabled()) {
 515         LogStream ls(lt_mh);
 516         ls.print("lookup_polymorphic_method => (via Java) ");
 517         result->print_on(&ls);
 518         ls.print("  lookup_polymorphic_method => appendix = ");
 519         appendix.is_null() ? ls.print_cr("(none)") : appendix->print_on(&ls);
 520       }
 521       if (result != nullptr) {
 522 #ifdef ASSERT
 523         ResourceMark rm(THREAD);
 524 
 525         TempNewSymbol basic_signature =
 526           MethodHandles::lookup_basic_type_signature(full_signature);
 527         int actual_size_of_params = result->size_of_parameters();
 528         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
 529         // +1 for MethodHandle.this, +1 for trailing MethodType
 530         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
 531         if (appendix.not_null())                                   expected_size_of_params += 1;
 532         if (actual_size_of_params != expected_size_of_params) {
 533           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 534           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 535           result->print();
 536         }
 537         assert(actual_size_of_params == expected_size_of_params,
 538                "%d != %d", actual_size_of_params, expected_size_of_params);
 539 #endif //ASSERT
 540 
 541         assert(appendix_result_or_null != nullptr, "");
 542         (*appendix_result_or_null) = appendix;
 543       }
 544       return result;
 545     }
 546   }
 547   return nullptr;
 548 }
 549 
 550 static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass) {
 551   assert(ref_klass->is_instance_klass(), "must be");
 552   assert(sel_klass->is_instance_klass(), "must be");
 553   InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
 554   InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
 555   const char* nest_host_error_1 = ref_ik->nest_host_error();
 556   const char* nest_host_error_2 = sel_ik->nest_host_error();
 557   if (nest_host_error_1 != nullptr || nest_host_error_2 != nullptr) {
 558     ss->print(", (%s%s%s)",
 559               (nest_host_error_1 != nullptr) ? nest_host_error_1 : "",
 560               (nest_host_error_1 != nullptr && nest_host_error_2 != nullptr) ? ", " : "",
 561               (nest_host_error_2 != nullptr) ? nest_host_error_2 : "");
 562   }
 563 }
 564 
 565 void LinkResolver::check_method_accessability(Klass* ref_klass,
 566                                               Klass* resolved_klass,
 567                                               Klass* sel_klass,
 568                                               const methodHandle& sel_method,
 569                                               TRAPS) {
 570 
 571   AccessFlags flags = sel_method->access_flags();
 572 
 573   // Special case:  arrays always override "clone". JVMS 2.15.
 574   // If the resolved klass is an array class, and the declaring class
 575   // is java.lang.Object and the method is "clone", set the flags
 576   // to public.
 577   //
 578   // We'll check for the method name first, as that's most likely
 579   // to be false (so we'll short-circuit out of these tests).
 580   if (sel_method->name() == vmSymbols::clone_name() &&
 581       sel_klass == vmClasses::Object_klass() &&
 582       resolved_klass->is_array_klass()) {
 583     // We need to change "protected" to "public".
 584     assert(flags.is_protected(), "clone not protected?");
 585     u2 new_flags = flags.as_method_flags();
 586     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 587     new_flags = new_flags | JVM_ACC_PUBLIC;
 588     flags.set_flags(new_flags);
 589   }
 590 //  assert(extra_arg_result_or_null != nullptr, "must be able to return extra argument");
 591 
 592   bool can_access = Reflection::verify_member_access(ref_klass,
 593                                                      resolved_klass,
 594                                                      sel_klass,
 595                                                      flags,
 596                                                      true, false, CHECK);
 597   // Any existing exceptions that may have been thrown
 598   // have been allowed to propagate.
 599   if (!can_access) {
 600     ResourceMark rm(THREAD);
 601     stringStream ss;
 602     bool same_module = (sel_klass->module() == ref_klass->module());
 603     ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",
 604              ref_klass->external_name(),
 605              sel_method->is_abstract()  ? "abstract "  : "",
 606              sel_method->is_protected() ? "protected " : "",
 607              sel_method->is_private()   ? "private "   : "",
 608              sel_method->external_name(),
 609              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 610              (same_module) ? "" : "; ",
 611              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 612              );
 613 
 614     // For private access see if there was a problem with nest host
 615     // resolution, and if so report that as part of the message.
 616     if (sel_method->is_private()) {
 617       print_nest_host_error_on(&ss, ref_klass, sel_klass);
 618     }
 619 
 620     // Names are all known to be < 64k so we know this formatted message is not excessively large.
 621     Exceptions::fthrow(THREAD_AND_LOCATION,
 622                        vmSymbols::java_lang_IllegalAccessError(),
 623                        "%s",
 624                        ss.as_string()
 625                        );
 626     return;
 627   }
 628 }
 629 
 630 void LinkResolver::resolve_continuation_enter(CallInfo& callinfo, TRAPS) {
 631   Klass* resolved_klass = vmClasses::Continuation_klass();
 632   Symbol* method_name = vmSymbols::enter_name();
 633   Symbol* method_signature = vmSymbols::continuationEnter_signature();
 634   Klass*  current_klass = resolved_klass;
 635   LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 636   Method* resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK);
 637   callinfo.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
 638 }
 639 
 640 Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
 641                                                 const constantPoolHandle& pool, int index, TRAPS) {
 642   // This method is used only
 643   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 644   // and
 645   // (2) in Bytecode_invoke::static_target
 646   // It appears to fail when applied to an invokeinterface call site.
 647   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 648   // resolve klass
 649   if (code == Bytecodes::_invokedynamic) {
 650     Klass* resolved_klass = vmClasses::MethodHandle_klass();
 651     Symbol* method_name = vmSymbols::invoke_name();
 652     Symbol* method_signature = pool->signature_ref_at(index, code);
 653     Klass*  current_klass = pool->pool_holder();
 654     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 655     return resolve_method(link_info, code, THREAD);
 656   }
 657 
 658   LinkInfo link_info(pool, index, methodHandle(), code, CHECK_NULL);
 659   Klass* resolved_klass = link_info.resolved_klass();
 660 
 661   if (pool->has_preresolution()
 662       || ((resolved_klass == vmClasses::MethodHandle_klass() || resolved_klass == vmClasses::VarHandle_klass()) &&
 663           MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
 664     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 665     if (result != nullptr) {
 666       return result;
 667     }
 668   }
 669 
 670   if (code == Bytecodes::_invokeinterface) {
 671     return resolve_interface_method(link_info, code, THREAD);
 672   } else if (code == Bytecodes::_invokevirtual) {
 673     return resolve_method(link_info, code, THREAD);
 674   } else if (!resolved_klass->is_interface()) {
 675     return resolve_method(link_info, code, THREAD);
 676   } else {
 677     return resolve_interface_method(link_info, code, THREAD);
 678   }
 679 }
 680 
 681 // Check and print a loader constraint violation message for method or interface method
 682 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 683                                                    const methodHandle& resolved_method,
 684                                                    const char* method_type, TRAPS) {
 685   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 686   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 687 
 688   ResourceMark rm(THREAD);
 689   Symbol* failed_type_symbol =
 690     SystemDictionary::check_signature_loaders(link_info.signature(),
 691                                               /*klass_being_linked*/ nullptr, // We are not linking class
 692                                               current_loader,
 693                                               resolved_loader, true);
 694   if (failed_type_symbol != nullptr) {
 695     Klass* current_class = link_info.current_klass();
 696     ClassLoaderData* current_loader_data = current_class->class_loader_data();
 697     assert(current_loader_data != nullptr, "current class has no class loader data");
 698     Klass* resolved_method_class = resolved_method->method_holder();
 699     ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
 700     assert(target_loader_data != nullptr, "resolved method's class has no class loader data");
 701 
 702     stringStream ss;
 703     ss.print("loader constraint violation: when resolving %s '", method_type);
 704     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 705     ss.print("' the class loader %s of the current class, %s,"
 706              " and the class loader %s for the method's defining class, %s, have"
 707              " different Class objects for the type %s used in the signature (%s; %s)",
 708              current_loader_data->loader_name_and_id(),
 709              current_class->name()->as_C_string(),
 710              target_loader_data->loader_name_and_id(),
 711              resolved_method_class->name()->as_C_string(),
 712              failed_type_symbol->as_C_string(),
 713              current_class->class_in_module_of_loader(false, true),
 714              resolved_method_class->class_in_module_of_loader(false, true));
 715     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 716   }
 717 }
 718 
 719 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 720                                                   Klass* current_klass,
 721                                                   Klass* sel_klass, TRAPS) {
 722   Handle ref_loader(THREAD, current_klass->class_loader());
 723   Handle sel_loader(THREAD, sel_klass->class_loader());
 724 
 725   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 726   Symbol* failed_type_symbol =
 727     SystemDictionary::check_signature_loaders(sig,
 728                                               /*klass_being_linked*/ nullptr, // We are not linking class
 729                                               ref_loader, sel_loader,
 730                                               false);
 731   if (failed_type_symbol != nullptr) {
 732     stringStream ss;
 733     const char* failed_type_name = failed_type_symbol->as_klass_external_name();
 734 
 735     ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
 736              "the class loader %s of the current class, %s, "
 737              "and the class loader %s for the field's defining %s, %s, "
 738              "have different Class objects for type %s (%s; %s)",
 739              field->as_C_string(),
 740              failed_type_name,
 741              current_klass->class_loader_data()->loader_name_and_id(),
 742              current_klass->external_name(),
 743              sel_klass->class_loader_data()->loader_name_and_id(),
 744              sel_klass->external_kind(),
 745              sel_klass->external_name(),
 746              failed_type_name,
 747              current_klass->class_in_module_of_loader(false, true),
 748              sel_klass->class_in_module_of_loader(false, true));
 749     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 750   }
 751 }
 752 
 753 Method* LinkResolver::resolve_method(const LinkInfo& link_info,
 754                                      Bytecodes::Code code, TRAPS) {
 755 
 756   Handle nested_exception;
 757   Klass* resolved_klass = link_info.resolved_klass();
 758 
 759   // 1. For invokevirtual, cannot call an interface method
 760   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 761     ResourceMark rm(THREAD);
 762     char buf[200];
 763     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 764         resolved_klass->external_name());
 765     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 766   }
 767 
 768   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 769   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 770     ResourceMark rm(THREAD);
 771     stringStream ss;
 772     ss.print("Method '");
 773     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 774     ss.print("' must be Methodref constant");
 775     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 776   }
 777 
 778   // 3. lookup method in resolved klass and its super klasses
 779   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 780 
 781   // 4. lookup method in all the interfaces implemented by the resolved klass
 782   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 783     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 784 
 785     if (resolved_method.is_null()) {
 786       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 787       Method* method = lookup_polymorphic_method(link_info, (Handle*)nullptr, THREAD);
 788       resolved_method = methodHandle(THREAD, method);
 789       if (HAS_PENDING_EXCEPTION) {
 790         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 791         CLEAR_PENDING_EXCEPTION;
 792       }
 793     }
 794   }
 795 
 796   // 5. method lookup failed
 797   if (resolved_method.is_null()) {
 798     ResourceMark rm(THREAD);
 799     stringStream ss;
 800     ss.print("'");
 801     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 802     ss.print("'");
 803     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 804                      ss.as_string(), nested_exception, nullptr);
 805   }
 806 
 807   // 6. access checks, access checking may be turned off when calling from within the VM.
 808   Klass* current_klass = link_info.current_klass();
 809   if (link_info.check_access()) {
 810     assert(current_klass != nullptr , "current_klass should not be null");
 811 
 812     // check if method can be accessed by the referring class
 813     check_method_accessability(current_klass,
 814                                resolved_klass,
 815                                resolved_method->method_holder(),
 816                                resolved_method,
 817                                CHECK_NULL);
 818   }
 819   if (link_info.check_loader_constraints()) {
 820     // check loader constraints
 821     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 822   }
 823 
 824   return resolved_method();
 825 }
 826 
 827 static void trace_method_resolution(const char* prefix,
 828                                     Klass* klass,
 829                                     Klass* resolved_klass,
 830                                     Method* method,
 831                                     bool logitables,
 832                                     int index = -1) {
 833 #ifndef PRODUCT
 834   ResourceMark rm;
 835   Log(itables) logi;
 836   LogStream lsi(logi.trace());
 837   Log(vtables) logv;
 838   LogStream lsv(logv.trace());
 839   outputStream* st;
 840   if (logitables) {
 841     st = &lsi;
 842   } else {
 843     st = &lsv;
 844   }
 845   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 846             prefix,
 847             (klass == nullptr ? "<null>" : klass->internal_name()),
 848             resolved_klass->internal_name(),
 849             Method::name_and_sig_as_C_string(resolved_klass,
 850                                              method->name(),
 851                                              method->signature()),
 852             method->method_holder()->internal_name());
 853   method->print_linkage_flags(st);
 854   if (index != -1) {
 855     st->print("vtable_index:%d", index);
 856   }
 857   st->cr();
 858 #endif // PRODUCT
 859 }
 860 
 861 // Do linktime resolution of a method in the interface within the context of the specified bytecode.
 862 Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 863 
 864   Klass* resolved_klass = link_info.resolved_klass();
 865 
 866   // check if klass is interface
 867   if (!resolved_klass->is_interface()) {
 868     ResourceMark rm(THREAD);
 869     char buf[200];
 870     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 871     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 872   }
 873 
 874   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 875   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 876     ResourceMark rm(THREAD);
 877     stringStream ss;
 878     ss.print("Method '");
 879     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 880     ss.print("' must be InterfaceMethodref constant");
 881     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 882   }
 883 
 884   // lookup method in this interface or its super, java.lang.Object
 885   // JDK8: also look for static methods
 886   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 887 
 888   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 889     // lookup method in all the super-interfaces
 890     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 891   }
 892 
 893   if (resolved_method.is_null()) {
 894     // no method found
 895     ResourceMark rm(THREAD);
 896     stringStream ss;
 897     ss.print("'");
 898     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 899     ss.print("'");
 900     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
 901   }
 902 
 903   if (link_info.check_access()) {
 904     // JDK8 adds non-public interface methods, and accessability check requirement
 905     Klass* current_klass = link_info.current_klass();
 906 
 907     assert(current_klass != nullptr , "current_klass should not be null");
 908 
 909     // check if method can be accessed by the referring class
 910     check_method_accessability(current_klass,
 911                                resolved_klass,
 912                                resolved_method->method_holder(),
 913                                resolved_method,
 914                                CHECK_NULL);
 915   }
 916   if (link_info.check_loader_constraints()) {
 917     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 918   }
 919 
 920   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 921     ResourceMark rm(THREAD);
 922     stringStream ss;
 923     ss.print("Expected instance not static method '");
 924     Method::print_external_name(&ss, resolved_klass,
 925                                 resolved_method->name(), resolved_method->signature());
 926     ss.print("'");
 927     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 928   }
 929 
 930   if (log_develop_is_enabled(Trace, itables)) {
 931     char buf[200];
 932     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 933                  Bytecodes::name(code));
 934     trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
 935   }
 936 
 937   return resolved_method();
 938 }
 939 
 940 //------------------------------------------------------------------------------------------------------------------------
 941 // Field resolution
 942 
 943 void LinkResolver::check_field_accessability(Klass* ref_klass,
 944                                              Klass* resolved_klass,
 945                                              Klass* sel_klass,
 946                                              const fieldDescriptor& fd,
 947                                              TRAPS) {
 948   bool can_access = Reflection::verify_member_access(ref_klass,
 949                                                      resolved_klass,
 950                                                      sel_klass,
 951                                                      fd.access_flags(),
 952                                                      true, false, CHECK);
 953   // Any existing exceptions that may have been thrown, for example LinkageErrors
 954   // from nest-host resolution, have been allowed to propagate.
 955   if (!can_access) {
 956     bool same_module = (sel_klass->module() == ref_klass->module());
 957     ResourceMark rm(THREAD);
 958     stringStream ss;
 959     ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",
 960              ref_klass->external_name(),
 961              fd.is_protected() ? "protected " : "",
 962              fd.is_private()   ? "private "   : "",
 963              sel_klass->external_name(),
 964              fd.name()->as_C_string(),
 965              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 966              (same_module) ? "" : "; ",
 967              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 968              );
 969     // For private access see if there was a problem with nest host
 970     // resolution, and if so report that as part of the message.
 971     if (fd.is_private()) {
 972       print_nest_host_error_on(&ss, ref_klass, sel_klass);
 973     }
 974     // Names are all known to be < 64k so we know this formatted message is not excessively large.
 975     Exceptions::fthrow(THREAD_AND_LOCATION,
 976                        vmSymbols::java_lang_IllegalAccessError(),
 977                        "%s",
 978                        ss.as_string()
 979                        );
 980     return;
 981   }
 982 }
 983 
 984 void LinkResolver::resolve_field_access(fieldDescriptor& fd,
 985                                         const constantPoolHandle& pool,
 986                                         int index,
 987                                         const methodHandle& method,
 988                                         Bytecodes::Code byte,
 989                                         StaticMode static_mode, TRAPS) {
 990   LinkInfo link_info(pool, index, method, byte, CHECK);
 991   resolve_field(fd, link_info, byte, static_mode, CHECK);
 992 }
 993 
 994 void LinkResolver::resolve_field(fieldDescriptor& fd,
 995                                  const LinkInfo& link_info,
 996                                  Bytecodes::Code byte, StaticMode static_mode,
 997                                  TRAPS) {
 998   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 999          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
1000          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
1001          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
1002 
1003   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
1004   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
1005   // Check if there's a resolved klass containing the field
1006   Klass* resolved_klass = link_info.resolved_klass();
1007   Symbol* field = link_info.name();
1008   Symbol* sig = link_info.signature();
1009 
1010   // Resolve instance field
1011   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
1012   // check if field exists; i.e., if a klass containing the field def has been selected
1013   if (sel_klass == nullptr) {
1014     ResourceMark rm(THREAD);
1015     stringStream ss;
1016     ss.print("Class %s does not have member field '", resolved_klass->external_name());
1017     sig->print_as_field_external_type(&ss);
1018     ss.print(" %s'", field->as_C_string());
1019     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
1020   }
1021 
1022   // Access checking may be turned off when calling from within the VM.
1023   Klass* current_klass = link_info.current_klass();
1024   if (link_info.check_access()) {
1025 
1026     // check access
1027     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1028 
1029     // check for errors
1030     if (is_static != fd.is_static()) {
1031       ResourceMark rm(THREAD);
1032       char msg[200];
1033       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1034       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1035     }
1036 
1037     // A final field can be modified only
1038     // (1) by methods declared in the class declaring the field and
1039     // (2) by the <clinit> method (in case of a static field)
1040     //     or by the <init> method (in case of an instance field).
1041     if (is_put && fd.access_flags().is_final()) {
1042 
1043       if (sel_klass != current_klass) {
1044         ResourceMark rm(THREAD);
1045         stringStream ss;
1046         ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1047                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1048                 current_klass->external_name());
1049         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1050       }
1051 
1052       if (fd.constants()->pool_holder()->major_version() >= 53) {
1053         Method* m = link_info.current_method();
1054         assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1055         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1056                                                    fd.is_static() &&
1057                                                    !m->is_static_initializer());
1058         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1059                                                      !fd.is_static() &&
1060                                                      !m->is_object_initializer());
1061 
1062         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1063           ResourceMark rm(THREAD);
1064           stringStream ss;
1065           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1066                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1067                    m->name()->as_C_string(),
1068                    is_static ? "<clinit>" : "<init>");
1069           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1070         }
1071       }
1072     }
1073 
1074     // initialize resolved_klass if necessary
1075     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1076     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1077     //
1078     // note 2: we don't want to force initialization if we are just checking
1079     //         if the field access is legal; e.g., during compilation
1080     if (is_static) {
1081       if (static_mode == StaticMode::initialize_klass) {
1082         sel_klass->initialize(CHECK);
1083       } else if (static_mode == StaticMode::initialize_klass_preemptable) {
1084         sel_klass->initialize_preemptable(CHECK);
1085       }
1086     }
1087   }
1088 
1089   if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != nullptr)) {
1090     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1091   }
1092 
1093   // return information. note that the klass is set to the actual klass containing the
1094   // field, otherwise access of static fields in superclasses will not work.
1095 }
1096 
1097 
1098 //------------------------------------------------------------------------------------------------------------------------
1099 // Invoke resolution
1100 //
1101 // Naming conventions:
1102 //
1103 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1104 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1105 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1106 // recv_klass         the receiver klass
1107 
1108 
1109 void LinkResolver::resolve_static_call(CallInfo& result,
1110                                        const LinkInfo& link_info,
1111                                        StaticMode static_mode, TRAPS) {
1112   Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1113 
1114   // The resolved class can change as a result of this resolution.
1115   Klass* resolved_klass = resolved_method->method_holder();
1116 
1117   // Initialize klass (this should only happen if everything is ok)
1118   if (static_mode != StaticMode::dont_initialize_klass && resolved_klass->should_be_initialized()) {
1119     if (static_mode == StaticMode::initialize_klass) {
1120       resolved_klass->initialize(CHECK);
1121     } else if (static_mode == StaticMode::initialize_klass_preemptable) {
1122       resolved_klass->initialize_preemptable(CHECK);
1123     }
1124     // Use updated LinkInfo to reresolve with resolved method holder
1125     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1126                       link_info.current_klass(),
1127                       link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1128                       link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1129     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1130   }
1131 
1132   // setup result
1133   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1134   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1135 }
1136 
1137 // throws linktime exceptions
1138 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1139 
1140   Klass* resolved_klass = link_info.resolved_klass();
1141   Method* resolved_method;
1142   if (!resolved_klass->is_interface()) {
1143     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1144   } else {
1145     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1146   }
1147   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1148 
1149   // check if static
1150   if (!resolved_method->is_static()) {
1151     ResourceMark rm(THREAD);
1152     stringStream ss;
1153     ss.print("Expected static method '");
1154     resolved_method->print_external_name(&ss);
1155     ss.print("'");
1156     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1157   }
1158   return resolved_method;
1159 }
1160 
1161 
1162 void LinkResolver::resolve_special_call(CallInfo& result,
1163                                         Handle recv,
1164                                         const LinkInfo& link_info,
1165                                         TRAPS) {
1166   Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1167   runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1168 }
1169 
1170 void LinkResolver::cds_resolve_special_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1171   resolve_special_call(result, Handle(), link_info, CHECK);
1172 }
1173 
1174 // throws linktime exceptions
1175 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1176 
1177   // Invokespecial is called for multiple special reasons:
1178   // <init>
1179   // local private method invocation, for classes and interfaces
1180   // superclass.method, which can also resolve to a default method
1181   // and the selected method is recalculated relative to the direct superclass
1182   // superinterface.method, which explicitly does not check shadowing
1183   Klass* resolved_klass = link_info.resolved_klass();
1184   Method* resolved_method = nullptr;
1185 
1186   if (!resolved_klass->is_interface()) {
1187     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1188   } else {
1189     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1190   }
1191 
1192   // check if method name is <init>, that it is found in same klass as static type
1193   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1194       resolved_method->method_holder() != resolved_klass) {
1195     ResourceMark rm(THREAD);
1196     stringStream ss;
1197     ss.print("%s: method '", resolved_klass->external_name());
1198     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1199     ss.print(" %s(", resolved_method->name()->as_C_string());
1200     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1201     ss.print(")' not found");
1202     // Names are all known to be < 64k so we know this formatted message is not excessively large.
1203     Exceptions::fthrow(
1204       THREAD_AND_LOCATION,
1205       vmSymbols::java_lang_NoSuchMethodError(),
1206       "%s", ss.as_string());
1207     return nullptr;
1208   }
1209 
1210   // ensure that invokespecial's interface method reference is in
1211   // a direct superinterface, not an indirect superinterface
1212   Klass* current_klass = link_info.current_klass();
1213   if (current_klass != nullptr && resolved_klass->is_interface()) {
1214     InstanceKlass* klass_to_check = InstanceKlass::cast(current_klass);
1215     if (!klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1216       ResourceMark rm(THREAD);
1217       stringStream ss;
1218       ss.print("Interface method reference: '");
1219       resolved_method->print_external_name(&ss);
1220       ss.print("', is in an indirect superinterface of %s",
1221                current_klass->external_name());
1222       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1223     }
1224   }
1225 
1226   // check if not static
1227   if (resolved_method->is_static()) {
1228     ResourceMark rm(THREAD);
1229     stringStream ss;
1230     ss.print("Expecting non-static method '");
1231     resolved_method->print_external_name(&ss);
1232     ss.print("'");
1233     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1234   }
1235 
1236   if (log_develop_is_enabled(Trace, itables)) {
1237     trace_method_resolution("invokespecial resolved method: caller-class:",
1238                             current_klass, resolved_klass, resolved_method, true);
1239   }
1240 
1241   return resolved_method;
1242 }
1243 
1244 // throws runtime exceptions
1245 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1246                                                   const LinkInfo& link_info,
1247                                                   const methodHandle& resolved_method,
1248                                                   Handle recv, TRAPS) {
1249 
1250   Klass* resolved_klass = link_info.resolved_klass();
1251 
1252   // resolved method is selected method unless we have an old-style lookup
1253   // for a superclass method
1254   // Invokespecial for a superinterface, resolved method is selected method,
1255   // no checks for shadowing
1256   methodHandle sel_method(THREAD, resolved_method());
1257 
1258   if (link_info.check_access() &&
1259       // check if the method is not <init>
1260       resolved_method->name() != vmSymbols::object_initializer_name()) {
1261 
1262     Klass* current_klass = link_info.current_klass();
1263 
1264     // Check if the class of the resolved_klass is a superclass
1265     // (not supertype in order to exclude interface classes) of the current class.
1266     // This check is not performed for super.invoke for interface methods
1267     // in super interfaces.
1268     if (current_klass->is_subclass_of(resolved_klass) &&
1269         current_klass != resolved_klass) {
1270       // Lookup super method
1271       Klass* super_klass = current_klass->super();
1272       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1273                                                      resolved_method->name(),
1274                                                      resolved_method->signature(),
1275                                                      Klass::PrivateLookupMode::find);
1276       sel_method = methodHandle(THREAD, instance_method);
1277 
1278       // check if found
1279       if (sel_method.is_null()) {
1280         ResourceMark rm(THREAD);
1281         stringStream ss;
1282         ss.print("'");
1283         resolved_method->print_external_name(&ss);
1284         ss.print("'");
1285         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1286       // check loader constraints if found a different method
1287       } else if (link_info.check_loader_constraints() && sel_method() != resolved_method()) {
1288         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1289       }
1290     }
1291 
1292     // Check that the class of objectref (the receiver) is the current class or interface,
1293     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1294     // throws IllegalAccessError.
1295     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1296     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1297     // a class.  If the sender is an interface, the check has to be performed at runtime.
1298     InstanceKlass* sender = InstanceKlass::cast(current_klass);
1299     if (sender->is_interface() && recv.not_null()) {
1300       Klass* receiver_klass = recv->klass();
1301       if (!receiver_klass->is_subtype_of(sender)) {
1302         ResourceMark rm(THREAD);
1303         char buf[500];
1304         jio_snprintf(buf, sizeof(buf),
1305                      "Receiver class %s must be the current class or a subtype of interface %s",
1306                      receiver_klass->external_name(),
1307                      sender->external_name());
1308         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1309       }
1310     }
1311   }
1312 
1313   // check if not static
1314   if (sel_method->is_static()) {
1315     ResourceMark rm(THREAD);
1316     stringStream ss;
1317     ss.print("Expecting non-static method '");
1318     resolved_method->print_external_name(&ss);
1319     ss.print("'");
1320     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1321   }
1322 
1323   // check if abstract
1324   if (sel_method->is_abstract()) {
1325     ResourceMark rm(THREAD);
1326     stringStream ss;
1327     ss.print("'");
1328     Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1329     ss.print("'");
1330     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1331   }
1332 
1333   if (log_develop_is_enabled(Trace, itables)) {
1334     trace_method_resolution("invokespecial selected method: resolved-class:",
1335                             resolved_klass, resolved_klass, sel_method(), true);
1336   }
1337 
1338   // setup result
1339   result.set_static(resolved_klass, sel_method, CHECK);
1340   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1341 }
1342 
1343 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1344                                         const LinkInfo& link_info,
1345                                         bool check_null_and_abstract, TRAPS) {
1346   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1347   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1348                                  link_info.resolved_klass(),
1349                                  recv, receiver_klass,
1350                                  check_null_and_abstract,
1351                                  /*is_abstract_interpretation*/ false, CHECK);
1352 }
1353 
1354 void LinkResolver::cds_resolve_virtual_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1355   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1356   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1357                                  link_info.resolved_klass(),
1358                                  Handle(), nullptr,
1359                                  /*check_null_and_abstract*/ false,
1360                                  /*is_abstract_interpretation*/ true, CHECK);
1361 }
1362 
1363 // throws linktime exceptions
1364 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1365                                                            TRAPS) {
1366   // normal method resolution
1367   Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1368 
1369   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1370   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1371 
1372   // check if private interface method
1373   Klass* resolved_klass = link_info.resolved_klass();
1374   Klass* current_klass = link_info.current_klass();
1375 
1376   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1377   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1378     ResourceMark rm(THREAD);
1379     stringStream ss;
1380     ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1381     resolved_method->print_external_name(&ss);
1382     ss.print("', caller-class: %s",
1383              (current_klass == nullptr ? "<null>" : current_klass->internal_name()));
1384     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1385   }
1386 
1387   // check if not static
1388   if (resolved_method->is_static()) {
1389     ResourceMark rm(THREAD);
1390     stringStream ss;
1391     ss.print("Expecting non-static method '");
1392     resolved_method->print_external_name(&ss);
1393     ss.print("'");
1394     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1395   }
1396 
1397   if (log_develop_is_enabled(Trace, vtables)) {
1398     trace_method_resolution("invokevirtual resolved method: caller-class:",
1399                             current_klass, resolved_klass, resolved_method, false);
1400   }
1401 
1402   return resolved_method;
1403 }
1404 
1405 // throws runtime exceptions
1406 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1407                                                   const methodHandle& resolved_method,
1408                                                   Klass* resolved_klass,
1409                                                   Handle recv,
1410                                                   Klass* recv_klass,
1411                                                   bool check_null_and_abstract,
1412                                                   bool is_abstract_interpretation,
1413                                                   TRAPS) {
1414   // is_abstract_interpretation is true IFF CDS is resolving method references without
1415   // running any actual bytecode. Therefore, we don't have an actual recv/recv_klass, so
1416   // we cannot check the actual selected_method (which is not needed by CDS anyway).
1417 
1418   // setup default return values
1419   int vtable_index = Method::invalid_vtable_index;
1420   methodHandle selected_method;
1421 
1422   // runtime method resolution
1423   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1424     THROW(vmSymbols::java_lang_NullPointerException());
1425   }
1426 
1427   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1428   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1429   // a missing receiver might result in a bogus lookup.
1430   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1431 
1432   // do lookup based on receiver klass using the vtable index
1433   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1434     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1435     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1436 
1437     if (!is_abstract_interpretation) {
1438       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1439     }
1440   } else {
1441     // at this point we are sure that resolved_method is virtual and not
1442     // a default or miranda method; therefore, it must have a valid vtable index.
1443     assert(!resolved_method->has_itable_index(), "");
1444     vtable_index = resolved_method->vtable_index();
1445     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1446     // methods, or for final methods. Private methods never appear in the vtable
1447     // and never override other methods. As an optimization, final methods are
1448     // never put in the vtable, unless they override an existing method.
1449     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1450     // resolved method, and it can never be changed by an override.
1451     if (vtable_index == Method::nonvirtual_vtable_index) {
1452       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1453       if (!is_abstract_interpretation) {
1454         selected_method = resolved_method;
1455       }
1456     } else {
1457       if (!is_abstract_interpretation) {
1458         selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1459       }
1460     }
1461   }
1462 
1463   if (!is_abstract_interpretation) {
1464     // check if method exists
1465     if (selected_method.is_null()) {
1466       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1467     }
1468 
1469     // check if abstract
1470     if (check_null_and_abstract && selected_method->is_abstract()) {
1471       // Pass arguments for generating a verbose error message.
1472       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1473     }
1474 
1475     if (log_develop_is_enabled(Trace, vtables)) {
1476       trace_method_resolution("invokevirtual selected method: receiver-class:",
1477                               recv_klass, resolved_klass, selected_method(),
1478                               false, vtable_index);
1479     }
1480   }
1481 
1482   // setup result
1483   result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1484   if (selected_method.not_null()) {
1485     JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1486   }
1487 }
1488 
1489 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1490                                           const LinkInfo& link_info,
1491                                           bool check_null_and_abstract, TRAPS) {
1492   // throws linktime exceptions
1493   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1494   methodHandle mh(THREAD, resolved_method);
1495   runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1496                                    recv, recv_klass, check_null_and_abstract,
1497                                    /*is_abstract_interpretation*/ false, CHECK);
1498 }
1499 
1500 void LinkResolver::cds_resolve_interface_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1501   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1502   runtime_resolve_interface_method(result, methodHandle(THREAD, resolved_method), link_info.resolved_klass(),
1503                                    Handle(), nullptr,
1504                                    /*check_null_and_abstract*/ false,
1505                                    /*is_abstract_interpretation*/ true, CHECK);
1506 }
1507 
1508 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1509                                                              TRAPS) {
1510   // normal interface method resolution
1511   Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1512   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1513   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1514 
1515   return resolved_method;
1516 }
1517 
1518 // throws runtime exceptions
1519 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1520                                                     const methodHandle& resolved_method,
1521                                                     Klass* resolved_klass,
1522                                                     Handle recv,
1523                                                     Klass* recv_klass,
1524                                                     bool check_null_and_abstract,
1525                                                     bool is_abstract_interpretation, TRAPS) {
1526   // is_abstract_interpretation -- see comments in runtime_resolve_virtual_method()
1527 
1528   // check if receiver exists
1529   if (check_null_and_abstract && recv.is_null()) {
1530     THROW(vmSymbols::java_lang_NullPointerException());
1531   }
1532 
1533   // check if receiver klass implements the resolved interface
1534   if (!is_abstract_interpretation && !recv_klass->is_subtype_of(resolved_klass)) {
1535     ResourceMark rm(THREAD);
1536     char buf[200];
1537     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1538                  recv_klass->external_name(),
1539                  resolved_klass->external_name());
1540     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1541   }
1542 
1543   methodHandle selected_method;
1544 
1545   if (!is_abstract_interpretation) {
1546     selected_method = resolved_method;
1547   }
1548 
1549   // resolve the method in the receiver class, unless it is private
1550   if (!is_abstract_interpretation && !resolved_method->is_private()) {
1551     // do lookup based on receiver klass
1552     // This search must match the linktime preparation search for itable initialization
1553     // to correctly enforce loader constraints for interface method inheritance.
1554     // Private methods are skipped as the resolved method was not private.
1555     Method* method = lookup_instance_method_in_klasses(recv_klass,
1556                                                        resolved_method->name(),
1557                                                        resolved_method->signature(),
1558                                                        Klass::PrivateLookupMode::skip);
1559     selected_method = methodHandle(THREAD, method);
1560 
1561     if (selected_method.is_null() && !check_null_and_abstract) {
1562       // In theory this is a harmless placeholder value, but
1563       // in practice leaving in null affects the nsk default method tests.
1564       // This needs further study.
1565       selected_method = resolved_method;
1566     }
1567     // check if method exists
1568     if (selected_method.is_null()) {
1569       // Pass arguments for generating a verbose error message.
1570       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1571     }
1572     // check access
1573     // Throw Illegal Access Error if selected_method is not public.
1574     if (!selected_method->is_public()) {
1575       ResourceMark rm(THREAD);
1576       stringStream ss;
1577       ss.print("'");
1578       Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1579       ss.print("'");
1580       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1581     }
1582     // check if abstract
1583     if (check_null_and_abstract && selected_method->is_abstract()) {
1584       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1585     }
1586   }
1587 
1588   if (log_develop_is_enabled(Trace, itables)) {
1589     trace_method_resolution("invokeinterface selected method: receiver-class:",
1590                             recv_klass, resolved_klass, selected_method(), true);
1591   }
1592   // setup result
1593   if (resolved_method->has_vtable_index()) {
1594     int vtable_index = resolved_method->vtable_index();
1595     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1596     assert(is_abstract_interpretation || vtable_index == selected_method->vtable_index(), "sanity check");
1597     result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1598   } else if (resolved_method->has_itable_index()) {
1599     int itable_index = resolved_method->itable_index();
1600     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1601     result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);
1602   } else {
1603     int index = resolved_method->vtable_index();
1604     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1605     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1606     assert(resolved_method->is_private() ||
1607            (resolved_method->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()),
1608            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1609     assert(resolved_method->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1610     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1611     result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);
1612   }
1613   if (!is_abstract_interpretation) {
1614     JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1615   }
1616 }
1617 
1618 
1619 Method* LinkResolver::linktime_resolve_interface_method_or_null(
1620                                                  const LinkInfo& link_info) {
1621   EXCEPTION_MARK;
1622   Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1623   if (HAS_PENDING_EXCEPTION) {
1624     CLEAR_PENDING_EXCEPTION;
1625     return nullptr;
1626   } else {
1627     return method_result;
1628   }
1629 }
1630 
1631 Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1632                                                  const LinkInfo& link_info) {
1633   EXCEPTION_MARK;
1634   Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1635   if (HAS_PENDING_EXCEPTION) {
1636     CLEAR_PENDING_EXCEPTION;
1637     return nullptr;
1638   } else {
1639     return method_result;
1640   }
1641 }
1642 
1643 Method* LinkResolver::resolve_virtual_call_or_null(
1644                                                  Klass* receiver_klass,
1645                                                  const LinkInfo& link_info) {
1646   EXCEPTION_MARK;
1647   CallInfo info;
1648   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1649   if (HAS_PENDING_EXCEPTION) {
1650     CLEAR_PENDING_EXCEPTION;
1651     return nullptr;
1652   }
1653   return info.selected_method();
1654 }
1655 
1656 Method* LinkResolver::resolve_interface_call_or_null(
1657                                                  Klass* receiver_klass,
1658                                                  const LinkInfo& link_info) {
1659   EXCEPTION_MARK;
1660   CallInfo info;
1661   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1662   if (HAS_PENDING_EXCEPTION) {
1663     CLEAR_PENDING_EXCEPTION;
1664     return nullptr;
1665   }
1666   return info.selected_method();
1667 }
1668 
1669 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1670                                                const LinkInfo& link_info) {
1671   EXCEPTION_MARK;
1672   CallInfo info;
1673   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1674                        /*check_null_or_abstract*/false, THREAD);
1675   if (HAS_PENDING_EXCEPTION) {
1676     CLEAR_PENDING_EXCEPTION;
1677     return Method::invalid_vtable_index;
1678   }
1679   return info.vtable_index();
1680 }
1681 
1682 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1683   EXCEPTION_MARK;
1684   CallInfo info;
1685   resolve_static_call(info, link_info, StaticMode::dont_initialize_klass, THREAD);
1686   if (HAS_PENDING_EXCEPTION) {
1687     CLEAR_PENDING_EXCEPTION;
1688     return nullptr;
1689   }
1690   return info.selected_method();
1691 }
1692 
1693 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1694   EXCEPTION_MARK;
1695   CallInfo info;
1696   resolve_special_call(info, Handle(), link_info, THREAD);
1697   if (HAS_PENDING_EXCEPTION) {
1698     CLEAR_PENDING_EXCEPTION;
1699     return nullptr;
1700   }
1701   return info.selected_method();
1702 }
1703 
1704 
1705 
1706 //------------------------------------------------------------------------------------------------------------------------
1707 // ConstantPool entries
1708 
1709 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, StaticMode static_mode, TRAPS) {
1710   switch (byte) {
1711     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, static_mode, CHECK); break;
1712     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index,              CHECK); break;
1713     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index,              CHECK); break;
1714     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index,              CHECK); break;
1715     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index,              CHECK); break;
1716     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index,              CHECK); break;
1717     default                         :                                                                         break;
1718   }
1719   return;
1720 }
1721 
1722 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1723                              const methodHandle& attached_method,
1724                              Bytecodes::Code byte, TRAPS) {
1725   Klass* defc = attached_method->method_holder();
1726   Symbol* name = attached_method->name();
1727   Symbol* type = attached_method->signature();
1728   LinkInfo link_info(defc, name, type);
1729   switch(byte) {
1730     case Bytecodes::_invokevirtual:
1731       resolve_virtual_call(result, recv, recv->klass(), link_info,
1732                            /*check_null_and_abstract=*/true, CHECK);
1733       break;
1734     case Bytecodes::_invokeinterface:
1735       resolve_interface_call(result, recv, recv->klass(), link_info,
1736                              /*check_null_and_abstract=*/true, CHECK);
1737       break;
1738     case Bytecodes::_invokestatic:
1739       resolve_static_call(result, link_info, StaticMode::dont_initialize_klass, CHECK);
1740       break;
1741     case Bytecodes::_invokespecial:
1742       resolve_special_call(result, recv, link_info, CHECK);
1743       break;
1744     default:
1745       fatal("bad call: %s", Bytecodes::name(byte));
1746       break;
1747   }
1748 }
1749 
1750 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, StaticMode static_mode, TRAPS) {
1751   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1752   resolve_static_call(result, link_info, static_mode, CHECK);
1753 }
1754 
1755 
1756 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1757                                          const constantPoolHandle& pool, int index, TRAPS) {
1758   LinkInfo link_info(pool, index, Bytecodes::_invokespecial, CHECK);
1759   resolve_special_call(result, recv, link_info, CHECK);
1760 }
1761 
1762 
1763 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1764                                           const constantPoolHandle& pool, int index,
1765                                           TRAPS) {
1766 
1767   LinkInfo link_info(pool, index, Bytecodes::_invokevirtual, CHECK);
1768   Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1769   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1770 }
1771 
1772 
1773 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1774   LinkInfo link_info(pool, index, Bytecodes::_invokeinterface, CHECK);
1775   Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1776   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1777 }
1778 
1779 bool LinkResolver::resolve_previously_linked_invokehandle(CallInfo& result, const LinkInfo& link_info, const constantPoolHandle& pool, int index, TRAPS) {
1780   ResolvedMethodEntry* method_entry = pool->cache()->resolved_method_entry_at(index);
1781   if (method_entry->method() != nullptr) {
1782     Klass* resolved_klass = link_info.resolved_klass();
1783     methodHandle method(THREAD, method_entry->method());
1784     Handle     appendix(THREAD, pool->cache()->appendix_if_resolved(method_entry));
1785     result.set_handle(resolved_klass, method, appendix, CHECK_false);
1786     JFR_ONLY(Jfr::on_resolution(result, CHECK_false);)
1787     return true;
1788   } else {
1789     return false;
1790   }
1791 }
1792 
1793 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1794 
1795   PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokehandle_time(),
1796                             ClassLoader::perf_resolve_invokehandle_count());
1797 
1798   LinkInfo link_info(pool, index, Bytecodes::_invokehandle, CHECK);
1799   if (log_is_enabled(Info, methodhandles)) {
1800     ResourceMark rm(THREAD);
1801     log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1802                             link_info.signature()->as_C_string());
1803   }
1804   { // Check if the call site has been bound already, and short circuit:
1805     bool is_done = resolve_previously_linked_invokehandle(result, link_info, pool, index, CHECK);
1806     if (is_done) return;
1807   }
1808   resolve_handle_call(result, link_info, CHECK);
1809 }
1810 
1811 void LinkResolver::resolve_handle_call(CallInfo& result,
1812                                        const LinkInfo& link_info,
1813                                        TRAPS) {
1814   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1815   Klass* resolved_klass = link_info.resolved_klass();
1816   assert(resolved_klass == vmClasses::MethodHandle_klass() ||
1817          resolved_klass == vmClasses::VarHandle_klass(), "");
1818   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1819   Handle resolved_appendix;
1820   Method* m = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1821   methodHandle resolved_method(THREAD, m);
1822 
1823   if (link_info.check_access()) {
1824     Symbol* name = link_info.name();
1825     vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
1826     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
1827       // Check if method can be accessed by the referring class.
1828       // MH.linkTo* invocations are not rewritten to invokehandle.
1829       assert(iid == vmIntrinsicID::_invokeBasic, "%s", vmIntrinsics::name_at(iid));
1830 
1831       Klass* current_klass = link_info.current_klass();
1832       assert(current_klass != nullptr , "current_klass should not be null");
1833       check_method_accessability(current_klass,
1834                                  resolved_klass,
1835                                  resolved_method->method_holder(),
1836                                  resolved_method,
1837                                  CHECK);
1838     } else {
1839       // Java code is free to arbitrarily link signature-polymorphic invokers.
1840       assert(iid == vmIntrinsics::_invokeGeneric, "not an invoker: %s", vmIntrinsics::name_at(iid));
1841       assert(MethodHandles::is_signature_polymorphic_public_name(resolved_klass, name), "not public");
1842     }
1843   }
1844   result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK);
1845   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1846 }
1847 
1848 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1849   PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(),
1850                             ClassLoader::perf_resolve_invokedynamic_count());
1851 
1852   int pool_index = pool->resolved_indy_entry_at(indy_index)->constant_pool_index();
1853 
1854   // Resolve the bootstrap specifier (BSM + optional arguments).
1855   BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1856 
1857   // Check if CallSite has been bound already or failed already, and short circuit:
1858   {
1859     bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1860     if (is_done) return;
1861   }
1862 
1863   // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1864   // reference to a method handle which will be the bootstrap method for a dynamic
1865   // call site.  If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1866   // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1867   // method's CP index for the CONSTANT_MethodHandle_info.
1868   // Any subsequent invokedynamic instruction which shares
1869   // this bootstrap method will encounter the resolution of MethodHandleInError.
1870 
1871   resolve_dynamic_call(result, bootstrap_specifier, CHECK);
1872 
1873   LogTarget(Debug, methodhandles, indy) lt_indy;
1874   if (lt_indy.is_enabled()) {
1875     LogStream ls(lt_indy);
1876     bootstrap_specifier.print_msg_on(&ls, "resolve_invokedynamic");
1877   }
1878 
1879   // The returned linkage result is provisional up to the moment
1880   // the interpreter or runtime performs a serialized check of
1881   // the relevant ResolvedIndyEntry::method field.  This is done by the caller
1882   // of this method, via CPC::set_dynamic_call, which uses
1883   // an ObjectLocker to do the final serialization of updates
1884   // to ResolvedIndyEntry state, including method.
1885 
1886   // Log dynamic info to CDS classlist.
1887   ArchiveUtils::log_to_classlist(&bootstrap_specifier, CHECK);
1888 }
1889 
1890 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1891                                         BootstrapInfo& bootstrap_specifier,
1892                                         TRAPS) {
1893   // JSR 292:  this must resolve to an implicitly generated method
1894   // such as MH.linkToCallSite(*...) or some other call-site shape.
1895   // The appendix argument is likely to be a freshly-created CallSite.
1896   // It may also be a MethodHandle from an unwrapped ConstantCallSite,
1897   // or any other reference.  The resolved_method as well as the appendix
1898   // are both recorded together via CallInfo::set_handle.
1899   SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1900   Exceptions::wrap_dynamic_exception(/* is_indy */ true, THREAD);
1901 
1902   if (HAS_PENDING_EXCEPTION) {
1903     if (!PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) {
1904       // Let any random low-level IE or SOE or OOME just bleed through.
1905       // Basically we pretend that the bootstrap method was never called,
1906       // if it fails this way:  We neither record a successful linkage,
1907       // nor do we memorize a LE for posterity.
1908       return;
1909     }
1910     // JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve
1911     // a symbolic reference fails because an error is thrown that is an
1912     // instance of LinkageError (or a subclass), then subsequent attempts to
1913     // resolve the reference always fail with the same error that was thrown
1914     // as a result of the initial resolution attempt.
1915     bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK);
1916     if (!recorded_res_status) {
1917       // Another thread got here just before we did.  So, either use the method
1918       // that it resolved or throw the LinkageError exception that it threw.
1919       bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1920       if (is_done) return;
1921     }
1922     assert(bootstrap_specifier.pool()->resolved_indy_entry_at(bootstrap_specifier.indy_index())->resolution_failed(),
1923           "Resolution should have failed");
1924   }
1925 
1926   bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK);
1927   // Exceptions::wrap_dynamic_exception not used because
1928   // set_handle doesn't throw linkage errors
1929   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1930 }
1931 
1932 // Selected method is abstract.
1933 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1934                                                const methodHandle& selected_method,
1935                                                Klass *recv_klass, TRAPS) {
1936   Klass *resolved_klass = resolved_method->method_holder();
1937   ResourceMark rm(THREAD);
1938   stringStream ss;
1939 
1940   if (recv_klass != nullptr) {
1941     ss.print("Receiver class %s does not define or inherit an "
1942              "implementation of the",
1943              recv_klass->external_name());
1944   } else {
1945     ss.print("Missing implementation of");
1946   }
1947 
1948   assert(resolved_method.not_null(), "Sanity");
1949   ss.print(" resolved method '%s%s",
1950            resolved_method->is_abstract() ? "abstract " : "",
1951            resolved_method->is_private()  ? "private "  : "");
1952   resolved_method->signature()->print_as_signature_external_return_type(&ss);
1953   ss.print(" %s(", resolved_method->name()->as_C_string());
1954   resolved_method->signature()->print_as_signature_external_parameters(&ss);
1955   ss.print(")' of %s %s.",
1956            resolved_klass->external_kind(),
1957            resolved_klass->external_name());
1958 
1959   if (selected_method.not_null() && !(resolved_method == selected_method)) {
1960     ss.print(" Selected method is '%s%s",
1961              selected_method->is_abstract() ? "abstract " : "",
1962              selected_method->is_private()  ? "private "  : "");
1963     selected_method->print_external_name(&ss);
1964     ss.print("'.");
1965   }
1966 
1967   THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1968 }