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